1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* Copyright © 2018 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir.h"
#include "nir_builder.h"
/**
* Recursively removes unused deref instructions
*/
bool
nir_deref_instr_remove_if_unused(nir_deref_instr *instr)
{
bool progress = false;
for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) {
/* If anyone is using this deref, leave it alone */
assert(d->dest.is_ssa);
if (!list_empty(&d->dest.ssa.uses))
break;
nir_instr_remove(&d->instr);
progress = true;
}
return progress;
}
bool
nir_remove_dead_derefs_impl(nir_function_impl *impl)
{
bool progress = false;
nir_foreach_block(block, impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type == nir_instr_type_deref &&
nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
progress = true;
}
}
if (progress)
nir_metadata_preserve(impl, nir_metadata_block_index |
nir_metadata_dominance);
return progress;
}
bool
nir_remove_dead_derefs(nir_shader *shader)
{
bool progress = false;
nir_foreach_function(function, shader) {
if (function->impl && nir_remove_dead_derefs_impl(function->impl))
progress = true;
}
return progress;
}
nir_deref_var *
nir_deref_instr_to_deref(nir_deref_instr *instr, void *mem_ctx)
{
nir_deref *deref = NULL;
while (instr->deref_type != nir_deref_type_var) {
nir_deref *nderef;
switch (instr->deref_type) {
case nir_deref_type_array:
case nir_deref_type_array_wildcard: {
nir_deref_array *deref_arr = nir_deref_array_create(mem_ctx);
if (instr->deref_type == nir_deref_type_array) {
nir_const_value *const_index =
nir_src_as_const_value(instr->arr.index);
if (const_index) {
deref_arr->deref_array_type = nir_deref_array_type_direct;
deref_arr->base_offset = const_index->u32[0];
} else {
deref_arr->deref_array_type = nir_deref_array_type_indirect;
deref_arr->base_offset = 0;
nir_src_copy(&deref_arr->indirect, &instr->arr.index, mem_ctx);
}
} else {
deref_arr->deref_array_type = nir_deref_array_type_wildcard;
}
nderef = &deref_arr->deref;
break;
}
case nir_deref_type_struct:
nderef = &nir_deref_struct_create(mem_ctx, instr->strct.index)->deref;
break;
default:
unreachable("Invalid deref instruction type");
}
nderef->child = deref;
ralloc_steal(nderef, deref);
nderef->type = instr->type;
deref = nderef;
assert(instr->parent.is_ssa);
instr = nir_src_as_deref(instr->parent);
}
assert(instr->deref_type == nir_deref_type_var);
nir_deref_var *deref_var = nir_deref_var_create(mem_ctx, instr->var);
deref_var->deref.child = deref;
ralloc_steal(deref_var, deref);
return deref_var;
}
static nir_deref_var *
nir_deref_src_to_deref(nir_src src, void *mem_ctx)
{
return nir_deref_instr_to_deref(nir_src_as_deref(src), mem_ctx);
}
static bool
nir_lower_deref_instrs_tex(nir_tex_instr *tex)
{
bool progress = false;
/* Remove the instruction before we modify it. This way we won't mess up
* use-def chains when we move sources around.
*/
nir_cursor cursor = nir_instr_remove(&tex->instr);
unsigned new_num_srcs = 0;
for (unsigned i = 0; i < tex->num_srcs; i++) {
if (tex->src[i].src_type == nir_tex_src_texture_deref) {
tex->texture = nir_deref_src_to_deref(tex->src[i].src, tex);
progress = true;
continue;
} else if (tex->src[i].src_type == nir_tex_src_sampler_deref) {
tex->sampler = nir_deref_src_to_deref(tex->src[i].src, tex);
progress = true;
continue;
}
/* Compact the sources down to remove the deref sources */
assert(new_num_srcs <= i);
tex->src[new_num_srcs++] = tex->src[i];
}
tex->num_srcs = new_num_srcs;
nir_instr_insert(cursor, &tex->instr);
return progress;
}
static bool
nir_lower_deref_instrs_intrin(nir_intrinsic_instr *intrin,
enum nir_lower_deref_flags flags)
{
nir_intrinsic_op deref_op = intrin->intrinsic;
nir_intrinsic_op var_op;
switch (deref_op) {
#define CASE(a) \
case nir_intrinsic_##a##_deref: \
if (!(flags & nir_lower_load_store_derefs)) \
return false; \
var_op = nir_intrinsic_##a##_var; \
break;
CASE(load)
CASE(store)
CASE(copy)
#undef CASE
#define CASE(a) \
case nir_intrinsic_interp_deref_##a: \
if (!(flags & nir_lower_interp_derefs)) \
return false; \
var_op = nir_intrinsic_interp_var_##a; \
break;
CASE(at_centroid)
CASE(at_sample)
CASE(at_offset)
#undef CASE
#define CASE(a) \
case nir_intrinsic_atomic_counter_##a##_deref: \
if (!(flags & nir_lower_atomic_counter_derefs)) \
return false; \
var_op = nir_intrinsic_atomic_counter_##a##_var; \
break;
CASE(inc)
CASE(dec)
CASE(read)
CASE(add)
CASE(min)
CASE(max)
CASE(and)
CASE(or)
CASE(xor)
CASE(exchange)
CASE(comp_swap)
#undef CASE
#define CASE(a) \
case nir_intrinsic_deref_atomic_##a: \
if (!(flags & nir_lower_atomic_derefs)) \
return false; \
var_op = nir_intrinsic_var_atomic_##a; \
break;
CASE(add)
CASE(imin)
CASE(umin)
CASE(imax)
CASE(umax)
CASE(and)
CASE(or)
CASE(xor)
CASE(exchange)
CASE(comp_swap)
#undef CASE
#define CASE(a) \
case nir_intrinsic_image_deref_##a: \
if (!(flags & nir_lower_image_derefs)) \
return false; \
var_op = nir_intrinsic_image_var_##a; \
break;
CASE(load)
CASE(store)
CASE(atomic_add)
CASE(atomic_min)
CASE(atomic_max)
CASE(atomic_and)
CASE(atomic_or)
CASE(atomic_xor)
CASE(atomic_exchange)
CASE(atomic_comp_swap)
CASE(size)
CASE(samples)
#undef CASE
default:
return false;
}
/* Remove the instruction before we modify it. This way we won't mess up
* use-def chains when we move sources around.
*/
nir_cursor cursor = nir_instr_remove(&intrin->instr);
unsigned num_derefs = nir_intrinsic_infos[var_op].num_variables;
assert(nir_intrinsic_infos[var_op].num_srcs + num_derefs ==
nir_intrinsic_infos[deref_op].num_srcs);
/* Move deref sources to variables */
for (unsigned i = 0; i < num_derefs; i++)
intrin->variables[i] = nir_deref_src_to_deref(intrin->src[i], intrin);
/* Shift all the other sources down */
for (unsigned i = 0; i < nir_intrinsic_infos[var_op].num_srcs; i++)
nir_src_copy(&intrin->src[i], &intrin->src[i + num_derefs], intrin);
/* Rewrite the extra sources to NIR_SRC_INIT just in case */
for (unsigned i = 0; i < num_derefs; i++)
intrin->src[nir_intrinsic_infos[var_op].num_srcs + i] = NIR_SRC_INIT;
/* It's safe to just stomp the intrinsic to var intrinsic since every
* intrinsic has room for some variables and the number of sources only
* shrinks.
*/
intrin->intrinsic = var_op;
nir_instr_insert(cursor, &intrin->instr);
return true;
}
static bool
nir_lower_deref_instrs_impl(nir_function_impl *impl,
enum nir_lower_deref_flags flags)
{
bool progress = false;
/* Walk the instructions in reverse order so that we can safely clean up
* the deref instructions after we clean up their uses.
*/
nir_foreach_block_reverse(block, impl) {
nir_foreach_instr_reverse_safe(instr, block) {
switch (instr->type) {
case nir_instr_type_deref:
if (list_empty(&nir_instr_as_deref(instr)->dest.ssa.uses)) {
nir_instr_remove(instr);
progress = true;
}
break;
case nir_instr_type_tex:
if (flags & nir_lower_texture_derefs)
progress |= nir_lower_deref_instrs_tex(nir_instr_as_tex(instr));
break;
case nir_instr_type_intrinsic:
progress |=
nir_lower_deref_instrs_intrin(nir_instr_as_intrinsic(instr),
flags);
break;
default:
break; /* Nothing to do */
}
}
}
if (progress) {
nir_metadata_preserve(impl, nir_metadata_block_index |
nir_metadata_dominance);
}
return progress;
}
bool
nir_lower_deref_instrs(nir_shader *shader,
enum nir_lower_deref_flags flags)
{
bool progress = false;
nir_foreach_function(function, shader) {
if (!function->impl)
continue;
progress |= nir_lower_deref_instrs_impl(function->impl, flags);
}
shader->lowered_derefs |= flags;
return progress;
}
void
nir_fixup_deref_modes(nir_shader *shader)
{
nir_foreach_function(function, shader) {
if (!function->impl)
continue;
nir_foreach_block(block, function->impl) {
nir_foreach_instr(instr, block) {
if (instr->type != nir_instr_type_deref)
continue;
nir_deref_instr *deref = nir_instr_as_deref(instr);
nir_variable_mode parent_mode;
if (deref->deref_type == nir_deref_type_var) {
parent_mode = deref->var->data.mode;
} else {
assert(deref->parent.is_ssa);
nir_deref_instr *parent =
nir_instr_as_deref(deref->parent.ssa->parent_instr);
parent_mode = parent->mode;
}
deref->mode = parent_mode;
}
}
}
}
|