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
ir->counter, ir->from, ir->to, ir->increment);
131
if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
132
printf("ir_loop has invalid comparitor %d\n", ir->cmp);
136
if ((ir->from != NULL) || (ir->from != NULL) || (ir->increment != NULL)) {
137
printf("ir_loop has invalid loop controls:\n"
142
ir->counter, ir->from, ir->to, ir->increment);
147
return visit_continue;
152
ir_validate::visit_enter(ir_function *ir)
154
/* Function definitions cannot be nested.
156
if (this->current_function != NULL) {
157
printf("Function definition nested inside another function "
159
printf("%s %p inside %s %p\n",
160
ir->name, (void *) ir,
161
this->current_function->name, (void *) this->current_function);
165
/* Store the current function hierarchy being traversed. This is used
166
* by the function signature visitor to ensure that the signatures are
167
* linked with the correct functions.
169
this->current_function = ir;
171
this->validate_ir(ir, this->data);
173
return visit_continue;
177
ir_validate::visit_leave(ir_function *ir)
179
assert(talloc_parent(ir->name) == ir);
181
this->current_function = NULL;
182
return visit_continue;
186
ir_validate::visit_enter(ir_function_signature *ir)
188
if (this->current_function != ir->function()) {
189
printf("Function signature nested inside wrong function "
191
printf("%p inside %s %p instead of %s %p\n",
193
this->current_function->name, (void *) this->current_function,
194
ir->function_name(), (void *) ir->function());
198
this->validate_ir(ir, this->data);
200
return visit_continue;
204
ir_validate::visit_leave(ir_expression *ir)
206
switch (ir->operation) {
207
case ir_unop_bit_not:
208
assert(ir->operands[0]->type == ir->type);
210
case ir_unop_logic_not:
211
assert(ir->type->base_type == GLSL_TYPE_BOOL);
212
assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
221
assert(ir->type == ir->operands[0]->type);
228
assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
229
assert(ir->type == ir->operands[0]->type);
233
assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
234
assert(ir->type->base_type == GLSL_TYPE_INT);
237
assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
238
assert(ir->type->base_type == GLSL_TYPE_FLOAT);
241
assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
242
assert(ir->type->base_type == GLSL_TYPE_BOOL);
245
assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
246
assert(ir->type->base_type == GLSL_TYPE_FLOAT);
249
assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
250
assert(ir->type->base_type == GLSL_TYPE_BOOL);
253
assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
254
assert(ir->type->base_type == GLSL_TYPE_INT);
257
assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
258
assert(ir->type->base_type == GLSL_TYPE_FLOAT);
262
assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
263
assert(ir->type == glsl_type::bool_type);
274
assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
275
assert(ir->operands[0]->type == ir->type);
286
if (ir->operands[0]->type->is_scalar())
287
assert(ir->operands[1]->type == ir->type);
288
else if (ir->operands[1]->type->is_scalar())
289
assert(ir->operands[0]->type == ir->type);
290
else if (ir->operands[0]->type->is_vector() &&
291
ir->operands[1]->type->is_vector()) {
292
assert(ir->operands[0]->type == ir->operands[1]->type);
293
assert(ir->operands[0]->type == ir->type);
297
case ir_binop_greater:
298
case ir_binop_lequal:
299
case ir_binop_gequal:
300
/* GLSL < > <= >= operators take scalar floats/ints, but in the
301
* IR we may want to do them for vectors instead to support the
302
* lessEqual() and friends builtins.
304
assert(ir->type == glsl_type::bool_type);
305
assert(ir->operands[0]->type == ir->operands[1]->type);
309
case ir_binop_nequal:
310
/* GLSL == and != operate on vectors and return a bool, and the
311
* IR matches that. We may want to switch up the IR to work on
312
* vectors and return a bvec and make the operators break down
313
* to ANDing/ORing the results of the vector comparison.
315
assert(ir->type == glsl_type::bool_type);
316
assert(ir->operands[0]->type == ir->operands[1]->type);
319
case ir_binop_lshift:
320
case ir_binop_rshift:
321
case ir_binop_bit_and:
322
case ir_binop_bit_xor:
323
case ir_binop_bit_or:
324
assert(ir->operands[0]->type == ir->operands[1]->type);
325
assert(ir->type == ir->operands[0]->type);
326
assert(ir->type->base_type == GLSL_TYPE_INT ||
327
ir->type->base_type == GLSL_TYPE_UINT);
330
case ir_binop_logic_and:
331
case ir_binop_logic_xor:
332
case ir_binop_logic_or:
333
assert(ir->type == glsl_type::bool_type);
334
assert(ir->operands[0]->type == glsl_type::bool_type);
335
assert(ir->operands[1]->type == glsl_type::bool_type);
339
assert(ir->type == glsl_type::float_type);
340
assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
341
assert(ir->operands[0]->type->is_vector());
342
assert(ir->operands[0]->type == ir->operands[1]->type);
346
assert(ir->operands[0]->type == glsl_type::vec3_type);
347
assert(ir->operands[1]->type == glsl_type::vec3_type);
348
assert(ir->type == glsl_type::vec3_type);
352
return visit_continue;
356
ir_validate::visit(ir_variable *ir)
358
/* An ir_variable is the one thing that can (and will) appear multiple times
359
* in an IR tree. It is added to the hashtable so that it can be used
360
* in the ir_dereference_variable handler to ensure that a variable is
361
* declared before it is dereferenced.
364
assert(talloc_parent(ir->name) == ir);
366
hash_table_insert(ht, ir, ir);
367
return visit_continue;
371
ir_validate::visit_enter(ir_assignment *ir)
373
const ir_dereference *const lhs = ir->lhs;
374
if (lhs->type->is_scalar() || lhs->type->is_vector()) {
375
if (ir->write_mask == 0) {
376
printf("Assignment LHS is %s, but write mask is 0:\n",
377
lhs->type->is_scalar() ? "scalar" : "vector");
382
/* Mask of fields that do not exist in the destination. These should
383
* not be written by the assignment.
385
const unsigned invalid_mask = ~((1U << lhs->type->components()) - 1);
387
if ((invalid_mask & ir->write_mask) != 0) {
388
printf("Assignment write mask enables invalid components for "
389
"type %s:\n", lhs->type->name);
395
this->validate_ir(ir, this->data);
397
return visit_continue;
401
ir_validate::validate_ir(ir_instruction *ir, void *data)
403
struct hash_table *ht = (struct hash_table *) data;
405
if (hash_table_find(ht, ir)) {
406
printf("Instruction node present twice in ir tree:\n");
411
hash_table_insert(ht, ir, ir);
415
check_node_type(ir_instruction *ir, void *data)
419
if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
420
printf("Instruction node with unset type\n");
421
ir->print(); printf("\n");
423
assert(ir->type != glsl_type::error_type);
427
validate_ir_tree(exec_list *instructions)
433
foreach_iter(exec_list_iterator, iter, *instructions) {
434
ir_instruction *ir = (ir_instruction *)iter.get();
436
visit_tree(ir, check_node_type, NULL);