1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci/* This pass optimizes GL access qualifiers. So far it does three things:
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci * - Infer readonly when it's missing.
29bf215546Sopenharmony_ci * - Infer writeonly when it's missing.
30bf215546Sopenharmony_ci * - Infer ACCESS_CAN_REORDER when the following are true:
31bf215546Sopenharmony_ci *   - Either there are no writes, or ACCESS_NON_WRITEABLE is set. In either
32bf215546Sopenharmony_ci *     case there are no writes to the underlying memory.
33bf215546Sopenharmony_ci *   - ACCESS_VOLATILE is not set.
34bf215546Sopenharmony_ci *
35bf215546Sopenharmony_ci * If these conditions are true, then image and buffer reads may be treated as
36bf215546Sopenharmony_ci * if they were uniform buffer reads, i.e. they may be arbitrarily moved,
37bf215546Sopenharmony_ci * combined, rematerialized etc.
38bf215546Sopenharmony_ci */
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistruct access_state {
41bf215546Sopenharmony_ci   nir_shader *shader;
42bf215546Sopenharmony_ci   bool infer_non_readable;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   struct set *vars_written;
45bf215546Sopenharmony_ci   struct set *vars_read;
46bf215546Sopenharmony_ci   bool images_written;
47bf215546Sopenharmony_ci   bool buffers_written;
48bf215546Sopenharmony_ci   bool images_read;
49bf215546Sopenharmony_ci   bool buffers_read;
50bf215546Sopenharmony_ci};
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic void
53bf215546Sopenharmony_cigather_buffer_access(struct access_state *state, nir_ssa_def *def, bool read, bool write)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   state->buffers_read |= read;
56bf215546Sopenharmony_ci   state->buffers_written |= write;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   if (!def)
59bf215546Sopenharmony_ci      return;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   const nir_variable *var = nir_get_binding_variable(
62bf215546Sopenharmony_ci      state->shader, nir_chase_binding(nir_src_for_ssa(def)));
63bf215546Sopenharmony_ci   if (var) {
64bf215546Sopenharmony_ci      if (read)
65bf215546Sopenharmony_ci         _mesa_set_add(state->vars_read, var);
66bf215546Sopenharmony_ci      if (write)
67bf215546Sopenharmony_ci         _mesa_set_add(state->vars_written, var);
68bf215546Sopenharmony_ci   } else {
69bf215546Sopenharmony_ci      nir_foreach_variable_with_modes(possible_var, state->shader, nir_var_mem_ssbo) {
70bf215546Sopenharmony_ci         if (read)
71bf215546Sopenharmony_ci            _mesa_set_add(state->vars_read, possible_var);
72bf215546Sopenharmony_ci         if (write)
73bf215546Sopenharmony_ci            _mesa_set_add(state->vars_written, possible_var);
74bf215546Sopenharmony_ci      }
75bf215546Sopenharmony_ci   }
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_cistatic void
79bf215546Sopenharmony_cigather_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   const nir_variable *var;
82bf215546Sopenharmony_ci   bool read, write;
83bf215546Sopenharmony_ci   switch (instr->intrinsic) {
84bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_load:
85bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_store:
86bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_sparse_load:
87bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_add:
88bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imin:
89bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umin:
90bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imax:
91bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umax:
92bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_and:
93bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_or:
94bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_xor:
95bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_exchange:
96bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_comp_swap:
97bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fadd:
98bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fmin:
99bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fmax:
100bf215546Sopenharmony_ci      var = nir_intrinsic_get_var(instr, 0);
101bf215546Sopenharmony_ci      read = instr->intrinsic != nir_intrinsic_image_deref_store;
102bf215546Sopenharmony_ci      write = instr->intrinsic != nir_intrinsic_image_deref_load &&
103bf215546Sopenharmony_ci              instr->intrinsic != nir_intrinsic_image_deref_sparse_load;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      /* In OpenGL, buffer images use normal buffer objects, whereas other
106bf215546Sopenharmony_ci       * image types use textures which cannot alias with buffer objects.
107bf215546Sopenharmony_ci       * Therefore we have to group buffer samplers together with SSBO's.
108bf215546Sopenharmony_ci       */
109bf215546Sopenharmony_ci      if (glsl_get_sampler_dim(glsl_without_array(var->type)) ==
110bf215546Sopenharmony_ci          GLSL_SAMPLER_DIM_BUF) {
111bf215546Sopenharmony_ci         state->buffers_read |= read;
112bf215546Sopenharmony_ci         state->buffers_written |= write;
113bf215546Sopenharmony_ci      } else {
114bf215546Sopenharmony_ci         state->images_read |= read;
115bf215546Sopenharmony_ci         state->images_written |= write;
116bf215546Sopenharmony_ci      }
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci      if ((var->data.mode == nir_var_uniform ||
119bf215546Sopenharmony_ci           var->data.mode == nir_var_image) && read)
120bf215546Sopenharmony_ci         _mesa_set_add(state->vars_read, var);
121bf215546Sopenharmony_ci      if ((var->data.mode == nir_var_uniform ||
122bf215546Sopenharmony_ci           var->data.mode == nir_var_image) && write)
123bf215546Sopenharmony_ci         _mesa_set_add(state->vars_written, var);
124bf215546Sopenharmony_ci      break;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_load:
127bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_store:
128bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_sparse_load:
129bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_add:
130bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_imin:
131bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_umin:
132bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_imax:
133bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_umax:
134bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_and:
135bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_or:
136bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_xor:
137bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_exchange:
138bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_comp_swap:
139bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_fadd:
140bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_fmin:
141bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_fmax:
142bf215546Sopenharmony_ci      read = instr->intrinsic != nir_intrinsic_bindless_image_store;
143bf215546Sopenharmony_ci      write = instr->intrinsic != nir_intrinsic_bindless_image_load &&
144bf215546Sopenharmony_ci              instr->intrinsic != nir_intrinsic_bindless_image_sparse_load;
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci      if (nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF) {
147bf215546Sopenharmony_ci         state->buffers_read |= read;
148bf215546Sopenharmony_ci         state->buffers_written |= write;
149bf215546Sopenharmony_ci      } else {
150bf215546Sopenharmony_ci         state->images_read |= read;
151bf215546Sopenharmony_ci         state->images_written |= write;
152bf215546Sopenharmony_ci      }
153bf215546Sopenharmony_ci      break;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   case nir_intrinsic_load_deref:
156bf215546Sopenharmony_ci   case nir_intrinsic_store_deref:
157bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_add:
158bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_imin:
159bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_umin:
160bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_imax:
161bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_umax:
162bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_and:
163bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_or:
164bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_xor:
165bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_exchange:
166bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_comp_swap:
167bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_fadd:
168bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_fmin:
169bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_fmax:
170bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_fcomp_swap: {
171bf215546Sopenharmony_ci      nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
172bf215546Sopenharmony_ci      if (!nir_deref_mode_may_be(deref, nir_var_mem_ssbo | nir_var_mem_global))
173bf215546Sopenharmony_ci         break;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci      bool ssbo = nir_deref_mode_is(deref, nir_var_mem_ssbo);
176bf215546Sopenharmony_ci      gather_buffer_access(state, ssbo ? instr->src[0].ssa : NULL,
177bf215546Sopenharmony_ci                           instr->intrinsic != nir_intrinsic_store_deref,
178bf215546Sopenharmony_ci                           instr->intrinsic != nir_intrinsic_load_deref);
179bf215546Sopenharmony_ci      break;
180bf215546Sopenharmony_ci   }
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   default:
183bf215546Sopenharmony_ci      break;
184bf215546Sopenharmony_ci   }
185bf215546Sopenharmony_ci}
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_cistatic bool
188bf215546Sopenharmony_ciprocess_variable(struct access_state *state, nir_variable *var)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   const struct glsl_type *type = glsl_without_array(var->type);
191bf215546Sopenharmony_ci   if (var->data.mode != nir_var_mem_ssbo &&
192bf215546Sopenharmony_ci       !(var->data.mode == nir_var_uniform && glsl_type_is_image(type)) &&
193bf215546Sopenharmony_ci       var->data.mode != nir_var_image)
194bf215546Sopenharmony_ci      return false;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   /* Ignore variables we've already marked */
197bf215546Sopenharmony_ci   if (var->data.access & ACCESS_CAN_REORDER)
198bf215546Sopenharmony_ci      return false;
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   unsigned access = var->data.access;
201bf215546Sopenharmony_ci   bool is_buffer = var->data.mode == nir_var_mem_ssbo ||
202bf215546Sopenharmony_ci                    glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   if (!(access & ACCESS_NON_WRITEABLE)) {
205bf215546Sopenharmony_ci      if (is_buffer ? !state->buffers_written : !state->images_written)
206bf215546Sopenharmony_ci         access |= ACCESS_NON_WRITEABLE;
207bf215546Sopenharmony_ci      else if ((access & ACCESS_RESTRICT) && !_mesa_set_search(state->vars_written, var))
208bf215546Sopenharmony_ci         access |= ACCESS_NON_WRITEABLE;
209bf215546Sopenharmony_ci   }
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   if (state->infer_non_readable && !(access & ACCESS_NON_READABLE)) {
212bf215546Sopenharmony_ci      if (is_buffer ? !state->buffers_read : !state->images_read)
213bf215546Sopenharmony_ci         access |= ACCESS_NON_READABLE;
214bf215546Sopenharmony_ci      else if ((access & ACCESS_RESTRICT) && !_mesa_set_search(state->vars_read, var))
215bf215546Sopenharmony_ci         access |= ACCESS_NON_READABLE;
216bf215546Sopenharmony_ci   }
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   bool changed = var->data.access != access;
219bf215546Sopenharmony_ci   var->data.access = access;
220bf215546Sopenharmony_ci   return changed;
221bf215546Sopenharmony_ci}
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_cistatic bool
224bf215546Sopenharmony_ciupdate_access(struct access_state *state, nir_intrinsic_instr *instr, bool is_buffer, bool is_global)
225bf215546Sopenharmony_ci{
226bf215546Sopenharmony_ci   enum gl_access_qualifier access = nir_intrinsic_access(instr);
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci   bool is_memory_readonly = access & ACCESS_NON_WRITEABLE;
229bf215546Sopenharmony_ci   bool is_memory_writeonly = access & ACCESS_NON_READABLE;
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   if (instr->intrinsic != nir_intrinsic_bindless_image_load &&
232bf215546Sopenharmony_ci       instr->intrinsic != nir_intrinsic_bindless_image_store &&
233bf215546Sopenharmony_ci       instr->intrinsic != nir_intrinsic_bindless_image_sparse_load &&
234bf215546Sopenharmony_ci       !is_global) {
235bf215546Sopenharmony_ci      const nir_variable *var = nir_get_binding_variable(
236bf215546Sopenharmony_ci         state->shader, nir_chase_binding(instr->src[0]));
237bf215546Sopenharmony_ci      is_memory_readonly |= var && (var->data.access & ACCESS_NON_WRITEABLE);
238bf215546Sopenharmony_ci      is_memory_writeonly |= var && (var->data.access & ACCESS_NON_READABLE);
239bf215546Sopenharmony_ci   }
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   if (is_global) {
242bf215546Sopenharmony_ci      is_memory_readonly |= !state->buffers_written && !state->images_written;
243bf215546Sopenharmony_ci      is_memory_writeonly |= !state->buffers_read && !state->images_read;
244bf215546Sopenharmony_ci   } else {
245bf215546Sopenharmony_ci      is_memory_readonly |= is_buffer ? !state->buffers_written : !state->images_written;
246bf215546Sopenharmony_ci      is_memory_writeonly |= is_buffer ? !state->buffers_read : !state->images_read;
247bf215546Sopenharmony_ci   }
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   if (is_memory_readonly)
250bf215546Sopenharmony_ci      access |= ACCESS_NON_WRITEABLE;
251bf215546Sopenharmony_ci   if (state->infer_non_readable && is_memory_writeonly)
252bf215546Sopenharmony_ci      access |= ACCESS_NON_READABLE;
253bf215546Sopenharmony_ci   if (!(access & ACCESS_VOLATILE) && is_memory_readonly)
254bf215546Sopenharmony_ci      access |= ACCESS_CAN_REORDER;
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci   bool progress = nir_intrinsic_access(instr) != access;
257bf215546Sopenharmony_ci   nir_intrinsic_set_access(instr, access);
258bf215546Sopenharmony_ci   return progress;
259bf215546Sopenharmony_ci}
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_cistatic bool
262bf215546Sopenharmony_ciprocess_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
263bf215546Sopenharmony_ci{
264bf215546Sopenharmony_ci   switch (instr->intrinsic) {
265bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_load:
266bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_store:
267bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_sparse_load:
268bf215546Sopenharmony_ci      return update_access(state, instr, nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF,
269bf215546Sopenharmony_ci                           false);
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci   case nir_intrinsic_load_deref:
272bf215546Sopenharmony_ci   case nir_intrinsic_store_deref: {
273bf215546Sopenharmony_ci      if (nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_global))
274bf215546Sopenharmony_ci         return update_access(state, instr, false, true);
275bf215546Sopenharmony_ci      else if (nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_ssbo))
276bf215546Sopenharmony_ci         return update_access(state, instr, true, false);
277bf215546Sopenharmony_ci      else
278bf215546Sopenharmony_ci         return false;
279bf215546Sopenharmony_ci   }
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_load:
282bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_store:
283bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_sparse_load: {
284bf215546Sopenharmony_ci      nir_variable *var = nir_intrinsic_get_var(instr, 0);
285bf215546Sopenharmony_ci
286bf215546Sopenharmony_ci      bool is_buffer =
287bf215546Sopenharmony_ci         glsl_get_sampler_dim(glsl_without_array(var->type)) == GLSL_SAMPLER_DIM_BUF;
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci      return update_access(state, instr, is_buffer, false);
290bf215546Sopenharmony_ci   }
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   default:
293bf215546Sopenharmony_ci      return false;
294bf215546Sopenharmony_ci   }
295bf215546Sopenharmony_ci}
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_cistatic bool
298bf215546Sopenharmony_ciopt_access_impl(struct access_state *state,
299bf215546Sopenharmony_ci                nir_function_impl *impl)
300bf215546Sopenharmony_ci{
301bf215546Sopenharmony_ci   bool progress = false;
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
304bf215546Sopenharmony_ci      nir_foreach_instr(instr, block) {
305bf215546Sopenharmony_ci         if (instr->type == nir_instr_type_intrinsic)
306bf215546Sopenharmony_ci            progress |= process_intrinsic(state,
307bf215546Sopenharmony_ci                                          nir_instr_as_intrinsic(instr));
308bf215546Sopenharmony_ci      }
309bf215546Sopenharmony_ci   }
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   if (progress) {
312bf215546Sopenharmony_ci      nir_metadata_preserve(impl,
313bf215546Sopenharmony_ci                            nir_metadata_block_index |
314bf215546Sopenharmony_ci                            nir_metadata_dominance |
315bf215546Sopenharmony_ci                            nir_metadata_live_ssa_defs |
316bf215546Sopenharmony_ci                            nir_metadata_loop_analysis);
317bf215546Sopenharmony_ci   }
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   return progress;
321bf215546Sopenharmony_ci}
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_cibool
324bf215546Sopenharmony_cinir_opt_access(nir_shader *shader, const nir_opt_access_options *options)
325bf215546Sopenharmony_ci{
326bf215546Sopenharmony_ci   struct access_state state = {
327bf215546Sopenharmony_ci      .shader = shader,
328bf215546Sopenharmony_ci      .infer_non_readable = options->infer_non_readable,
329bf215546Sopenharmony_ci      .vars_written = _mesa_pointer_set_create(NULL),
330bf215546Sopenharmony_ci      .vars_read = _mesa_pointer_set_create(NULL),
331bf215546Sopenharmony_ci   };
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci   bool var_progress = false;
334bf215546Sopenharmony_ci   bool progress = false;
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   nir_foreach_function(func, shader) {
337bf215546Sopenharmony_ci      if (func->impl) {
338bf215546Sopenharmony_ci         nir_foreach_block(block, func->impl) {
339bf215546Sopenharmony_ci            nir_foreach_instr(instr, block) {
340bf215546Sopenharmony_ci               if (instr->type == nir_instr_type_intrinsic)
341bf215546Sopenharmony_ci                  gather_intrinsic(&state, nir_instr_as_intrinsic(instr));
342bf215546Sopenharmony_ci            }
343bf215546Sopenharmony_ci         }
344bf215546Sopenharmony_ci      }
345bf215546Sopenharmony_ci   }
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   /* In Vulkan, buffers and images can alias. */
348bf215546Sopenharmony_ci   if (options->is_vulkan) {
349bf215546Sopenharmony_ci      state.buffers_written |= state.images_written;
350bf215546Sopenharmony_ci      state.images_written |= state.buffers_written;
351bf215546Sopenharmony_ci      state.buffers_read |= state.images_read;
352bf215546Sopenharmony_ci      state.images_read |= state.buffers_read;
353bf215546Sopenharmony_ci   }
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, shader, nir_var_uniform |
356bf215546Sopenharmony_ci                                                nir_var_mem_ubo |
357bf215546Sopenharmony_ci                                                nir_var_mem_ssbo |
358bf215546Sopenharmony_ci                                                nir_var_image)
359bf215546Sopenharmony_ci      var_progress |= process_variable(&state, var);
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci   nir_foreach_function(func, shader) {
362bf215546Sopenharmony_ci      if (func->impl) {
363bf215546Sopenharmony_ci         progress |= opt_access_impl(&state, func->impl);
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci         /* If we make a change to the uniforms, update all the impls. */
366bf215546Sopenharmony_ci         if (var_progress) {
367bf215546Sopenharmony_ci            nir_metadata_preserve(func->impl,
368bf215546Sopenharmony_ci                                  nir_metadata_block_index |
369bf215546Sopenharmony_ci                                  nir_metadata_dominance |
370bf215546Sopenharmony_ci                                  nir_metadata_live_ssa_defs |
371bf215546Sopenharmony_ci                                  nir_metadata_loop_analysis);
372bf215546Sopenharmony_ci         }
373bf215546Sopenharmony_ci      }
374bf215546Sopenharmony_ci   }
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   progress |= var_progress;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci   _mesa_set_destroy(state.vars_read, NULL);
379bf215546Sopenharmony_ci   _mesa_set_destroy(state.vars_written, NULL);
380bf215546Sopenharmony_ci   return progress;
381bf215546Sopenharmony_ci}
382