The motivation for this is twofold.
Firstly, this makes code a little nicer to read, by virtue of removing a ubiquitous &(...)->node.
Secondly, and more importantly, this allows for the possibility of constructors returning a different instruction type than intended. The ultimate goal here is to return a preallocated "error" instruction when allocation fails, instead of returning NULL and propagating out-of-memory handling to the caller.
From: Zebediah Figura zfigura@codeweavers.com
--- libs/vkd3d-shader/hlsl.c | 10 +++++----- libs/vkd3d-shader/hlsl.h | 2 +- libs/vkd3d-shader/hlsl.y | 6 +++--- libs/vkd3d-shader/hlsl_codegen.c | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 54f3292b..8a8aacfa 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1062,14 +1062,14 @@ struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_typ return c; }
-struct hlsl_ir_constant *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc) +struct hlsl_ir_node *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c;
if ((c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), loc))) c->value[0].u = b ? ~0u : 0;
- return c; + return &c->node; }
struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, @@ -1596,7 +1596,7 @@ struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, const struct hlsl_semantic *semantic, const struct vkd3d_shader_location *loc) { struct hlsl_ir_function_decl *decl; - struct hlsl_ir_constant *constant; + struct hlsl_ir_node *constant; struct hlsl_ir_store *store;
if (!(decl = hlsl_alloc(ctx, sizeof(*decl)))) @@ -1622,9 +1622,9 @@ struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx,
if (!(constant = hlsl_new_bool_constant(ctx, false, loc))) return decl; - hlsl_block_add_instr(&decl->body, &constant->node); + hlsl_block_add_instr(&decl->body, constant);
- if (!(store = hlsl_new_simple_store(ctx, decl->early_return_var, &constant->node))) + if (!(store = hlsl_new_simple_store(ctx, decl->early_return_var, constant))) return decl; hlsl_block_add_instr(&decl->body, &store->node);
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 3e631dd9..c2f0fe4a 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1034,7 +1034,7 @@ const char *hlsl_jump_type_to_string(enum hlsl_ir_jump_type type); struct hlsl_type *hlsl_new_array_type(struct hlsl_ctx *ctx, struct hlsl_type *basic_type, unsigned int array_size); struct hlsl_ir_node *hlsl_new_binary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg1, struct hlsl_ir_node *arg2); -struct hlsl_ir_constant *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc); +struct hlsl_ir_node *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc); struct hlsl_buffer *hlsl_new_buffer(struct hlsl_ctx *ctx, enum hlsl_buffer_type type, const char *name, const struct hlsl_reg_reservation *reservation, struct vkd3d_shader_location loc); struct hlsl_ir_node *hlsl_new_call(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *decl, diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 3873a939..d3774c96 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -5359,13 +5359,13 @@ primary_expr: } | boolean { - struct hlsl_ir_constant *c; + struct hlsl_ir_node *c;
if (!(c = hlsl_new_bool_constant(ctx, $1, &@1))) YYABORT; - if (!($$ = make_list(ctx, &c->node))) + if (!($$ = make_list(ctx, c))) { - hlsl_free_instr(&c->node); + hlsl_free_instr(c); YYABORT; } } diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index f3fcbb71..904ab1d1 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -628,18 +628,18 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun else if (instr->type == HLSL_IR_JUMP) { struct hlsl_ir_jump *jump = hlsl_ir_jump(instr); - struct hlsl_ir_constant *constant; + struct hlsl_ir_node *constant; struct hlsl_ir_store *store;
if (jump->type == HLSL_IR_JUMP_RETURN) { if (!(constant = hlsl_new_bool_constant(ctx, true, &jump->node.loc))) return false; - list_add_before(&jump->node.entry, &constant->node.entry); + list_add_before(&jump->node.entry, &constant->entry);
- if (!(store = hlsl_new_simple_store(ctx, func->early_return_var, &constant->node))) + if (!(store = hlsl_new_simple_store(ctx, func->early_return_var, constant))) return false; - list_add_after(&constant->node.entry, &store->node.entry); + list_add_after(&constant->entry, &store->node.entry);
has_early_return = true; if (in_loop)
From: Zebediah Figura zfigura@codeweavers.com
--- libs/vkd3d-shader/hlsl.c | 6 ++-- libs/vkd3d-shader/hlsl.h | 4 +-- libs/vkd3d-shader/hlsl.y | 28 +++++++++---------- libs/vkd3d-shader/hlsl_codegen.c | 47 +++++++++++++++----------------- 4 files changed, 41 insertions(+), 44 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 8a8aacfa..3b5e66f0 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -850,7 +850,7 @@ bool hlsl_scope_add_type(struct hlsl_scope *scope, struct hlsl_type *type) return true; }
-struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, +struct hlsl_ir_node *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, const struct vkd3d_shader_location *loc) { struct hlsl_ir_node *cast; @@ -858,10 +858,10 @@ struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *no cast = hlsl_new_unary_expr(ctx, HLSL_OP1_CAST, node, *loc); if (cast) cast->data_type = type; - return hlsl_ir_expr(cast); + return cast; }
-struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node) +struct hlsl_ir_node *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node) { /* Use a cast to the same type as a makeshift identity expression. */ return hlsl_new_cast(ctx, node, node->data_type, &node->loc); diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index c2f0fe4a..fa2ba426 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1039,11 +1039,11 @@ struct hlsl_buffer *hlsl_new_buffer(struct hlsl_ctx *ctx, enum hlsl_buffer_type const struct hlsl_reg_reservation *reservation, struct vkd3d_shader_location loc); struct hlsl_ir_node *hlsl_new_call(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *decl, const struct vkd3d_shader_location *loc); -struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, +struct hlsl_ir_node *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, const struct vkd3d_shader_location *loc); struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *type, const struct vkd3d_shader_location *loc); -struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node); +struct hlsl_ir_node *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node); struct hlsl_ir_node *hlsl_new_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS], struct hlsl_type *data_type, const struct vkd3d_shader_location *loc); diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index d3774c96..4c8541e3 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -280,7 +280,7 @@ static struct hlsl_ir_node *add_cast(struct hlsl_ctx *ctx, struct list *instrs, struct hlsl_ir_node *node, struct hlsl_type *dst_type, const struct vkd3d_shader_location *loc) { struct hlsl_type *src_type = node->data_type; - struct hlsl_ir_expr *cast; + struct hlsl_ir_node *cast;
if (hlsl_types_are_equal(src_type, dst_type)) return node; @@ -338,9 +338,9 @@ static struct hlsl_ir_node *add_cast(struct hlsl_ctx *ctx, struct list *instrs,
if (!(cast = hlsl_new_cast(ctx, &load->node, dst_comp_type, loc))) return NULL; - list_add_tail(instrs, &cast->node.entry); + list_add_tail(instrs, &cast->entry);
- if (!(store = hlsl_new_store_component(ctx, &block, &var_deref, dst_idx, &cast->node))) + if (!(store = hlsl_new_store_component(ctx, &block, &var_deref, dst_idx, cast))) return NULL; list_move_tail(instrs, &block.instrs); } @@ -355,8 +355,8 @@ static struct hlsl_ir_node *add_cast(struct hlsl_ctx *ctx, struct list *instrs, { if (!(cast = hlsl_new_cast(ctx, node, dst_type, loc))) return NULL; - list_add_tail(instrs, &cast->node.entry); - return &cast->node; + list_add_tail(instrs, &cast->entry); + return cast; } }
@@ -787,7 +787,7 @@ static bool add_array_load(struct hlsl_ctx *ctx, struct list *instrs, struct hls struct hlsl_ir_node *index, const struct vkd3d_shader_location *loc) { const struct hlsl_type *expr_type = array->data_type, *index_type = index->data_type; - struct hlsl_ir_expr *cast; + struct hlsl_ir_node *cast;
if (expr_type->class == HLSL_CLASS_OBJECT && (expr_type->base_type == HLSL_TYPE_TEXTURE || expr_type->base_type == HLSL_TYPE_UAV) @@ -835,8 +835,8 @@ static bool add_array_load(struct hlsl_ctx *ctx, struct list *instrs, struct hls
if (!(cast = hlsl_new_cast(ctx, index, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), &index->loc))) return false; - list_add_tail(instrs, &cast->node.entry); - index = &cast->node; + list_add_tail(instrs, &cast->entry); + index = cast;
if (expr_type->class == HLSL_CLASS_MATRIX) return add_matrix_index(ctx, instrs, array, index, loc); @@ -1759,7 +1759,7 @@ static struct hlsl_ir_node *add_assignment(struct hlsl_ctx *ctx, struct list *in enum parse_assign_op assign_op, struct hlsl_ir_node *rhs) { struct hlsl_type *lhs_type = lhs->data_type; - struct hlsl_ir_expr *copy; + struct hlsl_ir_node *copy; unsigned int writemask = 0;
if (assign_op == ASSIGN_OP_SUB) @@ -1870,8 +1870,8 @@ static struct hlsl_ir_node *add_assignment(struct hlsl_ctx *ctx, struct list *in * the last instruction in the list, we do need to copy. */ if (!(copy = hlsl_new_copy(ctx, rhs))) return NULL; - list_add_tail(instrs, ©->node.entry); - return ©->node; + list_add_tail(instrs, ©->entry); + return copy; }
static bool add_increment(struct hlsl_ctx *ctx, struct list *instrs, bool decrement, bool post, @@ -1893,14 +1893,14 @@ static bool add_increment(struct hlsl_ctx *ctx, struct list *instrs, bool decrem
if (post) { - struct hlsl_ir_expr *copy; + struct hlsl_ir_node *copy;
if (!(copy = hlsl_new_copy(ctx, lhs))) return false; - list_add_tail(instrs, ©->node.entry); + list_add_tail(instrs, ©->entry);
/* Post increment/decrement expressions are considered const. */ - if (!(copy->node.data_type = hlsl_type_clone(ctx, copy->node.data_type, 0, HLSL_MODIFIER_CONST))) + if (!(copy->data_type = hlsl_type_clone(ctx, copy->data_type, 0, HLSL_MODIFIER_CONST))) return false; }
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 904ab1d1..30adc025 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -748,23 +748,22 @@ static bool lower_broadcasts(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, v
if (src_type->class <= HLSL_CLASS_VECTOR && dst_type->class <= HLSL_CLASS_VECTOR && src_type->dimx == 1) { - struct hlsl_ir_node *replacement; + struct hlsl_ir_node *replacement, *new_cast; struct hlsl_ir_swizzle *swizzle; - struct hlsl_ir_expr *new_cast;
dst_scalar_type = hlsl_get_scalar_type(ctx, dst_type->base_type); /* We need to preserve the cast since it might be doing more than just * turning the scalar into a vector. */ if (!(new_cast = hlsl_new_cast(ctx, cast->operands[0].node, dst_scalar_type, &cast->node.loc))) return false; - list_add_after(&cast->node.entry, &new_cast->node.entry); - replacement = &new_cast->node; + list_add_after(&cast->node.entry, &new_cast->entry); + replacement = new_cast;
if (dst_type->dimx != 1) { if (!(swizzle = hlsl_new_swizzle(ctx, HLSL_SWIZZLE(X, X, X, X), dst_type->dimx, replacement, &cast->node.loc))) return false; - list_add_after(&new_cast->node.entry, &swizzle->node.entry); + list_add_after(&new_cast->entry, &swizzle->node.entry); replacement = &swizzle->node; }
@@ -1652,17 +1651,17 @@ static bool lower_narrowing_casts(struct hlsl_ctx *ctx, struct hlsl_ir_node *ins if (src_type->class <= HLSL_CLASS_VECTOR && dst_type->class <= HLSL_CLASS_VECTOR && dst_type->dimx < src_type->dimx) { struct hlsl_ir_swizzle *swizzle; - struct hlsl_ir_expr *new_cast; + struct hlsl_ir_node *new_cast;
dst_vector_type = hlsl_get_vector_type(ctx, dst_type->base_type, src_type->dimx); /* We need to preserve the cast since it might be doing more than just * narrowing the vector. */ if (!(new_cast = hlsl_new_cast(ctx, cast->operands[0].node, dst_vector_type, &cast->node.loc))) return false; - list_add_after(&cast->node.entry, &new_cast->node.entry); - if (!(swizzle = hlsl_new_swizzle(ctx, HLSL_SWIZZLE(X, Y, Z, W), dst_type->dimx, &new_cast->node, &cast->node.loc))) + list_add_after(&cast->node.entry, &new_cast->entry); + if (!(swizzle = hlsl_new_swizzle(ctx, HLSL_SWIZZLE(X, Y, Z, W), dst_type->dimx, new_cast, &cast->node.loc))) return false; - list_add_after(&new_cast->node.entry, &swizzle->node.entry); + list_add_after(&new_cast->entry, &swizzle->node.entry);
hlsl_replace_node(&cast->node, &swizzle->node); return true; @@ -1958,9 +1957,8 @@ struct hlsl_ir_load *hlsl_add_conditional(struct hlsl_ctx *ctx, struct list *ins
static bool lower_int_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { - struct hlsl_ir_node *arg1, *arg2, *xor, *and, *abs1, *abs2, *div, *neg; + struct hlsl_ir_node *arg1, *arg2, *xor, *and, *abs1, *abs2, *div, *neg, *cast1, *cast2, *cast3; struct hlsl_type *type = instr->data_type, *utype; - struct hlsl_ir_expr *cast1, *cast2, *cast3; struct hlsl_ir_constant *high_bit; struct hlsl_ir_expr *expr; struct hlsl_ir_load *cond; @@ -1999,7 +1997,7 @@ static bool lower_int_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
if (!(cast1 = hlsl_new_cast(ctx, abs1, utype, &instr->loc))) return false; - list_add_before(&instr->entry, &cast1->node.entry); + list_add_before(&instr->entry, &cast1->entry);
if (!(abs2 = hlsl_new_unary_expr(ctx, HLSL_OP1_ABS, arg2, instr->loc))) return false; @@ -2007,21 +2005,21 @@ static bool lower_int_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
if (!(cast2 = hlsl_new_cast(ctx, abs2, utype, &instr->loc))) return false; - list_add_before(&instr->entry, &cast2->node.entry); + list_add_before(&instr->entry, &cast2->entry);
- if (!(div = hlsl_new_binary_expr(ctx, HLSL_OP2_DIV, &cast1->node, &cast2->node))) + if (!(div = hlsl_new_binary_expr(ctx, HLSL_OP2_DIV, cast1, cast2))) return false; list_add_before(&instr->entry, &div->entry);
if (!(cast3 = hlsl_new_cast(ctx, div, type, &instr->loc))) return false; - list_add_before(&instr->entry, &cast3->node.entry); + list_add_before(&instr->entry, &cast3->entry);
- if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, &cast3->node, instr->loc))) + if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, cast3, instr->loc))) return false; list_add_before(&instr->entry, &neg->entry);
- if (!(cond = hlsl_add_conditional(ctx, &instr->entry, and, neg, &cast3->node))) + if (!(cond = hlsl_add_conditional(ctx, &instr->entry, and, neg, cast3))) return false; hlsl_replace_node(instr, &cond->node);
@@ -2030,9 +2028,8 @@ static bool lower_int_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
static bool lower_int_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { - struct hlsl_ir_node *arg1, *arg2, *and, *abs1, *abs2, *div, *neg; + struct hlsl_ir_node *arg1, *arg2, *and, *abs1, *abs2, *div, *neg, *cast1, *cast2, *cast3; struct hlsl_type *type = instr->data_type, *utype; - struct hlsl_ir_expr *cast1, *cast2, *cast3; struct hlsl_ir_constant *high_bit; struct hlsl_ir_expr *expr; struct hlsl_ir_load *cond; @@ -2067,7 +2064,7 @@ static bool lower_int_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
if (!(cast1 = hlsl_new_cast(ctx, abs1, utype, &instr->loc))) return false; - list_add_before(&instr->entry, &cast1->node.entry); + list_add_before(&instr->entry, &cast1->entry);
if (!(abs2 = hlsl_new_unary_expr(ctx, HLSL_OP1_ABS, arg2, instr->loc))) return false; @@ -2075,21 +2072,21 @@ static bool lower_int_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
if (!(cast2 = hlsl_new_cast(ctx, abs2, utype, &instr->loc))) return false; - list_add_before(&instr->entry, &cast2->node.entry); + list_add_before(&instr->entry, &cast2->entry);
- if (!(div = hlsl_new_binary_expr(ctx, HLSL_OP2_MOD, &cast1->node, &cast2->node))) + if (!(div = hlsl_new_binary_expr(ctx, HLSL_OP2_MOD, cast1, cast2))) return false; list_add_before(&instr->entry, &div->entry);
if (!(cast3 = hlsl_new_cast(ctx, div, type, &instr->loc))) return false; - list_add_before(&instr->entry, &cast3->node.entry); + list_add_before(&instr->entry, &cast3->entry);
- if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, &cast3->node, instr->loc))) + if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, cast3, instr->loc))) return false; list_add_before(&instr->entry, &neg->entry);
- if (!(cond = hlsl_add_conditional(ctx, &instr->entry, and, neg, &cast3->node))) + if (!(cond = hlsl_add_conditional(ctx, &instr->entry, and, neg, cast3))) return false; hlsl_replace_node(instr, &cond->node);
From: Zebediah Figura zfigura@codeweavers.com
--- libs/vkd3d-shader/hlsl.c | 4 +-- libs/vkd3d-shader/hlsl.h | 2 +- libs/vkd3d-shader/hlsl.y | 54 ++++++++++++++------------------ libs/vkd3d-shader/hlsl_codegen.c | 7 ++--- 4 files changed, 30 insertions(+), 37 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 3b5e66f0..5d39baa4 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1072,7 +1072,7 @@ struct hlsl_ir_node *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const return &c->node; }
-struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, +struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c; @@ -1080,7 +1080,7 @@ struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, if ((c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_FLOAT), loc))) c->value[0].f = f;
- return c; + return &c->node; }
struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index fa2ba426..4870e9bb 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1047,7 +1047,7 @@ struct hlsl_ir_node *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *no struct hlsl_ir_node *hlsl_new_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS], struct hlsl_type *data_type, const struct vkd3d_shader_location *loc); -struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx, +struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, const struct vkd3d_shader_location *loc); struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, const struct hlsl_func_parameters *parameters, diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 4c8541e3..df099f01 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2447,20 +2447,19 @@ static bool intrinsic_abs(struct hlsl_ctx *ctx, static bool intrinsic_all(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { - struct hlsl_ir_node *arg = params->args[0], *mul; - struct hlsl_ir_constant *one, *zero; + struct hlsl_ir_node *arg = params->args[0], *mul, *one, *zero; struct hlsl_ir_load *load; unsigned int i, count;
if (!(one = hlsl_new_float_constant(ctx, 1.0f, loc))) return false; - list_add_tail(params->instrs, &one->node.entry); + list_add_tail(params->instrs, &one->entry);
if (!(zero = hlsl_new_float_constant(ctx, 0.0f, loc))) return false; - list_add_tail(params->instrs, &zero->node.entry); + list_add_tail(params->instrs, &zero->entry);
- mul = &one->node; + mul = one;
count = hlsl_type_component_count(arg->data_type); for (i = 0; i < count; ++i) @@ -2472,7 +2471,7 @@ static bool intrinsic_all(struct hlsl_ctx *ctx, return false; }
- return !!add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_NEQUAL, mul, &zero->node, loc); + return !!add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_NEQUAL, mul, zero, loc); }
/* Find the type corresponding to the given source type, with the same @@ -2629,8 +2628,7 @@ static bool intrinsic_dot(struct hlsl_ctx *ctx, static bool intrinsic_exp(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { - struct hlsl_ir_constant *coeff; - struct hlsl_ir_node *arg, *mul; + struct hlsl_ir_node *arg, *mul, *coeff;
if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) return false; @@ -2638,9 +2636,9 @@ static bool intrinsic_exp(struct hlsl_ctx *ctx, /* 1/ln(2) */ if (!(coeff = hlsl_new_float_constant(ctx, 1.442695f, loc))) return false; - list_add_tail(params->instrs, &coeff->node.entry); + list_add_tail(params->instrs, &coeff->entry);
- if (!(mul = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, &coeff->node, params->args[0], loc))) + if (!(mul = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, coeff, params->args[0], loc))) return false;
return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_EXP2, mul, loc); @@ -2757,9 +2755,8 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { struct hlsl_ir_node *n_l_neg, *n_h_neg, *specular_or, *specular_pow; - struct hlsl_ir_constant *init, *zero; - struct hlsl_ir_node *n_l, *n_h, *m; - struct hlsl_ir_node *diffuse; + struct hlsl_ir_node *n_l, *n_h, *m, *diffuse, *zero; + struct hlsl_ir_constant *init; struct hlsl_ir_store *store; struct hlsl_deref var_deref; struct hlsl_type *ret_type; @@ -2804,10 +2801,10 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx,
if (!(zero = hlsl_new_float_constant(ctx, 0.0f, loc))) return false; - list_add_tail(params->instrs, &zero->node.entry); + list_add_tail(params->instrs, &zero->entry);
/* Diffuse component. */ - if (!(diffuse = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MAX, n_l, &zero->node, loc))) + if (!(diffuse = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MAX, n_l, zero, loc))) return false;
if (!(store = hlsl_new_store_component(ctx, &block, &var_deref, 1, diffuse))) @@ -2815,12 +2812,10 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, list_move_tail(params->instrs, &block.instrs);
/* Specular component. */ - if (!(n_h_neg = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, - n_h, &zero->node, loc))) + if (!(n_h_neg = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, n_h, zero, loc))) return false;
- if (!(n_l_neg = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, - n_l, &zero->node, loc))) + if (!(n_l_neg = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, n_l, zero, loc))) return false;
if (!(specular_or = add_binary_logical_expr(ctx, params->instrs, HLSL_OP2_LOGIC_OR, n_l_neg, n_h_neg, loc))) @@ -2829,7 +2824,7 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, if (!(specular_pow = add_pow_expr(ctx, params->instrs, n_h, m, loc))) return false;
- if (!(load = hlsl_add_conditional(ctx, params->instrs, specular_or, &zero->node, specular_pow))) + if (!(load = hlsl_add_conditional(ctx, params->instrs, specular_or, zero, specular_pow))) return false;
if (!(store = hlsl_new_store_component(ctx, &block, &var_deref, 2, &load->node))) @@ -3065,8 +3060,7 @@ static bool intrinsic_sin(struct hlsl_ctx *ctx, static bool intrinsic_smoothstep(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { - struct hlsl_ir_node *min_arg, *max_arg, *x_arg, *p, *p_num, *p_denom, *res; - struct hlsl_ir_constant *one, *minus_two, *three; + struct hlsl_ir_node *min_arg, *max_arg, *x_arg, *p, *p_num, *p_denom, *res, *one, *minus_two, *three;
if (!elementwise_intrinsic_float_convert_args(ctx, params, loc)) return false; @@ -3086,9 +3080,9 @@ static bool intrinsic_smoothstep(struct hlsl_ctx *ctx,
if (!(one = hlsl_new_float_constant(ctx, 1.0, loc))) return false; - list_add_tail(params->instrs, &one->node.entry); + list_add_tail(params->instrs, &one->entry);
- if (!(p_denom = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_DIV, &one->node, p_denom, loc))) + if (!(p_denom = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_DIV, one, p_denom, loc))) return false;
if (!(p = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, p_num, p_denom, loc))) @@ -3099,16 +3093,16 @@ static bool intrinsic_smoothstep(struct hlsl_ctx *ctx,
if (!(minus_two = hlsl_new_float_constant(ctx, -2.0, loc))) return false; - list_add_tail(params->instrs, &minus_two->node.entry); + list_add_tail(params->instrs, &minus_two->entry);
if (!(three = hlsl_new_float_constant(ctx, 3.0, loc))) return false; - list_add_tail(params->instrs, &three->node.entry); + list_add_tail(params->instrs, &three->entry);
- if (!(res = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, &minus_two->node, p, loc))) + if (!(res = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, minus_two, p, loc))) return false;
- if (!(res = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, &three->node, res, loc))) + if (!(res = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, three, res, loc))) return false;
if (!(p = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, p, p, loc))) @@ -5341,11 +5335,11 @@ func_arguments: primary_expr: C_FLOAT { - struct hlsl_ir_constant *c; + struct hlsl_ir_node *c;
if (!(c = hlsl_new_float_constant(ctx, $1, &@1))) YYABORT; - if (!($$ = make_list(ctx, &c->node))) + if (!($$ = make_list(ctx, c))) YYABORT; } | C_INTEGER diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 30adc025..bc2791b3 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1769,9 +1769,8 @@ static bool lower_sqrt(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *c /* Lower DP2 to MUL + ADD */ static bool lower_dot(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { - struct hlsl_ir_node *arg1, *arg2, *mul, *replacement; + struct hlsl_ir_node *arg1, *arg2, *mul, *replacement, *zero; struct hlsl_ir_swizzle *add_x, *add_y; - struct hlsl_ir_constant *zero; struct hlsl_ir_expr *expr;
if (instr->type != HLSL_IR_EXPR) @@ -1790,11 +1789,11 @@ static bool lower_dot(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *co
if (!(zero = hlsl_new_float_constant(ctx, 0.0f, &expr->node.loc))) return false; - list_add_before(&instr->entry, &zero->node.entry); + list_add_before(&instr->entry, &zero->entry);
operands[0] = arg1; operands[1] = arg2; - operands[2] = &zero->node; + operands[2] = zero;
if (!(replacement = hlsl_new_expr(ctx, HLSL_OP3_DP2ADD, operands, instr->data_type, &expr->node.loc))) return false;
From: Zebediah Figura zfigura@codeweavers.com
--- libs/vkd3d-shader/hlsl.c | 35 +++++++++------ libs/vkd3d-shader/hlsl.h | 7 +-- libs/vkd3d-shader/hlsl.y | 39 ++++++++++------- libs/vkd3d-shader/hlsl_codegen.c | 74 ++++++++++++++++++-------------- libs/vkd3d-shader/hlsl_sm4.c | 6 +-- 5 files changed, 94 insertions(+), 67 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 5d39baa4..494103d2 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1142,16 +1142,20 @@ struct hlsl_ir_node *hlsl_new_binary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_exp return hlsl_new_expr(ctx, op, operands, arg1->data_type, &arg1->loc); }
-struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct vkd3d_shader_location loc) +struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, + struct hlsl_block *then_block, struct hlsl_block *else_block, const struct vkd3d_shader_location *loc) { struct hlsl_ir_if *iff;
if (!(iff = hlsl_alloc(ctx, sizeof(*iff)))) return NULL; - init_node(&iff->node, HLSL_IR_IF, NULL, &loc); + init_node(&iff->node, HLSL_IR_IF, NULL, loc); hlsl_src_from_node(&iff->condition, condition); - hlsl_block_init(&iff->then_instrs); - hlsl_block_init(&iff->else_instrs); + hlsl_block_init(&iff->then_block); + list_move_tail(&iff->then_block.instrs, &then_block->instrs); + hlsl_block_init(&iff->else_block); + if (else_block) + list_move_tail(&iff->else_block.instrs, &else_block->instrs); return iff; }
@@ -1413,17 +1417,24 @@ static struct hlsl_ir_node *clone_expr(struct hlsl_ctx *ctx, struct clone_instr_
static struct hlsl_ir_node *clone_if(struct hlsl_ctx *ctx, struct clone_instr_map *map, struct hlsl_ir_if *src) { + struct hlsl_block then_block, else_block; struct hlsl_ir_if *dst;
- if (!(dst = hlsl_new_if(ctx, map_instr(map, src->condition.node), src->node.loc))) + if (!clone_block(ctx, &then_block, &src->then_block, map)) return NULL; + if (!clone_block(ctx, &else_block, &src->else_block, map)) + { + hlsl_free_instr_list(&then_block.instrs); + return NULL; + }
- if (!clone_block(ctx, &dst->then_instrs, &src->then_instrs, map) - || !clone_block(ctx, &dst->else_instrs, &src->else_instrs, map)) + if (!(dst = hlsl_new_if(ctx, map_instr(map, src->condition.node), &then_block, &else_block, &src->node.loc))) { - hlsl_free_instr(&dst->node); + hlsl_free_instr_list(&then_block.instrs); + hlsl_free_instr_list(&else_block.instrs); return NULL; } + return &dst->node; }
@@ -2216,9 +2227,9 @@ static void dump_ir_if(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, vkd3d_string_buffer_printf(buffer, "if ("); dump_src(buffer, &if_node->condition); vkd3d_string_buffer_printf(buffer, ") {\n"); - dump_instr_list(ctx, buffer, &if_node->then_instrs.instrs); + dump_instr_list(ctx, buffer, &if_node->then_block.instrs); vkd3d_string_buffer_printf(buffer, " %10s } else {\n", ""); - dump_instr_list(ctx, buffer, &if_node->else_instrs.instrs); + dump_instr_list(ctx, buffer, &if_node->else_block.instrs); vkd3d_string_buffer_printf(buffer, " %10s }", ""); }
@@ -2470,8 +2481,8 @@ static void free_ir_expr(struct hlsl_ir_expr *expr)
static void free_ir_if(struct hlsl_ir_if *if_node) { - hlsl_free_instr_list(&if_node->then_instrs.instrs); - hlsl_free_instr_list(&if_node->else_instrs.instrs); + hlsl_free_instr_list(&if_node->then_block.instrs); + hlsl_free_instr_list(&if_node->else_block.instrs); hlsl_src_remove(&if_node->condition); vkd3d_free(if_node); } diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 4870e9bb..24f9e1f2 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -450,8 +450,8 @@ struct hlsl_ir_if { struct hlsl_ir_node node; struct hlsl_src condition; - struct hlsl_block then_instrs; - struct hlsl_block else_instrs; + struct hlsl_block then_block; + struct hlsl_block else_block; };
struct hlsl_ir_loop @@ -1052,7 +1052,8 @@ struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, const struct hlsl_func_parameters *parameters, const struct hlsl_semantic *semantic, const struct vkd3d_shader_location *loc); -struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct vkd3d_shader_location loc); +struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, + struct hlsl_block *then_block, struct hlsl_block *else_block, const struct vkd3d_shader_location *loc); struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, const struct vkd3d_shader_location *loc); struct hlsl_ir_jump *hlsl_new_jump(struct hlsl_ctx *ctx, enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc); diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index df099f01..91bbeae4 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -85,8 +85,8 @@ struct parse_function
struct parse_if_body { - struct list *then_instrs; - struct list *else_instrs; + struct list *then_block; + struct list *else_block; };
enum parse_assign_op @@ -407,6 +407,7 @@ static DWORD add_modifiers(struct hlsl_ctx *ctx, DWORD modifiers, DWORD mod, con static bool append_conditional_break(struct hlsl_ctx *ctx, struct list *cond_list) { struct hlsl_ir_node *condition, *not; + struct hlsl_block then_block; struct hlsl_ir_jump *jump; struct hlsl_ir_if *iff;
@@ -419,13 +420,15 @@ static bool append_conditional_break(struct hlsl_ctx *ctx, struct list *cond_lis return false; list_add_tail(cond_list, ¬->entry);
- if (!(iff = hlsl_new_if(ctx, not, condition->loc))) - return false; - list_add_tail(cond_list, &iff->node.entry); + hlsl_block_init(&then_block);
if (!(jump = hlsl_new_jump(ctx, HLSL_IR_JUMP_BREAK, condition->loc))) return false; - hlsl_block_add_instr(&iff->then_instrs, &jump->node); + hlsl_block_add_instr(&then_block, &jump->node); + + if (!(iff = hlsl_new_if(ctx, not, &then_block, NULL, &condition->loc))) + return false; + list_add_tail(cond_list, &iff->node.entry); return true; }
@@ -5253,15 +5256,19 @@ selection_statement: KW_IF '(' expr ')' if_body { struct hlsl_ir_node *condition = node_from_list($3); + struct hlsl_block then_block, else_block; struct hlsl_ir_if *instr;
- if (!(instr = hlsl_new_if(ctx, condition, @1))) + hlsl_block_init(&then_block); + list_move_tail(&then_block.instrs, $5.then_block); + hlsl_block_init(&else_block); + if ($5.else_block) + list_move_tail(&else_block.instrs, $5.else_block); + vkd3d_free($5.then_block); + vkd3d_free($5.else_block); + + if (!(instr = hlsl_new_if(ctx, condition, &then_block, &else_block, &@1))) YYABORT; - list_move_tail(&instr->then_instrs.instrs, $5.then_instrs); - if ($5.else_instrs) - list_move_tail(&instr->else_instrs.instrs, $5.else_instrs); - vkd3d_free($5.then_instrs); - vkd3d_free($5.else_instrs); if (condition->data_type->dimx > 1 || condition->data_type->dimy > 1) { struct vkd3d_string_buffer *string; @@ -5278,13 +5285,13 @@ selection_statement: if_body: statement { - $$.then_instrs = $1; - $$.else_instrs = NULL; + $$.then_block = $1; + $$.else_block = NULL; } | statement KW_ELSE statement { - $$.then_instrs = $1; - $$.else_instrs = $3; + $$.then_block = $1; + $$.else_block = $3; }
loop_statement: diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index bc2791b3..0e252dac 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -459,8 +459,8 @@ static bool transform_ir(struct hlsl_ctx *ctx, bool (*func)(struct hlsl_ctx *ctx { struct hlsl_ir_if *iff = hlsl_ir_if(instr);
- progress |= transform_ir(ctx, func, &iff->then_instrs, context); - progress |= transform_ir(ctx, func, &iff->else_instrs, context); + progress |= transform_ir(ctx, func, &iff->then_block, context); + progress |= transform_ir(ctx, func, &iff->else_block, context); } else if (instr->type == HLSL_IR_LOOP) progress |= transform_ir(ctx, func, &hlsl_ir_loop(instr)->body, context); @@ -516,21 +516,24 @@ static bool find_recursive_calls(struct hlsl_ctx *ctx, struct hlsl_ir_node *inst static void insert_early_return_break(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *func, struct hlsl_ir_node *cf_instr) { + struct hlsl_block then_block; struct hlsl_ir_jump *jump; struct hlsl_ir_load *load; struct hlsl_ir_if *iff;
+ hlsl_block_init(&then_block); + if (!(load = hlsl_new_var_load(ctx, func->early_return_var, cf_instr->loc))) return; list_add_after(&cf_instr->entry, &load->node.entry);
- if (!(iff = hlsl_new_if(ctx, &load->node, cf_instr->loc))) + if (!(jump = hlsl_new_jump(ctx, HLSL_IR_JUMP_BREAK, cf_instr->loc))) return; - list_add_after(&load->node.entry, &iff->node.entry); + hlsl_block_add_instr(&then_block, &jump->node);
- if (!(jump = hlsl_new_jump(ctx, HLSL_IR_JUMP_BREAK, cf_instr->loc))) + if (!(iff = hlsl_new_if(ctx, &load->node, &then_block, NULL, &cf_instr->loc))) return; - hlsl_block_add_instr(&iff->then_instrs, &jump->node); + list_add_after(&load->node.entry, &iff->node.entry); }
/* Remove HLSL_IR_JUMP_RETURN calls by altering subsequent control flow. */ @@ -591,8 +594,8 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun { struct hlsl_ir_if *iff = hlsl_ir_if(instr);
- has_early_return |= lower_return(ctx, func, &iff->then_instrs, in_loop); - has_early_return |= lower_return(ctx, func, &iff->else_instrs, in_loop); + has_early_return |= lower_return(ctx, func, &iff->then_block, in_loop); + has_early_return |= lower_return(ctx, func, &iff->else_block, in_loop);
if (has_early_return) { @@ -675,6 +678,7 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun else if (cf_instr) { struct list *tail = list_tail(&block->instrs); + struct hlsl_block then_block; struct hlsl_ir_load *load; struct hlsl_ir_node *not; struct hlsl_ir_if *iff; @@ -685,6 +689,10 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun if (tail == &cf_instr->entry) return has_early_return;
+ hlsl_block_init(&then_block); + list_move_slice_tail(&then_block.instrs, list_next(&block->instrs, &cf_instr->entry), tail); + lower_return(ctx, func, &then_block, in_loop); + if (!(load = hlsl_new_var_load(ctx, func->early_return_var, cf_instr->loc))) return false; hlsl_block_add_instr(block, &load->node); @@ -693,13 +701,9 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun return false; hlsl_block_add_instr(block, not);
- if (!(iff = hlsl_new_if(ctx, not, cf_instr->loc))) + if (!(iff = hlsl_new_if(ctx, not, &then_block, NULL, &cf_instr->loc))) return false; - hlsl_block_add_instr(block, &iff->node); - - list_move_slice_tail(&iff->then_instrs.instrs, list_next(&block->instrs, &cf_instr->entry), tail); - - lower_return(ctx, func, &iff->then_instrs, in_loop); + list_add_tail(&block->instrs, &iff->node.entry); }
return has_early_return; @@ -1269,8 +1273,8 @@ static void copy_propagation_invalidate_from_block(struct hlsl_ctx *ctx, struct { struct hlsl_ir_if *iff = hlsl_ir_if(instr);
- copy_propagation_invalidate_from_block(ctx, state, &iff->then_instrs); - copy_propagation_invalidate_from_block(ctx, state, &iff->else_instrs); + copy_propagation_invalidate_from_block(ctx, state, &iff->then_block); + copy_propagation_invalidate_from_block(ctx, state, &iff->else_block);
break; } @@ -1300,19 +1304,19 @@ static bool copy_propagation_process_if(struct hlsl_ctx *ctx, struct hlsl_ir_if bool progress = false;
copy_propagation_state_init(ctx, &inner_state, state); - progress |= copy_propagation_transform_block(ctx, &iff->then_instrs, &inner_state); + progress |= copy_propagation_transform_block(ctx, &iff->then_block, &inner_state); copy_propagation_state_destroy(&inner_state);
copy_propagation_state_init(ctx, &inner_state, state); - progress |= copy_propagation_transform_block(ctx, &iff->else_instrs, &inner_state); + progress |= copy_propagation_transform_block(ctx, &iff->else_block, &inner_state); copy_propagation_state_destroy(&inner_state);
/* Ideally we'd invalidate the outer state looking at what was * touched in the two inner states, but this doesn't work for * loops (because we need to know what is invalidated in advance), * so we need copy_propagation_invalidate_from_block() anyway. */ - copy_propagation_invalidate_from_block(ctx, state, &iff->then_instrs); - copy_propagation_invalidate_from_block(ctx, state, &iff->else_instrs); + copy_propagation_invalidate_from_block(ctx, state, &iff->then_block); + copy_propagation_invalidate_from_block(ctx, state, &iff->else_block);
return progress; } @@ -1925,6 +1929,7 @@ static bool lower_casts_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr struct hlsl_ir_load *hlsl_add_conditional(struct hlsl_ctx *ctx, struct list *instrs, struct hlsl_ir_node *condition, struct hlsl_ir_node *if_true, struct hlsl_ir_node *if_false) { + struct hlsl_block then_block, else_block; struct hlsl_ir_store *store; struct hlsl_ir_load *load; struct hlsl_ir_var *var; @@ -1935,17 +1940,20 @@ struct hlsl_ir_load *hlsl_add_conditional(struct hlsl_ctx *ctx, struct list *ins if (!(var = hlsl_new_synthetic_var(ctx, "conditional", if_true->data_type, &condition->loc))) return NULL;
- if (!(iff = hlsl_new_if(ctx, condition, condition->loc))) - return NULL; - list_add_tail(instrs, &iff->node.entry); + hlsl_block_init(&then_block); + hlsl_block_init(&else_block);
if (!(store = hlsl_new_simple_store(ctx, var, if_true))) return NULL; - hlsl_block_add_instr(&iff->then_instrs, &store->node); + hlsl_block_add_instr(&then_block, &store->node);
if (!(store = hlsl_new_simple_store(ctx, var, if_false))) return NULL; - hlsl_block_add_instr(&iff->else_instrs, &store->node); + hlsl_block_add_instr(&else_block, &store->node); + + if (!(iff = hlsl_new_if(ctx, condition, &then_block, &else_block, &condition->loc))) + return NULL; + list_add_tail(instrs, &iff->node.entry);
if (!(load = hlsl_new_var_load(ctx, var, condition->loc))) return NULL; @@ -2245,8 +2253,8 @@ static unsigned int index_instructions(struct hlsl_block *block, unsigned int in if (instr->type == HLSL_IR_IF) { struct hlsl_ir_if *iff = hlsl_ir_if(instr); - index = index_instructions(&iff->then_instrs, index); - index = index_instructions(&iff->else_instrs, index); + index = index_instructions(&iff->then_block, index); + index = index_instructions(&iff->else_block, index); } else if (instr->type == HLSL_IR_LOOP) { @@ -2371,8 +2379,8 @@ static void compute_liveness_recurse(struct hlsl_block *block, unsigned int loop { struct hlsl_ir_if *iff = hlsl_ir_if(instr);
- compute_liveness_recurse(&iff->then_instrs, loop_first, loop_last); - compute_liveness_recurse(&iff->else_instrs, loop_first, loop_last); + compute_liveness_recurse(&iff->then_block, loop_first, loop_last); + compute_liveness_recurse(&iff->else_block, loop_first, loop_last); iff->condition.node->last_read = instr->index; break; } @@ -2641,8 +2649,8 @@ static void allocate_temp_registers_recurse(struct hlsl_ctx *ctx, struct hlsl_bl case HLSL_IR_IF: { struct hlsl_ir_if *iff = hlsl_ir_if(instr); - allocate_temp_registers_recurse(ctx, &iff->then_instrs, liveness); - allocate_temp_registers_recurse(ctx, &iff->else_instrs, liveness); + allocate_temp_registers_recurse(ctx, &iff->then_block, liveness); + allocate_temp_registers_recurse(ctx, &iff->else_block, liveness); break; }
@@ -2756,8 +2764,8 @@ static void allocate_const_registers_recurse(struct hlsl_ctx *ctx, struct hlsl_b case HLSL_IR_IF: { struct hlsl_ir_if *iff = hlsl_ir_if(instr); - allocate_const_registers_recurse(ctx, &iff->then_instrs, liveness); - allocate_const_registers_recurse(ctx, &iff->else_instrs, liveness); + allocate_const_registers_recurse(ctx, &iff->then_block, liveness); + allocate_const_registers_recurse(ctx, &iff->else_block, liveness); break; }
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index 5bd4f2d6..5486c20a 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -2117,15 +2117,15 @@ static void write_sm4_if(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buf sm4_src_from_node(&instr.srcs[0], iff->condition.node, VKD3DSP_WRITEMASK_ALL); write_sm4_instruction(buffer, &instr);
- write_sm4_block(ctx, buffer, &iff->then_instrs); + write_sm4_block(ctx, buffer, &iff->then_block);
- if (!list_empty(&iff->else_instrs.instrs)) + if (!list_empty(&iff->else_block.instrs)) { instr.opcode = VKD3D_SM4_OP_ELSE; instr.src_count = 0; write_sm4_instruction(buffer, &instr);
- write_sm4_block(ctx, buffer, &iff->else_instrs); + write_sm4_block(ctx, buffer, &iff->else_block); }
instr.opcode = VKD3D_SM4_OP_ENDIF;
From: Zebediah Figura zfigura@codeweavers.com
--- libs/vkd3d-shader/hlsl.c | 8 ++++---- libs/vkd3d-shader/hlsl.h | 2 +- libs/vkd3d-shader/hlsl.y | 11 +++++------ libs/vkd3d-shader/hlsl_codegen.c | 13 ++++++------- 4 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 494103d2..99ebc905 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1142,7 +1142,7 @@ struct hlsl_ir_node *hlsl_new_binary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_exp return hlsl_new_expr(ctx, op, operands, arg1->data_type, &arg1->loc); }
-struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, +struct hlsl_ir_node *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct hlsl_block *then_block, struct hlsl_block *else_block, const struct vkd3d_shader_location *loc) { struct hlsl_ir_if *iff; @@ -1156,7 +1156,7 @@ struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condit hlsl_block_init(&iff->else_block); if (else_block) list_move_tail(&iff->else_block.instrs, &else_block->instrs); - return iff; + return &iff->node; }
struct hlsl_ir_load *hlsl_new_load_index(struct hlsl_ctx *ctx, const struct hlsl_deref *deref, @@ -1418,7 +1418,7 @@ static struct hlsl_ir_node *clone_expr(struct hlsl_ctx *ctx, struct clone_instr_ static struct hlsl_ir_node *clone_if(struct hlsl_ctx *ctx, struct clone_instr_map *map, struct hlsl_ir_if *src) { struct hlsl_block then_block, else_block; - struct hlsl_ir_if *dst; + struct hlsl_ir_node *dst;
if (!clone_block(ctx, &then_block, &src->then_block, map)) return NULL; @@ -1435,7 +1435,7 @@ static struct hlsl_ir_node *clone_if(struct hlsl_ctx *ctx, struct clone_instr_ma return NULL; }
- return &dst->node; + return dst; }
static struct hlsl_ir_node *clone_jump(struct hlsl_ctx *ctx, struct hlsl_ir_jump *src) diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 24f9e1f2..21bda978 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1052,7 +1052,7 @@ struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, const struct hlsl_func_parameters *parameters, const struct hlsl_semantic *semantic, const struct vkd3d_shader_location *loc); -struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, +struct hlsl_ir_node *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct hlsl_block *then_block, struct hlsl_block *else_block, const struct vkd3d_shader_location *loc); struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, const struct vkd3d_shader_location *loc); diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 91bbeae4..cad83239 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -406,10 +406,9 @@ static DWORD add_modifiers(struct hlsl_ctx *ctx, DWORD modifiers, DWORD mod, con
static bool append_conditional_break(struct hlsl_ctx *ctx, struct list *cond_list) { - struct hlsl_ir_node *condition, *not; + struct hlsl_ir_node *condition, *not, *iff; struct hlsl_block then_block; struct hlsl_ir_jump *jump; - struct hlsl_ir_if *iff;
/* E.g. "for (i = 0; ; ++i)". */ if (list_empty(cond_list)) @@ -428,7 +427,7 @@ static bool append_conditional_break(struct hlsl_ctx *ctx, struct list *cond_lis
if (!(iff = hlsl_new_if(ctx, not, &then_block, NULL, &condition->loc))) return false; - list_add_tail(cond_list, &iff->node.entry); + list_add_tail(cond_list, &iff->entry); return true; }
@@ -5257,7 +5256,7 @@ selection_statement: { struct hlsl_ir_node *condition = node_from_list($3); struct hlsl_block then_block, else_block; - struct hlsl_ir_if *instr; + struct hlsl_ir_node *instr;
hlsl_block_init(&then_block); list_move_tail(&then_block.instrs, $5.then_block); @@ -5274,12 +5273,12 @@ selection_statement: struct vkd3d_string_buffer *string;
if ((string = hlsl_type_to_string(ctx, condition->data_type))) - hlsl_error(ctx, &instr->node.loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, + hlsl_error(ctx, &instr->loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "if condition type %s is not scalar.", string->buffer); hlsl_release_string_buffer(ctx, string); } $$ = $3; - list_add_tail($$, &instr->node.entry); + list_add_tail($$, &instr->entry); }
if_body: diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 0e252dac..93623802 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -519,7 +519,7 @@ static void insert_early_return_break(struct hlsl_ctx *ctx, struct hlsl_block then_block; struct hlsl_ir_jump *jump; struct hlsl_ir_load *load; - struct hlsl_ir_if *iff; + struct hlsl_ir_node *iff;
hlsl_block_init(&then_block);
@@ -533,7 +533,7 @@ static void insert_early_return_break(struct hlsl_ctx *ctx,
if (!(iff = hlsl_new_if(ctx, &load->node, &then_block, NULL, &cf_instr->loc))) return; - list_add_after(&load->node.entry, &iff->node.entry); + list_add_after(&load->node.entry, &iff->entry); }
/* Remove HLSL_IR_JUMP_RETURN calls by altering subsequent control flow. */ @@ -678,10 +678,9 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun else if (cf_instr) { struct list *tail = list_tail(&block->instrs); + struct hlsl_ir_node *not, *iff; struct hlsl_block then_block; struct hlsl_ir_load *load; - struct hlsl_ir_node *not; - struct hlsl_ir_if *iff;
/* If we're in a loop, we should have used "break" instead. */ assert(!in_loop); @@ -703,7 +702,7 @@ static bool lower_return(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *fun
if (!(iff = hlsl_new_if(ctx, not, &then_block, NULL, &cf_instr->loc))) return false; - list_add_tail(&block->instrs, &iff->node.entry); + list_add_tail(&block->instrs, &iff->entry); }
return has_early_return; @@ -1932,8 +1931,8 @@ struct hlsl_ir_load *hlsl_add_conditional(struct hlsl_ctx *ctx, struct list *ins struct hlsl_block then_block, else_block; struct hlsl_ir_store *store; struct hlsl_ir_load *load; + struct hlsl_ir_node *iff; struct hlsl_ir_var *var; - struct hlsl_ir_if *iff;
assert(hlsl_types_are_equal(if_true->data_type, if_false->data_type));
@@ -1953,7 +1952,7 @@ struct hlsl_ir_load *hlsl_add_conditional(struct hlsl_ctx *ctx, struct list *ins
if (!(iff = hlsl_new_if(ctx, condition, &then_block, &else_block, &condition->loc))) return NULL; - list_add_tail(instrs, &iff->node.entry); + list_add_tail(instrs, &iff->entry);
if (!(load = hlsl_new_var_load(ctx, var, condition->loc))) return NULL;