nir: Use nir_src_is_const and nir_src_as_* in core code
[mesa.git] / src / compiler / nir / nir_lower_indirect_derefs.c
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
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 nir_ssa_def *index = nir_imm_int(b, start);
44 emit_load_store_deref(b, orig_instr,
45 nir_build_deref_array(b, parent, index),
46 deref_arr + 1, dest, src);
47 } else {
48 int mid = start + (end - start) / 2;
49
50 nir_ssa_def *then_dest, *else_dest;
51
52 nir_deref_instr *deref = *deref_arr;
53 assert(deref->deref_type == nir_deref_type_array);
54
55 nir_push_if(b, nir_ilt(b, deref->arr.index.ssa, nir_imm_int(b, mid)));
56 emit_indirect_load_store_deref(b, orig_instr, parent, deref_arr,
57 start, mid, &then_dest, src);
58 nir_push_else(b, NULL);
59 emit_indirect_load_store_deref(b, orig_instr, parent, deref_arr,
60 mid, end, &else_dest, src);
61 nir_pop_if(b, NULL);
62
63 if (src == NULL)
64 *dest = nir_if_phi(b, then_dest, else_dest);
65 }
66 }
67
68 static void
69 emit_load_store_deref(nir_builder *b, nir_intrinsic_instr *orig_instr,
70 nir_deref_instr *parent,
71 nir_deref_instr **deref_arr,
72 nir_ssa_def **dest, nir_ssa_def *src)
73 {
74 for (; *deref_arr; deref_arr++) {
75 nir_deref_instr *deref = *deref_arr;
76 if (deref->deref_type == nir_deref_type_array &&
77 !nir_src_is_const(deref->arr.index)) {
78 int length = glsl_get_length(parent->type);
79
80 emit_indirect_load_store_deref(b, orig_instr, parent, deref_arr,
81 0, length, dest, src);
82 return;
83 }
84
85 parent = nir_build_deref_follower(b, parent, deref);
86 }
87
88 /* We reached the end of the deref chain. Emit the instruction */
89 assert(*deref_arr == NULL);
90
91 if (src == NULL) {
92 /* This is a load instruction */
93 nir_intrinsic_instr *load =
94 nir_intrinsic_instr_create(b->shader, orig_instr->intrinsic);
95 load->num_components = orig_instr->num_components;
96
97 load->src[0] = nir_src_for_ssa(&parent->dest.ssa);
98
99 /* Copy over any other sources. This is needed for interp_deref_at */
100 for (unsigned i = 1;
101 i < nir_intrinsic_infos[orig_instr->intrinsic].num_srcs; i++)
102 nir_src_copy(&load->src[i], &orig_instr->src[i], load);
103
104 nir_ssa_dest_init(&load->instr, &load->dest,
105 orig_instr->dest.ssa.num_components,
106 orig_instr->dest.ssa.bit_size, NULL);
107 nir_builder_instr_insert(b, &load->instr);
108 *dest = &load->dest.ssa;
109 } else {
110 assert(orig_instr->intrinsic == nir_intrinsic_store_deref);
111 nir_store_deref(b, parent, src, nir_intrinsic_write_mask(orig_instr));
112 }
113 }
114
115 static bool
116 lower_indirect_derefs_block(nir_block *block, nir_builder *b,
117 nir_variable_mode modes)
118 {
119 bool progress = false;
120
121 nir_foreach_instr_safe(instr, block) {
122 if (instr->type != nir_instr_type_intrinsic)
123 continue;
124
125 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
126 if (intrin->intrinsic != nir_intrinsic_load_deref &&
127 intrin->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
128 intrin->intrinsic != nir_intrinsic_interp_deref_at_sample &&
129 intrin->intrinsic != nir_intrinsic_interp_deref_at_offset &&
130 intrin->intrinsic != nir_intrinsic_store_deref)
131 continue;
132
133 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
134
135 /* Walk the deref chain back to the base and look for indirects */
136 bool has_indirect = false;
137 nir_deref_instr *base = deref;
138 while (base->deref_type != nir_deref_type_var) {
139 if (base->deref_type == nir_deref_type_array &&
140 !nir_src_is_const(base->arr.index))
141 has_indirect = true;
142
143 base = nir_deref_instr_parent(base);
144 }
145
146 if (!has_indirect)
147 continue;
148
149 /* Only lower variables whose mode is in the mask, or compact
150 * array variables. (We can't handle indirects on tightly packed
151 * scalar arrays, so we need to lower them regardless.)
152 */
153 if (!(modes & base->var->data.mode) && !base->var->data.compact)
154 continue;
155
156 b->cursor = nir_instr_remove(&intrin->instr);
157
158 nir_deref_path path;
159 nir_deref_path_init(&path, deref, NULL);
160 assert(path.path[0] == base);
161
162 if (intrin->intrinsic == nir_intrinsic_store_deref) {
163 assert(intrin->src[1].is_ssa);
164 emit_load_store_deref(b, intrin, base, &path.path[1],
165 NULL, intrin->src[1].ssa);
166 } else {
167 nir_ssa_def *result;
168 emit_load_store_deref(b, intrin, base, &path.path[1],
169 &result, NULL);
170 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(result));
171 }
172
173 nir_deref_path_finish(&path);
174
175 progress = true;
176 }
177
178 return progress;
179 }
180
181 static bool
182 lower_indirects_impl(nir_function_impl *impl, nir_variable_mode modes)
183 {
184 nir_builder builder;
185 nir_builder_init(&builder, impl);
186 bool progress = false;
187
188 nir_foreach_block_safe(block, impl) {
189 progress |= lower_indirect_derefs_block(block, &builder, modes);
190 }
191
192 if (progress)
193 nir_metadata_preserve(impl, nir_metadata_none);
194
195 return progress;
196 }
197
198 /** Lowers indirect variable loads/stores to direct loads/stores.
199 *
200 * The pass works by replacing any indirect load or store with an if-ladder
201 * that does a binary search on the array index.
202 */
203 bool
204 nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes)
205 {
206 bool progress = false;
207
208 if (modes == 0)
209 return false;
210
211 nir_foreach_function(function, shader) {
212 if (function->impl)
213 progress = lower_indirects_impl(function->impl, modes) || progress;
214 }
215
216 return progress;
217 }