i965/fs: Add support for ir_unop_saturate
[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 case ir_unop_saturate:
245 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
246 assert(ir->type == ir->operands[0]->type);
247 break;
248
249 case ir_unop_f2i:
250 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
251 assert(ir->type->base_type == GLSL_TYPE_INT);
252 break;
253 case ir_unop_f2u:
254 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
255 assert(ir->type->base_type == GLSL_TYPE_UINT);
256 break;
257 case ir_unop_i2f:
258 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
259 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
260 break;
261 case ir_unop_f2b:
262 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
263 assert(ir->type->base_type == GLSL_TYPE_BOOL);
264 break;
265 case ir_unop_b2f:
266 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
267 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
268 break;
269 case ir_unop_i2b:
270 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
271 assert(ir->type->base_type == GLSL_TYPE_BOOL);
272 break;
273 case ir_unop_b2i:
274 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
275 assert(ir->type->base_type == GLSL_TYPE_INT);
276 break;
277 case ir_unop_u2f:
278 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
279 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
280 break;
281 case ir_unop_i2u:
282 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
283 assert(ir->type->base_type == GLSL_TYPE_UINT);
284 break;
285 case ir_unop_u2i:
286 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
287 assert(ir->type->base_type == GLSL_TYPE_INT);
288 break;
289 case ir_unop_bitcast_i2f:
290 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
291 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
292 break;
293 case ir_unop_bitcast_f2i:
294 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
295 assert(ir->type->base_type == GLSL_TYPE_INT);
296 break;
297 case ir_unop_bitcast_u2f:
298 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
299 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
300 break;
301 case ir_unop_bitcast_f2u:
302 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
303 assert(ir->type->base_type == GLSL_TYPE_UINT);
304 break;
305
306 case ir_unop_any:
307 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
308 assert(ir->type == glsl_type::bool_type);
309 break;
310
311 case ir_unop_trunc:
312 case ir_unop_round_even:
313 case ir_unop_ceil:
314 case ir_unop_floor:
315 case ir_unop_fract:
316 case ir_unop_sin:
317 case ir_unop_cos:
318 case ir_unop_sin_reduced:
319 case ir_unop_cos_reduced:
320 case ir_unop_dFdx:
321 case ir_unop_dFdx_coarse:
322 case ir_unop_dFdx_fine:
323 case ir_unop_dFdy:
324 case ir_unop_dFdy_coarse:
325 case ir_unop_dFdy_fine:
326 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
327 assert(ir->operands[0]->type == ir->type);
328 break;
329
330 case ir_unop_pack_snorm_2x16:
331 case ir_unop_pack_unorm_2x16:
332 case ir_unop_pack_half_2x16:
333 assert(ir->type == glsl_type::uint_type);
334 assert(ir->operands[0]->type == glsl_type::vec2_type);
335 break;
336
337 case ir_unop_pack_snorm_4x8:
338 case ir_unop_pack_unorm_4x8:
339 assert(ir->type == glsl_type::uint_type);
340 assert(ir->operands[0]->type == glsl_type::vec4_type);
341 break;
342
343 case ir_unop_unpack_snorm_2x16:
344 case ir_unop_unpack_unorm_2x16:
345 case ir_unop_unpack_half_2x16:
346 assert(ir->type == glsl_type::vec2_type);
347 assert(ir->operands[0]->type == glsl_type::uint_type);
348 break;
349
350 case ir_unop_unpack_snorm_4x8:
351 case ir_unop_unpack_unorm_4x8:
352 assert(ir->type == glsl_type::vec4_type);
353 assert(ir->operands[0]->type == glsl_type::uint_type);
354 break;
355
356 case ir_unop_unpack_half_2x16_split_x:
357 case ir_unop_unpack_half_2x16_split_y:
358 assert(ir->type == glsl_type::float_type);
359 assert(ir->operands[0]->type == glsl_type::uint_type);
360 break;
361
362 case ir_unop_bitfield_reverse:
363 assert(ir->operands[0]->type == ir->type);
364 assert(ir->type->is_integer());
365 break;
366
367 case ir_unop_bit_count:
368 case ir_unop_find_msb:
369 case ir_unop_find_lsb:
370 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
371 assert(ir->operands[0]->type->is_integer());
372 assert(ir->type->base_type == GLSL_TYPE_INT);
373 break;
374
375 case ir_unop_noise:
376 /* XXX what can we assert here? */
377 break;
378
379 case ir_unop_interpolate_at_centroid:
380 assert(ir->operands[0]->type == ir->type);
381 assert(ir->operands[0]->type->is_float());
382 break;
383
384 case ir_binop_add:
385 case ir_binop_sub:
386 case ir_binop_mul:
387 case ir_binop_div:
388 case ir_binop_mod:
389 case ir_binop_min:
390 case ir_binop_max:
391 case ir_binop_pow:
392 assert(ir->operands[0]->type->base_type ==
393 ir->operands[1]->type->base_type);
394
395 if (ir->operands[0]->type->is_scalar())
396 assert(ir->operands[1]->type == ir->type);
397 else if (ir->operands[1]->type->is_scalar())
398 assert(ir->operands[0]->type == ir->type);
399 else if (ir->operands[0]->type->is_vector() &&
400 ir->operands[1]->type->is_vector()) {
401 assert(ir->operands[0]->type == ir->operands[1]->type);
402 assert(ir->operands[0]->type == ir->type);
403 }
404 break;
405
406 case ir_binop_imul_high:
407 assert(ir->type == ir->operands[0]->type);
408 assert(ir->type == ir->operands[1]->type);
409 assert(ir->type->is_integer());
410 break;
411
412 case ir_binop_carry:
413 case ir_binop_borrow:
414 assert(ir->type == ir->operands[0]->type);
415 assert(ir->type == ir->operands[1]->type);
416 assert(ir->type->base_type == GLSL_TYPE_UINT);
417 break;
418
419 case ir_binop_less:
420 case ir_binop_greater:
421 case ir_binop_lequal:
422 case ir_binop_gequal:
423 case ir_binop_equal:
424 case ir_binop_nequal:
425 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
426 * ==, and != operators. The IR operators perform a component-wise
427 * comparison on scalar or vector types and return a boolean scalar or
428 * vector type of the same size.
429 */
430 assert(ir->type->base_type == GLSL_TYPE_BOOL);
431 assert(ir->operands[0]->type == ir->operands[1]->type);
432 assert(ir->operands[0]->type->is_vector()
433 || ir->operands[0]->type->is_scalar());
434 assert(ir->operands[0]->type->vector_elements
435 == ir->type->vector_elements);
436 break;
437
438 case ir_binop_all_equal:
439 case ir_binop_any_nequal:
440 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
441 * return a scalar boolean. The IR matches that.
442 */
443 assert(ir->type == glsl_type::bool_type);
444 assert(ir->operands[0]->type == ir->operands[1]->type);
445 break;
446
447 case ir_binop_lshift:
448 case ir_binop_rshift:
449 assert(ir->operands[0]->type->is_integer() &&
450 ir->operands[1]->type->is_integer());
451 if (ir->operands[0]->type->is_scalar()) {
452 assert(ir->operands[1]->type->is_scalar());
453 }
454 if (ir->operands[0]->type->is_vector() &&
455 ir->operands[1]->type->is_vector()) {
456 assert(ir->operands[0]->type->components() ==
457 ir->operands[1]->type->components());
458 }
459 assert(ir->type == ir->operands[0]->type);
460 break;
461
462 case ir_binop_bit_and:
463 case ir_binop_bit_xor:
464 case ir_binop_bit_or:
465 assert(ir->operands[0]->type->base_type ==
466 ir->operands[1]->type->base_type);
467 assert(ir->type->is_integer());
468 if (ir->operands[0]->type->is_vector() &&
469 ir->operands[1]->type->is_vector()) {
470 assert(ir->operands[0]->type->vector_elements ==
471 ir->operands[1]->type->vector_elements);
472 }
473 break;
474
475 case ir_binop_logic_and:
476 case ir_binop_logic_xor:
477 case ir_binop_logic_or:
478 assert(ir->type == glsl_type::bool_type);
479 assert(ir->operands[0]->type == glsl_type::bool_type);
480 assert(ir->operands[1]->type == glsl_type::bool_type);
481 break;
482
483 case ir_binop_dot:
484 assert(ir->type == glsl_type::float_type);
485 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
486 assert(ir->operands[0]->type->is_vector());
487 assert(ir->operands[0]->type == ir->operands[1]->type);
488 break;
489
490 case ir_binop_pack_half_2x16_split:
491 assert(ir->type == glsl_type::uint_type);
492 assert(ir->operands[0]->type == glsl_type::float_type);
493 assert(ir->operands[1]->type == glsl_type::float_type);
494 break;
495
496 case ir_binop_bfm:
497 assert(ir->type->is_integer());
498 assert(ir->operands[0]->type->is_integer());
499 assert(ir->operands[1]->type->is_integer());
500 break;
501
502 case ir_binop_ubo_load:
503 assert(ir->operands[0]->type == glsl_type::uint_type);
504
505 assert(ir->operands[1]->type == glsl_type::uint_type);
506 break;
507
508 case ir_binop_ldexp:
509 assert(ir->operands[0]->type == ir->type);
510 assert(ir->operands[0]->type->is_float());
511 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
512 assert(ir->operands[0]->type->components() ==
513 ir->operands[1]->type->components());
514 break;
515
516 case ir_binop_vector_extract:
517 assert(ir->operands[0]->type->is_vector());
518 assert(ir->operands[1]->type->is_scalar()
519 && ir->operands[1]->type->is_integer());
520 break;
521
522 case ir_binop_interpolate_at_offset:
523 assert(ir->operands[0]->type == ir->type);
524 assert(ir->operands[0]->type->is_float());
525 assert(ir->operands[1]->type->components() == 2);
526 assert(ir->operands[1]->type->is_float());
527 break;
528
529 case ir_binop_interpolate_at_sample:
530 assert(ir->operands[0]->type == ir->type);
531 assert(ir->operands[0]->type->is_float());
532 assert(ir->operands[1]->type == glsl_type::int_type);
533 break;
534
535 case ir_triop_fma:
536 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
537 assert(ir->type == ir->operands[0]->type);
538 assert(ir->type == ir->operands[1]->type);
539 assert(ir->type == ir->operands[2]->type);
540 break;
541
542 case ir_triop_lrp:
543 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
544 assert(ir->operands[0]->type == ir->operands[1]->type);
545 assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
546 break;
547
548 case ir_triop_csel:
549 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
550 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
551 assert(ir->type == ir->operands[1]->type);
552 assert(ir->type == ir->operands[2]->type);
553 break;
554
555 case ir_triop_bfi:
556 assert(ir->operands[0]->type->is_integer());
557 assert(ir->operands[1]->type == ir->operands[2]->type);
558 assert(ir->operands[1]->type == ir->type);
559 break;
560
561 case ir_triop_bitfield_extract:
562 assert(ir->operands[0]->type == ir->type);
563 assert(ir->operands[1]->type == glsl_type::int_type);
564 assert(ir->operands[2]->type == glsl_type::int_type);
565 break;
566
567 case ir_triop_vector_insert:
568 assert(ir->operands[0]->type->is_vector());
569 assert(ir->operands[1]->type->is_scalar());
570 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
571 assert(ir->operands[2]->type->is_scalar()
572 && ir->operands[2]->type->is_integer());
573 assert(ir->type == ir->operands[0]->type);
574 break;
575
576 case ir_quadop_bitfield_insert:
577 assert(ir->operands[0]->type == ir->type);
578 assert(ir->operands[1]->type == ir->type);
579 assert(ir->operands[2]->type == glsl_type::int_type);
580 assert(ir->operands[3]->type == glsl_type::int_type);
581 break;
582
583 case ir_quadop_vector:
584 /* The vector operator collects some number of scalars and generates a
585 * vector from them.
586 *
587 * - All of the operands must be scalar.
588 * - Number of operands must matche the size of the resulting vector.
589 * - Base type of the operands must match the base type of the result.
590 */
591 assert(ir->type->is_vector());
592 switch (ir->type->vector_elements) {
593 case 2:
594 assert(ir->operands[0]->type->is_scalar());
595 assert(ir->operands[0]->type->base_type == ir->type->base_type);
596 assert(ir->operands[1]->type->is_scalar());
597 assert(ir->operands[1]->type->base_type == ir->type->base_type);
598 assert(ir->operands[2] == NULL);
599 assert(ir->operands[3] == NULL);
600 break;
601 case 3:
602 assert(ir->operands[0]->type->is_scalar());
603 assert(ir->operands[0]->type->base_type == ir->type->base_type);
604 assert(ir->operands[1]->type->is_scalar());
605 assert(ir->operands[1]->type->base_type == ir->type->base_type);
606 assert(ir->operands[2]->type->is_scalar());
607 assert(ir->operands[2]->type->base_type == ir->type->base_type);
608 assert(ir->operands[3] == NULL);
609 break;
610 case 4:
611 assert(ir->operands[0]->type->is_scalar());
612 assert(ir->operands[0]->type->base_type == ir->type->base_type);
613 assert(ir->operands[1]->type->is_scalar());
614 assert(ir->operands[1]->type->base_type == ir->type->base_type);
615 assert(ir->operands[2]->type->is_scalar());
616 assert(ir->operands[2]->type->base_type == ir->type->base_type);
617 assert(ir->operands[3]->type->is_scalar());
618 assert(ir->operands[3]->type->base_type == ir->type->base_type);
619 break;
620 default:
621 /* The is_vector assertion above should prevent execution from ever
622 * getting here.
623 */
624 assert(!"Should not get here.");
625 break;
626 }
627 }
628
629 return visit_continue;
630 }
631
632 ir_visitor_status
633 ir_validate::visit_leave(ir_swizzle *ir)
634 {
635 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
636
637 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
638 if (chans[i] >= ir->val->type->vector_elements) {
639 printf("ir_swizzle @ %p specifies a channel not present "
640 "in the value.\n", (void *) ir);
641 ir->print();
642 abort();
643 }
644 }
645
646 return visit_continue;
647 }
648
649 ir_visitor_status
650 ir_validate::visit(ir_variable *ir)
651 {
652 /* An ir_variable is the one thing that can (and will) appear multiple times
653 * in an IR tree. It is added to the hashtable so that it can be used
654 * in the ir_dereference_variable handler to ensure that a variable is
655 * declared before it is dereferenced.
656 */
657 if (ir->name)
658 assert(ralloc_parent(ir->name) == ir);
659
660 hash_table_insert(ht, ir, ir);
661
662
663 /* If a variable is an array, verify that the maximum array index is in
664 * bounds. There was once an error in AST-to-HIR conversion that set this
665 * to be out of bounds.
666 */
667 if (ir->type->array_size() > 0) {
668 if (ir->data.max_array_access >= ir->type->length) {
669 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
670 ir->data.max_array_access, ir->type->length - 1);
671 ir->print();
672 abort();
673 }
674 }
675
676 /* If a variable is an interface block (or an array of interface blocks),
677 * verify that the maximum array index for each interface member is in
678 * bounds.
679 */
680 if (ir->is_interface_instance()) {
681 const glsl_struct_field *fields =
682 ir->get_interface_type()->fields.structure;
683 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
684 if (fields[i].type->array_size() > 0) {
685 if (ir->max_ifc_array_access[i] >= fields[i].type->length) {
686 printf("ir_variable has maximum access out of bounds for "
687 "field %s (%d vs %d)\n", fields[i].name,
688 ir->max_ifc_array_access[i], fields[i].type->length);
689 ir->print();
690 abort();
691 }
692 }
693 }
694 }
695
696 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
697 printf("ir_variable didn't have an initializer, but has a constant "
698 "initializer value.\n");
699 ir->print();
700 abort();
701 }
702
703 return visit_continue;
704 }
705
706 ir_visitor_status
707 ir_validate::visit_enter(ir_assignment *ir)
708 {
709 const ir_dereference *const lhs = ir->lhs;
710 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
711 if (ir->write_mask == 0) {
712 printf("Assignment LHS is %s, but write mask is 0:\n",
713 lhs->type->is_scalar() ? "scalar" : "vector");
714 ir->print();
715 abort();
716 }
717
718 int lhs_components = 0;
719 for (int i = 0; i < 4; i++) {
720 if (ir->write_mask & (1 << i))
721 lhs_components++;
722 }
723
724 if (lhs_components != ir->rhs->type->vector_elements) {
725 printf("Assignment count of LHS write mask channels enabled not\n"
726 "matching RHS vector size (%d LHS, %d RHS).\n",
727 lhs_components, ir->rhs->type->vector_elements);
728 ir->print();
729 abort();
730 }
731 }
732
733 this->validate_ir(ir, this->data_enter);
734
735 return visit_continue;
736 }
737
738 ir_visitor_status
739 ir_validate::visit_enter(ir_call *ir)
740 {
741 ir_function_signature *const callee = ir->callee;
742
743 if (callee->ir_type != ir_type_function_signature) {
744 printf("IR called by ir_call is not ir_function_signature!\n");
745 abort();
746 }
747
748 if (ir->return_deref) {
749 if (ir->return_deref->type != callee->return_type) {
750 printf("callee type %s does not match return storage type %s\n",
751 callee->return_type->name, ir->return_deref->type->name);
752 abort();
753 }
754 } else if (callee->return_type != glsl_type::void_type) {
755 printf("ir_call has non-void callee but no return storage\n");
756 abort();
757 }
758
759 const exec_node *formal_param_node = callee->parameters.head;
760 const exec_node *actual_param_node = ir->actual_parameters.head;
761 while (true) {
762 if (formal_param_node->is_tail_sentinel()
763 != actual_param_node->is_tail_sentinel()) {
764 printf("ir_call has the wrong number of parameters:\n");
765 goto dump_ir;
766 }
767 if (formal_param_node->is_tail_sentinel()) {
768 break;
769 }
770 const ir_variable *formal_param
771 = (const ir_variable *) formal_param_node;
772 const ir_rvalue *actual_param
773 = (const ir_rvalue *) actual_param_node;
774 if (formal_param->type != actual_param->type) {
775 printf("ir_call parameter type mismatch:\n");
776 goto dump_ir;
777 }
778 if (formal_param->data.mode == ir_var_function_out
779 || formal_param->data.mode == ir_var_function_inout) {
780 if (!actual_param->is_lvalue()) {
781 printf("ir_call out/inout parameters must be lvalues:\n");
782 goto dump_ir;
783 }
784 }
785 formal_param_node = formal_param_node->next;
786 actual_param_node = actual_param_node->next;
787 }
788
789 return visit_continue;
790
791 dump_ir:
792 ir->print();
793 printf("callee:\n");
794 callee->print();
795 abort();
796 return visit_stop;
797 }
798
799 void
800 ir_validate::validate_ir(ir_instruction *ir, void *data)
801 {
802 struct hash_table *ht = (struct hash_table *) data;
803
804 if (hash_table_find(ht, ir)) {
805 printf("Instruction node present twice in ir tree:\n");
806 ir->print();
807 printf("\n");
808 abort();
809 }
810 hash_table_insert(ht, ir, ir);
811 }
812
813 void
814 check_node_type(ir_instruction *ir, void *data)
815 {
816 (void) data;
817
818 if (ir->ir_type >= ir_type_max) {
819 printf("Instruction node with unset type\n");
820 ir->print(); printf("\n");
821 }
822 ir_rvalue *value = ir->as_rvalue();
823 if (value != NULL)
824 assert(value->type != glsl_type::error_type);
825 }
826
827 void
828 validate_ir_tree(exec_list *instructions)
829 {
830 /* We shouldn't have any reason to validate IR in a release build,
831 * and it's half composed of assert()s anyway which wouldn't do
832 * anything.
833 */
834 #ifdef DEBUG
835 ir_validate v;
836
837 v.run(instructions);
838
839 foreach_in_list(ir_instruction, ir, instructions) {
840 visit_tree(ir, check_node_type, NULL);
841 }
842 #endif
843 }