blob: 701bf21ea61388412789830ee0dec37525e741fe [file] [log] [blame]
Eric Anholt53cdb7e2010-06-22 12:07:21 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24/**
25 * \file ir_validate.cpp
26 *
27 * Attempts to verify that various invariants of the IR tree are true.
28 *
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.
34 */
35
36#include <inttypes.h>
37#include "ir.h"
Ian Romanick865cf2d2010-06-22 18:41:50 -070038#include "ir_hierarchical_visitor.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030039#include "program/hash_table.h"
Eric Anholtf141fa62010-07-20 17:18:57 -070040#include "glsl_types.h"
Ian Romanick865cf2d2010-06-22 18:41:50 -070041
42class ir_validate : public ir_hierarchical_visitor {
43public:
44 ir_validate()
45 {
Ian Romanickd1a1ee52010-07-06 14:49:14 -070046 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
47 hash_table_pointer_compare);
Ian Romanick865cf2d2010-06-22 18:41:50 -070048
Ian Romanickc67016d2010-07-02 13:30:23 -070049 this->current_function = NULL;
50
Ian Romanick865cf2d2010-06-22 18:41:50 -070051 this->callback = ir_validate::validate_ir;
52 this->data = ht;
53 }
54
55 ~ir_validate()
56 {
57 hash_table_dtor(this->ht);
58 }
59
60 virtual ir_visitor_status visit(ir_variable *v);
Ian Romanick8baf21b2010-07-12 13:55:32 -070061 virtual ir_visitor_status visit(ir_dereference_variable *ir);
Eric Anholt432b7872010-07-22 16:24:49 -070062 virtual ir_visitor_status visit(ir_if *ir);
Ian Romanick865cf2d2010-06-22 18:41:50 -070063
Ian Romanickc67016d2010-07-02 13:30:23 -070064 virtual ir_visitor_status visit_enter(ir_function *ir);
65 virtual ir_visitor_status visit_leave(ir_function *ir);
66 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
67
Eric Anholt5533c6e2010-07-27 09:01:30 -070068 virtual ir_visitor_status visit_leave(ir_expression *ir);
69
Ian Romanick6235c6a2010-08-04 16:27:17 -070070 virtual ir_visitor_status visit_enter(ir_assignment *ir);
71
Ian Romanick865cf2d2010-06-22 18:41:50 -070072 static void validate_ir(ir_instruction *ir, void *data);
73
Ian Romanickc67016d2010-07-02 13:30:23 -070074 ir_function *current_function;
75
Ian Romanick865cf2d2010-06-22 18:41:50 -070076 struct hash_table *ht;
77};
78
Ian Romanick8baf21b2010-07-12 13:55:32 -070079
80ir_visitor_status
81ir_validate::visit(ir_dereference_variable *ir)
82{
83 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
84 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
85 ir, ir->var);
86 abort();
87 }
88
89 if (hash_table_find(ht, ir->var) == NULL) {
90 printf("ir_dereference_variable @ %p specifies undeclared variable "
91 "`%s' @ %p\n",
92 ir, ir->var->name, ir->var);
93 abort();
94 }
95
Ian Romanick506880b2010-07-12 15:46:16 -070096 this->validate_ir(ir, this->data);
97
Ian Romanick8baf21b2010-07-12 13:55:32 -070098 return visit_continue;
99}
100
Eric Anholt432b7872010-07-22 16:24:49 -0700101ir_visitor_status
102ir_validate::visit(ir_if *ir)
103{
104 if (ir->condition->type != glsl_type::bool_type) {
105 printf("ir_if condition %s type instead of bool.\n",
106 ir->condition->type->name);
107 ir->print();
108 printf("\n");
109 abort();
110 }
Eric Anholt6a1401e2010-07-27 00:18:33 -0700111
112 return visit_continue;
Eric Anholt432b7872010-07-22 16:24:49 -0700113}
114
Ian Romanick8baf21b2010-07-12 13:55:32 -0700115
Ian Romanick865cf2d2010-06-22 18:41:50 -0700116ir_visitor_status
Ian Romanickc67016d2010-07-02 13:30:23 -0700117ir_validate::visit_enter(ir_function *ir)
118{
119 /* Function definitions cannot be nested.
120 */
121 if (this->current_function != NULL) {
122 printf("Function definition nested inside another function "
123 "definition:\n");
124 printf("%s %p inside %s %p\n",
125 ir->name, ir,
126 this->current_function->name, this->current_function);
127 abort();
128 }
129
130 /* Store the current function hierarchy being traversed. This is used
131 * by the function signature visitor to ensure that the signatures are
132 * linked with the correct functions.
133 */
134 this->current_function = ir;
135
136 this->validate_ir(ir, this->data);
137
138 return visit_continue;
139}
140
141ir_visitor_status
142ir_validate::visit_leave(ir_function *ir)
143{
Eric Anholtee7666b2010-08-02 12:08:52 -0700144 assert(talloc_parent(ir->name) == ir);
Ian Romanickc67016d2010-07-02 13:30:23 -0700145
146 this->current_function = NULL;
147 return visit_continue;
148}
149
150ir_visitor_status
151ir_validate::visit_enter(ir_function_signature *ir)
152{
153 if (this->current_function != ir->function()) {
154 printf("Function signature nested inside wrong function "
155 "definition:\n");
156 printf("%p inside %s %p instead of %s %p\n",
157 ir,
158 this->current_function->name, this->current_function,
159 ir->function_name(), ir->function());
160 abort();
161 }
162
163 this->validate_ir(ir, this->data);
164
165 return visit_continue;
166}
167
168ir_visitor_status
Eric Anholt5533c6e2010-07-27 09:01:30 -0700169ir_validate::visit_leave(ir_expression *ir)
170{
171 switch (ir->operation) {
172 case ir_unop_bit_not:
173 assert(ir->operands[0]->type == ir->type);
174 break;
175 case ir_unop_logic_not:
Eric Anholte75dbf62010-08-02 12:06:34 -0700176 assert(ir->type->base_type == GLSL_TYPE_BOOL);
177 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholt5533c6e2010-07-27 09:01:30 -0700178 break;
179
180 case ir_unop_neg:
181 case ir_unop_abs:
182 case ir_unop_sign:
183 case ir_unop_rcp:
184 case ir_unop_rsq:
185 case ir_unop_sqrt:
186 case ir_unop_exp:
187 case ir_unop_log:
188 case ir_unop_exp2:
189 case ir_unop_log2:
190 assert(ir->type == ir->operands[0]->type);
191 break;
192
193 case ir_unop_f2i:
194 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
195 assert(ir->type->base_type == GLSL_TYPE_INT);
196 break;
197 case ir_unop_i2f:
198 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
199 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
200 break;
201 case ir_unop_f2b:
202 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
203 assert(ir->type->base_type == GLSL_TYPE_BOOL);
204 break;
205 case ir_unop_b2f:
206 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
207 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
208 break;
209 case ir_unop_i2b:
210 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
211 assert(ir->type->base_type == GLSL_TYPE_BOOL);
212 break;
213 case ir_unop_b2i:
214 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
215 assert(ir->type->base_type == GLSL_TYPE_INT);
216 break;
217 case ir_unop_u2f:
218 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
219 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
220 break;
221
222 case ir_unop_trunc:
223 case ir_unop_ceil:
224 case ir_unop_floor:
225 case ir_unop_fract:
226 case ir_unop_sin:
227 case ir_unop_cos:
228 case ir_unop_dFdx:
229 case ir_unop_dFdy:
230 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
231 assert(ir->operands[0]->type == ir->type);
232 break;
233
234 case ir_binop_add:
235 case ir_binop_sub:
236 case ir_binop_mul:
237 case ir_binop_div:
238 case ir_binop_mod:
239 case ir_binop_min:
240 case ir_binop_max:
241 case ir_binop_pow:
242 if (ir->operands[0]->type->is_scalar())
243 assert(ir->operands[1]->type == ir->type);
244 else if (ir->operands[1]->type->is_scalar())
245 assert(ir->operands[0]->type == ir->type);
246 else if (ir->operands[0]->type->is_vector() &&
247 ir->operands[1]->type->is_vector()) {
248 assert(ir->operands[0]->type == ir->operands[1]->type);
249 assert(ir->operands[0]->type == ir->type);
250 }
251 break;
252 case ir_binop_less:
253 case ir_binop_greater:
254 case ir_binop_lequal:
255 case ir_binop_gequal:
256 /* GLSL < > <= >= operators take scalar floats/ints, but in the
257 * IR we may want to do them for vectors instead to support the
258 * lessEqual() and friends builtins.
259 */
260 assert(ir->type == glsl_type::bool_type);
261 assert(ir->operands[0]->type == ir->operands[1]->type);
262 break;
263
264 case ir_binop_equal:
265 case ir_binop_nequal:
266 /* GLSL == and != operate on vectors and return a bool, and the
267 * IR matches that. We may want to switch up the IR to work on
268 * vectors and return a bvec and make the operators break down
269 * to ANDing/ORing the results of the vector comparison.
270 */
271 assert(ir->type == glsl_type::bool_type);
272 assert(ir->operands[0]->type == ir->operands[1]->type);
273 break;
274
275 case ir_binop_lshift:
276 case ir_binop_rshift:
277 case ir_binop_bit_and:
278 case ir_binop_bit_xor:
279 case ir_binop_bit_or:
280 assert(ir->operands[0]->type == ir->operands[1]->type);
281 assert(ir->type == ir->operands[0]->type);
282 assert(ir->type->base_type == GLSL_TYPE_INT ||
283 ir->type->base_type == GLSL_TYPE_UINT);
284 break;
285
286 case ir_binop_logic_and:
287 case ir_binop_logic_xor:
288 case ir_binop_logic_or:
289 assert(ir->type == glsl_type::bool_type);
290 assert(ir->operands[0]->type == glsl_type::bool_type);
291 assert(ir->operands[1]->type == glsl_type::bool_type);
292 break;
293
294 case ir_binop_dot:
295 assert(ir->type == glsl_type::float_type);
296 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
297 assert(ir->operands[0]->type == ir->operands[1]->type);
298 break;
299
300 case ir_binop_cross:
301 assert(ir->operands[0]->type == glsl_type::vec3_type);
302 assert(ir->operands[1]->type == glsl_type::vec3_type);
303 assert(ir->type == glsl_type::vec3_type);
304 break;
305 }
306
307 return visit_continue;
308}
309
310ir_visitor_status
Ian Romanick865cf2d2010-06-22 18:41:50 -0700311ir_validate::visit(ir_variable *ir)
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700312{
Ian Romanick865cf2d2010-06-22 18:41:50 -0700313 /* An ir_variable is the one thing that can (and will) appear multiple times
Ian Romanick8baf21b2010-07-12 13:55:32 -0700314 * in an IR tree. It is added to the hashtable so that it can be used
315 * in the ir_dereference_variable handler to ensure that a variable is
316 * declared before it is dereferenced.
Ian Romanick865cf2d2010-06-22 18:41:50 -0700317 */
Eric Anholtc22dee72010-08-03 11:43:25 -0700318 if (ir->name)
319 assert(talloc_parent(ir->name) == ir);
Eric Anholtee7666b2010-08-02 12:08:52 -0700320
Ian Romanick8baf21b2010-07-12 13:55:32 -0700321 hash_table_insert(ht, ir, ir);
Ian Romanick865cf2d2010-06-22 18:41:50 -0700322 return visit_continue;
323}
324
Ian Romanick6235c6a2010-08-04 16:27:17 -0700325ir_visitor_status
326ir_validate::visit_enter(ir_assignment *ir)
327{
328 const ir_dereference *const lhs = ir->lhs;
329 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
330 if (ir->write_mask == 0) {
331 printf("Assignment LHS is %s, but write mask is 0:\n",
332 lhs->type->is_scalar() ? "scalar" : "vector");
333 ir->print();
334 abort();
335 }
336
337 /* Mask of fields that do not exist in the destination. These should
338 * not be written by the assignment.
339 */
340 const unsigned invalid_mask = ~((1U << lhs->type->components()) - 1);
341
342 if ((invalid_mask & ir->write_mask) != 0) {
343 printf("Assignment write mask enables invalid components for "
344 "type %s:\n", lhs->type->name);
345 ir->print();
346 abort();
347 }
348 }
349
350 this->validate_ir(ir, this->data);
351
352 return visit_continue;
353}
354
Ian Romanick865cf2d2010-06-22 18:41:50 -0700355void
356ir_validate::validate_ir(ir_instruction *ir, void *data)
357{
358 struct hash_table *ht = (struct hash_table *) data;
359
360 if (hash_table_find(ht, ir)) {
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700361 printf("Instruction node present twice in ir tree:\n");
362 ir->print();
363 printf("\n");
364 abort();
365 }
Ian Romanick865cf2d2010-06-22 18:41:50 -0700366 hash_table_insert(ht, ir, ir);
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700367}
368
369void
Eric Anholtd16044a2010-07-19 09:05:42 -0700370check_node_type(ir_instruction *ir, void *data)
371{
Ian Romanick7ffe4052010-08-02 11:46:22 -0700372 (void) data;
373
Eric Anholtd16044a2010-07-19 09:05:42 -0700374 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
375 printf("Instruction node with unset type\n");
376 ir->print(); printf("\n");
377 }
Eric Anholtf141fa62010-07-20 17:18:57 -0700378 assert(ir->type != glsl_type::error_type);
Eric Anholtd16044a2010-07-19 09:05:42 -0700379}
380
381void
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700382validate_ir_tree(exec_list *instructions)
383{
384 ir_validate v;
385
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700386 v.run(instructions);
Eric Anholtd16044a2010-07-19 09:05:42 -0700387
388 foreach_iter(exec_list_iterator, iter, *instructions) {
389 ir_instruction *ir = (ir_instruction *)iter.get();
390
391 visit_tree(ir, check_node_type, NULL);
392 }
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700393}