1 /*
2 * Copyright © 2014 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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_builder.h"
30 #include "nir_control_flow_private.h"
31 #include "util/half_float.h"
32 #include <limits.h>
33 #include <assert.h>
34 #include <math.h>
35 #include "util/u_math.h"
36
37 #include "main/menums.h" /* BITFIELD64_MASK */
38
39
40 /** Return true if the component mask "mask" with bit size "old_bit_size" can
41 * be re-interpreted to be used with "new_bit_size".
42 */
43 bool
nir_component_mask_can_reinterpret(nir_component_mask_t mask,unsigned old_bit_size,unsigned new_bit_size)44 nir_component_mask_can_reinterpret(nir_component_mask_t mask,
45 unsigned old_bit_size,
46 unsigned new_bit_size)
47 {
48 assert(util_is_power_of_two_nonzero(old_bit_size));
49 assert(util_is_power_of_two_nonzero(new_bit_size));
50
51 if (old_bit_size == new_bit_size)
52 return true;
53
54 if (old_bit_size == 1 || new_bit_size == 1)
55 return false;
56
57 if (old_bit_size > new_bit_size) {
58 unsigned ratio = old_bit_size / new_bit_size;
59 return util_last_bit(mask) * ratio <= NIR_MAX_VEC_COMPONENTS;
60 }
61
62 unsigned iter = mask;
63 while (iter) {
64 int start, count;
65 u_bit_scan_consecutive_range(&iter, &start, &count);
66 start *= old_bit_size;
67 count *= old_bit_size;
68 if (start % new_bit_size != 0)
69 return false;
70 if (count % new_bit_size != 0)
71 return false;
72 }
73 return true;
74 }
75
76 /** Re-interprets a component mask "mask" with bit size "old_bit_size" so that
77 * it can be used can be used with "new_bit_size".
78 */
79 nir_component_mask_t
nir_component_mask_reinterpret(nir_component_mask_t mask,unsigned old_bit_size,unsigned new_bit_size)80 nir_component_mask_reinterpret(nir_component_mask_t mask,
81 unsigned old_bit_size,
82 unsigned new_bit_size)
83 {
84 assert(nir_component_mask_can_reinterpret(mask, old_bit_size, new_bit_size));
85
86 if (old_bit_size == new_bit_size)
87 return mask;
88
89 nir_component_mask_t new_mask = 0;
90 unsigned iter = mask;
91 while (iter) {
92 int start, count;
93 u_bit_scan_consecutive_range(&iter, &start, &count);
94 start = start * old_bit_size / new_bit_size;
95 count = count * old_bit_size / new_bit_size;
96 new_mask |= BITFIELD_RANGE(start, count);
97 }
98 return new_mask;
99 }
100
101 nir_shader *
nir_shader_create(void * mem_ctx,gl_shader_stage stage,const nir_shader_compiler_options * options,shader_info * si)102 nir_shader_create(void *mem_ctx,
103 gl_shader_stage stage,
104 const nir_shader_compiler_options *options,
105 shader_info *si)
106 {
107 nir_shader *shader = rzalloc(mem_ctx, nir_shader);
108
109 exec_list_make_empty(&shader->variables);
110
111 shader->options = options;
112
113 if (si) {
114 assert(si->stage == stage);
115 shader->info = *si;
116 } else {
117 shader->info.stage = stage;
118 }
119
120 exec_list_make_empty(&shader->functions);
121
122 shader->num_inputs = 0;
123 shader->num_outputs = 0;
124 shader->num_uniforms = 0;
125 shader->shared_size = 0;
126
127 return shader;
128 }
129
130 static nir_register *
reg_create(void * mem_ctx,struct exec_list * list)131 reg_create(void *mem_ctx, struct exec_list *list)
132 {
133 nir_register *reg = ralloc(mem_ctx, nir_register);
134
135 list_inithead(®->uses);
136 list_inithead(®->defs);
137 list_inithead(®->if_uses);
138
139 reg->num_components = 0;
140 reg->bit_size = 32;
141 reg->num_array_elems = 0;
142 reg->name = NULL;
143
144 exec_list_push_tail(list, ®->node);
145
146 return reg;
147 }
148
149 nir_register *
nir_local_reg_create(nir_function_impl * impl)150 nir_local_reg_create(nir_function_impl *impl)
151 {
152 nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
153 reg->index = impl->reg_alloc++;
154
155 return reg;
156 }
157
158 void
nir_reg_remove(nir_register * reg)159 nir_reg_remove(nir_register *reg)
160 {
161 exec_node_remove(®->node);
162 }
163
164 void
nir_shader_add_variable(nir_shader * shader,nir_variable * var)165 nir_shader_add_variable(nir_shader *shader, nir_variable *var)
166 {
167 switch (var->data.mode) {
168 case nir_var_function_temp:
169 assert(!"nir_shader_add_variable cannot be used for local variables");
170 return;
171
172 case nir_var_shader_temp:
173 case nir_var_shader_in:
174 case nir_var_shader_out:
175 case nir_var_uniform:
176 case nir_var_mem_ubo:
177 case nir_var_mem_ssbo:
178 case nir_var_mem_shared:
179 case nir_var_system_value:
180 case nir_var_mem_push_const:
181 case nir_var_mem_constant:
182 case nir_var_shader_call_data:
183 case nir_var_ray_hit_attrib:
184 break;
185
186 case nir_var_mem_global:
187 assert(!"nir_shader_add_variable cannot be used for global memory");
188 return;
189
190 default:
191 assert(!"invalid mode");
192 return;
193 }
194
195 exec_list_push_tail(&shader->variables, &var->node);
196 }
197
198 nir_variable *
nir_variable_create(nir_shader * shader,nir_variable_mode mode,const struct glsl_type * type,const char * name)199 nir_variable_create(nir_shader *shader, nir_variable_mode mode,
200 const struct glsl_type *type, const char *name)
201 {
202 nir_variable *var = rzalloc(shader, nir_variable);
203 var->name = ralloc_strdup(var, name);
204 var->type = type;
205 var->data.mode = mode;
206 var->data.how_declared = nir_var_declared_normally;
207
208 if ((mode == nir_var_shader_in &&
209 shader->info.stage != MESA_SHADER_VERTEX &&
210 shader->info.stage != MESA_SHADER_KERNEL) ||
211 (mode == nir_var_shader_out &&
212 shader->info.stage != MESA_SHADER_FRAGMENT))
213 var->data.interpolation = INTERP_MODE_SMOOTH;
214
215 if (mode == nir_var_shader_in || mode == nir_var_uniform)
216 var->data.read_only = true;
217
218 nir_shader_add_variable(shader, var);
219
220 return var;
221 }
222
223 nir_variable *
nir_local_variable_create(nir_function_impl * impl,const struct glsl_type * type,const char * name)224 nir_local_variable_create(nir_function_impl *impl,
225 const struct glsl_type *type, const char *name)
226 {
227 nir_variable *var = rzalloc(impl->function->shader, nir_variable);
228 var->name = ralloc_strdup(var, name);
229 var->type = type;
230 var->data.mode = nir_var_function_temp;
231
232 nir_function_impl_add_variable(impl, var);
233
234 return var;
235 }
236
237 nir_variable *
nir_find_variable_with_location(nir_shader * shader,nir_variable_mode mode,unsigned location)238 nir_find_variable_with_location(nir_shader *shader,
239 nir_variable_mode mode,
240 unsigned location)
241 {
242 assert(util_bitcount(mode) == 1 && mode != nir_var_function_temp);
243 nir_foreach_variable_with_modes(var, shader, mode) {
244 if (var->data.location == location)
245 return var;
246 }
247 return NULL;
248 }
249
250 nir_variable *
nir_find_variable_with_driver_location(nir_shader * shader,nir_variable_mode mode,unsigned location)251 nir_find_variable_with_driver_location(nir_shader *shader,
252 nir_variable_mode mode,
253 unsigned location)
254 {
255 assert(util_bitcount(mode) == 1 && mode != nir_var_function_temp);
256 nir_foreach_variable_with_modes(var, shader, mode) {
257 if (var->data.driver_location == location)
258 return var;
259 }
260 return NULL;
261 }
262
263 nir_function *
nir_function_create(nir_shader * shader,const char * name)264 nir_function_create(nir_shader *shader, const char *name)
265 {
266 nir_function *func = ralloc(shader, nir_function);
267
268 exec_list_push_tail(&shader->functions, &func->node);
269
270 func->name = ralloc_strdup(func, name);
271 func->shader = shader;
272 func->num_params = 0;
273 func->params = NULL;
274 func->impl = NULL;
275 func->is_entrypoint = false;
276
277 return func;
278 }
279
280 /* NOTE: if the instruction you are copying a src to is already added
281 * to the IR, use nir_instr_rewrite_src() instead.
282 */
nir_src_copy(nir_src * dest,const nir_src * src,void * mem_ctx)283 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
284 {
285 dest->is_ssa = src->is_ssa;
286 if (src->is_ssa) {
287 dest->ssa = src->ssa;
288 } else {
289 dest->reg.base_offset = src->reg.base_offset;
290 dest->reg.reg = src->reg.reg;
291 if (src->reg.indirect) {
292 dest->reg.indirect = ralloc(mem_ctx, nir_src);
293 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
294 } else {
295 dest->reg.indirect = NULL;
296 }
297 }
298 }
299
nir_dest_copy(nir_dest * dest,const nir_dest * src,nir_instr * instr)300 void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr)
301 {
302 /* Copying an SSA definition makes no sense whatsoever. */
303 assert(!src->is_ssa);
304
305 dest->is_ssa = false;
306
307 dest->reg.base_offset = src->reg.base_offset;
308 dest->reg.reg = src->reg.reg;
309 if (src->reg.indirect) {
310 dest->reg.indirect = ralloc(instr, nir_src);
311 nir_src_copy(dest->reg.indirect, src->reg.indirect, instr);
312 } else {
313 dest->reg.indirect = NULL;
314 }
315 }
316
317 void
nir_alu_src_copy(nir_alu_src * dest,const nir_alu_src * src,nir_alu_instr * instr)318 nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
319 nir_alu_instr *instr)
320 {
321 nir_src_copy(&dest->src, &src->src, &instr->instr);
322 dest->abs = src->abs;
323 dest->negate = src->negate;
324 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
325 dest->swizzle[i] = src->swizzle[i];
326 }
327
328 void
nir_alu_dest_copy(nir_alu_dest * dest,const nir_alu_dest * src,nir_alu_instr * instr)329 nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
330 nir_alu_instr *instr)
331 {
332 nir_dest_copy(&dest->dest, &src->dest, &instr->instr);
333 dest->write_mask = src->write_mask;
334 dest->saturate = src->saturate;
335 }
336
337 bool
nir_alu_src_is_trivial_ssa(const nir_alu_instr * alu,unsigned srcn)338 nir_alu_src_is_trivial_ssa(const nir_alu_instr *alu, unsigned srcn)
339 {
340 static uint8_t trivial_swizzle[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
341 STATIC_ASSERT(ARRAY_SIZE(trivial_swizzle) == NIR_MAX_VEC_COMPONENTS);
342
343 const nir_alu_src *src = &alu->src[srcn];
344 unsigned num_components = nir_ssa_alu_instr_src_components(alu, srcn);
345
346 return src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
347 !src->abs && !src->negate &&
348 (memcmp(src->swizzle, trivial_swizzle, num_components) == 0);
349 }
350
351
352 static void
cf_init(nir_cf_node * node,nir_cf_node_type type)353 cf_init(nir_cf_node *node, nir_cf_node_type type)
354 {
355 exec_node_init(&node->node);
356 node->parent = NULL;
357 node->type = type;
358 }
359
360 nir_function_impl *
nir_function_impl_create_bare(nir_shader * shader)361 nir_function_impl_create_bare(nir_shader *shader)
362 {
363 nir_function_impl *impl = ralloc(shader, nir_function_impl);
364
365 impl->function = NULL;
366
367 cf_init(&impl->cf_node, nir_cf_node_function);
368
369 exec_list_make_empty(&impl->body);
370 exec_list_make_empty(&impl->registers);
371 exec_list_make_empty(&impl->locals);
372 impl->reg_alloc = 0;
373 impl->ssa_alloc = 0;
374 impl->valid_metadata = nir_metadata_none;
375 impl->structured = true;
376
377 /* create start & end blocks */
378 nir_block *start_block = nir_block_create(shader);
379 nir_block *end_block = nir_block_create(shader);
380 start_block->cf_node.parent = &impl->cf_node;
381 end_block->cf_node.parent = &impl->cf_node;
382 impl->end_block = end_block;
383
384 exec_list_push_tail(&impl->body, &start_block->cf_node.node);
385
386 start_block->successors[0] = end_block;
387 _mesa_set_add(end_block->predecessors, start_block);
388 return impl;
389 }
390
391 nir_function_impl *
nir_function_impl_create(nir_function * function)392 nir_function_impl_create(nir_function *function)
393 {
394 assert(function->impl == NULL);
395
396 nir_function_impl *impl = nir_function_impl_create_bare(function->shader);
397
398 function->impl = impl;
399 impl->function = function;
400
401 return impl;
402 }
403
404 nir_block *
nir_block_create(nir_shader * shader)405 nir_block_create(nir_shader *shader)
406 {
407 nir_block *block = rzalloc(shader, nir_block);
408
409 cf_init(&block->cf_node, nir_cf_node_block);
410
411 block->successors[0] = block->successors[1] = NULL;
412 block->predecessors = _mesa_pointer_set_create(block);
413 block->imm_dom = NULL;
414 /* XXX maybe it would be worth it to defer allocation? This
415 * way it doesn't get allocated for shader refs that never run
416 * nir_calc_dominance? For example, state-tracker creates an
417 * initial IR, clones that, runs appropriate lowering pass, passes
418 * to driver which does common lowering/opt, and then stores ref
419 * which is later used to do state specific lowering and futher
420 * opt. Do any of the references not need dominance metadata?
421 */
422 block->dom_frontier = _mesa_pointer_set_create(block);
423
424 exec_list_make_empty(&block->instr_list);
425
426 return block;
427 }
428
429 static inline void
src_init(nir_src * src)430 src_init(nir_src *src)
431 {
432 src->is_ssa = false;
433 src->reg.reg = NULL;
434 src->reg.indirect = NULL;
435 src->reg.base_offset = 0;
436 }
437
438 nir_if *
nir_if_create(nir_shader * shader)439 nir_if_create(nir_shader *shader)
440 {
441 nir_if *if_stmt = ralloc(shader, nir_if);
442
443 if_stmt->control = nir_selection_control_none;
444
445 cf_init(&if_stmt->cf_node, nir_cf_node_if);
446 src_init(&if_stmt->condition);
447
448 nir_block *then = nir_block_create(shader);
449 exec_list_make_empty(&if_stmt->then_list);
450 exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
451 then->cf_node.parent = &if_stmt->cf_node;
452
453 nir_block *else_stmt = nir_block_create(shader);
454 exec_list_make_empty(&if_stmt->else_list);
455 exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
456 else_stmt->cf_node.parent = &if_stmt->cf_node;
457
458 return if_stmt;
459 }
460
461 nir_loop *
nir_loop_create(nir_shader * shader)462 nir_loop_create(nir_shader *shader)
463 {
464 nir_loop *loop = rzalloc(shader, nir_loop);
465
466 cf_init(&loop->cf_node, nir_cf_node_loop);
467
468 nir_block *body = nir_block_create(shader);
469 exec_list_make_empty(&loop->body);
470 exec_list_push_tail(&loop->body, &body->cf_node.node);
471 body->cf_node.parent = &loop->cf_node;
472
473 body->successors[0] = body;
474 _mesa_set_add(body->predecessors, body);
475
476 return loop;
477 }
478
479 static void
instr_init(nir_instr * instr,nir_instr_type type)480 instr_init(nir_instr *instr, nir_instr_type type)
481 {
482 instr->type = type;
483 instr->block = NULL;
484 exec_node_init(&instr->node);
485 }
486
487 static void
dest_init(nir_dest * dest)488 dest_init(nir_dest *dest)
489 {
490 dest->is_ssa = false;
491 dest->reg.reg = NULL;
492 dest->reg.indirect = NULL;
493 dest->reg.base_offset = 0;
494 }
495
496 static void
alu_dest_init(nir_alu_dest * dest)497 alu_dest_init(nir_alu_dest *dest)
498 {
499 dest_init(&dest->dest);
500 dest->saturate = false;
501 dest->write_mask = 0xf;
502 }
503
504 static void
alu_src_init(nir_alu_src * src)505 alu_src_init(nir_alu_src *src)
506 {
507 src_init(&src->src);
508 src->abs = src->negate = false;
509 for (int i = 0; i < NIR_MAX_VEC_COMPONENTS; ++i)
510 src->swizzle[i] = i;
511 }
512
513 nir_alu_instr *
nir_alu_instr_create(nir_shader * shader,nir_op op)514 nir_alu_instr_create(nir_shader *shader, nir_op op)
515 {
516 unsigned num_srcs = nir_op_infos[op].num_inputs;
517 /* TODO: don't use rzalloc */
518 nir_alu_instr *instr =
519 rzalloc_size(shader,
520 sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
521
522 instr_init(&instr->instr, nir_instr_type_alu);
523 instr->op = op;
524 alu_dest_init(&instr->dest);
525 for (unsigned i = 0; i < num_srcs; i++)
526 alu_src_init(&instr->src[i]);
527
528 return instr;
529 }
530
531 nir_deref_instr *
nir_deref_instr_create(nir_shader * shader,nir_deref_type deref_type)532 nir_deref_instr_create(nir_shader *shader, nir_deref_type deref_type)
533 {
534 nir_deref_instr *instr =
535 rzalloc_size(shader, sizeof(nir_deref_instr));
536
537 instr_init(&instr->instr, nir_instr_type_deref);
538
539 instr->deref_type = deref_type;
540 if (deref_type != nir_deref_type_var)
541 src_init(&instr->parent);
542
543 if (deref_type == nir_deref_type_array ||
544 deref_type == nir_deref_type_ptr_as_array)
545 src_init(&instr->arr.index);
546
547 dest_init(&instr->dest);
548
549 return instr;
550 }
551
552 nir_jump_instr *
nir_jump_instr_create(nir_shader * shader,nir_jump_type type)553 nir_jump_instr_create(nir_shader *shader, nir_jump_type type)
554 {
555 nir_jump_instr *instr = ralloc(shader, nir_jump_instr);
556 instr_init(&instr->instr, nir_instr_type_jump);
557 src_init(&instr->condition);
558 instr->type = type;
559 instr->target = NULL;
560 instr->else_target = NULL;
561 return instr;
562 }
563
564 nir_load_const_instr *
nir_load_const_instr_create(nir_shader * shader,unsigned num_components,unsigned bit_size)565 nir_load_const_instr_create(nir_shader *shader, unsigned num_components,
566 unsigned bit_size)
567 {
568 nir_load_const_instr *instr =
569 rzalloc_size(shader, sizeof(*instr) + num_components * sizeof(*instr->value));
570 instr_init(&instr->instr, nir_instr_type_load_const);
571
572 nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
573
574 return instr;
575 }
576
577 nir_intrinsic_instr *
nir_intrinsic_instr_create(nir_shader * shader,nir_intrinsic_op op)578 nir_intrinsic_instr_create(nir_shader *shader, nir_intrinsic_op op)
579 {
580 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
581 /* TODO: don't use rzalloc */
582 nir_intrinsic_instr *instr =
583 rzalloc_size(shader,
584 sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
585
586 instr_init(&instr->instr, nir_instr_type_intrinsic);
587 instr->intrinsic = op;
588
589 if (nir_intrinsic_infos[op].has_dest)
590 dest_init(&instr->dest);
591
592 for (unsigned i = 0; i < num_srcs; i++)
593 src_init(&instr->src[i]);
594
595 return instr;
596 }
597
598 nir_call_instr *
nir_call_instr_create(nir_shader * shader,nir_function * callee)599 nir_call_instr_create(nir_shader *shader, nir_function *callee)
600 {
601 const unsigned num_params = callee->num_params;
602 nir_call_instr *instr =
603 rzalloc_size(shader, sizeof(*instr) +
604 num_params * sizeof(instr->params[0]));
605
606 instr_init(&instr->instr, nir_instr_type_call);
607 instr->callee = callee;
608 instr->num_params = num_params;
609 for (unsigned i = 0; i < num_params; i++)
610 src_init(&instr->params[i]);
611
612 return instr;
613 }
614
615 static int8_t default_tg4_offsets[4][2] =
616 {
617 { 0, 1 },
618 { 1, 1 },
619 { 1, 0 },
620 { 0, 0 },
621 };
622
623 nir_tex_instr *
nir_tex_instr_create(nir_shader * shader,unsigned num_srcs)624 nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
625 {
626 nir_tex_instr *instr = rzalloc(shader, nir_tex_instr);
627 instr_init(&instr->instr, nir_instr_type_tex);
628
629 dest_init(&instr->dest);
630
631 instr->num_srcs = num_srcs;
632 instr->src = ralloc_array(instr, nir_tex_src, num_srcs);
633 for (unsigned i = 0; i < num_srcs; i++)
634 src_init(&instr->src[i].src);
635
636 instr->texture_index = 0;
637 instr->sampler_index = 0;
638 memcpy(instr->tg4_offsets, default_tg4_offsets, sizeof(instr->tg4_offsets));
639
640 return instr;
641 }
642
643 void
nir_tex_instr_add_src(nir_tex_instr * tex,nir_tex_src_type src_type,nir_src src)644 nir_tex_instr_add_src(nir_tex_instr *tex,
645 nir_tex_src_type src_type,
646 nir_src src)
647 {
648 nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
649 tex->num_srcs + 1);
650
651 for (unsigned i = 0; i < tex->num_srcs; i++) {
652 new_srcs[i].src_type = tex->src[i].src_type;
653 nir_instr_move_src(&tex->instr, &new_srcs[i].src,
654 &tex->src[i].src);
655 }
656
657 ralloc_free(tex->src);
658 tex->src = new_srcs;
659
660 tex->src[tex->num_srcs].src_type = src_type;
661 nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
662 tex->num_srcs++;
663 }
664
665 void
nir_tex_instr_remove_src(nir_tex_instr * tex,unsigned src_idx)666 nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
667 {
668 assert(src_idx < tex->num_srcs);
669
670 /* First rewrite the source to NIR_SRC_INIT */
671 nir_instr_rewrite_src(&tex->instr, &tex->src[src_idx].src, NIR_SRC_INIT);
672
673 /* Now, move all of the other sources down */
674 for (unsigned i = src_idx + 1; i < tex->num_srcs; i++) {
675 tex->src[i-1].src_type = tex->src[i].src_type;
676 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
677 }
678 tex->num_srcs--;
679 }
680
681 bool
nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr * tex)682 nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr *tex)
683 {
684 if (tex->op != nir_texop_tg4)
685 return false;
686 return memcmp(tex->tg4_offsets, default_tg4_offsets,
687 sizeof(tex->tg4_offsets)) != 0;
688 }
689
690 nir_phi_instr *
nir_phi_instr_create(nir_shader * shader)691 nir_phi_instr_create(nir_shader *shader)
692 {
693 nir_phi_instr *instr = ralloc(shader, nir_phi_instr);
694 instr_init(&instr->instr, nir_instr_type_phi);
695
696 dest_init(&instr->dest);
697 exec_list_make_empty(&instr->srcs);
698 return instr;
699 }
700
701 nir_parallel_copy_instr *
nir_parallel_copy_instr_create(nir_shader * shader)702 nir_parallel_copy_instr_create(nir_shader *shader)
703 {
704 nir_parallel_copy_instr *instr = ralloc(shader, nir_parallel_copy_instr);
705 instr_init(&instr->instr, nir_instr_type_parallel_copy);
706
707 exec_list_make_empty(&instr->entries);
708
709 return instr;
710 }
711
712 nir_ssa_undef_instr *
nir_ssa_undef_instr_create(nir_shader * shader,unsigned num_components,unsigned bit_size)713 nir_ssa_undef_instr_create(nir_shader *shader,
714 unsigned num_components,
715 unsigned bit_size)
716 {
717 nir_ssa_undef_instr *instr = ralloc(shader, nir_ssa_undef_instr);
718 instr_init(&instr->instr, nir_instr_type_ssa_undef);
719
720 nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
721
722 return instr;
723 }
724
725 static nir_const_value
const_value_float(double d,unsigned bit_size)726 const_value_float(double d, unsigned bit_size)
727 {
728 nir_const_value v;
729 memset(&v, 0, sizeof(v));
730 switch (bit_size) {
731 case 16: v.u16 = _mesa_float_to_half(d); break;
732 case 32: v.f32 = d; break;
733 case 64: v.f64 = d; break;
734 default:
735 unreachable("Invalid bit size");
736 }
737 return v;
738 }
739
740 static nir_const_value
const_value_int(int64_t i,unsigned bit_size)741 const_value_int(int64_t i, unsigned bit_size)
742 {
743 nir_const_value v;
744 memset(&v, 0, sizeof(v));
745 switch (bit_size) {
746 case 1: v.b = i & 1; break;
747 case 8: v.i8 = i; break;
748 case 16: v.i16 = i; break;
749 case 32: v.i32 = i; break;
750 case 64: v.i64 = i; break;
751 default:
752 unreachable("Invalid bit size");
753 }
754 return v;
755 }
756
757 nir_const_value
nir_alu_binop_identity(nir_op binop,unsigned bit_size)758 nir_alu_binop_identity(nir_op binop, unsigned bit_size)
759 {
760 const int64_t max_int = (1ull << (bit_size - 1)) - 1;
761 const int64_t min_int = -max_int - 1;
762 switch (binop) {
763 case nir_op_iadd:
764 return const_value_int(0, bit_size);
765 case nir_op_fadd:
766 return const_value_float(0, bit_size);
767 case nir_op_imul:
768 return const_value_int(1, bit_size);
769 case nir_op_fmul:
770 return const_value_float(1, bit_size);
771 case nir_op_imin:
772 return const_value_int(max_int, bit_size);
773 case nir_op_umin:
774 return const_value_int(~0ull, bit_size);
775 case nir_op_fmin:
776 return const_value_float(INFINITY, bit_size);
777 case nir_op_imax:
778 return const_value_int(min_int, bit_size);
779 case nir_op_umax:
780 return const_value_int(0, bit_size);
781 case nir_op_fmax:
782 return const_value_float(-INFINITY, bit_size);
783 case nir_op_iand:
784 return const_value_int(~0ull, bit_size);
785 case nir_op_ior:
786 return const_value_int(0, bit_size);
787 case nir_op_ixor:
788 return const_value_int(0, bit_size);
789 default:
790 unreachable("Invalid reduction operation");
791 }
792 }
793
794 nir_function_impl *
nir_cf_node_get_function(nir_cf_node * node)795 nir_cf_node_get_function(nir_cf_node *node)
796 {
797 while (node->type != nir_cf_node_function) {
798 node = node->parent;
799 }
800
801 return nir_cf_node_as_function(node);
802 }
803
804 /* Reduces a cursor by trying to convert everything to after and trying to
805 * go up to block granularity when possible.
806 */
807 static nir_cursor
reduce_cursor(nir_cursor cursor)808 reduce_cursor(nir_cursor cursor)
809 {
810 switch (cursor.option) {
811 case nir_cursor_before_block:
812 if (exec_list_is_empty(&cursor.block->instr_list)) {
813 /* Empty block. After is as good as before. */
814 cursor.option = nir_cursor_after_block;
815 }
816 return cursor;
817
818 case nir_cursor_after_block:
819 return cursor;
820
821 case nir_cursor_before_instr: {
822 nir_instr *prev_instr = nir_instr_prev(cursor.instr);
823 if (prev_instr) {
824 /* Before this instruction is after the previous */
825 cursor.instr = prev_instr;
826 cursor.option = nir_cursor_after_instr;
827 } else {
828 /* No previous instruction. Switch to before block */
829 cursor.block = cursor.instr->block;
830 cursor.option = nir_cursor_before_block;
831 }
832 return reduce_cursor(cursor);
833 }
834
835 case nir_cursor_after_instr:
836 if (nir_instr_next(cursor.instr) == NULL) {
837 /* This is the last instruction, switch to after block */
838 cursor.option = nir_cursor_after_block;
839 cursor.block = cursor.instr->block;
840 }
841 return cursor;
842
843 default:
844 unreachable("Inavlid cursor option");
845 }
846 }
847
848 bool
nir_cursors_equal(nir_cursor a,nir_cursor b)849 nir_cursors_equal(nir_cursor a, nir_cursor b)
850 {
851 /* Reduced cursors should be unique */
852 a = reduce_cursor(a);
853 b = reduce_cursor(b);
854
855 return a.block == b.block && a.option == b.option;
856 }
857
858 static bool
add_use_cb(nir_src * src,void * state)859 add_use_cb(nir_src *src, void *state)
860 {
861 nir_instr *instr = state;
862
863 src->parent_instr = instr;
864 list_addtail(&src->use_link,
865 src->is_ssa ? &src->ssa->uses : &src->reg.reg->uses);
866
867 return true;
868 }
869
870 static bool
add_ssa_def_cb(nir_ssa_def * def,void * state)871 add_ssa_def_cb(nir_ssa_def *def, void *state)
872 {
873 nir_instr *instr = state;
874
875 if (instr->block && def->index == UINT_MAX) {
876 nir_function_impl *impl =
877 nir_cf_node_get_function(&instr->block->cf_node);
878
879 def->index = impl->ssa_alloc++;
880
881 impl->valid_metadata &= ~nir_metadata_live_ssa_defs;
882 }
883
884 return true;
885 }
886
887 static bool
add_reg_def_cb(nir_dest * dest,void * state)888 add_reg_def_cb(nir_dest *dest, void *state)
889 {
890 nir_instr *instr = state;
891
892 if (!dest->is_ssa) {
893 dest->reg.parent_instr = instr;
894 list_addtail(&dest->reg.def_link, &dest->reg.reg->defs);
895 }
896
897 return true;
898 }
899
900 static void
add_defs_uses(nir_instr * instr)901 add_defs_uses(nir_instr *instr)
902 {
903 nir_foreach_src(instr, add_use_cb, instr);
904 nir_foreach_dest(instr, add_reg_def_cb, instr);
905 nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
906 }
907
908 void
nir_instr_insert(nir_cursor cursor,nir_instr * instr)909 nir_instr_insert(nir_cursor cursor, nir_instr *instr)
910 {
911 switch (cursor.option) {
912 case nir_cursor_before_block:
913 /* Only allow inserting jumps into empty blocks. */
914 if (instr->type == nir_instr_type_jump)
915 assert(exec_list_is_empty(&cursor.block->instr_list));
916
917 instr->block = cursor.block;
918 add_defs_uses(instr);
919 exec_list_push_head(&cursor.block->instr_list, &instr->node);
920 break;
921 case nir_cursor_after_block: {
922 /* Inserting instructions after a jump is illegal. */
923 nir_instr *last = nir_block_last_instr(cursor.block);
924 assert(last == NULL || last->type != nir_instr_type_jump);
925 (void) last;
926
927 instr->block = cursor.block;
928 add_defs_uses(instr);
929 exec_list_push_tail(&cursor.block->instr_list, &instr->node);
930 break;
931 }
932 case nir_cursor_before_instr:
933 assert(instr->type != nir_instr_type_jump);
934 instr->block = cursor.instr->block;
935 add_defs_uses(instr);
936 exec_node_insert_node_before(&cursor.instr->node, &instr->node);
937 break;
938 case nir_cursor_after_instr:
939 /* Inserting instructions after a jump is illegal. */
940 assert(cursor.instr->type != nir_instr_type_jump);
941
942 /* Only allow inserting jumps at the end of the block. */
943 if (instr->type == nir_instr_type_jump)
944 assert(cursor.instr == nir_block_last_instr(cursor.instr->block));
945
946 instr->block = cursor.instr->block;
947 add_defs_uses(instr);
948 exec_node_insert_after(&cursor.instr->node, &instr->node);
949 break;
950 }
951
952 if (instr->type == nir_instr_type_jump)
953 nir_handle_add_jump(instr->block);
954
955 nir_function_impl *impl = nir_cf_node_get_function(&instr->block->cf_node);
956 impl->valid_metadata &= ~nir_metadata_instr_index;
957 }
958
959 static bool
src_is_valid(const nir_src * src)960 src_is_valid(const nir_src *src)
961 {
962 return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
963 }
964
965 static bool
remove_use_cb(nir_src * src,void * state)966 remove_use_cb(nir_src *src, void *state)
967 {
968 (void) state;
969
970 if (src_is_valid(src))
971 list_del(&src->use_link);
972
973 return true;
974 }
975
976 static bool
remove_def_cb(nir_dest * dest,void * state)977 remove_def_cb(nir_dest *dest, void *state)
978 {
979 (void) state;
980
981 if (!dest->is_ssa)
982 list_del(&dest->reg.def_link);
983
984 return true;
985 }
986
987 static void
remove_defs_uses(nir_instr * instr)988 remove_defs_uses(nir_instr *instr)
989 {
990 nir_foreach_dest(instr, remove_def_cb, instr);
991 nir_foreach_src(instr, remove_use_cb, instr);
992 }
993
nir_instr_remove_v(nir_instr * instr)994 void nir_instr_remove_v(nir_instr *instr)
995 {
996 remove_defs_uses(instr);
997 exec_node_remove(&instr->node);
998
999 if (instr->type == nir_instr_type_jump) {
1000 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
1001 nir_handle_remove_jump(instr->block, jump_instr->type);
1002 }
1003 }
1004
1005 /*@}*/
1006
1007 void
nir_index_local_regs(nir_function_impl * impl)1008 nir_index_local_regs(nir_function_impl *impl)
1009 {
1010 unsigned index = 0;
1011 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1012 reg->index = index++;
1013 }
1014 impl->reg_alloc = index;
1015 }
1016
1017 static bool
visit_alu_dest(nir_alu_instr * instr,nir_foreach_dest_cb cb,void * state)1018 visit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
1019 {
1020 return cb(&instr->dest.dest, state);
1021 }
1022
1023 static bool
visit_deref_dest(nir_deref_instr * instr,nir_foreach_dest_cb cb,void * state)1024 visit_deref_dest(nir_deref_instr *instr, nir_foreach_dest_cb cb, void *state)
1025 {
1026 return cb(&instr->dest, state);
1027 }
1028
1029 static bool
visit_intrinsic_dest(nir_intrinsic_instr * instr,nir_foreach_dest_cb cb,void * state)1030 visit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
1031 void *state)
1032 {
1033 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1034 return cb(&instr->dest, state);
1035
1036 return true;
1037 }
1038
1039 static bool
visit_texture_dest(nir_tex_instr * instr,nir_foreach_dest_cb cb,void * state)1040 visit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
1041 void *state)
1042 {
1043 return cb(&instr->dest, state);
1044 }
1045
1046 static bool
visit_phi_dest(nir_phi_instr * instr,nir_foreach_dest_cb cb,void * state)1047 visit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
1048 {
1049 return cb(&instr->dest, state);
1050 }
1051
1052 static bool
visit_parallel_copy_dest(nir_parallel_copy_instr * instr,nir_foreach_dest_cb cb,void * state)1053 visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
1054 nir_foreach_dest_cb cb, void *state)
1055 {
1056 nir_foreach_parallel_copy_entry(entry, instr) {
1057 if (!cb(&entry->dest, state))
1058 return false;
1059 }
1060
1061 return true;
1062 }
1063
1064 bool
nir_foreach_dest(nir_instr * instr,nir_foreach_dest_cb cb,void * state)1065 nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
1066 {
1067 switch (instr->type) {
1068 case nir_instr_type_alu:
1069 return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
1070 case nir_instr_type_deref:
1071 return visit_deref_dest(nir_instr_as_deref(instr), cb, state);
1072 case nir_instr_type_intrinsic:
1073 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
1074 case nir_instr_type_tex:
1075 return visit_texture_dest(nir_instr_as_tex(instr), cb, state);
1076 case nir_instr_type_phi:
1077 return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
1078 case nir_instr_type_parallel_copy:
1079 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
1080 cb, state);
1081
1082 case nir_instr_type_load_const:
1083 case nir_instr_type_ssa_undef:
1084 case nir_instr_type_call:
1085 case nir_instr_type_jump:
1086 break;
1087
1088 default:
1089 unreachable("Invalid instruction type");
1090 break;
1091 }
1092
1093 return true;
1094 }
1095
1096 struct foreach_ssa_def_state {
1097 nir_foreach_ssa_def_cb cb;
1098 void *client_state;
1099 };
1100
1101 static inline bool
nir_ssa_def_visitor(nir_dest * dest,void * void_state)1102 nir_ssa_def_visitor(nir_dest *dest, void *void_state)
1103 {
1104 struct foreach_ssa_def_state *state = void_state;
1105
1106 if (dest->is_ssa)
1107 return state->cb(&dest->ssa, state->client_state);
1108 else
1109 return true;
1110 }
1111
1112 bool
nir_foreach_ssa_def(nir_instr * instr,nir_foreach_ssa_def_cb cb,void * state)1113 nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
1114 {
1115 switch (instr->type) {
1116 case nir_instr_type_alu:
1117 case nir_instr_type_deref:
1118 case nir_instr_type_tex:
1119 case nir_instr_type_intrinsic:
1120 case nir_instr_type_phi:
1121 case nir_instr_type_parallel_copy: {
1122 struct foreach_ssa_def_state foreach_state = {cb, state};
1123 return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
1124 }
1125
1126 case nir_instr_type_load_const:
1127 return cb(&nir_instr_as_load_const(instr)->def, state);
1128 case nir_instr_type_ssa_undef:
1129 return cb(&nir_instr_as_ssa_undef(instr)->def, state);
1130 case nir_instr_type_call:
1131 case nir_instr_type_jump:
1132 return true;
1133 default:
1134 unreachable("Invalid instruction type");
1135 }
1136 }
1137
1138 nir_ssa_def *
nir_instr_ssa_def(nir_instr * instr)1139 nir_instr_ssa_def(nir_instr *instr)
1140 {
1141 switch (instr->type) {
1142 case nir_instr_type_alu:
1143 assert(nir_instr_as_alu(instr)->dest.dest.is_ssa);
1144 return &nir_instr_as_alu(instr)->dest.dest.ssa;
1145
1146 case nir_instr_type_deref:
1147 assert(nir_instr_as_deref(instr)->dest.is_ssa);
1148 return &nir_instr_as_deref(instr)->dest.ssa;
1149
1150 case nir_instr_type_tex:
1151 assert(nir_instr_as_tex(instr)->dest.is_ssa);
1152 return &nir_instr_as_tex(instr)->dest.ssa;
1153
1154 case nir_instr_type_intrinsic: {
1155 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1156 if (nir_intrinsic_infos[intrin->intrinsic].has_dest) {
1157 assert(intrin->dest.is_ssa);
1158 return &intrin->dest.ssa;
1159 } else {
1160 return NULL;
1161 }
1162 }
1163
1164 case nir_instr_type_phi:
1165 assert(nir_instr_as_phi(instr)->dest.is_ssa);
1166 return &nir_instr_as_phi(instr)->dest.ssa;
1167
1168 case nir_instr_type_parallel_copy:
1169 unreachable("Parallel copies are unsupported by this function");
1170
1171 case nir_instr_type_load_const:
1172 return &nir_instr_as_load_const(instr)->def;
1173
1174 case nir_instr_type_ssa_undef:
1175 return &nir_instr_as_ssa_undef(instr)->def;
1176
1177 case nir_instr_type_call:
1178 case nir_instr_type_jump:
1179 return NULL;
1180 }
1181
1182 unreachable("Invalid instruction type");
1183 }
1184
1185 static bool
visit_src(nir_src * src,nir_foreach_src_cb cb,void * state)1186 visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
1187 {
1188 if (!cb(src, state))
1189 return false;
1190 if (!src->is_ssa && src->reg.indirect)
1191 return cb(src->reg.indirect, state);
1192 return true;
1193 }
1194
1195 static bool
visit_alu_src(nir_alu_instr * instr,nir_foreach_src_cb cb,void * state)1196 visit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
1197 {
1198 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1199 if (!visit_src(&instr->src[i].src, cb, state))
1200 return false;
1201
1202 return true;
1203 }
1204
1205 static bool
visit_deref_instr_src(nir_deref_instr * instr,nir_foreach_src_cb cb,void * state)1206 visit_deref_instr_src(nir_deref_instr *instr,
1207 nir_foreach_src_cb cb, void *state)
1208 {
1209 if (instr->deref_type != nir_deref_type_var) {
1210 if (!visit_src(&instr->parent, cb, state))
1211 return false;
1212 }
1213
1214 if (instr->deref_type == nir_deref_type_array ||
1215 instr->deref_type == nir_deref_type_ptr_as_array) {
1216 if (!visit_src(&instr->arr.index, cb, state))
1217 return false;
1218 }
1219
1220 return true;
1221 }
1222
1223 static bool
visit_tex_src(nir_tex_instr * instr,nir_foreach_src_cb cb,void * state)1224 visit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
1225 {
1226 for (unsigned i = 0; i < instr->num_srcs; i++) {
1227 if (!visit_src(&instr->src[i].src, cb, state))
1228 return false;
1229 }
1230
1231 return true;
1232 }
1233
1234 static bool
visit_intrinsic_src(nir_intrinsic_instr * instr,nir_foreach_src_cb cb,void * state)1235 visit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
1236 void *state)
1237 {
1238 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
1239 for (unsigned i = 0; i < num_srcs; i++) {
1240 if (!visit_src(&instr->src[i], cb, state))
1241 return false;
1242 }
1243
1244 return true;
1245 }
1246
1247 static bool
visit_call_src(nir_call_instr * instr,nir_foreach_src_cb cb,void * state)1248 visit_call_src(nir_call_instr *instr, nir_foreach_src_cb cb, void *state)
1249 {
1250 for (unsigned i = 0; i < instr->num_params; i++) {
1251 if (!visit_src(&instr->params[i], cb, state))
1252 return false;
1253 }
1254
1255 return true;
1256 }
1257
1258 static bool
visit_phi_src(nir_phi_instr * instr,nir_foreach_src_cb cb,void * state)1259 visit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
1260 {
1261 nir_foreach_phi_src(src, instr) {
1262 if (!visit_src(&src->src, cb, state))
1263 return false;
1264 }
1265
1266 return true;
1267 }
1268
1269 static bool
visit_parallel_copy_src(nir_parallel_copy_instr * instr,nir_foreach_src_cb cb,void * state)1270 visit_parallel_copy_src(nir_parallel_copy_instr *instr,
1271 nir_foreach_src_cb cb, void *state)
1272 {
1273 nir_foreach_parallel_copy_entry(entry, instr) {
1274 if (!visit_src(&entry->src, cb, state))
1275 return false;
1276 }
1277
1278 return true;
1279 }
1280
1281 static bool
visit_jump_src(nir_jump_instr * instr,nir_foreach_src_cb cb,void * state)1282 visit_jump_src(nir_jump_instr *instr, nir_foreach_src_cb cb, void *state)
1283 {
1284 if (instr->type != nir_jump_goto_if)
1285 return true;
1286
1287 return visit_src(&instr->condition, cb, state);
1288 }
1289
1290 typedef struct {
1291 void *state;
1292 nir_foreach_src_cb cb;
1293 } visit_dest_indirect_state;
1294
1295 static bool
visit_dest_indirect(nir_dest * dest,void * _state)1296 visit_dest_indirect(nir_dest *dest, void *_state)
1297 {
1298 visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
1299
1300 if (!dest->is_ssa && dest->reg.indirect)
1301 return state->cb(dest->reg.indirect, state->state);
1302
1303 return true;
1304 }
1305
1306 bool
nir_foreach_src(nir_instr * instr,nir_foreach_src_cb cb,void * state)1307 nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
1308 {
1309 switch (instr->type) {
1310 case nir_instr_type_alu:
1311 if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
1312 return false;
1313 break;
1314 case nir_instr_type_deref:
1315 if (!visit_deref_instr_src(nir_instr_as_deref(instr), cb, state))
1316 return false;
1317 break;
1318 case nir_instr_type_intrinsic:
1319 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
1320 return false;
1321 break;
1322 case nir_instr_type_tex:
1323 if (!visit_tex_src(nir_instr_as_tex(instr), cb, state))
1324 return false;
1325 break;
1326 case nir_instr_type_call:
1327 if (!visit_call_src(nir_instr_as_call(instr), cb, state))
1328 return false;
1329 break;
1330 case nir_instr_type_load_const:
1331 /* Constant load instructions have no regular sources */
1332 break;
1333 case nir_instr_type_phi:
1334 if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
1335 return false;
1336 break;
1337 case nir_instr_type_parallel_copy:
1338 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
1339 cb, state))
1340 return false;
1341 break;
1342 case nir_instr_type_jump:
1343 return visit_jump_src(nir_instr_as_jump(instr), cb, state);
1344 case nir_instr_type_ssa_undef:
1345 return true;
1346
1347 default:
1348 unreachable("Invalid instruction type");
1349 break;
1350 }
1351
1352 visit_dest_indirect_state dest_state;
1353 dest_state.state = state;
1354 dest_state.cb = cb;
1355 return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
1356 }
1357
1358 bool
nir_foreach_phi_src_leaving_block(nir_block * block,nir_foreach_src_cb cb,void * state)1359 nir_foreach_phi_src_leaving_block(nir_block *block,
1360 nir_foreach_src_cb cb,
1361 void *state)
1362 {
1363 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
1364 if (block->successors[i] == NULL)
1365 continue;
1366
1367 nir_foreach_instr(instr, block->successors[i]) {
1368 if (instr->type != nir_instr_type_phi)
1369 break;
1370
1371 nir_phi_instr *phi = nir_instr_as_phi(instr);
1372 nir_foreach_phi_src(phi_src, phi) {
1373 if (phi_src->pred == block) {
1374 if (!cb(&phi_src->src, state))
1375 return false;
1376 }
1377 }
1378 }
1379 }
1380
1381 return true;
1382 }
1383
1384 nir_const_value
nir_const_value_for_float(double f,unsigned bit_size)1385 nir_const_value_for_float(double f, unsigned bit_size)
1386 {
1387 nir_const_value v;
1388 memset(&v, 0, sizeof(v));
1389
1390 switch (bit_size) {
1391 case 16:
1392 v.u16 = _mesa_float_to_half(f);
1393 break;
1394 case 32:
1395 v.f32 = f;
1396 break;
1397 case 64:
1398 v.f64 = f;
1399 break;
1400 default:
1401 unreachable("Invalid bit size");
1402 }
1403
1404 return v;
1405 }
1406
1407 double
nir_const_value_as_float(nir_const_value value,unsigned bit_size)1408 nir_const_value_as_float(nir_const_value value, unsigned bit_size)
1409 {
1410 switch (bit_size) {
1411 case 16: return _mesa_half_to_float(value.u16);
1412 case 32: return value.f32;
1413 case 64: return value.f64;
1414 default:
1415 unreachable("Invalid bit size");
1416 }
1417 }
1418
1419 nir_const_value *
nir_src_as_const_value(nir_src src)1420 nir_src_as_const_value(nir_src src)
1421 {
1422 if (!src.is_ssa)
1423 return NULL;
1424
1425 if (src.ssa->parent_instr->type != nir_instr_type_load_const)
1426 return NULL;
1427
1428 nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1429
1430 return load->value;
1431 }
1432
1433 /**
1434 * Returns true if the source is known to be dynamically uniform. Otherwise it
1435 * returns false which means it may or may not be dynamically uniform but it
1436 * can't be determined.
1437 */
1438 bool
nir_src_is_dynamically_uniform(nir_src src)1439 nir_src_is_dynamically_uniform(nir_src src)
1440 {
1441 if (!src.is_ssa)
1442 return false;
1443
1444 /* Constants are trivially dynamically uniform */
1445 if (src.ssa->parent_instr->type == nir_instr_type_load_const)
1446 return true;
1447
1448 /* As are uniform variables */
1449 if (src.ssa->parent_instr->type == nir_instr_type_intrinsic) {
1450 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(src.ssa->parent_instr);
1451 if (intr->intrinsic == nir_intrinsic_load_uniform &&
1452 nir_src_is_dynamically_uniform(intr->src[0]))
1453 return true;
1454 }
1455
1456 /* Operating together dynamically uniform expressions produces a
1457 * dynamically uniform result
1458 */
1459 if (src.ssa->parent_instr->type == nir_instr_type_alu) {
1460 nir_alu_instr *alu = nir_instr_as_alu(src.ssa->parent_instr);
1461 for (int i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
1462 if (!nir_src_is_dynamically_uniform(alu->src[i].src))
1463 return false;
1464 }
1465
1466 return true;
1467 }
1468
1469 /* XXX: this could have many more tests, such as when a sampler function is
1470 * called with dynamically uniform arguments.
1471 */
1472 return false;
1473 }
1474
1475 static void
src_remove_all_uses(nir_src * src)1476 src_remove_all_uses(nir_src *src)
1477 {
1478 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1479 if (!src_is_valid(src))
1480 continue;
1481
1482 list_del(&src->use_link);
1483 }
1484 }
1485
1486 static void
src_add_all_uses(nir_src * src,nir_instr * parent_instr,nir_if * parent_if)1487 src_add_all_uses(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
1488 {
1489 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1490 if (!src_is_valid(src))
1491 continue;
1492
1493 if (parent_instr) {
1494 src->parent_instr = parent_instr;
1495 if (src->is_ssa)
1496 list_addtail(&src->use_link, &src->ssa->uses);
1497 else
1498 list_addtail(&src->use_link, &src->reg.reg->uses);
1499 } else {
1500 assert(parent_if);
1501 src->parent_if = parent_if;
1502 if (src->is_ssa)
1503 list_addtail(&src->use_link, &src->ssa->if_uses);
1504 else
1505 list_addtail(&src->use_link, &src->reg.reg->if_uses);
1506 }
1507 }
1508 }
1509
1510 void
nir_instr_rewrite_src(nir_instr * instr,nir_src * src,nir_src new_src)1511 nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
1512 {
1513 assert(!src_is_valid(src) || src->parent_instr == instr);
1514
1515 src_remove_all_uses(src);
1516 *src = new_src;
1517 src_add_all_uses(src, instr, NULL);
1518 }
1519
1520 void
nir_instr_move_src(nir_instr * dest_instr,nir_src * dest,nir_src * src)1521 nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
1522 {
1523 assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
1524
1525 src_remove_all_uses(dest);
1526 src_remove_all_uses(src);
1527 *dest = *src;
1528 *src = NIR_SRC_INIT;
1529 src_add_all_uses(dest, dest_instr, NULL);
1530 }
1531
1532 void
nir_if_rewrite_condition(nir_if * if_stmt,nir_src new_src)1533 nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
1534 {
1535 nir_src *src = &if_stmt->condition;
1536 assert(!src_is_valid(src) || src->parent_if == if_stmt);
1537
1538 src_remove_all_uses(src);
1539 *src = new_src;
1540 src_add_all_uses(src, NULL, if_stmt);
1541 }
1542
1543 void
nir_instr_rewrite_dest(nir_instr * instr,nir_dest * dest,nir_dest new_dest)1544 nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest)
1545 {
1546 if (dest->is_ssa) {
1547 /* We can only overwrite an SSA destination if it has no uses. */
1548 assert(list_is_empty(&dest->ssa.uses) && list_is_empty(&dest->ssa.if_uses));
1549 } else {
1550 list_del(&dest->reg.def_link);
1551 if (dest->reg.indirect)
1552 src_remove_all_uses(dest->reg.indirect);
1553 }
1554
1555 /* We can't re-write with an SSA def */
1556 assert(!new_dest.is_ssa);
1557
1558 nir_dest_copy(dest, &new_dest, instr);
1559
1560 dest->reg.parent_instr = instr;
1561 list_addtail(&dest->reg.def_link, &new_dest.reg.reg->defs);
1562
1563 if (dest->reg.indirect)
1564 src_add_all_uses(dest->reg.indirect, instr, NULL);
1565 }
1566
1567 /* note: does *not* take ownership of 'name' */
1568 void
nir_ssa_def_init(nir_instr * instr,nir_ssa_def * def,unsigned num_components,unsigned bit_size,const char * name)1569 nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1570 unsigned num_components,
1571 unsigned bit_size, const char *name)
1572 {
1573 def->name = ralloc_strdup(instr, name);
1574 def->parent_instr = instr;
1575 list_inithead(&def->uses);
1576 list_inithead(&def->if_uses);
1577 def->num_components = num_components;
1578 def->bit_size = bit_size;
1579 def->divergent = true; /* This is the safer default */
1580
1581 if (instr->block) {
1582 nir_function_impl *impl =
1583 nir_cf_node_get_function(&instr->block->cf_node);
1584
1585 def->index = impl->ssa_alloc++;
1586
1587 impl->valid_metadata &= ~nir_metadata_live_ssa_defs;
1588 } else {
1589 def->index = UINT_MAX;
1590 }
1591 }
1592
1593 /* note: does *not* take ownership of 'name' */
1594 void
nir_ssa_dest_init(nir_instr * instr,nir_dest * dest,unsigned num_components,unsigned bit_size,const char * name)1595 nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1596 unsigned num_components, unsigned bit_size,
1597 const char *name)
1598 {
1599 dest->is_ssa = true;
1600 nir_ssa_def_init(instr, &dest->ssa, num_components, bit_size, name);
1601 }
1602
1603 void
nir_ssa_def_rewrite_uses(nir_ssa_def * def,nir_src new_src)1604 nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src)
1605 {
1606 assert(!new_src.is_ssa || def != new_src.ssa);
1607
1608 nir_foreach_use_safe(use_src, def)
1609 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1610
1611 nir_foreach_if_use_safe(use_src, def)
1612 nir_if_rewrite_condition(use_src->parent_if, new_src);
1613 }
1614
1615 static bool
is_instr_between(nir_instr * start,nir_instr * end,nir_instr * between)1616 is_instr_between(nir_instr *start, nir_instr *end, nir_instr *between)
1617 {
1618 assert(start->block == end->block);
1619
1620 if (between->block != start->block)
1621 return false;
1622
1623 /* Search backwards looking for "between" */
1624 while (start != end) {
1625 if (between == end)
1626 return true;
1627
1628 end = nir_instr_prev(end);
1629 assert(end);
1630 }
1631
1632 return false;
1633 }
1634
1635 /* Replaces all uses of the given SSA def with the given source but only if
1636 * the use comes after the after_me instruction. This can be useful if you
1637 * are emitting code to fix up the result of some instruction: you can freely
1638 * use the result in that code and then call rewrite_uses_after and pass the
1639 * last fixup instruction as after_me and it will replace all of the uses you
1640 * want without touching the fixup code.
1641 *
1642 * This function assumes that after_me is in the same block as
1643 * def->parent_instr and that after_me comes after def->parent_instr.
1644 */
1645 void
nir_ssa_def_rewrite_uses_after(nir_ssa_def * def,nir_src new_src,nir_instr * after_me)1646 nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
1647 nir_instr *after_me)
1648 {
1649 if (new_src.is_ssa && def == new_src.ssa)
1650 return;
1651
1652 nir_foreach_use_safe(use_src, def) {
1653 assert(use_src->parent_instr != def->parent_instr);
1654 /* Since def already dominates all of its uses, the only way a use can
1655 * not be dominated by after_me is if it is between def and after_me in
1656 * the instruction list.
1657 */
1658 if (!is_instr_between(def->parent_instr, after_me, use_src->parent_instr))
1659 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1660 }
1661
1662 nir_foreach_if_use_safe(use_src, def)
1663 nir_if_rewrite_condition(use_src->parent_if, new_src);
1664 }
1665
1666 nir_component_mask_t
nir_ssa_def_components_read(const nir_ssa_def * def)1667 nir_ssa_def_components_read(const nir_ssa_def *def)
1668 {
1669 nir_component_mask_t read_mask = 0;
1670 nir_foreach_use(use, def) {
1671 if (use->parent_instr->type == nir_instr_type_alu) {
1672 nir_alu_instr *alu = nir_instr_as_alu(use->parent_instr);
1673 nir_alu_src *alu_src = exec_node_data(nir_alu_src, use, src);
1674 int src_idx = alu_src - &alu->src[0];
1675 assert(src_idx >= 0 && src_idx < nir_op_infos[alu->op].num_inputs);
1676 read_mask |= nir_alu_instr_src_read_mask(alu, src_idx);
1677 } else {
1678 return (1 << def->num_components) - 1;
1679 }
1680 }
1681
1682 if (!list_is_empty(&def->if_uses))
1683 read_mask |= 1;
1684
1685 return read_mask;
1686 }
1687
1688 nir_block *
nir_block_unstructured_next(nir_block * block)1689 nir_block_unstructured_next(nir_block *block)
1690 {
1691 if (block == NULL) {
1692 /* nir_foreach_block_unstructured_safe() will call this function on a
1693 * NULL block after the last iteration, but it won't use the result so
1694 * just return NULL here.
1695 */
1696 return NULL;
1697 }
1698
1699 nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
1700 if (cf_next == NULL && block->cf_node.parent->type == nir_cf_node_function)
1701 return NULL;
1702
1703 if (cf_next && cf_next->type == nir_cf_node_block)
1704 return nir_cf_node_as_block(cf_next);
1705
1706 return nir_block_cf_tree_next(block);
1707 }
1708
1709 nir_block *
nir_unstructured_start_block(nir_function_impl * impl)1710 nir_unstructured_start_block(nir_function_impl *impl)
1711 {
1712 return nir_start_block(impl);
1713 }
1714
1715 nir_block *
nir_block_cf_tree_next(nir_block * block)1716 nir_block_cf_tree_next(nir_block *block)
1717 {
1718 if (block == NULL) {
1719 /* nir_foreach_block_safe() will call this function on a NULL block
1720 * after the last iteration, but it won't use the result so just return
1721 * NULL here.
1722 */
1723 return NULL;
1724 }
1725
1726 assert(nir_cf_node_get_function(&block->cf_node)->structured);
1727
1728 nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
1729 if (cf_next)
1730 return nir_cf_node_cf_tree_first(cf_next);
1731
1732 nir_cf_node *parent = block->cf_node.parent;
1733
1734 switch (parent->type) {
1735 case nir_cf_node_if: {
1736 /* Are we at the end of the if? Go to the beginning of the else */
1737 nir_if *if_stmt = nir_cf_node_as_if(parent);
1738 if (block == nir_if_last_then_block(if_stmt))
1739 return nir_if_first_else_block(if_stmt);
1740
1741 assert(block == nir_if_last_else_block(if_stmt));
1742 }
1743 /* fallthrough */
1744
1745 case nir_cf_node_loop:
1746 return nir_cf_node_as_block(nir_cf_node_next(parent));
1747
1748 case nir_cf_node_function:
1749 return NULL;
1750
1751 default:
1752 unreachable("unknown cf node type");
1753 }
1754 }
1755
1756 nir_block *
nir_block_cf_tree_prev(nir_block * block)1757 nir_block_cf_tree_prev(nir_block *block)
1758 {
1759 if (block == NULL) {
1760 /* do this for consistency with nir_block_cf_tree_next() */
1761 return NULL;
1762 }
1763
1764 assert(nir_cf_node_get_function(&block->cf_node)->structured);
1765
1766 nir_cf_node *cf_prev = nir_cf_node_prev(&block->cf_node);
1767 if (cf_prev)
1768 return nir_cf_node_cf_tree_last(cf_prev);
1769
1770 nir_cf_node *parent = block->cf_node.parent;
1771
1772 switch (parent->type) {
1773 case nir_cf_node_if: {
1774 /* Are we at the beginning of the else? Go to the end of the if */
1775 nir_if *if_stmt = nir_cf_node_as_if(parent);
1776 if (block == nir_if_first_else_block(if_stmt))
1777 return nir_if_last_then_block(if_stmt);
1778
1779 assert(block == nir_if_first_then_block(if_stmt));
1780 }
1781 /* fallthrough */
1782
1783 case nir_cf_node_loop:
1784 return nir_cf_node_as_block(nir_cf_node_prev(parent));
1785
1786 case nir_cf_node_function:
1787 return NULL;
1788
1789 default:
1790 unreachable("unknown cf node type");
1791 }
1792 }
1793
nir_cf_node_cf_tree_first(nir_cf_node * node)1794 nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node)
1795 {
1796 switch (node->type) {
1797 case nir_cf_node_function: {
1798 nir_function_impl *impl = nir_cf_node_as_function(node);
1799 return nir_start_block(impl);
1800 }
1801
1802 case nir_cf_node_if: {
1803 nir_if *if_stmt = nir_cf_node_as_if(node);
1804 return nir_if_first_then_block(if_stmt);
1805 }
1806
1807 case nir_cf_node_loop: {
1808 nir_loop *loop = nir_cf_node_as_loop(node);
1809 return nir_loop_first_block(loop);
1810 }
1811
1812 case nir_cf_node_block: {
1813 return nir_cf_node_as_block(node);
1814 }
1815
1816 default:
1817 unreachable("unknown node type");
1818 }
1819 }
1820
nir_cf_node_cf_tree_last(nir_cf_node * node)1821 nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node)
1822 {
1823 switch (node->type) {
1824 case nir_cf_node_function: {
1825 nir_function_impl *impl = nir_cf_node_as_function(node);
1826 return nir_impl_last_block(impl);
1827 }
1828
1829 case nir_cf_node_if: {
1830 nir_if *if_stmt = nir_cf_node_as_if(node);
1831 return nir_if_last_else_block(if_stmt);
1832 }
1833
1834 case nir_cf_node_loop: {
1835 nir_loop *loop = nir_cf_node_as_loop(node);
1836 return nir_loop_last_block(loop);
1837 }
1838
1839 case nir_cf_node_block: {
1840 return nir_cf_node_as_block(node);
1841 }
1842
1843 default:
1844 unreachable("unknown node type");
1845 }
1846 }
1847
nir_cf_node_cf_tree_next(nir_cf_node * node)1848 nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node)
1849 {
1850 if (node->type == nir_cf_node_block)
1851 return nir_block_cf_tree_next(nir_cf_node_as_block(node));
1852 else if (node->type == nir_cf_node_function)
1853 return NULL;
1854 else
1855 return nir_cf_node_as_block(nir_cf_node_next(node));
1856 }
1857
1858 nir_if *
nir_block_get_following_if(nir_block * block)1859 nir_block_get_following_if(nir_block *block)
1860 {
1861 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1862 return NULL;
1863
1864 if (nir_cf_node_is_last(&block->cf_node))
1865 return NULL;
1866
1867 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1868
1869 if (next_node->type != nir_cf_node_if)
1870 return NULL;
1871
1872 return nir_cf_node_as_if(next_node);
1873 }
1874
1875 nir_loop *
nir_block_get_following_loop(nir_block * block)1876 nir_block_get_following_loop(nir_block *block)
1877 {
1878 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1879 return NULL;
1880
1881 if (nir_cf_node_is_last(&block->cf_node))
1882 return NULL;
1883
1884 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1885
1886 if (next_node->type != nir_cf_node_loop)
1887 return NULL;
1888
1889 return nir_cf_node_as_loop(next_node);
1890 }
1891
1892 void
nir_index_blocks(nir_function_impl * impl)1893 nir_index_blocks(nir_function_impl *impl)
1894 {
1895 unsigned index = 0;
1896
1897 if (impl->valid_metadata & nir_metadata_block_index)
1898 return;
1899
1900 nir_foreach_block_unstructured(block, impl) {
1901 block->index = index++;
1902 }
1903
1904 /* The end_block isn't really part of the program, which is why its index
1905 * is >= num_blocks.
1906 */
1907 impl->num_blocks = impl->end_block->index = index;
1908 }
1909
1910 static bool
index_ssa_def_cb(nir_ssa_def * def,void * state)1911 index_ssa_def_cb(nir_ssa_def *def, void *state)
1912 {
1913 unsigned *index = (unsigned *) state;
1914 def->index = (*index)++;
1915
1916 return true;
1917 }
1918
1919 /**
1920 * The indices are applied top-to-bottom which has the very nice property
1921 * that, if A dominates B, then A->index <= B->index.
1922 */
1923 void
nir_index_ssa_defs(nir_function_impl * impl)1924 nir_index_ssa_defs(nir_function_impl *impl)
1925 {
1926 unsigned index = 0;
1927
1928 impl->valid_metadata &= ~nir_metadata_live_ssa_defs;
1929
1930 nir_foreach_block_unstructured(block, impl) {
1931 nir_foreach_instr(instr, block)
1932 nir_foreach_ssa_def(instr, index_ssa_def_cb, &index);
1933 }
1934
1935 impl->ssa_alloc = index;
1936 }
1937
1938 /**
1939 * The indices are applied top-to-bottom which has the very nice property
1940 * that, if A dominates B, then A->index <= B->index.
1941 */
1942 unsigned
nir_index_instrs(nir_function_impl * impl)1943 nir_index_instrs(nir_function_impl *impl)
1944 {
1945 unsigned index = 0;
1946
1947 nir_foreach_block(block, impl) {
1948 block->start_ip = index++;
1949
1950 nir_foreach_instr(instr, block)
1951 instr->index = index++;
1952
1953 block->end_ip = index++;
1954 }
1955
1956 return index;
1957 }
1958
1959 unsigned
nir_shader_index_vars(nir_shader * shader,nir_variable_mode modes)1960 nir_shader_index_vars(nir_shader *shader, nir_variable_mode modes)
1961 {
1962 unsigned count = 0;
1963 nir_foreach_variable_with_modes(var, shader, modes)
1964 var->index = count++;
1965 return count;
1966 }
1967
1968 unsigned
nir_function_impl_index_vars(nir_function_impl * impl)1969 nir_function_impl_index_vars(nir_function_impl *impl)
1970 {
1971 unsigned count = 0;
1972 nir_foreach_function_temp_variable(var, impl)
1973 var->index = count++;
1974 return count;
1975 }
1976
1977 static nir_instr *
cursor_next_instr(nir_cursor cursor)1978 cursor_next_instr(nir_cursor cursor)
1979 {
1980 switch (cursor.option) {
1981 case nir_cursor_before_block:
1982 for (nir_block *block = cursor.block; block;
1983 block = nir_block_cf_tree_next(block)) {
1984 nir_instr *instr = nir_block_first_instr(block);
1985 if (instr)
1986 return instr;
1987 }
1988 return NULL;
1989
1990 case nir_cursor_after_block:
1991 cursor.block = nir_block_cf_tree_next(cursor.block);
1992 if (cursor.block == NULL)
1993 return NULL;
1994
1995 cursor.option = nir_cursor_before_block;
1996 return cursor_next_instr(cursor);
1997
1998 case nir_cursor_before_instr:
1999 return cursor.instr;
2000
2001 case nir_cursor_after_instr:
2002 if (nir_instr_next(cursor.instr))
2003 return nir_instr_next(cursor.instr);
2004
2005 cursor.option = nir_cursor_after_block;
2006 cursor.block = cursor.instr->block;
2007 return cursor_next_instr(cursor);
2008 }
2009
2010 unreachable("Inavlid cursor option");
2011 }
2012
2013 ASSERTED static bool
dest_is_ssa(nir_dest * dest,void * _state)2014 dest_is_ssa(nir_dest *dest, void *_state)
2015 {
2016 (void) _state;
2017 return dest->is_ssa;
2018 }
2019
2020 bool
nir_function_impl_lower_instructions(nir_function_impl * impl,nir_instr_filter_cb filter,nir_lower_instr_cb lower,void * cb_data)2021 nir_function_impl_lower_instructions(nir_function_impl *impl,
2022 nir_instr_filter_cb filter,
2023 nir_lower_instr_cb lower,
2024 void *cb_data)
2025 {
2026 nir_builder b;
2027 nir_builder_init(&b, impl);
2028
2029 nir_metadata preserved = nir_metadata_block_index |
2030 nir_metadata_dominance;
2031
2032 bool progress = false;
2033 nir_cursor iter = nir_before_cf_list(&impl->body);
2034 nir_instr *instr;
2035 while ((instr = cursor_next_instr(iter)) != NULL) {
2036 if (filter && !filter(instr, cb_data)) {
2037 iter = nir_after_instr(instr);
2038 continue;
2039 }
2040
2041 assert(nir_foreach_dest(instr, dest_is_ssa, NULL));
2042 nir_ssa_def *old_def = nir_instr_ssa_def(instr);
2043 if (old_def == NULL) {
2044 iter = nir_after_instr(instr);
2045 continue;
2046 }
2047
2048 /* We're about to ask the callback to generate a replacement for instr.
2049 * Save off the uses from instr's SSA def so we know what uses to
2050 * rewrite later. If we use nir_ssa_def_rewrite_uses, it fails in the
2051 * case where the generated replacement code uses the result of instr
2052 * itself. If we use nir_ssa_def_rewrite_uses_after (which is the
2053 * normal solution to this problem), it doesn't work well if control-
2054 * flow is inserted as part of the replacement, doesn't handle cases
2055 * where the replacement is something consumed by instr, and suffers
2056 * from performance issues. This is the only way to 100% guarantee
2057 * that we rewrite the correct set efficiently.
2058 */
2059 struct list_head old_uses, old_if_uses;
2060 list_replace(&old_def->uses, &old_uses);
2061 list_inithead(&old_def->uses);
2062 list_replace(&old_def->if_uses, &old_if_uses);
2063 list_inithead(&old_def->if_uses);
2064
2065 b.cursor = nir_after_instr(instr);
2066 nir_ssa_def *new_def = lower(&b, instr, cb_data);
2067 if (new_def && new_def != NIR_LOWER_INSTR_PROGRESS) {
2068 assert(old_def != NULL);
2069 if (new_def->parent_instr->block != instr->block)
2070 preserved = nir_metadata_none;
2071
2072 nir_src new_src = nir_src_for_ssa(new_def);
2073 list_for_each_entry_safe(nir_src, use_src, &old_uses, use_link)
2074 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
2075
2076 list_for_each_entry_safe(nir_src, use_src, &old_if_uses, use_link)
2077 nir_if_rewrite_condition(use_src->parent_if, new_src);
2078
2079 if (list_is_empty(&old_def->uses) && list_is_empty(&old_def->if_uses)) {
2080 iter = nir_instr_remove(instr);
2081 } else {
2082 iter = nir_after_instr(instr);
2083 }
2084 progress = true;
2085 } else {
2086 /* We didn't end up lowering after all. Put the uses back */
2087 if (old_def) {
2088 list_replace(&old_uses, &old_def->uses);
2089 list_replace(&old_if_uses, &old_def->if_uses);
2090 }
2091 iter = nir_after_instr(instr);
2092
2093 if (new_def == NIR_LOWER_INSTR_PROGRESS)
2094 progress = true;
2095 }
2096 }
2097
2098 if (progress) {
2099 nir_metadata_preserve(impl, preserved);
2100 } else {
2101 nir_metadata_preserve(impl, nir_metadata_all);
2102 }
2103
2104 return progress;
2105 }
2106
2107 bool
nir_shader_lower_instructions(nir_shader * shader,nir_instr_filter_cb filter,nir_lower_instr_cb lower,void * cb_data)2108 nir_shader_lower_instructions(nir_shader *shader,
2109 nir_instr_filter_cb filter,
2110 nir_lower_instr_cb lower,
2111 void *cb_data)
2112 {
2113 bool progress = false;
2114
2115 nir_foreach_function(function, shader) {
2116 if (function->impl &&
2117 nir_function_impl_lower_instructions(function->impl,
2118 filter, lower, cb_data))
2119 progress = true;
2120 }
2121
2122 return progress;
2123 }
2124
2125 nir_intrinsic_op
nir_intrinsic_from_system_value(gl_system_value val)2126 nir_intrinsic_from_system_value(gl_system_value val)
2127 {
2128 switch (val) {
2129 case SYSTEM_VALUE_VERTEX_ID:
2130 return nir_intrinsic_load_vertex_id;
2131 case SYSTEM_VALUE_INSTANCE_ID:
2132 return nir_intrinsic_load_instance_id;
2133 case SYSTEM_VALUE_DRAW_ID:
2134 return nir_intrinsic_load_draw_id;
2135 case SYSTEM_VALUE_BASE_INSTANCE:
2136 return nir_intrinsic_load_base_instance;
2137 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
2138 return nir_intrinsic_load_vertex_id_zero_base;
2139 case SYSTEM_VALUE_IS_INDEXED_DRAW:
2140 return nir_intrinsic_load_is_indexed_draw;
2141 case SYSTEM_VALUE_FIRST_VERTEX:
2142 return nir_intrinsic_load_first_vertex;
2143 case SYSTEM_VALUE_BASE_VERTEX:
2144 return nir_intrinsic_load_base_vertex;
2145 case SYSTEM_VALUE_INVOCATION_ID:
2146 return nir_intrinsic_load_invocation_id;
2147 case SYSTEM_VALUE_FRAG_COORD:
2148 return nir_intrinsic_load_frag_coord;
2149 case SYSTEM_VALUE_POINT_COORD:
2150 return nir_intrinsic_load_point_coord;
2151 case SYSTEM_VALUE_LINE_COORD:
2152 return nir_intrinsic_load_line_coord;
2153 case SYSTEM_VALUE_FRONT_FACE:
2154 return nir_intrinsic_load_front_face;
2155 case SYSTEM_VALUE_SAMPLE_ID:
2156 return nir_intrinsic_load_sample_id;
2157 case SYSTEM_VALUE_SAMPLE_POS:
2158 return nir_intrinsic_load_sample_pos;
2159 case SYSTEM_VALUE_SAMPLE_MASK_IN:
2160 return nir_intrinsic_load_sample_mask_in;
2161 case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
2162 return nir_intrinsic_load_local_invocation_id;
2163 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
2164 return nir_intrinsic_load_local_invocation_index;
2165 case SYSTEM_VALUE_WORK_GROUP_ID:
2166 return nir_intrinsic_load_work_group_id;
2167 case SYSTEM_VALUE_NUM_WORK_GROUPS:
2168 return nir_intrinsic_load_num_work_groups;
2169 case SYSTEM_VALUE_PRIMITIVE_ID:
2170 return nir_intrinsic_load_primitive_id;
2171 case SYSTEM_VALUE_TESS_COORD:
2172 return nir_intrinsic_load_tess_coord;
2173 case SYSTEM_VALUE_TESS_LEVEL_OUTER:
2174 return nir_intrinsic_load_tess_level_outer;
2175 case SYSTEM_VALUE_TESS_LEVEL_INNER:
2176 return nir_intrinsic_load_tess_level_inner;
2177 case SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT:
2178 return nir_intrinsic_load_tess_level_outer_default;
2179 case SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT:
2180 return nir_intrinsic_load_tess_level_inner_default;
2181 case SYSTEM_VALUE_VERTICES_IN:
2182 return nir_intrinsic_load_patch_vertices_in;
2183 case SYSTEM_VALUE_HELPER_INVOCATION:
2184 return nir_intrinsic_load_helper_invocation;
2185 case SYSTEM_VALUE_COLOR0:
2186 return nir_intrinsic_load_color0;
2187 case SYSTEM_VALUE_COLOR1:
2188 return nir_intrinsic_load_color1;
2189 case SYSTEM_VALUE_VIEW_INDEX:
2190 return nir_intrinsic_load_view_index;
2191 case SYSTEM_VALUE_SUBGROUP_SIZE:
2192 return nir_intrinsic_load_subgroup_size;
2193 case SYSTEM_VALUE_SUBGROUP_INVOCATION:
2194 return nir_intrinsic_load_subgroup_invocation;
2195 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
2196 return nir_intrinsic_load_subgroup_eq_mask;
2197 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
2198 return nir_intrinsic_load_subgroup_ge_mask;
2199 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
2200 return nir_intrinsic_load_subgroup_gt_mask;
2201 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
2202 return nir_intrinsic_load_subgroup_le_mask;
2203 case SYSTEM_VALUE_SUBGROUP_LT_MASK:
2204 return nir_intrinsic_load_subgroup_lt_mask;
2205 case SYSTEM_VALUE_NUM_SUBGROUPS:
2206 return nir_intrinsic_load_num_subgroups;
2207 case SYSTEM_VALUE_SUBGROUP_ID:
2208 return nir_intrinsic_load_subgroup_id;
2209 case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
2210 return nir_intrinsic_load_local_group_size;
2211 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID:
2212 return nir_intrinsic_load_global_invocation_id;
2213 case SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID:
2214 return nir_intrinsic_load_base_global_invocation_id;
2215 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX:
2216 return nir_intrinsic_load_global_invocation_index;
2217 case SYSTEM_VALUE_WORK_DIM:
2218 return nir_intrinsic_load_work_dim;
2219 case SYSTEM_VALUE_USER_DATA_AMD:
2220 return nir_intrinsic_load_user_data_amd;
2221 case SYSTEM_VALUE_RAY_LAUNCH_ID:
2222 return nir_intrinsic_load_ray_launch_id;
2223 case SYSTEM_VALUE_RAY_LAUNCH_SIZE:
2224 return nir_intrinsic_load_ray_launch_size;
2225 case SYSTEM_VALUE_RAY_WORLD_ORIGIN:
2226 return nir_intrinsic_load_ray_world_origin;
2227 case SYSTEM_VALUE_RAY_WORLD_DIRECTION:
2228 return nir_intrinsic_load_ray_world_direction;
2229 case SYSTEM_VALUE_RAY_OBJECT_ORIGIN:
2230 return nir_intrinsic_load_ray_object_origin;
2231 case SYSTEM_VALUE_RAY_OBJECT_DIRECTION:
2232 return nir_intrinsic_load_ray_object_direction;
2233 case SYSTEM_VALUE_RAY_T_MIN:
2234 return nir_intrinsic_load_ray_t_min;
2235 case SYSTEM_VALUE_RAY_T_MAX:
2236 return nir_intrinsic_load_ray_t_max;
2237 case SYSTEM_VALUE_RAY_OBJECT_TO_WORLD:
2238 return nir_intrinsic_load_ray_object_to_world;
2239 case SYSTEM_VALUE_RAY_WORLD_TO_OBJECT:
2240 return nir_intrinsic_load_ray_world_to_object;
2241 case SYSTEM_VALUE_RAY_HIT_KIND:
2242 return nir_intrinsic_load_ray_hit_kind;
2243 case SYSTEM_VALUE_RAY_FLAGS:
2244 return nir_intrinsic_load_ray_flags;
2245 case SYSTEM_VALUE_RAY_GEOMETRY_INDEX:
2246 return nir_intrinsic_load_ray_geometry_index;
2247 case SYSTEM_VALUE_RAY_INSTANCE_CUSTOM_INDEX:
2248 return nir_intrinsic_load_ray_instance_custom_index;
2249 default:
2250 unreachable("system value does not directly correspond to intrinsic");
2251 }
2252 }
2253
2254 gl_system_value
nir_system_value_from_intrinsic(nir_intrinsic_op intrin)2255 nir_system_value_from_intrinsic(nir_intrinsic_op intrin)
2256 {
2257 switch (intrin) {
2258 case nir_intrinsic_load_vertex_id:
2259 return SYSTEM_VALUE_VERTEX_ID;
2260 case nir_intrinsic_load_instance_id:
2261 return SYSTEM_VALUE_INSTANCE_ID;
2262 case nir_intrinsic_load_draw_id:
2263 return SYSTEM_VALUE_DRAW_ID;
2264 case nir_intrinsic_load_base_instance:
2265 return SYSTEM_VALUE_BASE_INSTANCE;
2266 case nir_intrinsic_load_vertex_id_zero_base:
2267 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
2268 case nir_intrinsic_load_first_vertex:
2269 return SYSTEM_VALUE_FIRST_VERTEX;
2270 case nir_intrinsic_load_is_indexed_draw:
2271 return SYSTEM_VALUE_IS_INDEXED_DRAW;
2272 case nir_intrinsic_load_base_vertex:
2273 return SYSTEM_VALUE_BASE_VERTEX;
2274 case nir_intrinsic_load_invocation_id:
2275 return SYSTEM_VALUE_INVOCATION_ID;
2276 case nir_intrinsic_load_frag_coord:
2277 return SYSTEM_VALUE_FRAG_COORD;
2278 case nir_intrinsic_load_point_coord:
2279 return SYSTEM_VALUE_POINT_COORD;
2280 case nir_intrinsic_load_line_coord:
2281 return SYSTEM_VALUE_LINE_COORD;
2282 case nir_intrinsic_load_front_face:
2283 return SYSTEM_VALUE_FRONT_FACE;
2284 case nir_intrinsic_load_sample_id:
2285 return SYSTEM_VALUE_SAMPLE_ID;
2286 case nir_intrinsic_load_sample_pos:
2287 return SYSTEM_VALUE_SAMPLE_POS;
2288 case nir_intrinsic_load_sample_mask_in:
2289 return SYSTEM_VALUE_SAMPLE_MASK_IN;
2290 case nir_intrinsic_load_local_invocation_id:
2291 return SYSTEM_VALUE_LOCAL_INVOCATION_ID;
2292 case nir_intrinsic_load_local_invocation_index:
2293 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
2294 case nir_intrinsic_load_num_work_groups:
2295 return SYSTEM_VALUE_NUM_WORK_GROUPS;
2296 case nir_intrinsic_load_work_group_id:
2297 return SYSTEM_VALUE_WORK_GROUP_ID;
2298 case nir_intrinsic_load_primitive_id:
2299 return SYSTEM_VALUE_PRIMITIVE_ID;
2300 case nir_intrinsic_load_tess_coord:
2301 return SYSTEM_VALUE_TESS_COORD;
2302 case nir_intrinsic_load_tess_level_outer:
2303 return SYSTEM_VALUE_TESS_LEVEL_OUTER;
2304 case nir_intrinsic_load_tess_level_inner:
2305 return SYSTEM_VALUE_TESS_LEVEL_INNER;
2306 case nir_intrinsic_load_tess_level_outer_default:
2307 return SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT;
2308 case nir_intrinsic_load_tess_level_inner_default:
2309 return SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT;
2310 case nir_intrinsic_load_patch_vertices_in:
2311 return SYSTEM_VALUE_VERTICES_IN;
2312 case nir_intrinsic_load_helper_invocation:
2313 return SYSTEM_VALUE_HELPER_INVOCATION;
2314 case nir_intrinsic_load_color0:
2315 return SYSTEM_VALUE_COLOR0;
2316 case nir_intrinsic_load_color1:
2317 return SYSTEM_VALUE_COLOR1;
2318 case nir_intrinsic_load_view_index:
2319 return SYSTEM_VALUE_VIEW_INDEX;
2320 case nir_intrinsic_load_subgroup_size:
2321 return SYSTEM_VALUE_SUBGROUP_SIZE;
2322 case nir_intrinsic_load_subgroup_invocation:
2323 return SYSTEM_VALUE_SUBGROUP_INVOCATION;
2324 case nir_intrinsic_load_subgroup_eq_mask:
2325 return SYSTEM_VALUE_SUBGROUP_EQ_MASK;
2326 case nir_intrinsic_load_subgroup_ge_mask:
2327 return SYSTEM_VALUE_SUBGROUP_GE_MASK;
2328 case nir_intrinsic_load_subgroup_gt_mask:
2329 return SYSTEM_VALUE_SUBGROUP_GT_MASK;
2330 case nir_intrinsic_load_subgroup_le_mask:
2331 return SYSTEM_VALUE_SUBGROUP_LE_MASK;
2332 case nir_intrinsic_load_subgroup_lt_mask:
2333 return SYSTEM_VALUE_SUBGROUP_LT_MASK;
2334 case nir_intrinsic_load_num_subgroups:
2335 return SYSTEM_VALUE_NUM_SUBGROUPS;
2336 case nir_intrinsic_load_subgroup_id:
2337 return SYSTEM_VALUE_SUBGROUP_ID;
2338 case nir_intrinsic_load_local_group_size:
2339 return SYSTEM_VALUE_LOCAL_GROUP_SIZE;
2340 case nir_intrinsic_load_global_invocation_id:
2341 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
2342 case nir_intrinsic_load_base_global_invocation_id:
2343 return SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID;
2344 case nir_intrinsic_load_global_invocation_index:
2345 return SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX;
2346 case nir_intrinsic_load_work_dim:
2347 return SYSTEM_VALUE_WORK_DIM;
2348 case nir_intrinsic_load_user_data_amd:
2349 return SYSTEM_VALUE_USER_DATA_AMD;
2350 case nir_intrinsic_load_barycentric_model:
2351 return SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL;
2352 case nir_intrinsic_load_gs_header_ir3:
2353 return SYSTEM_VALUE_GS_HEADER_IR3;
2354 case nir_intrinsic_load_tcs_header_ir3:
2355 return SYSTEM_VALUE_TCS_HEADER_IR3;
2356 case nir_intrinsic_load_ray_launch_id:
2357 return SYSTEM_VALUE_RAY_LAUNCH_ID;
2358 case nir_intrinsic_load_ray_launch_size:
2359 return SYSTEM_VALUE_RAY_LAUNCH_SIZE;
2360 case nir_intrinsic_load_ray_world_origin:
2361 return SYSTEM_VALUE_RAY_WORLD_ORIGIN;
2362 case nir_intrinsic_load_ray_world_direction:
2363 return SYSTEM_VALUE_RAY_WORLD_DIRECTION;
2364 case nir_intrinsic_load_ray_object_origin:
2365 return SYSTEM_VALUE_RAY_OBJECT_ORIGIN;
2366 case nir_intrinsic_load_ray_object_direction:
2367 return SYSTEM_VALUE_RAY_OBJECT_DIRECTION;
2368 case nir_intrinsic_load_ray_t_min:
2369 return SYSTEM_VALUE_RAY_T_MIN;
2370 case nir_intrinsic_load_ray_t_max:
2371 return SYSTEM_VALUE_RAY_T_MAX;
2372 case nir_intrinsic_load_ray_object_to_world:
2373 return SYSTEM_VALUE_RAY_OBJECT_TO_WORLD;
2374 case nir_intrinsic_load_ray_world_to_object:
2375 return SYSTEM_VALUE_RAY_WORLD_TO_OBJECT;
2376 case nir_intrinsic_load_ray_hit_kind:
2377 return SYSTEM_VALUE_RAY_HIT_KIND;
2378 case nir_intrinsic_load_ray_flags:
2379 return SYSTEM_VALUE_RAY_FLAGS;
2380 case nir_intrinsic_load_ray_geometry_index:
2381 return SYSTEM_VALUE_RAY_GEOMETRY_INDEX;
2382 case nir_intrinsic_load_ray_instance_custom_index:
2383 return SYSTEM_VALUE_RAY_INSTANCE_CUSTOM_INDEX;
2384 default:
2385 unreachable("intrinsic doesn't produce a system value");
2386 }
2387 }
2388
2389 /* OpenGL utility method that remaps the location attributes if they are
2390 * doubles. Not needed for vulkan due the differences on the input location
2391 * count for doubles on vulkan vs OpenGL
2392 *
2393 * The bitfield returned in dual_slot is one bit for each double input slot in
2394 * the original OpenGL single-slot input numbering. The mapping from old
2395 * locations to new locations is as follows:
2396 *
2397 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2398 */
2399 void
nir_remap_dual_slot_attributes(nir_shader * shader,uint64_t * dual_slot)2400 nir_remap_dual_slot_attributes(nir_shader *shader, uint64_t *dual_slot)
2401 {
2402 assert(shader->info.stage == MESA_SHADER_VERTEX);
2403
2404 *dual_slot = 0;
2405 nir_foreach_shader_in_variable(var, shader) {
2406 if (glsl_type_is_dual_slot(glsl_without_array(var->type))) {
2407 unsigned slots = glsl_count_attribute_slots(var->type, true);
2408 *dual_slot |= BITFIELD64_MASK(slots) << var->data.location;
2409 }
2410 }
2411
2412 nir_foreach_shader_in_variable(var, shader) {
2413 var->data.location +=
2414 util_bitcount64(*dual_slot & BITFIELD64_MASK(var->data.location));
2415 }
2416 }
2417
2418 /* Returns an attribute mask that has been re-compacted using the given
2419 * dual_slot mask.
2420 */
2421 uint64_t
nir_get_single_slot_attribs_mask(uint64_t attribs,uint64_t dual_slot)2422 nir_get_single_slot_attribs_mask(uint64_t attribs, uint64_t dual_slot)
2423 {
2424 while (dual_slot) {
2425 unsigned loc = u_bit_scan64(&dual_slot);
2426 /* mask of all bits up to and including loc */
2427 uint64_t mask = BITFIELD64_MASK(loc + 1);
2428 attribs = (attribs & mask) | ((attribs & ~mask) >> 1);
2429 }
2430 return attribs;
2431 }
2432
2433 void
nir_rewrite_image_intrinsic(nir_intrinsic_instr * intrin,nir_ssa_def * src,bool bindless)2434 nir_rewrite_image_intrinsic(nir_intrinsic_instr *intrin, nir_ssa_def *src,
2435 bool bindless)
2436 {
2437 enum gl_access_qualifier access = nir_intrinsic_access(intrin);
2438
2439 /* Image intrinsics only have one of these */
2440 assert(!nir_intrinsic_has_src_type(intrin) ||
2441 !nir_intrinsic_has_dest_type(intrin));
2442
2443 nir_alu_type data_type = nir_type_invalid;
2444 if (nir_intrinsic_has_src_type(intrin))
2445 data_type = nir_intrinsic_src_type(intrin);
2446 if (nir_intrinsic_has_dest_type(intrin))
2447 data_type = nir_intrinsic_dest_type(intrin);
2448
2449 switch (intrin->intrinsic) {
2450 #define CASE(op) \
2451 case nir_intrinsic_image_deref_##op: \
2452 intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2453 : nir_intrinsic_image_##op; \
2454 break;
2455 CASE(load)
2456 CASE(store)
2457 CASE(atomic_add)
2458 CASE(atomic_imin)
2459 CASE(atomic_umin)
2460 CASE(atomic_imax)
2461 CASE(atomic_umax)
2462 CASE(atomic_and)
2463 CASE(atomic_or)
2464 CASE(atomic_xor)
2465 CASE(atomic_exchange)
2466 CASE(atomic_comp_swap)
2467 CASE(atomic_fadd)
2468 CASE(atomic_inc_wrap)
2469 CASE(atomic_dec_wrap)
2470 CASE(size)
2471 CASE(samples)
2472 CASE(load_raw_intel)
2473 CASE(store_raw_intel)
2474 #undef CASE
2475 default:
2476 unreachable("Unhanded image intrinsic");
2477 }
2478
2479 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
2480 nir_variable *var = nir_deref_instr_get_variable(deref);
2481
2482 nir_intrinsic_set_image_dim(intrin, glsl_get_sampler_dim(deref->type));
2483 nir_intrinsic_set_image_array(intrin, glsl_sampler_type_is_array(deref->type));
2484 nir_intrinsic_set_access(intrin, access | var->data.access);
2485 nir_intrinsic_set_format(intrin, var->data.image.format);
2486 if (nir_intrinsic_has_src_type(intrin))
2487 nir_intrinsic_set_src_type(intrin, data_type);
2488 if (nir_intrinsic_has_dest_type(intrin))
2489 nir_intrinsic_set_dest_type(intrin, data_type);
2490
2491 nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],
2492 nir_src_for_ssa(src));
2493 }
2494
2495 unsigned
nir_image_intrinsic_coord_components(const nir_intrinsic_instr * instr)2496 nir_image_intrinsic_coord_components(const nir_intrinsic_instr *instr)
2497 {
2498 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
2499 int coords = glsl_get_sampler_dim_coordinate_components(dim);
2500 if (dim == GLSL_SAMPLER_DIM_CUBE)
2501 return coords;
2502 else
2503 return coords + nir_intrinsic_image_array(instr);
2504 }
2505
2506 nir_src *
nir_get_shader_call_payload_src(nir_intrinsic_instr * call)2507 nir_get_shader_call_payload_src(nir_intrinsic_instr *call)
2508 {
2509 switch (call->intrinsic) {
2510 case nir_intrinsic_trace_ray:
2511 return &call->src[10];
2512 case nir_intrinsic_execute_callable:
2513 return &call->src[1];
2514 default:
2515 unreachable("Not a call intrinsic");
2516 return NULL;
2517 }
2518 }
2519