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