freedreno/ir3: add generic get_barycentric()
[mesa.git] / src / freedreno / ir3 / ir3_nir_move_varying_inputs.c
1 /*
2 * Copyright © 2019 Red Hat
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "ir3_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 /**
28 * This pass moves varying fetches (and the instructions they depend on
29 * into the start block.
30 *
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.
36 *
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.
39 */
40
41
42 typedef struct {
43 nir_shader *shader;
44 nir_block *start_block;
45 } state;
46
47 static void move_instruction_to_start_block(state *state, nir_instr *instr);
48
49 static bool
50 move_src(nir_src *src, void *state)
51 {
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);
55 return true;
56 }
57
58 static void
59 move_instruction_to_start_block(state *state, nir_instr *instr)
60 {
61 /* nothing to do if the instruction is already in the start block */
62 if (instr->block == state->start_block)
63 return;
64
65 /* first move (recursively) all src's to ensure they appear before
66 * load*_input that we are trying to move:
67 */
68 nir_foreach_src(instr, move_src, state);
69
70 /* and then move the instruction itself:
71 */
72 exec_node_remove(&instr->node);
73 exec_list_push_tail(&state->start_block->instr_list, &instr->node);
74 instr->block = state->start_block;
75 }
76
77 static bool
78 move_varying_inputs_block(state *state, nir_block *block)
79 {
80 bool progress = false;
81
82 nir_foreach_instr_safe (instr, block) {
83 if (instr->type != nir_instr_type_intrinsic)
84 continue;
85
86 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
87
88 switch (intr->intrinsic) {
89 case nir_intrinsic_load_interpolated_input:
90 case nir_intrinsic_load_input:
91 /* TODO any others to handle? */
92 break;
93 default:
94 continue;
95 }
96
97 debug_assert(intr->dest.is_ssa);
98
99 move_instruction_to_start_block(state, instr);
100
101 progress = true;
102 }
103
104 return progress;
105 }
106
107 bool
108 ir3_nir_move_varying_inputs(nir_shader *shader)
109 {
110 bool progress = false;
111
112 debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
113
114 nir_foreach_function (function, shader) {
115 state state;
116
117 if (!function->impl)
118 continue;
119
120 state.shader = shader;
121 state.start_block = nir_start_block(function->impl);
122
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)
127 continue;
128 progress |= move_varying_inputs_block(&state, block);
129 }
130
131 if (progress) {
132 nir_metadata_preserve(function->impl,
133 nir_metadata_block_index | nir_metadata_dominance);
134 }
135 }
136
137 return progress;
138 }