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