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