2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file ir_validate.cpp
27 * Attempts to verify that various invariants of the IR tree are true.
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.
37 #include "ir_hierarchical_visitor.h"
38 #include "program/hash_table.h"
39 #include "glsl_types.h"
41 class ir_validate : public ir_hierarchical_visitor {
45 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
46 hash_table_pointer_compare);
48 this->current_function = NULL;
50 this->callback = ir_validate::validate_ir;
56 hash_table_dtor(this->ht);
59 virtual ir_visitor_status visit(ir_variable *v);
60 virtual ir_visitor_status visit(ir_dereference_variable *ir);
62 virtual ir_visitor_status visit_enter(ir_if *ir);
64 virtual ir_visitor_status visit_leave(ir_loop *ir);
65 virtual ir_visitor_status visit_enter(ir_function *ir);
66 virtual ir_visitor_status visit_leave(ir_function *ir);
67 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
69 virtual ir_visitor_status visit_leave(ir_expression *ir);
70 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
72 virtual ir_visitor_status visit_enter(class ir_dereference_array *);
74 virtual ir_visitor_status visit_enter(ir_assignment *ir);
75 virtual ir_visitor_status visit_enter(ir_call *ir);
77 static void validate_ir(ir_instruction *ir, void *data);
79 ir_function *current_function;
81 struct hash_table *ht;
86 ir_validate::visit(ir_dereference_variable *ir)
88 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
89 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
90 (void *) ir, (void *) ir->var);
94 if (hash_table_find(ht, ir->var) == NULL) {
95 printf("ir_dereference_variable @ %p specifies undeclared variable "
97 (void *) ir, ir->var->name, (void *) ir->var);
101 this->validate_ir(ir, this->data);
103 return visit_continue;
107 ir_validate::visit_enter(class ir_dereference_array *ir)
109 if (!ir->array->type->is_array() && !ir->array->type->is_matrix()) {
110 printf("ir_dereference_array @ %p does not specify an array or a "
118 if (!ir->array_index->type->is_scalar()) {
119 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
120 (void *) ir, ir->array_index->type->name);
124 if (!ir->array_index->type->is_integer()) {
125 printf("ir_dereference_array @ %p does not have integer index: %s\n",
126 (void *) ir, ir->array_index->type->name);
130 return visit_continue;
134 ir_validate::visit_enter(ir_if *ir)
136 if (ir->condition->type != glsl_type::bool_type) {
137 printf("ir_if condition %s type instead of bool.\n",
138 ir->condition->type->name);
144 return visit_continue;
149 ir_validate::visit_leave(ir_loop *ir)
151 if (ir->counter != NULL) {
152 if ((ir->from == NULL) || (ir->to == NULL) || (ir->increment == NULL)) {
153 printf("ir_loop has invalid loop controls:\n"
158 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
159 (void *) ir->increment);
163 if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
164 printf("ir_loop has invalid comparitor %d\n", ir->cmp);
168 if ((ir->from != NULL) || (ir->to != NULL) || (ir->increment != NULL)) {
169 printf("ir_loop has invalid loop controls:\n"
174 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
175 (void *) ir->increment);
180 return visit_continue;
185 ir_validate::visit_enter(ir_function *ir)
187 /* Function definitions cannot be nested.
189 if (this->current_function != NULL) {
190 printf("Function definition nested inside another function "
192 printf("%s %p inside %s %p\n",
193 ir->name, (void *) ir,
194 this->current_function->name, (void *) this->current_function);
198 /* Store the current function hierarchy being traversed. This is used
199 * by the function signature visitor to ensure that the signatures are
200 * linked with the correct functions.
202 this->current_function = ir;
204 this->validate_ir(ir, this->data);
206 /* Verify that all of the things stored in the list of signatures are,
207 * in fact, function signatures.
209 foreach_list(node, &ir->signatures) {
210 ir_instruction *sig = (ir_instruction *) node;
212 if (sig->ir_type != ir_type_function_signature) {
213 printf("Non-signature in signature list of function `%s'\n",
219 return visit_continue;
223 ir_validate::visit_leave(ir_function *ir)
225 assert(ralloc_parent(ir->name) == ir);
227 this->current_function = NULL;
228 return visit_continue;
232 ir_validate::visit_enter(ir_function_signature *ir)
234 if (this->current_function != ir->function()) {
235 printf("Function signature nested inside wrong function "
237 printf("%p inside %s %p instead of %s %p\n",
239 this->current_function->name, (void *) this->current_function,
240 ir->function_name(), (void *) ir->function());
244 if (ir->return_type == NULL) {
245 printf("Function signature %p for function %s has NULL return type.\n",
246 (void *) ir, ir->function_name());
250 this->validate_ir(ir, this->data);
252 return visit_continue;
256 ir_validate::visit_leave(ir_expression *ir)
258 switch (ir->operation) {
259 case ir_unop_bit_not:
260 assert(ir->operands[0]->type == ir->type);
262 case ir_unop_logic_not:
263 assert(ir->type->base_type == GLSL_TYPE_BOOL);
264 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
273 assert(ir->type == ir->operands[0]->type);
280 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
281 assert(ir->type == ir->operands[0]->type);
285 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
286 assert(ir->type->base_type == GLSL_TYPE_INT);
289 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
290 assert(ir->type->base_type == GLSL_TYPE_UINT);
293 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
294 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
297 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
298 assert(ir->type->base_type == GLSL_TYPE_BOOL);
301 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
302 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
305 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
306 assert(ir->type->base_type == GLSL_TYPE_BOOL);
309 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
310 assert(ir->type->base_type == GLSL_TYPE_INT);
313 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
314 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
317 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
318 assert(ir->type->base_type == GLSL_TYPE_UINT);
321 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
322 assert(ir->type->base_type == GLSL_TYPE_INT);
324 case ir_unop_bitcast_i2f:
325 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
326 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
328 case ir_unop_bitcast_f2i:
329 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
330 assert(ir->type->base_type == GLSL_TYPE_INT);
332 case ir_unop_bitcast_u2f:
333 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
334 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
336 case ir_unop_bitcast_f2u:
337 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
338 assert(ir->type->base_type == GLSL_TYPE_UINT);
342 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
343 assert(ir->type == glsl_type::bool_type);
347 case ir_unop_round_even:
353 case ir_unop_sin_reduced:
354 case ir_unop_cos_reduced:
357 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
358 assert(ir->operands[0]->type == ir->type);
361 case ir_unop_pack_snorm_2x16:
362 case ir_unop_pack_unorm_2x16:
363 case ir_unop_pack_half_2x16:
364 assert(ir->type == glsl_type::uint_type);
365 assert(ir->operands[0]->type == glsl_type::vec2_type);
368 case ir_unop_pack_snorm_4x8:
369 case ir_unop_pack_unorm_4x8:
370 assert(ir->type == glsl_type::uint_type);
371 assert(ir->operands[0]->type == glsl_type::vec4_type);
374 case ir_unop_unpack_snorm_2x16:
375 case ir_unop_unpack_unorm_2x16:
376 case ir_unop_unpack_half_2x16:
377 assert(ir->type == glsl_type::vec2_type);
378 assert(ir->operands[0]->type == glsl_type::uint_type);
381 case ir_unop_unpack_snorm_4x8:
382 case ir_unop_unpack_unorm_4x8:
383 assert(ir->type == glsl_type::vec4_type);
384 assert(ir->operands[0]->type == glsl_type::uint_type);
387 case ir_unop_unpack_half_2x16_split_x:
388 case ir_unop_unpack_half_2x16_split_y:
389 assert(ir->type == glsl_type::float_type);
390 assert(ir->operands[0]->type == glsl_type::uint_type);
393 case ir_unop_bitfield_reverse:
394 assert(ir->operands[0]->type == ir->type);
395 assert(ir->type->is_integer());
398 case ir_unop_bit_count:
399 case ir_unop_find_msb:
400 case ir_unop_find_lsb:
401 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
402 assert(ir->operands[0]->type->is_integer());
403 assert(ir->type->base_type == GLSL_TYPE_INT);
407 /* XXX what can we assert here? */
418 if (ir->operands[0]->type->is_scalar())
419 assert(ir->operands[1]->type == ir->type);
420 else if (ir->operands[1]->type->is_scalar())
421 assert(ir->operands[0]->type == ir->type);
422 else if (ir->operands[0]->type->is_vector() &&
423 ir->operands[1]->type->is_vector()) {
424 assert(ir->operands[0]->type == ir->operands[1]->type);
425 assert(ir->operands[0]->type == ir->type);
430 case ir_binop_greater:
431 case ir_binop_lequal:
432 case ir_binop_gequal:
434 case ir_binop_nequal:
435 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
436 * ==, and != operators. The IR operators perform a component-wise
437 * comparison on scalar or vector types and return a boolean scalar or
438 * vector type of the same size.
440 assert(ir->type->base_type == GLSL_TYPE_BOOL);
441 assert(ir->operands[0]->type == ir->operands[1]->type);
442 assert(ir->operands[0]->type->is_vector()
443 || ir->operands[0]->type->is_scalar());
444 assert(ir->operands[0]->type->vector_elements
445 == ir->type->vector_elements);
448 case ir_binop_all_equal:
449 case ir_binop_any_nequal:
450 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
451 * return a scalar boolean. The IR matches that.
453 assert(ir->type == glsl_type::bool_type);
454 assert(ir->operands[0]->type == ir->operands[1]->type);
457 case ir_binop_lshift:
458 case ir_binop_rshift:
459 assert(ir->operands[0]->type->is_integer() &&
460 ir->operands[1]->type->is_integer());
461 if (ir->operands[0]->type->is_scalar()) {
462 assert(ir->operands[1]->type->is_scalar());
464 if (ir->operands[0]->type->is_vector() &&
465 ir->operands[1]->type->is_vector()) {
466 assert(ir->operands[0]->type->components() ==
467 ir->operands[1]->type->components());
469 assert(ir->type == ir->operands[0]->type);
472 case ir_binop_bit_and:
473 case ir_binop_bit_xor:
474 case ir_binop_bit_or:
475 assert(ir->operands[0]->type->base_type ==
476 ir->operands[1]->type->base_type);
477 assert(ir->type->is_integer());
478 if (ir->operands[0]->type->is_vector() &&
479 ir->operands[1]->type->is_vector()) {
480 assert(ir->operands[0]->type->vector_elements ==
481 ir->operands[1]->type->vector_elements);
485 case ir_binop_logic_and:
486 case ir_binop_logic_xor:
487 case ir_binop_logic_or:
488 assert(ir->type == glsl_type::bool_type);
489 assert(ir->operands[0]->type == glsl_type::bool_type);
490 assert(ir->operands[1]->type == glsl_type::bool_type);
494 assert(ir->type == glsl_type::float_type);
495 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
496 assert(ir->operands[0]->type->is_vector());
497 assert(ir->operands[0]->type == ir->operands[1]->type);
500 case ir_binop_pack_half_2x16_split:
501 assert(ir->type == glsl_type::uint_type);
502 assert(ir->operands[0]->type == glsl_type::float_type);
503 assert(ir->operands[1]->type == glsl_type::float_type);
507 assert(ir->type->is_integer());
508 assert(ir->operands[0]->type->is_integer());
509 assert(ir->operands[1]->type->is_integer());
512 case ir_binop_ubo_load:
513 assert(ir->operands[0]->as_constant());
514 assert(ir->operands[0]->type == glsl_type::uint_type);
516 assert(ir->operands[1]->type == glsl_type::uint_type);
519 case ir_binop_vector_extract:
520 assert(ir->operands[0]->type->is_vector());
521 assert(ir->operands[1]->type->is_scalar()
522 && ir->operands[1]->type->is_integer());
526 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
527 assert(ir->operands[0]->type == ir->operands[1]->type);
528 assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
532 assert(ir->operands[0]->type->is_integer());
533 assert(ir->operands[1]->type == ir->operands[2]->type);
534 assert(ir->operands[1]->type == ir->type);
537 case ir_triop_bitfield_extract:
538 assert(ir->operands[0]->type == ir->type);
539 assert(ir->operands[1]->type == glsl_type::int_type);
540 assert(ir->operands[2]->type == glsl_type::int_type);
543 case ir_triop_vector_insert:
544 assert(ir->operands[0]->type->is_vector());
545 assert(ir->operands[1]->type->is_scalar());
546 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
547 assert(ir->operands[2]->type->is_scalar()
548 && ir->operands[2]->type->is_integer());
549 assert(ir->type == ir->operands[0]->type);
552 case ir_quadop_bitfield_insert:
553 assert(ir->operands[0]->type == ir->type);
554 assert(ir->operands[1]->type == ir->type);
555 assert(ir->operands[2]->type == glsl_type::int_type);
556 assert(ir->operands[3]->type == glsl_type::int_type);
559 case ir_quadop_vector:
560 /* The vector operator collects some number of scalars and generates a
563 * - All of the operands must be scalar.
564 * - Number of operands must matche the size of the resulting vector.
565 * - Base type of the operands must match the base type of the result.
567 assert(ir->type->is_vector());
568 switch (ir->type->vector_elements) {
570 assert(ir->operands[0]->type->is_scalar());
571 assert(ir->operands[0]->type->base_type == ir->type->base_type);
572 assert(ir->operands[1]->type->is_scalar());
573 assert(ir->operands[1]->type->base_type == ir->type->base_type);
574 assert(ir->operands[2] == NULL);
575 assert(ir->operands[3] == NULL);
578 assert(ir->operands[0]->type->is_scalar());
579 assert(ir->operands[0]->type->base_type == ir->type->base_type);
580 assert(ir->operands[1]->type->is_scalar());
581 assert(ir->operands[1]->type->base_type == ir->type->base_type);
582 assert(ir->operands[2]->type->is_scalar());
583 assert(ir->operands[2]->type->base_type == ir->type->base_type);
584 assert(ir->operands[3] == NULL);
587 assert(ir->operands[0]->type->is_scalar());
588 assert(ir->operands[0]->type->base_type == ir->type->base_type);
589 assert(ir->operands[1]->type->is_scalar());
590 assert(ir->operands[1]->type->base_type == ir->type->base_type);
591 assert(ir->operands[2]->type->is_scalar());
592 assert(ir->operands[2]->type->base_type == ir->type->base_type);
593 assert(ir->operands[3]->type->is_scalar());
594 assert(ir->operands[3]->type->base_type == ir->type->base_type);
597 /* The is_vector assertion above should prevent execution from ever
600 assert(!"Should not get here.");
605 return visit_continue;
609 ir_validate::visit_leave(ir_swizzle *ir)
611 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
613 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
614 if (chans[i] >= ir->val->type->vector_elements) {
615 printf("ir_swizzle @ %p specifies a channel not present "
616 "in the value.\n", (void *) ir);
622 return visit_continue;
626 ir_validate::visit(ir_variable *ir)
628 /* An ir_variable is the one thing that can (and will) appear multiple times
629 * in an IR tree. It is added to the hashtable so that it can be used
630 * in the ir_dereference_variable handler to ensure that a variable is
631 * declared before it is dereferenced.
634 assert(ralloc_parent(ir->name) == ir);
636 hash_table_insert(ht, ir, ir);
639 /* If a variable is an array, verify that the maximum array index is in
640 * bounds. There was once an error in AST-to-HIR conversion that set this
641 * to be out of bounds.
643 if (ir->type->array_size() > 0) {
644 if (ir->max_array_access >= ir->type->length) {
645 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
646 ir->max_array_access, ir->type->length - 1);
652 if (ir->constant_initializer != NULL && !ir->has_initializer) {
653 printf("ir_variable didn't have an initializer, but has a constant "
654 "initializer value.\n");
659 return visit_continue;
663 ir_validate::visit_enter(ir_assignment *ir)
665 const ir_dereference *const lhs = ir->lhs;
666 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
667 if (ir->write_mask == 0) {
668 printf("Assignment LHS is %s, but write mask is 0:\n",
669 lhs->type->is_scalar() ? "scalar" : "vector");
674 int lhs_components = 0;
675 for (int i = 0; i < 4; i++) {
676 if (ir->write_mask & (1 << i))
680 if (lhs_components != ir->rhs->type->vector_elements) {
681 printf("Assignment count of LHS write mask channels enabled not\n"
682 "matching RHS vector size (%d LHS, %d RHS).\n",
683 lhs_components, ir->rhs->type->vector_elements);
689 this->validate_ir(ir, this->data);
691 return visit_continue;
695 ir_validate::visit_enter(ir_call *ir)
697 ir_function_signature *const callee = ir->callee;
699 if (callee->ir_type != ir_type_function_signature) {
700 printf("IR called by ir_call is not ir_function_signature!\n");
704 if (ir->return_deref) {
705 if (ir->return_deref->type != callee->return_type) {
706 printf("callee type %s does not match return storage type %s\n",
707 callee->return_type->name, ir->return_deref->type->name);
710 } else if (callee->return_type != glsl_type::void_type) {
711 printf("ir_call has non-void callee but no return storage\n");
715 const exec_node *formal_param_node = callee->parameters.head;
716 const exec_node *actual_param_node = ir->actual_parameters.head;
718 if (formal_param_node->is_tail_sentinel()
719 != actual_param_node->is_tail_sentinel()) {
720 printf("ir_call has the wrong number of parameters:\n");
723 if (formal_param_node->is_tail_sentinel()) {
726 const ir_variable *formal_param
727 = (const ir_variable *) formal_param_node;
728 const ir_rvalue *actual_param
729 = (const ir_rvalue *) actual_param_node;
730 if (formal_param->type != actual_param->type) {
731 printf("ir_call parameter type mismatch:\n");
734 if (formal_param->mode == ir_var_function_out
735 || formal_param->mode == ir_var_function_inout) {
736 if (!actual_param->is_lvalue()) {
737 printf("ir_call out/inout parameters must be lvalues:\n");
741 formal_param_node = formal_param_node->next;
742 actual_param_node = actual_param_node->next;
745 return visit_continue;
756 ir_validate::validate_ir(ir_instruction *ir, void *data)
758 struct hash_table *ht = (struct hash_table *) data;
760 if (hash_table_find(ht, ir)) {
761 printf("Instruction node present twice in ir tree:\n");
766 hash_table_insert(ht, ir, ir);
770 check_node_type(ir_instruction *ir, void *data)
774 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
775 printf("Instruction node with unset type\n");
776 ir->print(); printf("\n");
778 ir_rvalue *value = ir->as_rvalue();
780 assert(value->type != glsl_type::error_type);
784 validate_ir_tree(exec_list *instructions)
786 /* We shouldn't have any reason to validate IR in a release build,
787 * and it's half composed of assert()s anyway which wouldn't do
795 foreach_iter(exec_list_iterator, iter, *instructions) {
796 ir_instruction *ir = (ir_instruction *)iter.get();
798 visit_tree(ir, check_node_type, NULL);