2 * Copyright © 2019 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "compiler/nir/nir_builder.h"
28 * This pass moves varying fetches (and the instructions they depend on
29 * into the start block.
31 * We need to set the (ei) "end input" flag on the last varying fetch.
32 * And we want to ensure that all threads execute the instruction that
33 * sets (ei). The easiest way to ensure this is to move all varying
34 * fetches into the start block. Which is something we used to get for
35 * free by using lower_all_io_to_temps=true.
37 * This may come at the cost of additional register usage. OTOH setting
38 * the (ei) flag earlier probably frees up more VS to run.
44 nir_block
*start_block
;
47 static void move_instruction_to_start_block(state
*state
, nir_instr
*instr
);
50 move_src(nir_src
*src
, void *state
)
52 /* At this point we shouldn't have any non-ssa src: */
53 debug_assert(src
->is_ssa
);
54 move_instruction_to_start_block(state
, src
->ssa
->parent_instr
);
59 move_instruction_to_start_block(state
*state
, nir_instr
*instr
)
61 /* nothing to do if the instruction is already in the start block */
62 if (instr
->block
== state
->start_block
)
65 /* first move (recursively) all src's to ensure they appear before
66 * load*_input that we are trying to move:
68 nir_foreach_src(instr
, move_src
, state
);
70 /* and then move the instruction itself:
72 exec_node_remove(&instr
->node
);
73 exec_list_push_tail(&state
->start_block
->instr_list
, &instr
->node
);
74 instr
->block
= state
->start_block
;
78 move_varying_inputs_block(state
*state
, nir_block
*block
)
80 bool progress
= false;
82 nir_foreach_instr_safe (instr
, block
) {
83 if (instr
->type
!= nir_instr_type_intrinsic
)
86 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
88 switch (intr
->intrinsic
) {
89 case nir_intrinsic_load_interpolated_input
:
90 case nir_intrinsic_load_input
:
91 /* TODO any others to handle? */
97 debug_assert(intr
->dest
.is_ssa
);
99 move_instruction_to_start_block(state
, instr
);
108 ir3_nir_move_varying_inputs(nir_shader
*shader
)
110 bool progress
= false;
112 debug_assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
114 nir_foreach_function (function
, shader
) {
120 state
.shader
= shader
;
121 state
.start_block
= nir_start_block(function
->impl
);
123 bool progress
= false;
124 nir_foreach_block (block
, function
->impl
) {
125 /* don't need to move anything that is already in the first block */
126 if (block
== state
.start_block
)
128 progress
|= move_varying_inputs_block(&state
, block
);
132 nir_metadata_preserve(function
->impl
,
133 nir_metadata_block_index
| nir_metadata_dominance
);