• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2010 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
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * \file ir_validate.cpp
26  *
27  * Attempts to verify that various invariants of the IR tree are true.
28  *
29  * In particular, at the moment it makes sure that no single
30  * ir_instruction node except for ir_variable appears multiple times
31  * in the ir tree.  ir_variable does appear multiple times: Once as a
32  * declaration in an exec_list, and multiple times as the endpoint of
33  * a dereference chain.
34  */
35 
36 #include "ir.h"
37 #include "ir_hierarchical_visitor.h"
38 #include "linker_util.h"
39 #include "util/u_debug.h"
40 #include "util/hash_table.h"
41 #include "util/macros.h"
42 #include "util/set.h"
43 #include "compiler/glsl_types.h"
44 
45 namespace {
46 
47 class ir_validate : public ir_hierarchical_visitor {
48 public:
ir_validate()49    ir_validate()
50    {
51       this->ir_set = _mesa_pointer_set_create(NULL);
52 
53       this->current_function = NULL;
54 
55       this->callback_enter = ir_validate::validate_ir;
56       this->data_enter = ir_set;
57    }
58 
~ir_validate()59    ~ir_validate()
60    {
61       _mesa_set_destroy(this->ir_set, NULL);
62    }
63 
64    virtual ir_visitor_status visit(ir_variable *v);
65    virtual ir_visitor_status visit(ir_dereference_variable *ir);
66 
67    virtual ir_visitor_status visit_enter(ir_discard *ir);
68    virtual ir_visitor_status visit_enter(ir_if *ir);
69 
70    virtual ir_visitor_status visit_enter(ir_function *ir);
71    virtual ir_visitor_status visit_leave(ir_function *ir);
72    virtual ir_visitor_status visit_enter(ir_function_signature *ir);
73    virtual ir_visitor_status visit_enter(ir_return *ir);
74 
75    virtual ir_visitor_status visit_leave(ir_expression *ir);
76    virtual ir_visitor_status visit_leave(ir_swizzle *ir);
77 
78    virtual ir_visitor_status visit_enter(class ir_dereference_array *);
79    virtual ir_visitor_status visit_enter(class ir_dereference_record *);
80 
81    virtual ir_visitor_status visit_enter(ir_assignment *ir);
82    virtual ir_visitor_status visit_enter(ir_call *ir);
83 
84    static void validate_ir(ir_instruction *ir, void *data);
85 
86    ir_function *current_function;
87 
88    struct set *ir_set;
89 };
90 
91 } /* anonymous namespace */
92 
93 ir_visitor_status
visit(ir_dereference_variable * ir)94 ir_validate::visit(ir_dereference_variable *ir)
95 {
96    if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
97       printf("ir_dereference_variable @ %p does not specify a variable %p\n",
98 	     (void *) ir, (void *) ir->var);
99       abort();
100    }
101 
102    /* Compare types without arrays, because one side can be sized and
103     * the other unsized.
104     */
105    if (glsl_without_array(ir->var->type) != glsl_without_array(ir->type)) {
106       printf("ir_dereference_variable type is not equal to variable type: ");
107       ir->print();
108       printf("\n");
109       abort();
110    }
111 
112    if (_mesa_set_search(ir_set, ir->var) == NULL) {
113       printf("ir_dereference_variable @ %p specifies undeclared variable "
114 	     "`%s' @ %p\n",
115 	     (void *) ir, ir->var->name, (void *) ir->var);
116       abort();
117    }
118 
119    this->validate_ir(ir, this->data_enter);
120 
121    return visit_continue;
122 }
123 
124 ir_visitor_status
visit_enter(class ir_dereference_array * ir)125 ir_validate::visit_enter(class ir_dereference_array *ir)
126 {
127    if (!glsl_type_is_array(ir->array->type) && !glsl_type_is_matrix(ir->array->type) &&
128       !glsl_type_is_vector(ir->array->type)) {
129       printf("ir_dereference_array @ %p does not specify an array, a vector "
130              "or a matrix\n",
131              (void *) ir);
132       ir->print();
133       printf("\n");
134       abort();
135    }
136 
137    if (glsl_type_is_array(ir->array->type)) {
138       if (ir->array->type->fields.array != ir->type) {
139          printf("ir_dereference_array type is not equal to the array "
140                 "element type: ");
141          ir->print();
142          printf("\n");
143          abort();
144       }
145    } else if (ir->array->type->base_type != ir->type->base_type) {
146       printf("ir_dereference_array base types are not equal: ");
147       ir->print();
148       printf("\n");
149       abort();
150    }
151 
152    if (!glsl_type_is_scalar(ir->array_index->type)) {
153       printf("ir_dereference_array @ %p does not have scalar index: %s\n",
154              (void *) ir, glsl_get_type_name(ir->array_index->type));
155       abort();
156    }
157 
158    if (!glsl_type_is_integer_16_32(ir->array_index->type)) {
159       printf("ir_dereference_array @ %p does not have integer index: %s\n",
160              (void *) ir, glsl_get_type_name(ir->array_index->type));
161       abort();
162    }
163 
164    return visit_continue;
165 }
166 
167 ir_visitor_status
visit_enter(class ir_dereference_record * ir)168 ir_validate::visit_enter(class ir_dereference_record *ir)
169 {
170    if (!glsl_type_is_struct(ir->record->type) && !glsl_type_is_interface(ir->record->type)) {
171       printf("ir_dereference_record @ %p does not specify a record\n",
172              (void *) ir);
173       ir->print();
174       printf("\n");
175       abort();
176    }
177 
178    if (ir->record->type->fields.structure[ir->field_idx].type != ir->type) {
179       printf("ir_dereference_record type is not equal to the record "
180              "field type: ");
181       ir->print();
182       printf("\n");
183       abort();
184    }
185 
186    return visit_continue;
187 }
188 
189 ir_visitor_status
visit_enter(ir_discard * ir)190 ir_validate::visit_enter(ir_discard *ir)
191 {
192    if (ir->condition && ir->condition->type != &glsl_type_builtin_bool) {
193       printf("ir_discard condition %s type instead of bool.\n",
194 	     glsl_get_type_name(ir->condition->type));
195       ir->print();
196       printf("\n");
197       abort();
198    }
199 
200    return visit_continue;
201 }
202 
203 ir_visitor_status
visit_enter(ir_if * ir)204 ir_validate::visit_enter(ir_if *ir)
205 {
206    if (ir->condition->type != &glsl_type_builtin_bool) {
207       printf("ir_if condition %s type instead of bool.\n",
208 	     glsl_get_type_name(ir->condition->type));
209       ir->print();
210       printf("\n");
211       abort();
212    }
213 
214    return visit_continue;
215 }
216 
217 
218 ir_visitor_status
visit_enter(ir_function * ir)219 ir_validate::visit_enter(ir_function *ir)
220 {
221    /* Function definitions cannot be nested.
222     */
223    if (this->current_function != NULL) {
224       printf("Function definition nested inside another function "
225 	     "definition:\n");
226       printf("%s %p inside %s %p\n",
227 	     ir->name, (void *) ir,
228 	     this->current_function->name, (void *) this->current_function);
229       abort();
230    }
231 
232    /* Store the current function hierarchy being traversed.  This is used
233     * by the function signature visitor to ensure that the signatures are
234     * linked with the correct functions.
235     */
236    this->current_function = ir;
237 
238    this->validate_ir(ir, this->data_enter);
239 
240    /* Verify that all of the things stored in the list of signatures are,
241     * in fact, function signatures.
242     */
243    foreach_in_list(ir_instruction, sig, &ir->signatures) {
244       if (sig->ir_type != ir_type_function_signature) {
245 	 printf("Non-signature in signature list of function `%s'\n",
246 		ir->name);
247 	 abort();
248       }
249    }
250 
251    return visit_continue;
252 }
253 
254 ir_visitor_status
visit_leave(ir_function * ir)255 ir_validate::visit_leave(ir_function *ir)
256 {
257    assert(ralloc_parent(ir->name) == ir);
258 
259    this->current_function = NULL;
260    return visit_continue;
261 }
262 
263 ir_visitor_status
visit_enter(ir_function_signature * ir)264 ir_validate::visit_enter(ir_function_signature *ir)
265 {
266    if (this->current_function != ir->function()) {
267       printf("Function signature nested inside wrong function "
268 	     "definition:\n");
269       printf("%p inside %s %p instead of %s %p\n",
270 	     (void *) ir,
271 	     this->current_function->name, (void *) this->current_function,
272 	     ir->function_name(), (void *) ir->function());
273       abort();
274    }
275 
276    if (ir->return_type == NULL) {
277       printf("Function signature %p for function %s has NULL return type.\n",
278 	     (void *) ir, ir->function_name());
279       abort();
280    }
281 
282    this->validate_ir(ir, this->data_enter);
283 
284    return visit_continue;
285 }
286 
287 ir_visitor_status
visit_enter(ir_return * ir)288 ir_validate::visit_enter(ir_return *ir)
289 {
290    if (!this->current_function) {
291       printf("Return statement outside of a function\n");
292       abort();
293    }
294 
295    return visit_continue;
296 }
297 
298 ir_visitor_status
visit_leave(ir_expression * ir)299 ir_validate::visit_leave(ir_expression *ir)
300 {
301    for (unsigned i = ir->num_operands; i < 4; i++) {
302       assert(ir->operands[i] == NULL);
303    }
304 
305    for (unsigned i = 0; i < ir->num_operands; i++) {
306       assert(ir->operands[i] != NULL);
307    }
308 
309    switch (ir->operation) {
310    case ir_unop_bit_not:
311       assert(ir->operands[0]->type == ir->type);
312       break;
313    case ir_unop_logic_not:
314       assert(glsl_type_is_boolean(ir->type));
315       assert(glsl_type_is_boolean(ir->operands[0]->type));
316       break;
317 
318    case ir_unop_neg:
319       assert(ir->type == ir->operands[0]->type);
320       break;
321 
322    case ir_unop_abs:
323    case ir_unop_sign:
324       assert(glsl_type_is_int_16_32_64(ir->operands[0]->type) ||
325              glsl_type_is_float_16_32_64(ir->operands[0]->type));
326       assert(ir->type == ir->operands[0]->type);
327       break;
328 
329    case ir_unop_rcp:
330    case ir_unop_rsq:
331    case ir_unop_sqrt:
332       assert(glsl_type_is_float_16_32_64(ir->type));
333       assert(ir->type == ir->operands[0]->type);
334       break;
335 
336    case ir_unop_exp:
337    case ir_unop_log:
338    case ir_unop_exp2:
339    case ir_unop_log2:
340    case ir_unop_saturate:
341       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
342       assert(ir->type == ir->operands[0]->type);
343       break;
344 
345    case ir_unop_f2i:
346       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
347       assert(glsl_type_is_int_16_32(ir->type));
348       break;
349    case ir_unop_f2u:
350       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
351       assert(glsl_type_is_uint_16_32(ir->type));
352       break;
353    case ir_unop_i2f:
354       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
355       assert(glsl_type_is_float_16_32(ir->type));
356       break;
357    case ir_unop_f2b:
358       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
359       assert(glsl_type_is_boolean(ir->type));
360       break;
361    case ir_unop_f162b:
362       assert(ir->operands[0]->type->base_type ==
363              GLSL_TYPE_FLOAT16);
364       assert(glsl_type_is_boolean(ir->type));
365       break;
366    case ir_unop_b2f:
367       assert(glsl_type_is_boolean(ir->operands[0]->type));
368       assert(glsl_type_is_float_16_32(ir->type));
369       break;
370    case ir_unop_b2f16:
371       assert(glsl_type_is_boolean(ir->operands[0]->type));
372       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
373       break;
374    case ir_unop_i2b:
375       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
376       assert(glsl_type_is_boolean(ir->type));
377       break;
378    case ir_unop_b2i:
379       assert(glsl_type_is_boolean(ir->operands[0]->type));
380       assert(glsl_type_is_int_16_32(ir->type));
381       break;
382    case ir_unop_u2f:
383       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
384       assert(glsl_type_is_float_16_32(ir->type));
385       break;
386    case ir_unop_i2u:
387       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
388       assert(glsl_type_is_uint_16_32(ir->type));
389       break;
390    case ir_unop_u2i:
391       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
392       assert(glsl_type_is_int_16_32(ir->type));
393       break;
394    case ir_unop_bitcast_i2f:
395       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
396       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
397       break;
398    case ir_unop_bitcast_f2i:
399       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
400       assert(ir->type->base_type == GLSL_TYPE_INT);
401       break;
402    case ir_unop_bitcast_u2f:
403       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
404       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
405       break;
406    case ir_unop_bitcast_f2u:
407       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
408       assert(ir->type->base_type == GLSL_TYPE_UINT);
409       break;
410 
411    case ir_unop_bitcast_u642d:
412       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
413       assert(glsl_type_is_double(ir->type));
414       break;
415    case ir_unop_bitcast_i642d:
416       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
417       assert(glsl_type_is_double(ir->type));
418       break;
419    case ir_unop_bitcast_d2u64:
420       assert(glsl_type_is_double(ir->operands[0]->type));
421       assert(ir->type->base_type == GLSL_TYPE_UINT64);
422       break;
423    case ir_unop_bitcast_d2i64:
424       assert(glsl_type_is_double(ir->operands[0]->type));
425       assert(ir->type->base_type == GLSL_TYPE_INT64);
426       break;
427    case ir_unop_i642i:
428       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
429       assert(glsl_type_is_int_16_32(ir->type));
430       break;
431    case ir_unop_u642i:
432       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
433       assert(glsl_type_is_int_16_32(ir->type));
434       break;
435    case ir_unop_i642u:
436       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
437       assert(glsl_type_is_uint_16_32(ir->type));
438       break;
439    case ir_unop_u642u:
440       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
441       assert(glsl_type_is_uint_16_32(ir->type));
442       break;
443    case ir_unop_i642b:
444       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
445       assert(glsl_type_is_boolean(ir->type));
446       break;
447    case ir_unop_i642f:
448       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
449       assert(glsl_type_is_float(ir->type));
450       break;
451    case ir_unop_u642f:
452       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
453       assert(glsl_type_is_float(ir->type));
454       break;
455    case ir_unop_i642d:
456       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
457       assert(glsl_type_is_double(ir->type));
458       break;
459    case ir_unop_u642d:
460       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
461       assert(glsl_type_is_double(ir->type));
462       break;
463    case ir_unop_i2i64:
464       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
465       assert(ir->type->base_type == GLSL_TYPE_INT64);
466       break;
467    case ir_unop_u2i64:
468       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
469       assert(ir->type->base_type == GLSL_TYPE_INT64);
470       break;
471    case ir_unop_b2i64:
472       assert(glsl_type_is_boolean(ir->operands[0]->type));
473       assert(ir->type->base_type == GLSL_TYPE_INT64);
474       break;
475    case ir_unop_f2i64:
476       assert(glsl_type_is_float(ir->operands[0]->type));
477       assert(ir->type->base_type == GLSL_TYPE_INT64);
478       break;
479    case ir_unop_d2i64:
480       assert(glsl_type_is_double(ir->operands[0]->type));
481       assert(ir->type->base_type == GLSL_TYPE_INT64);
482       break;
483    case ir_unop_i2u64:
484       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
485       assert(ir->type->base_type == GLSL_TYPE_UINT64);
486       break;
487    case ir_unop_u2u64:
488       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
489       assert(ir->type->base_type == GLSL_TYPE_UINT64);
490       break;
491    case ir_unop_f2u64:
492       assert(glsl_type_is_float(ir->operands[0]->type));
493       assert(ir->type->base_type == GLSL_TYPE_UINT64);
494       break;
495    case ir_unop_d2u64:
496       assert(glsl_type_is_double(ir->operands[0]->type));
497       assert(ir->type->base_type == GLSL_TYPE_UINT64);
498       break;
499    case ir_unop_u642i64:
500       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
501       assert(ir->type->base_type == GLSL_TYPE_INT64);
502       break;
503    case ir_unop_i642u64:
504       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
505       assert(ir->type->base_type == GLSL_TYPE_UINT64);
506       break;
507    case ir_unop_trunc:
508    case ir_unop_round_even:
509    case ir_unop_ceil:
510    case ir_unop_floor:
511    case ir_unop_fract:
512       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
513       assert(ir->operands[0]->type == ir->type);
514       break;
515    case ir_unop_sin:
516    case ir_unop_cos:
517    case ir_unop_dFdx:
518    case ir_unop_dFdx_coarse:
519    case ir_unop_dFdx_fine:
520    case ir_unop_dFdy:
521    case ir_unop_dFdy_coarse:
522    case ir_unop_dFdy_fine:
523       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
524       assert(ir->operands[0]->type == ir->type);
525       break;
526 
527    case ir_unop_pack_snorm_2x16:
528    case ir_unop_pack_unorm_2x16:
529    case ir_unop_pack_half_2x16:
530       assert(ir->type == &glsl_type_builtin_uint);
531       assert(ir->operands[0]->type == &glsl_type_builtin_vec2);
532       break;
533 
534    case ir_unop_pack_snorm_4x8:
535    case ir_unop_pack_unorm_4x8:
536       assert(ir->type == &glsl_type_builtin_uint);
537       assert(ir->operands[0]->type == &glsl_type_builtin_vec4);
538       break;
539 
540    case ir_unop_pack_double_2x32:
541       assert(ir->type == &glsl_type_builtin_double);
542       assert(ir->operands[0]->type == &glsl_type_builtin_uvec2);
543       break;
544 
545    case ir_unop_pack_int_2x32:
546       assert(ir->type == &glsl_type_builtin_int64_t);
547       assert(ir->operands[0]->type == &glsl_type_builtin_ivec2);
548       break;
549 
550    case ir_unop_pack_uint_2x32:
551       assert(ir->type == &glsl_type_builtin_uint64_t);
552       assert(ir->operands[0]->type == &glsl_type_builtin_uvec2);
553       break;
554 
555    case ir_unop_pack_sampler_2x32:
556       assert(glsl_type_is_sampler(ir->type));
557       assert(ir->operands[0]->type == &glsl_type_builtin_uvec2);
558       break;
559 
560    case ir_unop_pack_image_2x32:
561       assert(glsl_type_is_image(ir->type));
562       assert(ir->operands[0]->type == &glsl_type_builtin_uvec2);
563       break;
564 
565    case ir_unop_unpack_snorm_2x16:
566    case ir_unop_unpack_unorm_2x16:
567    case ir_unop_unpack_half_2x16:
568       assert(ir->type == &glsl_type_builtin_vec2);
569       assert(ir->operands[0]->type == &glsl_type_builtin_uint);
570       break;
571 
572    case ir_unop_unpack_snorm_4x8:
573    case ir_unop_unpack_unorm_4x8:
574       assert(ir->type == &glsl_type_builtin_vec4);
575       assert(ir->operands[0]->type == &glsl_type_builtin_uint);
576       break;
577 
578    case ir_unop_unpack_double_2x32:
579       assert(ir->type == &glsl_type_builtin_uvec2);
580       assert(ir->operands[0]->type == &glsl_type_builtin_double);
581       break;
582 
583    case ir_unop_unpack_int_2x32:
584       assert(ir->type == &glsl_type_builtin_ivec2);
585       assert(ir->operands[0]->type == &glsl_type_builtin_int64_t);
586       break;
587 
588    case ir_unop_unpack_uint_2x32:
589       assert(ir->type == &glsl_type_builtin_uvec2);
590       assert(ir->operands[0]->type == &glsl_type_builtin_uint64_t);
591       break;
592 
593    case ir_unop_unpack_sampler_2x32:
594       assert(ir->type == &glsl_type_builtin_uvec2);
595       assert(glsl_type_is_sampler(ir->operands[0]->type));
596       break;
597 
598    case ir_unop_unpack_image_2x32:
599       assert(ir->type == &glsl_type_builtin_uvec2);
600       assert(glsl_type_is_image(ir->operands[0]->type));
601       break;
602 
603    case ir_unop_bitfield_reverse:
604       assert(ir->operands[0]->type == ir->type);
605       assert(glsl_type_is_integer_32(ir->type));
606       break;
607 
608    case ir_unop_bit_count:
609    case ir_unop_find_msb:
610    case ir_unop_find_lsb:
611       assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
612       assert(glsl_type_is_integer_16_32(ir->operands[0]->type));
613       assert(glsl_type_is_int_16_32(ir->type));
614       break;
615 
616    case ir_unop_clz:
617       assert(ir->operands[0]->type == ir->type);
618       assert(glsl_type_is_uint_16_32(ir->type));
619       break;
620 
621    case ir_unop_interpolate_at_centroid:
622       assert(ir->operands[0]->type == ir->type);
623       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
624       break;
625 
626    case ir_unop_get_buffer_size:
627       assert(ir->type == &glsl_type_builtin_int);
628       assert(ir->operands[0]->type == &glsl_type_builtin_uint);
629       break;
630 
631    case ir_unop_ssbo_unsized_array_length:
632       assert(ir->type == &glsl_type_builtin_int);
633       assert(glsl_type_is_array(ir->operands[0]->type));
634       assert(glsl_type_is_unsized_array(ir->operands[0]->type));
635       break;
636 
637    case ir_unop_implicitly_sized_array_length:
638       assert(ir->type == &glsl_type_builtin_int);
639       assert(glsl_type_is_array(ir->operands[0]->type));
640       break;
641 
642    case ir_unop_d2f:
643       assert(glsl_type_is_double(ir->operands[0]->type));
644       assert(glsl_type_is_float(ir->type));
645       break;
646    case ir_unop_f2d:
647       assert(glsl_type_is_float(ir->operands[0]->type));
648       assert(glsl_type_is_double(ir->type));
649       break;
650    case ir_unop_f162f:
651       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
652       assert(glsl_type_is_float(ir->type));
653       break;
654    case ir_unop_f2f16:
655    case ir_unop_f2fmp:
656       assert(glsl_type_is_float(ir->operands[0]->type));
657       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
658       break;
659    case ir_unop_i2i:
660       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
661       assert(glsl_type_is_int_16_32(ir->type));
662       assert(ir->type->base_type != ir->operands[0]->type->base_type);
663       break;
664    case ir_unop_u2u:
665       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
666       assert(glsl_type_is_uint_16_32(ir->type));
667       assert(ir->type->base_type != ir->operands[0]->type->base_type);
668       break;
669    case ir_unop_i2imp:
670       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
671       assert(ir->type->base_type == GLSL_TYPE_INT16);
672       break;
673    case ir_unop_u2ump:
674       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
675       assert(ir->type->base_type == GLSL_TYPE_UINT16);
676       break;
677    case ir_unop_d2i:
678       assert(glsl_type_is_double(ir->operands[0]->type));
679       assert(glsl_type_is_int_16_32(ir->type));
680       break;
681    case ir_unop_i2d:
682       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
683       assert(glsl_type_is_double(ir->type));
684       break;
685    case ir_unop_d2u:
686       assert(glsl_type_is_double(ir->operands[0]->type));
687       assert(glsl_type_is_uint_16_32(ir->type));
688       break;
689    case ir_unop_u2d:
690       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
691       assert(glsl_type_is_double(ir->type));
692       break;
693    case ir_unop_d2b:
694       assert(glsl_type_is_double(ir->operands[0]->type));
695       assert(glsl_type_is_boolean(ir->type));
696       break;
697    case ir_unop_u2f16:
698       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
699       assert(glsl_type_is_uint_16_32(ir->operands[0]->type));
700       break;
701    case ir_unop_f162u:
702       assert(glsl_type_is_uint_16_32(ir->type));
703       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
704       break;
705    case ir_unop_i2f16:
706       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
707       assert(glsl_type_is_int_16_32(ir->operands[0]->type));
708       break;
709    case ir_unop_f162i:
710       assert(glsl_type_is_int_16_32(ir->type));
711       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
712       break;
713    case ir_unop_d2f16:
714       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
715       assert(glsl_type_is_double(ir->operands[0]->type));
716       break;
717    case ir_unop_f162d:
718       assert(glsl_type_is_double(ir->type));
719       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
720       break;
721    case ir_unop_u642f16:
722       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
723       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
724       break;
725    case ir_unop_f162u64:
726       assert(ir->type->base_type == GLSL_TYPE_UINT64);
727       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
728       break;
729    case ir_unop_i642f16:
730       assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
731       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
732       break;
733    case ir_unop_f162i64:
734       assert(ir->type->base_type == GLSL_TYPE_INT64);
735       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
736       break;
737 
738    case ir_unop_frexp_sig:
739       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
740       break;
741    case ir_unop_frexp_exp:
742       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
743       assert(ir->type->base_type == GLSL_TYPE_INT);
744       break;
745    case ir_unop_subroutine_to_int:
746       assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
747       assert(ir->type->base_type == GLSL_TYPE_INT);
748       break;
749 
750    case ir_unop_atan:
751       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
752       assert(ir->type == ir->operands[0]->type);
753       break;
754 
755    case ir_binop_add:
756    case ir_binop_sub:
757    case ir_binop_mul:
758    case ir_binop_div:
759    case ir_binop_mod:
760    case ir_binop_min:
761    case ir_binop_max:
762    case ir_binop_pow:
763       assert(ir->operands[0]->type->base_type ==
764              ir->operands[1]->type->base_type);
765 
766       if (ir->operation == ir_binop_mul &&
767           (ir->type->base_type == GLSL_TYPE_UINT64 ||
768            ir->type->base_type == GLSL_TYPE_INT64) &&
769           (glsl_type_is_int_16_32(ir->operands[0]->type)||
770            glsl_type_is_int_16_32(ir->operands[1]->type)||
771            glsl_type_is_uint_16_32(ir->operands[0]->type) ||
772            glsl_type_is_uint_16_32(ir->operands[1]->type))) {
773          assert(ir->operands[0]->type == ir->operands[1]->type);
774          break;
775       }
776 
777       if (glsl_type_is_scalar(ir->operands[0]->type))
778 	 assert(ir->operands[1]->type == ir->type);
779       else if (glsl_type_is_scalar(ir->operands[1]->type))
780 	 assert(ir->operands[0]->type == ir->type);
781       else if (glsl_type_is_vector(ir->operands[0]->type) &&
782 	       glsl_type_is_vector(ir->operands[1]->type)) {
783 	 assert(ir->operands[0]->type == ir->operands[1]->type);
784 	 assert(ir->operands[0]->type == ir->type);
785       }
786       break;
787 
788    case ir_binop_abs_sub:
789       assert(ir->operands[0]->type == ir->operands[1]->type);
790       assert(glsl_type_is_integer_16_32_64(ir->operands[0]->type));
791       assert(ir->operands[0]->type->vector_elements ==
792              ir->type->vector_elements);
793       assert(glsl_type_is_uint_16_32_64(ir->type));
794       break;
795 
796    case ir_binop_add_sat:
797    case ir_binop_sub_sat:
798    case ir_binop_avg:
799    case ir_binop_avg_round:
800       assert(ir->type == ir->operands[0]->type);
801       assert(ir->type == ir->operands[1]->type);
802       assert(glsl_type_is_integer_16_32_64(ir->type));
803       break;
804 
805    case ir_binop_mul_32x16:
806    case ir_binop_imul_high:
807       assert(ir->type == ir->operands[0]->type);
808       assert(ir->type == ir->operands[1]->type);
809       assert(glsl_type_is_integer_32(ir->type));
810       break;
811 
812    case ir_binop_carry:
813    case ir_binop_borrow:
814       assert(ir->type == ir->operands[0]->type);
815       assert(ir->type == ir->operands[1]->type);
816       assert(ir->type->base_type == GLSL_TYPE_UINT);
817       break;
818 
819    case ir_binop_less:
820    case ir_binop_gequal:
821    case ir_binop_equal:
822    case ir_binop_nequal:
823       /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
824        * ==, and != operators.  The IR operators perform a component-wise
825        * comparison on scalar or vector types and return a boolean scalar or
826        * vector type of the same size.
827        */
828       assert(glsl_type_is_boolean(ir->type));
829       assert(ir->operands[0]->type == ir->operands[1]->type);
830       assert(glsl_type_is_vector(ir->operands[0]->type)
831 	     || glsl_type_is_scalar(ir->operands[0]->type));
832       assert(ir->operands[0]->type->vector_elements
833 	     == ir->type->vector_elements);
834       break;
835 
836    case ir_binop_all_equal:
837    case ir_binop_any_nequal:
838       /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
839        * return a scalar boolean.  The IR matches that.
840        */
841       assert(ir->type == &glsl_type_builtin_bool);
842       assert(ir->operands[0]->type == ir->operands[1]->type);
843       break;
844 
845    case ir_binop_lshift:
846    case ir_binop_rshift:
847       assert(glsl_type_is_integer_16_32_64(ir->operands[0]->type) &&
848              glsl_type_is_integer_16_32_64(ir->operands[1]->type));
849       if (glsl_type_is_scalar(ir->operands[0]->type)) {
850           assert(glsl_type_is_scalar(ir->operands[1]->type));
851       }
852       if (glsl_type_is_vector(ir->operands[0]->type) &&
853           glsl_type_is_vector(ir->operands[1]->type)) {
854           assert(glsl_get_components(ir->operands[0]->type) ==
855                  glsl_get_components(ir->operands[1]->type));
856       }
857       assert(ir->type == ir->operands[0]->type);
858       break;
859 
860    case ir_binop_bit_and:
861    case ir_binop_bit_xor:
862    case ir_binop_bit_or:
863        assert(ir->operands[0]->type->base_type ==
864               ir->operands[1]->type->base_type);
865        assert(glsl_type_is_integer_16_32_64(ir->type));
866        if (glsl_type_is_vector(ir->operands[0]->type) &&
867            glsl_type_is_vector(ir->operands[1]->type)) {
868            assert(ir->operands[0]->type->vector_elements ==
869                   ir->operands[1]->type->vector_elements);
870        }
871        break;
872 
873    case ir_binop_logic_and:
874    case ir_binop_logic_xor:
875    case ir_binop_logic_or:
876       assert(glsl_type_is_boolean(ir->type));
877       assert(glsl_type_is_boolean(ir->operands[0]->type));
878       assert(glsl_type_is_boolean(ir->operands[1]->type));
879       break;
880 
881    case ir_binop_dot:
882       assert(ir->type == &glsl_type_builtin_float ||
883              ir->type == &glsl_type_builtin_double ||
884              ir->type == &glsl_type_builtin_float16_t);
885       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
886       assert(glsl_type_is_vector(ir->operands[0]->type));
887       assert(ir->operands[0]->type == ir->operands[1]->type);
888       break;
889 
890    case ir_binop_ldexp:
891       assert(ir->operands[0]->type == ir->type);
892       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
893       assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
894       assert(glsl_get_components(ir->operands[0]->type) ==
895              glsl_get_components(ir->operands[1]->type));
896       break;
897 
898    case ir_binop_vector_extract:
899       assert(glsl_type_is_vector(ir->operands[0]->type));
900       assert(glsl_type_is_scalar(ir->operands[1]->type)
901              && glsl_type_is_integer_16_32(ir->operands[1]->type));
902       break;
903 
904    case ir_binop_interpolate_at_offset:
905       assert(ir->operands[0]->type == ir->type);
906       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
907       assert(glsl_get_components(ir->operands[1]->type) == 2);
908       assert(glsl_type_is_float_16_32(ir->operands[1]->type));
909       break;
910 
911    case ir_binop_interpolate_at_sample:
912       assert(ir->operands[0]->type == ir->type);
913       assert(glsl_type_is_float_16_32(ir->operands[0]->type));
914       assert(ir->operands[1]->type == &glsl_type_builtin_int ||
915              ir->operands[1]->type == &glsl_type_builtin_int16_t);
916       break;
917 
918    case ir_binop_atan2:
919       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
920       assert(ir->operands[1]->type == ir->operands[0]->type);
921       assert(ir->type == ir->operands[0]->type);
922       break;
923 
924    case ir_triop_fma:
925       assert(glsl_type_is_float_16_32_64(ir->type));
926       assert(ir->type == ir->operands[0]->type);
927       assert(ir->type == ir->operands[1]->type);
928       assert(ir->type == ir->operands[2]->type);
929       break;
930 
931    case ir_triop_lrp:
932       assert(glsl_type_is_float_16_32_64(ir->operands[0]->type));
933       assert(ir->operands[0]->type == ir->operands[1]->type);
934       assert(ir->operands[2]->type == ir->operands[0]->type ||
935              ir->operands[2]->type == &glsl_type_builtin_float ||
936              ir->operands[2]->type == &glsl_type_builtin_double ||
937              ir->operands[2]->type == &glsl_type_builtin_float16_t);
938       break;
939 
940    case ir_triop_csel:
941       assert(glsl_type_is_boolean(ir->operands[0]->type));
942       assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
943       assert(ir->type == ir->operands[1]->type);
944       assert(ir->type == ir->operands[2]->type);
945       break;
946 
947    case ir_triop_bitfield_extract:
948       assert(glsl_type_is_integer_16_32(ir->type));
949       assert(ir->operands[0]->type == ir->type);
950       assert(ir->operands[1]->type == ir->type);
951       assert(ir->operands[2]->type == ir->type);
952       break;
953 
954    case ir_triop_vector_insert:
955       assert(glsl_type_is_vector(ir->operands[0]->type));
956       assert(glsl_type_is_scalar(ir->operands[1]->type));
957       assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
958       assert(glsl_type_is_scalar(ir->operands[2]->type)
959              && glsl_type_is_integer_16_32(ir->operands[2]->type));
960       assert(ir->type == ir->operands[0]->type);
961       break;
962 
963    case ir_quadop_bitfield_insert:
964       assert(glsl_type_is_integer_16_32(ir->type));
965       assert(ir->operands[0]->type == ir->type);
966       assert(ir->operands[1]->type == ir->type);
967       assert(ir->operands[2]->type == ir->type);
968       assert(ir->operands[3]->type == ir->type);
969       break;
970 
971    case ir_quadop_vector:
972       /* The vector operator collects some number of scalars and generates a
973        * vector from them.
974        *
975        *  - All of the operands must be scalar.
976        *  - Number of operands must matche the size of the resulting vector.
977        *  - Base type of the operands must match the base type of the result.
978        */
979       switch (ir->type->vector_elements) {
980       case 1:
981          assert(glsl_type_is_scalar(ir->operands[0]->type));
982          assert(ir->operands[0]->type->base_type == ir->type->base_type);
983          assert(ir->operands[1] == NULL);
984          assert(ir->operands[2] == NULL);
985          assert(ir->operands[3] == NULL);
986          break;
987       case 2:
988 	 assert(glsl_type_is_scalar(ir->operands[0]->type));
989 	 assert(ir->operands[0]->type->base_type == ir->type->base_type);
990 	 assert(glsl_type_is_scalar(ir->operands[1]->type));
991 	 assert(ir->operands[1]->type->base_type == ir->type->base_type);
992 	 assert(ir->operands[2] == NULL);
993 	 assert(ir->operands[3] == NULL);
994 	 break;
995       case 3:
996 	 assert(glsl_type_is_scalar(ir->operands[0]->type));
997 	 assert(ir->operands[0]->type->base_type == ir->type->base_type);
998 	 assert(glsl_type_is_scalar(ir->operands[1]->type));
999 	 assert(ir->operands[1]->type->base_type == ir->type->base_type);
1000 	 assert(glsl_type_is_scalar(ir->operands[2]->type));
1001 	 assert(ir->operands[2]->type->base_type == ir->type->base_type);
1002 	 assert(ir->operands[3] == NULL);
1003 	 break;
1004       case 4:
1005 	 assert(glsl_type_is_scalar(ir->operands[0]->type));
1006 	 assert(ir->operands[0]->type->base_type == ir->type->base_type);
1007 	 assert(glsl_type_is_scalar(ir->operands[1]->type));
1008 	 assert(ir->operands[1]->type->base_type == ir->type->base_type);
1009 	 assert(glsl_type_is_scalar(ir->operands[2]->type));
1010 	 assert(ir->operands[2]->type->base_type == ir->type->base_type);
1011 	 assert(glsl_type_is_scalar(ir->operands[3]->type));
1012 	 assert(ir->operands[3]->type->base_type == ir->type->base_type);
1013 	 break;
1014       default:
1015 	 /* The is_vector assertion above should prevent execution from ever
1016 	  * getting here.
1017 	  */
1018 	 assert(!"Should not get here.");
1019 	 break;
1020       }
1021    }
1022 
1023    return visit_continue;
1024 }
1025 
1026 ir_visitor_status
visit_leave(ir_swizzle * ir)1027 ir_validate::visit_leave(ir_swizzle *ir)
1028 {
1029    unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
1030 
1031    for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1032       if (chans[i] >= ir->val->type->vector_elements) {
1033 	 printf("ir_swizzle @ %p specifies a channel not present "
1034 		"in the value.\n", (void *) ir);
1035 	 ir->print();
1036 	 abort();
1037       }
1038    }
1039 
1040    return visit_continue;
1041 }
1042 
1043 ir_visitor_status
visit(ir_variable * ir)1044 ir_validate::visit(ir_variable *ir)
1045 {
1046    /* An ir_variable is the one thing that can (and will) appear multiple times
1047     * in an IR tree.  It is added to the hashtable so that it can be used
1048     * in the ir_dereference_variable handler to ensure that a variable is
1049     * declared before it is dereferenced.
1050     */
1051    if (ir->name && ir->is_name_ralloced())
1052       assert(ralloc_parent(ir->name) == ir);
1053 
1054    _mesa_set_add(ir_set, ir);
1055 
1056    /* If a variable is an array, verify that the maximum array index is in
1057     * bounds.  There was once an error in AST-to-HIR conversion that set this
1058     * to be out of bounds.
1059     */
1060    if (glsl_array_size(ir->type) > 0) {
1061       if (ir->data.max_array_access >= (int)ir->type->length) {
1062 	 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
1063 		ir->data.max_array_access, ir->type->length - 1);
1064 	 ir->print();
1065 	 abort();
1066       }
1067    }
1068 
1069    /* If a variable is an interface block (or an array of interface blocks),
1070     * verify that the maximum array index for each interface member is in
1071     * bounds.
1072     */
1073    if (ir->is_interface_instance()) {
1074       const glsl_struct_field *fields =
1075          ir->get_interface_type()->fields.structure;
1076       for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
1077          if (glsl_array_size(fields[i].type) > 0 &&
1078              !fields[i].implicit_sized_array) {
1079             const int *const max_ifc_array_access =
1080                ir->get_max_ifc_array_access();
1081 
1082             assert(max_ifc_array_access != NULL);
1083 
1084             if (max_ifc_array_access[i] >= (int)fields[i].type->length) {
1085                printf("ir_variable has maximum access out of bounds for "
1086                       "field %s (%d vs %d)\n", fields[i].name,
1087                       max_ifc_array_access[i], fields[i].type->length);
1088                ir->print();
1089                abort();
1090             }
1091          }
1092       }
1093    }
1094 
1095    if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
1096       printf("ir_variable didn't have an initializer, but has a constant "
1097 	     "initializer value.\n");
1098       ir->print();
1099       abort();
1100    }
1101 
1102    if (ir->data.mode == ir_var_uniform
1103        && is_gl_identifier(ir->name)
1104        && ir->get_state_slots() == NULL) {
1105       printf("built-in uniform has no state\n");
1106       ir->print();
1107       abort();
1108    }
1109 
1110    return visit_continue;
1111 }
1112 
1113 ir_visitor_status
visit_enter(ir_assignment * ir)1114 ir_validate::visit_enter(ir_assignment *ir)
1115 {
1116    const ir_dereference *const lhs = ir->lhs;
1117    if (glsl_type_is_scalar(lhs->type) || glsl_type_is_vector(lhs->type)) {
1118       if (ir->write_mask == 0) {
1119 	 printf("Assignment LHS is %s, but write mask is 0:\n",
1120 		glsl_type_is_scalar(lhs->type) ? "scalar" : "vector");
1121 	 ir->print();
1122 	 abort();
1123       }
1124 
1125       int lhs_components = 0;
1126       for (int i = 0; i < 4; i++) {
1127 	 if (ir->write_mask & (1 << i))
1128 	    lhs_components++;
1129       }
1130 
1131       if (lhs_components != ir->rhs->type->vector_elements) {
1132 	 printf("Assignment count of LHS write mask channels enabled not\n"
1133 		"matching RHS vector size (%d LHS, %d RHS).\n",
1134 		lhs_components, ir->rhs->type->vector_elements);
1135 	 ir->print();
1136 	 abort();
1137       }
1138    }
1139 
1140    if (lhs->type->base_type != ir->rhs->type->base_type) {
1141       printf("Assignment LHS and RHS base types are different:\n");
1142       lhs->print();
1143       printf("\n");
1144       ir->rhs->print();
1145       printf("\n");
1146       abort();
1147    }
1148 
1149    this->validate_ir(ir, this->data_enter);
1150 
1151    return visit_continue;
1152 }
1153 
1154 ir_visitor_status
visit_enter(ir_call * ir)1155 ir_validate::visit_enter(ir_call *ir)
1156 {
1157    ir_function_signature *const callee = ir->callee;
1158 
1159    if (callee->ir_type != ir_type_function_signature) {
1160       printf("IR called by ir_call is not ir_function_signature!\n");
1161       abort();
1162    }
1163 
1164    if (ir->return_deref) {
1165       if (ir->return_deref->type != callee->return_type) {
1166 	 printf("callee type %s does not match return storage type %s\n",
1167 	        glsl_get_type_name(callee->return_type), glsl_get_type_name(ir->return_deref->type));
1168 	 abort();
1169       }
1170    } else if (callee->return_type != &glsl_type_builtin_void) {
1171       printf("ir_call has non-void callee but no return storage\n");
1172       abort();
1173    }
1174 
1175    const exec_node *formal_param_node = callee->parameters.get_head_raw();
1176    const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
1177    while (true) {
1178       if (formal_param_node->is_tail_sentinel()
1179           != actual_param_node->is_tail_sentinel()) {
1180          printf("ir_call has the wrong number of parameters:\n");
1181          goto dump_ir;
1182       }
1183       if (formal_param_node->is_tail_sentinel()) {
1184          break;
1185       }
1186       const ir_variable *formal_param
1187          = (const ir_variable *) formal_param_node;
1188       const ir_rvalue *actual_param
1189          = (const ir_rvalue *) actual_param_node;
1190       if (formal_param->type != actual_param->type) {
1191          printf("ir_call parameter type mismatch:\n");
1192          goto dump_ir;
1193       }
1194       if (formal_param->data.mode == ir_var_function_out
1195           || formal_param->data.mode == ir_var_function_inout) {
1196          if (!actual_param->is_lvalue()) {
1197             printf("ir_call out/inout parameters must be lvalues:\n");
1198             goto dump_ir;
1199          }
1200       }
1201       formal_param_node = formal_param_node->next;
1202       actual_param_node = actual_param_node->next;
1203    }
1204 
1205    return visit_continue;
1206 
1207 dump_ir:
1208    ir->print();
1209    printf("callee:\n");
1210    callee->print();
1211    abort();
1212    return visit_stop;
1213 }
1214 
1215 void
validate_ir(ir_instruction * ir,void * data)1216 ir_validate::validate_ir(ir_instruction *ir, void *data)
1217 {
1218    struct set *ir_set = (struct set *) data;
1219 
1220    if (_mesa_set_search(ir_set, ir)) {
1221       printf("Instruction node present twice in ir tree:\n");
1222       ir->print();
1223       printf("\n");
1224       abort();
1225    }
1226    _mesa_set_add(ir_set, ir);
1227 }
1228 
1229 static void
check_node_type(ir_instruction * ir,void * data)1230 check_node_type(ir_instruction *ir, void *data)
1231 {
1232    (void) data;
1233 
1234    if (ir->ir_type >= ir_type_max) {
1235       printf("Instruction node with unset type\n");
1236       ir->print(); printf("\n");
1237    }
1238    ir_rvalue *value = ir->as_rvalue();
1239    if (value != NULL)
1240       assert(value->type != &glsl_type_builtin_error);
1241 }
1242 
1243 void
validate_ir_tree(exec_list * instructions)1244 validate_ir_tree(exec_list *instructions)
1245 {
1246    /* We shouldn't have any reason to validate IR in a release build,
1247     * and it's half composed of assert()s anyway which wouldn't do
1248     * anything.
1249     */
1250 #ifndef DEBUG
1251    if (!debug_get_bool_option("GLSL_VALIDATE", false))
1252       return;
1253 #endif
1254    ir_validate v;
1255 
1256    v.run(instructions);
1257 
1258    foreach_in_list(ir_instruction, ir, instructions) {
1259       visit_tree(ir, check_node_type, NULL);
1260    }
1261 }
1262