nir: Validate all lists in the validator
[mesa.git] / src / glsl / nir / nir_validate.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 <assert.h>
30
31 /*
32 * This file checks for invalid IR indicating a bug somewhere in the compiler.
33 */
34
35 /*
36 * Per-register validation state.
37 */
38
39 typedef struct {
40 /*
41 * equivalent to the uses and defs in nir_register, but built up by the
42 * validator. At the end, we verify that the sets have the same entries.
43 */
44 struct set *uses, *defs;
45 nir_function_impl *where_defined; /* NULL for global registers */
46 } reg_validate_state;
47
48 typedef struct {
49 /* map of register -> validation state (struct above) */
50 struct hash_table *regs;
51
52 /* the current instruction being validated */
53 nir_instr *instr;
54
55 /* the current basic block being validated */
56 nir_block *block;
57
58 /* the current if statement being validated */
59 nir_if *if_stmt;
60
61 /* the parent of the current cf node being visited */
62 nir_cf_node *parent_node;
63
64 /* the current function implementation being validated */
65 nir_function_impl *impl;
66
67 /* map of SSA value -> function implementation where it is defined */
68 struct hash_table *ssa_defs;
69
70 /* map of local variable -> function implementation where it is defined */
71 struct hash_table *var_defs;
72 } validate_state;
73
74 static void validate_src(nir_src *src, validate_state *state);
75
76 static void
77 validate_reg_src(nir_reg_src *src, validate_state *state)
78 {
79 assert(src->reg != NULL);
80
81 struct set_entry *entry =
82 _mesa_set_search(src->reg->uses, _mesa_hash_pointer(state->instr),
83 state->instr);
84 assert(entry && "use not in nir_register.uses");
85
86 struct hash_entry *entry2;
87 entry2 = _mesa_hash_table_search(state->regs, src->reg);
88
89 assert(entry2);
90
91 reg_validate_state *reg_state = (reg_validate_state *) entry2->data;
92 _mesa_set_add(reg_state->uses, _mesa_hash_pointer(state->instr),
93 state->instr);
94
95 if (!src->reg->is_global) {
96 assert(reg_state->where_defined == state->impl &&
97 "using a register declared in a different function");
98 }
99
100 assert((src->reg->num_array_elems == 0 ||
101 src->base_offset < src->reg->num_array_elems) &&
102 "definitely out-of-bounds array access");
103
104 if (src->indirect) {
105 assert(src->reg->num_array_elems != 0);
106 assert((src->indirect->is_ssa || src->indirect->reg.indirect == NULL) &&
107 "only one level of indirection allowed");
108 validate_src(src->indirect, state);
109 }
110 }
111
112 static void
113 validate_ssa_src(nir_ssa_def *def, validate_state *state)
114 {
115 assert(def != NULL);
116
117 struct hash_entry *entry = _mesa_hash_table_search(state->ssa_defs, def);
118
119 assert(entry);
120
121 assert((nir_function_impl *) entry->data == state->impl &&
122 "using an SSA value defined in a different function");
123
124 struct set_entry *entry2;
125
126 entry2 = _mesa_set_search(def->uses, _mesa_hash_pointer(state->instr),
127 state->instr);
128
129 assert(entry2 && "SSA use missing");
130
131 /* TODO validate that the use is dominated by the definition */
132 }
133
134 static void
135 validate_src(nir_src *src, validate_state *state)
136 {
137 if (src->is_ssa)
138 validate_ssa_src(src->ssa, state);
139 else
140 validate_reg_src(&src->reg, state);
141 }
142
143 static void
144 validate_alu_src(nir_alu_instr *instr, unsigned index, validate_state *state)
145 {
146 nir_alu_src *src = &instr->src[index];
147
148 unsigned num_components;
149 if (src->src.is_ssa)
150 num_components = src->src.ssa->num_components;
151 else {
152 if (src->src.reg.reg->is_packed)
153 num_components = 4; /* can't check anything */
154 else
155 num_components = src->src.reg.reg->num_components;
156 }
157 for (unsigned i = 0; i < 4; i++) {
158 assert(src->swizzle[i] < 4);
159
160 if (nir_alu_instr_channel_used(instr, index, i))
161 assert(src->swizzle[i] < num_components);
162 }
163
164 validate_src(&src->src, state);
165 }
166
167 static void
168 validate_reg_dest(nir_reg_dest *dest, validate_state *state)
169 {
170 assert(dest->reg != NULL);
171
172 struct set_entry *entry =
173 _mesa_set_search(dest->reg->defs, _mesa_hash_pointer(state->instr),
174 state->instr);
175 assert(entry && "definition not in nir_register.defs");
176
177 struct hash_entry *entry2;
178 entry2 = _mesa_hash_table_search(state->regs, dest->reg);
179
180 assert(entry2);
181
182 reg_validate_state *reg_state = (reg_validate_state *) entry2->data;
183 _mesa_set_add(reg_state->defs, _mesa_hash_pointer(state->instr),
184 state->instr);
185
186 if (!dest->reg->is_global) {
187 assert(reg_state->where_defined == state->impl &&
188 "writing to a register declared in a different function");
189 }
190
191 assert((dest->reg->num_array_elems == 0 ||
192 dest->base_offset < dest->reg->num_array_elems) &&
193 "definitely out-of-bounds array access");
194
195 if (dest->indirect) {
196 assert(dest->reg->num_array_elems != 0);
197 assert((dest->indirect->is_ssa || dest->indirect->reg.indirect == NULL) &&
198 "only one level of indirection allowed");
199 validate_src(dest->indirect, state);
200 }
201 }
202
203 static void
204 validate_ssa_def(nir_ssa_def *def, validate_state *state)
205 {
206 assert(def->num_components <= 4);
207 _mesa_hash_table_insert(state->ssa_defs, def, state->impl);
208 }
209
210 static void
211 validate_dest(nir_dest *dest, validate_state *state)
212 {
213 if (dest->is_ssa)
214 validate_ssa_def(&dest->ssa, state);
215 else
216 validate_reg_dest(&dest->reg, state);
217 }
218
219 static void
220 validate_alu_dest(nir_alu_dest *dest, validate_state *state)
221 {
222 unsigned dest_size =
223 dest->dest.is_ssa ? dest->dest.ssa.num_components
224 : dest->dest.reg.reg->num_components;
225 bool is_packed = !dest->dest.is_ssa && dest->dest.reg.reg->is_packed;
226 /*
227 * validate that the instruction doesn't write to components not in the
228 * register/SSA value
229 */
230 assert(is_packed || !(dest->write_mask & ~((1 << dest_size) - 1)));
231 validate_dest(&dest->dest, state);
232 }
233
234 static void
235 validate_alu_instr(nir_alu_instr *instr, validate_state *state)
236 {
237 assert(instr->op < nir_num_opcodes);
238
239 validate_alu_dest(&instr->dest, state);
240
241 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
242 validate_alu_src(instr, i, state);
243 }
244
245 if (instr->has_predicate)
246 validate_src(&instr->predicate, state);
247 }
248
249 static void
250 validate_deref_chain(nir_deref *deref, validate_state *state)
251 {
252 nir_deref *parent = NULL;
253 while (deref != NULL) {
254 switch (deref->deref_type) {
255 case nir_deref_type_array:
256 assert(deref->type == glsl_get_array_element(parent->type));
257 if (nir_deref_as_array(deref)->has_indirect)
258 validate_src(&nir_deref_as_array(deref)->indirect, state);
259 break;
260
261 case nir_deref_type_struct:
262 assert(deref->type ==
263 glsl_get_struct_field(parent->type,
264 nir_deref_as_struct(deref)->elem));
265 break;
266
267 case nir_deref_type_var:
268 break;
269
270 default:
271 assert(!"Invalid deref type");
272 break;
273 }
274
275 parent = deref;
276 deref = deref->child;
277 }
278 }
279
280 static void
281 validate_var_use(nir_variable *var, validate_state *state)
282 {
283 if (var->data.mode == nir_var_local) {
284 struct hash_entry *entry = _mesa_hash_table_search(state->var_defs, var);
285
286 assert(entry);
287 assert((nir_function_impl *) entry->data == state->impl);
288 }
289 }
290
291 static void
292 validate_deref_var(nir_deref_var *deref, validate_state *state)
293 {
294 assert(deref != NULL);
295 assert(deref->deref.type == deref->var->type);
296
297 validate_var_use(deref->var, state);
298
299 validate_deref_chain(&deref->deref, state);
300 }
301
302 static void
303 validate_intrinsic_instr(nir_intrinsic_instr *instr, validate_state *state)
304 {
305 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
306 for (unsigned i = 0; i < num_srcs; i++) {
307 validate_src(&instr->src[i], state);
308 }
309
310 if (nir_intrinsic_infos[instr->intrinsic].has_dest) {
311 validate_dest(&instr->dest, state);
312 }
313
314 unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
315 for (unsigned i = 0; i < num_vars; i++) {
316 validate_deref_var(instr->variables[i], state);
317 }
318
319 if (instr->has_predicate)
320 validate_src(&instr->predicate, state);
321 }
322
323 static void
324 validate_tex_instr(nir_tex_instr *instr, validate_state *state)
325 {
326 validate_dest(&instr->dest, state);
327
328 bool src_type_seen[nir_num_texinput_types];
329 for (unsigned i = 0; i < nir_num_texinput_types; i++)
330 src_type_seen[i] = false;
331
332 for (unsigned i = 0; i < instr->num_srcs; i++) {
333 assert(!src_type_seen[instr->src_type[i]]);
334 src_type_seen[instr->src_type[i]] = true;
335 validate_src(&instr->src[i], state);
336 }
337
338 if (instr->sampler != NULL)
339 validate_deref_var(instr->sampler, state);
340 }
341
342 static void
343 validate_call_instr(nir_call_instr *instr, validate_state *state)
344 {
345 if (instr->return_deref == NULL)
346 assert(glsl_type_is_void(instr->callee->return_type));
347 else
348 assert(instr->return_deref->deref.type == instr->callee->return_type);
349
350 assert(instr->num_params == instr->callee->num_params);
351
352 for (unsigned i = 0; i < instr->num_params; i++) {
353 assert(instr->callee->params[i].type == instr->params[i]->deref.type);
354 validate_deref_var(instr->params[i], state);
355 }
356
357 validate_deref_var(instr->return_deref, state);
358
359 if (instr->has_predicate)
360 validate_src(&instr->predicate, state);
361 }
362
363 static void
364 validate_load_const_instr(nir_load_const_instr *instr, validate_state *state)
365 {
366 validate_dest(&instr->dest, state);
367
368 if (instr->array_elems != 0) {
369 assert(!instr->dest.is_ssa);
370 assert(instr->dest.reg.base_offset + instr->array_elems <=
371 instr->dest.reg.reg->num_array_elems);
372 }
373
374 if (instr->has_predicate)
375 validate_src(&instr->predicate, state);
376 }
377
378 static void
379 validate_ssa_undef_instr(nir_ssa_undef_instr *instr, validate_state *state)
380 {
381 validate_ssa_def(&instr->def, state);
382 }
383
384 static void
385 validate_phi_instr(nir_phi_instr *instr, validate_state *state)
386 {
387 /*
388 * don't validate the sources until we get to them from their predecessor
389 * basic blocks, to avoid validating an SSA use before its definition.
390 */
391
392 validate_dest(&instr->dest, state);
393
394 exec_list_validate(&instr->srcs);
395 assert(exec_list_length(&instr->srcs) ==
396 state->block->predecessors->entries);
397 }
398
399 static void
400 validate_instr(nir_instr *instr, validate_state *state)
401 {
402 assert(instr->block == state->block);
403
404 state->instr = instr;
405
406 switch (instr->type) {
407 case nir_instr_type_alu:
408 validate_alu_instr(nir_instr_as_alu(instr), state);
409 break;
410
411 case nir_instr_type_call:
412 validate_call_instr(nir_instr_as_call(instr), state);
413 break;
414
415 case nir_instr_type_intrinsic:
416 validate_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
417 break;
418
419 case nir_instr_type_texture:
420 validate_tex_instr(nir_instr_as_texture(instr), state);
421 break;
422
423 case nir_instr_type_load_const:
424 validate_load_const_instr(nir_instr_as_load_const(instr), state);
425 break;
426
427 case nir_instr_type_phi:
428 validate_phi_instr(nir_instr_as_phi(instr), state);
429 break;
430
431 case nir_instr_type_ssa_undef:
432 validate_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
433 break;
434
435 case nir_instr_type_jump:
436 break;
437
438 default:
439 assert(!"Invalid ALU instruction type");
440 break;
441 }
442 }
443
444 static void
445 validate_phi_src(nir_phi_instr *instr, nir_block *pred, validate_state *state)
446 {
447 state->instr = &instr->instr;
448
449 exec_list_validate(&instr->srcs);
450 foreach_list_typed(nir_phi_src, src, node, &instr->srcs) {
451 if (src->pred == pred) {
452 validate_src(&src->src, state);
453 return;
454 }
455 }
456
457 abort();
458 }
459
460 static void
461 validate_phi_srcs(nir_block *block, nir_block *succ, validate_state *state)
462 {
463 nir_foreach_instr(succ, instr) {
464 if (instr->type != nir_instr_type_phi)
465 break;
466
467 validate_phi_src(nir_instr_as_phi(instr), block, state);
468 }
469 }
470
471 static void validate_cf_node(nir_cf_node *node, validate_state *state);
472
473 static void
474 validate_block(nir_block *block, validate_state *state)
475 {
476 assert(block->cf_node.parent == state->parent_node);
477
478 state->block = block;
479
480 exec_list_validate(&block->instr_list);
481 nir_foreach_instr(block, instr) {
482 if (instr->type == nir_instr_type_phi) {
483 assert(instr == nir_block_first_instr(block) ||
484 nir_instr_prev(instr)->type == nir_instr_type_phi);
485 }
486
487 if (instr->type == nir_instr_type_jump) {
488 assert(instr == nir_block_last_instr(block));
489 }
490
491 validate_instr(instr, state);
492 }
493
494 assert(block->successors[0] != NULL);
495
496 for (unsigned i = 0; i < 2; i++) {
497 if (block->successors[i] != NULL) {
498 struct set_entry *entry =
499 _mesa_set_search(block->successors[i]->predecessors,
500 _mesa_hash_pointer(block), block);
501 assert(entry);
502
503 validate_phi_srcs(block, block->successors[i], state);
504 }
505 }
506
507 if (!exec_list_is_empty(&block->instr_list) &&
508 nir_block_last_instr(block)->type == nir_instr_type_jump)
509 assert(block->successors[1] == NULL);
510 }
511
512 static void
513 validate_if(nir_if *if_stmt, validate_state *state)
514 {
515 assert(!exec_node_is_head_sentinel(if_stmt->cf_node.node.prev));
516 nir_cf_node *prev_node = nir_cf_node_prev(&if_stmt->cf_node);
517 assert(prev_node->type == nir_cf_node_block);
518
519 nir_block *prev_block = nir_cf_node_as_block(prev_node);
520 assert(&prev_block->successors[0]->cf_node ==
521 nir_if_first_then_node(if_stmt));
522 assert(&prev_block->successors[1]->cf_node ==
523 nir_if_first_else_node(if_stmt));
524
525 assert(!exec_node_is_tail_sentinel(if_stmt->cf_node.node.next));
526 nir_cf_node *next_node = nir_cf_node_next(&if_stmt->cf_node);
527 assert(next_node->type == nir_cf_node_block);
528
529 if (!if_stmt->condition.is_ssa) {
530 nir_register *reg = if_stmt->condition.reg.reg;
531 struct set_entry *entry =
532 _mesa_set_search(reg->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
533 assert(entry);
534 } else {
535 nir_ssa_def *def = if_stmt->condition.ssa;
536 struct set_entry *entry =
537 _mesa_set_search(def->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
538 assert(entry);
539 }
540
541 assert(!exec_list_is_empty(&if_stmt->then_list));
542 assert(!exec_list_is_empty(&if_stmt->else_list));
543
544 nir_cf_node *old_parent = state->parent_node;
545 state->parent_node = &if_stmt->cf_node;
546
547 exec_list_validate(&if_stmt->then_list);
548 foreach_list_typed(nir_cf_node, cf_node, node, &if_stmt->then_list) {
549 validate_cf_node(cf_node, state);
550 }
551
552 exec_list_validate(&if_stmt->else_list);
553 foreach_list_typed(nir_cf_node, cf_node, node, &if_stmt->else_list) {
554 validate_cf_node(cf_node, state);
555 }
556
557 state->parent_node = old_parent;
558 }
559
560 static void
561 validate_loop(nir_loop *loop, validate_state *state)
562 {
563 assert(!exec_node_is_head_sentinel(loop->cf_node.node.prev));
564 nir_cf_node *prev_node = nir_cf_node_prev(&loop->cf_node);
565 assert(prev_node->type == nir_cf_node_block);
566
567 nir_block *prev_block = nir_cf_node_as_block(prev_node);
568 assert(&prev_block->successors[0]->cf_node == nir_loop_first_cf_node(loop));
569 assert(prev_block->successors[1] == NULL);
570
571 assert(!exec_node_is_tail_sentinel(loop->cf_node.node.next));
572 nir_cf_node *next_node = nir_cf_node_next(&loop->cf_node);
573 assert(next_node->type == nir_cf_node_block);
574
575 assert(!exec_list_is_empty(&loop->body));
576
577 nir_cf_node *old_parent = state->parent_node;
578 state->parent_node = &loop->cf_node;
579
580 exec_list_validate(&loop->body);
581 foreach_list_typed(nir_cf_node, cf_node, node, &loop->body) {
582 validate_cf_node(cf_node, state);
583 }
584
585 state->parent_node = old_parent;
586 }
587
588 static void
589 validate_cf_node(nir_cf_node *node, validate_state *state)
590 {
591 assert(node->parent == state->parent_node);
592
593 switch (node->type) {
594 case nir_cf_node_block:
595 validate_block(nir_cf_node_as_block(node), state);
596 break;
597
598 case nir_cf_node_if:
599 validate_if(nir_cf_node_as_if(node), state);
600 break;
601
602 case nir_cf_node_loop:
603 validate_loop(nir_cf_node_as_loop(node), state);
604 break;
605
606 default:
607 assert(!"Invalid ALU instruction type");
608 break;
609 }
610 }
611
612 static void
613 prevalidate_reg_decl(nir_register *reg, bool is_global, validate_state *state)
614 {
615 assert(reg->is_global == is_global);
616
617 reg_validate_state *reg_state = ralloc(state->regs, reg_validate_state);
618 reg_state->uses = _mesa_set_create(reg_state, _mesa_key_pointer_equal);
619 reg_state->defs = _mesa_set_create(reg_state, _mesa_key_pointer_equal);
620
621 reg_state->where_defined = is_global ? NULL : state->impl;
622
623 _mesa_hash_table_insert(state->regs, reg, reg_state);
624 }
625
626 static void
627 postvalidate_reg_decl(nir_register *reg, validate_state *state)
628 {
629 struct hash_entry *entry = _mesa_hash_table_search(state->regs, reg);
630
631 reg_validate_state *reg_state = (reg_validate_state *) entry->data;
632
633 if (reg_state->uses->entries != reg->uses->entries) {
634 printf("extra entries in register uses:\n");
635 struct set_entry *entry;
636 set_foreach(reg->uses, entry) {
637 struct set_entry *entry2 =
638 _mesa_set_search(reg_state->uses, _mesa_hash_pointer(entry->key),
639 entry->key);
640
641 if (entry2 == NULL) {
642 printf("%p\n", entry->key);
643 }
644 }
645
646 abort();
647 }
648
649 if (reg_state->defs->entries != reg->defs->entries) {
650 printf("extra entries in register defs:\n");
651 struct set_entry *entry;
652 set_foreach(reg->defs, entry) {
653 struct set_entry *entry2 =
654 _mesa_set_search(reg_state->defs, _mesa_hash_pointer(entry->key),
655 entry->key);
656
657 if (entry2 == NULL) {
658 printf("%p\n", entry->key);
659 }
660 }
661
662 abort();
663 }
664 }
665
666 static void
667 validate_var_decl(nir_variable *var, bool is_global, validate_state *state)
668 {
669 assert(is_global != (var->data.mode == nir_var_local));
670
671 /*
672 * TODO validate some things ir_validate.cpp does (requires more GLSL type
673 * support)
674 */
675
676 if (!is_global) {
677 _mesa_hash_table_insert(state->var_defs, var, state->impl);
678 }
679 }
680
681 static void
682 validate_function_impl(nir_function_impl *impl, validate_state *state)
683 {
684 assert(impl->overload->impl == impl);
685 assert(impl->cf_node.parent == NULL);
686
687 assert(impl->num_params == impl->overload->num_params);
688 for (unsigned i = 0; i < impl->num_params; i++)
689 assert(impl->params[i]->type == impl->overload->params[i].type);
690
691 if (glsl_type_is_void(impl->overload->return_type))
692 assert(impl->return_var == NULL);
693 else
694 assert(impl->return_var->type == impl->overload->return_type);
695
696 assert(exec_list_is_empty(&impl->end_block->instr_list));
697 assert(impl->end_block->successors[0] == NULL);
698 assert(impl->end_block->successors[1] == NULL);
699
700 state->impl = impl;
701 state->parent_node = &impl->cf_node;
702
703 exec_list_validate(&impl->locals);
704 foreach_list_typed(nir_variable, var, node, &impl->locals) {
705 validate_var_decl(var, false, state);
706 }
707
708 exec_list_validate(&impl->registers);
709 foreach_list_typed(nir_register, reg, node, &impl->registers) {
710 prevalidate_reg_decl(reg, false, state);
711 }
712
713 exec_list_validate(&impl->body);
714 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
715 validate_cf_node(node, state);
716 }
717
718 foreach_list_typed(nir_register, reg, node, &impl->registers) {
719 postvalidate_reg_decl(reg, state);
720 }
721 }
722
723 static void
724 validate_function_overload(nir_function_overload *overload,
725 validate_state *state)
726 {
727 if (overload->impl != NULL)
728 validate_function_impl(overload->impl, state);
729 }
730
731 static void
732 validate_function(nir_function *func, validate_state *state)
733 {
734 exec_list_validate(&func->overload_list);
735 foreach_list_typed(nir_function_overload, overload, node, &func->overload_list) {
736 assert(overload->function == func);
737 validate_function_overload(overload, state);
738 }
739 }
740
741 static void
742 init_validate_state(validate_state *state)
743 {
744 state->regs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
745 _mesa_key_pointer_equal);
746 state->ssa_defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
747 _mesa_key_pointer_equal);
748 state->var_defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
749 _mesa_key_pointer_equal);
750 }
751
752 static void
753 destroy_validate_state(validate_state *state)
754 {
755 _mesa_hash_table_destroy(state->regs, NULL);
756 _mesa_hash_table_destroy(state->ssa_defs, NULL);
757 _mesa_hash_table_destroy(state->var_defs, NULL);
758 }
759
760 void
761 nir_validate_shader(nir_shader *shader)
762 {
763 validate_state state;
764 init_validate_state(&state);
765
766 struct hash_entry *entry;
767 hash_table_foreach(shader->uniforms, entry) {
768 validate_var_decl((nir_variable *) entry->data, true, &state);
769 }
770
771 hash_table_foreach(shader->inputs, entry) {
772 validate_var_decl((nir_variable *) entry->data, true, &state);
773 }
774
775 hash_table_foreach(shader->outputs, entry) {
776 validate_var_decl((nir_variable *) entry->data, true, &state);
777 }
778
779 exec_list_validate(&shader->globals);
780 foreach_list_typed(nir_variable, var, node, &shader->globals) {
781 validate_var_decl(var, true, &state);
782 }
783
784 exec_list_validate(&shader->system_values);
785 foreach_list_typed(nir_variable, var, node, &shader->system_values) {
786 validate_var_decl(var, true, &state);
787 }
788
789 exec_list_validate(&shader->registers);
790 foreach_list_typed(nir_register, reg, node, &shader->registers) {
791 prevalidate_reg_decl(reg, true, &state);
792 }
793
794 exec_list_validate(&shader->functions);
795 foreach_list_typed(nir_function, func, node, &shader->functions) {
796 validate_function(func, &state);
797 }
798
799 foreach_list_typed(nir_register, reg, node, &shader->registers) {
800 postvalidate_reg_decl(reg, &state);
801 }
802
803 destroy_validate_state(&state);
804 }