mesa: Compiler delint
[AROS.git] / workbench / libs / mesa / src / glsl / ir_validate.cpp
blob58a0783506ca10a32f063003b107ac0c54514c15
1 /*
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
13 * Software.
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.
24 /**
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.
36 #include <inttypes.h>
37 #include "ir.h"
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 {
43 public:
44 ir_validate()
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;
52 this->data = ht;
55 ~ir_validate()
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);
73 virtual ir_visitor_status visit_enter(ir_call *ir);
75 static void validate_ir(ir_instruction *ir, void *data);
77 ir_function *current_function;
79 struct hash_table *ht;
83 ir_visitor_status
84 ir_validate::visit(ir_dereference_variable *ir)
86 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
87 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
88 (void *) ir, (void *) ir->var);
89 abort();
92 if (hash_table_find(ht, ir->var) == NULL) {
93 printf("ir_dereference_variable @ %p specifies undeclared variable "
94 "`%s' @ %p\n",
95 (void *) ir, ir->var->name, (void *) ir->var);
96 abort();
99 this->validate_ir(ir, this->data);
101 return visit_continue;
104 ir_visitor_status
105 ir_validate::visit(ir_if *ir)
107 if (ir->condition->type != glsl_type::bool_type) {
108 printf("ir_if condition %s type instead of bool.\n",
109 ir->condition->type->name);
110 ir->print();
111 printf("\n");
112 abort();
115 return visit_continue;
119 ir_visitor_status
120 ir_validate::visit_leave(ir_loop *ir)
122 if (ir->counter != NULL) {
123 if ((ir->from == NULL) || (ir->from == NULL) || (ir->increment == NULL)) {
124 printf("ir_loop has invalid loop controls:\n"
125 " counter: %p\n"
126 " from: %p\n"
127 " to: %p\n"
128 " increment: %p\n",
129 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
130 (void *) ir->increment);
131 abort();
134 if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
135 printf("ir_loop has invalid comparitor %d\n", ir->cmp);
136 abort();
138 } else {
139 if ((ir->from != NULL) || (ir->from != NULL) || (ir->increment != NULL)) {
140 printf("ir_loop has invalid loop controls:\n"
141 " counter: %p\n"
142 " from: %p\n"
143 " to: %p\n"
144 " increment: %p\n",
145 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
146 (void *) ir->increment);
147 abort();
151 return visit_continue;
155 ir_visitor_status
156 ir_validate::visit_enter(ir_function *ir)
158 /* Function definitions cannot be nested.
160 if (this->current_function != NULL) {
161 printf("Function definition nested inside another function "
162 "definition:\n");
163 printf("%s %p inside %s %p\n",
164 ir->name, (void *) ir,
165 this->current_function->name, (void *) this->current_function);
166 abort();
169 /* Store the current function hierarchy being traversed. This is used
170 * by the function signature visitor to ensure that the signatures are
171 * linked with the correct functions.
173 this->current_function = ir;
175 this->validate_ir(ir, this->data);
177 /* Verify that all of the things stored in the list of signatures are,
178 * in fact, function signatures.
180 foreach_list(node, &ir->signatures) {
181 ir_instruction *sig = (ir_instruction *) node;
183 if (sig->ir_type != ir_type_function_signature) {
184 printf("Non-signature in signature list of function `%s'\n",
185 ir->name);
186 abort();
190 return visit_continue;
193 ir_visitor_status
194 ir_validate::visit_leave(ir_function *ir)
196 assert(ralloc_parent(ir->name) == ir);
198 this->current_function = NULL;
199 return visit_continue;
202 ir_visitor_status
203 ir_validate::visit_enter(ir_function_signature *ir)
205 if (this->current_function != ir->function()) {
206 printf("Function signature nested inside wrong function "
207 "definition:\n");
208 printf("%p inside %s %p instead of %s %p\n",
209 (void *) ir,
210 this->current_function->name, (void *) this->current_function,
211 ir->function_name(), (void *) ir->function());
212 abort();
215 if (ir->return_type == NULL) {
216 printf("Function signature %p for function %s has NULL return type.\n",
217 (void *) ir, ir->function_name());
218 abort();
221 this->validate_ir(ir, this->data);
223 return visit_continue;
226 ir_visitor_status
227 ir_validate::visit_leave(ir_expression *ir)
229 switch (ir->operation) {
230 case ir_unop_bit_not:
231 assert(ir->operands[0]->type == ir->type);
232 break;
233 case ir_unop_logic_not:
234 assert(ir->type->base_type == GLSL_TYPE_BOOL);
235 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
236 break;
238 case ir_unop_neg:
239 case ir_unop_abs:
240 case ir_unop_sign:
241 case ir_unop_rcp:
242 case ir_unop_rsq:
243 case ir_unop_sqrt:
244 assert(ir->type == ir->operands[0]->type);
245 break;
247 case ir_unop_exp:
248 case ir_unop_log:
249 case ir_unop_exp2:
250 case ir_unop_log2:
251 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
252 assert(ir->type == ir->operands[0]->type);
253 break;
255 case ir_unop_f2i:
256 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
257 assert(ir->type->is_integer());
258 break;
259 case ir_unop_i2f:
260 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
261 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
262 break;
263 case ir_unop_f2b:
264 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
265 assert(ir->type->base_type == GLSL_TYPE_BOOL);
266 break;
267 case ir_unop_b2f:
268 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
269 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
270 break;
271 case ir_unop_i2b:
272 assert(ir->operands[0]->type->is_integer());
273 assert(ir->type->base_type == GLSL_TYPE_BOOL);
274 break;
275 case ir_unop_b2i:
276 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
277 assert(ir->type->is_integer());
278 break;
279 case ir_unop_u2f:
280 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
281 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
282 break;
284 case ir_unop_any:
285 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
286 assert(ir->type == glsl_type::bool_type);
287 break;
289 case ir_unop_trunc:
290 case ir_unop_round_even:
291 case ir_unop_ceil:
292 case ir_unop_floor:
293 case ir_unop_fract:
294 case ir_unop_sin:
295 case ir_unop_cos:
296 case ir_unop_sin_reduced:
297 case ir_unop_cos_reduced:
298 case ir_unop_dFdx:
299 case ir_unop_dFdy:
300 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
301 assert(ir->operands[0]->type == ir->type);
302 break;
304 case ir_unop_noise:
305 /* XXX what can we assert here? */
306 break;
308 case ir_binop_add:
309 case ir_binop_sub:
310 case ir_binop_mul:
311 case ir_binop_div:
312 case ir_binop_mod:
313 case ir_binop_min:
314 case ir_binop_max:
315 case ir_binop_pow:
316 if (ir->operands[0]->type->is_scalar())
317 assert(ir->operands[1]->type == ir->type);
318 else if (ir->operands[1]->type->is_scalar())
319 assert(ir->operands[0]->type == ir->type);
320 else if (ir->operands[0]->type->is_vector() &&
321 ir->operands[1]->type->is_vector()) {
322 assert(ir->operands[0]->type == ir->operands[1]->type);
323 assert(ir->operands[0]->type == ir->type);
325 break;
327 case ir_binop_less:
328 case ir_binop_greater:
329 case ir_binop_lequal:
330 case ir_binop_gequal:
331 case ir_binop_equal:
332 case ir_binop_nequal:
333 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
334 * ==, and != operators. The IR operators perform a component-wise
335 * comparison on scalar or vector types and return a boolean scalar or
336 * vector type of the same size.
338 assert(ir->type->base_type == GLSL_TYPE_BOOL);
339 assert(ir->operands[0]->type == ir->operands[1]->type);
340 assert(ir->operands[0]->type->is_vector()
341 || ir->operands[0]->type->is_scalar());
342 assert(ir->operands[0]->type->vector_elements
343 == ir->type->vector_elements);
344 break;
346 case ir_binop_all_equal:
347 case ir_binop_any_nequal:
348 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
349 * return a scalar boolean. The IR matches that.
351 assert(ir->type == glsl_type::bool_type);
352 assert(ir->operands[0]->type == ir->operands[1]->type);
353 break;
355 case ir_binop_lshift:
356 case ir_binop_rshift:
357 assert(ir->operands[0]->type->is_integer() &&
358 ir->operands[1]->type->is_integer());
359 if (ir->operands[0]->type->is_scalar()) {
360 assert(ir->operands[1]->type->is_scalar());
362 if (ir->operands[0]->type->is_vector() &&
363 ir->operands[1]->type->is_vector()) {
364 assert(ir->operands[0]->type->components() ==
365 ir->operands[1]->type->components());
367 assert(ir->type == ir->operands[0]->type);
368 break;
370 case ir_binop_bit_and:
371 case ir_binop_bit_xor:
372 case ir_binop_bit_or:
373 assert(ir->operands[0]->type->base_type ==
374 ir->operands[1]->type->base_type);
375 assert(ir->type->is_integer());
376 if (ir->operands[0]->type->is_vector() &&
377 ir->operands[1]->type->is_vector()) {
378 assert(ir->operands[0]->type->vector_elements ==
379 ir->operands[1]->type->vector_elements);
381 break;
383 case ir_binop_logic_and:
384 case ir_binop_logic_xor:
385 case ir_binop_logic_or:
386 assert(ir->type == glsl_type::bool_type);
387 assert(ir->operands[0]->type == glsl_type::bool_type);
388 assert(ir->operands[1]->type == glsl_type::bool_type);
389 break;
391 case ir_binop_dot:
392 assert(ir->type == glsl_type::float_type);
393 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
394 assert(ir->operands[0]->type->is_vector());
395 assert(ir->operands[0]->type == ir->operands[1]->type);
396 break;
398 case ir_quadop_vector:
399 /* The vector operator collects some number of scalars and generates a
400 * vector from them.
402 * - All of the operands must be scalar.
403 * - Number of operands must matche the size of the resulting vector.
404 * - Base type of the operands must match the base type of the result.
406 assert(ir->type->is_vector());
407 switch (ir->type->vector_elements) {
408 case 2:
409 assert(ir->operands[0]->type->is_scalar());
410 assert(ir->operands[0]->type->base_type == ir->type->base_type);
411 assert(ir->operands[1]->type->is_scalar());
412 assert(ir->operands[1]->type->base_type == ir->type->base_type);
413 assert(ir->operands[2] == NULL);
414 assert(ir->operands[3] == NULL);
415 break;
416 case 3:
417 assert(ir->operands[0]->type->is_scalar());
418 assert(ir->operands[0]->type->base_type == ir->type->base_type);
419 assert(ir->operands[1]->type->is_scalar());
420 assert(ir->operands[1]->type->base_type == ir->type->base_type);
421 assert(ir->operands[2]->type->is_scalar());
422 assert(ir->operands[2]->type->base_type == ir->type->base_type);
423 assert(ir->operands[3] == NULL);
424 break;
425 case 4:
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]->type->is_scalar());
433 assert(ir->operands[3]->type->base_type == ir->type->base_type);
434 break;
435 default:
436 /* The is_vector assertion above should prevent execution from ever
437 * getting here.
439 assert(!"Should not get here.");
440 break;
444 return visit_continue;
447 ir_visitor_status
448 ir_validate::visit_leave(ir_swizzle *ir)
450 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
452 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
453 if (chans[i] >= ir->val->type->vector_elements) {
454 printf("ir_swizzle @ %p specifies a channel not present "
455 "in the value.\n", (void *) ir);
456 ir->print();
457 abort();
461 return visit_continue;
464 ir_visitor_status
465 ir_validate::visit(ir_variable *ir)
467 /* An ir_variable is the one thing that can (and will) appear multiple times
468 * in an IR tree. It is added to the hashtable so that it can be used
469 * in the ir_dereference_variable handler to ensure that a variable is
470 * declared before it is dereferenced.
472 if (ir->name)
473 assert(ralloc_parent(ir->name) == ir);
475 hash_table_insert(ht, ir, ir);
478 /* If a variable is an array, verify that the maximum array index is in
479 * bounds. There was once an error in AST-to-HIR conversion that set this
480 * to be out of bounds.
482 if (ir->type->array_size() > 0) {
483 if (ir->max_array_access >= ir->type->length) {
484 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
485 ir->max_array_access, ir->type->length - 1);
486 ir->print();
487 abort();
491 return visit_continue;
494 ir_visitor_status
495 ir_validate::visit_enter(ir_assignment *ir)
497 const ir_dereference *const lhs = ir->lhs;
498 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
499 if (ir->write_mask == 0) {
500 printf("Assignment LHS is %s, but write mask is 0:\n",
501 lhs->type->is_scalar() ? "scalar" : "vector");
502 ir->print();
503 abort();
506 int lhs_components = 0;
507 for (int i = 0; i < 4; i++) {
508 if (ir->write_mask & (1 << i))
509 lhs_components++;
512 if (lhs_components != ir->rhs->type->vector_elements) {
513 printf("Assignment count of LHS write mask channels enabled not\n"
514 "matching RHS vector size (%d LHS, %d RHS).\n",
515 lhs_components, ir->rhs->type->vector_elements);
516 ir->print();
517 abort();
521 this->validate_ir(ir, this->data);
523 return visit_continue;
526 ir_visitor_status
527 ir_validate::visit_enter(ir_call *ir)
529 ir_function_signature *const callee = ir->get_callee();
531 if (callee->ir_type != ir_type_function_signature) {
532 printf("IR called by ir_call is not ir_function_signature!\n");
533 abort();
536 return visit_continue;
539 void
540 ir_validate::validate_ir(ir_instruction *ir, void *data)
542 struct hash_table *ht = (struct hash_table *) data;
544 if (hash_table_find(ht, ir)) {
545 printf("Instruction node present twice in ir tree:\n");
546 ir->print();
547 printf("\n");
548 abort();
550 hash_table_insert(ht, ir, ir);
553 void
554 check_node_type(ir_instruction *ir, void *data)
556 (void) data;
558 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
559 printf("Instruction node with unset type\n");
560 ir->print(); printf("\n");
562 assert(ir->type != glsl_type::error_type);
565 void
566 validate_ir_tree(exec_list *instructions)
568 ir_validate v;
570 v.run(instructions);
572 foreach_iter(exec_list_iterator, iter, *instructions) {
573 ir_instruction *ir = (ir_instruction *)iter.get();
575 visit_tree(ir, check_node_type, NULL);