glsl: run validate_ir_tree if GLSL_VALIDATE=1 regardless of the build config
[mesa.git] / src / compiler / 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 "util/debug.h"
39 #include "util/hash_table.h"
40 #include "util/macros.h"
41 #include "util/set.h"
42 #include "compiler/glsl_types.h"
43
44 namespace {
45
46 class ir_validate : public ir_hierarchical_visitor {
47 public:
48 ir_validate()
49 {
50 this->ir_set = _mesa_pointer_set_create(NULL);
51
52 this->current_function = NULL;
53
54 this->callback_enter = ir_validate::validate_ir;
55 this->data_enter = ir_set;
56 }
57
58 ~ir_validate()
59 {
60 _mesa_set_destroy(this->ir_set, NULL);
61 }
62
63 virtual ir_visitor_status visit(ir_variable *v);
64 virtual ir_visitor_status visit(ir_dereference_variable *ir);
65
66 virtual ir_visitor_status visit_enter(ir_discard *ir);
67 virtual ir_visitor_status visit_enter(ir_if *ir);
68
69 virtual ir_visitor_status visit_enter(ir_function *ir);
70 virtual ir_visitor_status visit_leave(ir_function *ir);
71 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
72
73 virtual ir_visitor_status visit_leave(ir_expression *ir);
74 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
75
76 virtual ir_visitor_status visit_enter(class ir_dereference_array *);
77
78 virtual ir_visitor_status visit_enter(ir_assignment *ir);
79 virtual ir_visitor_status visit_enter(ir_call *ir);
80
81 static void validate_ir(ir_instruction *ir, void *data);
82
83 ir_function *current_function;
84
85 struct set *ir_set;
86 };
87
88 } /* anonymous namespace */
89
90 ir_visitor_status
91 ir_validate::visit(ir_dereference_variable *ir)
92 {
93 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
94 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
95 (void *) ir, (void *) ir->var);
96 abort();
97 }
98
99 if (_mesa_set_search(ir_set, ir->var) == NULL) {
100 printf("ir_dereference_variable @ %p specifies undeclared variable "
101 "`%s' @ %p\n",
102 (void *) ir, ir->var->name, (void *) ir->var);
103 abort();
104 }
105
106 this->validate_ir(ir, this->data_enter);
107
108 return visit_continue;
109 }
110
111 ir_visitor_status
112 ir_validate::visit_enter(class ir_dereference_array *ir)
113 {
114 if (!ir->array->type->is_array() && !ir->array->type->is_matrix() &&
115 !ir->array->type->is_vector()) {
116 printf("ir_dereference_array @ %p does not specify an array, a vector "
117 "or a matrix\n",
118 (void *) ir);
119 ir->print();
120 printf("\n");
121 abort();
122 }
123
124 if (!ir->array_index->type->is_scalar()) {
125 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
126 (void *) ir, ir->array_index->type->name);
127 abort();
128 }
129
130 if (!ir->array_index->type->is_integer_16_32()) {
131 printf("ir_dereference_array @ %p does not have integer index: %s\n",
132 (void *) ir, ir->array_index->type->name);
133 abort();
134 }
135
136 return visit_continue;
137 }
138
139 ir_visitor_status
140 ir_validate::visit_enter(ir_discard *ir)
141 {
142 if (ir->condition && ir->condition->type != glsl_type::bool_type) {
143 printf("ir_discard condition %s type instead of bool.\n",
144 ir->condition->type->name);
145 ir->print();
146 printf("\n");
147 abort();
148 }
149
150 return visit_continue;
151 }
152
153 ir_visitor_status
154 ir_validate::visit_enter(ir_if *ir)
155 {
156 if (ir->condition->type != glsl_type::bool_type) {
157 printf("ir_if condition %s type instead of bool.\n",
158 ir->condition->type->name);
159 ir->print();
160 printf("\n");
161 abort();
162 }
163
164 return visit_continue;
165 }
166
167
168 ir_visitor_status
169 ir_validate::visit_enter(ir_function *ir)
170 {
171 /* Function definitions cannot be nested.
172 */
173 if (this->current_function != NULL) {
174 printf("Function definition nested inside another function "
175 "definition:\n");
176 printf("%s %p inside %s %p\n",
177 ir->name, (void *) ir,
178 this->current_function->name, (void *) this->current_function);
179 abort();
180 }
181
182 /* Store the current function hierarchy being traversed. This is used
183 * by the function signature visitor to ensure that the signatures are
184 * linked with the correct functions.
185 */
186 this->current_function = ir;
187
188 this->validate_ir(ir, this->data_enter);
189
190 /* Verify that all of the things stored in the list of signatures are,
191 * in fact, function signatures.
192 */
193 foreach_in_list(ir_instruction, sig, &ir->signatures) {
194 if (sig->ir_type != ir_type_function_signature) {
195 printf("Non-signature in signature list of function `%s'\n",
196 ir->name);
197 abort();
198 }
199 }
200
201 return visit_continue;
202 }
203
204 ir_visitor_status
205 ir_validate::visit_leave(ir_function *ir)
206 {
207 assert(ralloc_parent(ir->name) == ir);
208
209 this->current_function = NULL;
210 return visit_continue;
211 }
212
213 ir_visitor_status
214 ir_validate::visit_enter(ir_function_signature *ir)
215 {
216 if (this->current_function != ir->function()) {
217 printf("Function signature nested inside wrong function "
218 "definition:\n");
219 printf("%p inside %s %p instead of %s %p\n",
220 (void *) ir,
221 this->current_function->name, (void *) this->current_function,
222 ir->function_name(), (void *) ir->function());
223 abort();
224 }
225
226 if (ir->return_type == NULL) {
227 printf("Function signature %p for function %s has NULL return type.\n",
228 (void *) ir, ir->function_name());
229 abort();
230 }
231
232 this->validate_ir(ir, this->data_enter);
233
234 return visit_continue;
235 }
236
237 ir_visitor_status
238 ir_validate::visit_leave(ir_expression *ir)
239 {
240 for (unsigned i = ir->num_operands; i < 4; i++) {
241 assert(ir->operands[i] == NULL);
242 }
243
244 for (unsigned i = 0; i < ir->num_operands; i++) {
245 assert(ir->operands[i] != NULL);
246 }
247
248 switch (ir->operation) {
249 case ir_unop_bit_not:
250 assert(ir->operands[0]->type == ir->type);
251 break;
252 case ir_unop_logic_not:
253 assert(ir->type->is_boolean());
254 assert(ir->operands[0]->type->is_boolean());
255 break;
256
257 case ir_unop_neg:
258 assert(ir->type == ir->operands[0]->type);
259 break;
260
261 case ir_unop_abs:
262 case ir_unop_sign:
263 assert(ir->operands[0]->type->is_int_16_32_64() ||
264 ir->operands[0]->type->is_float_16_32_64());
265 assert(ir->type == ir->operands[0]->type);
266 break;
267
268 case ir_unop_rcp:
269 case ir_unop_rsq:
270 case ir_unop_sqrt:
271 assert(ir->type->is_float_16_32_64());
272 assert(ir->type == ir->operands[0]->type);
273 break;
274
275 case ir_unop_exp:
276 case ir_unop_log:
277 case ir_unop_exp2:
278 case ir_unop_log2:
279 case ir_unop_saturate:
280 assert(ir->operands[0]->type->is_float_16_32());
281 assert(ir->type == ir->operands[0]->type);
282 break;
283
284 case ir_unop_f2i:
285 assert(ir->operands[0]->type->is_float_16_32());
286 assert(ir->type->is_int_16_32());
287 break;
288 case ir_unop_f2u:
289 assert(ir->operands[0]->type->is_float_16_32());
290 assert(ir->type->is_uint_16_32());
291 break;
292 case ir_unop_i2f:
293 assert(ir->operands[0]->type->is_int_16_32());
294 assert(ir->type->is_float_16_32());
295 break;
296 case ir_unop_f2b:
297 assert(ir->operands[0]->type->is_float_16_32());
298 assert(ir->type->is_boolean());
299 break;
300 case ir_unop_f162b:
301 assert(ir->operands[0]->type->base_type ==
302 GLSL_TYPE_FLOAT16);
303 assert(ir->type->is_boolean());
304 break;
305 case ir_unop_b2f:
306 assert(ir->operands[0]->type->is_boolean());
307 assert(ir->type->is_float_16_32());
308 break;
309 case ir_unop_b2f16:
310 assert(ir->operands[0]->type->is_boolean());
311 assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
312 break;
313 case ir_unop_i2b:
314 assert(ir->operands[0]->type->is_int_16_32());
315 assert(ir->type->is_boolean());
316 break;
317 case ir_unop_b2i:
318 assert(ir->operands[0]->type->is_boolean());
319 assert(ir->type->is_int_16_32());
320 break;
321 case ir_unop_u2f:
322 assert(ir->operands[0]->type->is_uint_16_32());
323 assert(ir->type->is_float_16_32());
324 break;
325 case ir_unop_i2u:
326 assert(ir->operands[0]->type->is_int_16_32());
327 assert(ir->type->is_uint_16_32());
328 break;
329 case ir_unop_u2i:
330 assert(ir->operands[0]->type->is_uint_16_32());
331 assert(ir->type->is_int_16_32());
332 break;
333 case ir_unop_bitcast_i2f:
334 assert(ir->operands[0]->type->is_int_16_32());
335 assert(ir->type->is_float_16_32());
336 break;
337 case ir_unop_bitcast_f2i:
338 assert(ir->operands[0]->type->is_float_16_32());
339 assert(ir->type->is_int_16_32());
340 break;
341 case ir_unop_bitcast_u2f:
342 assert(ir->operands[0]->type->is_uint_16_32());
343 assert(ir->type->is_float_16_32());
344 break;
345 case ir_unop_bitcast_f2u:
346 assert(ir->operands[0]->type->is_float_16_32());
347 assert(ir->type->is_uint_16_32());
348 break;
349
350 case ir_unop_bitcast_u642d:
351 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
352 assert(ir->type->is_double());
353 break;
354 case ir_unop_bitcast_i642d:
355 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
356 assert(ir->type->is_double());
357 break;
358 case ir_unop_bitcast_d2u64:
359 assert(ir->operands[0]->type->is_double());
360 assert(ir->type->base_type == GLSL_TYPE_UINT64);
361 break;
362 case ir_unop_bitcast_d2i64:
363 assert(ir->operands[0]->type->is_double());
364 assert(ir->type->base_type == GLSL_TYPE_INT64);
365 break;
366 case ir_unop_i642i:
367 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
368 assert(ir->type->is_int_16_32());
369 break;
370 case ir_unop_u642i:
371 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
372 assert(ir->type->is_int_16_32());
373 break;
374 case ir_unop_i642u:
375 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
376 assert(ir->type->is_uint_16_32());
377 break;
378 case ir_unop_u642u:
379 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
380 assert(ir->type->is_uint_16_32());
381 break;
382 case ir_unop_i642b:
383 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
384 assert(ir->type->is_boolean());
385 break;
386 case ir_unop_i642f:
387 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
388 assert(ir->type->is_float());
389 break;
390 case ir_unop_u642f:
391 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
392 assert(ir->type->is_float());
393 break;
394 case ir_unop_i642d:
395 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
396 assert(ir->type->is_double());
397 break;
398 case ir_unop_u642d:
399 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
400 assert(ir->type->is_double());
401 break;
402 case ir_unop_i2i64:
403 assert(ir->operands[0]->type->is_int_16_32());
404 assert(ir->type->base_type == GLSL_TYPE_INT64);
405 break;
406 case ir_unop_u2i64:
407 assert(ir->operands[0]->type->is_uint_16_32());
408 assert(ir->type->base_type == GLSL_TYPE_INT64);
409 break;
410 case ir_unop_b2i64:
411 assert(ir->operands[0]->type->is_boolean());
412 assert(ir->type->base_type == GLSL_TYPE_INT64);
413 break;
414 case ir_unop_f2i64:
415 assert(ir->operands[0]->type->is_float());
416 assert(ir->type->base_type == GLSL_TYPE_INT64);
417 break;
418 case ir_unop_d2i64:
419 assert(ir->operands[0]->type->is_double());
420 assert(ir->type->base_type == GLSL_TYPE_INT64);
421 break;
422 case ir_unop_i2u64:
423 assert(ir->operands[0]->type->is_int_16_32());
424 assert(ir->type->base_type == GLSL_TYPE_UINT64);
425 break;
426 case ir_unop_u2u64:
427 assert(ir->operands[0]->type->is_uint_16_32());
428 assert(ir->type->base_type == GLSL_TYPE_UINT64);
429 break;
430 case ir_unop_f2u64:
431 assert(ir->operands[0]->type->is_float());
432 assert(ir->type->base_type == GLSL_TYPE_UINT64);
433 break;
434 case ir_unop_d2u64:
435 assert(ir->operands[0]->type->is_double());
436 assert(ir->type->base_type == GLSL_TYPE_UINT64);
437 break;
438 case ir_unop_u642i64:
439 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
440 assert(ir->type->base_type == GLSL_TYPE_INT64);
441 break;
442 case ir_unop_i642u64:
443 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
444 assert(ir->type->base_type == GLSL_TYPE_UINT64);
445 break;
446 case ir_unop_trunc:
447 case ir_unop_round_even:
448 case ir_unop_ceil:
449 case ir_unop_floor:
450 case ir_unop_fract:
451 assert(ir->operands[0]->type->is_float_16_32_64());
452 assert(ir->operands[0]->type == ir->type);
453 break;
454 case ir_unop_sin:
455 case ir_unop_cos:
456 case ir_unop_dFdx:
457 case ir_unop_dFdx_coarse:
458 case ir_unop_dFdx_fine:
459 case ir_unop_dFdy:
460 case ir_unop_dFdy_coarse:
461 case ir_unop_dFdy_fine:
462 assert(ir->operands[0]->type->is_float_16_32());
463 assert(ir->operands[0]->type == ir->type);
464 break;
465
466 case ir_unop_pack_snorm_2x16:
467 case ir_unop_pack_unorm_2x16:
468 case ir_unop_pack_half_2x16:
469 assert(ir->type == glsl_type::uint_type);
470 assert(ir->operands[0]->type == glsl_type::vec2_type);
471 break;
472
473 case ir_unop_pack_snorm_4x8:
474 case ir_unop_pack_unorm_4x8:
475 assert(ir->type == glsl_type::uint_type);
476 assert(ir->operands[0]->type == glsl_type::vec4_type);
477 break;
478
479 case ir_unop_pack_double_2x32:
480 assert(ir->type == glsl_type::double_type);
481 assert(ir->operands[0]->type == glsl_type::uvec2_type);
482 break;
483
484 case ir_unop_pack_int_2x32:
485 assert(ir->type == glsl_type::int64_t_type);
486 assert(ir->operands[0]->type == glsl_type::ivec2_type);
487 break;
488
489 case ir_unop_pack_uint_2x32:
490 assert(ir->type == glsl_type::uint64_t_type);
491 assert(ir->operands[0]->type == glsl_type::uvec2_type);
492 break;
493
494 case ir_unop_pack_sampler_2x32:
495 assert(ir->type->is_sampler());
496 assert(ir->operands[0]->type == glsl_type::uvec2_type);
497 break;
498
499 case ir_unop_pack_image_2x32:
500 assert(ir->type->is_image());
501 assert(ir->operands[0]->type == glsl_type::uvec2_type);
502 break;
503
504 case ir_unop_unpack_snorm_2x16:
505 case ir_unop_unpack_unorm_2x16:
506 case ir_unop_unpack_half_2x16:
507 assert(ir->type == glsl_type::vec2_type);
508 assert(ir->operands[0]->type == glsl_type::uint_type);
509 break;
510
511 case ir_unop_unpack_snorm_4x8:
512 case ir_unop_unpack_unorm_4x8:
513 assert(ir->type == glsl_type::vec4_type);
514 assert(ir->operands[0]->type == glsl_type::uint_type);
515 break;
516
517 case ir_unop_unpack_double_2x32:
518 assert(ir->type == glsl_type::uvec2_type);
519 assert(ir->operands[0]->type == glsl_type::double_type);
520 break;
521
522 case ir_unop_unpack_int_2x32:
523 assert(ir->type == glsl_type::ivec2_type);
524 assert(ir->operands[0]->type == glsl_type::int64_t_type);
525 break;
526
527 case ir_unop_unpack_uint_2x32:
528 assert(ir->type == glsl_type::uvec2_type);
529 assert(ir->operands[0]->type == glsl_type::uint64_t_type);
530 break;
531
532 case ir_unop_unpack_sampler_2x32:
533 assert(ir->type == glsl_type::uvec2_type);
534 assert(ir->operands[0]->type->is_sampler());
535 break;
536
537 case ir_unop_unpack_image_2x32:
538 assert(ir->type == glsl_type::uvec2_type);
539 assert(ir->operands[0]->type->is_image());
540 break;
541
542 case ir_unop_bitfield_reverse:
543 assert(ir->operands[0]->type == ir->type);
544 assert(ir->type->is_integer_16_32());
545 break;
546
547 case ir_unop_bit_count:
548 case ir_unop_find_msb:
549 case ir_unop_find_lsb:
550 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
551 assert(ir->operands[0]->type->is_integer_16_32());
552 assert(ir->type->is_int_16_32());
553 break;
554
555 case ir_unop_clz:
556 assert(ir->operands[0]->type == ir->type);
557 assert(ir->type->is_uint_16_32());
558 break;
559
560 case ir_unop_interpolate_at_centroid:
561 assert(ir->operands[0]->type == ir->type);
562 assert(ir->operands[0]->type->is_float_16_32());
563 break;
564
565 case ir_unop_get_buffer_size:
566 assert(ir->type == glsl_type::int_type);
567 assert(ir->operands[0]->type == glsl_type::uint_type);
568 break;
569
570 case ir_unop_ssbo_unsized_array_length:
571 assert(ir->type == glsl_type::int_type);
572 assert(ir->operands[0]->type->is_array());
573 assert(ir->operands[0]->type->is_unsized_array());
574 break;
575
576 case ir_unop_d2f:
577 assert(ir->operands[0]->type->is_double());
578 assert(ir->type->is_float());
579 break;
580 case ir_unop_f2d:
581 assert(ir->operands[0]->type->is_float());
582 assert(ir->type->is_double());
583 break;
584 case ir_unop_f162f:
585 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
586 assert(ir->type->is_float());
587 break;
588 case ir_unop_f2f16:
589 case ir_unop_f2fmp:
590 assert(ir->operands[0]->type->is_float());
591 assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
592 break;
593 case ir_unop_i2i:
594 assert(ir->operands[0]->type->is_int_16_32());
595 assert(ir->type->is_int_16_32());
596 assert(ir->type->base_type != ir->operands[0]->type->base_type);
597 break;
598 case ir_unop_u2u:
599 assert(ir->operands[0]->type->is_uint_16_32());
600 assert(ir->type->is_uint_16_32());
601 assert(ir->type->base_type != ir->operands[0]->type->base_type);
602 break;
603 case ir_unop_i2imp:
604 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
605 assert(ir->type->base_type == GLSL_TYPE_INT16);
606 break;
607 case ir_unop_u2ump:
608 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
609 assert(ir->type->base_type == GLSL_TYPE_UINT16);
610 break;
611 case ir_unop_d2i:
612 assert(ir->operands[0]->type->is_double());
613 assert(ir->type->is_int_16_32());
614 break;
615 case ir_unop_i2d:
616 assert(ir->operands[0]->type->is_int_16_32());
617 assert(ir->type->is_double());
618 break;
619 case ir_unop_d2u:
620 assert(ir->operands[0]->type->is_double());
621 assert(ir->type->is_uint_16_32());
622 break;
623 case ir_unop_u2d:
624 assert(ir->operands[0]->type->is_uint_16_32());
625 assert(ir->type->is_double());
626 break;
627 case ir_unop_d2b:
628 assert(ir->operands[0]->type->is_double());
629 assert(ir->type->is_boolean());
630 break;
631
632 case ir_unop_frexp_sig:
633 assert(ir->operands[0]->type->is_float_16_32_64());
634 assert(ir->type->is_double());
635 break;
636 case ir_unop_frexp_exp:
637 assert(ir->operands[0]->type->is_float_16_32_64());
638 assert(ir->type->is_int_16_32());
639 break;
640 case ir_unop_subroutine_to_int:
641 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
642 assert(ir->type->base_type == GLSL_TYPE_INT);
643 break;
644
645 case ir_unop_atan:
646 assert(ir->operands[0]->type->is_float_16_32_64());
647 assert(ir->type == ir->operands[0]->type);
648 break;
649
650 case ir_binop_add:
651 case ir_binop_sub:
652 case ir_binop_mul:
653 case ir_binop_div:
654 case ir_binop_mod:
655 case ir_binop_min:
656 case ir_binop_max:
657 case ir_binop_pow:
658 assert(ir->operands[0]->type->base_type ==
659 ir->operands[1]->type->base_type);
660
661 if (ir->operation == ir_binop_mul &&
662 (ir->type->base_type == GLSL_TYPE_UINT64 ||
663 ir->type->base_type == GLSL_TYPE_INT64) &&
664 (ir->operands[0]->type->is_int_16_32()||
665 ir->operands[1]->type->is_int_16_32()||
666 ir->operands[0]->type->is_uint_16_32() ||
667 ir->operands[1]->type->is_uint_16_32())) {
668 assert(ir->operands[0]->type == ir->operands[1]->type);
669 break;
670 }
671
672 if (ir->operands[0]->type->is_scalar())
673 assert(ir->operands[1]->type == ir->type);
674 else if (ir->operands[1]->type->is_scalar())
675 assert(ir->operands[0]->type == ir->type);
676 else if (ir->operands[0]->type->is_vector() &&
677 ir->operands[1]->type->is_vector()) {
678 assert(ir->operands[0]->type == ir->operands[1]->type);
679 assert(ir->operands[0]->type == ir->type);
680 }
681 break;
682
683 case ir_binop_abs_sub:
684 assert(ir->operands[0]->type == ir->operands[1]->type);
685 assert(ir->operands[0]->type->is_integer_16_32_64());
686 assert(ir->operands[0]->type->vector_elements ==
687 ir->type->vector_elements);
688 assert(ir->type->is_uint_16_32_64());
689 break;
690
691 case ir_binop_add_sat:
692 case ir_binop_sub_sat:
693 case ir_binop_avg:
694 case ir_binop_avg_round:
695 assert(ir->type == ir->operands[0]->type);
696 assert(ir->type == ir->operands[1]->type);
697 assert(ir->type->is_integer_16_32_64());
698 break;
699
700 case ir_binop_mul_32x16:
701 case ir_binop_imul_high:
702 assert(ir->type == ir->operands[0]->type);
703 assert(ir->type == ir->operands[1]->type);
704 assert(ir->type->is_integer_32());
705 break;
706
707 case ir_binop_carry:
708 case ir_binop_borrow:
709 assert(ir->type == ir->operands[0]->type);
710 assert(ir->type == ir->operands[1]->type);
711 assert(ir->type->base_type == GLSL_TYPE_UINT);
712 break;
713
714 case ir_binop_less:
715 case ir_binop_gequal:
716 case ir_binop_equal:
717 case ir_binop_nequal:
718 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
719 * ==, and != operators. The IR operators perform a component-wise
720 * comparison on scalar or vector types and return a boolean scalar or
721 * vector type of the same size.
722 */
723 assert(ir->type->is_boolean());
724 assert(ir->operands[0]->type == ir->operands[1]->type);
725 assert(ir->operands[0]->type->is_vector()
726 || ir->operands[0]->type->is_scalar());
727 assert(ir->operands[0]->type->vector_elements
728 == ir->type->vector_elements);
729 break;
730
731 case ir_binop_all_equal:
732 case ir_binop_any_nequal:
733 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
734 * return a scalar boolean. The IR matches that.
735 */
736 assert(ir->type == glsl_type::bool_type);
737 assert(ir->operands[0]->type == ir->operands[1]->type);
738 break;
739
740 case ir_binop_lshift:
741 case ir_binop_rshift:
742 assert(ir->operands[0]->type->is_integer_16_32_64() &&
743 ir->operands[1]->type->is_integer_16_32());
744 if (ir->operands[0]->type->is_scalar()) {
745 assert(ir->operands[1]->type->is_scalar());
746 }
747 if (ir->operands[0]->type->is_vector() &&
748 ir->operands[1]->type->is_vector()) {
749 assert(ir->operands[0]->type->components() ==
750 ir->operands[1]->type->components());
751 }
752 assert(ir->type == ir->operands[0]->type);
753 break;
754
755 case ir_binop_bit_and:
756 case ir_binop_bit_xor:
757 case ir_binop_bit_or:
758 assert(ir->operands[0]->type->base_type ==
759 ir->operands[1]->type->base_type);
760 assert(ir->type->is_integer_16_32_64());
761 if (ir->operands[0]->type->is_vector() &&
762 ir->operands[1]->type->is_vector()) {
763 assert(ir->operands[0]->type->vector_elements ==
764 ir->operands[1]->type->vector_elements);
765 }
766 break;
767
768 case ir_binop_logic_and:
769 case ir_binop_logic_xor:
770 case ir_binop_logic_or:
771 assert(ir->type->is_boolean());
772 assert(ir->operands[0]->type->is_boolean());
773 assert(ir->operands[1]->type->is_boolean());
774 break;
775
776 case ir_binop_dot:
777 assert(ir->type == glsl_type::float_type ||
778 ir->type == glsl_type::double_type ||
779 ir->type == glsl_type::float16_t_type);
780 assert(ir->operands[0]->type->is_float_16_32_64());
781 assert(ir->operands[0]->type->is_vector());
782 assert(ir->operands[0]->type == ir->operands[1]->type);
783 break;
784
785 case ir_binop_ubo_load:
786 assert(ir->operands[0]->type == glsl_type::uint_type);
787
788 assert(ir->operands[1]->type == glsl_type::uint_type);
789 break;
790
791 case ir_binop_ldexp:
792 assert(ir->operands[0]->type == ir->type);
793 assert(ir->operands[0]->type->is_float_16_32_64());
794 assert(ir->operands[1]->type->is_int_16_32());
795 assert(ir->operands[0]->type->components() ==
796 ir->operands[1]->type->components());
797 break;
798
799 case ir_binop_vector_extract:
800 assert(ir->operands[0]->type->is_vector());
801 assert(ir->operands[1]->type->is_scalar()
802 && ir->operands[1]->type->is_integer_16_32());
803 break;
804
805 case ir_binop_interpolate_at_offset:
806 assert(ir->operands[0]->type == ir->type);
807 assert(ir->operands[0]->type->is_float_16_32());
808 assert(ir->operands[1]->type->components() == 2);
809 assert(ir->operands[1]->type->is_float_16_32());
810 break;
811
812 case ir_binop_interpolate_at_sample:
813 assert(ir->operands[0]->type == ir->type);
814 assert(ir->operands[0]->type->is_float_16_32());
815 assert(ir->operands[1]->type == glsl_type::int_type ||
816 ir->operands[1]->type == glsl_type::int16_t_type);
817 break;
818
819 case ir_binop_atan2:
820 assert(ir->operands[0]->type->is_float_16_32_64());
821 assert(ir->operands[1]->type == ir->operands[0]->type);
822 assert(ir->type == ir->operands[0]->type);
823 break;
824
825 case ir_triop_fma:
826 assert(ir->type->is_float_16_32_64());
827 assert(ir->type == ir->operands[0]->type);
828 assert(ir->type == ir->operands[1]->type);
829 assert(ir->type == ir->operands[2]->type);
830 break;
831
832 case ir_triop_lrp:
833 assert(ir->operands[0]->type->is_float_16_32_64());
834 assert(ir->operands[0]->type == ir->operands[1]->type);
835 assert(ir->operands[2]->type == ir->operands[0]->type ||
836 ir->operands[2]->type == glsl_type::float_type ||
837 ir->operands[2]->type == glsl_type::double_type ||
838 ir->operands[2]->type == glsl_type::float16_t_type);
839 break;
840
841 case ir_triop_csel:
842 assert(ir->operands[0]->type->is_boolean());
843 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
844 assert(ir->type == ir->operands[1]->type);
845 assert(ir->type == ir->operands[2]->type);
846 break;
847
848 case ir_triop_bitfield_extract:
849 assert(ir->type->is_integer_16_32());
850 assert(ir->operands[0]->type == ir->type);
851 assert(ir->operands[1]->type == ir->type);
852 assert(ir->operands[2]->type == ir->type);
853 break;
854
855 case ir_triop_vector_insert:
856 assert(ir->operands[0]->type->is_vector());
857 assert(ir->operands[1]->type->is_scalar());
858 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
859 assert(ir->operands[2]->type->is_scalar()
860 && ir->operands[2]->type->is_integer_16_32());
861 assert(ir->type == ir->operands[0]->type);
862 break;
863
864 case ir_quadop_bitfield_insert:
865 assert(ir->type->is_integer_16_32());
866 assert(ir->operands[0]->type == ir->type);
867 assert(ir->operands[1]->type == ir->type);
868 assert(ir->operands[2]->type == ir->type);
869 assert(ir->operands[3]->type == ir->type);
870 break;
871
872 case ir_quadop_vector:
873 /* The vector operator collects some number of scalars and generates a
874 * vector from them.
875 *
876 * - All of the operands must be scalar.
877 * - Number of operands must matche the size of the resulting vector.
878 * - Base type of the operands must match the base type of the result.
879 */
880 assert(ir->type->is_vector());
881 switch (ir->type->vector_elements) {
882 case 2:
883 assert(ir->operands[0]->type->is_scalar());
884 assert(ir->operands[0]->type->base_type == ir->type->base_type);
885 assert(ir->operands[1]->type->is_scalar());
886 assert(ir->operands[1]->type->base_type == ir->type->base_type);
887 assert(ir->operands[2] == NULL);
888 assert(ir->operands[3] == NULL);
889 break;
890 case 3:
891 assert(ir->operands[0]->type->is_scalar());
892 assert(ir->operands[0]->type->base_type == ir->type->base_type);
893 assert(ir->operands[1]->type->is_scalar());
894 assert(ir->operands[1]->type->base_type == ir->type->base_type);
895 assert(ir->operands[2]->type->is_scalar());
896 assert(ir->operands[2]->type->base_type == ir->type->base_type);
897 assert(ir->operands[3] == NULL);
898 break;
899 case 4:
900 assert(ir->operands[0]->type->is_scalar());
901 assert(ir->operands[0]->type->base_type == ir->type->base_type);
902 assert(ir->operands[1]->type->is_scalar());
903 assert(ir->operands[1]->type->base_type == ir->type->base_type);
904 assert(ir->operands[2]->type->is_scalar());
905 assert(ir->operands[2]->type->base_type == ir->type->base_type);
906 assert(ir->operands[3]->type->is_scalar());
907 assert(ir->operands[3]->type->base_type == ir->type->base_type);
908 break;
909 default:
910 /* The is_vector assertion above should prevent execution from ever
911 * getting here.
912 */
913 assert(!"Should not get here.");
914 break;
915 }
916 }
917
918 return visit_continue;
919 }
920
921 ir_visitor_status
922 ir_validate::visit_leave(ir_swizzle *ir)
923 {
924 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
925
926 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
927 if (chans[i] >= ir->val->type->vector_elements) {
928 printf("ir_swizzle @ %p specifies a channel not present "
929 "in the value.\n", (void *) ir);
930 ir->print();
931 abort();
932 }
933 }
934
935 return visit_continue;
936 }
937
938 ir_visitor_status
939 ir_validate::visit(ir_variable *ir)
940 {
941 /* An ir_variable is the one thing that can (and will) appear multiple times
942 * in an IR tree. It is added to the hashtable so that it can be used
943 * in the ir_dereference_variable handler to ensure that a variable is
944 * declared before it is dereferenced.
945 */
946 if (ir->name && ir->is_name_ralloced())
947 assert(ralloc_parent(ir->name) == ir);
948
949 _mesa_set_add(ir_set, ir);
950
951 /* If a variable is an array, verify that the maximum array index is in
952 * bounds. There was once an error in AST-to-HIR conversion that set this
953 * to be out of bounds.
954 */
955 if (ir->type->array_size() > 0) {
956 if (ir->data.max_array_access >= (int)ir->type->length) {
957 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
958 ir->data.max_array_access, ir->type->length - 1);
959 ir->print();
960 abort();
961 }
962 }
963
964 /* If a variable is an interface block (or an array of interface blocks),
965 * verify that the maximum array index for each interface member is in
966 * bounds.
967 */
968 if (ir->is_interface_instance()) {
969 const glsl_struct_field *fields =
970 ir->get_interface_type()->fields.structure;
971 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
972 if (fields[i].type->array_size() > 0 &&
973 !fields[i].implicit_sized_array) {
974 const int *const max_ifc_array_access =
975 ir->get_max_ifc_array_access();
976
977 assert(max_ifc_array_access != NULL);
978
979 if (max_ifc_array_access[i] >= (int)fields[i].type->length) {
980 printf("ir_variable has maximum access out of bounds for "
981 "field %s (%d vs %d)\n", fields[i].name,
982 max_ifc_array_access[i], fields[i].type->length);
983 ir->print();
984 abort();
985 }
986 }
987 }
988 }
989
990 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
991 printf("ir_variable didn't have an initializer, but has a constant "
992 "initializer value.\n");
993 ir->print();
994 abort();
995 }
996
997 if (ir->data.mode == ir_var_uniform
998 && is_gl_identifier(ir->name)
999 && ir->get_state_slots() == NULL) {
1000 printf("built-in uniform has no state\n");
1001 ir->print();
1002 abort();
1003 }
1004
1005 return visit_continue;
1006 }
1007
1008 ir_visitor_status
1009 ir_validate::visit_enter(ir_assignment *ir)
1010 {
1011 const ir_dereference *const lhs = ir->lhs;
1012 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
1013 if (ir->write_mask == 0) {
1014 printf("Assignment LHS is %s, but write mask is 0:\n",
1015 lhs->type->is_scalar() ? "scalar" : "vector");
1016 ir->print();
1017 abort();
1018 }
1019
1020 int lhs_components = 0;
1021 for (int i = 0; i < 4; i++) {
1022 if (ir->write_mask & (1 << i))
1023 lhs_components++;
1024 }
1025
1026 if (lhs_components != ir->rhs->type->vector_elements) {
1027 printf("Assignment count of LHS write mask channels enabled not\n"
1028 "matching RHS vector size (%d LHS, %d RHS).\n",
1029 lhs_components, ir->rhs->type->vector_elements);
1030 ir->print();
1031 abort();
1032 }
1033 }
1034
1035 this->validate_ir(ir, this->data_enter);
1036
1037 return visit_continue;
1038 }
1039
1040 ir_visitor_status
1041 ir_validate::visit_enter(ir_call *ir)
1042 {
1043 ir_function_signature *const callee = ir->callee;
1044
1045 if (callee->ir_type != ir_type_function_signature) {
1046 printf("IR called by ir_call is not ir_function_signature!\n");
1047 abort();
1048 }
1049
1050 if (ir->return_deref) {
1051 if (ir->return_deref->type != callee->return_type) {
1052 printf("callee type %s does not match return storage type %s\n",
1053 callee->return_type->name, ir->return_deref->type->name);
1054 abort();
1055 }
1056 } else if (callee->return_type != glsl_type::void_type) {
1057 printf("ir_call has non-void callee but no return storage\n");
1058 abort();
1059 }
1060
1061 const exec_node *formal_param_node = callee->parameters.get_head_raw();
1062 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
1063 while (true) {
1064 if (formal_param_node->is_tail_sentinel()
1065 != actual_param_node->is_tail_sentinel()) {
1066 printf("ir_call has the wrong number of parameters:\n");
1067 goto dump_ir;
1068 }
1069 if (formal_param_node->is_tail_sentinel()) {
1070 break;
1071 }
1072 const ir_variable *formal_param
1073 = (const ir_variable *) formal_param_node;
1074 const ir_rvalue *actual_param
1075 = (const ir_rvalue *) actual_param_node;
1076 if (formal_param->type != actual_param->type) {
1077 printf("ir_call parameter type mismatch:\n");
1078 goto dump_ir;
1079 }
1080 if (formal_param->data.mode == ir_var_function_out
1081 || formal_param->data.mode == ir_var_function_inout) {
1082 if (!actual_param->is_lvalue()) {
1083 printf("ir_call out/inout parameters must be lvalues:\n");
1084 goto dump_ir;
1085 }
1086 }
1087 formal_param_node = formal_param_node->next;
1088 actual_param_node = actual_param_node->next;
1089 }
1090
1091 return visit_continue;
1092
1093 dump_ir:
1094 ir->print();
1095 printf("callee:\n");
1096 callee->print();
1097 abort();
1098 return visit_stop;
1099 }
1100
1101 void
1102 ir_validate::validate_ir(ir_instruction *ir, void *data)
1103 {
1104 struct set *ir_set = (struct set *) data;
1105
1106 if (_mesa_set_search(ir_set, ir)) {
1107 printf("Instruction node present twice in ir tree:\n");
1108 ir->print();
1109 printf("\n");
1110 abort();
1111 }
1112 _mesa_set_add(ir_set, ir);
1113 }
1114
1115 static void
1116 check_node_type(ir_instruction *ir, void *data)
1117 {
1118 (void) data;
1119
1120 if (ir->ir_type >= ir_type_max) {
1121 printf("Instruction node with unset type\n");
1122 ir->print(); printf("\n");
1123 }
1124 ir_rvalue *value = ir->as_rvalue();
1125 if (value != NULL)
1126 assert(value->type != glsl_type::error_type);
1127 }
1128
1129 void
1130 validate_ir_tree(exec_list *instructions)
1131 {
1132 /* We shouldn't have any reason to validate IR in a release build,
1133 * and it's half composed of assert()s anyway which wouldn't do
1134 * anything.
1135 */
1136 #ifndef DEBUG
1137 if (!env_var_as_boolean("GLSL_VALIDATE", false))
1138 return;
1139 #endif
1140 ir_validate v;
1141
1142 v.run(instructions);
1143
1144 foreach_in_list(ir_instruction, ir, instructions) {
1145 visit_tree(ir, check_node_type, NULL);
1146 }
1147 }