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.
38 #include "ir_hierarchical_visitor.h"
39 #include "program/hash_table.h"
40 #include "glsl_types.h"
42 class ir_validate
: public ir_hierarchical_visitor
{
46 this->ht
= hash_table_ctor(0, hash_table_pointer_hash
,
47 hash_table_pointer_compare
);
49 this->current_function
= NULL
;
51 this->callback
= ir_validate::validate_ir
;
57 hash_table_dtor(this->ht
);
60 virtual ir_visitor_status
visit(ir_variable
*v
);
61 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
);
63 virtual ir_visitor_status
visit_enter(ir_if
*ir
);
65 virtual ir_visitor_status
visit_leave(ir_loop
*ir
);
66 virtual ir_visitor_status
visit_enter(ir_function
*ir
);
67 virtual ir_visitor_status
visit_leave(ir_function
*ir
);
68 virtual ir_visitor_status
visit_enter(ir_function_signature
*ir
);
70 virtual ir_visitor_status
visit_leave(ir_expression
*ir
);
71 virtual ir_visitor_status
visit_leave(ir_swizzle
*ir
);
73 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
74 virtual ir_visitor_status
visit_enter(ir_call
*ir
);
76 static void validate_ir(ir_instruction
*ir
, void *data
);
78 ir_function
*current_function
;
80 struct hash_table
*ht
;
85 ir_validate::visit(ir_dereference_variable
*ir
)
87 if ((ir
->var
== NULL
) || (ir
->var
->as_variable() == NULL
)) {
88 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
89 (void *) ir
, (void *) ir
->var
);
93 if (hash_table_find(ht
, ir
->var
) == NULL
) {
94 printf("ir_dereference_variable @ %p specifies undeclared variable "
96 (void *) ir
, ir
->var
->name
, (void *) ir
->var
);
100 this->validate_ir(ir
, this->data
);
102 return visit_continue
;
106 ir_validate::visit_enter(ir_if
*ir
)
108 if (ir
->condition
->type
!= glsl_type::bool_type
) {
109 printf("ir_if condition %s type instead of bool.\n",
110 ir
->condition
->type
->name
);
116 return visit_continue
;
121 ir_validate::visit_leave(ir_loop
*ir
)
123 if (ir
->counter
!= NULL
) {
124 if ((ir
->from
== NULL
) || (ir
->from
== NULL
) || (ir
->increment
== NULL
)) {
125 printf("ir_loop has invalid loop controls:\n"
130 (void *) ir
->counter
, (void *) ir
->from
, (void *) ir
->to
,
131 (void *) ir
->increment
);
135 if ((ir
->cmp
< ir_binop_less
) || (ir
->cmp
> ir_binop_nequal
)) {
136 printf("ir_loop has invalid comparitor %d\n", ir
->cmp
);
140 if ((ir
->from
!= NULL
) || (ir
->from
!= NULL
) || (ir
->increment
!= NULL
)) {
141 printf("ir_loop has invalid loop controls:\n"
146 (void *) ir
->counter
, (void *) ir
->from
, (void *) ir
->to
,
147 (void *) ir
->increment
);
152 return visit_continue
;
157 ir_validate::visit_enter(ir_function
*ir
)
159 /* Function definitions cannot be nested.
161 if (this->current_function
!= NULL
) {
162 printf("Function definition nested inside another function "
164 printf("%s %p inside %s %p\n",
165 ir
->name
, (void *) ir
,
166 this->current_function
->name
, (void *) this->current_function
);
170 /* Store the current function hierarchy being traversed. This is used
171 * by the function signature visitor to ensure that the signatures are
172 * linked with the correct functions.
174 this->current_function
= ir
;
176 this->validate_ir(ir
, this->data
);
178 /* Verify that all of the things stored in the list of signatures are,
179 * in fact, function signatures.
181 foreach_list(node
, &ir
->signatures
) {
182 ir_instruction
*sig
= (ir_instruction
*) node
;
184 if (sig
->ir_type
!= ir_type_function_signature
) {
185 printf("Non-signature in signature list of function `%s'\n",
191 return visit_continue
;
195 ir_validate::visit_leave(ir_function
*ir
)
197 assert(ralloc_parent(ir
->name
) == ir
);
199 this->current_function
= NULL
;
200 return visit_continue
;
204 ir_validate::visit_enter(ir_function_signature
*ir
)
206 if (this->current_function
!= ir
->function()) {
207 printf("Function signature nested inside wrong function "
209 printf("%p inside %s %p instead of %s %p\n",
211 this->current_function
->name
, (void *) this->current_function
,
212 ir
->function_name(), (void *) ir
->function());
216 if (ir
->return_type
== NULL
) {
217 printf("Function signature %p for function %s has NULL return type.\n",
218 (void *) ir
, ir
->function_name());
222 this->validate_ir(ir
, this->data
);
224 return visit_continue
;
228 ir_validate::visit_leave(ir_expression
*ir
)
230 switch (ir
->operation
) {
231 case ir_unop_bit_not
:
232 assert(ir
->operands
[0]->type
== ir
->type
);
234 case ir_unop_logic_not
:
235 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
236 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
245 assert(ir
->type
== ir
->operands
[0]->type
);
252 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
253 assert(ir
->type
== ir
->operands
[0]->type
);
257 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
258 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
261 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
262 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
265 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
266 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
269 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
270 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
273 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
274 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
277 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
278 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
281 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
282 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
285 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
286 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
289 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
290 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
294 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
295 assert(ir
->type
== glsl_type::bool_type
);
299 case ir_unop_round_even
:
305 case ir_unop_sin_reduced
:
306 case ir_unop_cos_reduced
:
309 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
310 assert(ir
->operands
[0]->type
== ir
->type
);
314 /* XXX what can we assert here? */
325 if (ir
->operands
[0]->type
->is_scalar())
326 assert(ir
->operands
[1]->type
== ir
->type
);
327 else if (ir
->operands
[1]->type
->is_scalar())
328 assert(ir
->operands
[0]->type
== ir
->type
);
329 else if (ir
->operands
[0]->type
->is_vector() &&
330 ir
->operands
[1]->type
->is_vector()) {
331 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
332 assert(ir
->operands
[0]->type
== ir
->type
);
337 case ir_binop_greater
:
338 case ir_binop_lequal
:
339 case ir_binop_gequal
:
341 case ir_binop_nequal
:
342 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
343 * ==, and != operators. The IR operators perform a component-wise
344 * comparison on scalar or vector types and return a boolean scalar or
345 * vector type of the same size.
347 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
348 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
349 assert(ir
->operands
[0]->type
->is_vector()
350 || ir
->operands
[0]->type
->is_scalar());
351 assert(ir
->operands
[0]->type
->vector_elements
352 == ir
->type
->vector_elements
);
355 case ir_binop_all_equal
:
356 case ir_binop_any_nequal
:
357 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
358 * return a scalar boolean. The IR matches that.
360 assert(ir
->type
== glsl_type::bool_type
);
361 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
364 case ir_binop_lshift
:
365 case ir_binop_rshift
:
366 assert(ir
->operands
[0]->type
->is_integer() &&
367 ir
->operands
[1]->type
->is_integer());
368 if (ir
->operands
[0]->type
->is_scalar()) {
369 assert(ir
->operands
[1]->type
->is_scalar());
371 if (ir
->operands
[0]->type
->is_vector() &&
372 ir
->operands
[1]->type
->is_vector()) {
373 assert(ir
->operands
[0]->type
->components() ==
374 ir
->operands
[1]->type
->components());
376 assert(ir
->type
== ir
->operands
[0]->type
);
379 case ir_binop_bit_and
:
380 case ir_binop_bit_xor
:
381 case ir_binop_bit_or
:
382 assert(ir
->operands
[0]->type
->base_type
==
383 ir
->operands
[1]->type
->base_type
);
384 assert(ir
->type
->is_integer());
385 if (ir
->operands
[0]->type
->is_vector() &&
386 ir
->operands
[1]->type
->is_vector()) {
387 assert(ir
->operands
[0]->type
->vector_elements
==
388 ir
->operands
[1]->type
->vector_elements
);
392 case ir_binop_logic_and
:
393 case ir_binop_logic_xor
:
394 case ir_binop_logic_or
:
395 assert(ir
->type
== glsl_type::bool_type
);
396 assert(ir
->operands
[0]->type
== glsl_type::bool_type
);
397 assert(ir
->operands
[1]->type
== glsl_type::bool_type
);
401 assert(ir
->type
== glsl_type::float_type
);
402 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
403 assert(ir
->operands
[0]->type
->is_vector());
404 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
407 case ir_quadop_vector
:
408 /* The vector operator collects some number of scalars and generates a
411 * - All of the operands must be scalar.
412 * - Number of operands must matche the size of the resulting vector.
413 * - Base type of the operands must match the base type of the result.
415 assert(ir
->type
->is_vector());
416 switch (ir
->type
->vector_elements
) {
418 assert(ir
->operands
[0]->type
->is_scalar());
419 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
420 assert(ir
->operands
[1]->type
->is_scalar());
421 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
422 assert(ir
->operands
[2] == NULL
);
423 assert(ir
->operands
[3] == NULL
);
426 assert(ir
->operands
[0]->type
->is_scalar());
427 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
428 assert(ir
->operands
[1]->type
->is_scalar());
429 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
430 assert(ir
->operands
[2]->type
->is_scalar());
431 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
432 assert(ir
->operands
[3] == NULL
);
435 assert(ir
->operands
[0]->type
->is_scalar());
436 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
437 assert(ir
->operands
[1]->type
->is_scalar());
438 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
439 assert(ir
->operands
[2]->type
->is_scalar());
440 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
441 assert(ir
->operands
[3]->type
->is_scalar());
442 assert(ir
->operands
[3]->type
->base_type
== ir
->type
->base_type
);
445 /* The is_vector assertion above should prevent execution from ever
448 assert(!"Should not get here.");
453 return visit_continue
;
457 ir_validate::visit_leave(ir_swizzle
*ir
)
459 int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
461 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
462 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
463 printf("ir_swizzle @ %p specifies a channel not present "
464 "in the value.\n", (void *) ir
);
470 return visit_continue
;
474 ir_validate::visit(ir_variable
*ir
)
476 /* An ir_variable is the one thing that can (and will) appear multiple times
477 * in an IR tree. It is added to the hashtable so that it can be used
478 * in the ir_dereference_variable handler to ensure that a variable is
479 * declared before it is dereferenced.
482 assert(ralloc_parent(ir
->name
) == ir
);
484 hash_table_insert(ht
, ir
, ir
);
487 /* If a variable is an array, verify that the maximum array index is in
488 * bounds. There was once an error in AST-to-HIR conversion that set this
489 * to be out of bounds.
491 if (ir
->type
->array_size() > 0) {
492 if (ir
->max_array_access
>= ir
->type
->length
) {
493 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
494 ir
->max_array_access
, ir
->type
->length
- 1);
500 return visit_continue
;
504 ir_validate::visit_enter(ir_assignment
*ir
)
506 const ir_dereference
*const lhs
= ir
->lhs
;
507 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
508 if (ir
->write_mask
== 0) {
509 printf("Assignment LHS is %s, but write mask is 0:\n",
510 lhs
->type
->is_scalar() ? "scalar" : "vector");
515 int lhs_components
= 0;
516 for (int i
= 0; i
< 4; i
++) {
517 if (ir
->write_mask
& (1 << i
))
521 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
522 printf("Assignment count of LHS write mask channels enabled not\n"
523 "matching RHS vector size (%d LHS, %d RHS).\n",
524 lhs_components
, ir
->rhs
->type
->vector_elements
);
530 this->validate_ir(ir
, this->data
);
532 return visit_continue
;
536 ir_validate::visit_enter(ir_call
*ir
)
538 ir_function_signature
*const callee
= ir
->get_callee();
540 if (callee
->ir_type
!= ir_type_function_signature
) {
541 printf("IR called by ir_call is not ir_function_signature!\n");
545 const exec_node
*formal_param_node
= callee
->parameters
.head
;
546 const exec_node
*actual_param_node
= ir
->actual_parameters
.head
;
548 if (formal_param_node
->is_tail_sentinel()
549 != actual_param_node
->is_tail_sentinel()) {
550 printf("ir_call has the wrong number of parameters:\n");
553 if (formal_param_node
->is_tail_sentinel()) {
556 const ir_variable
*formal_param
557 = (const ir_variable
*) formal_param_node
;
558 const ir_rvalue
*actual_param
559 = (const ir_rvalue
*) actual_param_node
;
560 if (formal_param
->type
!= actual_param
->type
) {
561 printf("ir_call parameter type mismatch:\n");
564 if (formal_param
->mode
== ir_var_out
565 || formal_param
->mode
== ir_var_inout
) {
566 if (!actual_param
->is_lvalue()) {
567 printf("ir_call out/inout parameters must be lvalues:\n");
571 formal_param_node
= formal_param_node
->next
;
572 actual_param_node
= actual_param_node
->next
;
575 return visit_continue
;
585 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
587 struct hash_table
*ht
= (struct hash_table
*) data
;
589 if (hash_table_find(ht
, ir
)) {
590 printf("Instruction node present twice in ir tree:\n");
595 hash_table_insert(ht
, ir
, ir
);
599 check_node_type(ir_instruction
*ir
, void *data
)
603 if (ir
->ir_type
<= ir_type_unset
|| ir
->ir_type
>= ir_type_max
) {
604 printf("Instruction node with unset type\n");
605 ir
->print(); printf("\n");
607 assert(ir
->type
!= glsl_type::error_type
);
611 validate_ir_tree(exec_list
*instructions
)
617 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
618 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
620 visit_tree(ir
, check_node_type
, NULL
);