glsl/shader_cache: handle SPIR-V shaders
[mesa.git] / src / compiler / glsl / gl_nir_opt_access.c
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/nir.h"
25 #include "gl_nir.h"
26
27 /* This pass optimizes GL access qualifiers. So far it does two things:
28 *
29 * - Infer readonly when it's missing.
30 * - Infer ACCESS_CAN_REORDER when the following are true:
31 * - Either there are no writes, or ACCESS_NON_WRITEABLE and ACCESS_RESTRICT
32 * are both set. In either case there are no writes to the underlying
33 * memory.
34 * - If ACCESS_COHERENT is set, then there must be no memory barriers
35 * involving the access. Coherent accesses may return different results
36 * before and after barriers.
37 * - ACCESS_VOLATILE is not set.
38 *
39 * If these conditions are true, then image and buffer reads may be treated as
40 * if they were uniform buffer reads, i.e. they may be arbitrarily moved,
41 * combined, rematerialized etc.
42 */
43
44 struct access_state {
45 struct set *vars_written;
46 bool images_written;
47 bool buffers_written;
48 bool image_barriers;
49 bool buffer_barriers;
50 };
51
52 static void
53 gather_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
54 {
55 nir_variable *var;
56 switch (instr->intrinsic) {
57 case nir_intrinsic_image_deref_store:
58 case nir_intrinsic_image_deref_atomic_add:
59 case nir_intrinsic_image_deref_atomic_min:
60 case nir_intrinsic_image_deref_atomic_max:
61 case nir_intrinsic_image_deref_atomic_and:
62 case nir_intrinsic_image_deref_atomic_or:
63 case nir_intrinsic_image_deref_atomic_xor:
64 case nir_intrinsic_image_deref_atomic_exchange:
65 case nir_intrinsic_image_deref_atomic_comp_swap:
66 case nir_intrinsic_image_deref_atomic_fadd:
67 var = nir_intrinsic_get_var(instr, 0);
68
69 /* In OpenGL, buffer images use normal buffer objects, whereas other
70 * image types use textures which cannot alias with buffer objects.
71 * Therefore we have to group buffer samplers together with SSBO's.
72 */
73 if (glsl_get_sampler_dim(glsl_without_array(var->type)) ==
74 GLSL_SAMPLER_DIM_BUF)
75 state->buffers_written = true;
76 else
77 state->images_written = true;
78
79 if (var->data.mode == nir_var_uniform)
80 _mesa_set_add(state->vars_written, var);
81 break;
82
83 case nir_intrinsic_bindless_image_store:
84 case nir_intrinsic_bindless_image_atomic_add:
85 case nir_intrinsic_bindless_image_atomic_min:
86 case nir_intrinsic_bindless_image_atomic_max:
87 case nir_intrinsic_bindless_image_atomic_and:
88 case nir_intrinsic_bindless_image_atomic_or:
89 case nir_intrinsic_bindless_image_atomic_xor:
90 case nir_intrinsic_bindless_image_atomic_exchange:
91 case nir_intrinsic_bindless_image_atomic_comp_swap:
92 case nir_intrinsic_bindless_image_atomic_fadd:
93 if (nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF)
94 state->buffers_written = true;
95 else
96 state->images_written = true;
97 break;
98
99 case nir_intrinsic_store_deref:
100 case nir_intrinsic_deref_atomic_add:
101 case nir_intrinsic_deref_atomic_imin:
102 case nir_intrinsic_deref_atomic_umin:
103 case nir_intrinsic_deref_atomic_imax:
104 case nir_intrinsic_deref_atomic_umax:
105 case nir_intrinsic_deref_atomic_and:
106 case nir_intrinsic_deref_atomic_or:
107 case nir_intrinsic_deref_atomic_xor:
108 case nir_intrinsic_deref_atomic_exchange:
109 case nir_intrinsic_deref_atomic_comp_swap:
110 case nir_intrinsic_deref_atomic_fadd:
111 case nir_intrinsic_deref_atomic_fmin:
112 case nir_intrinsic_deref_atomic_fmax:
113 case nir_intrinsic_deref_atomic_fcomp_swap:
114 var = nir_intrinsic_get_var(instr, 0);
115 if (var->data.mode != nir_var_mem_ssbo)
116 break;
117
118 _mesa_set_add(state->vars_written, var);
119 state->buffers_written = true;
120
121 case nir_intrinsic_memory_barrier:
122 state->buffer_barriers = true;
123 state->image_barriers = true;
124 break;
125
126 case nir_intrinsic_memory_barrier_buffer:
127 state->buffer_barriers = true;
128 break;
129
130 case nir_intrinsic_memory_barrier_image:
131 state->image_barriers = true;
132 break;
133
134 default:
135 break;
136 }
137 }
138
139 static bool
140 process_variable(struct access_state *state, nir_variable *var)
141 {
142 if (var->data.mode != nir_var_mem_ssbo &&
143 !(var->data.mode == nir_var_uniform &&
144 glsl_type_is_image(var->type)))
145 return false;
146
147 /* Ignore variables we've already marked */
148 if (var->data.image.access & ACCESS_CAN_REORDER)
149 return false;
150
151 if (!(var->data.image.access & ACCESS_NON_WRITEABLE) &&
152 !_mesa_set_search(state->vars_written, var)) {
153 var->data.image.access |= ACCESS_NON_WRITEABLE;
154 return true;
155 }
156
157 return false;
158 }
159
160 static bool
161 can_reorder(struct access_state *state, enum gl_access_qualifier access,
162 bool is_buffer, bool is_ssbo)
163 {
164 bool is_any_written = is_buffer ? state->buffers_written :
165 state->images_written;
166
167 /* Can we guarantee that the underlying memory is never written? */
168 if (!is_any_written ||
169 ((access & ACCESS_NON_WRITEABLE) &&
170 (access & ACCESS_RESTRICT))) {
171 /* Note: memoryBarrierBuffer() is only guaranteed to flush buffer
172 * variables and not imageBuffer's, so we only consider the GL-level
173 * type here.
174 */
175 bool is_any_barrier = is_ssbo ?
176 state->buffer_barriers : state->image_barriers;
177
178 return (!is_any_barrier || !(access & ACCESS_COHERENT)) &&
179 !(access & ACCESS_VOLATILE);
180 }
181
182 return false;
183 }
184
185 static bool
186 process_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
187 {
188 switch (instr->intrinsic) {
189 case nir_intrinsic_bindless_image_load:
190 if (nir_intrinsic_access(instr) & ACCESS_CAN_REORDER)
191 return false;
192
193 /* We have less information about bindless intrinsics, since we can't
194 * always trace uses back to the variable. Don't try and infer if it's
195 * read-only, unless there are no image writes at all.
196 */
197 bool progress = false;
198 bool is_buffer =
199 nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF;
200
201 bool is_any_written =
202 is_buffer ? state->buffers_written : state->images_written;
203
204 if (!(nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE) &&
205 !is_any_written) {
206 progress = true;
207 nir_intrinsic_set_access(instr,
208 nir_intrinsic_access(instr) |
209 ACCESS_NON_WRITEABLE);
210 }
211
212 if (can_reorder(state, nir_intrinsic_access(instr), is_buffer, false)) {
213 progress = true;
214 nir_intrinsic_set_access(instr,
215 nir_intrinsic_access(instr) |
216 ACCESS_CAN_REORDER);
217 }
218
219 return progress;
220
221 case nir_intrinsic_load_deref:
222 case nir_intrinsic_image_deref_load: {
223 nir_variable *var = nir_intrinsic_get_var(instr, 0);
224
225 if (instr->intrinsic == nir_intrinsic_load_deref &&
226 var->data.mode != nir_var_mem_ssbo)
227 return false;
228
229 if (nir_intrinsic_access(instr) & ACCESS_CAN_REORDER)
230 return false;
231
232 bool progress = false;
233
234 /* Check if we were able to mark the whole variable non-writeable */
235 if (!(nir_intrinsic_access(instr) & ACCESS_NON_WRITEABLE) &&
236 var->data.image.access & ACCESS_NON_WRITEABLE) {
237 progress = true;
238 nir_intrinsic_set_access(instr,
239 nir_intrinsic_access(instr) |
240 ACCESS_NON_WRITEABLE);
241 }
242
243 bool is_ssbo = var->data.mode == nir_var_mem_ssbo;
244
245 bool is_buffer = is_ssbo ||
246 glsl_get_sampler_dim(glsl_without_array(var->type)) == GLSL_SAMPLER_DIM_BUF;
247
248 if (can_reorder(state, nir_intrinsic_access(instr), is_buffer, is_ssbo)) {
249 progress = true;
250 nir_intrinsic_set_access(instr,
251 nir_intrinsic_access(instr) |
252 ACCESS_CAN_REORDER);
253 }
254
255 return progress;
256 }
257
258 default:
259 return false;
260 }
261 }
262
263 static bool
264 opt_access_impl(struct access_state *state,
265 nir_function_impl *impl)
266 {
267 bool progress = false;
268
269 nir_foreach_block(block, impl) {
270 nir_foreach_instr(instr, block) {
271 if (instr->type == nir_instr_type_intrinsic)
272 progress |= process_intrinsic(state,
273 nir_instr_as_intrinsic(instr));
274 }
275 }
276
277 if (progress) {
278 nir_metadata_preserve(impl,
279 nir_metadata_block_index |
280 nir_metadata_dominance |
281 nir_metadata_live_ssa_defs |
282 nir_metadata_loop_analysis);
283 }
284
285
286 return progress;
287 }
288
289 bool
290 gl_nir_opt_access(nir_shader *shader)
291 {
292 struct access_state state = {
293 .vars_written = _mesa_pointer_set_create(NULL),
294 };
295
296 bool var_progress = false;
297 bool progress = false;
298
299 nir_foreach_function(func, shader) {
300 if (func->impl) {
301 nir_foreach_block(block, func->impl) {
302 nir_foreach_instr(instr, block) {
303 if (instr->type == nir_instr_type_intrinsic)
304 gather_intrinsic(&state, nir_instr_as_intrinsic(instr));
305 }
306 }
307 }
308 }
309
310 nir_foreach_variable(var, &shader->uniforms)
311 var_progress |= process_variable(&state, var);
312
313 nir_foreach_function(func, shader) {
314 if (func->impl) {
315 progress |= opt_access_impl(&state, func->impl);
316
317 /* If we make a change to the uniforms, update all the impls. */
318 if (var_progress) {
319 nir_metadata_preserve(func->impl,
320 nir_metadata_block_index |
321 nir_metadata_dominance |
322 nir_metadata_live_ssa_defs |
323 nir_metadata_loop_analysis);
324 }
325 }
326 }
327
328 progress |= var_progress;
329
330 _mesa_set_destroy(state.vars_written, NULL);
331 return progress;
332 }
333