1 /*
2 * Copyright © 2019 Valve 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25
26 /* This pass optimizes GL access qualifiers. So far it does three things:
27 *
28 * - Infer readonly when it's missing.
29 * - Infer writeonly when it's missing.
30 * - Infer ACCESS_CAN_REORDER when the following are true:
31 * - Either there are no writes, or ACCESS_NON_WRITEABLE is set. In either
32 * case there are no writes to the underlying memory.
33 * - ACCESS_VOLATILE is not set.
34 *
35 * If these conditions are true, then image and buffer reads may be treated as
36 * if they were uniform buffer reads, i.e. they may be arbitrarily moved,
37 * combined, rematerialized etc.
38 */
39
40 struct access_state {
41 nir_shader *shader;
42
43 struct set *vars_written;
44 struct set *vars_read;
45 bool images_written;
46 bool buffers_written;
47 bool images_read;
48 bool buffers_read;
49 };
50
51 static void
gather_buffer_access(struct access_state * state,nir_def * def,bool read,bool write)52 gather_buffer_access(struct access_state *state, nir_def *def, bool read, bool write)
53 {
54 state->buffers_read |= read;
55 state->buffers_written |= write;
56
57 if (!def)
58 return;
59
60 const nir_variable *var = nir_get_binding_variable(
61 state->shader, nir_chase_binding(nir_src_for_ssa(def)));
62 if (var) {
63 if (read)
64 _mesa_set_add(state->vars_read, var);
65 if (write)
66 _mesa_set_add(state->vars_written, var);
67 } else {
68 nir_foreach_variable_with_modes(possible_var, state->shader, nir_var_mem_ssbo) {
69 if (read)
70 _mesa_set_add(state->vars_read, possible_var);
71 if (write)
72 _mesa_set_add(state->vars_written, possible_var);
73 }
74 }
75 }
76
77 static void
gather_intrinsic(struct access_state * state,nir_intrinsic_instr * instr)78 gather_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
79 {
80 const nir_variable *var;
81 bool read, write;
82 switch (instr->intrinsic) {
83 case nir_intrinsic_image_deref_load:
84 case nir_intrinsic_image_deref_store:
85 case nir_intrinsic_image_deref_sparse_load:
86 case nir_intrinsic_image_deref_atomic:
87 case nir_intrinsic_image_deref_atomic_swap:
88 case nir_intrinsic_image_deref_samples_identical:
89 var = nir_intrinsic_get_var(instr, 0);
90 read = instr->intrinsic != nir_intrinsic_image_deref_store;
91 write = instr->intrinsic != nir_intrinsic_image_deref_load &&
92 instr->intrinsic != nir_intrinsic_image_deref_sparse_load;
93
94 /* In OpenGL, buffer images use normal buffer objects, whereas other
95 * image types use textures which cannot alias with buffer objects.
96 * Therefore we have to group buffer samplers together with SSBO's.
97 */
98 if (glsl_get_sampler_dim(glsl_without_array(var->type)) ==
99 GLSL_SAMPLER_DIM_BUF) {
100 state->buffers_read |= read;
101 state->buffers_written |= write;
102 } else {
103 state->images_read |= read;
104 state->images_written |= write;
105 }
106
107 if ((var->data.mode == nir_var_uniform ||
108 var->data.mode == nir_var_image) &&
109 read)
110 _mesa_set_add(state->vars_read, var);
111 if ((var->data.mode == nir_var_uniform ||
112 var->data.mode == nir_var_image) &&
113 write)
114 _mesa_set_add(state->vars_written, var);
115 break;
116
117 case nir_intrinsic_bindless_image_load:
118 case nir_intrinsic_bindless_image_store:
119 case nir_intrinsic_bindless_image_sparse_load:
120 case nir_intrinsic_bindless_image_atomic:
121 case nir_intrinsic_bindless_image_atomic_swap:
122 case nir_intrinsic_bindless_image_samples_identical:
123 read = instr->intrinsic != nir_intrinsic_bindless_image_store;
124 write = instr->intrinsic != nir_intrinsic_bindless_image_load &&
125 instr->intrinsic != nir_intrinsic_bindless_image_sparse_load;
126
127 if (nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF) {
128 state->buffers_read |= read;
129 state->buffers_written |= write;
130 } else {
131 state->images_read |= read;
132 state->images_written |= write;
133 }
134 break;
135
136 case nir_intrinsic_load_deref:
137 case nir_intrinsic_store_deref:
138 case nir_intrinsic_deref_atomic:
139 case nir_intrinsic_deref_atomic_swap: {
140 nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
141 if (!nir_deref_mode_may_be(deref, nir_var_mem_ssbo | nir_var_mem_global))
142 break;
143
144 bool ssbo = nir_deref_mode_is(deref, nir_var_mem_ssbo);
145 gather_buffer_access(state, ssbo ? instr->src[0].ssa : NULL,
146 instr->intrinsic != nir_intrinsic_store_deref,
147 instr->intrinsic != nir_intrinsic_load_deref);
148 break;
149 }
150
151 default:
152 break;
153 }
154 }
155
156 static bool
process_variable(struct access_state * state,nir_variable * var)157 process_variable(struct access_state *state, nir_variable *var)
158 {
159 const struct glsl_type *type = glsl_without_array(var->type);
160 if (var->data.mode != nir_var_mem_ssbo &&
161 !(var->data.mode == nir_var_uniform && glsl_type_is_image(type)) &&
162 var->data.mode != nir_var_image)
163 return false;
164
165 /* Ignore variables we've already marked */
166 if (var->data.access & ACCESS_CAN_REORDER)
167 return false;
168
169 unsigned access = var->data.access;
170 bool is_buffer = var->data.mode == nir_var_mem_ssbo ||
171 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF;
172
173 if (!(access & ACCESS_NON_WRITEABLE)) {
174 if (is_buffer ? !state->buffers_written : !state->images_written)
175 access |= ACCESS_NON_WRITEABLE;
176 else if ((access & ACCESS_RESTRICT) && !_mesa_set_search(state->vars_written, var))
177 access |= ACCESS_NON_WRITEABLE;
178 }
179
180 if (!(access & ACCESS_NON_READABLE)) {
181 if (is_buffer ? !state->buffers_read : !state->images_read)
182 access |= ACCESS_NON_READABLE;
183 else if ((access & ACCESS_RESTRICT) && !_mesa_set_search(state->vars_read, var))
184 access |= ACCESS_NON_READABLE;
185 }
186
187 bool changed = var->data.access != access;
188 var->data.access = access;
189 return changed;
190 }
191
192 static bool
update_access(struct access_state * state,nir_intrinsic_instr * instr,bool is_buffer,bool is_global)193 update_access(struct access_state *state, nir_intrinsic_instr *instr, bool is_buffer, bool is_global)
194 {
195 enum gl_access_qualifier access = nir_intrinsic_access(instr);
196
197 bool is_memory_readonly = access & ACCESS_NON_WRITEABLE;
198 bool is_memory_writeonly = access & ACCESS_NON_READABLE;
199
200 if (instr->intrinsic != nir_intrinsic_bindless_image_load &&
201 instr->intrinsic != nir_intrinsic_bindless_image_store &&
202 instr->intrinsic != nir_intrinsic_bindless_image_sparse_load &&
203 !is_global) {
204 const nir_variable *var = nir_get_binding_variable(
205 state->shader, nir_chase_binding(instr->src[0]));
206 is_memory_readonly |= var && (var->data.access & ACCESS_NON_WRITEABLE);
207 is_memory_writeonly |= var && (var->data.access & ACCESS_NON_READABLE);
208 }
209
210 if (is_global) {
211 is_memory_readonly |= !state->buffers_written && !state->images_written;
212 is_memory_writeonly |= !state->buffers_read && !state->images_read;
213 } else {
214 is_memory_readonly |= is_buffer ? !state->buffers_written : !state->images_written;
215 is_memory_writeonly |= is_buffer ? !state->buffers_read : !state->images_read;
216 }
217
218 if (is_memory_readonly)
219 access |= ACCESS_NON_WRITEABLE;
220 if (is_memory_writeonly)
221 access |= ACCESS_NON_READABLE;
222 if (!(access & ACCESS_VOLATILE) && is_memory_readonly)
223 access |= ACCESS_CAN_REORDER;
224
225 bool progress = nir_intrinsic_access(instr) != access;
226 nir_intrinsic_set_access(instr, access);
227 return progress;
228 }
229
230 static bool
process_intrinsic(struct access_state * state,nir_intrinsic_instr * instr)231 process_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
232 {
233 switch (instr->intrinsic) {
234 case nir_intrinsic_bindless_image_load:
235 case nir_intrinsic_bindless_image_store:
236 case nir_intrinsic_bindless_image_sparse_load:
237 return update_access(state, instr, nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF,
238 false);
239
240 case nir_intrinsic_load_deref:
241 case nir_intrinsic_store_deref: {
242 if (nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_global))
243 return update_access(state, instr, false, true);
244 else if (nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_ssbo))
245 return update_access(state, instr, true, false);
246 else
247 return false;
248 }
249
250 case nir_intrinsic_image_deref_load:
251 case nir_intrinsic_image_deref_store:
252 case nir_intrinsic_image_deref_sparse_load: {
253 nir_variable *var = nir_intrinsic_get_var(instr, 0);
254
255 bool is_buffer =
256 glsl_get_sampler_dim(glsl_without_array(var->type)) == GLSL_SAMPLER_DIM_BUF;
257
258 return update_access(state, instr, is_buffer, false);
259 }
260
261 default:
262 return false;
263 }
264 }
265
266 static bool
opt_access_impl(struct access_state * state,nir_function_impl * impl)267 opt_access_impl(struct access_state *state,
268 nir_function_impl *impl)
269 {
270 bool progress = false;
271
272 nir_foreach_block(block, impl) {
273 nir_foreach_instr(instr, block) {
274 if (instr->type == nir_instr_type_intrinsic)
275 progress |= process_intrinsic(state,
276 nir_instr_as_intrinsic(instr));
277 }
278 }
279
280 if (progress) {
281 nir_metadata_preserve(impl,
282 nir_metadata_control_flow |
283 nir_metadata_live_defs |
284 nir_metadata_loop_analysis);
285 }
286
287 return progress;
288 }
289
290 bool
nir_opt_access(nir_shader * shader,const nir_opt_access_options * options)291 nir_opt_access(nir_shader *shader, const nir_opt_access_options *options)
292 {
293 struct access_state state = {
294 .shader = shader,
295 .vars_written = _mesa_pointer_set_create(NULL),
296 .vars_read = _mesa_pointer_set_create(NULL),
297 };
298
299 bool var_progress = false;
300 bool progress = false;
301
302 nir_foreach_function_impl(impl, shader) {
303 nir_foreach_block(block, impl) {
304 nir_foreach_instr(instr, block) {
305 if (instr->type == nir_instr_type_intrinsic)
306 gather_intrinsic(&state, nir_instr_as_intrinsic(instr));
307 }
308 }
309 }
310
311 /* In Vulkan, buffers and images can alias. */
312 if (options->is_vulkan) {
313 state.buffers_written |= state.images_written;
314 state.images_written |= state.buffers_written;
315 state.buffers_read |= state.images_read;
316 state.images_read |= state.buffers_read;
317 }
318
319 nir_foreach_variable_with_modes(var, shader, nir_var_uniform | nir_var_mem_ubo | nir_var_mem_ssbo | nir_var_image)
320 var_progress |= process_variable(&state, var);
321
322 nir_foreach_function_impl(impl, shader) {
323 progress |= opt_access_impl(&state, impl);
324
325 /* If we make a change to the uniforms, update all the impls. */
326 if (var_progress) {
327 nir_metadata_preserve(impl,
328 nir_metadata_control_flow |
329 nir_metadata_live_defs |
330 nir_metadata_loop_analysis);
331 }
332 }
333
334 progress |= var_progress;
335
336 _mesa_set_destroy(state.vars_read, NULL);
337 _mesa_set_destroy(state.vars_written, NULL);
338 return progress;
339 }
340