mesa/src/intel/compiler/brw_load_reg.cpp

185 lines
6 KiB
C++
Raw Normal View History

brw: Add passes to generate and lower load_reg v2: Add support for WE_all instructions... this already just worked, so I only had to delete the check and the FINISHME comment. v3: Use logic more like def_analysis::update_for_reads to determine when to not insert LOAD_REG instructions. Based on a suggestion by Ken. v4: Eliminate "store" from all the names since STORE_REG does not exist anymore. Fold insert_load_reg into brw_insert_load_reg. Elminate extra call to s.def_analysis.require() after progress. Pull a loop-invariant check out of the inst->srouces loop. Drop call to brw_opt_split_virtual_grfs after lowering load_reg. All suggested by Caio. v5: Assert that LOAD_REG doesn't already exist in brw_insert_load_reg. Update comment before fully_defines. Both suggested by Caio. v6: Don't explicitly special-case SHADER_OPCODE_MEMORY_STORE_LOGICAL. Move the inst->dst.file != VGRF check earlier to avoid the loop over sources. Both suggested by Ken. Move the call the brw_insert_load_reg a little bit later, and explain why it's at that location. Suggested by Caio. v7: Many changes to the for-each-source loop in brw_insert_load_reg. Removes incorrect multiplication of s.alloc.sizes with reg_unit. Adds checks for matching SIMD size and NoMask in the search for pre-existing LOAD_REG of same value. v8: Add some unit tests. Suggested by Caio. shader-db: Lunar Lake total instructions in shared programs: 16923237 -> 16921895 (<.01%) instructions in affected programs: 450565 -> 449223 (-0.30%) helped: 251 / HURT: 377 total cycles in shared programs: 910428418 -> 889920590 (-2.25%) cycles in affected programs: 719248184 -> 698740356 (-2.85%) helped: 9076 / HURT: 9082 total fills in shared programs: 2242 -> 2218 (-1.07%) fills in affected programs: 116 -> 92 (-20.69%) helped: 2 / HURT: 0 total sends in shared programs: 848635 -> 848421 (-0.03%) sends in affected programs: 810 -> 596 (-26.42%) helped: 10 / HURT: 0 LOST: 82 GAINED: 78 Meteor Lake and DG2 had similar results. (Meteor Lake shown) total instructions in shared programs: 19875784 -> 19871694 (-0.02%) instructions in affected programs: 1050091 -> 1046001 (-0.39%) helped: 251 / HURT: 2403 total cycles in shared programs: 905328238 -> 882446458 (-2.53%) cycles in affected programs: 682736344 -> 659854564 (-3.35%) helped: 7869 / HURT: 7911 total spills in shared programs: 5512 -> 5032 (-8.71%) spills in affected programs: 1830 -> 1350 (-26.23%) helped: 8 / HURT: 0 total fills in shared programs: 5648 -> 4782 (-15.33%) fills in affected programs: 3312 -> 2446 (-26.15%) helped: 8 / HURT: 0 total sends in shared programs: 1032942 -> 1032722 (-0.02%) sends in affected programs: 572 -> 352 (-38.46%) helped: 10 / HURT: 0 LOST: 138 GAINED: 53 Tiger Lake total instructions in shared programs: 19711930 -> 19715591 (0.02%) instructions in affected programs: 1040623 -> 1044284 (0.35%) helped: 317 / HURT: 2474 total cycles in shared programs: 862988990 -> 860573870 (-0.28%) cycles in affected programs: 612392461 -> 609977341 (-0.39%) helped: 7447 / HURT: 7686 total sends in shared programs: 1034763 -> 1034555 (-0.02%) sends in affected programs: 784 -> 576 (-26.53%) helped: 8 / HURT: 0 LOST: 56 GAINED: 143 Ice Lake and Skylake had similar results. (Ice Lake shown) total instructions in shared programs: 20545461 -> 20545220 (<.01%) instructions in affected programs: 422405 -> 422164 (-0.06%) helped: 180 / HURT: 459 total cycles in shared programs: 872697345 -> 866874523 (-0.67%) cycles in affected programs: 573117917 -> 567295095 (-1.02%) helped: 6783 / HURT: 6980 total spills in shared programs: 4335 -> 4336 (0.02%) spills in affected programs: 90 -> 91 (1.11%) helped: 1 / HURT: 2 total fills in shared programs: 4194 -> 4196 (0.05%) fills in affected programs: 463 -> 465 (0.43%) helped: 1 / HURT: 2 total sends in shared programs: 1079446 -> 1079238 (-0.02%) sends in affected programs: 784 -> 576 (-26.53%) helped: 8 / HURT: 0 LOST: 117 GAINED: 37 fossil-db: All Intel platforms had similar results. (Lunar Lake shown) Totals: Instrs: 209708136 -> 209695617 (-0.01%); split: -0.02%, +0.01% Send messages: 10927753 -> 10927640 (-0.00%) Cycle count: 30540172048 -> 30427084732 (-0.37%); split: -0.99%, +0.62% Spill count: 511621 -> 510932 (-0.13%); split: -0.22%, +0.08% Fill count: 621166 -> 618440 (-0.44%); split: -0.56%, +0.12% Scratch Memory Size: 35574784 -> 35648512 (+0.21%); split: -0.06%, +0.26% Max live registers: 65453860 -> 65453140 (-0.00%); split: -0.00%, +0.00% Non SSA regs after NIR: 75374990 -> 35195764 (-53.31%) Totals from 503284 (71.25% of 706391) affected shaders: Instrs: 180203778 -> 180191259 (-0.01%); split: -0.02%, +0.01% Send messages: 9699732 -> 9699619 (-0.00%) Cycle count: 30080349592 -> 29967262276 (-0.38%); split: -1.01%, +0.63% Spill count: 511584 -> 510895 (-0.13%); split: -0.22%, +0.08% Fill count: 621120 -> 618394 (-0.44%); split: -0.56%, +0.12% Scratch Memory Size: 35443712 -> 35517440 (+0.21%); split: -0.06%, +0.27% Max live registers: 52566092 -> 52565372 (-0.00%); split: -0.01%, +0.00% Non SSA regs after NIR: 70110949 -> 29931723 (-57.31%) Reviewed-by: Caio Oliveira <caio.oliveira@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/31497>
2024-09-18 18:11:13 -07:00
/*
* Copyright © 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*/
#include "brw_shader.h"
#include "brw_cfg.h"
#include "brw_analysis.h"
#include "brw_builder.h"
/* Duplicated from brw_def_analysis::fully_defines. */
static bool
fully_defines(const brw_shader &s, brw_inst *inst)
{
return s.alloc.sizes[inst->dst.nr] * REG_SIZE == inst->size_written &&
!inst->is_partial_write();
}
bool
brw_insert_load_reg(brw_shader &s)
{
bool progress = false;
const brw_def_analysis &defs = s.def_analysis.require();
foreach_block_and_inst_safe(block, brw_inst, inst, s.cfg) {
/* These should not exist yet. */
assert(inst->opcode != SHADER_OPCODE_LOAD_REG);
/* These opcodes may have the right source and destination patterns to
* have their sources replaced by load_reg, but these instructions are
* special and / or wierd. They should not be modified.
*/
if (inst->opcode == SHADER_OPCODE_UNDEF ||
inst->opcode == BRW_OPCODE_DPAS) {
continue;
}
/* If the destination is non-VGRF adding load_reg instructions will not
* help. If the destination is already SSA, nothing needs to be done.
*/
if (inst->dst.file != VGRF || defs.get(inst->dst) != NULL)
continue;
/* If there is a source that would cause def_analysis::update_for_reads
* to mark the def as invalid, adding load_reg for the sources will not
* help.
*/
if (inst->reads_accumulator_implicitly())
continue;
bool bad_source = false;
for (int i = 0; i < inst->sources; i++) {
if (inst->src[i].file == ARF &&
(inst->src[i].nr == BRW_ARF_ADDRESS ||
inst->src[i].nr == BRW_ARF_ACCUMULATOR ||
inst->src[i].nr == BRW_ARF_FLAG)) {
bad_source = true;
break;
}
}
if (bad_source)
continue;
/* If the instruction does not fully define the destination, adding
* load_reg instructions will not help.
*/
if (!fully_defines(s, inst))
continue;
if (inst->exec_size < 8)
continue;
assert(inst->exec_size == 8 || inst->exec_size == 16 ||
inst->exec_size == 32);
const unsigned mask = (inst->exec_size / 8) - 1;
/* Replace any non-SSA sources with load_reg of the source. */
const brw_builder bld = brw_builder(inst);
for (int i = 0; i < inst->sources; i++) {
/* LOAD_REG only operates on VGRF sources. If the source is not VGRF,
* skip it.
*/
if (inst->src[i].file != VGRF)
continue;
/* The source is already a def, so don't add a LOAD_REG. */
if (defs.get(inst->src[i]) != NULL)
continue;
/* Cases of stride != 1 are difficult to handle correctly. For
* example, when stride is 0, the source may have been written by
* NoMask instruction that cannot be seen from here. In this case,
* emitting a non-NoMask LOAD_REG may not actually copy the value
* that the instruction is trying to read.
*
* This means that is_scalar sources in larger exec sizes are not
* handled. Since enough information is available in the source, this
* could be added later.
*/
if (inst->src[i].stride != 1)
continue;
/* If the size of the VGRF allocation is not an even multiple of
* the SIMD size, don't emit a load_reg. This can occur for sparse
* texture loads. These will have SIMD-size values for the texture
* data and a single SIMD1 register for the residency information.
*/
if ((s.alloc.sizes[inst->src[i].nr] & mask) != 0)
continue;
brw_reg_type t =
brw_type_with_size(BRW_TYPE_UD,
brw_type_size_bits(inst->src[i].type));
brw_reg old_src = brw_vgrf(inst->src[i].nr, t);
brw_reg new_src;
/* Since the sources of a LOAD_REG will likely not be defs,
* none of the existing optimizations passes will eliminate
* redundant LOAD_REG instructions. Search back though this
* block to find a LOAD_REG of the same value to avoid emitting
* too many redundant instructions.
*/
foreach_inst_in_block_reverse_starting_from(brw_inst, scan_inst, inst) {
if (scan_inst->dst.file == old_src.file &&
scan_inst->dst.nr == old_src.nr) {
break;
}
if (scan_inst->opcode == SHADER_OPCODE_LOAD_REG &&
scan_inst->exec_size == inst->exec_size &&
scan_inst->force_writemask_all == inst->force_writemask_all &&
old_src.equals(scan_inst->src[0])) {
new_src = scan_inst->dst;
break;
}
}
if (new_src.file == BAD_FILE)
new_src = bld.LOAD_REG(old_src);
inst->src[i].nr = new_src.nr;
progress = true;
}
}
if (progress)
s.invalidate_analysis(BRW_DEPENDENCY_INSTRUCTIONS |
BRW_DEPENDENCY_VARIABLES);
return progress;
}
bool
brw_lower_load_reg(brw_shader &s)
{
bool progress = false;
foreach_block_and_inst_safe(block, brw_inst, inst, s.cfg) {
if (inst->opcode == SHADER_OPCODE_LOAD_REG) {
const brw_builder ibld = brw_builder(inst);
const unsigned bytes = inst->size_written;
const unsigned type_bytes = brw_type_size_bytes(inst->dst.type);
const unsigned bytes_per_mov = inst->exec_size * type_bytes;
for (unsigned i = 0; i < bytes; i += bytes_per_mov) {
ibld.MOV(byte_offset(inst->dst, i),
byte_offset(inst->src[0], i));
}
inst->remove();
progress = true;
}
}
if (progress)
s.invalidate_analysis(BRW_DEPENDENCY_INSTRUCTIONS |
BRW_DEPENDENCY_VARIABLES);
return progress;
}