svgadump: Dump the new depth format names.
[mesa.git] / src / glsl / ir_validate.cpp
1 /*
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"
38 #include "ir_hierarchical_visitor.h"
39 #include "program/hash_table.h"
40 #include "glsl_types.h"
41
42 class ir_validate : public ir_hierarchical_visitor {
43 public:
44 ir_validate()
45 {
46 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
47 hash_table_pointer_compare);
48
49 this->current_function = NULL;
50
51 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);
61 virtual ir_visitor_status visit(ir_dereference_variable *ir);
62
63 virtual ir_visitor_status visit_enter(ir_if *ir);
64
65 virtual ir_visitor_status visit_leave(ir_loop *ir);
66 virtual ir_visitor_status visit_enter(ir_function *ir);
67 virtual ir_visitor_status visit_leave(ir_function *ir);
68 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
69
70 virtual ir_visitor_status visit_leave(ir_expression *ir);
71 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
72
73 virtual ir_visitor_status visit_enter(ir_assignment *ir);
74 virtual ir_visitor_status visit_enter(ir_call *ir);
75
76 static void validate_ir(ir_instruction *ir, void *data);
77
78 ir_function *current_function;
79
80 struct hash_table *ht;
81 };
82
83
84 ir_visitor_status
85 ir_validate::visit(ir_dereference_variable *ir)
86 {
87 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
88 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
89 (void *) ir, (void *) ir->var);
90 abort();
91 }
92
93 if (hash_table_find(ht, ir->var) == NULL) {
94 printf("ir_dereference_variable @ %p specifies undeclared variable "
95 "`%s' @ %p\n",
96 (void *) ir, ir->var->name, (void *) ir->var);
97 abort();
98 }
99
100 this->validate_ir(ir, this->data);
101
102 return visit_continue;
103 }
104
105 ir_visitor_status
106 ir_validate::visit_enter(ir_if *ir)
107 {
108 if (ir->condition->type != glsl_type::bool_type) {
109 printf("ir_if condition %s type instead of bool.\n",
110 ir->condition->type->name);
111 ir->print();
112 printf("\n");
113 abort();
114 }
115
116 return visit_continue;
117 }
118
119
120 ir_visitor_status
121 ir_validate::visit_leave(ir_loop *ir)
122 {
123 if (ir->counter != NULL) {
124 if ((ir->from == NULL) || (ir->from == NULL) || (ir->increment == NULL)) {
125 printf("ir_loop has invalid loop controls:\n"
126 " counter: %p\n"
127 " from: %p\n"
128 " to: %p\n"
129 " increment: %p\n",
130 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
131 (void *) ir->increment);
132 abort();
133 }
134
135 if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
136 printf("ir_loop has invalid comparitor %d\n", ir->cmp);
137 abort();
138 }
139 } else {
140 if ((ir->from != NULL) || (ir->from != NULL) || (ir->increment != NULL)) {
141 printf("ir_loop has invalid loop controls:\n"
142 " counter: %p\n"
143 " from: %p\n"
144 " to: %p\n"
145 " increment: %p\n",
146 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
147 (void *) ir->increment);
148 abort();
149 }
150 }
151
152 return visit_continue;
153 }
154
155
156 ir_visitor_status
157 ir_validate::visit_enter(ir_function *ir)
158 {
159 /* Function definitions cannot be nested.
160 */
161 if (this->current_function != NULL) {
162 printf("Function definition nested inside another function "
163 "definition:\n");
164 printf("%s %p inside %s %p\n",
165 ir->name, (void *) ir,
166 this->current_function->name, (void *) this->current_function);
167 abort();
168 }
169
170 /* Store the current function hierarchy being traversed. This is used
171 * by the function signature visitor to ensure that the signatures are
172 * linked with the correct functions.
173 */
174 this->current_function = ir;
175
176 this->validate_ir(ir, this->data);
177
178 /* Verify that all of the things stored in the list of signatures are,
179 * in fact, function signatures.
180 */
181 foreach_list(node, &ir->signatures) {
182 ir_instruction *sig = (ir_instruction *) node;
183
184 if (sig->ir_type != ir_type_function_signature) {
185 printf("Non-signature in signature list of function `%s'\n",
186 ir->name);
187 abort();
188 }
189 }
190
191 return visit_continue;
192 }
193
194 ir_visitor_status
195 ir_validate::visit_leave(ir_function *ir)
196 {
197 assert(ralloc_parent(ir->name) == ir);
198
199 this->current_function = NULL;
200 return visit_continue;
201 }
202
203 ir_visitor_status
204 ir_validate::visit_enter(ir_function_signature *ir)
205 {
206 if (this->current_function != ir->function()) {
207 printf("Function signature nested inside wrong function "
208 "definition:\n");
209 printf("%p inside %s %p instead of %s %p\n",
210 (void *) ir,
211 this->current_function->name, (void *) this->current_function,
212 ir->function_name(), (void *) ir->function());
213 abort();
214 }
215
216 if (ir->return_type == NULL) {
217 printf("Function signature %p for function %s has NULL return type.\n",
218 (void *) ir, ir->function_name());
219 abort();
220 }
221
222 this->validate_ir(ir, this->data);
223
224 return visit_continue;
225 }
226
227 ir_visitor_status
228 ir_validate::visit_leave(ir_expression *ir)
229 {
230 switch (ir->operation) {
231 case ir_unop_bit_not:
232 assert(ir->operands[0]->type == ir->type);
233 break;
234 case ir_unop_logic_not:
235 assert(ir->type->base_type == GLSL_TYPE_BOOL);
236 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
237 break;
238
239 case ir_unop_neg:
240 case ir_unop_abs:
241 case ir_unop_sign:
242 case ir_unop_rcp:
243 case ir_unop_rsq:
244 case ir_unop_sqrt:
245 assert(ir->type == ir->operands[0]->type);
246 break;
247
248 case ir_unop_exp:
249 case ir_unop_log:
250 case ir_unop_exp2:
251 case ir_unop_log2:
252 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
253 assert(ir->type == ir->operands[0]->type);
254 break;
255
256 case ir_unop_f2i:
257 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
258 assert(ir->type->base_type == GLSL_TYPE_INT);
259 break;
260 case ir_unop_i2f:
261 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
262 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
263 break;
264 case ir_unop_f2b:
265 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
266 assert(ir->type->base_type == GLSL_TYPE_BOOL);
267 break;
268 case ir_unop_b2f:
269 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
270 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
271 break;
272 case ir_unop_i2b:
273 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
274 assert(ir->type->base_type == GLSL_TYPE_BOOL);
275 break;
276 case ir_unop_b2i:
277 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
278 assert(ir->type->base_type == GLSL_TYPE_INT);
279 break;
280 case ir_unop_u2f:
281 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
282 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
283 break;
284 case ir_unop_i2u:
285 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
286 assert(ir->type->base_type == GLSL_TYPE_UINT);
287 break;
288 case ir_unop_u2i:
289 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
290 assert(ir->type->base_type == GLSL_TYPE_INT);
291 break;
292
293 case ir_unop_any:
294 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
295 assert(ir->type == glsl_type::bool_type);
296 break;
297
298 case ir_unop_trunc:
299 case ir_unop_round_even:
300 case ir_unop_ceil:
301 case ir_unop_floor:
302 case ir_unop_fract:
303 case ir_unop_sin:
304 case ir_unop_cos:
305 case ir_unop_sin_reduced:
306 case ir_unop_cos_reduced:
307 case ir_unop_dFdx:
308 case ir_unop_dFdy:
309 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
310 assert(ir->operands[0]->type == ir->type);
311 break;
312
313 case ir_unop_noise:
314 /* XXX what can we assert here? */
315 break;
316
317 case ir_binop_add:
318 case ir_binop_sub:
319 case ir_binop_mul:
320 case ir_binop_div:
321 case ir_binop_mod:
322 case ir_binop_min:
323 case ir_binop_max:
324 case ir_binop_pow:
325 if (ir->operands[0]->type->is_scalar())
326 assert(ir->operands[1]->type == ir->type);
327 else if (ir->operands[1]->type->is_scalar())
328 assert(ir->operands[0]->type == ir->type);
329 else if (ir->operands[0]->type->is_vector() &&
330 ir->operands[1]->type->is_vector()) {
331 assert(ir->operands[0]->type == ir->operands[1]->type);
332 assert(ir->operands[0]->type == ir->type);
333 }
334 break;
335
336 case ir_binop_less:
337 case ir_binop_greater:
338 case ir_binop_lequal:
339 case ir_binop_gequal:
340 case ir_binop_equal:
341 case ir_binop_nequal:
342 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
343 * ==, and != operators. The IR operators perform a component-wise
344 * comparison on scalar or vector types and return a boolean scalar or
345 * vector type of the same size.
346 */
347 assert(ir->type->base_type == GLSL_TYPE_BOOL);
348 assert(ir->operands[0]->type == ir->operands[1]->type);
349 assert(ir->operands[0]->type->is_vector()
350 || ir->operands[0]->type->is_scalar());
351 assert(ir->operands[0]->type->vector_elements
352 == ir->type->vector_elements);
353 break;
354
355 case ir_binop_all_equal:
356 case ir_binop_any_nequal:
357 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
358 * return a scalar boolean. The IR matches that.
359 */
360 assert(ir->type == glsl_type::bool_type);
361 assert(ir->operands[0]->type == ir->operands[1]->type);
362 break;
363
364 case ir_binop_lshift:
365 case ir_binop_rshift:
366 assert(ir->operands[0]->type->is_integer() &&
367 ir->operands[1]->type->is_integer());
368 if (ir->operands[0]->type->is_scalar()) {
369 assert(ir->operands[1]->type->is_scalar());
370 }
371 if (ir->operands[0]->type->is_vector() &&
372 ir->operands[1]->type->is_vector()) {
373 assert(ir->operands[0]->type->components() ==
374 ir->operands[1]->type->components());
375 }
376 assert(ir->type == ir->operands[0]->type);
377 break;
378
379 case ir_binop_bit_and:
380 case ir_binop_bit_xor:
381 case ir_binop_bit_or:
382 assert(ir->operands[0]->type->base_type ==
383 ir->operands[1]->type->base_type);
384 assert(ir->type->is_integer());
385 if (ir->operands[0]->type->is_vector() &&
386 ir->operands[1]->type->is_vector()) {
387 assert(ir->operands[0]->type->vector_elements ==
388 ir->operands[1]->type->vector_elements);
389 }
390 break;
391
392 case ir_binop_logic_and:
393 case ir_binop_logic_xor:
394 case ir_binop_logic_or:
395 assert(ir->type == glsl_type::bool_type);
396 assert(ir->operands[0]->type == glsl_type::bool_type);
397 assert(ir->operands[1]->type == glsl_type::bool_type);
398 break;
399
400 case ir_binop_dot:
401 assert(ir->type == glsl_type::float_type);
402 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
403 assert(ir->operands[0]->type->is_vector());
404 assert(ir->operands[0]->type == ir->operands[1]->type);
405 break;
406
407 case ir_quadop_vector:
408 /* The vector operator collects some number of scalars and generates a
409 * vector from them.
410 *
411 * - All of the operands must be scalar.
412 * - Number of operands must matche the size of the resulting vector.
413 * - Base type of the operands must match the base type of the result.
414 */
415 assert(ir->type->is_vector());
416 switch (ir->type->vector_elements) {
417 case 2:
418 assert(ir->operands[0]->type->is_scalar());
419 assert(ir->operands[0]->type->base_type == ir->type->base_type);
420 assert(ir->operands[1]->type->is_scalar());
421 assert(ir->operands[1]->type->base_type == ir->type->base_type);
422 assert(ir->operands[2] == NULL);
423 assert(ir->operands[3] == NULL);
424 break;
425 case 3:
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] == NULL);
433 break;
434 case 4:
435 assert(ir->operands[0]->type->is_scalar());
436 assert(ir->operands[0]->type->base_type == ir->type->base_type);
437 assert(ir->operands[1]->type->is_scalar());
438 assert(ir->operands[1]->type->base_type == ir->type->base_type);
439 assert(ir->operands[2]->type->is_scalar());
440 assert(ir->operands[2]->type->base_type == ir->type->base_type);
441 assert(ir->operands[3]->type->is_scalar());
442 assert(ir->operands[3]->type->base_type == ir->type->base_type);
443 break;
444 default:
445 /* The is_vector assertion above should prevent execution from ever
446 * getting here.
447 */
448 assert(!"Should not get here.");
449 break;
450 }
451 }
452
453 return visit_continue;
454 }
455
456 ir_visitor_status
457 ir_validate::visit_leave(ir_swizzle *ir)
458 {
459 int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
460
461 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
462 if (chans[i] >= ir->val->type->vector_elements) {
463 printf("ir_swizzle @ %p specifies a channel not present "
464 "in the value.\n", (void *) ir);
465 ir->print();
466 abort();
467 }
468 }
469
470 return visit_continue;
471 }
472
473 ir_visitor_status
474 ir_validate::visit(ir_variable *ir)
475 {
476 /* An ir_variable is the one thing that can (and will) appear multiple times
477 * in an IR tree. It is added to the hashtable so that it can be used
478 * in the ir_dereference_variable handler to ensure that a variable is
479 * declared before it is dereferenced.
480 */
481 if (ir->name)
482 assert(ralloc_parent(ir->name) == ir);
483
484 hash_table_insert(ht, ir, ir);
485
486
487 /* If a variable is an array, verify that the maximum array index is in
488 * bounds. There was once an error in AST-to-HIR conversion that set this
489 * to be out of bounds.
490 */
491 if (ir->type->array_size() > 0) {
492 if (ir->max_array_access >= ir->type->length) {
493 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
494 ir->max_array_access, ir->type->length - 1);
495 ir->print();
496 abort();
497 }
498 }
499
500 return visit_continue;
501 }
502
503 ir_visitor_status
504 ir_validate::visit_enter(ir_assignment *ir)
505 {
506 const ir_dereference *const lhs = ir->lhs;
507 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
508 if (ir->write_mask == 0) {
509 printf("Assignment LHS is %s, but write mask is 0:\n",
510 lhs->type->is_scalar() ? "scalar" : "vector");
511 ir->print();
512 abort();
513 }
514
515 int lhs_components = 0;
516 for (int i = 0; i < 4; i++) {
517 if (ir->write_mask & (1 << i))
518 lhs_components++;
519 }
520
521 if (lhs_components != ir->rhs->type->vector_elements) {
522 printf("Assignment count of LHS write mask channels enabled not\n"
523 "matching RHS vector size (%d LHS, %d RHS).\n",
524 lhs_components, ir->rhs->type->vector_elements);
525 ir->print();
526 abort();
527 }
528 }
529
530 this->validate_ir(ir, this->data);
531
532 return visit_continue;
533 }
534
535 ir_visitor_status
536 ir_validate::visit_enter(ir_call *ir)
537 {
538 ir_function_signature *const callee = ir->get_callee();
539
540 if (callee->ir_type != ir_type_function_signature) {
541 printf("IR called by ir_call is not ir_function_signature!\n");
542 abort();
543 }
544
545 const exec_node *formal_param_node = callee->parameters.head;
546 const exec_node *actual_param_node = ir->actual_parameters.head;
547 while (true) {
548 if (formal_param_node->is_tail_sentinel()
549 != actual_param_node->is_tail_sentinel()) {
550 printf("ir_call has the wrong number of parameters:\n");
551 goto dump_ir;
552 }
553 if (formal_param_node->is_tail_sentinel()) {
554 break;
555 }
556 const ir_variable *formal_param
557 = (const ir_variable *) formal_param_node;
558 const ir_rvalue *actual_param
559 = (const ir_rvalue *) actual_param_node;
560 if (formal_param->type != actual_param->type) {
561 printf("ir_call parameter type mismatch:\n");
562 goto dump_ir;
563 }
564 if (formal_param->mode == ir_var_out
565 || formal_param->mode == ir_var_inout) {
566 if (!actual_param->is_lvalue()) {
567 printf("ir_call out/inout parameters must be lvalues:\n");
568 goto dump_ir;
569 }
570 }
571 formal_param_node = formal_param_node->next;
572 actual_param_node = actual_param_node->next;
573 }
574
575 return visit_continue;
576
577 dump_ir:
578 ir->print();
579 printf("callee:\n");
580 callee->print();
581 abort();
582 }
583
584 void
585 ir_validate::validate_ir(ir_instruction *ir, void *data)
586 {
587 struct hash_table *ht = (struct hash_table *) data;
588
589 if (hash_table_find(ht, ir)) {
590 printf("Instruction node present twice in ir tree:\n");
591 ir->print();
592 printf("\n");
593 abort();
594 }
595 hash_table_insert(ht, ir, ir);
596 }
597
598 void
599 check_node_type(ir_instruction *ir, void *data)
600 {
601 (void) data;
602
603 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
604 printf("Instruction node with unset type\n");
605 ir->print(); printf("\n");
606 }
607 assert(ir->type != glsl_type::error_type);
608 }
609
610 void
611 validate_ir_tree(exec_list *instructions)
612 {
613 ir_validate v;
614
615 v.run(instructions);
616
617 foreach_iter(exec_list_iterator, iter, *instructions) {
618 ir_instruction *ir = (ir_instruction *)iter.get();
619
620 visit_tree(ir, check_node_type, NULL);
621 }
622 }