1 /*
2 * Copyright © 2016 Intel Corporation
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25
26 static void
add_src(nir_src * src,struct set * invariants)27 add_src(nir_src *src, struct set *invariants)
28 {
29 _mesa_set_add(invariants, src->ssa);
30 }
31
32 static bool
add_src_cb(nir_src * src,void * state)33 add_src_cb(nir_src *src, void *state)
34 {
35 add_src(src, state);
36 return true;
37 }
38
39 static bool
def_is_invariant(nir_def * def,struct set * invariants)40 def_is_invariant(nir_def *def, struct set *invariants)
41 {
42 return _mesa_set_search(invariants, def);
43 }
44
45 static void
add_cf_node(nir_cf_node * cf,struct set * invariants)46 add_cf_node(nir_cf_node *cf, struct set *invariants)
47 {
48 if (cf->type == nir_cf_node_if) {
49 nir_if *if_stmt = nir_cf_node_as_if(cf);
50 add_src(&if_stmt->condition, invariants);
51 }
52
53 if (cf->parent)
54 add_cf_node(cf->parent, invariants);
55 }
56
57 static void
add_var(nir_variable * var,struct set * invariants)58 add_var(nir_variable *var, struct set *invariants)
59 {
60 /* Because we pass the result of nir_intrinsic_get_var directly to this
61 * function, it's possible for var to be NULL if, for instance, there's a
62 * cast somewhere in the chain.
63 */
64 if (var != NULL)
65 _mesa_set_add(invariants, var);
66 }
67
68 static bool
var_is_invariant(nir_variable * var,struct set * invariants)69 var_is_invariant(nir_variable *var, struct set *invariants)
70 {
71 /* Because we pass the result of nir_intrinsic_get_var directly to this
72 * function, it's possible for var to be NULL if, for instance, there's a
73 * cast somewhere in the chain.
74 */
75 return var && (var->data.invariant || _mesa_set_search(invariants, var));
76 }
77
78 static void
propagate_invariant_instr(nir_instr * instr,struct set * invariants)79 propagate_invariant_instr(nir_instr *instr, struct set *invariants)
80 {
81 switch (instr->type) {
82 case nir_instr_type_alu: {
83 nir_alu_instr *alu = nir_instr_as_alu(instr);
84 if (!def_is_invariant(&alu->def, invariants))
85 break;
86
87 alu->exact = true;
88 nir_foreach_src(instr, add_src_cb, invariants);
89 break;
90 }
91
92 case nir_instr_type_tex: {
93 nir_tex_instr *tex = nir_instr_as_tex(instr);
94 if (def_is_invariant(&tex->def, invariants))
95 nir_foreach_src(instr, add_src_cb, invariants);
96 break;
97 }
98
99 case nir_instr_type_intrinsic: {
100 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
101 switch (intrin->intrinsic) {
102 case nir_intrinsic_copy_deref:
103 /* If the destination is invariant then so is the source */
104 if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
105 add_var(nir_intrinsic_get_var(intrin, 1), invariants);
106 break;
107
108 case nir_intrinsic_load_deref:
109 if (def_is_invariant(&intrin->def, invariants))
110 add_var(nir_intrinsic_get_var(intrin, 0), invariants);
111 break;
112
113 case nir_intrinsic_store_deref:
114 if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
115 add_src(&intrin->src[1], invariants);
116 break;
117
118 default:
119 /* Nothing to do */
120 break;
121 }
122 FALLTHROUGH;
123 }
124
125 case nir_instr_type_deref:
126 case nir_instr_type_jump:
127 case nir_instr_type_undef:
128 case nir_instr_type_load_const:
129 case nir_instr_type_debug_info:
130 break; /* Nothing to do */
131
132 case nir_instr_type_phi: {
133 nir_phi_instr *phi = nir_instr_as_phi(instr);
134 if (!def_is_invariant(&phi->def, invariants))
135 break;
136
137 nir_foreach_phi_src(src, phi) {
138 add_src(&src->src, invariants);
139 add_cf_node(&src->pred->cf_node, invariants);
140 }
141 break;
142 }
143
144 case nir_instr_type_call:
145 unreachable("This pass must be run after function inlining");
146
147 case nir_instr_type_parallel_copy:
148 default:
149 unreachable("Cannot have this instruction type");
150 }
151 }
152
153 static bool
propagate_invariant_impl(nir_function_impl * impl,struct set * invariants)154 propagate_invariant_impl(nir_function_impl *impl, struct set *invariants)
155 {
156 bool progress = false;
157
158 while (true) {
159 uint32_t prev_entries = invariants->entries;
160
161 nir_foreach_block_reverse(block, impl) {
162 nir_foreach_instr_reverse(instr, block)
163 propagate_invariant_instr(instr, invariants);
164 }
165
166 /* Keep running until we make no more progress. */
167 if (invariants->entries > prev_entries) {
168 progress = true;
169 continue;
170 } else {
171 break;
172 }
173 }
174
175 if (progress) {
176 nir_metadata_preserve(impl, nir_metadata_control_flow |
177 nir_metadata_live_defs);
178 } else {
179 nir_metadata_preserve(impl, nir_metadata_all);
180 }
181
182 return progress;
183 }
184
185 /* If invariant_prim=true, this pass considers all geometry-affecting
186 * outputs as invariant. Doing this works around a common class of application
187 * bugs appearing as flickering.
188 */
189 bool
nir_propagate_invariant(nir_shader * shader,bool invariant_prim)190 nir_propagate_invariant(nir_shader *shader, bool invariant_prim)
191 {
192 /* Hash set of invariant things */
193 struct set *invariants = _mesa_pointer_set_create(NULL);
194
195 if (shader->info.stage != MESA_SHADER_FRAGMENT && invariant_prim) {
196 nir_foreach_shader_out_variable(var, shader) {
197 switch (var->data.location) {
198 case VARYING_SLOT_POS:
199 case VARYING_SLOT_PSIZ:
200 case VARYING_SLOT_CLIP_DIST0:
201 case VARYING_SLOT_CLIP_DIST1:
202 case VARYING_SLOT_CULL_DIST0:
203 case VARYING_SLOT_CULL_DIST1:
204 case VARYING_SLOT_TESS_LEVEL_OUTER:
205 case VARYING_SLOT_TESS_LEVEL_INNER:
206 if (!var->data.invariant)
207 _mesa_set_add(invariants, var);
208 break;
209 default:
210 break;
211 }
212 }
213 }
214
215 bool progress = false;
216 nir_foreach_function_impl(impl, shader) {
217 if (propagate_invariant_impl(impl, invariants))
218 progress = true;
219 }
220
221 _mesa_set_destroy(invariants, NULL);
222
223 return progress;
224 }
225