glsl: Move ir_type_unset to end of enumeration.
[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 assert(ir->operands[0]->type->base_type ==
385 ir->operands[1]->type->base_type);
386
387 if (ir->operands[0]->type->is_scalar())
388 assert(ir->operands[1]->type == ir->type);
389 else if (ir->operands[1]->type->is_scalar())
390 assert(ir->operands[0]->type == ir->type);
391 else if (ir->operands[0]->type->is_vector() &&
392 ir->operands[1]->type->is_vector()) {
393 assert(ir->operands[0]->type == ir->operands[1]->type);
394 assert(ir->operands[0]->type == ir->type);
395 }
396 break;
397
398 case ir_binop_imul_high:
399 assert(ir->type == ir->operands[0]->type);
400 assert(ir->type == ir->operands[1]->type);
401 assert(ir->type->is_integer());
402 break;
403
404 case ir_binop_carry:
405 case ir_binop_borrow:
406 assert(ir->type == ir->operands[0]->type);
407 assert(ir->type == ir->operands[1]->type);
408 assert(ir->type->base_type == GLSL_TYPE_UINT);
409 break;
410
411 case ir_binop_less:
412 case ir_binop_greater:
413 case ir_binop_lequal:
414 case ir_binop_gequal:
415 case ir_binop_equal:
416 case ir_binop_nequal:
417 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
418 * ==, and != operators. The IR operators perform a component-wise
419 * comparison on scalar or vector types and return a boolean scalar or
420 * vector type of the same size.
421 */
422 assert(ir->type->base_type == GLSL_TYPE_BOOL);
423 assert(ir->operands[0]->type == ir->operands[1]->type);
424 assert(ir->operands[0]->type->is_vector()
425 || ir->operands[0]->type->is_scalar());
426 assert(ir->operands[0]->type->vector_elements
427 == ir->type->vector_elements);
428 break;
429
430 case ir_binop_all_equal:
431 case ir_binop_any_nequal:
432 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
433 * return a scalar boolean. The IR matches that.
434 */
435 assert(ir->type == glsl_type::bool_type);
436 assert(ir->operands[0]->type == ir->operands[1]->type);
437 break;
438
439 case ir_binop_lshift:
440 case ir_binop_rshift:
441 assert(ir->operands[0]->type->is_integer() &&
442 ir->operands[1]->type->is_integer());
443 if (ir->operands[0]->type->is_scalar()) {
444 assert(ir->operands[1]->type->is_scalar());
445 }
446 if (ir->operands[0]->type->is_vector() &&
447 ir->operands[1]->type->is_vector()) {
448 assert(ir->operands[0]->type->components() ==
449 ir->operands[1]->type->components());
450 }
451 assert(ir->type == ir->operands[0]->type);
452 break;
453
454 case ir_binop_bit_and:
455 case ir_binop_bit_xor:
456 case ir_binop_bit_or:
457 assert(ir->operands[0]->type->base_type ==
458 ir->operands[1]->type->base_type);
459 assert(ir->type->is_integer());
460 if (ir->operands[0]->type->is_vector() &&
461 ir->operands[1]->type->is_vector()) {
462 assert(ir->operands[0]->type->vector_elements ==
463 ir->operands[1]->type->vector_elements);
464 }
465 break;
466
467 case ir_binop_logic_and:
468 case ir_binop_logic_xor:
469 case ir_binop_logic_or:
470 assert(ir->type == glsl_type::bool_type);
471 assert(ir->operands[0]->type == glsl_type::bool_type);
472 assert(ir->operands[1]->type == glsl_type::bool_type);
473 break;
474
475 case ir_binop_dot:
476 assert(ir->type == glsl_type::float_type);
477 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
478 assert(ir->operands[0]->type->is_vector());
479 assert(ir->operands[0]->type == ir->operands[1]->type);
480 break;
481
482 case ir_binop_pack_half_2x16_split:
483 assert(ir->type == glsl_type::uint_type);
484 assert(ir->operands[0]->type == glsl_type::float_type);
485 assert(ir->operands[1]->type == glsl_type::float_type);
486 break;
487
488 case ir_binop_bfm:
489 assert(ir->type->is_integer());
490 assert(ir->operands[0]->type->is_integer());
491 assert(ir->operands[1]->type->is_integer());
492 break;
493
494 case ir_binop_ubo_load:
495 assert(ir->operands[0]->as_constant());
496 assert(ir->operands[0]->type == glsl_type::uint_type);
497
498 assert(ir->operands[1]->type == glsl_type::uint_type);
499 break;
500
501 case ir_binop_ldexp:
502 assert(ir->operands[0]->type == ir->type);
503 assert(ir->operands[0]->type->is_float());
504 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
505 assert(ir->operands[0]->type->components() ==
506 ir->operands[1]->type->components());
507 break;
508
509 case ir_binop_vector_extract:
510 assert(ir->operands[0]->type->is_vector());
511 assert(ir->operands[1]->type->is_scalar()
512 && ir->operands[1]->type->is_integer());
513 break;
514
515 case ir_triop_fma:
516 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
517 assert(ir->type == ir->operands[0]->type);
518 assert(ir->type == ir->operands[1]->type);
519 assert(ir->type == ir->operands[2]->type);
520 break;
521
522 case ir_triop_lrp:
523 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
524 assert(ir->operands[0]->type == ir->operands[1]->type);
525 assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
526 break;
527
528 case ir_triop_csel:
529 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
530 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
531 assert(ir->type == ir->operands[1]->type);
532 assert(ir->type == ir->operands[2]->type);
533 break;
534
535 case ir_triop_bfi:
536 assert(ir->operands[0]->type->is_integer());
537 assert(ir->operands[1]->type == ir->operands[2]->type);
538 assert(ir->operands[1]->type == ir->type);
539 break;
540
541 case ir_triop_bitfield_extract:
542 assert(ir->operands[0]->type == ir->type);
543 assert(ir->operands[1]->type == glsl_type::int_type);
544 assert(ir->operands[2]->type == glsl_type::int_type);
545 break;
546
547 case ir_triop_vector_insert:
548 assert(ir->operands[0]->type->is_vector());
549 assert(ir->operands[1]->type->is_scalar());
550 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
551 assert(ir->operands[2]->type->is_scalar()
552 && ir->operands[2]->type->is_integer());
553 assert(ir->type == ir->operands[0]->type);
554 break;
555
556 case ir_quadop_bitfield_insert:
557 assert(ir->operands[0]->type == ir->type);
558 assert(ir->operands[1]->type == ir->type);
559 assert(ir->operands[2]->type == glsl_type::int_type);
560 assert(ir->operands[3]->type == glsl_type::int_type);
561 break;
562
563 case ir_quadop_vector:
564 /* The vector operator collects some number of scalars and generates a
565 * vector from them.
566 *
567 * - All of the operands must be scalar.
568 * - Number of operands must matche the size of the resulting vector.
569 * - Base type of the operands must match the base type of the result.
570 */
571 assert(ir->type->is_vector());
572 switch (ir->type->vector_elements) {
573 case 2:
574 assert(ir->operands[0]->type->is_scalar());
575 assert(ir->operands[0]->type->base_type == ir->type->base_type);
576 assert(ir->operands[1]->type->is_scalar());
577 assert(ir->operands[1]->type->base_type == ir->type->base_type);
578 assert(ir->operands[2] == NULL);
579 assert(ir->operands[3] == NULL);
580 break;
581 case 3:
582 assert(ir->operands[0]->type->is_scalar());
583 assert(ir->operands[0]->type->base_type == ir->type->base_type);
584 assert(ir->operands[1]->type->is_scalar());
585 assert(ir->operands[1]->type->base_type == ir->type->base_type);
586 assert(ir->operands[2]->type->is_scalar());
587 assert(ir->operands[2]->type->base_type == ir->type->base_type);
588 assert(ir->operands[3] == NULL);
589 break;
590 case 4:
591 assert(ir->operands[0]->type->is_scalar());
592 assert(ir->operands[0]->type->base_type == ir->type->base_type);
593 assert(ir->operands[1]->type->is_scalar());
594 assert(ir->operands[1]->type->base_type == ir->type->base_type);
595 assert(ir->operands[2]->type->is_scalar());
596 assert(ir->operands[2]->type->base_type == ir->type->base_type);
597 assert(ir->operands[3]->type->is_scalar());
598 assert(ir->operands[3]->type->base_type == ir->type->base_type);
599 break;
600 default:
601 /* The is_vector assertion above should prevent execution from ever
602 * getting here.
603 */
604 assert(!"Should not get here.");
605 break;
606 }
607 }
608
609 return visit_continue;
610 }
611
612 ir_visitor_status
613 ir_validate::visit_leave(ir_swizzle *ir)
614 {
615 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
616
617 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
618 if (chans[i] >= ir->val->type->vector_elements) {
619 printf("ir_swizzle @ %p specifies a channel not present "
620 "in the value.\n", (void *) ir);
621 ir->print();
622 abort();
623 }
624 }
625
626 return visit_continue;
627 }
628
629 ir_visitor_status
630 ir_validate::visit(ir_variable *ir)
631 {
632 /* An ir_variable is the one thing that can (and will) appear multiple times
633 * in an IR tree. It is added to the hashtable so that it can be used
634 * in the ir_dereference_variable handler to ensure that a variable is
635 * declared before it is dereferenced.
636 */
637 if (ir->name)
638 assert(ralloc_parent(ir->name) == ir);
639
640 hash_table_insert(ht, ir, ir);
641
642
643 /* If a variable is an array, verify that the maximum array index is in
644 * bounds. There was once an error in AST-to-HIR conversion that set this
645 * to be out of bounds.
646 */
647 if (ir->type->array_size() > 0) {
648 if (ir->data.max_array_access >= ir->type->length) {
649 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
650 ir->data.max_array_access, ir->type->length - 1);
651 ir->print();
652 abort();
653 }
654 }
655
656 /* If a variable is an interface block (or an array of interface blocks),
657 * verify that the maximum array index for each interface member is in
658 * bounds.
659 */
660 if (ir->is_interface_instance()) {
661 const glsl_struct_field *fields =
662 ir->get_interface_type()->fields.structure;
663 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
664 if (fields[i].type->array_size() > 0) {
665 if (ir->max_ifc_array_access[i] >= fields[i].type->length) {
666 printf("ir_variable has maximum access out of bounds for "
667 "field %s (%d vs %d)\n", fields[i].name,
668 ir->max_ifc_array_access[i], fields[i].type->length);
669 ir->print();
670 abort();
671 }
672 }
673 }
674 }
675
676 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
677 printf("ir_variable didn't have an initializer, but has a constant "
678 "initializer value.\n");
679 ir->print();
680 abort();
681 }
682
683 return visit_continue;
684 }
685
686 ir_visitor_status
687 ir_validate::visit_enter(ir_assignment *ir)
688 {
689 const ir_dereference *const lhs = ir->lhs;
690 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
691 if (ir->write_mask == 0) {
692 printf("Assignment LHS is %s, but write mask is 0:\n",
693 lhs->type->is_scalar() ? "scalar" : "vector");
694 ir->print();
695 abort();
696 }
697
698 int lhs_components = 0;
699 for (int i = 0; i < 4; i++) {
700 if (ir->write_mask & (1 << i))
701 lhs_components++;
702 }
703
704 if (lhs_components != ir->rhs->type->vector_elements) {
705 printf("Assignment count of LHS write mask channels enabled not\n"
706 "matching RHS vector size (%d LHS, %d RHS).\n",
707 lhs_components, ir->rhs->type->vector_elements);
708 ir->print();
709 abort();
710 }
711 }
712
713 this->validate_ir(ir, this->data);
714
715 return visit_continue;
716 }
717
718 ir_visitor_status
719 ir_validate::visit_enter(ir_call *ir)
720 {
721 ir_function_signature *const callee = ir->callee;
722
723 if (callee->ir_type != ir_type_function_signature) {
724 printf("IR called by ir_call is not ir_function_signature!\n");
725 abort();
726 }
727
728 if (ir->return_deref) {
729 if (ir->return_deref->type != callee->return_type) {
730 printf("callee type %s does not match return storage type %s\n",
731 callee->return_type->name, ir->return_deref->type->name);
732 abort();
733 }
734 } else if (callee->return_type != glsl_type::void_type) {
735 printf("ir_call has non-void callee but no return storage\n");
736 abort();
737 }
738
739 const exec_node *formal_param_node = callee->parameters.head;
740 const exec_node *actual_param_node = ir->actual_parameters.head;
741 while (true) {
742 if (formal_param_node->is_tail_sentinel()
743 != actual_param_node->is_tail_sentinel()) {
744 printf("ir_call has the wrong number of parameters:\n");
745 goto dump_ir;
746 }
747 if (formal_param_node->is_tail_sentinel()) {
748 break;
749 }
750 const ir_variable *formal_param
751 = (const ir_variable *) formal_param_node;
752 const ir_rvalue *actual_param
753 = (const ir_rvalue *) actual_param_node;
754 if (formal_param->type != actual_param->type) {
755 printf("ir_call parameter type mismatch:\n");
756 goto dump_ir;
757 }
758 if (formal_param->data.mode == ir_var_function_out
759 || formal_param->data.mode == ir_var_function_inout) {
760 if (!actual_param->is_lvalue()) {
761 printf("ir_call out/inout parameters must be lvalues:\n");
762 goto dump_ir;
763 }
764 }
765 formal_param_node = formal_param_node->next;
766 actual_param_node = actual_param_node->next;
767 }
768
769 return visit_continue;
770
771 dump_ir:
772 ir->print();
773 printf("callee:\n");
774 callee->print();
775 abort();
776 return visit_stop;
777 }
778
779 void
780 ir_validate::validate_ir(ir_instruction *ir, void *data)
781 {
782 struct hash_table *ht = (struct hash_table *) data;
783
784 if (hash_table_find(ht, ir)) {
785 printf("Instruction node present twice in ir tree:\n");
786 ir->print();
787 printf("\n");
788 abort();
789 }
790 hash_table_insert(ht, ir, ir);
791 }
792
793 void
794 check_node_type(ir_instruction *ir, void *data)
795 {
796 (void) data;
797
798 if (ir->ir_type >= ir_type_max) {
799 printf("Instruction node with unset type\n");
800 ir->print(); printf("\n");
801 }
802 ir_rvalue *value = ir->as_rvalue();
803 if (value != NULL)
804 assert(value->type != glsl_type::error_type);
805 }
806
807 void
808 validate_ir_tree(exec_list *instructions)
809 {
810 /* We shouldn't have any reason to validate IR in a release build,
811 * and it's half composed of assert()s anyway which wouldn't do
812 * anything.
813 */
814 #ifdef DEBUG
815 ir_validate v;
816
817 v.run(instructions);
818
819 foreach_list(n, instructions) {
820 ir_instruction *ir = (ir_instruction *) n;
821
822 visit_tree(ir, check_node_type, NULL);
823 }
824 #endif
825 }