From d1614cd6db619e215f1e964cb22f0f8ef968aaaf Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Tue, 17 Feb 2026 11:38:22 -0800 Subject: [PATCH] brw/cmod: Don't propagate from CMP to ADD if there is a write between If either source of the CMP is modified before an appropriate ADD is found, the ADD and the CMP will not have the same result. shader-db: Lunar Lake total instructions in shared programs: 17098815 -> 17098818 (<.01%) instructions in affected programs: 1187 -> 1190 (0.25%) helped: 0 / HURT: 3 total cycles in shared programs: 876858960 -> 876858968 (<.01%) cycles in affected programs: 6878 -> 6886 (0.12%) helped: 0 / HURT: 1 Meteor Lake, DG2, Tiger Lake, Ice Lake, and Skylake had similar results. (Meteor Lake shown) total instructions in shared programs: 20034973 -> 20034984 (<.01%) instructions in affected programs: 4599 -> 4610 (0.24%) helped: 0 / HURT: 11 total cycles in shared programs: 881033088 -> 881033108 (<.01%) cycles in affected programs: 57872 -> 57892 (0.03%) helped: 0 / HURT: 5 fossil-db: All Intel platforms had similar results. (Lunar Lake shown) Totals: Instrs: 918873064 -> 918873269 (+0.00%) CodeSize: 14747338416 -> 14747339360 (+0.00%); split: -0.00%, +0.00% Cycle count: 104141836677 -> 104141840371 (+0.00%); split: -0.00%, +0.00% Totals from 205 (0.01% of 2011421) affected shaders: Instrs: 290415 -> 290620 (+0.07%) CodeSize: 4280704 -> 4281648 (+0.02%); split: -0.01%, +0.03% Cycle count: 18166526 -> 18170220 (+0.02%); split: -0.00%, +0.02% Closes: #14874 Fixes: 020b0055e7a ("i965/fs: Propagate conditional modifiers from compares to adds") Reviewed-by: Matt Turner Tested-by: Jordan Justen Reviewed-by: Jordan Justen Part-of: --- .../compiler/brw/brw_opt_cmod_propagation.cpp | 11 ++++++++++ .../brw/test_opt_cmod_propagation.cpp | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/intel/compiler/brw/brw_opt_cmod_propagation.cpp b/src/intel/compiler/brw/brw_opt_cmod_propagation.cpp index 496379fa8a8..0f66f379dbd 100644 --- a/src/intel/compiler/brw/brw_opt_cmod_propagation.cpp +++ b/src/intel/compiler/brw/brw_opt_cmod_propagation.cpp @@ -99,6 +99,17 @@ cmod_propagate_cmp_to_add(const intel_device_info *devinfo, brw_inst *inst) } foreach_inst_in_block_reverse_starting_from(brw_inst, scan_inst, inst) { + /* If either of the sources of the CMP is modified, the optimization + * cannot occur. This is the case even if the instruction modifying the + * value is an ADD of the appropriate form. + */ + if (regions_overlap(scan_inst->dst, scan_inst->size_written, + inst->src[0], inst->size_read(devinfo, 0)) || + regions_overlap(scan_inst->dst, scan_inst->size_written, + inst->src[1], inst->size_read(devinfo, 1))) { + break; + } + if (scan_inst->opcode == BRW_OPCODE_ADD && !scan_inst->predicate && scan_inst->dst.is_contiguous() && diff --git a/src/intel/compiler/brw/test_opt_cmod_propagation.cpp b/src/intel/compiler/brw/test_opt_cmod_propagation.cpp index c18b3f663eb..93d891be10e 100644 --- a/src/intel/compiler/brw/test_opt_cmod_propagation.cpp +++ b/src/intel/compiler/brw/test_opt_cmod_propagation.cpp @@ -693,6 +693,27 @@ TEST_F(cmod_propagation_test, subtract_delete_compare) EXPECT_SHADERS_MATCH(bld, exp); } +TEST_F(cmod_propagation_test, subtract_no_delete_compare_write_between) +{ + brw_builder bld = make_shader(); + brw_reg dest = vgrf(bld, BRW_TYPE_F); + brw_reg src0 = vgrf(bld, BRW_TYPE_F); + + set_condmod(BRW_CONDITIONAL_L, bld.ADD(dest, src0, brw_imm_f(-1.0f))); + bld.MOV(src0, brw_imm_f(5.0)); + bld.CMP(bld.null_reg_f(), src0, brw_imm_f(1.0f), BRW_CONDITIONAL_L); + + /* = Before = + * 0: add.l.f0(8) dest0:F src0:F -1.0:F + * 1: mov(0) src0:F 5.0:F + * 2: cmp.l.f0(8) null:F src0:F 1.0:F + * + * = After = + * No changes. + */ + EXPECT_NO_PROGRESS(brw_opt_cmod_propagation, bld); +} + TEST_F(cmod_propagation_test, subtract_delete_compare_other_flag) { /* This test is the same as subtract_delete_compare but it explicitly used