From 21e9a5e373670266746f450319095400d9083e5d Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Tue, 10 Dec 2024 13:22:21 -0800 Subject: [PATCH] brw/algebraic: Fix ADD constant folding Some callers of brw_constant_fold_instruction depend on the result being a MOV of immediate when progress is made. Previously `ADD dst:D src0:D 0:D` would be converted to `MOV dst:D src0:D`. There was also no handling for `ADD dst:D imm0:D imm1:D`. Reviewed-by: Caio Oliveira Reviewed-by: Matt Turner Fixes: 2cc1575a31d ("brw/algebraic: Refactor constant folding out of brw_fs_opt_algebraic") (cherry picked from commit 086e83ccd99a084dd5392725042c7322151eec23) Part-of: --- .pick_status.json | 2 +- src/intel/compiler/brw_fs_opt_algebraic.cpp | 35 ++++++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index dda2a774674..abaf296ed5f 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -484,7 +484,7 @@ "description": "brw/algebraic: Fix ADD constant folding", "nominated": true, "nomination_type": 2, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "2cc1575a31d2483e71c473d19618ddbd1ea9e1a4", "notes": null diff --git a/src/intel/compiler/brw_fs_opt_algebraic.cpp b/src/intel/compiler/brw_fs_opt_algebraic.cpp index 94f52d574f4..089da297a35 100644 --- a/src/intel/compiler/brw_fs_opt_algebraic.cpp +++ b/src/intel/compiler/brw_fs_opt_algebraic.cpp @@ -71,26 +71,22 @@ brw_constant_fold_instruction(const intel_device_info *devinfo, fs_inst *inst) switch (inst->opcode) { case BRW_OPCODE_ADD: - if (inst->src[1].file != IMM) + if (inst->src[0].file != IMM || inst->src[1].file != IMM) break; - if (brw_type_is_int(inst->src[1].type) && - inst->src[1].is_zero()) { - inst->opcode = BRW_OPCODE_MOV; - inst->resize_sources(1); - progress = true; - break; - } + if (brw_type_is_int(inst->src[0].type)) { + const uint64_t src0 = src_as_uint(inst->src[0]); + const uint64_t src1 = src_as_uint(inst->src[1]); - if (inst->src[0].file == IMM) { + inst->src[0] = brw_imm_for_type(src0 + src1, inst->dst.type); + } else { assert(inst->src[0].type == BRW_TYPE_F); - inst->opcode = BRW_OPCODE_MOV; inst->src[0].f += inst->src[1].f; - inst->resize_sources(1); - progress = true; - break; } + inst->opcode = BRW_OPCODE_MOV; + inst->resize_sources(1); + progress = true; break; @@ -242,6 +238,18 @@ brw_fs_opt_algebraic(fs_visitor &s) foreach_block_and_inst_safe(block, fs_inst, inst, s.cfg) { switch (inst->opcode) { + case BRW_OPCODE_ADD: + if (brw_constant_fold_instruction(devinfo, inst)) { + progress = true; + } else if (brw_type_is_int(inst->src[1].type) && + inst->src[1].is_zero()) { + inst->opcode = BRW_OPCODE_MOV; + inst->resize_sources(1); + progress = true; + } + + break; + case BRW_OPCODE_MOV: if ((inst->conditional_mod == BRW_CONDITIONAL_Z || inst->conditional_mod == BRW_CONDITIONAL_NZ) && @@ -277,7 +285,6 @@ brw_fs_opt_algebraic(fs_visitor &s) break; case BRW_OPCODE_MUL: - case BRW_OPCODE_ADD: case BRW_OPCODE_AND: if (brw_constant_fold_instruction(devinfo, inst)) progress = true;