mesa/src/intel/compiler/brw_fs_copy_propagation.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1883 lines
64 KiB
C++
Raw Normal View History

/*
* Copyright © 2012 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/** @file
*
* Support for global copy propagation in two passes: A local pass that does
* intra-block copy (and constant) propagation, and a global pass that uses
* dataflow analysis on the copies available at the end of each block to re-do
* local copy propagation with more copies available.
*
* See Muchnick's Advanced Compiler Design and Implementation, section
* 12.5 (p356).
*/
#include "util/bitset.h"
#include "util/u_math.h"
#include "util/rb_tree.h"
#include "brw_fs.h"
#include "brw_fs_live_variables.h"
#include "brw_cfg.h"
#include "brw_eu.h"
using namespace brw;
namespace { /* avoid conflict with opt_copy_propagation_elements */
struct acp_entry {
struct rb_node by_dst;
struct rb_node by_src;
brw_reg dst;
brw_reg src;
unsigned global_idx;
intel/fs: Add support for copy-propagating a block of multiple FIXED_GRFs. In cases where a LOAD_PAYLOAD instruction copies a single block of sequential GRF registers into the destination (see is_identity_payload()), splitting the block copy into a number of ACP entries (one for each LOAD_PAYLOAD source) is undesirable, because that prevents copy propagation into any instructions which read multiple components at once with the same source (the barycentric source of the LINTERP instruction is going to be the overwhelmingly most common example). Technically it would also be possible to do this for VGRF sources, but there is little benefit from that since register coalesce already covers many of those cases -- There is no way for a block of FIXED_GRFs to be coalesced into a VGRF though. This prevents the following shader-db regressions (including SIMD32 programs) in combination with the interpolation rework part of this series. On SKL: total instructions in shared programs: 18595160 -> 18828562 (1.26%) instructions in affected programs: 13374946 -> 13608348 (1.75%) helped: 7 HURT: 108977 total spills in shared programs: 9116 -> 9106 (-0.11%) spills in affected programs: 404 -> 394 (-2.48%) helped: 7 HURT: 9 total fills in shared programs: 8994 -> 9176 (2.02%) fills in affected programs: 898 -> 1080 (20.27%) helped: 7 HURT: 9 LOST: 469 GAINED: 220 On SNB: total instructions in shared programs: 13996898 -> 14096222 (0.71%) instructions in affected programs: 8088546 -> 8187870 (1.23%) helped: 2 HURT: 66520 total spills in shared programs: 2985 -> 2961 (-0.80%) spills in affected programs: 632 -> 608 (-3.80%) helped: 2 HURT: 0 total fills in shared programs: 3144 -> 3128 (-0.51%) fills in affected programs: 1515 -> 1499 (-1.06%) helped: 2 HURT: 0 LOST: 0 GAINED: 4 Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
2019-12-30 00:36:48 -08:00
unsigned size_written;
unsigned size_read;
enum opcode opcode;
intel/fs: Allow copy propagation between MOVs of mixed sizes This eliminates some spurious, size-converting moves. For example, on Ice Lake this helps dEQP-VK.spirv_assembly.type.vec3.i8.bitwise_xor_frag: SIMD8 shader: 52 instructions. 1 loops. 4164 cycles. 0:0 spills:fills, 5 sends SIMD8 shader: 49 instructions. 1 loops. 4044 cycles. 0:0 spills:fills, 5 sends Unfortunately, this doesn't clean everything up. Here's a subset of the "before" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; mov(8) g12<1>UB g7<32,8,4>UB { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g12<8,8,1>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g14<1>UB g8<32,8,4>UB { align1 1Q }; mov(8) g16<1>UW g14<8,8,1>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; And here's the same subset of the "after" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g7<32,8,4>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g16<1>UW g8<32,8,4>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; There are a lot of regioning and type restrictions in fs_visitor::try_copy_propagate, and I'm a little nervious about messing with them too much. Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Suggested-by: Francisco Jerez <currojerez@riseup.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9025>
2021-04-13 14:07:19 -07:00
bool is_partial_write;
bool force_writemask_all;
};
/**
* Compare two acp_entry::src.nr
*
* This is intended to be used as the comparison function for rb_tree.
*/
static int
cmp_entry_dst_entry_dst(const struct rb_node *a_node, const struct rb_node *b_node)
{
const struct acp_entry *a_entry =
rb_node_data(struct acp_entry, a_node, by_dst);
const struct acp_entry *b_entry =
rb_node_data(struct acp_entry, b_node, by_dst);
return a_entry->dst.nr - b_entry->dst.nr;
}
static int
cmp_entry_dst_nr(const struct rb_node *a_node, const void *b_key)
{
const struct acp_entry *a_entry =
rb_node_data(struct acp_entry, a_node, by_dst);
return a_entry->dst.nr - (uintptr_t) b_key;
}
static int
cmp_entry_src_entry_src(const struct rb_node *a_node, const struct rb_node *b_node)
{
const struct acp_entry *a_entry =
rb_node_data(struct acp_entry, a_node, by_src);
const struct acp_entry *b_entry =
rb_node_data(struct acp_entry, b_node, by_src);
return a_entry->src.nr - b_entry->src.nr;
}
/**
* Compare an acp_entry::src.nr with a raw nr.
*
* This is intended to be used as the comparison function for rb_tree.
*/
static int
cmp_entry_src_nr(const struct rb_node *a_node, const void *b_key)
{
const struct acp_entry *a_entry =
rb_node_data(struct acp_entry, a_node, by_src);
return a_entry->src.nr - (uintptr_t) b_key;
}
class acp_forward_iterator {
public:
acp_forward_iterator(struct rb_node *n, unsigned offset)
: curr(n), next(nullptr), offset(offset)
{
next = rb_node_next_or_null(curr);
}
acp_forward_iterator &operator++()
{
curr = next;
next = rb_node_next_or_null(curr);
return *this;
}
bool operator!=(const acp_forward_iterator &other) const
{
return curr != other.curr;
}
struct acp_entry *operator*() const
{
/* This open-codes part of rb_node_data. */
return curr != NULL ? (struct acp_entry *)(((char *)curr) - offset)
: NULL;
}
private:
struct rb_node *curr;
struct rb_node *next;
unsigned offset;
};
struct acp {
struct rb_tree by_dst;
struct rb_tree by_src;
acp()
{
rb_tree_init(&by_dst);
rb_tree_init(&by_src);
}
acp_forward_iterator begin()
{
return acp_forward_iterator(rb_tree_first(&by_src),
rb_tree_offsetof(struct acp_entry, by_src, 0));
}
const acp_forward_iterator end() const
{
return acp_forward_iterator(nullptr, 0);
}
unsigned length()
{
unsigned l = 0;
for (rb_node *iter = rb_tree_first(&by_src);
iter != NULL; iter = rb_node_next(iter))
l++;
return l;
}
void add(acp_entry *entry)
{
rb_tree_insert(&by_dst, &entry->by_dst, cmp_entry_dst_entry_dst);
rb_tree_insert(&by_src, &entry->by_src, cmp_entry_src_entry_src);
}
void remove(acp_entry *entry)
{
rb_tree_remove(&by_dst, &entry->by_dst);
rb_tree_remove(&by_src, &entry->by_src);
}
acp_forward_iterator find_by_src(unsigned nr)
{
struct rb_node *rbn = rb_tree_search(&by_src,
(void *)(uintptr_t) nr,
cmp_entry_src_nr);
return acp_forward_iterator(rbn, rb_tree_offsetof(struct acp_entry,
by_src, rbn));
}
acp_forward_iterator find_by_dst(unsigned nr)
{
struct rb_node *rbn = rb_tree_search(&by_dst,
(void *)(uintptr_t) nr,
cmp_entry_dst_nr);
return acp_forward_iterator(rbn, rb_tree_offsetof(struct acp_entry,
by_dst, rbn));
}
};
struct block_data {
/**
* Which entries in the fs_copy_prop_dataflow acp table are live at the
* start of this block. This is the useful output of the analysis, since
* it lets us plug those into the local copy propagation on the second
* pass.
*/
BITSET_WORD *livein;
/**
* Which entries in the fs_copy_prop_dataflow acp table are live at the end
* of this block. This is done in initial setup from the per-block acps
* returned by the first local copy prop pass.
*/
BITSET_WORD *liveout;
/**
* Which entries in the fs_copy_prop_dataflow acp table are generated by
* instructions in this block which reach the end of the block without
* being killed.
*/
BITSET_WORD *copy;
/**
* Which entries in the fs_copy_prop_dataflow acp table are killed over the
* course of this block.
*/
BITSET_WORD *kill;
/**
* Which entries in the fs_copy_prop_dataflow acp table are guaranteed to
* have a fully uninitialized destination at the end of this block.
*/
BITSET_WORD *undef;
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
/**
* Which entries in the fs_copy_prop_dataflow acp table can the
* start of this block be reached from. Note that this is a weaker
* condition than livein.
*/
BITSET_WORD *reachin;
/**
* Which entries in the fs_copy_prop_dataflow acp table are
* overwritten by an instruction with channel masks inconsistent
* with the copy instruction (e.g. due to force_writemask_all).
* Such an overwrite can cause the copy entry to become invalid
* even if the copy instruction is subsequently re-executed for any
* given channel i, since the execution of the overwrite for
* channel i may corrupt other channels j!=i inactive for the
* subsequent copy.
*/
BITSET_WORD *exec_mismatch;
};
class fs_copy_prop_dataflow
{
public:
fs_copy_prop_dataflow(linear_ctx *lin_ctx, cfg_t *cfg,
const fs_live_variables &live,
struct acp *out_acp);
void setup_initial_values();
void run();
void dump_block_data() const UNUSED;
cfg_t *cfg;
const fs_live_variables &live;
acp_entry **acp;
int num_acp;
int bitset_words;
struct block_data *bd;
};
} /* anonymous namespace */
fs_copy_prop_dataflow::fs_copy_prop_dataflow(linear_ctx *lin_ctx, cfg_t *cfg,
const fs_live_variables &live,
struct acp *out_acp)
: cfg(cfg), live(live)
{
bd = linear_zalloc_array(lin_ctx, struct block_data, cfg->num_blocks);
num_acp = 0;
foreach_block (block, cfg)
num_acp += out_acp[block->num].length();
bitset_words = BITSET_WORDS(num_acp);
foreach_block (block, cfg) {
bd[block->num].livein = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
bd[block->num].liveout = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
bd[block->num].copy = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
bd[block->num].kill = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
bd[block->num].undef = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
bd[block->num].reachin = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
bd[block->num].exec_mismatch = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
}
acp = linear_zalloc_array(lin_ctx, struct acp_entry *, num_acp);
int next_acp = 0;
foreach_block (block, cfg) {
for (auto iter = out_acp[block->num].begin();
iter != out_acp[block->num].end(); ++iter) {
acp[next_acp] = *iter;
(*iter)->global_idx = next_acp;
/* opt_copy_propagation_local populates out_acp with copies created
* in a block which are still live at the end of the block. This
* is exactly what we want in the COPY set.
*/
BITSET_SET(bd[block->num].copy, next_acp);
next_acp++;
}
}
assert(next_acp == num_acp);
setup_initial_values();
run();
}
/**
* Like reg_offset, but register must be VGRF or FIXED_GRF.
*/
static inline unsigned
grf_reg_offset(const brw_reg &r)
{
return (r.file == VGRF ? 0 : r.nr) * REG_SIZE +
r.offset +
(r.file == FIXED_GRF ? r.subnr : 0);
}
/**
* Like regions_overlap, but register must be VGRF or FIXED_GRF.
*/
static inline bool
grf_regions_overlap(const brw_reg &r, unsigned dr, const brw_reg &s, unsigned ds)
{
return reg_space(r) == reg_space(s) &&
!(grf_reg_offset(r) + dr <= grf_reg_offset(s) ||
grf_reg_offset(s) + ds <= grf_reg_offset(r));
}
/**
* Set up initial values for each of the data flow sets, prior to running
* the fixed-point algorithm.
*/
void
fs_copy_prop_dataflow::setup_initial_values()
{
/* Initialize the COPY and KILL sets. */
{
struct acp acp_table;
/* First, get all the KILLs for instructions which overwrite ACP
* destinations.
*/
for (int i = 0; i < num_acp; i++)
acp_table.add(acp[i]);
foreach_block (block, cfg) {
foreach_inst_in_block(fs_inst, inst, block) {
if (inst->dst.file != VGRF &&
inst->dst.file != FIXED_GRF)
continue;
for (auto iter = acp_table.find_by_src(inst->dst.nr);
iter != acp_table.end() && (*iter)->src.nr == inst->dst.nr;
++iter) {
if (grf_regions_overlap(inst->dst, inst->size_written,
(*iter)->src, (*iter)->size_read)) {
BITSET_SET(bd[block->num].kill, (*iter)->global_idx);
if (inst->force_writemask_all && !(*iter)->force_writemask_all)
BITSET_SET(bd[block->num].exec_mismatch, (*iter)->global_idx);
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
}
}
if (inst->dst.file != VGRF)
continue;
for (auto iter = acp_table.find_by_dst(inst->dst.nr);
iter != acp_table.end() && (*iter)->dst.nr == inst->dst.nr;
++iter) {
if (grf_regions_overlap(inst->dst, inst->size_written,
(*iter)->dst, (*iter)->size_written)) {
BITSET_SET(bd[block->num].kill, (*iter)->global_idx);
if (inst->force_writemask_all && !(*iter)->force_writemask_all)
BITSET_SET(bd[block->num].exec_mismatch, (*iter)->global_idx);
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
}
}
}
}
}
/* Populate the initial values for the livein and liveout sets. For the
* block at the start of the program, livein = 0 and liveout = copy.
intel/fs: Optimize and simplify the copy propagation dataflow logic. Previously the dataflow propagation algorithm would calculate the ACP live-in and -out sets in a two-pass fixed-point algorithm. The first pass would update the live-out sets of all basic blocks of the program based on their live-in sets, while the second pass would update the live-in sets based on the live-out sets. This is incredibly inefficient in the typical case where the CFG of the program is approximately acyclic, because it can take up to 2*n passes for an ACP entry introduced at the top of the program to reach the bottom (where n is the number of basic blocks in the program), until which point the algorithm won't be able to reach a fixed point. The same effect can be achieved in a single pass by computing the live-in and -out sets in lock-step, because that makes sure that processing of any basic block will pick up the updated live-out sets of the lexically preceding blocks. This gives the dataflow propagation algorithm effectively O(n) run-time instead of O(n^2) in the acyclic case. The time spent in dataflow propagation is reduced by 30x in the GLES31.functional.ssbo.layout.random.all_shared_buffer.5 dEQP test-case on my CHV system (the improvement is likely to be of the same order of magnitude on other platforms). This more than reverses an apparent run-time regression in this test-case from my previous copy-propagation undefined-value handling patch, which was ultimately caused by the additional work introduced in that commit to account for undefined values being multiplied by a huge quadratic factor. According to Chad this test was failing on CHV due to a 30s time-out imposed by the Android CTS (this was the case regardless of my undefined-value handling patch, even though my patch substantially exacerbated the issue). On my CHV system this patch reduces the overall run-time of the test by approximately 12x, getting us to around 13s, well below the time-out. v2: Initialize live-out set to the universal set to avoid rather pessimistic dataflow estimation in shaders with cycles (Addresses performance regression reported by Eero in GpuTest Piano). Performance numbers given above still apply. No shader-db changes with respect to master. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=104271 Reported-by: Chad Versace <chadversary@chromium.org> Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2017-12-18 15:22:04 -08:00
* For the others, set liveout and livein to ~0 (the universal set).
*/
foreach_block (block, cfg) {
if (block->parents.is_empty()) {
for (int i = 0; i < bitset_words; i++) {
bd[block->num].livein[i] = 0u;
bd[block->num].liveout[i] = bd[block->num].copy[i];
}
} else {
for (int i = 0; i < bitset_words; i++) {
intel/fs: Optimize and simplify the copy propagation dataflow logic. Previously the dataflow propagation algorithm would calculate the ACP live-in and -out sets in a two-pass fixed-point algorithm. The first pass would update the live-out sets of all basic blocks of the program based on their live-in sets, while the second pass would update the live-in sets based on the live-out sets. This is incredibly inefficient in the typical case where the CFG of the program is approximately acyclic, because it can take up to 2*n passes for an ACP entry introduced at the top of the program to reach the bottom (where n is the number of basic blocks in the program), until which point the algorithm won't be able to reach a fixed point. The same effect can be achieved in a single pass by computing the live-in and -out sets in lock-step, because that makes sure that processing of any basic block will pick up the updated live-out sets of the lexically preceding blocks. This gives the dataflow propagation algorithm effectively O(n) run-time instead of O(n^2) in the acyclic case. The time spent in dataflow propagation is reduced by 30x in the GLES31.functional.ssbo.layout.random.all_shared_buffer.5 dEQP test-case on my CHV system (the improvement is likely to be of the same order of magnitude on other platforms). This more than reverses an apparent run-time regression in this test-case from my previous copy-propagation undefined-value handling patch, which was ultimately caused by the additional work introduced in that commit to account for undefined values being multiplied by a huge quadratic factor. According to Chad this test was failing on CHV due to a 30s time-out imposed by the Android CTS (this was the case regardless of my undefined-value handling patch, even though my patch substantially exacerbated the issue). On my CHV system this patch reduces the overall run-time of the test by approximately 12x, getting us to around 13s, well below the time-out. v2: Initialize live-out set to the universal set to avoid rather pessimistic dataflow estimation in shaders with cycles (Addresses performance regression reported by Eero in GpuTest Piano). Performance numbers given above still apply. No shader-db changes with respect to master. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=104271 Reported-by: Chad Versace <chadversary@chromium.org> Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2017-12-18 15:22:04 -08:00
bd[block->num].liveout[i] = ~0u;
bd[block->num].livein[i] = ~0u;
}
}
}
/* Initialize the undef set. */
foreach_block (block, cfg) {
for (int i = 0; i < num_acp; i++) {
BITSET_SET(bd[block->num].undef, i);
for (unsigned off = 0; off < acp[i]->size_written; off += REG_SIZE) {
if (BITSET_TEST(live.block_data[block->num].defout,
live.var_from_reg(byte_offset(acp[i]->dst, off))))
BITSET_CLEAR(bd[block->num].undef, i);
}
}
}
}
/**
* Walk the set of instructions in the block, marking which entries in the acp
* are killed by the block.
*/
void
fs_copy_prop_dataflow::run()
{
bool progress;
do {
progress = false;
foreach_block (block, cfg) {
if (block->parents.is_empty())
continue;
for (int i = 0; i < bitset_words; i++) {
const BITSET_WORD old_liveout = bd[block->num].liveout[i];
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
const BITSET_WORD old_reachin = bd[block->num].reachin[i];
BITSET_WORD livein_from_any_block = 0;
intel/fs: Optimize and simplify the copy propagation dataflow logic. Previously the dataflow propagation algorithm would calculate the ACP live-in and -out sets in a two-pass fixed-point algorithm. The first pass would update the live-out sets of all basic blocks of the program based on their live-in sets, while the second pass would update the live-in sets based on the live-out sets. This is incredibly inefficient in the typical case where the CFG of the program is approximately acyclic, because it can take up to 2*n passes for an ACP entry introduced at the top of the program to reach the bottom (where n is the number of basic blocks in the program), until which point the algorithm won't be able to reach a fixed point. The same effect can be achieved in a single pass by computing the live-in and -out sets in lock-step, because that makes sure that processing of any basic block will pick up the updated live-out sets of the lexically preceding blocks. This gives the dataflow propagation algorithm effectively O(n) run-time instead of O(n^2) in the acyclic case. The time spent in dataflow propagation is reduced by 30x in the GLES31.functional.ssbo.layout.random.all_shared_buffer.5 dEQP test-case on my CHV system (the improvement is likely to be of the same order of magnitude on other platforms). This more than reverses an apparent run-time regression in this test-case from my previous copy-propagation undefined-value handling patch, which was ultimately caused by the additional work introduced in that commit to account for undefined values being multiplied by a huge quadratic factor. According to Chad this test was failing on CHV due to a 30s time-out imposed by the Android CTS (this was the case regardless of my undefined-value handling patch, even though my patch substantially exacerbated the issue). On my CHV system this patch reduces the overall run-time of the test by approximately 12x, getting us to around 13s, well below the time-out. v2: Initialize live-out set to the universal set to avoid rather pessimistic dataflow estimation in shaders with cycles (Addresses performance regression reported by Eero in GpuTest Piano). Performance numbers given above still apply. No shader-db changes with respect to master. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=104271 Reported-by: Chad Versace <chadversary@chromium.org> Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2017-12-18 15:22:04 -08:00
/* Update livein for this block. If a copy is live out of all
* parent blocks, it's live coming in to this block.
*/
bd[block->num].livein[i] = ~0u;
foreach_list_typed(bblock_link, parent_link, link, &block->parents) {
bblock_t *parent = parent_link->block;
/* Consider ACP entries with a known-undefined destination to
* be available from the parent. This is valid because we're
* free to set the undefined variable equal to the source of
* the ACP entry without breaking the application's
* expectations, since the variable is undefined.
*/
bd[block->num].livein[i] &= (bd[parent->num].liveout[i] |
bd[parent->num].undef[i]);
livein_from_any_block |= bd[parent->num].liveout[i];
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
/* Update reachin for this block. If the end of any
* parent block is reachable from the copy, the start
* of this block is reachable from it as well.
*/
bd[block->num].reachin[i] |= (bd[parent->num].reachin[i] |
bd[parent->num].copy[i]);
}
/* Limit to the set of ACP entries that can possibly be available
* at the start of the block, since propagating from a variable
* which is guaranteed to be undefined (rather than potentially
* undefined for some dynamic control-flow paths) doesn't seem
* particularly useful.
*/
bd[block->num].livein[i] &= livein_from_any_block;
intel/fs: Optimize and simplify the copy propagation dataflow logic. Previously the dataflow propagation algorithm would calculate the ACP live-in and -out sets in a two-pass fixed-point algorithm. The first pass would update the live-out sets of all basic blocks of the program based on their live-in sets, while the second pass would update the live-in sets based on the live-out sets. This is incredibly inefficient in the typical case where the CFG of the program is approximately acyclic, because it can take up to 2*n passes for an ACP entry introduced at the top of the program to reach the bottom (where n is the number of basic blocks in the program), until which point the algorithm won't be able to reach a fixed point. The same effect can be achieved in a single pass by computing the live-in and -out sets in lock-step, because that makes sure that processing of any basic block will pick up the updated live-out sets of the lexically preceding blocks. This gives the dataflow propagation algorithm effectively O(n) run-time instead of O(n^2) in the acyclic case. The time spent in dataflow propagation is reduced by 30x in the GLES31.functional.ssbo.layout.random.all_shared_buffer.5 dEQP test-case on my CHV system (the improvement is likely to be of the same order of magnitude on other platforms). This more than reverses an apparent run-time regression in this test-case from my previous copy-propagation undefined-value handling patch, which was ultimately caused by the additional work introduced in that commit to account for undefined values being multiplied by a huge quadratic factor. According to Chad this test was failing on CHV due to a 30s time-out imposed by the Android CTS (this was the case regardless of my undefined-value handling patch, even though my patch substantially exacerbated the issue). On my CHV system this patch reduces the overall run-time of the test by approximately 12x, getting us to around 13s, well below the time-out. v2: Initialize live-out set to the universal set to avoid rather pessimistic dataflow estimation in shaders with cycles (Addresses performance regression reported by Eero in GpuTest Piano). Performance numbers given above still apply. No shader-db changes with respect to master. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=104271 Reported-by: Chad Versace <chadversary@chromium.org> Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2017-12-18 15:22:04 -08:00
/* Update liveout for this block. */
bd[block->num].liveout[i] =
bd[block->num].copy[i] | (bd[block->num].livein[i] &
~bd[block->num].kill[i]);
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
if (old_liveout != bd[block->num].liveout[i] ||
old_reachin != bd[block->num].reachin[i])
progress = true;
}
}
} while (progress);
/* Perform a second fixed-point pass in order to propagate the
* exec_mismatch bitsets. Note that this requires an accurate
* value of the reachin bitsets as input, which isn't available
* until the end of the first propagation pass, so this loop cannot
* be folded into the previous one.
*/
do {
progress = false;
foreach_block (block, cfg) {
for (int i = 0; i < bitset_words; i++) {
const BITSET_WORD old_exec_mismatch = bd[block->num].exec_mismatch[i];
/* Update exec_mismatch for this block. If the end of a
* parent block is reachable by an overwrite with
* inconsistent execution masking, the start of this block
* is reachable by such an overwrite as well.
*/
foreach_list_typed(bblock_link, parent_link, link, &block->parents) {
bblock_t *parent = parent_link->block;
bd[block->num].exec_mismatch[i] |= (bd[parent->num].exec_mismatch[i] &
bd[parent->num].reachin[i]);
}
/* Only consider overwrites with inconsistent execution
* masking if they are reachable from the copy, since
* overwrites unreachable from a copy are harmless to that
* copy.
*/
bd[block->num].exec_mismatch[i] &= bd[block->num].reachin[i];
if (old_exec_mismatch != bd[block->num].exec_mismatch[i])
progress = true;
}
}
} while (progress);
}
void
fs_copy_prop_dataflow::dump_block_data() const
{
foreach_block (block, cfg) {
fprintf(stderr, "Block %d [%d, %d] (parents ", block->num,
block->start_ip, block->end_ip);
foreach_list_typed(bblock_link, link, link, &block->parents) {
bblock_t *parent = link->block;
fprintf(stderr, "%d ", parent->num);
}
fprintf(stderr, "):\n");
fprintf(stderr, " livein = 0x");
for (int i = 0; i < bitset_words; i++)
fprintf(stderr, "%08x", bd[block->num].livein[i]);
fprintf(stderr, ", liveout = 0x");
for (int i = 0; i < bitset_words; i++)
fprintf(stderr, "%08x", bd[block->num].liveout[i]);
fprintf(stderr, ",\n copy = 0x");
for (int i = 0; i < bitset_words; i++)
fprintf(stderr, "%08x", bd[block->num].copy[i]);
fprintf(stderr, ", kill = 0x");
for (int i = 0; i < bitset_words; i++)
fprintf(stderr, "%08x", bd[block->num].kill[i]);
fprintf(stderr, "\n");
}
}
static bool
is_logic_op(enum opcode opcode)
{
return (opcode == BRW_OPCODE_AND ||
opcode == BRW_OPCODE_OR ||
opcode == BRW_OPCODE_XOR ||
opcode == BRW_OPCODE_NOT);
}
static bool
can_take_stride(fs_inst *inst, brw_reg_type dst_type,
unsigned arg, unsigned stride,
const struct brw_compiler *compiler)
{
const struct intel_device_info *devinfo = compiler->devinfo;
if (stride > 4)
return false;
/* Bail if the channels of the source need to be aligned to the byte offset
* of the corresponding channel of the destination, and the provided stride
* would break this restriction.
*/
if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&
!(brw_type_size_bytes(inst->src[arg].type) * stride ==
brw_type_size_bytes(dst_type) * inst->dst.stride ||
stride == 0))
return false;
/* 3-source instructions can only be Align16, which restricts what strides
* they can take. They can only take a stride of 1 (the usual case), or 0
* with a special "repctrl" bit. But the repctrl bit doesn't work for
* 64-bit datatypes, so if the source type is 64-bit then only a stride of
* 1 is allowed. From the Broadwell PRM, Volume 7 "3D Media GPGPU", page
* 944:
*
* This is applicable to 32b datatypes and 16b datatype. 64b datatypes
* cannot use the replicate control.
*/
if (inst->is_3src(compiler)) {
if (brw_type_size_bytes(inst->src[arg].type) > 4)
return stride == 1;
else
return stride == 1 || stride == 0;
}
if (inst->is_math()) {
/* Wa_22016140776:
*
* Scalar broadcast on HF math (packed or unpacked) must not be used.
* Compiler must use a mov instruction to expand the scalar value to
* a vector before using in a HF (packed or unpacked) math operation.
*
* Prevent copy propagating a scalar value into a math instruction.
*/
if (intel_needs_workaround(devinfo, 22016140776) &&
stride == 0 && inst->src[arg].type == BRW_TYPE_HF) {
return false;
}
/* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",
* page 391 ("Extended Math Function"):
*
* The following restrictions apply for align1 mode: Scalar source
* is supported. Source and destination horizontal stride must be
* the same.
*/
return stride == inst->dst.stride || stride == 0;
}
return true;
}
static bool
instruction_requires_packed_data(fs_inst *inst)
{
switch (inst->opcode) {
case FS_OPCODE_DDX_FINE:
case FS_OPCODE_DDX_COARSE:
case FS_OPCODE_DDY_FINE:
case FS_OPCODE_DDY_COARSE:
case SHADER_OPCODE_QUAD_SWIZZLE:
return true;
default:
return false;
}
}
static bool
try_copy_propagate(const brw_compiler *compiler, fs_inst *inst,
acp_entry *entry, int arg,
const brw::simple_allocator &alloc,
uint8_t max_polygons)
{
if (inst->src[arg].file != VGRF)
return false;
const struct intel_device_info *devinfo = compiler->devinfo;
assert(entry->src.file == VGRF || entry->src.file == UNIFORM ||
entry->src.file == ATTR || entry->src.file == FIXED_GRF);
/* Avoid propagating a LOAD_PAYLOAD instruction into another if there is a
* good chance that we'll be able to eliminate the latter through register
* coalescing. If only part of the sources of the second LOAD_PAYLOAD can
* be simplified through copy propagation we would be making register
* coalescing impossible, ending up with unnecessary copies in the program.
* This is also the case for is_multi_copy_payload() copies that can only
* be coalesced when the instruction is lowered into a sequence of MOVs.
*
* Worse -- In cases where the ACP entry was the result of CSE combining
* multiple LOAD_PAYLOAD subexpressions, propagating the first LOAD_PAYLOAD
* into the second would undo the work of CSE, leading to an infinite
* optimization loop. Avoid this by detecting LOAD_PAYLOAD copies from CSE
* temporaries which should match is_coalescing_payload().
*/
if (entry->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
(is_coalescing_payload(alloc, inst) || is_multi_copy_payload(inst)))
return false;
assert(entry->dst.file == VGRF);
if (inst->src[arg].nr != entry->dst.nr)
i965/fs: Convert gen7 to using GRFs for texture messages. Looking at Lightsmark's shaders, the way we used MRFs (or in gen7's case, GRFs) was bad in a couple of ways. One was that it prevented compute-to-MRF for the common case of a texcoord that gets used exactly once, but where the texcoord setup all gets emitted before the texture calls (such as when it's a bare fragment shader input, which gets interpolated before processing main()). Another was that it introduced a bunch of dependencies that constrained scheduling, and forced waits for texture operations to be done before they are required. For example, we can now move the compute-to-MRF interpolation for the second texture send down after the first send. The downside is that this generally prevents remove_duplicate_mrf_writes() from doing anything, whereas previously it avoided work for the case of sampling from the same texcoord twice. However, I suspect that most of the win that originally justified that code was in avoiding the WAR stall on the first send, which this patch also avoids, rather than the small cost of the extra instruction. We see instruction count regressions in shaders in unigine, yofrankie, savage2, hon, and gstreamer. Improves GLB2.7 performance by 0.633628% +/- 0.491809% (n=121/125, avg of ~66fps, outliers below 61 dropped). Improves openarena performance by 1.01092% +/- 0.66897% (n=425). No significant difference on Lightsmark (n=44). v2: Squash in the fix for register unspilling for send-from-GRF, fixing a segfault in lightsmark. Reviewed-by: Kenneth Graunke <kenneth@whitecape.org> Acked-by: Matt Turner <mattst88@gmail.com>
2013-10-09 17:17:59 -07:00
return false;
/* Bail if inst is reading a range that isn't contained in the range
* that entry is writing.
*/
if (!region_contained_in(inst->src[arg], inst->size_read(arg),
entry->dst, entry->size_written))
return false;
intel/compiler: Avoid copy propagating large registers into EOT messages EOT messages need to use g112-g127 for their sources. With the new opt_split_sends pass, we may be constructing an EOT message from two different registers, and be able to copy propagate the original values into those SENDs. This can cause problems if we copy propagate from a large register (say an RGBA value which is 4 GRFs in SIMD8 or 8 GRFs in SIMD16), in a situation where the SEND only read a subset of that (say the alpha value out of an RGBA texturing result). g112-127 can only hold 16 registers worth of data, and sometimes we can only use g112-126. So, we can't propagate if the GRFs in question are larger than 15 GRFs. Fixes a shader validation failure in Alan Wake. Thanks to Ian Romanick for catching this! shader-db on Icelake shows that only SIMD32 programs are affected, and the results are pretty negligable: total instructions in shared programs: 19615228 -> 19615269 (<.01%) instructions in affected programs: 10702 -> 10743 (0.38%) helped: 1 / HURT: 43 / largest change: +/- 2 instructions total cycles in shared programs: 852001706 -> 852001566 (<.01%) cycles in affected programs: 767098 -> 766958 (-0.02%) helped: 68 / HURT: 64 / largest change: +/- 774 cycles GAINED: 2 / LOST: 0 Fixes: 589b03d02f0 ("intel/fs: Opportunistically split SEND message payloads") Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6803 Reviewed-by: Ian Romanick <ian.d.romanick@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17390>
2022-07-06 20:29:02 -07:00
/* Send messages with EOT set are restricted to use g112-g127 (and we
* sometimes need g127 for other purposes), so avoid copy propagating
* anything that would make it impossible to satisfy that restriction.
*/
intel/compiler: Avoid copy propagating large registers into EOT messages EOT messages need to use g112-g127 for their sources. With the new opt_split_sends pass, we may be constructing an EOT message from two different registers, and be able to copy propagate the original values into those SENDs. This can cause problems if we copy propagate from a large register (say an RGBA value which is 4 GRFs in SIMD8 or 8 GRFs in SIMD16), in a situation where the SEND only read a subset of that (say the alpha value out of an RGBA texturing result). g112-127 can only hold 16 registers worth of data, and sometimes we can only use g112-126. So, we can't propagate if the GRFs in question are larger than 15 GRFs. Fixes a shader validation failure in Alan Wake. Thanks to Ian Romanick for catching this! shader-db on Icelake shows that only SIMD32 programs are affected, and the results are pretty negligable: total instructions in shared programs: 19615228 -> 19615269 (<.01%) instructions in affected programs: 10702 -> 10743 (0.38%) helped: 1 / HURT: 43 / largest change: +/- 2 instructions total cycles in shared programs: 852001706 -> 852001566 (<.01%) cycles in affected programs: 767098 -> 766958 (-0.02%) helped: 68 / HURT: 64 / largest change: +/- 774 cycles GAINED: 2 / LOST: 0 Fixes: 589b03d02f0 ("intel/fs: Opportunistically split SEND message payloads") Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6803 Reviewed-by: Ian Romanick <ian.d.romanick@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17390>
2022-07-06 20:29:02 -07:00
if (inst->eot) {
/* Don't propagate things that are already pinned. */
if (entry->src.file != VGRF)
intel/compiler: Avoid copy propagating large registers into EOT messages EOT messages need to use g112-g127 for their sources. With the new opt_split_sends pass, we may be constructing an EOT message from two different registers, and be able to copy propagate the original values into those SENDs. This can cause problems if we copy propagate from a large register (say an RGBA value which is 4 GRFs in SIMD8 or 8 GRFs in SIMD16), in a situation where the SEND only read a subset of that (say the alpha value out of an RGBA texturing result). g112-127 can only hold 16 registers worth of data, and sometimes we can only use g112-126. So, we can't propagate if the GRFs in question are larger than 15 GRFs. Fixes a shader validation failure in Alan Wake. Thanks to Ian Romanick for catching this! shader-db on Icelake shows that only SIMD32 programs are affected, and the results are pretty negligable: total instructions in shared programs: 19615228 -> 19615269 (<.01%) instructions in affected programs: 10702 -> 10743 (0.38%) helped: 1 / HURT: 43 / largest change: +/- 2 instructions total cycles in shared programs: 852001706 -> 852001566 (<.01%) cycles in affected programs: 767098 -> 766958 (-0.02%) helped: 68 / HURT: 64 / largest change: +/- 774 cycles GAINED: 2 / LOST: 0 Fixes: 589b03d02f0 ("intel/fs: Opportunistically split SEND message payloads") Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6803 Reviewed-by: Ian Romanick <ian.d.romanick@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17390>
2022-07-06 20:29:02 -07:00
return false;
/* We might be propagating from a large register, while the SEND only
* is reading a portion of it (say the .A channel in an RGBA value).
* We need to pin both split SEND sources in g112-g126/127, so only
* allow this if the registers aren't too large.
*/
if (inst->opcode == SHADER_OPCODE_SEND && inst->sources >= 4 &&
entry->src.file == VGRF) {
intel/compiler: Avoid copy propagating large registers into EOT messages EOT messages need to use g112-g127 for their sources. With the new opt_split_sends pass, we may be constructing an EOT message from two different registers, and be able to copy propagate the original values into those SENDs. This can cause problems if we copy propagate from a large register (say an RGBA value which is 4 GRFs in SIMD8 or 8 GRFs in SIMD16), in a situation where the SEND only read a subset of that (say the alpha value out of an RGBA texturing result). g112-127 can only hold 16 registers worth of data, and sometimes we can only use g112-126. So, we can't propagate if the GRFs in question are larger than 15 GRFs. Fixes a shader validation failure in Alan Wake. Thanks to Ian Romanick for catching this! shader-db on Icelake shows that only SIMD32 programs are affected, and the results are pretty negligable: total instructions in shared programs: 19615228 -> 19615269 (<.01%) instructions in affected programs: 10702 -> 10743 (0.38%) helped: 1 / HURT: 43 / largest change: +/- 2 instructions total cycles in shared programs: 852001706 -> 852001566 (<.01%) cycles in affected programs: 767098 -> 766958 (-0.02%) helped: 68 / HURT: 64 / largest change: +/- 774 cycles GAINED: 2 / LOST: 0 Fixes: 589b03d02f0 ("intel/fs: Opportunistically split SEND message payloads") Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6803 Reviewed-by: Ian Romanick <ian.d.romanick@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17390>
2022-07-06 20:29:02 -07:00
int other_src = arg == 2 ? 3 : 2;
unsigned other_size = inst->src[other_src].file == VGRF ?
alloc.sizes[inst->src[other_src].nr] :
inst->size_read(other_src);
unsigned prop_src_size = alloc.sizes[entry->src.nr];
if (other_size + prop_src_size > 15)
return false;
}
}
/* we can't generally copy-propagate UD negations because we
* can end up accessing the resulting values as signed integers
* instead. See also resolve_ud_negate() and comment in
* fs_generator::generate_code.
*/
if (entry->src.type == BRW_TYPE_UD &&
entry->src.negate)
return false;
bool has_source_modifiers = entry->src.abs || entry->src.negate;
intel/compiler: Relax some conditions in try_copy_propagate Previously can_do_source_mods was used to determine whether a value with a source modifier or a value from a scalar source (e.g., a uniform) could be copy propagated. The former is a superset of the latter, so this always produces correct results, but it is overly restrictive. For example, a BFI instruction can't have source modifiers, but it can have scalar sources. This was originally authored to prevent a small number of shader-db regressions in a commit that marked SHR has not being able to have source modifiers. That commit has since been dropped in favor of a different method. v2: Refactor register region restriction detection to a helper function. Suggested by Jason. No fossil-db changes on any Intel platform. All Gen7+ platforms had similar results. (Ice Lake shown) total instructions in shared programs: 20039111 -> 20038943 (<.01%) instructions in affected programs: 31736 -> 31568 (-0.53%) helped: 104 HURT: 0 helped stats (abs) min: 1 max: 9 x̄: 1.62 x̃: 1 helped stats (rel) min: 0.30% max: 0.88% x̄: 0.45% x̃: 0.42% 95% mean confidence interval for instructions value: -2.03 -1.20 95% mean confidence interval for instructions %-change: -0.47% -0.42% Instructions are helped. total cycles in shared programs: 980309750 -> 980308897 (<.01%) cycles in affected programs: 591078 -> 590225 (-0.14%) helped: 70 HURT: 26 helped stats (abs) min: 2 max: 622 x̄: 23.94 x̃: 4 helped stats (rel) min: <.01% max: 2.85% x̄: 0.33% x̃: 0.12% HURT stats (abs) min: 2 max: 520 x̄: 31.65 x̃: 6 HURT stats (rel) min: 0.02% max: 2.45% x̄: 0.34% x̃: 0.15% 95% mean confidence interval for cycles value: -26.41 8.64 95% mean confidence interval for cycles %-change: -0.27% -0.03% Inconclusive result (value mean confidence interval includes 0). No shader-db changes on earlier Intel platforms. Reviewed-by: Anuj Phogat anuj.phogat@gmail.com [v1] Reviewed-by: Jason Ekstrand <jason@jlekstrand.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9237>
2020-12-06 16:26:04 -08:00
if (has_source_modifiers && !inst->can_do_source_mods(devinfo))
return false;
intel/compiler: Relax some conditions in try_copy_propagate Previously can_do_source_mods was used to determine whether a value with a source modifier or a value from a scalar source (e.g., a uniform) could be copy propagated. The former is a superset of the latter, so this always produces correct results, but it is overly restrictive. For example, a BFI instruction can't have source modifiers, but it can have scalar sources. This was originally authored to prevent a small number of shader-db regressions in a commit that marked SHR has not being able to have source modifiers. That commit has since been dropped in favor of a different method. v2: Refactor register region restriction detection to a helper function. Suggested by Jason. No fossil-db changes on any Intel platform. All Gen7+ platforms had similar results. (Ice Lake shown) total instructions in shared programs: 20039111 -> 20038943 (<.01%) instructions in affected programs: 31736 -> 31568 (-0.53%) helped: 104 HURT: 0 helped stats (abs) min: 1 max: 9 x̄: 1.62 x̃: 1 helped stats (rel) min: 0.30% max: 0.88% x̄: 0.45% x̃: 0.42% 95% mean confidence interval for instructions value: -2.03 -1.20 95% mean confidence interval for instructions %-change: -0.47% -0.42% Instructions are helped. total cycles in shared programs: 980309750 -> 980308897 (<.01%) cycles in affected programs: 591078 -> 590225 (-0.14%) helped: 70 HURT: 26 helped stats (abs) min: 2 max: 622 x̄: 23.94 x̃: 4 helped stats (rel) min: <.01% max: 2.85% x̄: 0.33% x̃: 0.12% HURT stats (abs) min: 2 max: 520 x̄: 31.65 x̃: 6 HURT stats (rel) min: 0.02% max: 2.45% x̄: 0.34% x̃: 0.15% 95% mean confidence interval for cycles value: -26.41 8.64 95% mean confidence interval for cycles %-change: -0.27% -0.03% Inconclusive result (value mean confidence interval includes 0). No shader-db changes on earlier Intel platforms. Reviewed-by: Anuj Phogat anuj.phogat@gmail.com [v1] Reviewed-by: Jason Ekstrand <jason@jlekstrand.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9237>
2020-12-06 16:26:04 -08:00
/* Reject cases that would violate register regioning restrictions. */
if ((entry->src.file == UNIFORM || !entry->src.is_contiguous()) &&
(inst->is_send_from_grf() ||
intel/compiler: Relax some conditions in try_copy_propagate Previously can_do_source_mods was used to determine whether a value with a source modifier or a value from a scalar source (e.g., a uniform) could be copy propagated. The former is a superset of the latter, so this always produces correct results, but it is overly restrictive. For example, a BFI instruction can't have source modifiers, but it can have scalar sources. This was originally authored to prevent a small number of shader-db regressions in a commit that marked SHR has not being able to have source modifiers. That commit has since been dropped in favor of a different method. v2: Refactor register region restriction detection to a helper function. Suggested by Jason. No fossil-db changes on any Intel platform. All Gen7+ platforms had similar results. (Ice Lake shown) total instructions in shared programs: 20039111 -> 20038943 (<.01%) instructions in affected programs: 31736 -> 31568 (-0.53%) helped: 104 HURT: 0 helped stats (abs) min: 1 max: 9 x̄: 1.62 x̃: 1 helped stats (rel) min: 0.30% max: 0.88% x̄: 0.45% x̃: 0.42% 95% mean confidence interval for instructions value: -2.03 -1.20 95% mean confidence interval for instructions %-change: -0.47% -0.42% Instructions are helped. total cycles in shared programs: 980309750 -> 980308897 (<.01%) cycles in affected programs: 591078 -> 590225 (-0.14%) helped: 70 HURT: 26 helped stats (abs) min: 2 max: 622 x̄: 23.94 x̃: 4 helped stats (rel) min: <.01% max: 2.85% x̄: 0.33% x̃: 0.12% HURT stats (abs) min: 2 max: 520 x̄: 31.65 x̃: 6 HURT stats (rel) min: 0.02% max: 2.45% x̄: 0.34% x̃: 0.15% 95% mean confidence interval for cycles value: -26.41 8.64 95% mean confidence interval for cycles %-change: -0.27% -0.03% Inconclusive result (value mean confidence interval includes 0). No shader-db changes on earlier Intel platforms. Reviewed-by: Anuj Phogat anuj.phogat@gmail.com [v1] Reviewed-by: Jason Ekstrand <jason@jlekstrand.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9237>
2020-12-06 16:26:04 -08:00
inst->uses_indirect_addressing())) {
return false;
}
/* Some instructions implemented in the generator backend, such as
* derivatives, assume that their operands are packed so we can't
* generally propagate strided regions to them.
*/
const unsigned entry_stride = (entry->src.file == FIXED_GRF ? 1 :
entry->src.stride);
if (instruction_requires_packed_data(inst) && entry_stride != 1)
return false;
const brw_reg_type dst_type = (has_source_modifiers &&
entry->dst.type != inst->src[arg].type) ?
entry->dst.type : inst->dst.type;
/* Bail if the result of composing both strides would exceed the
* hardware limit.
*/
if (!can_take_stride(inst, dst_type, arg,
entry_stride * inst->src[arg].stride,
compiler))
return false;
/* From the Cherry Trail/Braswell PRMs, Volume 7: 3D Media GPGPU:
* EU Overview
* Register Region Restrictions
* Special Requirements for Handling Double Precision Data Types :
*
* "When source or destination datatype is 64b or operation is integer
* DWord multiply, regioning in Align1 must follow these rules:
*
* 1. Source and Destination horizontal stride must be aligned to the
* same qword.
* 2. Regioning must ensure Src.Vstride = Src.Width * Src.Hstride.
* 3. Source and Destination offset must be the same, except the case
* of scalar source."
*
* Most of this is already checked in can_take_stride(), we're only left
* with checking 3.
*/
if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&
entry_stride != 0 &&
(reg_offset(inst->dst) % (REG_SIZE * reg_unit(devinfo))) != (reg_offset(entry->src) % (REG_SIZE * reg_unit(devinfo))))
return false;
/*
* Bail if the composition of both regions would be affected by the Xe2+
* regioning restrictions that apply to integer types smaller than a dword.
* See BSpec #56640 for details.
*/
const brw_reg tmp = horiz_stride(entry->src, inst->src[arg].stride);
if (has_subdword_integer_region_restriction(devinfo, inst, &tmp, 1))
return false;
/* The <8;8,0> regions used for FS attributes in multipolygon
* dispatch mode could violate regioning restrictions, don't copy
* propagate them in such cases.
*/
if (entry->src.file == ATTR && max_polygons > 1 &&
(has_dst_aligned_region_restriction(devinfo, inst, dst_type) ||
instruction_requires_packed_data(inst) ||
(inst->is_3src(compiler) && arg == 2) ||
entry->dst.type != inst->src[arg].type))
return false;
/* Bail if the source FIXED_GRF region of the copy cannot be trivially
* composed with the source region of the instruction -- E.g. because the
* copy uses some extended stride greater than 4 not supported natively by
* the hardware as a horizontal stride, or because instruction compression
* could require us to use a vertical stride shorter than a GRF.
*/
if (entry->src.file == FIXED_GRF &&
(inst->src[arg].stride > 4 ||
inst->dst.component_size(inst->exec_size) >
inst->src[arg].component_size(inst->exec_size)))
return false;
/* Bail if the instruction type is larger than the execution type of the
* copy, what implies that each channel is reading multiple channels of the
* destination of the copy, and simply replacing the sources would give a
* program with different semantics.
*/
if (brw_type_size_bits(entry->dst.type) < brw_type_size_bits(inst->src[arg].type) ||
(entry->is_partial_write && inst->opcode != BRW_OPCODE_MOV)) {
return false;
intel/fs: Allow copy propagation between MOVs of mixed sizes This eliminates some spurious, size-converting moves. For example, on Ice Lake this helps dEQP-VK.spirv_assembly.type.vec3.i8.bitwise_xor_frag: SIMD8 shader: 52 instructions. 1 loops. 4164 cycles. 0:0 spills:fills, 5 sends SIMD8 shader: 49 instructions. 1 loops. 4044 cycles. 0:0 spills:fills, 5 sends Unfortunately, this doesn't clean everything up. Here's a subset of the "before" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; mov(8) g12<1>UB g7<32,8,4>UB { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g12<8,8,1>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g14<1>UB g8<32,8,4>UB { align1 1Q }; mov(8) g16<1>UW g14<8,8,1>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; And here's the same subset of the "after" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g7<32,8,4>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g16<1>UW g8<32,8,4>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; There are a lot of regioning and type restrictions in fs_visitor::try_copy_propagate, and I'm a little nervious about messing with them too much. Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Suggested-by: Francisco Jerez <currojerez@riseup.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9025>
2021-04-13 14:07:19 -07:00
}
/* Bail if the result of composing both strides cannot be expressed
* as another stride. This avoids, for example, trying to transform
* this:
*
* MOV (8) rX<1>UD rY<0;1,0>UD
* FOO (8) ... rX<8;8,1>UW
*
* into this:
*
* FOO (8) ... rY<0;1,0>UW
*
* Which would have different semantics.
*/
if (entry_stride != 1 &&
(inst->src[arg].stride *
brw_type_size_bytes(inst->src[arg].type)) % brw_type_size_bytes(entry->src.type) != 0)
return false;
/* Since semantics of source modifiers are type-dependent we need to
* ensure that the meaning of the instruction remains the same if we
* change the type. If the sizes of the types are different the new
* instruction will read a different amount of data than the original
* and the semantics will always be different.
*/
if (has_source_modifiers &&
entry->dst.type != inst->src[arg].type &&
(!inst->can_change_types() ||
brw_type_size_bits(entry->dst.type) != brw_type_size_bits(inst->src[arg].type)))
return false;
if ((entry->src.negate || entry->src.abs) &&
is_logic_op(inst->opcode)) {
return false;
}
/* Save the offset of inst->src[arg] relative to entry->dst for it to be
* applied later.
*/
const unsigned rel_offset = inst->src[arg].offset - entry->dst.offset;
/* Fold the copy into the instruction consuming it. */
inst->src[arg].file = entry->src.file;
inst->src[arg].nr = entry->src.nr;
inst->src[arg].subnr = entry->src.subnr;
inst->src[arg].offset = entry->src.offset;
/* Compose the strides of both regions. */
if (entry->src.file == FIXED_GRF) {
if (inst->src[arg].stride) {
const unsigned orig_width = 1 << entry->src.width;
const unsigned reg_width =
REG_SIZE / (brw_type_size_bytes(inst->src[arg].type) *
inst->src[arg].stride);
inst->src[arg].width = cvt(MIN2(orig_width, reg_width)) - 1;
inst->src[arg].hstride = cvt(inst->src[arg].stride);
inst->src[arg].vstride = inst->src[arg].hstride + inst->src[arg].width;
} else {
inst->src[arg].vstride = inst->src[arg].hstride =
inst->src[arg].width = 0;
}
inst->src[arg].stride = 1;
/* Hopefully no Align16 around here... */
assert(entry->src.swizzle == BRW_SWIZZLE_XYZW);
inst->src[arg].swizzle = entry->src.swizzle;
} else {
inst->src[arg].stride *= entry->src.stride;
}
/* Compute the first component of the copy that the instruction is
* reading, and the base byte offset within that component.
*/
assert(entry->dst.stride == 1);
const unsigned component = rel_offset / brw_type_size_bytes(entry->dst.type);
const unsigned suboffset = rel_offset % brw_type_size_bytes(entry->dst.type);
/* Calculate the byte offset at the origin of the copy of the given
* component and suboffset.
*/
inst->src[arg] = byte_offset(inst->src[arg],
component * entry_stride * brw_type_size_bytes(entry->src.type) + suboffset);
if (has_source_modifiers) {
if (entry->dst.type != inst->src[arg].type) {
/* We are propagating source modifiers from a MOV with a different
* type. If we got here, then we can just change the source and
* destination types of the instruction and keep going.
*/
for (int i = 0; i < inst->sources; i++) {
inst->src[i].type = entry->dst.type;
}
inst->dst.type = entry->dst.type;
}
if (!inst->src[arg].abs) {
inst->src[arg].abs = entry->src.abs;
inst->src[arg].negate ^= entry->src.negate;
}
}
return true;
}
static bool
try_constant_propagate_value(brw_reg val, brw_reg_type dst_type,
fs_inst *inst, int arg)
{
bool progress = false;
if (brw_type_size_bytes(val.type) > 4)
return false;
/* If the size of the use type is smaller than the size of the entry,
* clamp the value to the range of the use type. This enables constant
* copy propagation in cases like
*
*
* mov(8) g12<1>UD 0x0000000cUD
* ...
* mul(8) g47<1>D g86<8,8,1>D g12<16,8,2>W
*/
if (brw_type_size_bits(inst->src[arg].type) <
brw_type_size_bits(dst_type)) {
if (brw_type_size_bytes(inst->src[arg].type) != 2 ||
brw_type_size_bytes(dst_type) != 4)
return false;
assert(inst->src[arg].subnr == 0 || inst->src[arg].subnr == 2);
/* When subnr is 0, we want the lower 16-bits, and when it's 2, we
* want the upper 16-bits. No other values of subnr are valid for a
* UD source.
*/
const uint16_t v = inst->src[arg].subnr == 2 ? val.ud >> 16 : val.ud;
val.ud = v | (uint32_t(v) << 16);
}
val.type = inst->src[arg].type;
if (inst->src[arg].abs) {
if (is_logic_op(inst->opcode) ||
!brw_reg_abs_immediate(&val)) {
return false;
}
}
if (inst->src[arg].negate) {
if (is_logic_op(inst->opcode) ||
!brw_reg_negate_immediate(&val)) {
return false;
}
}
switch (inst->opcode) {
case BRW_OPCODE_MOV:
case SHADER_OPCODE_LOAD_PAYLOAD:
case SHADER_OPCODE_POW:
case FS_OPCODE_PACK:
inst->src[arg] = val;
progress = true;
break;
case BRW_OPCODE_SUBB:
if (arg == 1) {
inst->src[arg] = val;
progress = true;
}
break;
case BRW_OPCODE_MACH:
case BRW_OPCODE_MUL:
case SHADER_OPCODE_MULH:
case BRW_OPCODE_ADD:
case BRW_OPCODE_XOR:
case BRW_OPCODE_ADDC:
if (arg == 1) {
inst->src[arg] = val;
progress = true;
} else if (arg == 0 && inst->src[1].file != IMM) {
/* We used to not copy propagate the constant in situations like
*
* mov(8) g8<1>D 0x7fffffffD
* mul(8) g16<1>D g8<8,8,1>D g15<16,8,2>W
*
* On platforms that only have a 32x16 multiplier, this would
* result in lowering the multiply to
*
* mul(8) g15<1>D g14<8,8,1>D 0xffffUW
* mul(8) g16<1>D g14<8,8,1>D 0x7fffUW
* add(8) g15.1<2>UW g15.1<16,8,2>UW g16<16,8,2>UW
*
* On Gfx8 and Gfx9, which have the full 32x32 multiplier, it
* would results in
*
* mul(8) g16<1>D g15<16,8,2>W 0x7fffffffD
*
* Volume 2a of the Skylake PRM says:
*
* When multiplying a DW and any lower precision integer, the
* DW operand must on src0.
*
* So it would have been invalid. However, brw_fs_combine_constants
* will now "fix" the constant.
*/
if (inst->opcode == BRW_OPCODE_MUL &&
brw_type_size_bytes(inst->src[1].type) < 4 &&
(inst->src[0].type == BRW_TYPE_D ||
inst->src[0].type == BRW_TYPE_UD)) {
inst->src[0] = val;
inst->src[0].type = BRW_TYPE_D;
progress = true;
break;
}
/* Fit this constant in by commuting the operands.
* Exception: we can't do this for 32-bit integer MUL/MACH
* because it's asymmetric.
*
* The BSpec says for Broadwell that
*
* "When multiplying DW x DW, the dst cannot be accumulator."
*
* Integer MUL with a non-accumulator destination will be lowered
* by lower_integer_multiplication(), so don't restrict it.
*/
if (((inst->opcode == BRW_OPCODE_MUL &&
inst->dst.is_accumulator()) ||
inst->opcode == BRW_OPCODE_MACH) &&
(inst->src[1].type == BRW_TYPE_D ||
inst->src[1].type == BRW_TYPE_UD))
break;
inst->src[0] = inst->src[1];
inst->src[1] = val;
progress = true;
}
break;
intel/fs: Combine constants for integer instructions too v2: Remove type change for SHR with negation. This was a leftover from a previous attempt to deal with SHR and negation. Now all right-shifts with unsigned parameters are marked as not being able to have source modifiers. v3: Disallow negations on right shifts of unsigned sources by setting the no_negations flag in add_candidate_immediate. This eliminates the need to exclude SHR in can_do_source_mods. Tiger Lake total instructions in shared programs: 21102817 -> 21099443 (-0.02%) instructions in affected programs: 296796 -> 293422 (-1.14%) helped: 92 / HURT: 356 total cycles in shared programs: 790564691 -> 790393358 (-0.02%) cycles in affected programs: 36456886 -> 36285553 (-0.47%) helped: 171 / HURT: 286 total spills in shared programs: 3951 -> 3959 (0.20%) spills in affected programs: 176 -> 184 (4.55%) helped: 0 / HURT: 2 total fills in shared programs: 2631 -> 2639 (0.30%) fills in affected programs: 176 -> 184 (4.55%) helped: 0 / HURT: 2 LOST: 0 GAINED: 4 Ice Lake total instructions in shared programs: 19954204 -> 19949122 (-0.03%) instructions in affected programs: 40301 -> 35219 (-12.61%) helped: 23 / HURT: 2 total cycles in shared programs: 858377735 -> 858462082 (<.01%) cycles in affected programs: 75537286 -> 75621633 (0.11%) helped: 124 / HURT: 319 total spills in shared programs: 6255 -> 6190 (-1.04%) spills in affected programs: 392 -> 327 (-16.58%) helped: 1 / HURT: 2 total fills in shared programs: 7813 -> 7382 (-5.52%) fills in affected programs: 942 -> 511 (-45.75%) helped: 1 / HURT: 2 LOST: 0 GAINED: 3 Skylake total instructions in shared programs: 18049362 -> 18044440 (-0.03%) instructions in affected programs: 48317 -> 43395 (-10.19%) helped: 26 / HURT: 2 total cycles in shared programs: 844884806 -> 844915655 (<.01%) cycles in affected programs: 76137133 -> 76167982 (0.04%) helped: 171 / HURT: 293 total spills in shared programs: 6148 -> 6149 (0.02%) spills in affected programs: 595 -> 596 (0.17%) helped: 4 / HURT: 2 total fills in shared programs: 7484 -> 7067 (-5.57%) fills in affected programs: 1226 -> 809 (-34.01%) helped: 4 / HURT: 2 LOST: 0 GAINED: 8 Broadwell total instructions in shared programs: 17826844 -> 17821805 (-0.03%) instructions in affected programs: 60687 -> 55648 (-8.30%) helped: 28 / HURT: 8 total cycles in shared programs: 905332682 -> 904369499 (-0.11%) cycles in affected programs: 76743509 -> 75780326 (-1.26%) helped: 179 / HURT: 225 total spills in shared programs: 17922 -> 17908 (-0.08%) spills in affected programs: 2495 -> 2481 (-0.56%) helped: 6 / HURT: 8 total fills in shared programs: 26290 -> 25397 (-3.40%) fills in affected programs: 2606 -> 1713 (-34.27%) helped: 8 / HURT: 6 LOST: 1 GAINED: 1 Haswell total instructions in shared programs: 16678878 -> 16674444 (-0.03%) instructions in affected programs: 78458 -> 74024 (-5.65%) helped: 87 / HURT: 6 total cycles in shared programs: 880189381 -> 880301043 (0.01%) cycles in affected programs: 29956463 -> 30068125 (0.37%) helped: 169 / HURT: 163 total spills in shared programs: 14428 -> 14378 (-0.35%) spills in affected programs: 2384 -> 2334 (-2.10%) helped: 8 / HURT: 6 total fills in shared programs: 16975 -> 16881 (-0.55%) fills in affected programs: 1334 -> 1240 (-7.05%) helped: 10 / HURT: 4 Ivy Bridge total instructions in shared programs: 15706048 -> 15706035 (<.01%) instructions in affected programs: 9941 -> 9928 (-0.13%) helped: 13 / HURT: 0 total cycles in shared programs: 433618834 -> 433624637 (<.01%) cycles in affected programs: 12926714 -> 12932517 (0.04%) helped: 52 / HURT: 41 Sandy Bridge total cycles in shared programs: 741223552 -> 741223443 (<.01%) cycles in affected programs: 19814 -> 19705 (-0.55%) helped: 14 / HURT: 0 No changes on Iron Lake or GM45 fossil-db changes: Tiger Lake Instructions in all programs: 156858030 -> 156905532 (+0.0%) Instructions helped: 3915 Instructions hurt: 15411 Cycles in all programs: 7529667771 -> 7532117340 (+0.0%) Cycles helped: 10260 Cycles hurt: 9990 Spills in all programs: 5610 -> 5457 (-2.7%) Spills helped: 18 Fills in all programs: 6274 -> 6091 (-2.9%) Fills helped: 18 Gained: 2 Lost: 16 Ice Lake Instructions in all programs: 141308082 -> 141303083 (-0.0%) Instructions helped: 574 Instructions hurt: 172 Cycles in all programs: 9091361325 -> 9094622766 (+0.0%) Cycles helped: 8764 Cycles hurt: 11702 Spills in all programs: 7531 -> 7385 (-1.9%) Spills helped: 19 Fills in all programs: 8462 -> 8294 (-2.0%) Fills helped: 19 Gained: 22 Lost: 15 Skylake Instructions in all programs: 131872162 -> 131867263 (-0.0%) Instructions helped: 566 Instructions hurt: 172 Cycles in all programs: 8795095440 -> 8799676943 (+0.1%) Cycles helped: 8333 Cycles hurt: 12182 Spills in all programs: 7006 -> 6884 (-1.7%) Spills helped: 13 Fills in all programs: 7696 -> 7552 (-1.9%) Fills helped: 13 Gained: 24 Lost: 1 Tested-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7698>
2020-11-12 14:50:23 -08:00
case BRW_OPCODE_ADD3:
/* add3 can have a single imm16 source. Proceed if the source type is
* already W or UW or the value can be coerced to one of those types.
*/
if (val.type == BRW_TYPE_W || val.type == BRW_TYPE_UW)
; /* Nothing to do. */
else if (val.ud <= 0xffff)
val = brw_imm_uw(val.ud);
else if (val.d >= -0x8000 && val.d <= 0x7fff)
val = brw_imm_w(val.d);
else
break;
if (arg == 2) {
inst->src[arg] = val;
progress = true;
} else if (inst->src[2].file != IMM) {
inst->src[arg] = inst->src[2];
inst->src[2] = val;
progress = true;
}
intel/fs: Constant fold SHL This is a modified version of a commit originally in !7698. This version add the changes to brw_fs_copy_propagation. If the address passed to fs_visitor::swizzle_nir_scratch_addr is a constant, that function will generate SHL with two constant sources. DG2 uses a different path to generate those addresses, so the constant folding can't occur there yet. That will be addressed in the next commit. What follows is the commit change history from that older MR. v2: Previously this commit was after `intel/fs: Combine constants for integer instructions too`. However, this commit can create invalid instructions that are only cleaned up by `intel/fs: Combine constants for integer instructions too`. That would potentially affect the shader-db results of each commit, but I did not collect new data for the reordering. v3: Fix masking for W/UW and for Q/UQ types. Add an assertion for !saturate. Both suggested by Ken. Also add an assertion that B/UB types don't matically come back. v4: Fix sources count. See also ed3c2f73dbb ("intel/fs: fixup sources number from opt_algebraic"). v5: Fix typo in comment added in v3. Noticed by Marcin. Fix a typo in a comment added when pulling this commit out of !7698. Noticed by Ken. shader-db results: DG2 No changes. Tiger Lake, Ice Lake, and Skylake had similar results (Ice Lake shown) total instructions in shared programs: 20655696 -> 20651648 (-0.02%) instructions in affected programs: 23125 -> 19077 (-17.50%) helped: 7 / HURT: 0 total cycles in shared programs: 858436639 -> 858407749 (<.01%) cycles in affected programs: 8990532 -> 8961642 (-0.32%) helped: 7 / HURT: 0 Broadwell and Haswell had similar results. (Broadwell shown) total instructions in shared programs: 18500780 -> 18496630 (-0.02%) instructions in affected programs: 24715 -> 20565 (-16.79%) helped: 7 / HURT: 0 total cycles in shared programs: 946100660 -> 946087688 (<.01%) cycles in affected programs: 5838252 -> 5825280 (-0.22%) helped: 7 / HURT: 0 total spills in shared programs: 17588 -> 17572 (-0.09%) spills in affected programs: 1206 -> 1190 (-1.33%) helped: 2 / HURT: 0 total fills in shared programs: 25192 -> 25156 (-0.14%) fills in affected programs: 156 -> 120 (-23.08%) helped: 2 / HURT: 0 No shader-db changes on any older Intel platforms. fossil-db results: DG2 Totals: Instrs: 197780415 -> 197780372 (-0.00%); split: -0.00%, +0.00% Cycles: 14066412266 -> 14066410782 (-0.00%); split: -0.00%, +0.00% Totals from 16 (0.00% of 668055) affected shaders: Instrs: 16420 -> 16377 (-0.26%); split: -0.43%, +0.17% Cycles: 220133 -> 218649 (-0.67%); split: -0.69%, +0.01% Tiger Lake, Ice Lake and Skylake had similar results. (Ice Lake shown) Totals: Instrs: 153425977 -> 153423678 (-0.00%) Cycles: 14747928947 -> 14747929547 (+0.00%); split: -0.00%, +0.00% Subgroup size: 8535968 -> 8535976 (+0.00%) Send messages: 7697606 -> 7697607 (+0.00%) Scratch Memory Size: 4380672 -> 4381696 (+0.02%) Totals from 6 (0.00% of 662749) affected shaders: Instrs: 13893 -> 11594 (-16.55%) Cycles: 5386074 -> 5386674 (+0.01%); split: -0.42%, +0.43% Subgroup size: 80 -> 88 (+10.00%) Send messages: 675 -> 676 (+0.15%) Scratch Memory Size: 91136 -> 92160 (+1.12%) Reviewed-by: Kenneth Graunke <kenneth@whitecape.org> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23884>
2020-11-13 19:11:56 -08:00
break;
case BRW_OPCODE_CMP:
if (arg == 1) {
inst->src[arg] = val;
progress = true;
} else if (arg == 0 && inst->src[1].file != IMM) {
enum brw_conditional_mod new_cmod;
new_cmod = brw_swap_cmod(inst->conditional_mod);
if (new_cmod != BRW_CONDITIONAL_NONE) {
/* Fit this constant in by swapping the operands and
* flipping the test
*/
inst->src[0] = inst->src[1];
inst->src[1] = val;
inst->conditional_mod = new_cmod;
progress = true;
}
}
break;
case BRW_OPCODE_SEL:
if (arg == 1) {
inst->src[arg] = val;
progress = true;
} else if (arg == 0) {
if (inst->src[1].file != IMM &&
(inst->conditional_mod == BRW_CONDITIONAL_NONE ||
/* Only GE and L are commutative. */
inst->conditional_mod == BRW_CONDITIONAL_GE ||
inst->conditional_mod == BRW_CONDITIONAL_L)) {
inst->src[0] = inst->src[1];
inst->src[1] = val;
/* If this was predicated, flipping operands means
* we also need to flip the predicate.
*/
if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
inst->predicate_inverse =
!inst->predicate_inverse;
}
} else {
inst->src[0] = val;
}
intel/fs: Better handle constant sources of FS_OPCODE_PACK_HALF_2x16_SPLIT I noticed that a *LOT* of fragment shaders in Shadow of the Tomb Raider, for instance, end up with a sequence of NIR like: vec1 32 ssa_2 = load_const (0x00000000 = 0.000000) ... vec1 32 ssa_191 = pack_half_2x16_split ssa_188, ssa_2 vec1 32 ssa_192 = pack_half_2x16_split ssa_189, ssa_2 vec1 32 ssa_193 = pack_half_2x16_split ssa_190, ssa_2 This results in an assembly sequence like: mov(8) g28<1>UD 0x00000000UD mov(8) g21<2>HF g28<8,8,1>F shl(8) g21<1>UD g21<8,8,1>UD 0x00000010UD mov(8) g21<2>HF g25<8,8,1>F mov(8) g19<2>HF g28<8,8,1>F shl(8) g19<1>UD g19<8,8,1>UD 0x00000010UD mov(8) g19<2>HF g23<8,8,1>F mov(8) g20<2>HF g28<8,8,1>F shl(8) g20<1>UD g20<8,8,1>UD 0x00000010UD mov(8) g20<2>HF g24<8,8,1>F After this commit, the generated assembly is: mov(8) g21<1>UD 0x00000000UD mov(8) g21<2>HF g23<8,8,1>F mov(8) g19<1>UD 0x00000000UD mov(8) g19<2>HF g17<8,8,1>F mov(8) g20<1>UD 0x00000000UD mov(8) g20<2>HF g18<8,8,1>F Tiger Lake, Ice Lake, Skylake, and Haswell had similar results. (Ice Lake shown) total instructions in shared programs: 20119086 -> 20119034 (<.01%) instructions in affected programs: 9056 -> 9004 (-0.57%) helped: 8 HURT: 0 helped stats (abs) min: 2 max: 16 x̄: 6.50 x̃: 4 helped stats (rel) min: 0.29% max: 1.75% x̄: 1.00% x̃: 0.98% 95% mean confidence interval for instructions value: -11.01 -1.99 95% mean confidence interval for instructions %-change: -1.56% -0.44% Instructions are helped. total cycles in shared programs: 861019414 -> 861021044 (<.01%) cycles in affected programs: 279862 -> 281492 (0.58%) helped: 4 HURT: 2 helped stats (abs) min: 6 max: 936 x̄: 239.00 x̃: 7 helped stats (rel) min: 0.03% max: 8.13% x̄: 2.09% x̃: 0.09% HURT stats (abs) min: 18 max: 2568 x̄: 1293.00 x̃: 1293 HURT stats (rel) min: 0.36% max: 1.14% x̄: 0.75% x̃: 0.75% 95% mean confidence interval for cycles value: -972.56 1515.89 95% mean confidence interval for cycles %-change: -4.77% 2.49% Inconclusive result (value mean confidence interval includes 0). Broadwell total instructions in shared programs: 17812327 -> 17812263 (<.01%) instructions in affected programs: 9867 -> 9803 (-0.65%) helped: 8 HURT: 0 helped stats (abs) min: 2 max: 28 x̄: 8.00 x̃: 4 helped stats (rel) min: 0.32% max: 1.80% x̄: 1.00% x̃: 0.95% 95% mean confidence interval for instructions value: -15.46 -0.54 95% mean confidence interval for instructions %-change: -1.54% -0.47% Instructions are helped. total cycles in shared programs: 904768620 -> 904773291 (<.01%) cycles in affected programs: 454799 -> 459470 (1.03%) helped: 4 HURT: 4 helped stats (abs) min: 36 max: 586 x̄: 344.50 x̃: 378 helped stats (rel) min: 0.47% max: 4.04% x̄: 2.01% x̃: 1.77% HURT stats (abs) min: 1 max: 5572 x̄: 1512.25 x̃: 238 HURT stats (rel) min: <.01% max: 2.77% x̄: 1.46% x̃: 1.53% 95% mean confidence interval for cycles value: -1122.40 2290.15 95% mean confidence interval for cycles %-change: -2.26% 1.71% Inconclusive result (value mean confidence interval includes 0). total spills in shared programs: 18581 -> 18579 (-0.01%) spills in affected programs: 323 -> 321 (-0.62%) helped: 1 HURT: 0 total fills in shared programs: 24985 -> 24981 (-0.02%) fills in affected programs: 1348 -> 1344 (-0.30%) helped: 1 HURT: 0 Tiger Lake, Ice Lake, and Skylake had similar results. (Ice Lake shown) Instructions in all programs: 143585431 -> 143513657 (-0.0%) Instructions helped: 14403 Cycles in all programs: 8439312778 -> 8439371578 (+0.0%) Cycles helped: 10570 Cycles hurt: 3290 Gained: 146 Lost: 74 All of the lost and gained fossil-db shaders are SIMD32 fragment shaders. 14,247 of the affected shaders are from Shadow of the Tomb Raider. 154 are from Batman Arkham Origins, and the remaining two are from Octopath Traveler. Reviewed-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15089>
2022-02-14 14:07:18 -08:00
progress = true;
}
break;
case BRW_OPCODE_CSEL:
assert(inst->conditional_mod != BRW_CONDITIONAL_NONE);
if (arg == 0 &&
inst->src[1].file != IMM &&
(!brw_type_is_float(inst->src[1].type) ||
inst->conditional_mod == BRW_CONDITIONAL_NZ ||
inst->conditional_mod == BRW_CONDITIONAL_Z)) {
/* Only EQ and NE are commutative due to NaN issues. */
inst->src[0] = inst->src[1];
inst->src[1] = val;
inst->conditional_mod = brw_negate_cmod(inst->conditional_mod);
} else {
/* While CSEL is a 3-source instruction, the last source should never
* be a constant. We'll support that, but should it ever happen, we
* should add support to the constant folding pass.
*/
inst->src[arg] = val;
}
progress = true;
break;
case FS_OPCODE_FB_WRITE_LOGICAL:
/* The stencil and omask sources of FS_OPCODE_FB_WRITE_LOGICAL are
* bit-cast using a strided region so they cannot be immediates.
*/
if (arg != FB_WRITE_LOGICAL_SRC_SRC_STENCIL &&
arg != FB_WRITE_LOGICAL_SRC_OMASK) {
inst->src[arg] = val;
progress = true;
}
break;
case SHADER_OPCODE_INT_QUOTIENT:
case SHADER_OPCODE_INT_REMAINDER:
case BRW_OPCODE_AND:
case BRW_OPCODE_ASR:
case BRW_OPCODE_BFE:
case BRW_OPCODE_BFI1:
case BRW_OPCODE_BFI2:
case BRW_OPCODE_ROL:
case BRW_OPCODE_ROR:
case BRW_OPCODE_SHL:
case BRW_OPCODE_SHR:
case BRW_OPCODE_OR:
case SHADER_OPCODE_TEX_LOGICAL:
case SHADER_OPCODE_TXD_LOGICAL:
case SHADER_OPCODE_TXF_LOGICAL:
case SHADER_OPCODE_TXL_LOGICAL:
case SHADER_OPCODE_TXS_LOGICAL:
case FS_OPCODE_TXB_LOGICAL:
case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
case SHADER_OPCODE_TXF_CMS_W_GFX12_LOGICAL:
case SHADER_OPCODE_TXF_MCS_LOGICAL:
case SHADER_OPCODE_LOD_LOGICAL:
case SHADER_OPCODE_TG4_BIAS_LOGICAL:
case SHADER_OPCODE_TG4_EXPLICIT_LOD_LOGICAL:
case SHADER_OPCODE_TG4_IMPLICIT_LOD_LOGICAL:
case SHADER_OPCODE_TG4_LOGICAL:
case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
case SHADER_OPCODE_TG4_OFFSET_LOD_LOGICAL:
case SHADER_OPCODE_TG4_OFFSET_BIAS_LOGICAL:
case SHADER_OPCODE_SAMPLEINFO_LOGICAL:
case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
case SHADER_OPCODE_MEMORY_LOAD_LOGICAL:
case SHADER_OPCODE_MEMORY_STORE_LOGICAL:
case SHADER_OPCODE_MEMORY_ATOMIC_LOGICAL:
case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL:
case SHADER_OPCODE_BROADCAST:
case BRW_OPCODE_MAD:
case BRW_OPCODE_LRP:
case FS_OPCODE_PACK_HALF_2x16_SPLIT:
case SHADER_OPCODE_SHUFFLE:
inst->src[arg] = val;
progress = true;
break;
default:
break;
}
return progress;
}
static bool
try_constant_propagate(fs_inst *inst, acp_entry *entry, int arg)
{
if (inst->src[arg].file != VGRF)
return false;
assert(entry->dst.file == VGRF);
if (inst->src[arg].nr != entry->dst.nr)
return false;
/* Bail if inst is reading a range that isn't contained in the range
* that entry is writing.
*/
if (!region_contained_in(inst->src[arg], inst->size_read(arg),
entry->dst, entry->size_written))
return false;
/* If the size of the use type is larger than the size of the entry
* type, the entry doesn't contain all of the data that the user is
* trying to use.
*/
if (brw_type_size_bits(inst->src[arg].type) >
brw_type_size_bits(entry->dst.type))
return false;
return try_constant_propagate_value(entry->src, entry->dst.type, inst, arg);
}
static bool
can_propagate_from(fs_inst *inst)
{
return (inst->opcode == BRW_OPCODE_MOV &&
inst->dst.file == VGRF &&
((inst->src[0].file == VGRF &&
!grf_regions_overlap(inst->dst, inst->size_written,
inst->src[0], inst->size_read(0))) ||
inst->src[0].file == ATTR ||
inst->src[0].file == UNIFORM ||
inst->src[0].file == IMM ||
(inst->src[0].file == FIXED_GRF &&
inst->src[0].is_contiguous())) &&
intel/brw: Copy prop from raw integer moves with mismatched types The specific pattern from the unit test was observed in ray tracing trampoline shaders. v2: Refactor the is_raw_move tests out to a utility function. Suggested by Ken. v3: Fix a regression caused by being too picky about source modifiers. This was introduced somewhere between when I did initial shader-db runs an v2. v4: Fix typo in comment. Noticed by Caio. shader-db: All Intel platforms had similar results. (Meteor Lake shown) total instructions in shared programs: 19734086 -> 19733997 (<.01%) instructions in affected programs: 135388 -> 135299 (-0.07%) helped: 76 / HURT: 2 total cycles in shared programs: 916290451 -> 916264968 (<.01%) cycles in affected programs: 41046002 -> 41020519 (-0.06%) helped: 32 / HURT: 29 fossil-db: Meteor Lake, DG2, and Skylake had similar results. (Meteor Lake shown) Totals: Instrs: 151531355 -> 151513669 (-0.01%); split: -0.01%, +0.00% Cycle count: 17209372399 -> 17208178205 (-0.01%); split: -0.01%, +0.00% Max live registers: 32016490 -> 32016493 (+0.00%) Totals from 17361 (2.75% of 630198) affected shaders: Instrs: 2642048 -> 2624362 (-0.67%); split: -0.67%, +0.00% Cycle count: 79803066 -> 78608872 (-1.50%); split: -1.75%, +0.25% Max live registers: 421668 -> 421671 (+0.00%) Tiger Lake and Ice Lake had similar results. (Tiger Lake shown) Totals: Instrs: 149995644 -> 149977326 (-0.01%); split: -0.01%, +0.00% Cycle count: 15567293770 -> 15566524840 (-0.00%); split: -0.02%, +0.01% Spill count: 61241 -> 61238 (-0.00%) Fill count: 107304 -> 107301 (-0.00%) Max live registers: 31993109 -> 31993112 (+0.00%) Totals from 17813 (2.83% of 629912) affected shaders: Instrs: 3738236 -> 3719918 (-0.49%); split: -0.49%, +0.00% Cycle count: 4251157049 -> 4250388119 (-0.02%); split: -0.06%, +0.04% Spill count: 28268 -> 28265 (-0.01%) Fill count: 50377 -> 50374 (-0.01%) Max live registers: 470648 -> 470651 (+0.00%) Reviewed-by: Kenneth Graunke <kenneth@whitecape.org> Reviewed-by: Caio Oliveira <caio.oliveira@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30251>
2024-07-16 16:04:38 -07:00
/* is_raw_move also rejects source modifiers, but copy propagation
* can handle that if the types are the same.
*/
((inst->src[0].type == inst->dst.type &&
!inst->saturate) ||
inst->is_raw_move()) &&
intel/fs: Allow copy propagation between MOVs of mixed sizes This eliminates some spurious, size-converting moves. For example, on Ice Lake this helps dEQP-VK.spirv_assembly.type.vec3.i8.bitwise_xor_frag: SIMD8 shader: 52 instructions. 1 loops. 4164 cycles. 0:0 spills:fills, 5 sends SIMD8 shader: 49 instructions. 1 loops. 4044 cycles. 0:0 spills:fills, 5 sends Unfortunately, this doesn't clean everything up. Here's a subset of the "before" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; mov(8) g12<1>UB g7<32,8,4>UB { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g12<8,8,1>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g14<1>UB g8<32,8,4>UB { align1 1Q }; mov(8) g16<1>UW g14<8,8,1>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; And here's the same subset of the "after" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g7<32,8,4>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g16<1>UW g8<32,8,4>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; There are a lot of regioning and type restrictions in fs_visitor::try_copy_propagate, and I'm a little nervious about messing with them too much. Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Suggested-by: Francisco Jerez <currojerez@riseup.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9025>
2021-04-13 14:07:19 -07:00
/* Subset of !is_partial_write() conditions. */
!inst->predicate && inst->dst.is_contiguous()) ||
intel/fs: Add support for copy-propagating a block of multiple FIXED_GRFs. In cases where a LOAD_PAYLOAD instruction copies a single block of sequential GRF registers into the destination (see is_identity_payload()), splitting the block copy into a number of ACP entries (one for each LOAD_PAYLOAD source) is undesirable, because that prevents copy propagation into any instructions which read multiple components at once with the same source (the barycentric source of the LINTERP instruction is going to be the overwhelmingly most common example). Technically it would also be possible to do this for VGRF sources, but there is little benefit from that since register coalesce already covers many of those cases -- There is no way for a block of FIXED_GRFs to be coalesced into a VGRF though. This prevents the following shader-db regressions (including SIMD32 programs) in combination with the interpolation rework part of this series. On SKL: total instructions in shared programs: 18595160 -> 18828562 (1.26%) instructions in affected programs: 13374946 -> 13608348 (1.75%) helped: 7 HURT: 108977 total spills in shared programs: 9116 -> 9106 (-0.11%) spills in affected programs: 404 -> 394 (-2.48%) helped: 7 HURT: 9 total fills in shared programs: 8994 -> 9176 (2.02%) fills in affected programs: 898 -> 1080 (20.27%) helped: 7 HURT: 9 LOST: 469 GAINED: 220 On SNB: total instructions in shared programs: 13996898 -> 14096222 (0.71%) instructions in affected programs: 8088546 -> 8187870 (1.23%) helped: 2 HURT: 66520 total spills in shared programs: 2985 -> 2961 (-0.80%) spills in affected programs: 632 -> 608 (-3.80%) helped: 2 HURT: 0 total fills in shared programs: 3144 -> 3128 (-0.51%) fills in affected programs: 1515 -> 1499 (-1.06%) helped: 2 HURT: 0 LOST: 0 GAINED: 4 Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
2019-12-30 00:36:48 -08:00
is_identity_payload(FIXED_GRF, inst);
}
static void
commute_immediates(fs_inst *inst)
{
/* ADD3 can only have the immediate as src0. */
if (inst->opcode == BRW_OPCODE_ADD3) {
if (inst->src[2].file == IMM) {
const auto src0 = inst->src[0];
inst->src[0] = inst->src[2];
inst->src[2] = src0;
}
}
/* If only one of the sources of a 2-source, commutative instruction (e.g.,
* AND) is immediate, it must be src1. If both are immediate, opt_algebraic
* should fold it away.
*/
if (inst->sources == 2 && inst->is_commutative() &&
inst->src[0].file == IMM && inst->src[1].file != IMM) {
const auto src1 = inst->src[1];
inst->src[1] = inst->src[0];
inst->src[0] = src1;
}
}
/* Walks a basic block and does copy propagation on it using the acp
* list.
*/
static bool
opt_copy_propagation_local(const brw_compiler *compiler, linear_ctx *lin_ctx,
bblock_t *block, struct acp &acp,
const brw::simple_allocator &alloc,
uint8_t max_polygons)
{
bool progress = false;
foreach_inst_in_block(fs_inst, inst, block) {
/* Try propagating into this instruction. */
bool constant_progress = false;
for (int i = inst->sources - 1; i >= 0; i--) {
if (inst->src[i].file != VGRF)
continue;
for (auto iter = acp.find_by_dst(inst->src[i].nr);
iter != acp.end() && (*iter)->dst.nr == inst->src[i].nr;
++iter) {
if ((*iter)->src.file == IMM) {
if (try_constant_propagate(inst, *iter, i)) {
constant_progress = true;
break;
}
} else {
if (try_copy_propagate(compiler, inst, *iter, i, alloc,
max_polygons)) {
progress = true;
break;
}
}
}
}
if (constant_progress) {
commute_immediates(inst);
brw_constant_fold_instruction(compiler->devinfo, inst);
progress = true;
}
/* kill the destination from the ACP */
if (inst->dst.file == VGRF || inst->dst.file == FIXED_GRF) {
for (auto iter = acp.find_by_dst(inst->dst.nr);
iter != acp.end() && (*iter)->dst.nr == inst->dst.nr;
++iter) {
if (grf_regions_overlap((*iter)->dst, (*iter)->size_written,
inst->dst, inst->size_written))
acp.remove(*iter);
}
for (auto iter = acp.find_by_src(inst->dst.nr);
iter != acp.end() && (*iter)->src.nr == inst->dst.nr;
++iter) {
/* Make sure we kill the entry if this instruction overwrites
* _any_ of the registers that it reads
*/
if (grf_regions_overlap((*iter)->src, (*iter)->size_read,
inst->dst, inst->size_written))
acp.remove(*iter);
}
}
/* If this instruction's source could potentially be folded into the
* operand of another instruction, add it to the ACP.
*/
if (can_propagate_from(inst)) {
acp_entry *entry = linear_zalloc(lin_ctx, acp_entry);
entry->dst = inst->dst;
entry->src = inst->src[0];
entry->size_written = inst->size_written;
intel/fs: Add support for copy-propagating a block of multiple FIXED_GRFs. In cases where a LOAD_PAYLOAD instruction copies a single block of sequential GRF registers into the destination (see is_identity_payload()), splitting the block copy into a number of ACP entries (one for each LOAD_PAYLOAD source) is undesirable, because that prevents copy propagation into any instructions which read multiple components at once with the same source (the barycentric source of the LINTERP instruction is going to be the overwhelmingly most common example). Technically it would also be possible to do this for VGRF sources, but there is little benefit from that since register coalesce already covers many of those cases -- There is no way for a block of FIXED_GRFs to be coalesced into a VGRF though. This prevents the following shader-db regressions (including SIMD32 programs) in combination with the interpolation rework part of this series. On SKL: total instructions in shared programs: 18595160 -> 18828562 (1.26%) instructions in affected programs: 13374946 -> 13608348 (1.75%) helped: 7 HURT: 108977 total spills in shared programs: 9116 -> 9106 (-0.11%) spills in affected programs: 404 -> 394 (-2.48%) helped: 7 HURT: 9 total fills in shared programs: 8994 -> 9176 (2.02%) fills in affected programs: 898 -> 1080 (20.27%) helped: 7 HURT: 9 LOST: 469 GAINED: 220 On SNB: total instructions in shared programs: 13996898 -> 14096222 (0.71%) instructions in affected programs: 8088546 -> 8187870 (1.23%) helped: 2 HURT: 66520 total spills in shared programs: 2985 -> 2961 (-0.80%) spills in affected programs: 632 -> 608 (-3.80%) helped: 2 HURT: 0 total fills in shared programs: 3144 -> 3128 (-0.51%) fills in affected programs: 1515 -> 1499 (-1.06%) helped: 2 HURT: 0 LOST: 0 GAINED: 4 Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
2019-12-30 00:36:48 -08:00
for (unsigned i = 0; i < inst->sources; i++)
entry->size_read += inst->size_read(i);
entry->opcode = inst->opcode;
intel/fs: Allow copy propagation between MOVs of mixed sizes This eliminates some spurious, size-converting moves. For example, on Ice Lake this helps dEQP-VK.spirv_assembly.type.vec3.i8.bitwise_xor_frag: SIMD8 shader: 52 instructions. 1 loops. 4164 cycles. 0:0 spills:fills, 5 sends SIMD8 shader: 49 instructions. 1 loops. 4044 cycles. 0:0 spills:fills, 5 sends Unfortunately, this doesn't clean everything up. Here's a subset of the "before" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; mov(8) g12<1>UB g7<32,8,4>UB { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g12<8,8,1>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g14<1>UB g8<32,8,4>UB { align1 1Q }; mov(8) g16<1>UW g14<8,8,1>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; And here's the same subset of the "after" assembly: send(8) g11<1>UW g2<0,1,0>UD 0x02106e02 dp data 1 MsgDesc: ( untyped surface read, Surface = 2, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g7<4>UB g11<8,8,1>UD { align1 1Q }; send(8) g13<1>UW g2<0,1,0>UD 0x02106e03 dp data 1 MsgDesc: ( untyped surface read, Surface = 3, SIMD8, Mask = 0xe) mlen 1 rlen 1 { align1 1Q }; mov(8) g15<1>UW g7<32,8,4>UB { align1 1Q }; mov(8) g8<4>UB g13<8,8,1>UD { align1 1Q }; mov(8) g16<1>UW g8<32,8,4>UB { align1 1Q }; xor(8) g17<1>UW g15<8,8,1>UW g16<8,8,1>UW { align1 1Q }; There are a lot of regioning and type restrictions in fs_visitor::try_copy_propagate, and I'm a little nervious about messing with them too much. Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Suggested-by: Francisco Jerez <currojerez@riseup.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9025>
2021-04-13 14:07:19 -07:00
entry->is_partial_write = inst->is_partial_write();
entry->force_writemask_all = inst->force_writemask_all;
acp.add(entry);
} else if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
inst->dst.file == VGRF) {
i965/fs_reg: Allocate double the number of vgrfs in SIMD16 mode This is actually the squash of a bunch of different changes. Individual commit titles follow: i965/fs: Always 2-align registers SIMD16 for gen <= 5 i965/fs: Use the register width when applying offsets This reworks both byte_offset() and offset() to be more intelligent. The byte_offset() function now supports offsets bigger than 32. The offset() function uses the byte_offset() function together with the register width and the type size to offset the register by the correct amount. i965/fs: Change regs_read to be in hardware registers i965/fs: Change regs_written to be actual hardware registers i965/fs: Properly handle register widths in LOAD_PAYLOAD The LOAD_PAYLOAD instruction is a bit special because it collects a bunch of registers (with possibly different widths) into a single payload block. Once the payload is constructed, it's treated as a single block of data and most of the information such as register widths doesn't matter anymore. In particular, the offset of any particular source register is the accumulation of the sizes of the previous source registers. i965/fs: Properly set writemasks in LOAD_PAYLOAD i965/fs: Handle register widths in demote_pull_constants i965/fs: Get rid of implicit register doubling in the allocator i965/fs: Reserve enough registers for PLN instructions i965/fs: Make sources and destinations interfere in 16-wide i965/fs: Properly handle register widths in CSE i965/fs: Properly handle register widths in register_coalesce i965/fs: Properly handle widths in copy propagation i965/fs: Properly handle register widths in VARYING_PULL_CONSTANT_LOAD i965/fs: Properly handle register widths and odd register sizes in spilling i965/fs: Don't waste a register on texture lookups for gen >= 7 Previously, we were waisting a register in SIMD16 mode because we could only allocate registers in pairs. Now that we can allocate and address odd-sized registers, let's get rid of this special-case. Signed-off-by: Jason Ekstrand <jason.ekstrand@intel.com> Reviewed-by: Matt Turner <mattst88@gmail.com>
2014-08-18 14:27:55 -07:00
int offset = 0;
for (int i = 0; i < inst->sources; i++) {
int effective_width = i < inst->header_size ? 8 : inst->exec_size;
const unsigned size_written =
effective_width * brw_type_size_bytes(inst->src[i].type);
if (inst->src[i].file == VGRF ||
(inst->src[i].file == FIXED_GRF &&
inst->src[i].is_contiguous())) {
const brw_reg_type t = i < inst->header_size ?
BRW_TYPE_UD : inst->src[i].type;
brw_reg dst = byte_offset(retype(inst->dst, t), offset);
if (!dst.equals(inst->src[i])) {
acp_entry *entry = linear_zalloc(lin_ctx, acp_entry);
entry->dst = dst;
entry->src = retype(inst->src[i], t);
entry->size_written = size_written;
entry->size_read = inst->size_read(i);
entry->opcode = inst->opcode;
entry->force_writemask_all = inst->force_writemask_all;
acp.add(entry);
}
}
offset += size_written;
}
}
}
return progress;
}
bool
brw_fs_opt_copy_propagation(fs_visitor &s)
{
bool progress = false;
void *copy_prop_ctx = ralloc_context(NULL);
linear_ctx *lin_ctx = linear_context(copy_prop_ctx);
struct acp out_acp[s.cfg->num_blocks];
const fs_live_variables &live = s.live_analysis.require();
/* First, walk through each block doing local copy propagation and getting
* the set of copies available at the end of the block.
*/
foreach_block (block, s.cfg) {
progress = opt_copy_propagation_local(s.compiler, lin_ctx, block,
out_acp[block->num], s.alloc,
s.max_polygons) || progress;
/* If the destination of an ACP entry exists only within this block,
* then there's no need to keep it for dataflow analysis. We can delete
* it from the out_acp table and avoid growing the bitsets any bigger
* than we absolutely have to.
*
* Because nothing in opt_copy_propagation_local touches the block
* start/end IPs and opt_copy_propagation_local is incapable of
* extending the live range of an ACP destination beyond the block,
* it's safe to use the liveness information in this way.
*/
for (auto iter = out_acp[block->num].begin();
iter != out_acp[block->num].end(); ++iter) {
assert((*iter)->dst.file == VGRF);
if (block->start_ip <= live.vgrf_start[(*iter)->dst.nr] &&
live.vgrf_end[(*iter)->dst.nr] <= block->end_ip) {
out_acp[block->num].remove(*iter);
}
}
}
/* Do dataflow analysis for those available copies. */
fs_copy_prop_dataflow dataflow(lin_ctx, s.cfg, live, out_acp);
/* Next, re-run local copy propagation, this time with the set of copies
* provided by the dataflow analysis available at the start of a block.
*/
foreach_block (block, s.cfg) {
struct acp in_acp;
for (int i = 0; i < dataflow.num_acp; i++) {
intel/fs: Fix copy propagation dataflow analysis in presence of force_writemask_all ACP overwrites. This fixes the behavior of copy propagation in cases where either the source or destination of an ACP is overwritten elsewhere in the program by a force_writemask_all instruction, which could cause the overwrite to be executed for an inactive channel under non-uniform control flow, causing the current per-channel dataflow propagation to give incorrect results. This has been reported in cases like: > while (true) { > x = imageSize(img); > if (non_uniform_condition()) { > y = x; > break; > } > } > use(y); Currently the copy propagation pass would propagate copy 'y = x' into 'use(y)', which is invalid since in the example above imageSize() is implemented as a force_writemask_all SEND message, whose result is broadcast to all channels, so when a given channel executes 'y = x' and breaks out of the loop, another divergent channel can execute a subsequent iteration of the loop overwriting 'x' with a different value, hence replacing 'y' with 'x' at 'use(y)' changes the behavior of the program. This patch extends the global dataflow analysis algorithm to determine whether there is any control flow path from a given copy to an overwrite of its source or destination which has force_writemask_all behavior inconsistent with the copy, and in such case prevents copy propagation for that ACP entry at any point of the program which can be reached from the overwrite, even if the copy is statically re-executed along all such control flow paths (as in the example above), since the execution of the overwrite for a given channel i may corrupt other channels j!=i inactive for the subsequently re-executed copy. Note that a simpler solution has been attempted which fully shuts down copy propagation if such a force_writemask_all ACP overwrite is present /anywhere/ in the program regardless of its location in the control flow graph, however that led to large shader-db regressions in some programs from shader-db (like a CS from Car Chase which would emit 53% more instructions). With this solution the only handful of shaders that suffer instruction count regressions seem to be getting misoptimized right now (e.g. some compute shaders from Deus Ex Mankind). This solution doesn't seem to affect the run-time of shader-db significantly, it's less than 1% higher with the fix applied. Reported-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Acked-by: Matt Turner <mattst88@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21351>
2023-02-10 19:33:50 -08:00
if (BITSET_TEST(dataflow.bd[block->num].livein, i) &&
!BITSET_TEST(dataflow.bd[block->num].exec_mismatch, i)) {
struct acp_entry *entry = dataflow.acp[i];
in_acp.add(entry);
}
}
progress = opt_copy_propagation_local(s.compiler, lin_ctx, block,
in_acp, s.alloc, s.max_polygons) ||
progress;
}
ralloc_free(copy_prop_ctx);
if (progress)
s.invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW |
DEPENDENCY_INSTRUCTION_DETAIL);
return progress;
}
static bool
try_copy_propagate_def(const brw_compiler *compiler,
const brw::simple_allocator &alloc,
fs_inst *def, const brw_reg &val,
fs_inst *inst, int arg,
uint8_t max_polygons)
{
const struct intel_device_info *devinfo = compiler->devinfo;
assert(val.file != BAD_FILE);
/* We can't generally copy-propagate UD negations because we can end up
* accessing the resulting values as signed integers instead.
*/
if (val.negate && val.type == BRW_TYPE_UD)
return false;
/* Bail if the instruction type is larger than the execution type of the
* copy, what implies that each channel is reading multiple channels of the
* destination of the copy, and simply replacing the sources would give a
* program with different semantics.
*/
if (brw_type_size_bits(def->dst.type) <
brw_type_size_bits(inst->src[arg].type))
return false;
const bool has_source_modifiers = val.abs || val.negate;
if (has_source_modifiers) {
if (is_logic_op(inst->opcode) || !inst->can_do_source_mods(devinfo))
return false;
/* Since semantics of source modifiers are type-dependent we need to
* ensure that the meaning of the instruction remains the same if we
* change the type. If the sizes of the types are different the new
* instruction will read a different amount of data than the original
* and the semantics will always be different.
*/
if (def->dst.type != inst->src[arg].type &&
(!inst->can_change_types() ||
brw_type_size_bits(def->dst.type) !=
brw_type_size_bits(inst->src[arg].type)))
return false;
}
/* Send messages with EOT set are restricted to use g112-g127 (and we
* sometimes need g127 for other purposes), so avoid copy propagating
* anything that would make it impossible to satisfy that restriction.
*/
if (inst->eot) {
/* Don't propagate things that are already pinned. */
if (val.file != VGRF)
return false;
/* We might be propagating from a large register, while the SEND only
* is reading a portion of it (say the .A channel in an RGBA value).
* We need to pin both split SEND sources in g112-g126/127, so only
* allow this if the registers aren't too large.
*/
if (inst->opcode == SHADER_OPCODE_SEND && inst->sources >= 4 &&
val.file == VGRF) {
int other_src = arg == 2 ? 3 : 2;
unsigned other_size = inst->src[other_src].file == VGRF ?
alloc.sizes[inst->src[other_src].nr] :
inst->size_read(other_src);
unsigned prop_src_size = alloc.sizes[val.nr];
if (other_size + prop_src_size > 15)
return false;
}
}
/* Reject cases that would violate register regioning restrictions. */
if ((val.file == UNIFORM || !val.is_contiguous()) &&
(inst->is_send_from_grf() || inst->uses_indirect_addressing())) {
return false;
}
/* Some instructions implemented in the generator backend, such as
* derivatives, assume that their operands are packed so we can't
* generally propagate strided regions to them.
*/
const unsigned entry_stride = val.file == FIXED_GRF ? 1 : val.stride;
if (instruction_requires_packed_data(inst) && entry_stride != 1)
return false;
const brw_reg_type dst_type = (has_source_modifiers &&
def->dst.type != inst->src[arg].type) ?
def->dst.type : inst->dst.type;
/* Bail if the result of composing both strides would exceed the
* hardware limit.
*/
if (!can_take_stride(inst, dst_type, arg,
entry_stride * inst->src[arg].stride,
compiler))
return false;
/* Bail if the source FIXED_GRF region of the copy cannot be trivially
* composed with the source region of the instruction -- E.g. because the
* copy uses some extended stride greater than 4 not supported natively by
* the hardware as a horizontal stride, or because instruction compression
* could require us to use a vertical stride shorter than a GRF.
*/
if (val.file == FIXED_GRF &&
(inst->src[arg].stride > 4 ||
inst->dst.component_size(inst->exec_size) >
inst->src[arg].component_size(inst->exec_size)))
return false;
/* Bail if the result of composing both strides cannot be expressed
* as another stride. This avoids, for example, trying to transform
* this:
*
* MOV (8) rX<1>UD rY<0;1,0>UD
* FOO (8) ... rX<8;8,1>UW
*
* into this:
*
* FOO (8) ... rY<0;1,0>UW
*
* Which would have different semantics.
*/
if (entry_stride != 1 &&
(inst->src[arg].stride *
brw_type_size_bytes(inst->src[arg].type)) % brw_type_size_bytes(val.type) != 0)
return false;
/* From the Cherry Trail/Braswell PRMs, Volume 7: 3D Media GPGPU:
* EU Overview
* Register Region Restrictions
* Special Requirements for Handling Double Precision Data Types :
*
* "When source or destination datatype is 64b or operation is integer
* DWord multiply, regioning in Align1 must follow these rules:
*
* 1. Source and Destination horizontal stride must be aligned to the
* same qword.
* 2. Regioning must ensure Src.Vstride = Src.Width * Src.Hstride.
* 3. Source and Destination offset must be the same, except the case
* of scalar source."
*
* Most of this is already checked in can_take_stride(), we're only left
* with checking 3.
*/
if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&
entry_stride != 0 &&
(reg_offset(inst->dst) % (REG_SIZE * reg_unit(devinfo))) != (reg_offset(val) % (REG_SIZE * reg_unit(devinfo))))
return false;
/* The <8;8,0> regions used for FS attributes in multipolygon
* dispatch mode could violate regioning restrictions, don't copy
* propagate them in such cases.
*/
if (max_polygons > 1 && val.file == ATTR &&
(has_dst_aligned_region_restriction(devinfo, inst, dst_type) ||
instruction_requires_packed_data(inst) ||
(inst->is_3src(compiler) && arg == 2) ||
def->dst.type != inst->src[arg].type))
return false;
/* Fold the copy into the instruction consuming it. */
inst->src[arg].file = val.file;
inst->src[arg].nr = val.nr;
inst->src[arg].subnr = val.subnr;
inst->src[arg].offset = val.offset;
/* Compose the strides of both regions. */
if (val.file == FIXED_GRF) {
if (inst->src[arg].stride) {
const unsigned orig_width = 1 << val.width;
const unsigned reg_width =
REG_SIZE / (brw_type_size_bytes(inst->src[arg].type) *
inst->src[arg].stride);
inst->src[arg].width = cvt(MIN2(orig_width, reg_width)) - 1;
inst->src[arg].hstride = cvt(inst->src[arg].stride);
inst->src[arg].vstride = inst->src[arg].hstride + inst->src[arg].width;
} else {
inst->src[arg].vstride = inst->src[arg].hstride =
inst->src[arg].width = 0;
}
inst->src[arg].stride = 1;
/* Hopefully no Align16 around here... */
assert(val.swizzle == BRW_SWIZZLE_XYZW);
inst->src[arg].swizzle = val.swizzle;
} else {
inst->src[arg].stride *= val.stride;
}
/* Handle NoMask cases where the def replicates a small scalar to a number
* of channels, but the use is a lower SIMD width but larger type, so each
* invocation reads multiple channels worth of data, e.g.
*
* mov(16) vgrf1:UW, u0<0>:UW NoMask
* mov(8) vgrf2:UD, vgrf1:UD NoMask group0
*
* In this case, we should just use the scalar's type.
*/
if (val.stride == 0 &&
inst->opcode == BRW_OPCODE_MOV &&
inst->force_writemask_all && def->force_writemask_all &&
inst->exec_size < def->exec_size &&
(inst->exec_size * brw_type_size_bytes(inst->src[arg].type) ==
def->exec_size * brw_type_size_bytes(val.type))) {
inst->src[arg].type = val.type;
inst->dst.type = val.type;
inst->exec_size = def->exec_size;
}
if (has_source_modifiers) {
if (def->dst.type != inst->src[arg].type) {
/* We are propagating source modifiers from a MOV with a different
* type. If we got here, then we can just change the source and
* destination types of the instruction and keep going.
*/
for (int i = 0; i < inst->sources; i++) {
inst->src[i].type = def->dst.type;
}
inst->dst.type = def->dst.type;
}
if (!inst->src[arg].abs) {
inst->src[arg].abs = val.abs;
inst->src[arg].negate ^= val.negate;
}
}
return true;
}
static bool
try_constant_propagate_def(fs_inst *def, brw_reg val, fs_inst *inst, int arg)
{
/* Bail if inst is reading more than a single vector component of entry */
if (inst->size_read(arg) > def->dst.component_size(inst->exec_size))
return false;
return try_constant_propagate_value(val, def->dst.type, inst, arg);
}
/**
* Handle cases like UW subreads of a UD immediate, with an offset.
*/
static brw_reg
extract_imm(brw_reg val, brw_reg_type type, unsigned offset)
{
assert(val.file == IMM);
const unsigned bitsize = brw_type_size_bits(type);
if (offset == 0 || bitsize == brw_type_size_bits(val.type))
return val;
assert(bitsize < brw_type_size_bits(val.type));
val.u64 = (val.u64 >> (bitsize * offset)) & ((1ull << bitsize) - 1);
return val;
}
static brw_reg
find_value_for_offset(fs_inst *def, const brw_reg &src, unsigned src_size)
{
brw_reg val;
switch (def->opcode) {
case BRW_OPCODE_MOV:
intel/brw: Copy prop from raw integer moves with mismatched types The specific pattern from the unit test was observed in ray tracing trampoline shaders. v2: Refactor the is_raw_move tests out to a utility function. Suggested by Ken. v3: Fix a regression caused by being too picky about source modifiers. This was introduced somewhere between when I did initial shader-db runs an v2. v4: Fix typo in comment. Noticed by Caio. shader-db: All Intel platforms had similar results. (Meteor Lake shown) total instructions in shared programs: 19734086 -> 19733997 (<.01%) instructions in affected programs: 135388 -> 135299 (-0.07%) helped: 76 / HURT: 2 total cycles in shared programs: 916290451 -> 916264968 (<.01%) cycles in affected programs: 41046002 -> 41020519 (-0.06%) helped: 32 / HURT: 29 fossil-db: Meteor Lake, DG2, and Skylake had similar results. (Meteor Lake shown) Totals: Instrs: 151531355 -> 151513669 (-0.01%); split: -0.01%, +0.00% Cycle count: 17209372399 -> 17208178205 (-0.01%); split: -0.01%, +0.00% Max live registers: 32016490 -> 32016493 (+0.00%) Totals from 17361 (2.75% of 630198) affected shaders: Instrs: 2642048 -> 2624362 (-0.67%); split: -0.67%, +0.00% Cycle count: 79803066 -> 78608872 (-1.50%); split: -1.75%, +0.25% Max live registers: 421668 -> 421671 (+0.00%) Tiger Lake and Ice Lake had similar results. (Tiger Lake shown) Totals: Instrs: 149995644 -> 149977326 (-0.01%); split: -0.01%, +0.00% Cycle count: 15567293770 -> 15566524840 (-0.00%); split: -0.02%, +0.01% Spill count: 61241 -> 61238 (-0.00%) Fill count: 107304 -> 107301 (-0.00%) Max live registers: 31993109 -> 31993112 (+0.00%) Totals from 17813 (2.83% of 629912) affected shaders: Instrs: 3738236 -> 3719918 (-0.49%); split: -0.49%, +0.00% Cycle count: 4251157049 -> 4250388119 (-0.02%); split: -0.06%, +0.04% Spill count: 28268 -> 28265 (-0.01%) Fill count: 50377 -> 50374 (-0.01%) Max live registers: 470648 -> 470651 (+0.00%) Reviewed-by: Kenneth Graunke <kenneth@whitecape.org> Reviewed-by: Caio Oliveira <caio.oliveira@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30251>
2024-07-16 16:04:38 -07:00
/* is_raw_move also rejects source modifiers, but copy propagation
* can handle that if the tyeps are the same.
*/
if ((def->dst.type == def->src[0].type || def->is_raw_move()) &&
def->src[0].stride <= 1) {
val = def->src[0];
unsigned rel_offset = src.offset - def->dst.offset;
if (val.stride == 0)
rel_offset %= brw_type_size_bytes(def->dst.type);
if (val.file == IMM)
val = extract_imm(val, src.type, rel_offset);
else
val = byte_offset(def->src[0], rel_offset);
}
break;
case SHADER_OPCODE_LOAD_PAYLOAD: {
unsigned offset = 0;
for (int i = def->header_size; i < def->sources; i++) {
const unsigned splat = def->src[i].stride == 0 ? def->exec_size : 1;
if (offset == src.offset) {
if (def->dst.type == def->src[i].type &&
def->src[i].stride <= 1 &&
def->src[i].component_size(def->exec_size) * splat == src_size)
val = def->src[i];
break;
}
offset += def->exec_size * brw_type_size_bytes(def->src[i].type);
}
break;
}
default:
break;
}
return val;
}
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_safe(block, fs_inst, inst, s.cfg) {
/* Try propagating into this instruction. */
bool constant_progress = false;
for (int i = inst->sources - 1; i >= 0; i--) {
fs_inst *def = defs.get(inst->src[i]);
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)) {
source_progress =
try_copy_propagate_def(s.compiler, s.alloc, def, def->src[0],
inst, i, s.max_polygons);
if (source_progress) {
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;
}
}
brw_reg val =
find_value_for_offset(def, inst->src[i], inst->size_read(i));
if (val.file == IMM) {
if (try_constant_propagate_def(def, val, inst, i)) {
source_progress = true;
constant_progress = true;
}
} else if (val.file == VGRF ||
val.file == ATTR || val.file == UNIFORM ||
(val.file == FIXED_GRF && val.is_contiguous())) {
source_progress =
try_copy_propagate_def(s.compiler, s.alloc, def, val, inst, i,
s.max_polygons);
}
if (source_progress) {
progress = true;
++uses_deleted[def->dst.nr];
/* We can copy propagate through an instruction like
*
* mov.nz.f0.0(8) %2:D, -%78:D
*
* but deleting the instruction may alter the program.
*/
if (def->conditional_mod == BRW_CONDITIONAL_NONE &&
defs.get_use_count(def->dst) == uses_deleted[def->dst.nr]) {
def->remove(defs.get_block(def->dst), true);
}
}
}
if (constant_progress) {
commute_immediates(inst);
brw_constant_fold_instruction(s.compiler->devinfo, inst);
}
}
if (progress) {
s.cfg->adjust_block_ips();
s.invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW |
DEPENDENCY_INSTRUCTION_DETAIL);
}
delete [] uses_deleted;
return progress;
}