From 245430002aa31b0ee53e278a7d6beb7c78a22f43 Mon Sep 17 00:00:00 2001 From: Shaun Ren Date: Mon, 28 Jul 2025 21:51:09 -0400 Subject: [PATCH] vkd3d-shader/hlsl: Fold some general unary identities. The following unary identities are applied: ||x|| -> |x| |-x| -> |x| ~(~x) -> x f(g(x)) -> g(x), where f(), g() are floor() or ceil() functions. -(-x) -> x !!x -> x !(x == y) -> x != y, !(x < y) -> x >= y, etc (for integers). --- libs/vkd3d-shader/hlsl.h | 6 + libs/vkd3d-shader/hlsl_codegen.c | 125 +++++++++++++++++- .../hlsl/arithmetic-float-uniform.shader_test | 31 +++++ tests/hlsl/arithmetic-uint.shader_test | 15 +++ tests/hlsl/logic-operations.shader_test | 22 +++ 5 files changed, 197 insertions(+), 2 deletions(-) diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index d86594a5e..0cb5d1b90 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1541,6 +1541,12 @@ static inline bool hlsl_var_has_buffer_offset_register_reservation(struct hlsl_c return var->reg_reservation.reg_type == 'c' && var->buffer == ctx->globals_buffer; } +static inline bool hlsl_is_comparison_op(enum hlsl_ir_expr_op op) +{ + return op == HLSL_OP2_EQUAL || op == HLSL_OP2_GEQUAL + || op == HLSL_OP2_LESS || op == HLSL_OP2_NEQUAL; +} + char *hlsl_sprintf_alloc(struct hlsl_ctx *ctx, const char *fmt, ...) VKD3D_PRINTF_FUNC(2, 3); const char *debug_hlsl_expr_op(enum hlsl_ir_expr_op op); diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 5ca16e263..9d02f831c 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -5029,8 +5029,7 @@ static bool lower_comparison_operators(struct hlsl_ctx *ctx, struct hlsl_ir_node if (instr->type != HLSL_IR_EXPR) return false; expr = hlsl_ir_expr(instr); - if (expr->op != HLSL_OP2_EQUAL && expr->op != HLSL_OP2_NEQUAL && expr->op != HLSL_OP2_LESS - && expr->op != HLSL_OP2_GEQUAL) + if (!hlsl_is_comparison_op(expr->op)) return false; arg1 = expr->operands[0].node; @@ -8266,6 +8265,127 @@ void hlsl_lower_index_loads(struct hlsl_ctx *ctx, struct hlsl_block *body) lower_ir(ctx, lower_index_loads, body); } +static enum hlsl_ir_expr_op invert_comparison_op(enum hlsl_ir_expr_op op) +{ + switch (op) + { + case HLSL_OP2_EQUAL: + return HLSL_OP2_NEQUAL; + + case HLSL_OP2_GEQUAL: + return HLSL_OP2_LESS; + + case HLSL_OP2_LESS: + return HLSL_OP2_GEQUAL; + + case HLSL_OP2_NEQUAL: + return HLSL_OP2_EQUAL; + + default: + vkd3d_unreachable(); + } +} + +static bool fold_unary_identities(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{ + struct hlsl_ir_node *res = NULL; + struct hlsl_ir_expr *expr, *x; + + if (instr->type != HLSL_IR_EXPR) + return false; + + if (instr->data_type->class > HLSL_CLASS_VECTOR) + return false; + + expr = hlsl_ir_expr(instr); + if (!expr->operands[0].node) + return false; + + if (expr->operands[0].node->type != HLSL_IR_EXPR) + return false; + x = hlsl_ir_expr(expr->operands[0].node); + + switch (expr->op) + { + case HLSL_OP1_ABS: + if (x->op == HLSL_OP1_ABS) + { + /* ||x|| -> |x| */ + hlsl_replace_node(instr, &x->node); + return true; + } + + if (x->op == HLSL_OP1_NEG) + { + /* |-x| -> |x| */ + hlsl_src_remove(&expr->operands[0]); + hlsl_src_from_node(&expr->operands[0], x->operands[0].node); + return true; + } + break; + + case HLSL_OP1_BIT_NOT: + if (x->op == HLSL_OP1_BIT_NOT) + { + /* ~(~x) -> x */ + hlsl_replace_node(instr, x->operands[0].node); + return true; + } + break; + + case HLSL_OP1_CEIL: + case HLSL_OP1_FLOOR: + if (x->op == HLSL_OP1_CEIL || x->op == HLSL_OP1_FLOOR) + { + /* f(g(x)) -> g(x), where f(), g() are floor() or ceil() functions. */ + hlsl_replace_node(instr, &x->node); + return true; + } + break; + + case HLSL_OP1_NEG: + if (x->op == HLSL_OP1_NEG) + { + /* -(-x) -> x */ + hlsl_replace_node(instr, x->operands[0].node); + return true; + } + break; + + case HLSL_OP1_LOGIC_NOT: + if (x->op == HLSL_OP1_LOGIC_NOT) + { + /* !!x -> x */ + hlsl_replace_node(instr, x->operands[0].node); + return true; + } + + if (hlsl_is_comparison_op(x->op) + && hlsl_base_type_is_integer(x->operands[0].node->data_type->e.numeric.type) + && hlsl_base_type_is_integer(x->operands[1].node->data_type->e.numeric.type)) + { + struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS] = {x->operands[0].node, x->operands[1].node}; + struct hlsl_block block; + + hlsl_block_init(&block); + + /* !(x == y) -> x != y, !(x < y) -> x >= y, etc. */ + res = hlsl_block_add_expr(ctx, &block, invert_comparison_op(x->op), + operands, instr->data_type, &instr->loc); + + list_move_before(&instr->entry, &block.instrs); + hlsl_replace_node(instr, res); + return true; + } + + break; + + default: + break; + } + + return false; +} static bool simplify_exprs(struct hlsl_ctx *ctx, struct hlsl_block *block) { @@ -8275,6 +8395,7 @@ static bool simplify_exprs(struct hlsl_ctx *ctx, struct hlsl_block *block) { progress = hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, block, NULL); progress |= hlsl_transform_ir(ctx, hlsl_normalize_binary_exprs, block, NULL); + progress |= hlsl_transform_ir(ctx, fold_unary_identities, block, NULL); progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_identities, block, NULL); progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_swizzles, block, NULL); diff --git a/tests/hlsl/arithmetic-float-uniform.shader_test b/tests/hlsl/arithmetic-float-uniform.shader_test index 5c25bd6d7..2580d1267 100644 --- a/tests/hlsl/arithmetic-float-uniform.shader_test +++ b/tests/hlsl/arithmetic-float-uniform.shader_test @@ -127,6 +127,37 @@ uniform 8 float4 1.00000007 -1.0 0.5 -0.5 todo(msl & sm>=6) draw quad probe (0, 0) rgba (2.62500048, 209.5, 17.0, 224.5) 1 +[pixel shader] +uniform float4 a; + +float4 main() : sv_target +{ + return float4(abs(abs(a.x)), abs(-a.y), -(-a.z), 0); +} + +[test] +uniform 0 float4 -1.0 -2.0 -3.0 0.0 +draw quad +probe (0, 0) f32(1.0, 2.0, -3.0, 0.0) + +[pixel shader] +uniform float4 a; + +float4 main() : sv_target +{ + float4 ret; + ret.x = floor(floor(a.x)); + ret.y = floor(ceil(a.y)); + ret.z = ceil(floor(a.z)); + ret.w = ceil(ceil(a.w)); + return ret; +} + +[test] +uniform 0 float4 1.5 1.5 -1.5 -1.5 +draw quad +probe (0, 0) f32(1.0, 2.0, -2.0, -1.0) + [require] shader model >= 5.0 float64 diff --git a/tests/hlsl/arithmetic-uint.shader_test b/tests/hlsl/arithmetic-uint.shader_test index 9552a8925..087bb2e7c 100644 --- a/tests/hlsl/arithmetic-uint.shader_test +++ b/tests/hlsl/arithmetic-uint.shader_test @@ -89,6 +89,21 @@ uniform 0 uint 7 draw quad probe (0, 0) rgba(38.0, 5.0, 7.0, 0.0) +[pixel shader] +uniform uint x; + +float4 main() : SV_TARGET +{ + uint4 res = 0; + res.x = ~(~x); + return res; +} + +[test] +uniform 0 uint 7 +draw quad +probe (0, 0) f32(7.0, 0.0, 0.0, 0.0) + [rtv 0] format r32g32b32a32-uint size (2d, 640, 480) diff --git a/tests/hlsl/logic-operations.shader_test b/tests/hlsl/logic-operations.shader_test index f36f08b34..1e0870875 100644 --- a/tests/hlsl/logic-operations.shader_test +++ b/tests/hlsl/logic-operations.shader_test @@ -179,3 +179,25 @@ if(sm>=4) uniform 0 int -1 if(sm>=4) uniform 1 int 3 todo(msl & sm>=6) draw quad probe (0, 0) rgba (0.0, 1.0, 0.0, 0.0) + +[pixel shader] +int a, b; + +float4 main() : SV_TARGET +{ + return float4(!(a == b), !(a != b), !(a < b), !(a >= b)); +} + +[test] +if(sm<4) uniform 0 float 0 +if(sm<4) uniform 4 float 5 +if(sm>=4) uniform 0 int 0 +if(sm>=4) uniform 1 int 5 +todo(msl & sm>=6) draw quad +probe (0, 0) f32(1.0, 0.0, 0.0, 1.0) +if(sm<4) uniform 0 float -1 +if(sm<4) uniform 4 float -1 +if(sm>=4) uniform 0 int -1 +if(sm>=4) uniform 1 int -1 +todo(msl & sm>=6) draw quad +probe (0, 0) f32(0.0, 1.0, 1.0, 0.0)