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(ir_assignment *ir);
73 virtual ir_visitor_status visit_enter(ir_call *ir);
75 static void validate_ir(ir_instruction *ir, void *data);
77 ir_function *current_function;
79 struct hash_table *ht;
84 ir_validate::visit(ir_dereference_variable *ir)
86 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
87 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
88 (void *) ir, (void *) ir->var);
92 if (hash_table_find(ht, ir->var) == NULL) {
93 printf("ir_dereference_variable @ %p specifies undeclared variable "
95 (void *) ir, ir->var->name, (void *) ir->var);
99 this->validate_ir(ir, this->data);
101 return visit_continue;
105 ir_validate::visit_enter(ir_if *ir)
107 if (ir->condition->type != glsl_type::bool_type) {
108 printf("ir_if condition %s type instead of bool.\n",
109 ir->condition->type->name);
115 return visit_continue;
120 ir_validate::visit_leave(ir_loop *ir)
122 if (ir->counter != NULL) {
123 if ((ir->from == NULL) || (ir->to == NULL) || (ir->increment == NULL)) {
124 printf("ir_loop has invalid loop controls:\n"
129 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
130 (void *) ir->increment);
134 if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
135 printf("ir_loop has invalid comparitor %d\n", ir->cmp);
139 if ((ir->from != NULL) || (ir->to != NULL) || (ir->increment != NULL)) {
140 printf("ir_loop has invalid loop controls:\n"
145 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
146 (void *) ir->increment);
151 return visit_continue;
156 ir_validate::visit_enter(ir_function *ir)
158 /* Function definitions cannot be nested.
160 if (this->current_function != NULL) {
161 printf("Function definition nested inside another function "
163 printf("%s %p inside %s %p\n",
164 ir->name, (void *) ir,
165 this->current_function->name, (void *) this->current_function);
169 /* Store the current function hierarchy being traversed. This is used
170 * by the function signature visitor to ensure that the signatures are
171 * linked with the correct functions.
173 this->current_function = ir;
175 this->validate_ir(ir, this->data);
177 /* Verify that all of the things stored in the list of signatures are,
178 * in fact, function signatures.
180 foreach_list(node, &ir->signatures) {
181 ir_instruction *sig = (ir_instruction *) node;
183 if (sig->ir_type != ir_type_function_signature) {
184 printf("Non-signature in signature list of function `%s'\n",
190 return visit_continue;
194 ir_validate::visit_leave(ir_function *ir)
196 assert(ralloc_parent(ir->name) == ir);
198 this->current_function = NULL;
199 return visit_continue;
203 ir_validate::visit_enter(ir_function_signature *ir)
205 if (this->current_function != ir->function()) {
206 printf("Function signature nested inside wrong function "
208 printf("%p inside %s %p instead of %s %p\n",
210 this->current_function->name, (void *) this->current_function,
211 ir->function_name(), (void *) ir->function());
215 if (ir->return_type == NULL) {
216 printf("Function signature %p for function %s has NULL return type.\n",
217 (void *) ir, ir->function_name());
221 this->validate_ir(ir, this->data);
223 return visit_continue;
227 ir_validate::visit_leave(ir_expression *ir)
229 switch (ir->operation) {
230 case ir_unop_bit_not:
231 assert(ir->operands[0]->type == ir->type);
233 case ir_unop_logic_not:
234 assert(ir->type->base_type == GLSL_TYPE_BOOL);
235 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
244 assert(ir->type == ir->operands[0]->type);
251 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
252 assert(ir->type == ir->operands[0]->type);
256 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
257 assert(ir->type->base_type == GLSL_TYPE_INT);
260 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
261 assert(ir->type->base_type == GLSL_TYPE_UINT);
264 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
265 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
268 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
269 assert(ir->type->base_type == GLSL_TYPE_BOOL);
272 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
273 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
276 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
277 assert(ir->type->base_type == GLSL_TYPE_BOOL);
280 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
281 assert(ir->type->base_type == GLSL_TYPE_INT);
284 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
285 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
288 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
289 assert(ir->type->base_type == GLSL_TYPE_UINT);
292 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
293 assert(ir->type->base_type == GLSL_TYPE_INT);
295 case ir_unop_bitcast_i2f:
296 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
297 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
299 case ir_unop_bitcast_f2i:
300 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
301 assert(ir->type->base_type == GLSL_TYPE_INT);
303 case ir_unop_bitcast_u2f:
304 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
305 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
307 case ir_unop_bitcast_f2u:
308 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
309 assert(ir->type->base_type == GLSL_TYPE_UINT);
313 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
314 assert(ir->type == glsl_type::bool_type);
318 case ir_unop_round_even:
324 case ir_unop_sin_reduced:
325 case ir_unop_cos_reduced:
328 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
329 assert(ir->operands[0]->type == ir->type);
332 case ir_unop_pack_snorm_2x16:
333 case ir_unop_pack_unorm_2x16:
334 case ir_unop_pack_half_2x16:
335 assert(ir->type == glsl_type::uint_type);
336 assert(ir->operands[0]->type == glsl_type::vec2_type);
339 case ir_unop_pack_snorm_4x8:
340 case ir_unop_pack_unorm_4x8:
341 assert(ir->type == glsl_type::uint_type);
342 assert(ir->operands[0]->type == glsl_type::vec4_type);
345 case ir_unop_unpack_snorm_2x16:
346 case ir_unop_unpack_unorm_2x16:
347 case ir_unop_unpack_half_2x16:
348 assert(ir->type == glsl_type::vec2_type);
349 assert(ir->operands[0]->type == glsl_type::uint_type);
352 case ir_unop_unpack_snorm_4x8:
353 case ir_unop_unpack_unorm_4x8:
354 assert(ir->type == glsl_type::vec4_type);
355 assert(ir->operands[0]->type == glsl_type::uint_type);
358 case ir_unop_unpack_half_2x16_split_x:
359 case ir_unop_unpack_half_2x16_split_y:
360 assert(ir->type == glsl_type::float_type);
361 assert(ir->operands[0]->type == glsl_type::uint_type);
364 case ir_unop_bitfield_reverse:
365 assert(ir->operands[0]->type == ir->type);
366 assert(ir->type->is_integer());
369 case ir_unop_bit_count:
370 case ir_unop_find_msb:
371 case ir_unop_find_lsb:
372 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
373 assert(ir->operands[0]->type->is_integer());
374 assert(ir->type->base_type == GLSL_TYPE_INT);
378 /* XXX what can we assert here? */
389 if (ir->operands[0]->type->is_scalar())
390 assert(ir->operands[1]->type == ir->type);
391 else if (ir->operands[1]->type->is_scalar())
392 assert(ir->operands[0]->type == ir->type);
393 else if (ir->operands[0]->type->is_vector() &&
394 ir->operands[1]->type->is_vector()) {
395 assert(ir->operands[0]->type == ir->operands[1]->type);
396 assert(ir->operands[0]->type == ir->type);
401 case ir_binop_greater:
402 case ir_binop_lequal:
403 case ir_binop_gequal:
405 case ir_binop_nequal:
406 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
407 * ==, and != operators. The IR operators perform a component-wise
408 * comparison on scalar or vector types and return a boolean scalar or
409 * vector type of the same size.
411 assert(ir->type->base_type == GLSL_TYPE_BOOL);
412 assert(ir->operands[0]->type == ir->operands[1]->type);
413 assert(ir->operands[0]->type->is_vector()
414 || ir->operands[0]->type->is_scalar());
415 assert(ir->operands[0]->type->vector_elements
416 == ir->type->vector_elements);
419 case ir_binop_all_equal:
420 case ir_binop_any_nequal:
421 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
422 * return a scalar boolean. The IR matches that.
424 assert(ir->type == glsl_type::bool_type);
425 assert(ir->operands[0]->type == ir->operands[1]->type);
428 case ir_binop_lshift:
429 case ir_binop_rshift:
430 assert(ir->operands[0]->type->is_integer() &&
431 ir->operands[1]->type->is_integer());
432 if (ir->operands[0]->type->is_scalar()) {
433 assert(ir->operands[1]->type->is_scalar());
435 if (ir->operands[0]->type->is_vector() &&
436 ir->operands[1]->type->is_vector()) {
437 assert(ir->operands[0]->type->components() ==
438 ir->operands[1]->type->components());
440 assert(ir->type == ir->operands[0]->type);
443 case ir_binop_bit_and:
444 case ir_binop_bit_xor:
445 case ir_binop_bit_or:
446 assert(ir->operands[0]->type->base_type ==
447 ir->operands[1]->type->base_type);
448 assert(ir->type->is_integer());
449 if (ir->operands[0]->type->is_vector() &&
450 ir->operands[1]->type->is_vector()) {
451 assert(ir->operands[0]->type->vector_elements ==
452 ir->operands[1]->type->vector_elements);
456 case ir_binop_logic_and:
457 case ir_binop_logic_xor:
458 case ir_binop_logic_or:
459 assert(ir->type == glsl_type::bool_type);
460 assert(ir->operands[0]->type == glsl_type::bool_type);
461 assert(ir->operands[1]->type == glsl_type::bool_type);
465 assert(ir->type == glsl_type::float_type);
466 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
467 assert(ir->operands[0]->type->is_vector());
468 assert(ir->operands[0]->type == ir->operands[1]->type);
471 case ir_binop_pack_half_2x16_split:
472 assert(ir->type == glsl_type::uint_type);
473 assert(ir->operands[0]->type == glsl_type::float_type);
474 assert(ir->operands[1]->type == glsl_type::float_type);
478 assert(ir->type->is_integer());
479 assert(ir->operands[0]->type->is_integer());
480 assert(ir->operands[1]->type->is_integer());
483 case ir_binop_ubo_load:
484 assert(ir->operands[0]->as_constant());
485 assert(ir->operands[0]->type == glsl_type::uint_type);
487 assert(ir->operands[1]->type == glsl_type::uint_type);
490 case ir_binop_vector_extract:
491 assert(ir->operands[0]->type->is_vector());
492 assert(ir->operands[1]->type->is_scalar()
493 && ir->operands[1]->type->is_integer());
497 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
498 assert(ir->operands[0]->type == ir->operands[1]->type);
499 assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
503 assert(ir->operands[0]->type->is_integer());
504 assert(ir->operands[1]->type == ir->operands[2]->type);
505 assert(ir->operands[1]->type == ir->type);
508 case ir_triop_bitfield_extract:
509 assert(ir->operands[0]->type == ir->type);
510 assert(ir->operands[1]->type == glsl_type::int_type);
511 assert(ir->operands[2]->type == glsl_type::int_type);
514 case ir_triop_vector_insert:
515 assert(ir->operands[0]->type->is_vector());
516 assert(ir->operands[1]->type->is_scalar());
517 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
518 assert(ir->operands[2]->type->is_scalar()
519 && ir->operands[2]->type->is_integer());
520 assert(ir->type == ir->operands[0]->type);
523 case ir_quadop_bitfield_insert:
524 assert(ir->operands[0]->type == ir->type);
525 assert(ir->operands[1]->type == ir->type);
526 assert(ir->operands[2]->type == glsl_type::int_type);
527 assert(ir->operands[3]->type == glsl_type::int_type);
530 case ir_quadop_vector:
531 /* The vector operator collects some number of scalars and generates a
534 * - All of the operands must be scalar.
535 * - Number of operands must matche the size of the resulting vector.
536 * - Base type of the operands must match the base type of the result.
538 assert(ir->type->is_vector());
539 switch (ir->type->vector_elements) {
541 assert(ir->operands[0]->type->is_scalar());
542 assert(ir->operands[0]->type->base_type == ir->type->base_type);
543 assert(ir->operands[1]->type->is_scalar());
544 assert(ir->operands[1]->type->base_type == ir->type->base_type);
545 assert(ir->operands[2] == NULL);
546 assert(ir->operands[3] == NULL);
549 assert(ir->operands[0]->type->is_scalar());
550 assert(ir->operands[0]->type->base_type == ir->type->base_type);
551 assert(ir->operands[1]->type->is_scalar());
552 assert(ir->operands[1]->type->base_type == ir->type->base_type);
553 assert(ir->operands[2]->type->is_scalar());
554 assert(ir->operands[2]->type->base_type == ir->type->base_type);
555 assert(ir->operands[3] == NULL);
558 assert(ir->operands[0]->type->is_scalar());
559 assert(ir->operands[0]->type->base_type == ir->type->base_type);
560 assert(ir->operands[1]->type->is_scalar());
561 assert(ir->operands[1]->type->base_type == ir->type->base_type);
562 assert(ir->operands[2]->type->is_scalar());
563 assert(ir->operands[2]->type->base_type == ir->type->base_type);
564 assert(ir->operands[3]->type->is_scalar());
565 assert(ir->operands[3]->type->base_type == ir->type->base_type);
568 /* The is_vector assertion above should prevent execution from ever
571 assert(!"Should not get here.");
576 return visit_continue;
580 ir_validate::visit_leave(ir_swizzle *ir)
582 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
584 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
585 if (chans[i] >= ir->val->type->vector_elements) {
586 printf("ir_swizzle @ %p specifies a channel not present "
587 "in the value.\n", (void *) ir);
593 return visit_continue;
597 ir_validate::visit(ir_variable *ir)
599 /* An ir_variable is the one thing that can (and will) appear multiple times
600 * in an IR tree. It is added to the hashtable so that it can be used
601 * in the ir_dereference_variable handler to ensure that a variable is
602 * declared before it is dereferenced.
605 assert(ralloc_parent(ir->name) == ir);
607 hash_table_insert(ht, ir, ir);
610 /* If a variable is an array, verify that the maximum array index is in
611 * bounds. There was once an error in AST-to-HIR conversion that set this
612 * to be out of bounds.
614 if (ir->type->array_size() > 0) {
615 if (ir->max_array_access >= ir->type->length) {
616 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
617 ir->max_array_access, ir->type->length - 1);
623 if (ir->constant_initializer != NULL && !ir->has_initializer) {
624 printf("ir_variable didn't have an initializer, but has a constant "
625 "initializer value.\n");
630 return visit_continue;
634 ir_validate::visit_enter(ir_assignment *ir)
636 const ir_dereference *const lhs = ir->lhs;
637 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
638 if (ir->write_mask == 0) {
639 printf("Assignment LHS is %s, but write mask is 0:\n",
640 lhs->type->is_scalar() ? "scalar" : "vector");
645 int lhs_components = 0;
646 for (int i = 0; i < 4; i++) {
647 if (ir->write_mask & (1 << i))
651 if (lhs_components != ir->rhs->type->vector_elements) {
652 printf("Assignment count of LHS write mask channels enabled not\n"
653 "matching RHS vector size (%d LHS, %d RHS).\n",
654 lhs_components, ir->rhs->type->vector_elements);
660 this->validate_ir(ir, this->data);
662 return visit_continue;
666 ir_validate::visit_enter(ir_call *ir)
668 ir_function_signature *const callee = ir->callee;
670 if (callee->ir_type != ir_type_function_signature) {
671 printf("IR called by ir_call is not ir_function_signature!\n");
675 if (ir->return_deref) {
676 if (ir->return_deref->type != callee->return_type) {
677 printf("callee type %s does not match return storage type %s\n",
678 callee->return_type->name, ir->return_deref->type->name);
681 } else if (callee->return_type != glsl_type::void_type) {
682 printf("ir_call has non-void callee but no return storage\n");
686 const exec_node *formal_param_node = callee->parameters.head;
687 const exec_node *actual_param_node = ir->actual_parameters.head;
689 if (formal_param_node->is_tail_sentinel()
690 != actual_param_node->is_tail_sentinel()) {
691 printf("ir_call has the wrong number of parameters:\n");
694 if (formal_param_node->is_tail_sentinel()) {
697 const ir_variable *formal_param
698 = (const ir_variable *) formal_param_node;
699 const ir_rvalue *actual_param
700 = (const ir_rvalue *) actual_param_node;
701 if (formal_param->type != actual_param->type) {
702 printf("ir_call parameter type mismatch:\n");
705 if (formal_param->mode == ir_var_function_out
706 || formal_param->mode == ir_var_function_inout) {
707 if (!actual_param->is_lvalue()) {
708 printf("ir_call out/inout parameters must be lvalues:\n");
712 formal_param_node = formal_param_node->next;
713 actual_param_node = actual_param_node->next;
716 return visit_continue;
727 ir_validate::validate_ir(ir_instruction *ir, void *data)
729 struct hash_table *ht = (struct hash_table *) data;
731 if (hash_table_find(ht, ir)) {
732 printf("Instruction node present twice in ir tree:\n");
737 hash_table_insert(ht, ir, ir);
741 check_node_type(ir_instruction *ir, void *data)
745 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
746 printf("Instruction node with unset type\n");
747 ir->print(); printf("\n");
749 ir_rvalue *value = ir->as_rvalue();
751 assert(value->type != glsl_type::error_type);
755 validate_ir_tree(exec_list *instructions)
757 /* We shouldn't have any reason to validate IR in a release build,
758 * and it's half composed of assert()s anyway which wouldn't do
766 foreach_iter(exec_list_iterator, iter, *instructions) {
767 ir_instruction *ir = (ir_instruction *)iter.get();
769 visit_tree(ir, check_node_type, NULL);