From 9e750f00c3bfb86a7f41eb21babea5bb5c32ce4c Mon Sep 17 00:00:00 2001 From: Kenneth Graunke Date: Thu, 14 Mar 2024 02:25:08 -0700 Subject: [PATCH] intel/brw: Make opt_copy_propagation_defs clean up its own trash Copy propagation often eliminates all uses of an instruction. If we detect that we've done so, we can eliminate the instruction ourselves rather than leaving it hanging until the next DCE pass. This saves some CPU time as other passes don't see dead code. Reviewed-by: Caio Oliveira Part-of: --- .../compiler/brw_fs_copy_propagation.cpp | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/intel/compiler/brw_fs_copy_propagation.cpp b/src/intel/compiler/brw_fs_copy_propagation.cpp index 83cae0a5e33..3f82fd49b16 100644 --- a/src/intel/compiler/brw_fs_copy_propagation.cpp +++ b/src/intel/compiler/brw_fs_copy_propagation.cpp @@ -1803,9 +1803,10 @@ bool brw_fs_opt_copy_propagation_defs(fs_visitor &s) { const brw::def_analysis &defs = s.def_analysis.require(); + unsigned *uses_deleted = new unsigned[defs.count()](); bool progress = false; - foreach_block_and_inst(block, fs_inst, inst, s.cfg) { + foreach_block_and_inst_safe(block, fs_inst, inst, s.cfg) { /* Try propagating into this instruction. */ bool instruction_progress = false; @@ -1815,13 +1816,23 @@ brw_fs_opt_copy_propagation_defs(fs_visitor &s) if (!def || def->saturate) continue; + bool source_progress = false; + if (def->opcode == SHADER_OPCODE_LOAD_PAYLOAD) { if (inst->size_read(i) == def->size_written && def->src[0].file != BAD_FILE && def->src[0].file != IMM && is_identity_payload(def->src[0].file, def)) { - instruction_progress |= + source_progress = try_copy_propagate_def(s.compiler, s.alloc, def, def->src[0], inst, i, s.max_polygons); + + if (source_progress) { + instruction_progress = true; + ++uses_deleted[def->dst.nr]; + if (defs.get_use_count(def->dst) == uses_deleted[def->dst.nr]) + def->remove(defs.get_block(def->dst), true); + } + continue; } } @@ -1830,15 +1841,22 @@ brw_fs_opt_copy_propagation_defs(fs_visitor &s) find_value_for_offset(def, inst->src[i], inst->size_read(i)); if (val.file == IMM) { - instruction_progress |= + source_progress = try_constant_propagate_def(def, val, inst, i); } else if (val.file == VGRF || val.file == ATTR || val.file == UNIFORM || (val.file == FIXED_GRF && val.is_contiguous())) { - instruction_progress |= + source_progress = try_copy_propagate_def(s.compiler, s.alloc, def, val, inst, i, s.max_polygons); } + + if (source_progress) { + instruction_progress = true; + ++uses_deleted[def->dst.nr]; + if (defs.get_use_count(def->dst) == uses_deleted[def->dst.nr]) + def->remove(defs.get_block(def->dst), true); + } } if (instruction_progress) { @@ -1848,9 +1866,12 @@ brw_fs_opt_copy_propagation_defs(fs_visitor &s) } if (progress) { + s.cfg->adjust_block_ips(); s.invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW | DEPENDENCY_INSTRUCTION_DETAIL); } + delete [] uses_deleted; + return progress; }