glsl: add ARB_derivative control support
[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 "ir.h"
37 #include "ir_hierarchical_visitor.h"
38 #include "program/hash_table.h"
39 #include "glsl_types.h"
40
41 namespace {
42
43 class ir_validate : public ir_hierarchical_visitor {
44 public:
45 ir_validate()
46 {
47 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
48 hash_table_pointer_compare);
49
50 this->current_function = NULL;
51
52 this->callback_enter = ir_validate::validate_ir;
53 this->data_enter = ht;
54 }
55
56 ~ir_validate()
57 {
58 hash_table_dtor(this->ht);
59 }
60
61 virtual ir_visitor_status visit(ir_variable *v);
62 virtual ir_visitor_status visit(ir_dereference_variable *ir);
63
64 virtual ir_visitor_status visit_enter(ir_if *ir);
65
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(class ir_dereference_array *);
74
75 virtual ir_visitor_status visit_enter(ir_assignment *ir);
76 virtual ir_visitor_status visit_enter(ir_call *ir);
77
78 static void validate_ir(ir_instruction *ir, void *data);
79
80 ir_function *current_function;
81
82 struct hash_table *ht;
83 };
84
85 } /* anonymous namespace */
86
87 ir_visitor_status
88 ir_validate::visit(ir_dereference_variable *ir)
89 {
90 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
91 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
92 (void *) ir, (void *) ir->var);
93 abort();
94 }
95
96 if (hash_table_find(ht, ir->var) == NULL) {
97 printf("ir_dereference_variable @ %p specifies undeclared variable "
98 "`%s' @ %p\n",
99 (void *) ir, ir->var->name, (void *) ir->var);
100 abort();
101 }
102
103 this->validate_ir(ir, this->data_enter);
104
105 return visit_continue;
106 }
107
108 ir_visitor_status
109 ir_validate::visit_enter(class ir_dereference_array *ir)
110 {
111 if (!ir->array->type->is_array() && !ir->array->type->is_matrix()) {
112 printf("ir_dereference_array @ %p does not specify an array or a "
113 "matrix\n",
114 (void *) ir);
115 ir->print();
116 printf("\n");
117 abort();
118 }
119
120 if (!ir->array_index->type->is_scalar()) {
121 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
122 (void *) ir, ir->array_index->type->name);
123 abort();
124 }
125
126 if (!ir->array_index->type->is_integer()) {
127 printf("ir_dereference_array @ %p does not have integer index: %s\n",
128 (void *) ir, ir->array_index->type->name);
129 abort();
130 }
131
132 return visit_continue;
133 }
134
135 ir_visitor_status
136 ir_validate::visit_enter(ir_if *ir)
137 {
138 if (ir->condition->type != glsl_type::bool_type) {
139 printf("ir_if condition %s type instead of bool.\n",
140 ir->condition->type->name);
141 ir->print();
142 printf("\n");
143 abort();
144 }
145
146 return visit_continue;
147 }
148
149
150 ir_visitor_status
151 ir_validate::visit_enter(ir_function *ir)
152 {
153 /* Function definitions cannot be nested.
154 */
155 if (this->current_function != NULL) {
156 printf("Function definition nested inside another function "
157 "definition:\n");
158 printf("%s %p inside %s %p\n",
159 ir->name, (void *) ir,
160 this->current_function->name, (void *) this->current_function);
161 abort();
162 }
163
164 /* Store the current function hierarchy being traversed. This is used
165 * by the function signature visitor to ensure that the signatures are
166 * linked with the correct functions.
167 */
168 this->current_function = ir;
169
170 this->validate_ir(ir, this->data_enter);
171
172 /* Verify that all of the things stored in the list of signatures are,
173 * in fact, function signatures.
174 */
175 foreach_in_list(ir_instruction, sig, &ir->signatures) {
176 if (sig->ir_type != ir_type_function_signature) {
177 printf("Non-signature in signature list of function `%s'\n",
178 ir->name);
179 abort();
180 }
181 }
182
183 return visit_continue;
184 }
185
186 ir_visitor_status
187 ir_validate::visit_leave(ir_function *ir)
188 {
189 assert(ralloc_parent(ir->name) == ir);
190
191 this->current_function = NULL;
192 return visit_continue;
193 }
194
195 ir_visitor_status
196 ir_validate::visit_enter(ir_function_signature *ir)
197 {
198 if (this->current_function != ir->function()) {
199 printf("Function signature nested inside wrong function "
200 "definition:\n");
201 printf("%p inside %s %p instead of %s %p\n",
202 (void *) ir,
203 this->current_function->name, (void *) this->current_function,
204 ir->function_name(), (void *) ir->function());
205 abort();
206 }
207
208 if (ir->return_type == NULL) {
209 printf("Function signature %p for function %s has NULL return type.\n",
210 (void *) ir, ir->function_name());
211 abort();
212 }
213
214 this->validate_ir(ir, this->data_enter);
215
216 return visit_continue;
217 }
218
219 ir_visitor_status
220 ir_validate::visit_leave(ir_expression *ir)
221 {
222 switch (ir->operation) {
223 case ir_unop_bit_not:
224 assert(ir->operands[0]->type == ir->type);
225 break;
226 case ir_unop_logic_not:
227 assert(ir->type->base_type == GLSL_TYPE_BOOL);
228 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
229 break;
230
231 case ir_unop_neg:
232 case ir_unop_abs:
233 case ir_unop_sign:
234 case ir_unop_rcp:
235 case ir_unop_rsq:
236 case ir_unop_sqrt:
237 assert(ir->type == ir->operands[0]->type);
238 break;
239
240 case ir_unop_exp:
241 case ir_unop_log:
242 case ir_unop_exp2:
243 case ir_unop_log2:
244 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
245 assert(ir->type == ir->operands[0]->type);
246 break;
247
248 case ir_unop_f2i:
249 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
250 assert(ir->type->base_type == GLSL_TYPE_INT);
251 break;
252 case ir_unop_f2u:
253 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
254 assert(ir->type->base_type == GLSL_TYPE_UINT);
255 break;
256 case ir_unop_i2f:
257 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
258 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
259 break;
260 case ir_unop_f2b:
261 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
262 assert(ir->type->base_type == GLSL_TYPE_BOOL);
263 break;
264 case ir_unop_b2f:
265 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
266 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
267 break;
268 case ir_unop_i2b:
269 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
270 assert(ir->type->base_type == GLSL_TYPE_BOOL);
271 break;
272 case ir_unop_b2i:
273 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
274 assert(ir->type->base_type == GLSL_TYPE_INT);
275 break;
276 case ir_unop_u2f:
277 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
278 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
279 break;
280 case ir_unop_i2u:
281 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
282 assert(ir->type->base_type == GLSL_TYPE_UINT);
283 break;
284 case ir_unop_u2i:
285 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
286 assert(ir->type->base_type == GLSL_TYPE_INT);
287 break;
288 case ir_unop_bitcast_i2f:
289 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
290 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
291 break;
292 case ir_unop_bitcast_f2i:
293 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
294 assert(ir->type->base_type == GLSL_TYPE_INT);
295 break;
296 case ir_unop_bitcast_u2f:
297 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
298 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
299 break;
300 case ir_unop_bitcast_f2u:
301 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
302 assert(ir->type->base_type == GLSL_TYPE_UINT);
303 break;
304
305 case ir_unop_any:
306 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
307 assert(ir->type == glsl_type::bool_type);
308 break;
309
310 case ir_unop_trunc:
311 case ir_unop_round_even:
312 case ir_unop_ceil:
313 case ir_unop_floor:
314 case ir_unop_fract:
315 case ir_unop_sin:
316 case ir_unop_cos:
317 case ir_unop_sin_reduced:
318 case ir_unop_cos_reduced:
319 case ir_unop_dFdx:
320 case ir_unop_dFdx_coarse:
321 case ir_unop_dFdx_fine:
322 case ir_unop_dFdy:
323 case ir_unop_dFdy_coarse:
324 case ir_unop_dFdy_fine:
325 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
326 assert(ir->operands[0]->type == ir->type);
327 break;
328
329 case ir_unop_pack_snorm_2x16:
330 case ir_unop_pack_unorm_2x16:
331 case ir_unop_pack_half_2x16:
332 assert(ir->type == glsl_type::uint_type);
333 assert(ir->operands[0]->type == glsl_type::vec2_type);
334 break;
335
336 case ir_unop_pack_snorm_4x8:
337 case ir_unop_pack_unorm_4x8:
338 assert(ir->type == glsl_type::uint_type);
339 assert(ir->operands[0]->type == glsl_type::vec4_type);
340 break;
341
342 case ir_unop_unpack_snorm_2x16:
343 case ir_unop_unpack_unorm_2x16:
344 case ir_unop_unpack_half_2x16:
345 assert(ir->type == glsl_type::vec2_type);
346 assert(ir->operands[0]->type == glsl_type::uint_type);
347 break;
348
349 case ir_unop_unpack_snorm_4x8:
350 case ir_unop_unpack_unorm_4x8:
351 assert(ir->type == glsl_type::vec4_type);
352 assert(ir->operands[0]->type == glsl_type::uint_type);
353 break;
354
355 case ir_unop_unpack_half_2x16_split_x:
356 case ir_unop_unpack_half_2x16_split_y:
357 assert(ir->type == glsl_type::float_type);
358 assert(ir->operands[0]->type == glsl_type::uint_type);
359 break;
360
361 case ir_unop_bitfield_reverse:
362 assert(ir->operands[0]->type == ir->type);
363 assert(ir->type->is_integer());
364 break;
365
366 case ir_unop_bit_count:
367 case ir_unop_find_msb:
368 case ir_unop_find_lsb:
369 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
370 assert(ir->operands[0]->type->is_integer());
371 assert(ir->type->base_type == GLSL_TYPE_INT);
372 break;
373
374 case ir_unop_noise:
375 /* XXX what can we assert here? */
376 break;
377
378 case ir_unop_interpolate_at_centroid:
379 assert(ir->operands[0]->type == ir->type);
380 assert(ir->operands[0]->type->is_float());
381 break;
382
383 case ir_binop_add:
384 case ir_binop_sub:
385 case ir_binop_mul:
386 case ir_binop_div:
387 case ir_binop_mod:
388 case ir_binop_min:
389 case ir_binop_max:
390 case ir_binop_pow:
391 assert(ir->operands[0]->type->base_type ==
392 ir->operands[1]->type->base_type);
393
394 if (ir->operands[0]->type->is_scalar())
395 assert(ir->operands[1]->type == ir->type);
396 else if (ir->operands[1]->type->is_scalar())
397 assert(ir->operands[0]->type == ir->type);
398 else if (ir->operands[0]->type->is_vector() &&
399 ir->operands[1]->type->is_vector()) {
400 assert(ir->operands[0]->type == ir->operands[1]->type);
401 assert(ir->operands[0]->type == ir->type);
402 }
403 break;
404
405 case ir_binop_imul_high:
406 assert(ir->type == ir->operands[0]->type);
407 assert(ir->type == ir->operands[1]->type);
408 assert(ir->type->is_integer());
409 break;
410
411 case ir_binop_carry:
412 case ir_binop_borrow:
413 assert(ir->type == ir->operands[0]->type);
414 assert(ir->type == ir->operands[1]->type);
415 assert(ir->type->base_type == GLSL_TYPE_UINT);
416 break;
417
418 case ir_binop_less:
419 case ir_binop_greater:
420 case ir_binop_lequal:
421 case ir_binop_gequal:
422 case ir_binop_equal:
423 case ir_binop_nequal:
424 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
425 * ==, and != operators. The IR operators perform a component-wise
426 * comparison on scalar or vector types and return a boolean scalar or
427 * vector type of the same size.
428 */
429 assert(ir->type->base_type == GLSL_TYPE_BOOL);
430 assert(ir->operands[0]->type == ir->operands[1]->type);
431 assert(ir->operands[0]->type->is_vector()
432 || ir->operands[0]->type->is_scalar());
433 assert(ir->operands[0]->type->vector_elements
434 == ir->type->vector_elements);
435 break;
436
437 case ir_binop_all_equal:
438 case ir_binop_any_nequal:
439 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
440 * return a scalar boolean. The IR matches that.
441 */
442 assert(ir->type == glsl_type::bool_type);
443 assert(ir->operands[0]->type == ir->operands[1]->type);
444 break;
445
446 case ir_binop_lshift:
447 case ir_binop_rshift:
448 assert(ir->operands[0]->type->is_integer() &&
449 ir->operands[1]->type->is_integer());
450 if (ir->operands[0]->type->is_scalar()) {
451 assert(ir->operands[1]->type->is_scalar());
452 }
453 if (ir->operands[0]->type->is_vector() &&
454 ir->operands[1]->type->is_vector()) {
455 assert(ir->operands[0]->type->components() ==
456 ir->operands[1]->type->components());
457 }
458 assert(ir->type == ir->operands[0]->type);
459 break;
460
461 case ir_binop_bit_and:
462 case ir_binop_bit_xor:
463 case ir_binop_bit_or:
464 assert(ir->operands[0]->type->base_type ==
465 ir->operands[1]->type->base_type);
466 assert(ir->type->is_integer());
467 if (ir->operands[0]->type->is_vector() &&
468 ir->operands[1]->type->is_vector()) {
469 assert(ir->operands[0]->type->vector_elements ==
470 ir->operands[1]->type->vector_elements);
471 }
472 break;
473
474 case ir_binop_logic_and:
475 case ir_binop_logic_xor:
476 case ir_binop_logic_or:
477 assert(ir->type == glsl_type::bool_type);
478 assert(ir->operands[0]->type == glsl_type::bool_type);
479 assert(ir->operands[1]->type == glsl_type::bool_type);
480 break;
481
482 case ir_binop_dot:
483 assert(ir->type == glsl_type::float_type);
484 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
485 assert(ir->operands[0]->type->is_vector());
486 assert(ir->operands[0]->type == ir->operands[1]->type);
487 break;
488
489 case ir_binop_pack_half_2x16_split:
490 assert(ir->type == glsl_type::uint_type);
491 assert(ir->operands[0]->type == glsl_type::float_type);
492 assert(ir->operands[1]->type == glsl_type::float_type);
493 break;
494
495 case ir_binop_bfm:
496 assert(ir->type->is_integer());
497 assert(ir->operands[0]->type->is_integer());
498 assert(ir->operands[1]->type->is_integer());
499 break;
500
501 case ir_binop_ubo_load:
502 assert(ir->operands[0]->type == glsl_type::uint_type);
503
504 assert(ir->operands[1]->type == glsl_type::uint_type);
505 break;
506
507 case ir_binop_ldexp:
508 assert(ir->operands[0]->type == ir->type);
509 assert(ir->operands[0]->type->is_float());
510 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
511 assert(ir->operands[0]->type->components() ==
512 ir->operands[1]->type->components());
513 break;
514
515 case ir_binop_vector_extract:
516 assert(ir->operands[0]->type->is_vector());
517 assert(ir->operands[1]->type->is_scalar()
518 && ir->operands[1]->type->is_integer());
519 break;
520
521 case ir_binop_interpolate_at_offset:
522 assert(ir->operands[0]->type == ir->type);
523 assert(ir->operands[0]->type->is_float());
524 assert(ir->operands[1]->type->components() == 2);
525 assert(ir->operands[1]->type->is_float());
526 break;
527
528 case ir_binop_interpolate_at_sample:
529 assert(ir->operands[0]->type == ir->type);
530 assert(ir->operands[0]->type->is_float());
531 assert(ir->operands[1]->type == glsl_type::int_type);
532 break;
533
534 case ir_triop_fma:
535 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
536 assert(ir->type == ir->operands[0]->type);
537 assert(ir->type == ir->operands[1]->type);
538 assert(ir->type == ir->operands[2]->type);
539 break;
540
541 case ir_triop_lrp:
542 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
543 assert(ir->operands[0]->type == ir->operands[1]->type);
544 assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
545 break;
546
547 case ir_triop_csel:
548 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
549 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
550 assert(ir->type == ir->operands[1]->type);
551 assert(ir->type == ir->operands[2]->type);
552 break;
553
554 case ir_triop_bfi:
555 assert(ir->operands[0]->type->is_integer());
556 assert(ir->operands[1]->type == ir->operands[2]->type);
557 assert(ir->operands[1]->type == ir->type);
558 break;
559
560 case ir_triop_bitfield_extract:
561 assert(ir->operands[0]->type == ir->type);
562 assert(ir->operands[1]->type == glsl_type::int_type);
563 assert(ir->operands[2]->type == glsl_type::int_type);
564 break;
565
566 case ir_triop_vector_insert:
567 assert(ir->operands[0]->type->is_vector());
568 assert(ir->operands[1]->type->is_scalar());
569 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
570 assert(ir->operands[2]->type->is_scalar()
571 && ir->operands[2]->type->is_integer());
572 assert(ir->type == ir->operands[0]->type);
573 break;
574
575 case ir_quadop_bitfield_insert:
576 assert(ir->operands[0]->type == ir->type);
577 assert(ir->operands[1]->type == ir->type);
578 assert(ir->operands[2]->type == glsl_type::int_type);
579 assert(ir->operands[3]->type == glsl_type::int_type);
580 break;
581
582 case ir_quadop_vector:
583 /* The vector operator collects some number of scalars and generates a
584 * vector from them.
585 *
586 * - All of the operands must be scalar.
587 * - Number of operands must matche the size of the resulting vector.
588 * - Base type of the operands must match the base type of the result.
589 */
590 assert(ir->type->is_vector());
591 switch (ir->type->vector_elements) {
592 case 2:
593 assert(ir->operands[0]->type->is_scalar());
594 assert(ir->operands[0]->type->base_type == ir->type->base_type);
595 assert(ir->operands[1]->type->is_scalar());
596 assert(ir->operands[1]->type->base_type == ir->type->base_type);
597 assert(ir->operands[2] == NULL);
598 assert(ir->operands[3] == NULL);
599 break;
600 case 3:
601 assert(ir->operands[0]->type->is_scalar());
602 assert(ir->operands[0]->type->base_type == ir->type->base_type);
603 assert(ir->operands[1]->type->is_scalar());
604 assert(ir->operands[1]->type->base_type == ir->type->base_type);
605 assert(ir->operands[2]->type->is_scalar());
606 assert(ir->operands[2]->type->base_type == ir->type->base_type);
607 assert(ir->operands[3] == NULL);
608 break;
609 case 4:
610 assert(ir->operands[0]->type->is_scalar());
611 assert(ir->operands[0]->type->base_type == ir->type->base_type);
612 assert(ir->operands[1]->type->is_scalar());
613 assert(ir->operands[1]->type->base_type == ir->type->base_type);
614 assert(ir->operands[2]->type->is_scalar());
615 assert(ir->operands[2]->type->base_type == ir->type->base_type);
616 assert(ir->operands[3]->type->is_scalar());
617 assert(ir->operands[3]->type->base_type == ir->type->base_type);
618 break;
619 default:
620 /* The is_vector assertion above should prevent execution from ever
621 * getting here.
622 */
623 assert(!"Should not get here.");
624 break;
625 }
626 }
627
628 return visit_continue;
629 }
630
631 ir_visitor_status
632 ir_validate::visit_leave(ir_swizzle *ir)
633 {
634 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
635
636 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
637 if (chans[i] >= ir->val->type->vector_elements) {
638 printf("ir_swizzle @ %p specifies a channel not present "
639 "in the value.\n", (void *) ir);
640 ir->print();
641 abort();
642 }
643 }
644
645 return visit_continue;
646 }
647
648 ir_visitor_status
649 ir_validate::visit(ir_variable *ir)
650 {
651 /* An ir_variable is the one thing that can (and will) appear multiple times
652 * in an IR tree. It is added to the hashtable so that it can be used
653 * in the ir_dereference_variable handler to ensure that a variable is
654 * declared before it is dereferenced.
655 */
656 if (ir->name)
657 assert(ralloc_parent(ir->name) == ir);
658
659 hash_table_insert(ht, ir, ir);
660
661
662 /* If a variable is an array, verify that the maximum array index is in
663 * bounds. There was once an error in AST-to-HIR conversion that set this
664 * to be out of bounds.
665 */
666 if (ir->type->array_size() > 0) {
667 if (ir->data.max_array_access >= ir->type->length) {
668 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
669 ir->data.max_array_access, ir->type->length - 1);
670 ir->print();
671 abort();
672 }
673 }
674
675 /* If a variable is an interface block (or an array of interface blocks),
676 * verify that the maximum array index for each interface member is in
677 * bounds.
678 */
679 if (ir->is_interface_instance()) {
680 const glsl_struct_field *fields =
681 ir->get_interface_type()->fields.structure;
682 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
683 if (fields[i].type->array_size() > 0) {
684 if (ir->max_ifc_array_access[i] >= fields[i].type->length) {
685 printf("ir_variable has maximum access out of bounds for "
686 "field %s (%d vs %d)\n", fields[i].name,
687 ir->max_ifc_array_access[i], fields[i].type->length);
688 ir->print();
689 abort();
690 }
691 }
692 }
693 }
694
695 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
696 printf("ir_variable didn't have an initializer, but has a constant "
697 "initializer value.\n");
698 ir->print();
699 abort();
700 }
701
702 return visit_continue;
703 }
704
705 ir_visitor_status
706 ir_validate::visit_enter(ir_assignment *ir)
707 {
708 const ir_dereference *const lhs = ir->lhs;
709 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
710 if (ir->write_mask == 0) {
711 printf("Assignment LHS is %s, but write mask is 0:\n",
712 lhs->type->is_scalar() ? "scalar" : "vector");
713 ir->print();
714 abort();
715 }
716
717 int lhs_components = 0;
718 for (int i = 0; i < 4; i++) {
719 if (ir->write_mask & (1 << i))
720 lhs_components++;
721 }
722
723 if (lhs_components != ir->rhs->type->vector_elements) {
724 printf("Assignment count of LHS write mask channels enabled not\n"
725 "matching RHS vector size (%d LHS, %d RHS).\n",
726 lhs_components, ir->rhs->type->vector_elements);
727 ir->print();
728 abort();
729 }
730 }
731
732 this->validate_ir(ir, this->data_enter);
733
734 return visit_continue;
735 }
736
737 ir_visitor_status
738 ir_validate::visit_enter(ir_call *ir)
739 {
740 ir_function_signature *const callee = ir->callee;
741
742 if (callee->ir_type != ir_type_function_signature) {
743 printf("IR called by ir_call is not ir_function_signature!\n");
744 abort();
745 }
746
747 if (ir->return_deref) {
748 if (ir->return_deref->type != callee->return_type) {
749 printf("callee type %s does not match return storage type %s\n",
750 callee->return_type->name, ir->return_deref->type->name);
751 abort();
752 }
753 } else if (callee->return_type != glsl_type::void_type) {
754 printf("ir_call has non-void callee but no return storage\n");
755 abort();
756 }
757
758 const exec_node *formal_param_node = callee->parameters.head;
759 const exec_node *actual_param_node = ir->actual_parameters.head;
760 while (true) {
761 if (formal_param_node->is_tail_sentinel()
762 != actual_param_node->is_tail_sentinel()) {
763 printf("ir_call has the wrong number of parameters:\n");
764 goto dump_ir;
765 }
766 if (formal_param_node->is_tail_sentinel()) {
767 break;
768 }
769 const ir_variable *formal_param
770 = (const ir_variable *) formal_param_node;
771 const ir_rvalue *actual_param
772 = (const ir_rvalue *) actual_param_node;
773 if (formal_param->type != actual_param->type) {
774 printf("ir_call parameter type mismatch:\n");
775 goto dump_ir;
776 }
777 if (formal_param->data.mode == ir_var_function_out
778 || formal_param->data.mode == ir_var_function_inout) {
779 if (!actual_param->is_lvalue()) {
780 printf("ir_call out/inout parameters must be lvalues:\n");
781 goto dump_ir;
782 }
783 }
784 formal_param_node = formal_param_node->next;
785 actual_param_node = actual_param_node->next;
786 }
787
788 return visit_continue;
789
790 dump_ir:
791 ir->print();
792 printf("callee:\n");
793 callee->print();
794 abort();
795 return visit_stop;
796 }
797
798 void
799 ir_validate::validate_ir(ir_instruction *ir, void *data)
800 {
801 struct hash_table *ht = (struct hash_table *) data;
802
803 if (hash_table_find(ht, ir)) {
804 printf("Instruction node present twice in ir tree:\n");
805 ir->print();
806 printf("\n");
807 abort();
808 }
809 hash_table_insert(ht, ir, ir);
810 }
811
812 void
813 check_node_type(ir_instruction *ir, void *data)
814 {
815 (void) data;
816
817 if (ir->ir_type >= ir_type_max) {
818 printf("Instruction node with unset type\n");
819 ir->print(); printf("\n");
820 }
821 ir_rvalue *value = ir->as_rvalue();
822 if (value != NULL)
823 assert(value->type != glsl_type::error_type);
824 }
825
826 void
827 validate_ir_tree(exec_list *instructions)
828 {
829 /* We shouldn't have any reason to validate IR in a release build,
830 * and it's half composed of assert()s anyway which wouldn't do
831 * anything.
832 */
833 #ifdef DEBUG
834 ir_validate v;
835
836 v.run(instructions);
837
838 foreach_in_list(ir_instruction, ir, instructions) {
839 visit_tree(ir, check_node_type, NULL);
840 }
841 #endif
842 }