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
);
70 virtual ir_visitor_status
visit_leave(ir_swizzle
*ir
);
72 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
74 static void validate_ir(ir_instruction
*ir
, void *data
);
76 ir_function
*current_function
;
78 struct hash_table
*ht
;
83 ir_validate::visit(ir_dereference_variable
*ir
)
85 if ((ir
->var
== NULL
) || (ir
->var
->as_variable() == NULL
)) {
86 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
87 (void *) ir
, (void *) ir
->var
);
91 if (hash_table_find(ht
, ir
->var
) == NULL
) {
92 printf("ir_dereference_variable @ %p specifies undeclared variable "
94 (void *) ir
, ir
->var
->name
, (void *) ir
->var
);
98 this->validate_ir(ir
, this->data
);
100 return visit_continue
;
104 ir_validate::visit(ir_if
*ir
)
106 if (ir
->condition
->type
!= glsl_type::bool_type
) {
107 printf("ir_if condition %s type instead of bool.\n",
108 ir
->condition
->type
->name
);
114 return visit_continue
;
119 ir_validate::visit_leave(ir_loop
*ir
)
121 if (ir
->counter
!= NULL
) {
122 if ((ir
->from
== NULL
) || (ir
->from
== NULL
) || (ir
->increment
== NULL
)) {
123 printf("ir_loop has invalid loop controls:\n"
128 (void *) ir
->counter
, (void *) ir
->from
, (void *) ir
->to
,
129 (void *) ir
->increment
);
133 if ((ir
->cmp
< ir_binop_less
) || (ir
->cmp
> ir_binop_nequal
)) {
134 printf("ir_loop has invalid comparitor %d\n", ir
->cmp
);
138 if ((ir
->from
!= NULL
) || (ir
->from
!= NULL
) || (ir
->increment
!= NULL
)) {
139 printf("ir_loop has invalid loop controls:\n"
144 (void *) ir
->counter
, (void *) ir
->from
, (void *) ir
->to
,
145 (void *) ir
->increment
);
150 return visit_continue
;
155 ir_validate::visit_enter(ir_function
*ir
)
157 /* Function definitions cannot be nested.
159 if (this->current_function
!= NULL
) {
160 printf("Function definition nested inside another function "
162 printf("%s %p inside %s %p\n",
163 ir
->name
, (void *) ir
,
164 this->current_function
->name
, (void *) this->current_function
);
168 /* Store the current function hierarchy being traversed. This is used
169 * by the function signature visitor to ensure that the signatures are
170 * linked with the correct functions.
172 this->current_function
= ir
;
174 this->validate_ir(ir
, this->data
);
176 return visit_continue
;
180 ir_validate::visit_leave(ir_function
*ir
)
182 assert(talloc_parent(ir
->name
) == ir
);
184 this->current_function
= NULL
;
185 return visit_continue
;
189 ir_validate::visit_enter(ir_function_signature
*ir
)
191 if (this->current_function
!= ir
->function()) {
192 printf("Function signature nested inside wrong function "
194 printf("%p inside %s %p instead of %s %p\n",
196 this->current_function
->name
, (void *) this->current_function
,
197 ir
->function_name(), (void *) ir
->function());
201 this->validate_ir(ir
, this->data
);
203 return visit_continue
;
207 ir_validate::visit_leave(ir_expression
*ir
)
209 switch (ir
->operation
) {
210 case ir_unop_bit_not
:
211 assert(ir
->operands
[0]->type
== ir
->type
);
213 case ir_unop_logic_not
:
214 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
215 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
224 assert(ir
->type
== ir
->operands
[0]->type
);
231 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
232 assert(ir
->type
== ir
->operands
[0]->type
);
236 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
237 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
240 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
241 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
244 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
245 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
248 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
249 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
252 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
253 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
256 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
257 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
260 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
261 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
265 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
266 assert(ir
->type
== glsl_type::bool_type
);
270 case ir_unop_round_even
:
278 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
279 assert(ir
->operands
[0]->type
== ir
->type
);
283 /* XXX what can we assert here? */
294 if (ir
->operands
[0]->type
->is_scalar())
295 assert(ir
->operands
[1]->type
== ir
->type
);
296 else if (ir
->operands
[1]->type
->is_scalar())
297 assert(ir
->operands
[0]->type
== ir
->type
);
298 else if (ir
->operands
[0]->type
->is_vector() &&
299 ir
->operands
[1]->type
->is_vector()) {
300 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
301 assert(ir
->operands
[0]->type
== ir
->type
);
306 case ir_binop_greater
:
307 case ir_binop_lequal
:
308 case ir_binop_gequal
:
310 case ir_binop_nequal
:
311 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
312 * ==, and != operators. The IR operators perform a component-wise
313 * comparison on scalar or vector types and return a boolean scalar or
314 * vector type of the same size.
316 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
317 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
318 assert(ir
->operands
[0]->type
->is_vector()
319 || ir
->operands
[0]->type
->is_scalar());
320 assert(ir
->operands
[0]->type
->vector_elements
321 == ir
->type
->vector_elements
);
324 case ir_binop_all_equal
:
325 case ir_binop_any_nequal
:
326 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
327 * return a scalar boolean. The IR matches that.
329 assert(ir
->type
== glsl_type::bool_type
);
330 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
333 case ir_binop_lshift
:
334 case ir_binop_rshift
:
335 assert(ir
->operands
[0]->type
->is_integer() &&
336 ir
->operands
[1]->type
->is_integer());
337 if (ir
->operands
[0]->type
->is_scalar()) {
338 assert(ir
->operands
[1]->type
->is_scalar());
340 if (ir
->operands
[0]->type
->is_vector() &&
341 ir
->operands
[1]->type
->is_vector()) {
342 assert(ir
->operands
[0]->type
->components() ==
343 ir
->operands
[1]->type
->components());
345 assert(ir
->type
== ir
->operands
[0]->type
);
348 case ir_binop_bit_and
:
349 case ir_binop_bit_xor
:
350 case ir_binop_bit_or
:
351 assert(ir
->operands
[0]->type
->base_type
==
352 ir
->operands
[1]->type
->base_type
);
353 assert(ir
->type
->is_integer());
354 if (ir
->operands
[0]->type
->is_vector() &&
355 ir
->operands
[1]->type
->is_vector()) {
356 assert(ir
->operands
[0]->type
->vector_elements
==
357 ir
->operands
[1]->type
->vector_elements
);
361 case ir_binop_logic_and
:
362 case ir_binop_logic_xor
:
363 case ir_binop_logic_or
:
364 assert(ir
->type
== glsl_type::bool_type
);
365 assert(ir
->operands
[0]->type
== glsl_type::bool_type
);
366 assert(ir
->operands
[1]->type
== glsl_type::bool_type
);
370 assert(ir
->type
== glsl_type::float_type
);
371 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
372 assert(ir
->operands
[0]->type
->is_vector());
373 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
377 assert(ir
->operands
[0]->type
== glsl_type::vec3_type
);
378 assert(ir
->operands
[1]->type
== glsl_type::vec3_type
);
379 assert(ir
->type
== glsl_type::vec3_type
);
383 return visit_continue
;
387 ir_validate::visit_leave(ir_swizzle
*ir
)
389 int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
391 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
392 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
393 printf("ir_swizzle @ %p specifies a channel not present "
394 "in the value.\n", (void *) ir
);
400 return visit_continue
;
404 ir_validate::visit(ir_variable
*ir
)
406 /* An ir_variable is the one thing that can (and will) appear multiple times
407 * in an IR tree. It is added to the hashtable so that it can be used
408 * in the ir_dereference_variable handler to ensure that a variable is
409 * declared before it is dereferenced.
412 assert(talloc_parent(ir
->name
) == ir
);
414 hash_table_insert(ht
, ir
, ir
);
415 return visit_continue
;
419 ir_validate::visit_enter(ir_assignment
*ir
)
421 const ir_dereference
*const lhs
= ir
->lhs
;
422 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
423 if (ir
->write_mask
== 0) {
424 printf("Assignment LHS is %s, but write mask is 0:\n",
425 lhs
->type
->is_scalar() ? "scalar" : "vector");
430 int lhs_components
= 0;
431 for (int i
= 0; i
< 4; i
++) {
432 if (ir
->write_mask
& (1 << i
))
436 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
437 printf("Assignment count of LHS write mask channels enabled not\n"
438 "matching RHS vector size (%d LHS, %d RHS).\n",
439 lhs_components
, ir
->rhs
->type
->vector_elements
);
445 this->validate_ir(ir
, this->data
);
447 return visit_continue
;
451 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
453 struct hash_table
*ht
= (struct hash_table
*) data
;
455 if (hash_table_find(ht
, ir
)) {
456 printf("Instruction node present twice in ir tree:\n");
461 hash_table_insert(ht
, ir
, ir
);
465 check_node_type(ir_instruction
*ir
, void *data
)
469 if (ir
->ir_type
<= ir_type_unset
|| ir
->ir_type
>= ir_type_max
) {
470 printf("Instruction node with unset type\n");
471 ir
->print(); printf("\n");
473 assert(ir
->type
!= glsl_type::error_type
);
477 validate_ir_tree(exec_list
*instructions
)
483 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
484 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
486 visit_tree(ir
, check_node_type
, NULL
);