8e73229572dd1cc152ab321662f65f0c798bd135
[mesa.git] / src / glsl / ir_validate.cpp
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 "program/hash_table.h"
39 #include "glsl_types.h"
40
41 namespace {
42
43 class ir_validate : public ir_hierarchical_visitor {
44 public:
45 ir_validate()
46 {
47 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
48 hash_table_pointer_compare);
49
50 this->current_function = NULL;
51
52 this->callback = ir_validate::validate_ir;
53 this->data = ht;
54 }
55
56 ~ir_validate()
57 {
58 hash_table_dtor(this->ht);
59 }
60
61 virtual ir_visitor_status visit(ir_variable *v);
62 virtual ir_visitor_status visit(ir_dereference_variable *ir);
63
64 virtual ir_visitor_status visit_enter(ir_if *ir);
65
66 virtual ir_visitor_status visit_enter(ir_function *ir);
67 virtual ir_visitor_status visit_leave(ir_function *ir);
68 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
69
70 virtual ir_visitor_status visit_leave(ir_expression *ir);
71 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
72
73 virtual ir_visitor_status visit_enter(class ir_dereference_array *);
74
75 virtual ir_visitor_status visit_enter(ir_assignment *ir);
76 virtual ir_visitor_status visit_enter(ir_call *ir);
77
78 static void validate_ir(ir_instruction *ir, void *data);
79
80 ir_function *current_function;
81
82 struct hash_table *ht;
83 };
84
85 } /* anonymous namespace */
86
87 ir_visitor_status
88 ir_validate::visit(ir_dereference_variable *ir)
89 {
90 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
91 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
92 (void *) ir, (void *) ir->var);
93 abort();
94 }
95
96 if (hash_table_find(ht, ir->var) == NULL) {
97 printf("ir_dereference_variable @ %p specifies undeclared variable "
98 "`%s' @ %p\n",
99 (void *) ir, ir->var->name, (void *) ir->var);
100 abort();
101 }
102
103 this->validate_ir(ir, this->data);
104
105 return visit_continue;
106 }
107
108 ir_visitor_status
109 ir_validate::visit_enter(class ir_dereference_array *ir)
110 {
111 if (!ir->array->type->is_array() && !ir->array->type->is_matrix()) {
112 printf("ir_dereference_array @ %p does not specify an array or a "
113 "matrix\n",
114 (void *) ir);
115 ir->print();
116 printf("\n");
117 abort();
118 }
119
120 if (!ir->array_index->type->is_scalar()) {
121 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
122 (void *) ir, ir->array_index->type->name);
123 abort();
124 }
125
126 if (!ir->array_index->type->is_integer()) {
127 printf("ir_dereference_array @ %p does not have integer index: %s\n",
128 (void *) ir, ir->array_index->type->name);
129 abort();
130 }
131
132 return visit_continue;
133 }
134
135 ir_visitor_status
136 ir_validate::visit_enter(ir_if *ir)
137 {
138 if (ir->condition->type != glsl_type::bool_type) {
139 printf("ir_if condition %s type instead of bool.\n",
140 ir->condition->type->name);
141 ir->print();
142 printf("\n");
143 abort();
144 }
145
146 return visit_continue;
147 }
148
149
150 ir_visitor_status
151 ir_validate::visit_enter(ir_function *ir)
152 {
153 /* Function definitions cannot be nested.
154 */
155 if (this->current_function != NULL) {
156 printf("Function definition nested inside another function "
157 "definition:\n");
158 printf("%s %p inside %s %p\n",
159 ir->name, (void *) ir,
160 this->current_function->name, (void *) this->current_function);
161 abort();
162 }
163
164 /* Store the current function hierarchy being traversed. This is used
165 * by the function signature visitor to ensure that the signatures are
166 * linked with the correct functions.
167 */
168 this->current_function = ir;
169
170 this->validate_ir(ir, this->data);
171
172 /* Verify that all of the things stored in the list of signatures are,
173 * in fact, function signatures.
174 */
175 foreach_list(node, &ir->signatures) {
176 ir_instruction *sig = (ir_instruction *) node;
177
178 if (sig->ir_type != ir_type_function_signature) {
179 printf("Non-signature in signature list of function `%s'\n",
180 ir->name);
181 abort();
182 }
183 }
184
185 return visit_continue;
186 }
187
188 ir_visitor_status
189 ir_validate::visit_leave(ir_function *ir)
190 {
191 assert(ralloc_parent(ir->name) == ir);
192
193 this->current_function = NULL;
194 return visit_continue;
195 }
196
197 ir_visitor_status
198 ir_validate::visit_enter(ir_function_signature *ir)
199 {
200 if (this->current_function != ir->function()) {
201 printf("Function signature nested inside wrong function "
202 "definition:\n");
203 printf("%p inside %s %p instead of %s %p\n",
204 (void *) ir,
205 this->current_function->name, (void *) this->current_function,
206 ir->function_name(), (void *) ir->function());
207 abort();
208 }
209
210 if (ir->return_type == NULL) {
211 printf("Function signature %p for function %s has NULL return type.\n",
212 (void *) ir, ir->function_name());
213 abort();
214 }
215
216 this->validate_ir(ir, this->data);
217
218 return visit_continue;
219 }
220
221 ir_visitor_status
222 ir_validate::visit_leave(ir_expression *ir)
223 {
224 switch (ir->operation) {
225 case ir_unop_bit_not:
226 assert(ir->operands[0]->type == ir->type);
227 break;
228 case ir_unop_logic_not:
229 assert(ir->type->base_type == GLSL_TYPE_BOOL);
230 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
231 break;
232
233 case ir_unop_neg:
234 case ir_unop_abs:
235 case ir_unop_sign:
236 case ir_unop_rcp:
237 case ir_unop_rsq:
238 case ir_unop_sqrt:
239 assert(ir->type == ir->operands[0]->type);
240 break;
241
242 case ir_unop_exp:
243 case ir_unop_log:
244 case ir_unop_exp2:
245 case ir_unop_log2:
246 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
247 assert(ir->type == ir->operands[0]->type);
248 break;
249
250 case ir_unop_f2i:
251 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
252 assert(ir->type->base_type == GLSL_TYPE_INT);
253 break;
254 case ir_unop_f2u:
255 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
256 assert(ir->type->base_type == GLSL_TYPE_UINT);
257 break;
258 case ir_unop_i2f:
259 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
260 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
261 break;
262 case ir_unop_f2b:
263 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
264 assert(ir->type->base_type == GLSL_TYPE_BOOL);
265 break;
266 case ir_unop_b2f:
267 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
268 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
269 break;
270 case ir_unop_i2b:
271 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
272 assert(ir->type->base_type == GLSL_TYPE_BOOL);
273 break;
274 case ir_unop_b2i:
275 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
276 assert(ir->type->base_type == GLSL_TYPE_INT);
277 break;
278 case ir_unop_u2f:
279 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
280 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
281 break;
282 case ir_unop_i2u:
283 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
284 assert(ir->type->base_type == GLSL_TYPE_UINT);
285 break;
286 case ir_unop_u2i:
287 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
288 assert(ir->type->base_type == GLSL_TYPE_INT);
289 break;
290 case ir_unop_bitcast_i2f:
291 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
292 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
293 break;
294 case ir_unop_bitcast_f2i:
295 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
296 assert(ir->type->base_type == GLSL_TYPE_INT);
297 break;
298 case ir_unop_bitcast_u2f:
299 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
300 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
301 break;
302 case ir_unop_bitcast_f2u:
303 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
304 assert(ir->type->base_type == GLSL_TYPE_UINT);
305 break;
306
307 case ir_unop_any:
308 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
309 assert(ir->type == glsl_type::bool_type);
310 break;
311
312 case ir_unop_trunc:
313 case ir_unop_round_even:
314 case ir_unop_ceil:
315 case ir_unop_floor:
316 case ir_unop_fract:
317 case ir_unop_sin:
318 case ir_unop_cos:
319 case ir_unop_sin_reduced:
320 case ir_unop_cos_reduced:
321 case ir_unop_dFdx:
322 case ir_unop_dFdy:
323 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
324 assert(ir->operands[0]->type == ir->type);
325 break;
326
327 case ir_unop_pack_snorm_2x16:
328 case ir_unop_pack_unorm_2x16:
329 case ir_unop_pack_half_2x16:
330 assert(ir->type == glsl_type::uint_type);
331 assert(ir->operands[0]->type == glsl_type::vec2_type);
332 break;
333
334 case ir_unop_pack_snorm_4x8:
335 case ir_unop_pack_unorm_4x8:
336 assert(ir->type == glsl_type::uint_type);
337 assert(ir->operands[0]->type == glsl_type::vec4_type);
338 break;
339
340 case ir_unop_unpack_snorm_2x16:
341 case ir_unop_unpack_unorm_2x16:
342 case ir_unop_unpack_half_2x16:
343 assert(ir->type == glsl_type::vec2_type);
344 assert(ir->operands[0]->type == glsl_type::uint_type);
345 break;
346
347 case ir_unop_unpack_snorm_4x8:
348 case ir_unop_unpack_unorm_4x8:
349 assert(ir->type == glsl_type::vec4_type);
350 assert(ir->operands[0]->type == glsl_type::uint_type);
351 break;
352
353 case ir_unop_unpack_half_2x16_split_x:
354 case ir_unop_unpack_half_2x16_split_y:
355 assert(ir->type == glsl_type::float_type);
356 assert(ir->operands[0]->type == glsl_type::uint_type);
357 break;
358
359 case ir_unop_bitfield_reverse:
360 assert(ir->operands[0]->type == ir->type);
361 assert(ir->type->is_integer());
362 break;
363
364 case ir_unop_bit_count:
365 case ir_unop_find_msb:
366 case ir_unop_find_lsb:
367 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
368 assert(ir->operands[0]->type->is_integer());
369 assert(ir->type->base_type == GLSL_TYPE_INT);
370 break;
371
372 case ir_unop_noise:
373 /* XXX what can we assert here? */
374 break;
375
376 case ir_binop_add:
377 case ir_binop_sub:
378 case ir_binop_mul:
379 case ir_binop_div:
380 case ir_binop_mod:
381 case ir_binop_min:
382 case ir_binop_max:
383 case ir_binop_pow:
384 if (ir->operands[0]->type->is_scalar())
385 assert(ir->operands[1]->type == ir->type);
386 else if (ir->operands[1]->type->is_scalar())
387 assert(ir->operands[0]->type == ir->type);
388 else if (ir->operands[0]->type->is_vector() &&
389 ir->operands[1]->type->is_vector()) {
390 assert(ir->operands[0]->type == ir->operands[1]->type);
391 assert(ir->operands[0]->type == ir->type);
392 }
393 break;
394
395 case ir_binop_imul_high:
396 assert(ir->type == ir->operands[0]->type);
397 assert(ir->type == ir->operands[1]->type);
398 assert(ir->type->is_integer());
399 break;
400
401 case ir_binop_carry:
402 case ir_binop_borrow:
403 assert(ir->type == ir->operands[0]->type);
404 assert(ir->type == ir->operands[1]->type);
405 assert(ir->type->base_type == GLSL_TYPE_UINT);
406 break;
407
408 case ir_binop_less:
409 case ir_binop_greater:
410 case ir_binop_lequal:
411 case ir_binop_gequal:
412 case ir_binop_equal:
413 case ir_binop_nequal:
414 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
415 * ==, and != operators. The IR operators perform a component-wise
416 * comparison on scalar or vector types and return a boolean scalar or
417 * vector type of the same size.
418 */
419 assert(ir->type->base_type == GLSL_TYPE_BOOL);
420 assert(ir->operands[0]->type == ir->operands[1]->type);
421 assert(ir->operands[0]->type->is_vector()
422 || ir->operands[0]->type->is_scalar());
423 assert(ir->operands[0]->type->vector_elements
424 == ir->type->vector_elements);
425 break;
426
427 case ir_binop_all_equal:
428 case ir_binop_any_nequal:
429 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
430 * return a scalar boolean. The IR matches that.
431 */
432 assert(ir->type == glsl_type::bool_type);
433 assert(ir->operands[0]->type == ir->operands[1]->type);
434 break;
435
436 case ir_binop_lshift:
437 case ir_binop_rshift:
438 assert(ir->operands[0]->type->is_integer() &&
439 ir->operands[1]->type->is_integer());
440 if (ir->operands[0]->type->is_scalar()) {
441 assert(ir->operands[1]->type->is_scalar());
442 }
443 if (ir->operands[0]->type->is_vector() &&
444 ir->operands[1]->type->is_vector()) {
445 assert(ir->operands[0]->type->components() ==
446 ir->operands[1]->type->components());
447 }
448 assert(ir->type == ir->operands[0]->type);
449 break;
450
451 case ir_binop_bit_and:
452 case ir_binop_bit_xor:
453 case ir_binop_bit_or:
454 assert(ir->operands[0]->type->base_type ==
455 ir->operands[1]->type->base_type);
456 assert(ir->type->is_integer());
457 if (ir->operands[0]->type->is_vector() &&
458 ir->operands[1]->type->is_vector()) {
459 assert(ir->operands[0]->type->vector_elements ==
460 ir->operands[1]->type->vector_elements);
461 }
462 break;
463
464 case ir_binop_logic_and:
465 case ir_binop_logic_xor:
466 case ir_binop_logic_or:
467 assert(ir->type == glsl_type::bool_type);
468 assert(ir->operands[0]->type == glsl_type::bool_type);
469 assert(ir->operands[1]->type == glsl_type::bool_type);
470 break;
471
472 case ir_binop_dot:
473 assert(ir->type == glsl_type::float_type);
474 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
475 assert(ir->operands[0]->type->is_vector());
476 assert(ir->operands[0]->type == ir->operands[1]->type);
477 break;
478
479 case ir_binop_pack_half_2x16_split:
480 assert(ir->type == glsl_type::uint_type);
481 assert(ir->operands[0]->type == glsl_type::float_type);
482 assert(ir->operands[1]->type == glsl_type::float_type);
483 break;
484
485 case ir_binop_bfm:
486 assert(ir->type->is_integer());
487 assert(ir->operands[0]->type->is_integer());
488 assert(ir->operands[1]->type->is_integer());
489 break;
490
491 case ir_binop_ubo_load:
492 assert(ir->operands[0]->as_constant());
493 assert(ir->operands[0]->type == glsl_type::uint_type);
494
495 assert(ir->operands[1]->type == glsl_type::uint_type);
496 break;
497
498 case ir_binop_ldexp:
499 assert(ir->operands[0]->type == ir->type);
500 assert(ir->operands[0]->type->is_float());
501 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
502 assert(ir->operands[0]->type->components() ==
503 ir->operands[1]->type->components());
504 break;
505
506 case ir_binop_vector_extract:
507 assert(ir->operands[0]->type->is_vector());
508 assert(ir->operands[1]->type->is_scalar()
509 && ir->operands[1]->type->is_integer());
510 break;
511
512 case ir_triop_fma:
513 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
514 assert(ir->type == ir->operands[0]->type);
515 assert(ir->type == ir->operands[1]->type);
516 assert(ir->type == ir->operands[2]->type);
517 break;
518
519 case ir_triop_lrp:
520 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
521 assert(ir->operands[0]->type == ir->operands[1]->type);
522 assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
523 break;
524
525 case ir_triop_csel:
526 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
527 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
528 assert(ir->type == ir->operands[1]->type);
529 assert(ir->type == ir->operands[2]->type);
530 break;
531
532 case ir_triop_bfi:
533 assert(ir->operands[0]->type->is_integer());
534 assert(ir->operands[1]->type == ir->operands[2]->type);
535 assert(ir->operands[1]->type == ir->type);
536 break;
537
538 case ir_triop_bitfield_extract:
539 assert(ir->operands[0]->type == ir->type);
540 assert(ir->operands[1]->type == glsl_type::int_type);
541 assert(ir->operands[2]->type == glsl_type::int_type);
542 break;
543
544 case ir_triop_vector_insert:
545 assert(ir->operands[0]->type->is_vector());
546 assert(ir->operands[1]->type->is_scalar());
547 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
548 assert(ir->operands[2]->type->is_scalar()
549 && ir->operands[2]->type->is_integer());
550 assert(ir->type == ir->operands[0]->type);
551 break;
552
553 case ir_quadop_bitfield_insert:
554 assert(ir->operands[0]->type == ir->type);
555 assert(ir->operands[1]->type == ir->type);
556 assert(ir->operands[2]->type == glsl_type::int_type);
557 assert(ir->operands[3]->type == glsl_type::int_type);
558 break;
559
560 case ir_quadop_vector:
561 /* The vector operator collects some number of scalars and generates a
562 * vector from them.
563 *
564 * - All of the operands must be scalar.
565 * - Number of operands must matche the size of the resulting vector.
566 * - Base type of the operands must match the base type of the result.
567 */
568 assert(ir->type->is_vector());
569 switch (ir->type->vector_elements) {
570 case 2:
571 assert(ir->operands[0]->type->is_scalar());
572 assert(ir->operands[0]->type->base_type == ir->type->base_type);
573 assert(ir->operands[1]->type->is_scalar());
574 assert(ir->operands[1]->type->base_type == ir->type->base_type);
575 assert(ir->operands[2] == NULL);
576 assert(ir->operands[3] == NULL);
577 break;
578 case 3:
579 assert(ir->operands[0]->type->is_scalar());
580 assert(ir->operands[0]->type->base_type == ir->type->base_type);
581 assert(ir->operands[1]->type->is_scalar());
582 assert(ir->operands[1]->type->base_type == ir->type->base_type);
583 assert(ir->operands[2]->type->is_scalar());
584 assert(ir->operands[2]->type->base_type == ir->type->base_type);
585 assert(ir->operands[3] == NULL);
586 break;
587 case 4:
588 assert(ir->operands[0]->type->is_scalar());
589 assert(ir->operands[0]->type->base_type == ir->type->base_type);
590 assert(ir->operands[1]->type->is_scalar());
591 assert(ir->operands[1]->type->base_type == ir->type->base_type);
592 assert(ir->operands[2]->type->is_scalar());
593 assert(ir->operands[2]->type->base_type == ir->type->base_type);
594 assert(ir->operands[3]->type->is_scalar());
595 assert(ir->operands[3]->type->base_type == ir->type->base_type);
596 break;
597 default:
598 /* The is_vector assertion above should prevent execution from ever
599 * getting here.
600 */
601 assert(!"Should not get here.");
602 break;
603 }
604 }
605
606 return visit_continue;
607 }
608
609 ir_visitor_status
610 ir_validate::visit_leave(ir_swizzle *ir)
611 {
612 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
613
614 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
615 if (chans[i] >= ir->val->type->vector_elements) {
616 printf("ir_swizzle @ %p specifies a channel not present "
617 "in the value.\n", (void *) ir);
618 ir->print();
619 abort();
620 }
621 }
622
623 return visit_continue;
624 }
625
626 ir_visitor_status
627 ir_validate::visit(ir_variable *ir)
628 {
629 /* An ir_variable is the one thing that can (and will) appear multiple times
630 * in an IR tree. It is added to the hashtable so that it can be used
631 * in the ir_dereference_variable handler to ensure that a variable is
632 * declared before it is dereferenced.
633 */
634 if (ir->name)
635 assert(ralloc_parent(ir->name) == ir);
636
637 hash_table_insert(ht, ir, ir);
638
639
640 /* If a variable is an array, verify that the maximum array index is in
641 * bounds. There was once an error in AST-to-HIR conversion that set this
642 * to be out of bounds.
643 */
644 if (ir->type->array_size() > 0) {
645 if (ir->max_array_access >= ir->type->length) {
646 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
647 ir->max_array_access, ir->type->length - 1);
648 ir->print();
649 abort();
650 }
651 }
652
653 /* If a variable is an interface block (or an array of interface blocks),
654 * verify that the maximum array index for each interface member is in
655 * bounds.
656 */
657 if (ir->is_interface_instance()) {
658 const glsl_struct_field *fields =
659 ir->get_interface_type()->fields.structure;
660 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
661 if (fields[i].type->array_size() > 0) {
662 if (ir->max_ifc_array_access[i] >= fields[i].type->length) {
663 printf("ir_variable has maximum access out of bounds for "
664 "field %s (%d vs %d)\n", fields[i].name,
665 ir->max_ifc_array_access[i], fields[i].type->length);
666 ir->print();
667 abort();
668 }
669 }
670 }
671 }
672
673 if (ir->constant_initializer != NULL && !ir->has_initializer) {
674 printf("ir_variable didn't have an initializer, but has a constant "
675 "initializer value.\n");
676 ir->print();
677 abort();
678 }
679
680 return visit_continue;
681 }
682
683 ir_visitor_status
684 ir_validate::visit_enter(ir_assignment *ir)
685 {
686 const ir_dereference *const lhs = ir->lhs;
687 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
688 if (ir->write_mask == 0) {
689 printf("Assignment LHS is %s, but write mask is 0:\n",
690 lhs->type->is_scalar() ? "scalar" : "vector");
691 ir->print();
692 abort();
693 }
694
695 int lhs_components = 0;
696 for (int i = 0; i < 4; i++) {
697 if (ir->write_mask & (1 << i))
698 lhs_components++;
699 }
700
701 if (lhs_components != ir->rhs->type->vector_elements) {
702 printf("Assignment count of LHS write mask channels enabled not\n"
703 "matching RHS vector size (%d LHS, %d RHS).\n",
704 lhs_components, ir->rhs->type->vector_elements);
705 ir->print();
706 abort();
707 }
708 }
709
710 this->validate_ir(ir, this->data);
711
712 return visit_continue;
713 }
714
715 ir_visitor_status
716 ir_validate::visit_enter(ir_call *ir)
717 {
718 ir_function_signature *const callee = ir->callee;
719
720 if (callee->ir_type != ir_type_function_signature) {
721 printf("IR called by ir_call is not ir_function_signature!\n");
722 abort();
723 }
724
725 if (ir->return_deref) {
726 if (ir->return_deref->type != callee->return_type) {
727 printf("callee type %s does not match return storage type %s\n",
728 callee->return_type->name, ir->return_deref->type->name);
729 abort();
730 }
731 } else if (callee->return_type != glsl_type::void_type) {
732 printf("ir_call has non-void callee but no return storage\n");
733 abort();
734 }
735
736 const exec_node *formal_param_node = callee->parameters.head;
737 const exec_node *actual_param_node = ir->actual_parameters.head;
738 while (true) {
739 if (formal_param_node->is_tail_sentinel()
740 != actual_param_node->is_tail_sentinel()) {
741 printf("ir_call has the wrong number of parameters:\n");
742 goto dump_ir;
743 }
744 if (formal_param_node->is_tail_sentinel()) {
745 break;
746 }
747 const ir_variable *formal_param
748 = (const ir_variable *) formal_param_node;
749 const ir_rvalue *actual_param
750 = (const ir_rvalue *) actual_param_node;
751 if (formal_param->type != actual_param->type) {
752 printf("ir_call parameter type mismatch:\n");
753 goto dump_ir;
754 }
755 if (formal_param->mode == ir_var_function_out
756 || formal_param->mode == ir_var_function_inout) {
757 if (!actual_param->is_lvalue()) {
758 printf("ir_call out/inout parameters must be lvalues:\n");
759 goto dump_ir;
760 }
761 }
762 formal_param_node = formal_param_node->next;
763 actual_param_node = actual_param_node->next;
764 }
765
766 return visit_continue;
767
768 dump_ir:
769 ir->print();
770 printf("callee:\n");
771 callee->print();
772 abort();
773 return visit_stop;
774 }
775
776 void
777 ir_validate::validate_ir(ir_instruction *ir, void *data)
778 {
779 struct hash_table *ht = (struct hash_table *) data;
780
781 if (hash_table_find(ht, ir)) {
782 printf("Instruction node present twice in ir tree:\n");
783 ir->print();
784 printf("\n");
785 abort();
786 }
787 hash_table_insert(ht, ir, ir);
788 }
789
790 void
791 check_node_type(ir_instruction *ir, void *data)
792 {
793 (void) data;
794
795 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
796 printf("Instruction node with unset type\n");
797 ir->print(); printf("\n");
798 }
799 ir_rvalue *value = ir->as_rvalue();
800 if (value != NULL)
801 assert(value->type != glsl_type::error_type);
802 }
803
804 void
805 validate_ir_tree(exec_list *instructions)
806 {
807 /* We shouldn't have any reason to validate IR in a release build,
808 * and it's half composed of assert()s anyway which wouldn't do
809 * anything.
810 */
811 #ifdef DEBUG
812 ir_validate v;
813
814 v.run(instructions);
815
816 foreach_iter(exec_list_iterator, iter, *instructions) {
817 ir_instruction *ir = (ir_instruction *)iter.get();
818
819 visit_tree(ir, check_node_type, NULL);
820 }
821 #endif
822 }