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    bool infer_non_readable;
43 
44    struct set *vars_written;
45    struct set *vars_read;
46    bool images_written;
47    bool buffers_written;
48    bool images_read;
49    bool buffers_read;
50 };
51 
52 static void
gather_buffer_access(struct access_state * state,nir_ssa_def * def,bool read,bool write)53 gather_buffer_access(struct access_state *state, nir_ssa_def *def, bool read, bool write)
54 {
55    state->buffers_read |= read;
56    state->buffers_written |= write;
57 
58    if (!def)
59       return;
60 
61    const nir_variable *var = nir_get_binding_variable(
62       state->shader, nir_chase_binding(nir_src_for_ssa(def)));
63    if (var) {
64       if (read)
65          _mesa_set_add(state->vars_read, var);
66       if (write)
67          _mesa_set_add(state->vars_written, var);
68    } else {
69       nir_foreach_variable_with_modes(possible_var, state->shader, nir_var_mem_ssbo) {
70          if (read)
71             _mesa_set_add(state->vars_read, possible_var);
72          if (write)
73             _mesa_set_add(state->vars_written, possible_var);
74       }
75    }
76 }
77 
78 static void
gather_intrinsic(struct access_state * state,nir_intrinsic_instr * instr)79 gather_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
80 {
81    const nir_variable *var;
82    bool read, write;
83    switch (instr->intrinsic) {
84    case nir_intrinsic_image_deref_load:
85    case nir_intrinsic_image_deref_store:
86    case nir_intrinsic_image_deref_sparse_load:
87    case nir_intrinsic_image_deref_atomic_add:
88    case nir_intrinsic_image_deref_atomic_imin:
89    case nir_intrinsic_image_deref_atomic_umin:
90    case nir_intrinsic_image_deref_atomic_imax:
91    case nir_intrinsic_image_deref_atomic_umax:
92    case nir_intrinsic_image_deref_atomic_and:
93    case nir_intrinsic_image_deref_atomic_or:
94    case nir_intrinsic_image_deref_atomic_xor:
95    case nir_intrinsic_image_deref_atomic_exchange:
96    case nir_intrinsic_image_deref_atomic_comp_swap:
97    case nir_intrinsic_image_deref_atomic_fadd:
98    case nir_intrinsic_image_deref_atomic_fmin:
99    case nir_intrinsic_image_deref_atomic_fmax:
100       var = nir_intrinsic_get_var(instr, 0);
101       read = instr->intrinsic != nir_intrinsic_image_deref_store;
102       write = instr->intrinsic != nir_intrinsic_image_deref_load &&
103               instr->intrinsic != nir_intrinsic_image_deref_sparse_load;
104 
105       /* In OpenGL, buffer images use normal buffer objects, whereas other
106        * image types use textures which cannot alias with buffer objects.
107        * Therefore we have to group buffer samplers together with SSBO's.
108        */
109       if (glsl_get_sampler_dim(glsl_without_array(var->type)) ==
110           GLSL_SAMPLER_DIM_BUF) {
111          state->buffers_read |= read;
112          state->buffers_written |= write;
113       } else {
114          state->images_read |= read;
115          state->images_written |= write;
116       }
117 
118       if (var->data.mode == nir_var_uniform && read)
119          _mesa_set_add(state->vars_read, var);
120       if (var->data.mode == nir_var_uniform && write)
121          _mesa_set_add(state->vars_written, var);
122       break;
123 
124    case nir_intrinsic_bindless_image_load:
125    case nir_intrinsic_bindless_image_store:
126    case nir_intrinsic_bindless_image_sparse_load:
127    case nir_intrinsic_bindless_image_atomic_add:
128    case nir_intrinsic_bindless_image_atomic_imin:
129    case nir_intrinsic_bindless_image_atomic_umin:
130    case nir_intrinsic_bindless_image_atomic_imax:
131    case nir_intrinsic_bindless_image_atomic_umax:
132    case nir_intrinsic_bindless_image_atomic_and:
133    case nir_intrinsic_bindless_image_atomic_or:
134    case nir_intrinsic_bindless_image_atomic_xor:
135    case nir_intrinsic_bindless_image_atomic_exchange:
136    case nir_intrinsic_bindless_image_atomic_comp_swap:
137    case nir_intrinsic_bindless_image_atomic_fadd:
138    case nir_intrinsic_bindless_image_atomic_fmin:
139    case nir_intrinsic_bindless_image_atomic_fmax:
140       read = instr->intrinsic != nir_intrinsic_bindless_image_store;
141       write = instr->intrinsic != nir_intrinsic_bindless_image_load &&
142               instr->intrinsic != nir_intrinsic_bindless_image_sparse_load;
143 
144       if (nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF) {
145          state->buffers_read |= read;
146          state->buffers_written |= write;
147       } else {
148          state->images_read |= read;
149          state->images_written |= write;
150       }
151       break;
152 
153    case nir_intrinsic_load_deref:
154    case nir_intrinsic_store_deref:
155    case nir_intrinsic_deref_atomic_add:
156    case nir_intrinsic_deref_atomic_imin:
157    case nir_intrinsic_deref_atomic_umin:
158    case nir_intrinsic_deref_atomic_imax:
159    case nir_intrinsic_deref_atomic_umax:
160    case nir_intrinsic_deref_atomic_and:
161    case nir_intrinsic_deref_atomic_or:
162    case nir_intrinsic_deref_atomic_xor:
163    case nir_intrinsic_deref_atomic_exchange:
164    case nir_intrinsic_deref_atomic_comp_swap:
165    case nir_intrinsic_deref_atomic_fadd:
166    case nir_intrinsic_deref_atomic_fmin:
167    case nir_intrinsic_deref_atomic_fmax:
168    case nir_intrinsic_deref_atomic_fcomp_swap: {
169       nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
170       if (!nir_deref_mode_may_be(deref, nir_var_mem_ssbo | nir_var_mem_global))
171          break;
172 
173       bool ssbo = nir_deref_mode_is(deref, nir_var_mem_ssbo);
174       gather_buffer_access(state, ssbo ? instr->src[0].ssa : NULL,
175                            instr->intrinsic != nir_intrinsic_store_deref,
176                            instr->intrinsic != nir_intrinsic_load_deref);
177       break;
178    }
179 
180    default:
181       break;
182    }
183 }
184 
185 static bool
process_variable(struct access_state * state,nir_variable * var)186 process_variable(struct access_state *state, nir_variable *var)
187 {
188    const struct glsl_type *type = glsl_without_array(var->type);
189    if (var->data.mode != nir_var_mem_ssbo &&
190        !(var->data.mode == nir_var_uniform && glsl_type_is_image(type)))
191       return false;
192 
193    /* Ignore variables we've already marked */
194    if (var->data.access & ACCESS_CAN_REORDER)
195       return false;
196 
197    unsigned access = var->data.access;
198    bool is_buffer = var->data.mode == nir_var_mem_ssbo ||
199                     glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF;
200 
201    if (!(access & ACCESS_NON_WRITEABLE)) {
202       if (is_buffer ? !state->buffers_written : !state->images_written)
203          access |= ACCESS_NON_WRITEABLE;
204       else if ((access & ACCESS_RESTRICT) && !_mesa_set_search(state->vars_written, var))
205          access |= ACCESS_NON_WRITEABLE;
206    }
207 
208    if (state->infer_non_readable && !(access & ACCESS_NON_READABLE)) {
209       if (is_buffer ? !state->buffers_read : !state->images_read)
210          access |= ACCESS_NON_READABLE;
211       else if ((access & ACCESS_RESTRICT) && !_mesa_set_search(state->vars_read, var))
212          access |= ACCESS_NON_READABLE;
213    }
214 
215    bool changed = var->data.access != access;
216    var->data.access = access;
217    return changed;
218 }
219 
220 static bool
update_access(struct access_state * state,nir_intrinsic_instr * instr,bool is_image,bool is_buffer)221 update_access(struct access_state *state, nir_intrinsic_instr *instr, bool is_image, bool is_buffer)
222 {
223    enum gl_access_qualifier access = nir_intrinsic_access(instr);
224 
225    bool is_memory_readonly = access & ACCESS_NON_WRITEABLE;
226    bool is_memory_writeonly = access & ACCESS_NON_READABLE;
227 
228    if (instr->intrinsic != nir_intrinsic_bindless_image_load &&
229        instr->intrinsic != nir_intrinsic_bindless_image_store &&
230        instr->intrinsic != nir_intrinsic_bindless_image_sparse_load) {
231       const nir_variable *var = nir_get_binding_variable(
232          state->shader, nir_chase_binding(instr->src[0]));
233       is_memory_readonly |= var && (var->data.access & ACCESS_NON_WRITEABLE);
234       is_memory_writeonly |= var && (var->data.access & ACCESS_NON_READABLE);
235    }
236 
237    is_memory_readonly |= is_buffer ? !state->buffers_written : !state->images_written;
238    is_memory_writeonly |= is_buffer ? !state->buffers_read : !state->images_read;
239 
240    if (is_memory_readonly)
241       access |= ACCESS_NON_WRITEABLE;
242    if (state->infer_non_readable && is_memory_writeonly)
243       access |= ACCESS_NON_READABLE;
244    if (!(access & ACCESS_VOLATILE) && is_memory_readonly)
245       access |= ACCESS_CAN_REORDER;
246 
247    bool progress = nir_intrinsic_access(instr) != access;
248    nir_intrinsic_set_access(instr, access);
249    return progress;
250 }
251 
252 static bool
process_intrinsic(struct access_state * state,nir_intrinsic_instr * instr)253 process_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
254 {
255    switch (instr->intrinsic) {
256    case nir_intrinsic_bindless_image_load:
257    case nir_intrinsic_bindless_image_store:
258    case nir_intrinsic_bindless_image_sparse_load:
259       return update_access(state, instr, true,
260                            nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF);
261 
262    case nir_intrinsic_load_deref:
263    case nir_intrinsic_store_deref: {
264       if (!nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_ssbo))
265          return false;
266 
267       return update_access(state, instr, false, true);
268    }
269 
270    case nir_intrinsic_image_deref_load:
271    case nir_intrinsic_image_deref_store:
272    case nir_intrinsic_image_deref_sparse_load: {
273       nir_variable *var = nir_intrinsic_get_var(instr, 0);
274 
275       bool is_buffer =
276          glsl_get_sampler_dim(glsl_without_array(var->type)) == GLSL_SAMPLER_DIM_BUF;
277 
278       return update_access(state, instr, true, is_buffer);
279    }
280 
281    default:
282       return false;
283    }
284 }
285 
286 static bool
opt_access_impl(struct access_state * state,nir_function_impl * impl)287 opt_access_impl(struct access_state *state,
288                 nir_function_impl *impl)
289 {
290    bool progress = false;
291 
292    nir_foreach_block(block, impl) {
293       nir_foreach_instr(instr, block) {
294          if (instr->type == nir_instr_type_intrinsic)
295             progress |= process_intrinsic(state,
296                                           nir_instr_as_intrinsic(instr));
297       }
298    }
299 
300    if (progress) {
301       nir_metadata_preserve(impl,
302                             nir_metadata_block_index |
303                             nir_metadata_dominance |
304                             nir_metadata_live_ssa_defs |
305                             nir_metadata_loop_analysis);
306    }
307 
308 
309    return progress;
310 }
311 
312 bool
nir_opt_access(nir_shader * shader,const nir_opt_access_options * options)313 nir_opt_access(nir_shader *shader, const nir_opt_access_options *options)
314 {
315    struct access_state state = {
316       .shader = shader,
317       .infer_non_readable = options->infer_non_readable,
318       .vars_written = _mesa_pointer_set_create(NULL),
319       .vars_read = _mesa_pointer_set_create(NULL),
320    };
321 
322    bool var_progress = false;
323    bool progress = false;
324 
325    nir_foreach_function(func, shader) {
326       if (func->impl) {
327          nir_foreach_block(block, func->impl) {
328             nir_foreach_instr(instr, block) {
329                if (instr->type == nir_instr_type_intrinsic)
330                   gather_intrinsic(&state, nir_instr_as_intrinsic(instr));
331             }
332          }
333       }
334    }
335 
336    /* In Vulkan, buffers and images can alias. */
337    if (options->is_vulkan) {
338       state.buffers_written |= state.images_written;
339       state.images_written |= state.buffers_written;
340       state.buffers_read |= state.images_read;
341       state.images_read |= state.buffers_read;
342    }
343 
344    nir_foreach_variable_with_modes(var, shader, nir_var_uniform |
345                                                 nir_var_mem_ubo |
346                                                 nir_var_mem_ssbo)
347       var_progress |= process_variable(&state, var);
348 
349    nir_foreach_function(func, shader) {
350       if (func->impl) {
351          progress |= opt_access_impl(&state, func->impl);
352 
353          /* If we make a change to the uniforms, update all the impls. */
354          if (var_progress) {
355             nir_metadata_preserve(func->impl,
356                                   nir_metadata_block_index |
357                                   nir_metadata_dominance |
358                                   nir_metadata_live_ssa_defs |
359                                   nir_metadata_loop_analysis);
360          }
361       }
362    }
363 
364    progress |= var_progress;
365 
366    _mesa_set_destroy(state.vars_read, NULL);
367    _mesa_set_destroy(state.vars_written, NULL);
368    return progress;
369 }
370