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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_deref.h"
27 
28 static void
29 emit_load_store_deref(nir_builder *b, nir_intrinsic_instr *orig_instr,
30                       nir_deref_instr *parent,
31                       nir_deref_instr **deref_arr,
32                       nir_ssa_def **dest, nir_ssa_def *src);
33 
34 static void
emit_indirect_load_store_deref(nir_builder * b,nir_intrinsic_instr * orig_instr,nir_deref_instr * parent,nir_deref_instr ** deref_arr,int start,int end,nir_ssa_def ** dest,nir_ssa_def * src)35 emit_indirect_load_store_deref(nir_builder *b, nir_intrinsic_instr *orig_instr,
36                                nir_deref_instr *parent,
37                                nir_deref_instr **deref_arr,
38                                int start, int end,
39                                nir_ssa_def **dest, nir_ssa_def *src)
40 {
41    assert(start < end);
42    if (start == end - 1) {
43       emit_load_store_deref(b, orig_instr,
44                             nir_build_deref_array_imm(b, parent, start),
45                             deref_arr + 1, dest, src);
46    } else {
47       int mid = start + (end - start) / 2;
48 
49       nir_ssa_def *then_dest, *else_dest;
50 
51       nir_deref_instr *deref = *deref_arr;
52       assert(deref->deref_type == nir_deref_type_array);
53 
54       nir_push_if(b, nir_ilt(b, deref->arr.index.ssa, nir_imm_intN_t(b, mid, parent->dest.ssa.bit_size)));
55       emit_indirect_load_store_deref(b, orig_instr, parent, deref_arr,
56                                      start, mid, &then_dest, src);
57       nir_push_else(b, NULL);
58       emit_indirect_load_store_deref(b, orig_instr, parent, deref_arr,
59                                      mid, end, &else_dest, src);
60       nir_pop_if(b, NULL);
61 
62       if (src == NULL)
63          *dest = nir_if_phi(b, then_dest, else_dest);
64    }
65 }
66 
67 static void
emit_load_store_deref(nir_builder * b,nir_intrinsic_instr * orig_instr,nir_deref_instr * parent,nir_deref_instr ** deref_arr,nir_ssa_def ** dest,nir_ssa_def * src)68 emit_load_store_deref(nir_builder *b, nir_intrinsic_instr *orig_instr,
69                       nir_deref_instr *parent,
70                       nir_deref_instr **deref_arr,
71                       nir_ssa_def **dest, nir_ssa_def *src)
72 {
73    for (; *deref_arr; deref_arr++) {
74       nir_deref_instr *deref = *deref_arr;
75       if (deref->deref_type == nir_deref_type_array &&
76           !nir_src_is_const(deref->arr.index)) {
77          int length = glsl_get_length(parent->type);
78 
79          emit_indirect_load_store_deref(b, orig_instr, parent, deref_arr,
80                                         0, length, dest, src);
81          return;
82       }
83 
84       parent = nir_build_deref_follower(b, parent, deref);
85    }
86 
87    /* We reached the end of the deref chain.  Emit the instruction */
88    assert(*deref_arr == NULL);
89 
90    if (src == NULL) {
91       /* This is a load instruction */
92       nir_intrinsic_instr *load =
93          nir_intrinsic_instr_create(b->shader, orig_instr->intrinsic);
94       load->num_components = orig_instr->num_components;
95 
96       load->src[0] = nir_src_for_ssa(&parent->dest.ssa);
97 
98       /* Copy over any other sources.  This is needed for interp_deref_at */
99       for (unsigned i = 1;
100            i < nir_intrinsic_infos[orig_instr->intrinsic].num_srcs; i++)
101          nir_src_copy(&load->src[i], &orig_instr->src[i]);
102 
103       nir_ssa_dest_init(&load->instr, &load->dest,
104                         orig_instr->dest.ssa.num_components,
105                         orig_instr->dest.ssa.bit_size, NULL);
106       nir_builder_instr_insert(b, &load->instr);
107       *dest = &load->dest.ssa;
108    } else {
109       assert(orig_instr->intrinsic == nir_intrinsic_store_deref);
110       nir_store_deref(b, parent, src, nir_intrinsic_write_mask(orig_instr));
111    }
112 }
113 
114 static bool
lower_indirect_derefs_block(nir_block * block,nir_builder * b,nir_variable_mode modes,uint32_t max_lower_array_len,bool builtins_only)115 lower_indirect_derefs_block(nir_block *block, nir_builder *b,
116                             nir_variable_mode modes,
117                             uint32_t max_lower_array_len,
118                             bool builtins_only)
119 {
120    bool progress = false;
121 
122    nir_foreach_instr_safe(instr, block) {
123       if (instr->type != nir_instr_type_intrinsic)
124          continue;
125 
126       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
127       if (intrin->intrinsic != nir_intrinsic_load_deref &&
128           intrin->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
129           intrin->intrinsic != nir_intrinsic_interp_deref_at_sample &&
130           intrin->intrinsic != nir_intrinsic_interp_deref_at_offset &&
131           intrin->intrinsic != nir_intrinsic_interp_deref_at_vertex &&
132           intrin->intrinsic != nir_intrinsic_store_deref)
133          continue;
134 
135       nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
136 
137       /* Walk the deref chain back to the base and look for indirects */
138       uint32_t indirect_array_len = 1;
139       bool has_indirect = false;
140       nir_deref_instr *base = deref;
141       while (base && base->deref_type != nir_deref_type_var) {
142          nir_deref_instr *parent = nir_deref_instr_parent(base);
143          if (base->deref_type == nir_deref_type_array &&
144              !nir_src_is_const(base->arr.index)) {
145             indirect_array_len *= glsl_get_length(parent->type);
146             has_indirect = true;
147          }
148 
149          base = parent;
150       }
151 
152       if (!has_indirect || !base || indirect_array_len > max_lower_array_len)
153          continue;
154 
155       /* Only lower variables whose mode is in the mask, or compact
156        * array variables.  (We can't handle indirects on tightly packed
157        * scalar arrays, so we need to lower them regardless.)
158        */
159       if (!(modes & base->var->data.mode) && !base->var->data.compact)
160          continue;
161 
162       /* built-in's will always start with "gl_" */
163       if (builtins_only && strncmp(base->var->name, "gl_", 3))
164          continue;
165 
166       b->cursor = nir_instr_remove(&intrin->instr);
167 
168       nir_deref_path path;
169       nir_deref_path_init(&path, deref, NULL);
170       assert(path.path[0] == base);
171 
172       if (intrin->intrinsic == nir_intrinsic_store_deref) {
173          assert(intrin->src[1].is_ssa);
174          emit_load_store_deref(b, intrin, base, &path.path[1],
175                                NULL, intrin->src[1].ssa);
176       } else {
177          nir_ssa_def *result;
178          emit_load_store_deref(b, intrin, base, &path.path[1],
179                                &result, NULL);
180          nir_ssa_def_rewrite_uses(&intrin->dest.ssa, result);
181       }
182 
183       nir_deref_path_finish(&path);
184 
185       progress = true;
186    }
187 
188    return progress;
189 }
190 
191 static bool
lower_indirects_impl(nir_function_impl * impl,nir_variable_mode modes,uint32_t max_lower_array_len,bool builtins_only)192 lower_indirects_impl(nir_function_impl *impl, nir_variable_mode modes,
193                      uint32_t max_lower_array_len, bool builtins_only)
194 {
195    nir_builder builder;
196    nir_builder_init(&builder, impl);
197    bool progress = false;
198 
199    nir_foreach_block_safe(block, impl) {
200       progress |= lower_indirect_derefs_block(block, &builder, modes,
201                                               max_lower_array_len, builtins_only);
202    }
203 
204    if (progress)
205       nir_metadata_preserve(impl, nir_metadata_none);
206    else
207       nir_metadata_preserve(impl, nir_metadata_all);
208 
209    return progress;
210 }
211 
212 /** Lowers indirect variable loads/stores to direct loads/stores.
213  *
214  * The pass works by replacing any indirect load or store with an if-ladder
215  * that does a binary search on the array index.
216  */
217 bool
nir_lower_indirect_derefs(nir_shader * shader,nir_variable_mode modes,uint32_t max_lower_array_len)218 nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes,
219                           uint32_t max_lower_array_len)
220 {
221    bool progress = false;
222 
223    nir_foreach_function(function, shader) {
224       if (function->impl) {
225          progress = lower_indirects_impl(function->impl, modes,
226                                          max_lower_array_len, false) || progress;
227       }
228    }
229 
230    return progress;
231 }
232 
233 /** Lowers indirect variable loads/stores to direct loads/stores.
234  *
235  * The pass works by replacing any indirect load or store with an if-ladder
236  * that does a binary search on the array index. It only changes uniform variable builtins,
237  * e.g., gl_LightSource
238  */
239 bool
nir_lower_indirect_builtin_uniform_derefs(nir_shader * shader)240 nir_lower_indirect_builtin_uniform_derefs(nir_shader *shader)
241 {
242    bool progress = false;
243 
244    nir_foreach_function(function, shader) {
245       if (function->impl) {
246          progress = lower_indirects_impl(function->impl, nir_var_uniform,
247                                          UINT_MAX, true) || progress;
248       }
249    }
250 
251    return progress;
252 }
253