From 153b7c8460fb201491849facd0d7dadc9db9ec17 Mon Sep 17 00:00:00 2001 From: Francisco Casas Date: Wed, 29 Jan 2025 14:14:24 -0300 Subject: [PATCH] vkd3d-shader/hlsl: Run folding passes again after lower_nonconstant_array_loads. This is because lower_nonconstant_array_loads() can potentially turn nonconstant loads into constant loads, allowing copy-prop to turn these loads into previous instructions, which might help other passes as well. This patch lowers the number of required temps for the following ps_2_0 shader from 19 to 16: int i; float3x3 mats[4]; float4 main() : sv_target { return mul(mats[i], float3(1, 2, 3)).xyzz; } --- libs/vkd3d-shader/hlsl_codegen.c | 33 ++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index ea81d81a..b445b255 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -7024,6 +7024,24 @@ void hlsl_lower_index_loads(struct hlsl_ctx *ctx, struct hlsl_block *body) lower_ir(ctx, lower_index_loads, body); } +static void hlsl_run_folding_passes(struct hlsl_ctx *ctx, struct hlsl_block *body) +{ + bool progress; + + hlsl_transform_ir(ctx, fold_redundant_casts, body, NULL); + do + { + 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_normalize_binary_exprs, body, NULL); + progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_swizzles, body, NULL); + progress |= hlsl_copy_propagation_execute(ctx, body); + progress |= hlsl_transform_ir(ctx, fold_swizzle_chains, body, NULL); + progress |= hlsl_transform_ir(ctx, remove_trivial_swizzles, body, NULL); + progress |= hlsl_transform_ir(ctx, remove_trivial_conditional_branches, body, NULL); + } while (progress); +} + void hlsl_run_const_passes(struct hlsl_ctx *ctx, struct hlsl_block *body) { bool progress; @@ -7048,19 +7066,8 @@ void hlsl_run_const_passes(struct hlsl_ctx *ctx, struct hlsl_block *body) lower_ir(ctx, lower_int_abs, body); lower_ir(ctx, lower_casts_to_bool, body); lower_ir(ctx, lower_float_modulus, body); - hlsl_transform_ir(ctx, fold_redundant_casts, body, NULL); - do - { - 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_normalize_binary_exprs, body, NULL); - progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_swizzles, body, NULL); - progress |= hlsl_copy_propagation_execute(ctx, body); - progress |= hlsl_transform_ir(ctx, fold_swizzle_chains, body, NULL); - progress |= hlsl_transform_ir(ctx, remove_trivial_swizzles, body, NULL); - progress |= hlsl_transform_ir(ctx, remove_trivial_conditional_branches, body, NULL); - } while (progress); + hlsl_run_folding_passes(ctx, body); } static void generate_vsir_signature_entry(struct hlsl_ctx *ctx, struct vsir_program *program, @@ -12577,6 +12584,8 @@ static void process_entry_function(struct hlsl_ctx *ctx, lower_ir(ctx, validate_nonconstant_vector_store_derefs, body); + hlsl_run_folding_passes(ctx, body); + do compute_liveness(ctx, entry_func); while (hlsl_transform_ir(ctx, dce, body, NULL));