From 3377f2445a20d15eee95cf748c9fa52ab13a13a7 Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Thu, 18 Mar 2021 00:22:22 -0500 Subject: [PATCH] vkd3d-shader: Eliminate unused assignments. Signed-off-by: Zebediah Figura Signed-off-by: Matteo Bruni Signed-off-by: Henri Verbeet Signed-off-by: Alexandre Julliard --- libs/vkd3d-shader/hlsl_codegen.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 59514e4c..b9f753a1 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -244,6 +244,19 @@ static bool dce(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) break; case HLSL_IR_ASSIGNMENT: + { + struct hlsl_ir_assignment *assignment = hlsl_ir_assignment(instr); + struct hlsl_ir_var *var = assignment->lhs.var; + + if (var->last_read < instr->index) + { + list_remove(&instr->entry); + hlsl_free_instr(instr); + return true; + } + break; + } + case HLSL_IR_IF: case HLSL_IR_JUMP: case HLSL_IR_LOOP: @@ -371,8 +384,18 @@ static void compute_liveness_recurse(struct list *instrs, unsigned int loop_firs static void compute_liveness(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func) { + struct hlsl_scope *scope; struct hlsl_ir_var *var; + /* Index 0 means unused; index 1 means function entry, so start at 2. */ + index_instructions(entry_func->body, 2); + + LIST_FOR_EACH_ENTRY(scope, &ctx->scopes, struct hlsl_scope, entry) + { + LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry) + var->first_write = var->last_read = 0; + } + LIST_FOR_EACH_ENTRY(var, &ctx->globals->vars, struct hlsl_ir_var, scope_entry) { var->first_write = 1; @@ -399,16 +422,16 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun while (transform_ir(ctx, fold_redundant_casts, entry_func->body, NULL)); while (transform_ir(ctx, split_struct_copies, entry_func->body, NULL)); while (transform_ir(ctx, fold_constants, entry_func->body, NULL)); + + do + compute_liveness(ctx, entry_func); while (transform_ir(ctx, dce, entry_func->body, NULL)); - /* Index 0 means unused; index 1 means function entry, so start at 2. */ - index_instructions(entry_func->body, 2); + compute_liveness(ctx, entry_func); if (TRACE_ON()) rb_for_each_entry(&ctx->functions, dump_function, NULL); - compute_liveness(ctx, entry_func); - if (ctx->failed) return VKD3D_ERROR_INVALID_SHADER; return VKD3D_ERROR_NOT_IMPLEMENTED;