vkd3d-shader/hlsl: Implement normalization of binary expressions.

We normalize binary expressions by attempting to group constants
together, in order to facilitate further simplification of the
expressions.

For any binary operator OP, non-constants x, y, and constants a, b, we
apply the following rewrite rules:

  a OP x -> x OP a, if OP is commutative.

  (x OP a) OP b -> x OP (a OP b), if OP is associative.

  (x OP a) OP y -> (x OP y) OP a, if OP is associative and commutative.

  x OP (y OP a) -> (x OP y) OP a, if OP is associative.

Note that we consider floating point operations to be
non-associative.
This commit is contained in:
Shaun Ren
2024-12-06 17:07:13 -05:00
committed by Henri Verbeet
parent 65b67e84a8
commit 2c9cf7c78b
Notes: Henri Verbeet 2024-12-11 15:37:25 +01:00
Approved-by: Elizabeth Figura (@zfigura)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1307
4 changed files with 161 additions and 0 deletions

View File

@@ -1544,6 +1544,149 @@ bool hlsl_fold_constant_identities(struct hlsl_ctx *ctx, struct hlsl_ir_node *in
return false;
}
static bool is_op_associative(enum hlsl_ir_expr_op op, enum hlsl_base_type type)
{
switch (op)
{
case HLSL_OP2_ADD:
case HLSL_OP2_MUL:
return type == HLSL_TYPE_INT || type == HLSL_TYPE_UINT;
case HLSL_OP2_BIT_AND:
case HLSL_OP2_BIT_OR:
case HLSL_OP2_BIT_XOR:
case HLSL_OP2_LOGIC_AND:
case HLSL_OP2_LOGIC_OR:
case HLSL_OP2_MAX:
case HLSL_OP2_MIN:
return true;
default:
return false;
}
}
static bool is_op_commutative(enum hlsl_ir_expr_op op)
{
switch (op)
{
case HLSL_OP2_ADD:
case HLSL_OP2_BIT_AND:
case HLSL_OP2_BIT_OR:
case HLSL_OP2_BIT_XOR:
case HLSL_OP2_DOT:
case HLSL_OP2_LOGIC_AND:
case HLSL_OP2_LOGIC_OR:
case HLSL_OP2_MAX:
case HLSL_OP2_MIN:
case HLSL_OP2_MUL:
return true;
default:
return false;
}
}
bool hlsl_normalize_binary_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
{
struct hlsl_ir_node *arg1 , *arg2;
struct hlsl_ir_expr *expr;
enum hlsl_base_type type;
enum hlsl_ir_expr_op op;
bool progress = false;
if (instr->type != HLSL_IR_EXPR)
return false;
expr = hlsl_ir_expr(instr);
if (instr->data_type->class > HLSL_CLASS_VECTOR)
return false;
arg1 = expr->operands[0].node;
arg2 = expr->operands[1].node;
type = instr->data_type->e.numeric.type;
op = expr->op;
if (!arg1 || !arg2)
return false;
if (is_op_commutative(op) && arg1->type == HLSL_IR_CONSTANT && arg2->type != HLSL_IR_CONSTANT)
{
/* a OP x -> x OP a */
struct hlsl_ir_node *tmp = arg1;
arg1 = arg2;
arg2 = tmp;
progress = true;
}
if (is_op_associative(op, type))
{
struct hlsl_ir_expr *e1 = arg1->type == HLSL_IR_EXPR ? hlsl_ir_expr(arg1) : NULL;
struct hlsl_ir_expr *e2 = arg2->type == HLSL_IR_EXPR ? hlsl_ir_expr(arg2) : NULL;
if (e1 && e1->op == op && e1->operands[0].node->type != HLSL_IR_CONSTANT
&& e1->operands[1].node->type == HLSL_IR_CONSTANT)
{
if (arg2->type == HLSL_IR_CONSTANT)
{
/* (x OP a) OP b -> x OP (a OP b) */
struct hlsl_ir_node *ab;
if (!(ab = hlsl_new_binary_expr(ctx, op, e1->operands[1].node, arg2)))
return false;
list_add_before(&instr->entry, &ab->entry);
arg1 = e1->operands[0].node;
arg2 = ab;
progress = true;
}
else if (is_op_commutative(op))
{
/* (x OP a) OP y -> (x OP y) OP a */
struct hlsl_ir_node *xy;
if (!(xy = hlsl_new_binary_expr(ctx, op, e1->operands[0].node, arg2)))
return false;
list_add_before(&instr->entry, &xy->entry);
arg1 = xy;
arg2 = e1->operands[1].node;
progress = true;
}
}
if (!progress && arg1->type != HLSL_IR_CONSTANT && e2 && e2->op == op
&& e2->operands[0].node->type != HLSL_IR_CONSTANT && e2->operands[1].node->type == HLSL_IR_CONSTANT)
{
/* x OP (y OP a) -> (x OP y) OP a */
struct hlsl_ir_node *xy;
if (!(xy = hlsl_new_binary_expr(ctx, op, arg1, e2->operands[0].node)))
return false;
list_add_before(&instr->entry, &xy->entry);
arg1 = xy;
arg2 = e2->operands[1].node;
progress = true;
}
}
if (progress)
{
struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS] = {arg1, arg2};
struct hlsl_ir_node *res;
if (!(res = hlsl_new_expr(ctx, op, operands, instr->data_type, &instr->loc)))
return false;
list_add_before(&instr->entry, &res->entry);
hlsl_replace_node(instr, res);
}
return progress;
}
bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
{
struct hlsl_constant_value value;