nir: Remove some no longer needed asserts
[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
27 static void
28 emit_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,
29 nir_deref_var *deref, nir_deref *tail,
30 nir_ssa_def **dest, nir_ssa_def *src);
31
32 static void
33 emit_indirect_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,
34 nir_deref_var *deref, nir_deref *arr_parent,
35 int start, int end,
36 nir_ssa_def **dest, nir_ssa_def *src)
37 {
38 nir_deref_array *arr = nir_deref_as_array(arr_parent->child);
39 assert(arr->deref_array_type == nir_deref_array_type_indirect);
40 assert(arr->indirect.is_ssa);
41
42 assert(start < end);
43 if (start == end - 1) {
44 /* Base case. Just emit the load/store op */
45 nir_deref_array direct = *arr;
46 direct.deref_array_type = nir_deref_array_type_direct;
47 direct.base_offset += start;
48 direct.indirect = NIR_SRC_INIT;
49
50 arr_parent->child = &direct.deref;
51 emit_load_store(b, orig_instr, deref, &direct.deref, dest, src);
52 arr_parent->child = &arr->deref;
53 } else {
54 int mid = start + (end - start) / 2;
55
56 nir_ssa_def *then_dest, *else_dest;
57
58 nir_if *if_stmt = nir_if_create(b->shader);
59 if_stmt->condition = nir_src_for_ssa(nir_ilt(b, arr->indirect.ssa,
60 nir_imm_int(b, mid)));
61 nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
62
63 b->cursor = nir_after_cf_list(&if_stmt->then_list);
64 emit_indirect_load_store(b, orig_instr, deref, arr_parent,
65 start, mid, &then_dest, src);
66
67 b->cursor = nir_after_cf_list(&if_stmt->else_list);
68 emit_indirect_load_store(b, orig_instr, deref, arr_parent,
69 mid, end, &else_dest, src);
70
71 b->cursor = nir_after_cf_node(&if_stmt->cf_node);
72
73 if (src == NULL) {
74 /* We're a load. We need to insert a phi node */
75 nir_phi_instr *phi = nir_phi_instr_create(b->shader);
76 unsigned bit_size = then_dest->bit_size;
77 nir_ssa_dest_init(&phi->instr, &phi->dest,
78 then_dest->num_components, bit_size, NULL);
79
80 nir_phi_src *src0 = ralloc(phi, nir_phi_src);
81 src0->pred = nir_if_last_then_block(if_stmt);
82 src0->src = nir_src_for_ssa(then_dest);
83 exec_list_push_tail(&phi->srcs, &src0->node);
84
85 nir_phi_src *src1 = ralloc(phi, nir_phi_src);
86 src1->pred = nir_if_last_else_block(if_stmt);
87 src1->src = nir_src_for_ssa(else_dest);
88 exec_list_push_tail(&phi->srcs, &src1->node);
89
90 nir_builder_instr_insert(b, &phi->instr);
91 *dest = &phi->dest.ssa;
92 }
93 }
94 }
95
96 static void
97 emit_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,
98 nir_deref_var *deref, nir_deref *tail,
99 nir_ssa_def **dest, nir_ssa_def *src)
100 {
101 for (; tail->child; tail = tail->child) {
102 if (tail->child->deref_type != nir_deref_type_array)
103 continue;
104
105 nir_deref_array *arr = nir_deref_as_array(tail->child);
106 if (arr->deref_array_type != nir_deref_array_type_indirect)
107 continue;
108
109 int length = glsl_get_length(tail->type);
110
111 emit_indirect_load_store(b, orig_instr, deref, tail, -arr->base_offset,
112 length - arr->base_offset, dest, src);
113 return;
114 }
115
116 assert(tail && tail->child == NULL);
117
118 /* We reached the end of the deref chain. Emit the instruction */
119
120 if (src == NULL) {
121 /* This is a load instruction */
122 nir_intrinsic_instr *load =
123 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
124 load->num_components = orig_instr->num_components;
125 load->variables[0] =
126 nir_deref_as_var(nir_copy_deref(load, &deref->deref));
127 unsigned bit_size = orig_instr->dest.ssa.bit_size;
128 nir_ssa_dest_init(&load->instr, &load->dest,
129 load->num_components, bit_size, NULL);
130 nir_builder_instr_insert(b, &load->instr);
131 *dest = &load->dest.ssa;
132 } else {
133 /* This is a store instruction */
134 nir_intrinsic_instr *store =
135 nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
136 store->num_components = orig_instr->num_components;
137 nir_intrinsic_set_write_mask(store, nir_intrinsic_write_mask(orig_instr));
138 store->variables[0] =
139 nir_deref_as_var(nir_copy_deref(store, &deref->deref));
140 store->src[0] = nir_src_for_ssa(src);
141 nir_builder_instr_insert(b, &store->instr);
142 }
143 }
144
145 static bool
146 deref_has_indirect(nir_deref_var *deref)
147 {
148 for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {
149 if (tail->deref_type != nir_deref_type_array)
150 continue;
151
152 nir_deref_array *arr = nir_deref_as_array(tail);
153 if (arr->deref_array_type == nir_deref_array_type_indirect)
154 return true;
155 }
156
157 return false;
158 }
159
160 static bool
161 lower_indirect_block(nir_block *block, nir_builder *b,
162 nir_variable_mode modes)
163 {
164 bool progress = false;
165
166 nir_foreach_instr_safe(instr, block) {
167 if (instr->type != nir_instr_type_intrinsic)
168 continue;
169
170 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
171 if (intrin->intrinsic != nir_intrinsic_load_var &&
172 intrin->intrinsic != nir_intrinsic_store_var)
173 continue;
174
175 if (!deref_has_indirect(intrin->variables[0]))
176 continue;
177
178 /* Only lower variables whose mode is in the mask */
179 if (!(modes & intrin->variables[0]->var->data.mode))
180 continue;
181
182 b->cursor = nir_before_instr(&intrin->instr);
183
184 if (intrin->intrinsic == nir_intrinsic_load_var) {
185 nir_ssa_def *result;
186 emit_load_store(b, intrin, intrin->variables[0],
187 &intrin->variables[0]->deref, &result, NULL);
188 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(result));
189 } else {
190 assert(intrin->src[0].is_ssa);
191 emit_load_store(b, intrin, intrin->variables[0],
192 &intrin->variables[0]->deref, NULL, intrin->src[0].ssa);
193 }
194 nir_instr_remove(&intrin->instr);
195 progress = true;
196 }
197
198 return progress;
199 }
200
201 static bool
202 lower_indirects_impl(nir_function_impl *impl, nir_variable_mode modes)
203 {
204 nir_builder builder;
205 nir_builder_init(&builder, impl);
206 bool progress = false;
207
208 nir_foreach_block_safe(block, impl) {
209 progress |= lower_indirect_block(block, &builder, modes);
210 }
211
212 if (progress)
213 nir_metadata_preserve(impl, nir_metadata_none);
214
215 return progress;
216 }
217
218 /** Lowers indirect variable loads/stores to direct loads/stores.
219 *
220 * The pass works by replacing any indirect load or store with an if-ladder
221 * that does a binary search on the array index.
222 */
223 bool
224 nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes)
225 {
226 bool progress = false;
227
228 nir_foreach_function(function, shader) {
229 if (function->impl)
230 progress = lower_indirects_impl(function->impl, modes) || progress;
231 }
232
233 return progress;
234 }