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