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
);
62 virtual ir_visitor_status
visit(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
);
71 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
73 static void validate_ir(ir_instruction
*ir
, void *data
);
75 ir_function
*current_function
;
77 struct hash_table
*ht
;
82 ir_validate::visit(ir_dereference_variable
*ir
)
84 if ((ir
->var
== NULL
) || (ir
->var
->as_variable() == NULL
)) {
85 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
86 (void *) ir
, (void *) ir
->var
);
90 if (hash_table_find(ht
, ir
->var
) == NULL
) {
91 printf("ir_dereference_variable @ %p specifies undeclared variable "
93 (void *) ir
, ir
->var
->name
, (void *) ir
->var
);
97 this->validate_ir(ir
, this->data
);
99 return visit_continue
;
103 ir_validate::visit(ir_if
*ir
)
105 if (ir
->condition
->type
!= glsl_type::bool_type
) {
106 printf("ir_if condition %s type instead of bool.\n",
107 ir
->condition
->type
->name
);
113 return visit_continue
;
118 ir_validate::visit_leave(ir_loop
*ir
)
120 if (ir
->counter
!= NULL
) {
121 if ((ir
->from
== NULL
) || (ir
->from
== NULL
) || (ir
->increment
== NULL
)) {
122 printf("ir_loop has invalid loop controls:\n"
127 (void *) ir
->counter
, (void *) ir
->from
, (void *) ir
->to
,
128 (void *) ir
->increment
);
132 if ((ir
->cmp
< ir_binop_less
) || (ir
->cmp
> ir_binop_nequal
)) {
133 printf("ir_loop has invalid comparitor %d\n", ir
->cmp
);
137 if ((ir
->from
!= NULL
) || (ir
->from
!= NULL
) || (ir
->increment
!= NULL
)) {
138 printf("ir_loop has invalid loop controls:\n"
143 (void *) ir
->counter
, (void *) ir
->from
, (void *) ir
->to
,
144 (void *) ir
->increment
);
149 return visit_continue
;
154 ir_validate::visit_enter(ir_function
*ir
)
156 /* Function definitions cannot be nested.
158 if (this->current_function
!= NULL
) {
159 printf("Function definition nested inside another function "
161 printf("%s %p inside %s %p\n",
162 ir
->name
, (void *) ir
,
163 this->current_function
->name
, (void *) this->current_function
);
167 /* Store the current function hierarchy being traversed. This is used
168 * by the function signature visitor to ensure that the signatures are
169 * linked with the correct functions.
171 this->current_function
= ir
;
173 this->validate_ir(ir
, this->data
);
175 return visit_continue
;
179 ir_validate::visit_leave(ir_function
*ir
)
181 assert(talloc_parent(ir
->name
) == ir
);
183 this->current_function
= NULL
;
184 return visit_continue
;
188 ir_validate::visit_enter(ir_function_signature
*ir
)
190 if (this->current_function
!= ir
->function()) {
191 printf("Function signature nested inside wrong function "
193 printf("%p inside %s %p instead of %s %p\n",
195 this->current_function
->name
, (void *) this->current_function
,
196 ir
->function_name(), (void *) ir
->function());
200 this->validate_ir(ir
, this->data
);
202 return visit_continue
;
206 ir_validate::visit_leave(ir_expression
*ir
)
208 switch (ir
->operation
) {
209 case ir_unop_bit_not
:
210 assert(ir
->operands
[0]->type
== ir
->type
);
212 case ir_unop_logic_not
:
213 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
214 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
223 assert(ir
->type
== ir
->operands
[0]->type
);
230 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
231 assert(ir
->type
== ir
->operands
[0]->type
);
235 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
236 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
239 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
240 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
243 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
244 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
247 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
248 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
251 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
252 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
255 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
256 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
259 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
260 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
264 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
265 assert(ir
->type
== glsl_type::bool_type
);
276 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
277 assert(ir
->operands
[0]->type
== ir
->type
);
281 /* XXX what can we assert here? */
292 if (ir
->operands
[0]->type
->is_scalar())
293 assert(ir
->operands
[1]->type
== ir
->type
);
294 else if (ir
->operands
[1]->type
->is_scalar())
295 assert(ir
->operands
[0]->type
== ir
->type
);
296 else if (ir
->operands
[0]->type
->is_vector() &&
297 ir
->operands
[1]->type
->is_vector()) {
298 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
299 assert(ir
->operands
[0]->type
== ir
->type
);
304 case ir_binop_greater
:
305 case ir_binop_lequal
:
306 case ir_binop_gequal
:
308 case ir_binop_nequal
:
309 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
310 * ==, and != operators. The IR operators perform a component-wise
311 * comparison on scalar or vector types and return a boolean scalar or
312 * vector type of the same size.
314 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
315 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
316 assert(ir
->operands
[0]->type
->is_vector()
317 || ir
->operands
[0]->type
->is_scalar());
318 assert(ir
->operands
[0]->type
->vector_elements
319 == ir
->type
->vector_elements
);
322 case ir_binop_all_equal
:
323 case ir_binop_any_nequal
:
324 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
325 * return a scalar boolean. The IR matches that.
327 assert(ir
->type
== glsl_type::bool_type
);
328 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
331 case ir_binop_lshift
:
332 case ir_binop_rshift
:
333 case ir_binop_bit_and
:
334 case ir_binop_bit_xor
:
335 case ir_binop_bit_or
:
336 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
337 assert(ir
->type
== ir
->operands
[0]->type
);
338 assert(ir
->type
->base_type
== GLSL_TYPE_INT
||
339 ir
->type
->base_type
== GLSL_TYPE_UINT
);
342 case ir_binop_logic_and
:
343 case ir_binop_logic_xor
:
344 case ir_binop_logic_or
:
345 assert(ir
->type
== glsl_type::bool_type
);
346 assert(ir
->operands
[0]->type
== glsl_type::bool_type
);
347 assert(ir
->operands
[1]->type
== glsl_type::bool_type
);
351 assert(ir
->type
== glsl_type::float_type
);
352 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
353 assert(ir
->operands
[0]->type
->is_vector());
354 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
358 assert(ir
->operands
[0]->type
== glsl_type::vec3_type
);
359 assert(ir
->operands
[1]->type
== glsl_type::vec3_type
);
360 assert(ir
->type
== glsl_type::vec3_type
);
364 return visit_continue
;
368 ir_validate::visit(ir_variable
*ir
)
370 /* An ir_variable is the one thing that can (and will) appear multiple times
371 * in an IR tree. It is added to the hashtable so that it can be used
372 * in the ir_dereference_variable handler to ensure that a variable is
373 * declared before it is dereferenced.
376 assert(talloc_parent(ir
->name
) == ir
);
378 hash_table_insert(ht
, ir
, ir
);
379 return visit_continue
;
383 ir_validate::visit_enter(ir_assignment
*ir
)
385 const ir_dereference
*const lhs
= ir
->lhs
;
386 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
387 if (ir
->write_mask
== 0) {
388 printf("Assignment LHS is %s, but write mask is 0:\n",
389 lhs
->type
->is_scalar() ? "scalar" : "vector");
394 int lhs_components
= 0;
395 for (int i
= 0; i
< 4; i
++) {
396 if (ir
->write_mask
& (1 << i
))
400 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
401 printf("Assignment count of LHS write mask channels enabled not\n"
402 "matching RHS vector size (%d LHS, %d RHS).\n",
403 lhs_components
, ir
->rhs
->type
->vector_elements
);
409 this->validate_ir(ir
, this->data
);
411 return visit_continue
;
415 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
417 struct hash_table
*ht
= (struct hash_table
*) data
;
419 if (hash_table_find(ht
, ir
)) {
420 printf("Instruction node present twice in ir tree:\n");
425 hash_table_insert(ht
, ir
, ir
);
429 check_node_type(ir_instruction
*ir
, void *data
)
433 if (ir
->ir_type
<= ir_type_unset
|| ir
->ir_type
>= ir_type_max
) {
434 printf("Instruction node with unset type\n");
435 ir
->print(); printf("\n");
437 assert(ir
->type
!= glsl_type::error_type
);
441 validate_ir_tree(exec_list
*instructions
)
447 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
448 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
450 visit_tree(ir
, check_node_type
, NULL
);