vkd3d-shader/hlsl: Fold x + 0 identities.

This commit is contained in:
Francisco Casas 2024-04-19 12:57:38 -04:00 committed by Alexandre Julliard
parent c43c900a30
commit b14f935d0f
Notes: Alexandre Julliard 2024-04-30 23:13:01 +02:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Henri Verbeet (@hverbeet)
Approved-by: Zebediah Figura (@zfigura)
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/809
3 changed files with 93 additions and 0 deletions

View File

@ -1422,6 +1422,7 @@ struct hlsl_reg hlsl_reg_from_deref(struct hlsl_ctx *ctx, const struct hlsl_dere
bool hlsl_copy_propagation_execute(struct hlsl_ctx *ctx, struct hlsl_block *block); bool hlsl_copy_propagation_execute(struct hlsl_ctx *ctx, struct hlsl_block *block);
bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context); bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
bool hlsl_fold_constant_identities(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context); bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
bool hlsl_transform_ir(struct hlsl_ctx *ctx, bool (*func)(struct hlsl_ctx *ctx, struct hlsl_ir_node *, void *), bool hlsl_transform_ir(struct hlsl_ctx *ctx, bool (*func)(struct hlsl_ctx *ctx, struct hlsl_ir_node *, void *),
struct hlsl_block *block, void *context); struct hlsl_block *block, void *context);

View File

@ -5427,6 +5427,7 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry
do do
{ {
progress = hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL); progress = hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL);
progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_identities, body, NULL);
progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_swizzles, body, NULL); progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_swizzles, body, NULL);
progress |= hlsl_copy_propagation_execute(ctx, body); progress |= hlsl_copy_propagation_execute(ctx, body);
progress |= hlsl_transform_ir(ctx, fold_swizzle_chains, body, NULL); progress |= hlsl_transform_ir(ctx, fold_swizzle_chains, body, NULL);

View File

@ -1396,6 +1396,97 @@ bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
return success; return success;
} }
static bool constant_is_zero(struct hlsl_ir_constant *const_arg)
{
struct hlsl_type *data_type = const_arg->node.data_type;
unsigned int k;
for (k = 0; k < data_type->dimx; ++k)
{
switch (data_type->base_type)
{
case HLSL_TYPE_FLOAT:
case HLSL_TYPE_HALF:
if (const_arg->value.u[k].f != 0.0f)
return false;
break;
case HLSL_TYPE_DOUBLE:
if (const_arg->value.u[k].d != 0.0)
return false;
break;
case HLSL_TYPE_UINT:
case HLSL_TYPE_INT:
case HLSL_TYPE_BOOL:
if (const_arg->value.u[k].u != 0)
return false;
break;
default:
return false;
}
}
return true;
}
bool hlsl_fold_constant_identities(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
{
struct hlsl_ir_constant *const_arg = NULL;
struct hlsl_ir_node *mut_arg = NULL;
struct hlsl_ir_node *res_node;
struct hlsl_ir_expr *expr;
unsigned int i;
if (instr->type != HLSL_IR_EXPR)
return false;
expr = hlsl_ir_expr(instr);
if (instr->data_type->class > HLSL_CLASS_VECTOR)
return false;
/* Verify that the expression has two operands. */
for (i = 0; i < ARRAY_SIZE(expr->operands); ++i)
{
if (!!expr->operands[i].node != (i < 2))
return false;
}
if (expr->operands[0].node->type == HLSL_IR_CONSTANT)
{
const_arg = hlsl_ir_constant(expr->operands[0].node);
mut_arg = expr->operands[1].node;
}
else if (expr->operands[1].node->type == HLSL_IR_CONSTANT)
{
mut_arg = expr->operands[0].node;
const_arg = hlsl_ir_constant(expr->operands[1].node);
}
else
{
return false;
}
res_node = NULL;
switch (expr->op)
{
case HLSL_OP2_ADD:
if (constant_is_zero(const_arg))
res_node = mut_arg;
break;
default:
break;
}
if (res_node)
{
hlsl_replace_node(&expr->node, res_node);
return true;
}
return false;
}
bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
{ {
struct hlsl_constant_value value; struct hlsl_constant_value value;