nir: add deref lowering sanity checking
[mesa.git] / src / compiler / nir / nir.c
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_control_flow_private.h"
30 #include "util/half_float.h"
31 #include <limits.h>
32 #include <assert.h>
33 #include <math.h>
34
35 nir_shader *
36 nir_shader_create(void *mem_ctx,
37 gl_shader_stage stage,
38 const nir_shader_compiler_options *options,
39 shader_info *si)
40 {
41 nir_shader *shader = rzalloc(mem_ctx, nir_shader);
42
43 exec_list_make_empty(&shader->uniforms);
44 exec_list_make_empty(&shader->inputs);
45 exec_list_make_empty(&shader->outputs);
46 exec_list_make_empty(&shader->shared);
47
48 shader->options = options;
49
50 if (si) {
51 assert(si->stage == stage);
52 shader->info = *si;
53 } else {
54 shader->info.stage = stage;
55 }
56
57 exec_list_make_empty(&shader->functions);
58 exec_list_make_empty(&shader->registers);
59 exec_list_make_empty(&shader->globals);
60 exec_list_make_empty(&shader->system_values);
61 shader->reg_alloc = 0;
62
63 shader->num_inputs = 0;
64 shader->num_outputs = 0;
65 shader->num_uniforms = 0;
66 shader->num_shared = 0;
67 shader->lowered_derefs = 0;
68
69 return shader;
70 }
71
72 static nir_register *
73 reg_create(void *mem_ctx, struct exec_list *list)
74 {
75 nir_register *reg = ralloc(mem_ctx, nir_register);
76
77 list_inithead(&reg->uses);
78 list_inithead(&reg->defs);
79 list_inithead(&reg->if_uses);
80
81 reg->num_components = 0;
82 reg->bit_size = 32;
83 reg->num_array_elems = 0;
84 reg->is_packed = false;
85 reg->name = NULL;
86
87 exec_list_push_tail(list, &reg->node);
88
89 return reg;
90 }
91
92 nir_register *
93 nir_global_reg_create(nir_shader *shader)
94 {
95 nir_register *reg = reg_create(shader, &shader->registers);
96 reg->index = shader->reg_alloc++;
97 reg->is_global = true;
98
99 return reg;
100 }
101
102 nir_register *
103 nir_local_reg_create(nir_function_impl *impl)
104 {
105 nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
106 reg->index = impl->reg_alloc++;
107 reg->is_global = false;
108
109 return reg;
110 }
111
112 void
113 nir_reg_remove(nir_register *reg)
114 {
115 exec_node_remove(&reg->node);
116 }
117
118 void
119 nir_shader_add_variable(nir_shader *shader, nir_variable *var)
120 {
121 switch (var->data.mode) {
122 case nir_var_all:
123 assert(!"invalid mode");
124 break;
125
126 case nir_var_local:
127 assert(!"nir_shader_add_variable cannot be used for local variables");
128 break;
129
130 case nir_var_param:
131 assert(!"nir_shader_add_variable cannot be used for function parameters");
132 break;
133
134 case nir_var_global:
135 exec_list_push_tail(&shader->globals, &var->node);
136 break;
137
138 case nir_var_shader_in:
139 exec_list_push_tail(&shader->inputs, &var->node);
140 break;
141
142 case nir_var_shader_out:
143 exec_list_push_tail(&shader->outputs, &var->node);
144 break;
145
146 case nir_var_uniform:
147 case nir_var_shader_storage:
148 exec_list_push_tail(&shader->uniforms, &var->node);
149 break;
150
151 case nir_var_shared:
152 assert(shader->info.stage == MESA_SHADER_COMPUTE);
153 exec_list_push_tail(&shader->shared, &var->node);
154 break;
155
156 case nir_var_system_value:
157 exec_list_push_tail(&shader->system_values, &var->node);
158 break;
159 }
160 }
161
162 nir_variable *
163 nir_variable_create(nir_shader *shader, nir_variable_mode mode,
164 const struct glsl_type *type, const char *name)
165 {
166 nir_variable *var = rzalloc(shader, nir_variable);
167 var->name = ralloc_strdup(var, name);
168 var->type = type;
169 var->data.mode = mode;
170
171 if ((mode == nir_var_shader_in &&
172 shader->info.stage != MESA_SHADER_VERTEX) ||
173 (mode == nir_var_shader_out &&
174 shader->info.stage != MESA_SHADER_FRAGMENT))
175 var->data.interpolation = INTERP_MODE_SMOOTH;
176
177 if (mode == nir_var_shader_in || mode == nir_var_uniform)
178 var->data.read_only = true;
179
180 nir_shader_add_variable(shader, var);
181
182 return var;
183 }
184
185 nir_variable *
186 nir_local_variable_create(nir_function_impl *impl,
187 const struct glsl_type *type, const char *name)
188 {
189 nir_variable *var = rzalloc(impl->function->shader, nir_variable);
190 var->name = ralloc_strdup(var, name);
191 var->type = type;
192 var->data.mode = nir_var_local;
193
194 nir_function_impl_add_variable(impl, var);
195
196 return var;
197 }
198
199 nir_function *
200 nir_function_create(nir_shader *shader, const char *name)
201 {
202 nir_function *func = ralloc(shader, nir_function);
203
204 exec_list_push_tail(&shader->functions, &func->node);
205
206 func->name = ralloc_strdup(func, name);
207 func->shader = shader;
208 func->num_params = 0;
209 func->params = NULL;
210 func->return_type = glsl_void_type();
211 func->impl = NULL;
212
213 return func;
214 }
215
216 /* NOTE: if the instruction you are copying a src to is already added
217 * to the IR, use nir_instr_rewrite_src() instead.
218 */
219 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
220 {
221 dest->is_ssa = src->is_ssa;
222 if (src->is_ssa) {
223 dest->ssa = src->ssa;
224 } else {
225 dest->reg.base_offset = src->reg.base_offset;
226 dest->reg.reg = src->reg.reg;
227 if (src->reg.indirect) {
228 dest->reg.indirect = ralloc(mem_ctx, nir_src);
229 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
230 } else {
231 dest->reg.indirect = NULL;
232 }
233 }
234 }
235
236 void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr)
237 {
238 /* Copying an SSA definition makes no sense whatsoever. */
239 assert(!src->is_ssa);
240
241 dest->is_ssa = false;
242
243 dest->reg.base_offset = src->reg.base_offset;
244 dest->reg.reg = src->reg.reg;
245 if (src->reg.indirect) {
246 dest->reg.indirect = ralloc(instr, nir_src);
247 nir_src_copy(dest->reg.indirect, src->reg.indirect, instr);
248 } else {
249 dest->reg.indirect = NULL;
250 }
251 }
252
253 void
254 nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
255 nir_alu_instr *instr)
256 {
257 nir_src_copy(&dest->src, &src->src, &instr->instr);
258 dest->abs = src->abs;
259 dest->negate = src->negate;
260 for (unsigned i = 0; i < 4; i++)
261 dest->swizzle[i] = src->swizzle[i];
262 }
263
264 void
265 nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
266 nir_alu_instr *instr)
267 {
268 nir_dest_copy(&dest->dest, &src->dest, &instr->instr);
269 dest->write_mask = src->write_mask;
270 dest->saturate = src->saturate;
271 }
272
273
274 static void
275 cf_init(nir_cf_node *node, nir_cf_node_type type)
276 {
277 exec_node_init(&node->node);
278 node->parent = NULL;
279 node->type = type;
280 }
281
282 nir_function_impl *
283 nir_function_impl_create_bare(nir_shader *shader)
284 {
285 nir_function_impl *impl = ralloc(shader, nir_function_impl);
286
287 impl->function = NULL;
288
289 cf_init(&impl->cf_node, nir_cf_node_function);
290
291 exec_list_make_empty(&impl->body);
292 exec_list_make_empty(&impl->registers);
293 exec_list_make_empty(&impl->locals);
294 impl->num_params = 0;
295 impl->params = NULL;
296 impl->return_var = NULL;
297 impl->reg_alloc = 0;
298 impl->ssa_alloc = 0;
299 impl->valid_metadata = nir_metadata_none;
300
301 /* create start & end blocks */
302 nir_block *start_block = nir_block_create(shader);
303 nir_block *end_block = nir_block_create(shader);
304 start_block->cf_node.parent = &impl->cf_node;
305 end_block->cf_node.parent = &impl->cf_node;
306 impl->end_block = end_block;
307
308 exec_list_push_tail(&impl->body, &start_block->cf_node.node);
309
310 start_block->successors[0] = end_block;
311 _mesa_set_add(end_block->predecessors, start_block);
312 return impl;
313 }
314
315 nir_function_impl *
316 nir_function_impl_create(nir_function *function)
317 {
318 assert(function->impl == NULL);
319
320 nir_function_impl *impl = nir_function_impl_create_bare(function->shader);
321
322 function->impl = impl;
323 impl->function = function;
324
325 impl->num_params = function->num_params;
326 impl->params = ralloc_array(function->shader,
327 nir_variable *, impl->num_params);
328
329 for (unsigned i = 0; i < impl->num_params; i++) {
330 impl->params[i] = rzalloc(function->shader, nir_variable);
331 impl->params[i]->type = function->params[i].type;
332 impl->params[i]->data.mode = nir_var_param;
333 impl->params[i]->data.location = i;
334 }
335
336 if (!glsl_type_is_void(function->return_type)) {
337 impl->return_var = rzalloc(function->shader, nir_variable);
338 impl->return_var->type = function->return_type;
339 impl->return_var->data.mode = nir_var_param;
340 impl->return_var->data.location = -1;
341 } else {
342 impl->return_var = NULL;
343 }
344
345 return impl;
346 }
347
348 nir_block *
349 nir_block_create(nir_shader *shader)
350 {
351 nir_block *block = rzalloc(shader, nir_block);
352
353 cf_init(&block->cf_node, nir_cf_node_block);
354
355 block->successors[0] = block->successors[1] = NULL;
356 block->predecessors = _mesa_set_create(block, _mesa_hash_pointer,
357 _mesa_key_pointer_equal);
358 block->imm_dom = NULL;
359 /* XXX maybe it would be worth it to defer allocation? This
360 * way it doesn't get allocated for shader refs that never run
361 * nir_calc_dominance? For example, state-tracker creates an
362 * initial IR, clones that, runs appropriate lowering pass, passes
363 * to driver which does common lowering/opt, and then stores ref
364 * which is later used to do state specific lowering and futher
365 * opt. Do any of the references not need dominance metadata?
366 */
367 block->dom_frontier = _mesa_set_create(block, _mesa_hash_pointer,
368 _mesa_key_pointer_equal);
369
370 exec_list_make_empty(&block->instr_list);
371
372 return block;
373 }
374
375 static inline void
376 src_init(nir_src *src)
377 {
378 src->is_ssa = false;
379 src->reg.reg = NULL;
380 src->reg.indirect = NULL;
381 src->reg.base_offset = 0;
382 }
383
384 nir_if *
385 nir_if_create(nir_shader *shader)
386 {
387 nir_if *if_stmt = ralloc(shader, nir_if);
388
389 cf_init(&if_stmt->cf_node, nir_cf_node_if);
390 src_init(&if_stmt->condition);
391
392 nir_block *then = nir_block_create(shader);
393 exec_list_make_empty(&if_stmt->then_list);
394 exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
395 then->cf_node.parent = &if_stmt->cf_node;
396
397 nir_block *else_stmt = nir_block_create(shader);
398 exec_list_make_empty(&if_stmt->else_list);
399 exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
400 else_stmt->cf_node.parent = &if_stmt->cf_node;
401
402 return if_stmt;
403 }
404
405 nir_loop *
406 nir_loop_create(nir_shader *shader)
407 {
408 nir_loop *loop = rzalloc(shader, nir_loop);
409
410 cf_init(&loop->cf_node, nir_cf_node_loop);
411
412 nir_block *body = nir_block_create(shader);
413 exec_list_make_empty(&loop->body);
414 exec_list_push_tail(&loop->body, &body->cf_node.node);
415 body->cf_node.parent = &loop->cf_node;
416
417 body->successors[0] = body;
418 _mesa_set_add(body->predecessors, body);
419
420 return loop;
421 }
422
423 static void
424 instr_init(nir_instr *instr, nir_instr_type type)
425 {
426 instr->type = type;
427 instr->block = NULL;
428 exec_node_init(&instr->node);
429 }
430
431 static void
432 dest_init(nir_dest *dest)
433 {
434 dest->is_ssa = false;
435 dest->reg.reg = NULL;
436 dest->reg.indirect = NULL;
437 dest->reg.base_offset = 0;
438 }
439
440 static void
441 alu_dest_init(nir_alu_dest *dest)
442 {
443 dest_init(&dest->dest);
444 dest->saturate = false;
445 dest->write_mask = 0xf;
446 }
447
448 static void
449 alu_src_init(nir_alu_src *src)
450 {
451 src_init(&src->src);
452 src->abs = src->negate = false;
453 src->swizzle[0] = 0;
454 src->swizzle[1] = 1;
455 src->swizzle[2] = 2;
456 src->swizzle[3] = 3;
457 }
458
459 nir_alu_instr *
460 nir_alu_instr_create(nir_shader *shader, nir_op op)
461 {
462 unsigned num_srcs = nir_op_infos[op].num_inputs;
463 /* TODO: don't use rzalloc */
464 nir_alu_instr *instr =
465 rzalloc_size(shader,
466 sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
467
468 instr_init(&instr->instr, nir_instr_type_alu);
469 instr->op = op;
470 alu_dest_init(&instr->dest);
471 for (unsigned i = 0; i < num_srcs; i++)
472 alu_src_init(&instr->src[i]);
473
474 return instr;
475 }
476
477 nir_deref_instr *
478 nir_deref_instr_create(nir_shader *shader, nir_deref_type deref_type)
479 {
480 nir_deref_instr *instr =
481 rzalloc_size(shader, sizeof(nir_deref_instr));
482
483 instr_init(&instr->instr, nir_instr_type_deref);
484
485 instr->deref_type = deref_type;
486 if (deref_type != nir_deref_type_var)
487 src_init(&instr->parent);
488
489 if (deref_type == nir_deref_type_array)
490 src_init(&instr->arr.index);
491
492 dest_init(&instr->dest);
493
494 return instr;
495 }
496
497 nir_jump_instr *
498 nir_jump_instr_create(nir_shader *shader, nir_jump_type type)
499 {
500 nir_jump_instr *instr = ralloc(shader, nir_jump_instr);
501 instr_init(&instr->instr, nir_instr_type_jump);
502 instr->type = type;
503 return instr;
504 }
505
506 nir_load_const_instr *
507 nir_load_const_instr_create(nir_shader *shader, unsigned num_components,
508 unsigned bit_size)
509 {
510 nir_load_const_instr *instr = rzalloc(shader, nir_load_const_instr);
511 instr_init(&instr->instr, nir_instr_type_load_const);
512
513 nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
514
515 return instr;
516 }
517
518 nir_intrinsic_instr *
519 nir_intrinsic_instr_create(nir_shader *shader, nir_intrinsic_op op)
520 {
521 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
522 /* TODO: don't use rzalloc */
523 nir_intrinsic_instr *instr =
524 rzalloc_size(shader,
525 sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
526
527 instr_init(&instr->instr, nir_instr_type_intrinsic);
528 instr->intrinsic = op;
529
530 if (nir_intrinsic_infos[op].has_dest)
531 dest_init(&instr->dest);
532
533 for (unsigned i = 0; i < num_srcs; i++)
534 src_init(&instr->src[i]);
535
536 return instr;
537 }
538
539 nir_call_instr *
540 nir_call_instr_create(nir_shader *shader, nir_function *callee)
541 {
542 nir_call_instr *instr = ralloc(shader, nir_call_instr);
543 instr_init(&instr->instr, nir_instr_type_call);
544
545 instr->callee = callee;
546 instr->num_params = callee->num_params;
547 instr->params = ralloc_array(instr, nir_deref_var *, instr->num_params);
548 instr->return_deref = NULL;
549
550 return instr;
551 }
552
553 nir_tex_instr *
554 nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
555 {
556 nir_tex_instr *instr = rzalloc(shader, nir_tex_instr);
557 instr_init(&instr->instr, nir_instr_type_tex);
558
559 dest_init(&instr->dest);
560
561 instr->num_srcs = num_srcs;
562 instr->src = ralloc_array(instr, nir_tex_src, num_srcs);
563 for (unsigned i = 0; i < num_srcs; i++)
564 src_init(&instr->src[i].src);
565
566 instr->texture_index = 0;
567 instr->texture_array_size = 0;
568 instr->texture = NULL;
569 instr->sampler_index = 0;
570 instr->sampler = NULL;
571
572 return instr;
573 }
574
575 void
576 nir_tex_instr_add_src(nir_tex_instr *tex,
577 nir_tex_src_type src_type,
578 nir_src src)
579 {
580 nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
581 tex->num_srcs + 1);
582
583 for (unsigned i = 0; i < tex->num_srcs; i++) {
584 new_srcs[i].src_type = tex->src[i].src_type;
585 nir_instr_move_src(&tex->instr, &new_srcs[i].src,
586 &tex->src[i].src);
587 }
588
589 ralloc_free(tex->src);
590 tex->src = new_srcs;
591
592 tex->src[tex->num_srcs].src_type = src_type;
593 nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
594 tex->num_srcs++;
595 }
596
597 void
598 nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
599 {
600 assert(src_idx < tex->num_srcs);
601
602 /* First rewrite the source to NIR_SRC_INIT */
603 nir_instr_rewrite_src(&tex->instr, &tex->src[src_idx].src, NIR_SRC_INIT);
604
605 /* Now, move all of the other sources down */
606 for (unsigned i = src_idx + 1; i < tex->num_srcs; i++) {
607 tex->src[i-1].src_type = tex->src[i].src_type;
608 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
609 }
610 tex->num_srcs--;
611 }
612
613 nir_phi_instr *
614 nir_phi_instr_create(nir_shader *shader)
615 {
616 nir_phi_instr *instr = ralloc(shader, nir_phi_instr);
617 instr_init(&instr->instr, nir_instr_type_phi);
618
619 dest_init(&instr->dest);
620 exec_list_make_empty(&instr->srcs);
621 return instr;
622 }
623
624 nir_parallel_copy_instr *
625 nir_parallel_copy_instr_create(nir_shader *shader)
626 {
627 nir_parallel_copy_instr *instr = ralloc(shader, nir_parallel_copy_instr);
628 instr_init(&instr->instr, nir_instr_type_parallel_copy);
629
630 exec_list_make_empty(&instr->entries);
631
632 return instr;
633 }
634
635 nir_ssa_undef_instr *
636 nir_ssa_undef_instr_create(nir_shader *shader,
637 unsigned num_components,
638 unsigned bit_size)
639 {
640 nir_ssa_undef_instr *instr = ralloc(shader, nir_ssa_undef_instr);
641 instr_init(&instr->instr, nir_instr_type_ssa_undef);
642
643 nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
644
645 return instr;
646 }
647
648 nir_deref_var *
649 nir_deref_var_create(void *mem_ctx, nir_variable *var)
650 {
651 nir_deref_var *deref = ralloc(mem_ctx, nir_deref_var);
652 deref->deref.deref_type = nir_deref_type_var;
653 deref->deref.child = NULL;
654 deref->deref.type = var->type;
655 deref->var = var;
656 return deref;
657 }
658
659 nir_deref_array *
660 nir_deref_array_create(void *mem_ctx)
661 {
662 nir_deref_array *deref = ralloc(mem_ctx, nir_deref_array);
663 deref->deref.deref_type = nir_deref_type_array;
664 deref->deref.child = NULL;
665 deref->deref_array_type = nir_deref_array_type_direct;
666 src_init(&deref->indirect);
667 deref->base_offset = 0;
668 return deref;
669 }
670
671 nir_deref_struct *
672 nir_deref_struct_create(void *mem_ctx, unsigned field_index)
673 {
674 nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
675 deref->deref.deref_type = nir_deref_type_struct;
676 deref->deref.child = NULL;
677 deref->index = field_index;
678 return deref;
679 }
680
681 nir_deref_var *
682 nir_deref_var_clone(const nir_deref_var *deref, void *mem_ctx)
683 {
684 if (deref == NULL)
685 return NULL;
686
687 nir_deref_var *ret = nir_deref_var_create(mem_ctx, deref->var);
688 ret->deref.type = deref->deref.type;
689 if (deref->deref.child)
690 ret->deref.child = nir_deref_clone(deref->deref.child, ret);
691 return ret;
692 }
693
694 static nir_deref_array *
695 deref_array_clone(const nir_deref_array *deref, void *mem_ctx)
696 {
697 nir_deref_array *ret = nir_deref_array_create(mem_ctx);
698 ret->base_offset = deref->base_offset;
699 ret->deref_array_type = deref->deref_array_type;
700 if (deref->deref_array_type == nir_deref_array_type_indirect) {
701 nir_src_copy(&ret->indirect, &deref->indirect, mem_ctx);
702 }
703 ret->deref.type = deref->deref.type;
704 if (deref->deref.child)
705 ret->deref.child = nir_deref_clone(deref->deref.child, ret);
706 return ret;
707 }
708
709 static nir_deref_struct *
710 deref_struct_clone(const nir_deref_struct *deref, void *mem_ctx)
711 {
712 nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->index);
713 ret->deref.type = deref->deref.type;
714 if (deref->deref.child)
715 ret->deref.child = nir_deref_clone(deref->deref.child, ret);
716 return ret;
717 }
718
719 nir_deref *
720 nir_deref_clone(const nir_deref *deref, void *mem_ctx)
721 {
722 if (deref == NULL)
723 return NULL;
724
725 switch (deref->deref_type) {
726 case nir_deref_type_var:
727 return &nir_deref_var_clone(nir_deref_as_var(deref), mem_ctx)->deref;
728 case nir_deref_type_array:
729 return &deref_array_clone(nir_deref_as_array(deref), mem_ctx)->deref;
730 case nir_deref_type_struct:
731 return &deref_struct_clone(nir_deref_as_struct(deref), mem_ctx)->deref;
732 default:
733 unreachable("Invalid dereference type");
734 }
735
736 return NULL;
737 }
738
739 /* This is the second step in the recursion. We've found the tail and made a
740 * copy. Now we need to iterate over all possible leaves and call the
741 * callback on each one.
742 */
743 static bool
744 deref_foreach_leaf_build_recur(nir_deref_var *deref, nir_deref *tail,
745 nir_deref_foreach_leaf_cb cb, void *state)
746 {
747 unsigned length;
748 union {
749 nir_deref_array arr;
750 nir_deref_struct str;
751 } tmp;
752
753 assert(tail->child == NULL);
754 switch (glsl_get_base_type(tail->type)) {
755 case GLSL_TYPE_UINT:
756 case GLSL_TYPE_UINT16:
757 case GLSL_TYPE_UINT64:
758 case GLSL_TYPE_INT:
759 case GLSL_TYPE_INT16:
760 case GLSL_TYPE_INT64:
761 case GLSL_TYPE_FLOAT:
762 case GLSL_TYPE_FLOAT16:
763 case GLSL_TYPE_DOUBLE:
764 case GLSL_TYPE_BOOL:
765 if (glsl_type_is_vector_or_scalar(tail->type))
766 return cb(deref, state);
767 /* Fall Through */
768
769 case GLSL_TYPE_ARRAY:
770 tmp.arr.deref.deref_type = nir_deref_type_array;
771 tmp.arr.deref.type = glsl_get_array_element(tail->type);
772 tmp.arr.deref_array_type = nir_deref_array_type_direct;
773 tmp.arr.indirect = NIR_SRC_INIT;
774 tail->child = &tmp.arr.deref;
775
776 length = glsl_get_length(tail->type);
777 for (unsigned i = 0; i < length; i++) {
778 tmp.arr.deref.child = NULL;
779 tmp.arr.base_offset = i;
780 if (!deref_foreach_leaf_build_recur(deref, &tmp.arr.deref, cb, state))
781 return false;
782 }
783 return true;
784
785 case GLSL_TYPE_STRUCT:
786 tmp.str.deref.deref_type = nir_deref_type_struct;
787 tail->child = &tmp.str.deref;
788
789 length = glsl_get_length(tail->type);
790 for (unsigned i = 0; i < length; i++) {
791 tmp.arr.deref.child = NULL;
792 tmp.str.deref.type = glsl_get_struct_field(tail->type, i);
793 tmp.str.index = i;
794 if (!deref_foreach_leaf_build_recur(deref, &tmp.arr.deref, cb, state))
795 return false;
796 }
797 return true;
798
799 default:
800 unreachable("Invalid type for dereference");
801 }
802 }
803
804 /* This is the first step of the foreach_leaf recursion. In this step we are
805 * walking to the end of the deref chain and making a copy in the stack as we
806 * go. This is because we don't want to mutate the deref chain that was
807 * passed in by the caller. The downside is that this deref chain is on the
808 * stack and , if the caller wants to do anything with it, they will have to
809 * make their own copy because this one will go away.
810 */
811 static bool
812 deref_foreach_leaf_copy_recur(nir_deref_var *deref, nir_deref *tail,
813 nir_deref_foreach_leaf_cb cb, void *state)
814 {
815 union {
816 nir_deref_array arr;
817 nir_deref_struct str;
818 } c;
819
820 if (tail->child) {
821 switch (tail->child->deref_type) {
822 case nir_deref_type_array:
823 c.arr = *nir_deref_as_array(tail->child);
824 tail->child = &c.arr.deref;
825 return deref_foreach_leaf_copy_recur(deref, &c.arr.deref, cb, state);
826
827 case nir_deref_type_struct:
828 c.str = *nir_deref_as_struct(tail->child);
829 tail->child = &c.str.deref;
830 return deref_foreach_leaf_copy_recur(deref, &c.str.deref, cb, state);
831
832 case nir_deref_type_var:
833 default:
834 unreachable("Invalid deref type for a child");
835 }
836 } else {
837 /* We've gotten to the end of the original deref. Time to start
838 * building our own derefs.
839 */
840 return deref_foreach_leaf_build_recur(deref, tail, cb, state);
841 }
842 }
843
844 /**
845 * This function iterates over all of the possible derefs that can be created
846 * with the given deref as the head. It then calls the provided callback with
847 * a full deref for each one.
848 *
849 * The deref passed to the callback will be allocated on the stack. You will
850 * need to make a copy if you want it to hang around.
851 */
852 bool
853 nir_deref_foreach_leaf(nir_deref_var *deref,
854 nir_deref_foreach_leaf_cb cb, void *state)
855 {
856 nir_deref_var copy = *deref;
857 return deref_foreach_leaf_copy_recur(&copy, &copy.deref, cb, state);
858 }
859
860 /* Returns a load_const instruction that represents the constant
861 * initializer for the given deref chain. The caller is responsible for
862 * ensuring that there actually is a constant initializer.
863 */
864 nir_load_const_instr *
865 nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref)
866 {
867 nir_constant *constant = deref->var->constant_initializer;
868 assert(constant);
869
870 const nir_deref *tail = &deref->deref;
871 unsigned matrix_col = 0;
872 while (tail->child) {
873 switch (tail->child->deref_type) {
874 case nir_deref_type_array: {
875 nir_deref_array *arr = nir_deref_as_array(tail->child);
876 assert(arr->deref_array_type == nir_deref_array_type_direct);
877 if (glsl_type_is_matrix(tail->type)) {
878 assert(arr->deref.child == NULL);
879 matrix_col = arr->base_offset;
880 } else {
881 constant = constant->elements[arr->base_offset];
882 }
883 break;
884 }
885
886 case nir_deref_type_struct: {
887 constant = constant->elements[nir_deref_as_struct(tail->child)->index];
888 break;
889 }
890
891 default:
892 unreachable("Invalid deref child type");
893 }
894
895 tail = tail->child;
896 }
897
898 unsigned bit_size = glsl_get_bit_size(tail->type);
899 nir_load_const_instr *load =
900 nir_load_const_instr_create(shader, glsl_get_vector_elements(tail->type),
901 bit_size);
902
903 switch (glsl_get_base_type(tail->type)) {
904 case GLSL_TYPE_FLOAT:
905 case GLSL_TYPE_INT:
906 case GLSL_TYPE_UINT:
907 case GLSL_TYPE_FLOAT16:
908 case GLSL_TYPE_DOUBLE:
909 case GLSL_TYPE_INT16:
910 case GLSL_TYPE_UINT16:
911 case GLSL_TYPE_UINT64:
912 case GLSL_TYPE_INT64:
913 case GLSL_TYPE_BOOL:
914 load->value = constant->values[matrix_col];
915 break;
916 default:
917 unreachable("Invalid immediate type");
918 }
919
920 return load;
921 }
922
923 static nir_const_value
924 const_value_float(double d, unsigned bit_size)
925 {
926 nir_const_value v;
927 switch (bit_size) {
928 case 16: v.u16[0] = _mesa_float_to_half(d); break;
929 case 32: v.f32[0] = d; break;
930 case 64: v.f64[0] = d; break;
931 default:
932 unreachable("Invalid bit size");
933 }
934 return v;
935 }
936
937 static nir_const_value
938 const_value_int(int64_t i, unsigned bit_size)
939 {
940 nir_const_value v;
941 switch (bit_size) {
942 case 8: v.i8[0] = i; break;
943 case 16: v.i16[0] = i; break;
944 case 32: v.i32[0] = i; break;
945 case 64: v.i64[0] = i; break;
946 default:
947 unreachable("Invalid bit size");
948 }
949 return v;
950 }
951
952 nir_const_value
953 nir_alu_binop_identity(nir_op binop, unsigned bit_size)
954 {
955 const int64_t max_int = (1ull << (bit_size - 1)) - 1;
956 const int64_t min_int = -max_int - 1;
957 switch (binop) {
958 case nir_op_iadd:
959 return const_value_int(0, bit_size);
960 case nir_op_fadd:
961 return const_value_float(0, bit_size);
962 case nir_op_imul:
963 return const_value_int(1, bit_size);
964 case nir_op_fmul:
965 return const_value_float(1, bit_size);
966 case nir_op_imin:
967 return const_value_int(max_int, bit_size);
968 case nir_op_umin:
969 return const_value_int(~0ull, bit_size);
970 case nir_op_fmin:
971 return const_value_float(INFINITY, bit_size);
972 case nir_op_imax:
973 return const_value_int(min_int, bit_size);
974 case nir_op_umax:
975 return const_value_int(0, bit_size);
976 case nir_op_fmax:
977 return const_value_float(-INFINITY, bit_size);
978 case nir_op_iand:
979 return const_value_int(~0ull, bit_size);
980 case nir_op_ior:
981 return const_value_int(0, bit_size);
982 case nir_op_ixor:
983 return const_value_int(0, bit_size);
984 default:
985 unreachable("Invalid reduction operation");
986 }
987 }
988
989 nir_function_impl *
990 nir_cf_node_get_function(nir_cf_node *node)
991 {
992 while (node->type != nir_cf_node_function) {
993 node = node->parent;
994 }
995
996 return nir_cf_node_as_function(node);
997 }
998
999 /* Reduces a cursor by trying to convert everything to after and trying to
1000 * go up to block granularity when possible.
1001 */
1002 static nir_cursor
1003 reduce_cursor(nir_cursor cursor)
1004 {
1005 switch (cursor.option) {
1006 case nir_cursor_before_block:
1007 assert(nir_cf_node_prev(&cursor.block->cf_node) == NULL ||
1008 nir_cf_node_prev(&cursor.block->cf_node)->type != nir_cf_node_block);
1009 if (exec_list_is_empty(&cursor.block->instr_list)) {
1010 /* Empty block. After is as good as before. */
1011 cursor.option = nir_cursor_after_block;
1012 }
1013 return cursor;
1014
1015 case nir_cursor_after_block:
1016 return cursor;
1017
1018 case nir_cursor_before_instr: {
1019 nir_instr *prev_instr = nir_instr_prev(cursor.instr);
1020 if (prev_instr) {
1021 /* Before this instruction is after the previous */
1022 cursor.instr = prev_instr;
1023 cursor.option = nir_cursor_after_instr;
1024 } else {
1025 /* No previous instruction. Switch to before block */
1026 cursor.block = cursor.instr->block;
1027 cursor.option = nir_cursor_before_block;
1028 }
1029 return reduce_cursor(cursor);
1030 }
1031
1032 case nir_cursor_after_instr:
1033 if (nir_instr_next(cursor.instr) == NULL) {
1034 /* This is the last instruction, switch to after block */
1035 cursor.option = nir_cursor_after_block;
1036 cursor.block = cursor.instr->block;
1037 }
1038 return cursor;
1039
1040 default:
1041 unreachable("Inavlid cursor option");
1042 }
1043 }
1044
1045 bool
1046 nir_cursors_equal(nir_cursor a, nir_cursor b)
1047 {
1048 /* Reduced cursors should be unique */
1049 a = reduce_cursor(a);
1050 b = reduce_cursor(b);
1051
1052 return a.block == b.block && a.option == b.option;
1053 }
1054
1055 static bool
1056 add_use_cb(nir_src *src, void *state)
1057 {
1058 nir_instr *instr = state;
1059
1060 src->parent_instr = instr;
1061 list_addtail(&src->use_link,
1062 src->is_ssa ? &src->ssa->uses : &src->reg.reg->uses);
1063
1064 return true;
1065 }
1066
1067 static bool
1068 add_ssa_def_cb(nir_ssa_def *def, void *state)
1069 {
1070 nir_instr *instr = state;
1071
1072 if (instr->block && def->index == UINT_MAX) {
1073 nir_function_impl *impl =
1074 nir_cf_node_get_function(&instr->block->cf_node);
1075
1076 def->index = impl->ssa_alloc++;
1077 }
1078
1079 return true;
1080 }
1081
1082 static bool
1083 add_reg_def_cb(nir_dest *dest, void *state)
1084 {
1085 nir_instr *instr = state;
1086
1087 if (!dest->is_ssa) {
1088 dest->reg.parent_instr = instr;
1089 list_addtail(&dest->reg.def_link, &dest->reg.reg->defs);
1090 }
1091
1092 return true;
1093 }
1094
1095 static void
1096 add_defs_uses(nir_instr *instr)
1097 {
1098 nir_foreach_src(instr, add_use_cb, instr);
1099 nir_foreach_dest(instr, add_reg_def_cb, instr);
1100 nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
1101 }
1102
1103 void
1104 nir_instr_insert(nir_cursor cursor, nir_instr *instr)
1105 {
1106 switch (cursor.option) {
1107 case nir_cursor_before_block:
1108 /* Only allow inserting jumps into empty blocks. */
1109 if (instr->type == nir_instr_type_jump)
1110 assert(exec_list_is_empty(&cursor.block->instr_list));
1111
1112 instr->block = cursor.block;
1113 add_defs_uses(instr);
1114 exec_list_push_head(&cursor.block->instr_list, &instr->node);
1115 break;
1116 case nir_cursor_after_block: {
1117 /* Inserting instructions after a jump is illegal. */
1118 nir_instr *last = nir_block_last_instr(cursor.block);
1119 assert(last == NULL || last->type != nir_instr_type_jump);
1120 (void) last;
1121
1122 instr->block = cursor.block;
1123 add_defs_uses(instr);
1124 exec_list_push_tail(&cursor.block->instr_list, &instr->node);
1125 break;
1126 }
1127 case nir_cursor_before_instr:
1128 assert(instr->type != nir_instr_type_jump);
1129 instr->block = cursor.instr->block;
1130 add_defs_uses(instr);
1131 exec_node_insert_node_before(&cursor.instr->node, &instr->node);
1132 break;
1133 case nir_cursor_after_instr:
1134 /* Inserting instructions after a jump is illegal. */
1135 assert(cursor.instr->type != nir_instr_type_jump);
1136
1137 /* Only allow inserting jumps at the end of the block. */
1138 if (instr->type == nir_instr_type_jump)
1139 assert(cursor.instr == nir_block_last_instr(cursor.instr->block));
1140
1141 instr->block = cursor.instr->block;
1142 add_defs_uses(instr);
1143 exec_node_insert_after(&cursor.instr->node, &instr->node);
1144 break;
1145 }
1146
1147 if (instr->type == nir_instr_type_jump)
1148 nir_handle_add_jump(instr->block);
1149 }
1150
1151 static bool
1152 src_is_valid(const nir_src *src)
1153 {
1154 return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
1155 }
1156
1157 static bool
1158 remove_use_cb(nir_src *src, void *state)
1159 {
1160 (void) state;
1161
1162 if (src_is_valid(src))
1163 list_del(&src->use_link);
1164
1165 return true;
1166 }
1167
1168 static bool
1169 remove_def_cb(nir_dest *dest, void *state)
1170 {
1171 (void) state;
1172
1173 if (!dest->is_ssa)
1174 list_del(&dest->reg.def_link);
1175
1176 return true;
1177 }
1178
1179 static void
1180 remove_defs_uses(nir_instr *instr)
1181 {
1182 nir_foreach_dest(instr, remove_def_cb, instr);
1183 nir_foreach_src(instr, remove_use_cb, instr);
1184 }
1185
1186 void nir_instr_remove_v(nir_instr *instr)
1187 {
1188 remove_defs_uses(instr);
1189 exec_node_remove(&instr->node);
1190
1191 if (instr->type == nir_instr_type_jump) {
1192 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
1193 nir_handle_remove_jump(instr->block, jump_instr->type);
1194 }
1195 }
1196
1197 /*@}*/
1198
1199 void
1200 nir_index_local_regs(nir_function_impl *impl)
1201 {
1202 unsigned index = 0;
1203 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1204 reg->index = index++;
1205 }
1206 impl->reg_alloc = index;
1207 }
1208
1209 void
1210 nir_index_global_regs(nir_shader *shader)
1211 {
1212 unsigned index = 0;
1213 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1214 reg->index = index++;
1215 }
1216 shader->reg_alloc = index;
1217 }
1218
1219 static bool
1220 visit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
1221 {
1222 return cb(&instr->dest.dest, state);
1223 }
1224
1225 static bool
1226 visit_deref_dest(nir_deref_instr *instr, nir_foreach_dest_cb cb, void *state)
1227 {
1228 return cb(&instr->dest, state);
1229 }
1230
1231 static bool
1232 visit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
1233 void *state)
1234 {
1235 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1236 return cb(&instr->dest, state);
1237
1238 return true;
1239 }
1240
1241 static bool
1242 visit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
1243 void *state)
1244 {
1245 return cb(&instr->dest, state);
1246 }
1247
1248 static bool
1249 visit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
1250 {
1251 return cb(&instr->dest, state);
1252 }
1253
1254 static bool
1255 visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
1256 nir_foreach_dest_cb cb, void *state)
1257 {
1258 nir_foreach_parallel_copy_entry(entry, instr) {
1259 if (!cb(&entry->dest, state))
1260 return false;
1261 }
1262
1263 return true;
1264 }
1265
1266 bool
1267 nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
1268 {
1269 switch (instr->type) {
1270 case nir_instr_type_alu:
1271 return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
1272 case nir_instr_type_deref:
1273 return visit_deref_dest(nir_instr_as_deref(instr), cb, state);
1274 case nir_instr_type_intrinsic:
1275 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
1276 case nir_instr_type_tex:
1277 return visit_texture_dest(nir_instr_as_tex(instr), cb, state);
1278 case nir_instr_type_phi:
1279 return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
1280 case nir_instr_type_parallel_copy:
1281 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
1282 cb, state);
1283
1284 case nir_instr_type_load_const:
1285 case nir_instr_type_ssa_undef:
1286 case nir_instr_type_call:
1287 case nir_instr_type_jump:
1288 break;
1289
1290 default:
1291 unreachable("Invalid instruction type");
1292 break;
1293 }
1294
1295 return true;
1296 }
1297
1298 struct foreach_ssa_def_state {
1299 nir_foreach_ssa_def_cb cb;
1300 void *client_state;
1301 };
1302
1303 static inline bool
1304 nir_ssa_def_visitor(nir_dest *dest, void *void_state)
1305 {
1306 struct foreach_ssa_def_state *state = void_state;
1307
1308 if (dest->is_ssa)
1309 return state->cb(&dest->ssa, state->client_state);
1310 else
1311 return true;
1312 }
1313
1314 bool
1315 nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
1316 {
1317 switch (instr->type) {
1318 case nir_instr_type_alu:
1319 case nir_instr_type_deref:
1320 case nir_instr_type_tex:
1321 case nir_instr_type_intrinsic:
1322 case nir_instr_type_phi:
1323 case nir_instr_type_parallel_copy: {
1324 struct foreach_ssa_def_state foreach_state = {cb, state};
1325 return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
1326 }
1327
1328 case nir_instr_type_load_const:
1329 return cb(&nir_instr_as_load_const(instr)->def, state);
1330 case nir_instr_type_ssa_undef:
1331 return cb(&nir_instr_as_ssa_undef(instr)->def, state);
1332 case nir_instr_type_call:
1333 case nir_instr_type_jump:
1334 return true;
1335 default:
1336 unreachable("Invalid instruction type");
1337 }
1338 }
1339
1340 static bool
1341 visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
1342 {
1343 if (!cb(src, state))
1344 return false;
1345 if (!src->is_ssa && src->reg.indirect)
1346 return cb(src->reg.indirect, state);
1347 return true;
1348 }
1349
1350 static bool
1351 visit_deref_array_src(nir_deref_array *deref, nir_foreach_src_cb cb,
1352 void *state)
1353 {
1354 if (deref->deref_array_type == nir_deref_array_type_indirect)
1355 return visit_src(&deref->indirect, cb, state);
1356 return true;
1357 }
1358
1359 static bool
1360 visit_deref_src(nir_deref_var *deref, nir_foreach_src_cb cb, void *state)
1361 {
1362 nir_deref *cur = &deref->deref;
1363 while (cur != NULL) {
1364 if (cur->deref_type == nir_deref_type_array) {
1365 if (!visit_deref_array_src(nir_deref_as_array(cur), cb, state))
1366 return false;
1367 }
1368
1369 cur = cur->child;
1370 }
1371
1372 return true;
1373 }
1374
1375 static bool
1376 visit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
1377 {
1378 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1379 if (!visit_src(&instr->src[i].src, cb, state))
1380 return false;
1381
1382 return true;
1383 }
1384
1385 static bool
1386 visit_deref_instr_src(nir_deref_instr *instr,
1387 nir_foreach_src_cb cb, void *state)
1388 {
1389 if (instr->deref_type != nir_deref_type_var) {
1390 if (!visit_src(&instr->parent, cb, state))
1391 return false;
1392 }
1393
1394 if (instr->deref_type == nir_deref_type_array) {
1395 if (!visit_src(&instr->arr.index, cb, state))
1396 return false;
1397 }
1398
1399 return true;
1400 }
1401
1402 static bool
1403 visit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
1404 {
1405 for (unsigned i = 0; i < instr->num_srcs; i++) {
1406 if (!visit_src(&instr->src[i].src, cb, state))
1407 return false;
1408 }
1409
1410 if (instr->texture != NULL) {
1411 if (!visit_deref_src(instr->texture, cb, state))
1412 return false;
1413 }
1414
1415 if (instr->sampler != NULL) {
1416 if (!visit_deref_src(instr->sampler, cb, state))
1417 return false;
1418 }
1419
1420 return true;
1421 }
1422
1423 static bool
1424 visit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
1425 void *state)
1426 {
1427 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
1428 for (unsigned i = 0; i < num_srcs; i++) {
1429 if (!visit_src(&instr->src[i], cb, state))
1430 return false;
1431 }
1432
1433 unsigned num_vars =
1434 nir_intrinsic_infos[instr->intrinsic].num_variables;
1435 for (unsigned i = 0; i < num_vars; i++) {
1436 if (!visit_deref_src(instr->variables[i], cb, state))
1437 return false;
1438 }
1439
1440 return true;
1441 }
1442
1443 static bool
1444 visit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
1445 {
1446 nir_foreach_phi_src(src, instr) {
1447 if (!visit_src(&src->src, cb, state))
1448 return false;
1449 }
1450
1451 return true;
1452 }
1453
1454 static bool
1455 visit_parallel_copy_src(nir_parallel_copy_instr *instr,
1456 nir_foreach_src_cb cb, void *state)
1457 {
1458 nir_foreach_parallel_copy_entry(entry, instr) {
1459 if (!visit_src(&entry->src, cb, state))
1460 return false;
1461 }
1462
1463 return true;
1464 }
1465
1466 typedef struct {
1467 void *state;
1468 nir_foreach_src_cb cb;
1469 } visit_dest_indirect_state;
1470
1471 static bool
1472 visit_dest_indirect(nir_dest *dest, void *_state)
1473 {
1474 visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
1475
1476 if (!dest->is_ssa && dest->reg.indirect)
1477 return state->cb(dest->reg.indirect, state->state);
1478
1479 return true;
1480 }
1481
1482 bool
1483 nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
1484 {
1485 switch (instr->type) {
1486 case nir_instr_type_alu:
1487 if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
1488 return false;
1489 break;
1490 case nir_instr_type_deref:
1491 if (!visit_deref_instr_src(nir_instr_as_deref(instr), cb, state))
1492 return false;
1493 break;
1494 case nir_instr_type_intrinsic:
1495 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
1496 return false;
1497 break;
1498 case nir_instr_type_tex:
1499 if (!visit_tex_src(nir_instr_as_tex(instr), cb, state))
1500 return false;
1501 break;
1502 case nir_instr_type_call:
1503 /* Call instructions have no regular sources */
1504 break;
1505 case nir_instr_type_load_const:
1506 /* Constant load instructions have no regular sources */
1507 break;
1508 case nir_instr_type_phi:
1509 if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
1510 return false;
1511 break;
1512 case nir_instr_type_parallel_copy:
1513 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
1514 cb, state))
1515 return false;
1516 break;
1517 case nir_instr_type_jump:
1518 case nir_instr_type_ssa_undef:
1519 return true;
1520
1521 default:
1522 unreachable("Invalid instruction type");
1523 break;
1524 }
1525
1526 visit_dest_indirect_state dest_state;
1527 dest_state.state = state;
1528 dest_state.cb = cb;
1529 return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
1530 }
1531
1532 nir_const_value *
1533 nir_src_as_const_value(nir_src src)
1534 {
1535 if (!src.is_ssa)
1536 return NULL;
1537
1538 if (src.ssa->parent_instr->type != nir_instr_type_load_const)
1539 return NULL;
1540
1541 nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1542
1543 return &load->value;
1544 }
1545
1546 /**
1547 * Returns true if the source is known to be dynamically uniform. Otherwise it
1548 * returns false which means it may or may not be dynamically uniform but it
1549 * can't be determined.
1550 */
1551 bool
1552 nir_src_is_dynamically_uniform(nir_src src)
1553 {
1554 if (!src.is_ssa)
1555 return false;
1556
1557 /* Constants are trivially dynamically uniform */
1558 if (src.ssa->parent_instr->type == nir_instr_type_load_const)
1559 return true;
1560
1561 /* As are uniform variables */
1562 if (src.ssa->parent_instr->type == nir_instr_type_intrinsic) {
1563 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(src.ssa->parent_instr);
1564
1565 if (intr->intrinsic == nir_intrinsic_load_uniform)
1566 return true;
1567 }
1568
1569 /* XXX: this could have many more tests, such as when a sampler function is
1570 * called with dynamically uniform arguments.
1571 */
1572 return false;
1573 }
1574
1575 static void
1576 src_remove_all_uses(nir_src *src)
1577 {
1578 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1579 if (!src_is_valid(src))
1580 continue;
1581
1582 list_del(&src->use_link);
1583 }
1584 }
1585
1586 static void
1587 src_add_all_uses(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
1588 {
1589 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1590 if (!src_is_valid(src))
1591 continue;
1592
1593 if (parent_instr) {
1594 src->parent_instr = parent_instr;
1595 if (src->is_ssa)
1596 list_addtail(&src->use_link, &src->ssa->uses);
1597 else
1598 list_addtail(&src->use_link, &src->reg.reg->uses);
1599 } else {
1600 assert(parent_if);
1601 src->parent_if = parent_if;
1602 if (src->is_ssa)
1603 list_addtail(&src->use_link, &src->ssa->if_uses);
1604 else
1605 list_addtail(&src->use_link, &src->reg.reg->if_uses);
1606 }
1607 }
1608 }
1609
1610 void
1611 nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
1612 {
1613 assert(!src_is_valid(src) || src->parent_instr == instr);
1614
1615 src_remove_all_uses(src);
1616 *src = new_src;
1617 src_add_all_uses(src, instr, NULL);
1618 }
1619
1620 void
1621 nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
1622 {
1623 assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
1624
1625 src_remove_all_uses(dest);
1626 src_remove_all_uses(src);
1627 *dest = *src;
1628 *src = NIR_SRC_INIT;
1629 src_add_all_uses(dest, dest_instr, NULL);
1630 }
1631
1632 void
1633 nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
1634 {
1635 nir_src *src = &if_stmt->condition;
1636 assert(!src_is_valid(src) || src->parent_if == if_stmt);
1637
1638 src_remove_all_uses(src);
1639 *src = new_src;
1640 src_add_all_uses(src, NULL, if_stmt);
1641 }
1642
1643 void
1644 nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest)
1645 {
1646 if (dest->is_ssa) {
1647 /* We can only overwrite an SSA destination if it has no uses. */
1648 assert(list_empty(&dest->ssa.uses) && list_empty(&dest->ssa.if_uses));
1649 } else {
1650 list_del(&dest->reg.def_link);
1651 if (dest->reg.indirect)
1652 src_remove_all_uses(dest->reg.indirect);
1653 }
1654
1655 /* We can't re-write with an SSA def */
1656 assert(!new_dest.is_ssa);
1657
1658 nir_dest_copy(dest, &new_dest, instr);
1659
1660 dest->reg.parent_instr = instr;
1661 list_addtail(&dest->reg.def_link, &new_dest.reg.reg->defs);
1662
1663 if (dest->reg.indirect)
1664 src_add_all_uses(dest->reg.indirect, instr, NULL);
1665 }
1666
1667 void
1668 nir_instr_rewrite_deref(nir_instr *instr, nir_deref_var **deref,
1669 nir_deref_var *new_deref)
1670 {
1671 if (*deref)
1672 visit_deref_src(*deref, remove_use_cb, NULL);
1673
1674 *deref = new_deref;
1675
1676 if (*deref)
1677 visit_deref_src(*deref, add_use_cb, instr);
1678 }
1679
1680 /* note: does *not* take ownership of 'name' */
1681 void
1682 nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1683 unsigned num_components,
1684 unsigned bit_size, const char *name)
1685 {
1686 def->name = ralloc_strdup(instr, name);
1687 def->parent_instr = instr;
1688 list_inithead(&def->uses);
1689 list_inithead(&def->if_uses);
1690 def->num_components = num_components;
1691 def->bit_size = bit_size;
1692
1693 if (instr->block) {
1694 nir_function_impl *impl =
1695 nir_cf_node_get_function(&instr->block->cf_node);
1696
1697 def->index = impl->ssa_alloc++;
1698 } else {
1699 def->index = UINT_MAX;
1700 }
1701 }
1702
1703 /* note: does *not* take ownership of 'name' */
1704 void
1705 nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1706 unsigned num_components, unsigned bit_size,
1707 const char *name)
1708 {
1709 dest->is_ssa = true;
1710 nir_ssa_def_init(instr, &dest->ssa, num_components, bit_size, name);
1711 }
1712
1713 void
1714 nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src)
1715 {
1716 assert(!new_src.is_ssa || def != new_src.ssa);
1717
1718 nir_foreach_use_safe(use_src, def)
1719 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1720
1721 nir_foreach_if_use_safe(use_src, def)
1722 nir_if_rewrite_condition(use_src->parent_if, new_src);
1723 }
1724
1725 static bool
1726 is_instr_between(nir_instr *start, nir_instr *end, nir_instr *between)
1727 {
1728 assert(start->block == end->block);
1729
1730 if (between->block != start->block)
1731 return false;
1732
1733 /* Search backwards looking for "between" */
1734 while (start != end) {
1735 if (between == end)
1736 return true;
1737
1738 end = nir_instr_prev(end);
1739 assert(end);
1740 }
1741
1742 return false;
1743 }
1744
1745 /* Replaces all uses of the given SSA def with the given source but only if
1746 * the use comes after the after_me instruction. This can be useful if you
1747 * are emitting code to fix up the result of some instruction: you can freely
1748 * use the result in that code and then call rewrite_uses_after and pass the
1749 * last fixup instruction as after_me and it will replace all of the uses you
1750 * want without touching the fixup code.
1751 *
1752 * This function assumes that after_me is in the same block as
1753 * def->parent_instr and that after_me comes after def->parent_instr.
1754 */
1755 void
1756 nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
1757 nir_instr *after_me)
1758 {
1759 assert(!new_src.is_ssa || def != new_src.ssa);
1760
1761 nir_foreach_use_safe(use_src, def) {
1762 assert(use_src->parent_instr != def->parent_instr);
1763 /* Since def already dominates all of its uses, the only way a use can
1764 * not be dominated by after_me is if it is between def and after_me in
1765 * the instruction list.
1766 */
1767 if (!is_instr_between(def->parent_instr, after_me, use_src->parent_instr))
1768 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1769 }
1770
1771 nir_foreach_if_use_safe(use_src, def)
1772 nir_if_rewrite_condition(use_src->parent_if, new_src);
1773 }
1774
1775 uint8_t
1776 nir_ssa_def_components_read(const nir_ssa_def *def)
1777 {
1778 uint8_t read_mask = 0;
1779 nir_foreach_use(use, def) {
1780 if (use->parent_instr->type == nir_instr_type_alu) {
1781 nir_alu_instr *alu = nir_instr_as_alu(use->parent_instr);
1782 nir_alu_src *alu_src = exec_node_data(nir_alu_src, use, src);
1783 int src_idx = alu_src - &alu->src[0];
1784 assert(src_idx >= 0 && src_idx < nir_op_infos[alu->op].num_inputs);
1785
1786 for (unsigned c = 0; c < 4; c++) {
1787 if (!nir_alu_instr_channel_used(alu, src_idx, c))
1788 continue;
1789
1790 read_mask |= (1 << alu_src->swizzle[c]);
1791 }
1792 } else {
1793 return (1 << def->num_components) - 1;
1794 }
1795 }
1796
1797 return read_mask;
1798 }
1799
1800 nir_block *
1801 nir_block_cf_tree_next(nir_block *block)
1802 {
1803 if (block == NULL) {
1804 /* nir_foreach_block_safe() will call this function on a NULL block
1805 * after the last iteration, but it won't use the result so just return
1806 * NULL here.
1807 */
1808 return NULL;
1809 }
1810
1811 nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
1812 if (cf_next)
1813 return nir_cf_node_cf_tree_first(cf_next);
1814
1815 nir_cf_node *parent = block->cf_node.parent;
1816
1817 switch (parent->type) {
1818 case nir_cf_node_if: {
1819 /* Are we at the end of the if? Go to the beginning of the else */
1820 nir_if *if_stmt = nir_cf_node_as_if(parent);
1821 if (block == nir_if_last_then_block(if_stmt))
1822 return nir_if_first_else_block(if_stmt);
1823
1824 assert(block == nir_if_last_else_block(if_stmt));
1825 /* fall through */
1826 }
1827
1828 case nir_cf_node_loop:
1829 return nir_cf_node_as_block(nir_cf_node_next(parent));
1830
1831 case nir_cf_node_function:
1832 return NULL;
1833
1834 default:
1835 unreachable("unknown cf node type");
1836 }
1837 }
1838
1839 nir_block *
1840 nir_block_cf_tree_prev(nir_block *block)
1841 {
1842 if (block == NULL) {
1843 /* do this for consistency with nir_block_cf_tree_next() */
1844 return NULL;
1845 }
1846
1847 nir_cf_node *cf_prev = nir_cf_node_prev(&block->cf_node);
1848 if (cf_prev)
1849 return nir_cf_node_cf_tree_last(cf_prev);
1850
1851 nir_cf_node *parent = block->cf_node.parent;
1852
1853 switch (parent->type) {
1854 case nir_cf_node_if: {
1855 /* Are we at the beginning of the else? Go to the end of the if */
1856 nir_if *if_stmt = nir_cf_node_as_if(parent);
1857 if (block == nir_if_first_else_block(if_stmt))
1858 return nir_if_last_then_block(if_stmt);
1859
1860 assert(block == nir_if_first_then_block(if_stmt));
1861 /* fall through */
1862 }
1863
1864 case nir_cf_node_loop:
1865 return nir_cf_node_as_block(nir_cf_node_prev(parent));
1866
1867 case nir_cf_node_function:
1868 return NULL;
1869
1870 default:
1871 unreachable("unknown cf node type");
1872 }
1873 }
1874
1875 nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node)
1876 {
1877 switch (node->type) {
1878 case nir_cf_node_function: {
1879 nir_function_impl *impl = nir_cf_node_as_function(node);
1880 return nir_start_block(impl);
1881 }
1882
1883 case nir_cf_node_if: {
1884 nir_if *if_stmt = nir_cf_node_as_if(node);
1885 return nir_if_first_then_block(if_stmt);
1886 }
1887
1888 case nir_cf_node_loop: {
1889 nir_loop *loop = nir_cf_node_as_loop(node);
1890 return nir_loop_first_block(loop);
1891 }
1892
1893 case nir_cf_node_block: {
1894 return nir_cf_node_as_block(node);
1895 }
1896
1897 default:
1898 unreachable("unknown node type");
1899 }
1900 }
1901
1902 nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node)
1903 {
1904 switch (node->type) {
1905 case nir_cf_node_function: {
1906 nir_function_impl *impl = nir_cf_node_as_function(node);
1907 return nir_impl_last_block(impl);
1908 }
1909
1910 case nir_cf_node_if: {
1911 nir_if *if_stmt = nir_cf_node_as_if(node);
1912 return nir_if_last_else_block(if_stmt);
1913 }
1914
1915 case nir_cf_node_loop: {
1916 nir_loop *loop = nir_cf_node_as_loop(node);
1917 return nir_loop_last_block(loop);
1918 }
1919
1920 case nir_cf_node_block: {
1921 return nir_cf_node_as_block(node);
1922 }
1923
1924 default:
1925 unreachable("unknown node type");
1926 }
1927 }
1928
1929 nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node)
1930 {
1931 if (node->type == nir_cf_node_block)
1932 return nir_block_cf_tree_next(nir_cf_node_as_block(node));
1933 else if (node->type == nir_cf_node_function)
1934 return NULL;
1935 else
1936 return nir_cf_node_as_block(nir_cf_node_next(node));
1937 }
1938
1939 nir_if *
1940 nir_block_get_following_if(nir_block *block)
1941 {
1942 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1943 return NULL;
1944
1945 if (nir_cf_node_is_last(&block->cf_node))
1946 return NULL;
1947
1948 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1949
1950 if (next_node->type != nir_cf_node_if)
1951 return NULL;
1952
1953 return nir_cf_node_as_if(next_node);
1954 }
1955
1956 nir_loop *
1957 nir_block_get_following_loop(nir_block *block)
1958 {
1959 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1960 return NULL;
1961
1962 if (nir_cf_node_is_last(&block->cf_node))
1963 return NULL;
1964
1965 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1966
1967 if (next_node->type != nir_cf_node_loop)
1968 return NULL;
1969
1970 return nir_cf_node_as_loop(next_node);
1971 }
1972
1973 void
1974 nir_index_blocks(nir_function_impl *impl)
1975 {
1976 unsigned index = 0;
1977
1978 if (impl->valid_metadata & nir_metadata_block_index)
1979 return;
1980
1981 nir_foreach_block(block, impl) {
1982 block->index = index++;
1983 }
1984
1985 impl->num_blocks = index;
1986 }
1987
1988 static bool
1989 index_ssa_def_cb(nir_ssa_def *def, void *state)
1990 {
1991 unsigned *index = (unsigned *) state;
1992 def->index = (*index)++;
1993
1994 return true;
1995 }
1996
1997 /**
1998 * The indices are applied top-to-bottom which has the very nice property
1999 * that, if A dominates B, then A->index <= B->index.
2000 */
2001 void
2002 nir_index_ssa_defs(nir_function_impl *impl)
2003 {
2004 unsigned index = 0;
2005
2006 nir_foreach_block(block, impl) {
2007 nir_foreach_instr(instr, block)
2008 nir_foreach_ssa_def(instr, index_ssa_def_cb, &index);
2009 }
2010
2011 impl->ssa_alloc = index;
2012 }
2013
2014 /**
2015 * The indices are applied top-to-bottom which has the very nice property
2016 * that, if A dominates B, then A->index <= B->index.
2017 */
2018 unsigned
2019 nir_index_instrs(nir_function_impl *impl)
2020 {
2021 unsigned index = 0;
2022
2023 nir_foreach_block(block, impl) {
2024 nir_foreach_instr(instr, block)
2025 instr->index = index++;
2026 }
2027
2028 return index;
2029 }
2030
2031 nir_intrinsic_op
2032 nir_intrinsic_from_system_value(gl_system_value val)
2033 {
2034 switch (val) {
2035 case SYSTEM_VALUE_VERTEX_ID:
2036 return nir_intrinsic_load_vertex_id;
2037 case SYSTEM_VALUE_INSTANCE_ID:
2038 return nir_intrinsic_load_instance_id;
2039 case SYSTEM_VALUE_DRAW_ID:
2040 return nir_intrinsic_load_draw_id;
2041 case SYSTEM_VALUE_BASE_INSTANCE:
2042 return nir_intrinsic_load_base_instance;
2043 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
2044 return nir_intrinsic_load_vertex_id_zero_base;
2045 case SYSTEM_VALUE_IS_INDEXED_DRAW:
2046 return nir_intrinsic_load_is_indexed_draw;
2047 case SYSTEM_VALUE_FIRST_VERTEX:
2048 return nir_intrinsic_load_first_vertex;
2049 case SYSTEM_VALUE_BASE_VERTEX:
2050 return nir_intrinsic_load_base_vertex;
2051 case SYSTEM_VALUE_INVOCATION_ID:
2052 return nir_intrinsic_load_invocation_id;
2053 case SYSTEM_VALUE_FRAG_COORD:
2054 return nir_intrinsic_load_frag_coord;
2055 case SYSTEM_VALUE_FRONT_FACE:
2056 return nir_intrinsic_load_front_face;
2057 case SYSTEM_VALUE_SAMPLE_ID:
2058 return nir_intrinsic_load_sample_id;
2059 case SYSTEM_VALUE_SAMPLE_POS:
2060 return nir_intrinsic_load_sample_pos;
2061 case SYSTEM_VALUE_SAMPLE_MASK_IN:
2062 return nir_intrinsic_load_sample_mask_in;
2063 case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
2064 return nir_intrinsic_load_local_invocation_id;
2065 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
2066 return nir_intrinsic_load_local_invocation_index;
2067 case SYSTEM_VALUE_WORK_GROUP_ID:
2068 return nir_intrinsic_load_work_group_id;
2069 case SYSTEM_VALUE_NUM_WORK_GROUPS:
2070 return nir_intrinsic_load_num_work_groups;
2071 case SYSTEM_VALUE_PRIMITIVE_ID:
2072 return nir_intrinsic_load_primitive_id;
2073 case SYSTEM_VALUE_TESS_COORD:
2074 return nir_intrinsic_load_tess_coord;
2075 case SYSTEM_VALUE_TESS_LEVEL_OUTER:
2076 return nir_intrinsic_load_tess_level_outer;
2077 case SYSTEM_VALUE_TESS_LEVEL_INNER:
2078 return nir_intrinsic_load_tess_level_inner;
2079 case SYSTEM_VALUE_VERTICES_IN:
2080 return nir_intrinsic_load_patch_vertices_in;
2081 case SYSTEM_VALUE_HELPER_INVOCATION:
2082 return nir_intrinsic_load_helper_invocation;
2083 case SYSTEM_VALUE_VIEW_INDEX:
2084 return nir_intrinsic_load_view_index;
2085 case SYSTEM_VALUE_SUBGROUP_SIZE:
2086 return nir_intrinsic_load_subgroup_size;
2087 case SYSTEM_VALUE_SUBGROUP_INVOCATION:
2088 return nir_intrinsic_load_subgroup_invocation;
2089 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
2090 return nir_intrinsic_load_subgroup_eq_mask;
2091 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
2092 return nir_intrinsic_load_subgroup_ge_mask;
2093 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
2094 return nir_intrinsic_load_subgroup_gt_mask;
2095 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
2096 return nir_intrinsic_load_subgroup_le_mask;
2097 case SYSTEM_VALUE_SUBGROUP_LT_MASK:
2098 return nir_intrinsic_load_subgroup_lt_mask;
2099 case SYSTEM_VALUE_NUM_SUBGROUPS:
2100 return nir_intrinsic_load_num_subgroups;
2101 case SYSTEM_VALUE_SUBGROUP_ID:
2102 return nir_intrinsic_load_subgroup_id;
2103 case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
2104 return nir_intrinsic_load_local_group_size;
2105 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID:
2106 return nir_intrinsic_load_global_invocation_id;
2107 default:
2108 unreachable("system value does not directly correspond to intrinsic");
2109 }
2110 }
2111
2112 gl_system_value
2113 nir_system_value_from_intrinsic(nir_intrinsic_op intrin)
2114 {
2115 switch (intrin) {
2116 case nir_intrinsic_load_vertex_id:
2117 return SYSTEM_VALUE_VERTEX_ID;
2118 case nir_intrinsic_load_instance_id:
2119 return SYSTEM_VALUE_INSTANCE_ID;
2120 case nir_intrinsic_load_draw_id:
2121 return SYSTEM_VALUE_DRAW_ID;
2122 case nir_intrinsic_load_base_instance:
2123 return SYSTEM_VALUE_BASE_INSTANCE;
2124 case nir_intrinsic_load_vertex_id_zero_base:
2125 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
2126 case nir_intrinsic_load_first_vertex:
2127 return SYSTEM_VALUE_FIRST_VERTEX;
2128 case nir_intrinsic_load_is_indexed_draw:
2129 return SYSTEM_VALUE_IS_INDEXED_DRAW;
2130 case nir_intrinsic_load_base_vertex:
2131 return SYSTEM_VALUE_BASE_VERTEX;
2132 case nir_intrinsic_load_invocation_id:
2133 return SYSTEM_VALUE_INVOCATION_ID;
2134 case nir_intrinsic_load_frag_coord:
2135 return SYSTEM_VALUE_FRAG_COORD;
2136 case nir_intrinsic_load_front_face:
2137 return SYSTEM_VALUE_FRONT_FACE;
2138 case nir_intrinsic_load_sample_id:
2139 return SYSTEM_VALUE_SAMPLE_ID;
2140 case nir_intrinsic_load_sample_pos:
2141 return SYSTEM_VALUE_SAMPLE_POS;
2142 case nir_intrinsic_load_sample_mask_in:
2143 return SYSTEM_VALUE_SAMPLE_MASK_IN;
2144 case nir_intrinsic_load_local_invocation_id:
2145 return SYSTEM_VALUE_LOCAL_INVOCATION_ID;
2146 case nir_intrinsic_load_local_invocation_index:
2147 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
2148 case nir_intrinsic_load_num_work_groups:
2149 return SYSTEM_VALUE_NUM_WORK_GROUPS;
2150 case nir_intrinsic_load_work_group_id:
2151 return SYSTEM_VALUE_WORK_GROUP_ID;
2152 case nir_intrinsic_load_primitive_id:
2153 return SYSTEM_VALUE_PRIMITIVE_ID;
2154 case nir_intrinsic_load_tess_coord:
2155 return SYSTEM_VALUE_TESS_COORD;
2156 case nir_intrinsic_load_tess_level_outer:
2157 return SYSTEM_VALUE_TESS_LEVEL_OUTER;
2158 case nir_intrinsic_load_tess_level_inner:
2159 return SYSTEM_VALUE_TESS_LEVEL_INNER;
2160 case nir_intrinsic_load_patch_vertices_in:
2161 return SYSTEM_VALUE_VERTICES_IN;
2162 case nir_intrinsic_load_helper_invocation:
2163 return SYSTEM_VALUE_HELPER_INVOCATION;
2164 case nir_intrinsic_load_view_index:
2165 return SYSTEM_VALUE_VIEW_INDEX;
2166 case nir_intrinsic_load_subgroup_size:
2167 return SYSTEM_VALUE_SUBGROUP_SIZE;
2168 case nir_intrinsic_load_subgroup_invocation:
2169 return SYSTEM_VALUE_SUBGROUP_INVOCATION;
2170 case nir_intrinsic_load_subgroup_eq_mask:
2171 return SYSTEM_VALUE_SUBGROUP_EQ_MASK;
2172 case nir_intrinsic_load_subgroup_ge_mask:
2173 return SYSTEM_VALUE_SUBGROUP_GE_MASK;
2174 case nir_intrinsic_load_subgroup_gt_mask:
2175 return SYSTEM_VALUE_SUBGROUP_GT_MASK;
2176 case nir_intrinsic_load_subgroup_le_mask:
2177 return SYSTEM_VALUE_SUBGROUP_LE_MASK;
2178 case nir_intrinsic_load_subgroup_lt_mask:
2179 return SYSTEM_VALUE_SUBGROUP_LT_MASK;
2180 case nir_intrinsic_load_num_subgroups:
2181 return SYSTEM_VALUE_NUM_SUBGROUPS;
2182 case nir_intrinsic_load_subgroup_id:
2183 return SYSTEM_VALUE_SUBGROUP_ID;
2184 case nir_intrinsic_load_local_group_size:
2185 return SYSTEM_VALUE_LOCAL_GROUP_SIZE;
2186 case nir_intrinsic_load_global_invocation_id:
2187 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
2188 default:
2189 unreachable("intrinsic doesn't produce a system value");
2190 }
2191 }