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