0024fd764d16d42f8e4afeb822e3f0b8d8d178d2
[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/hash_table.h"
39 #include "util/macros.h"
40 #include "util/set.h"
41 #include "compiler/glsl_types.h"
42
43 namespace {
44
45 class ir_validate : public ir_hierarchical_visitor {
46 public:
47 ir_validate()
48 {
49 this->ir_set = _mesa_pointer_set_create(NULL);
50
51 this->current_function = NULL;
52
53 this->callback_enter = ir_validate::validate_ir;
54 this->data_enter = ir_set;
55 }
56
57 ~ir_validate()
58 {
59 _mesa_set_destroy(this->ir_set, NULL);
60 }
61
62 virtual ir_visitor_status visit(ir_variable *v);
63 virtual ir_visitor_status visit(ir_dereference_variable *ir);
64
65 virtual ir_visitor_status visit_enter(ir_discard *ir);
66 virtual ir_visitor_status visit_enter(ir_if *ir);
67
68 virtual ir_visitor_status visit_enter(ir_function *ir);
69 virtual ir_visitor_status visit_leave(ir_function *ir);
70 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
71
72 virtual ir_visitor_status visit_leave(ir_expression *ir);
73 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
74
75 virtual ir_visitor_status visit_enter(class ir_dereference_array *);
76
77 virtual ir_visitor_status visit_enter(ir_assignment *ir);
78 virtual ir_visitor_status visit_enter(ir_call *ir);
79
80 static void validate_ir(ir_instruction *ir, void *data);
81
82 ir_function *current_function;
83
84 struct set *ir_set;
85 };
86
87 } /* anonymous namespace */
88
89 ir_visitor_status
90 ir_validate::visit(ir_dereference_variable *ir)
91 {
92 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
93 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
94 (void *) ir, (void *) ir->var);
95 abort();
96 }
97
98 if (_mesa_set_search(ir_set, ir->var) == NULL) {
99 printf("ir_dereference_variable @ %p specifies undeclared variable "
100 "`%s' @ %p\n",
101 (void *) ir, ir->var->name, (void *) ir->var);
102 abort();
103 }
104
105 this->validate_ir(ir, this->data_enter);
106
107 return visit_continue;
108 }
109
110 ir_visitor_status
111 ir_validate::visit_enter(class ir_dereference_array *ir)
112 {
113 if (!ir->array->type->is_array() && !ir->array->type->is_matrix() &&
114 !ir->array->type->is_vector()) {
115 printf("ir_dereference_array @ %p does not specify an array, a vector "
116 "or a matrix\n",
117 (void *) ir);
118 ir->print();
119 printf("\n");
120 abort();
121 }
122
123 if (!ir->array_index->type->is_scalar()) {
124 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
125 (void *) ir, ir->array_index->type->name);
126 abort();
127 }
128
129 if (!ir->array_index->type->is_integer_32()) {
130 printf("ir_dereference_array @ %p does not have integer index: %s\n",
131 (void *) ir, ir->array_index->type->name);
132 abort();
133 }
134
135 return visit_continue;
136 }
137
138 ir_visitor_status
139 ir_validate::visit_enter(ir_discard *ir)
140 {
141 if (ir->condition && ir->condition->type != glsl_type::bool_type) {
142 printf("ir_discard condition %s type instead of bool.\n",
143 ir->condition->type->name);
144 ir->print();
145 printf("\n");
146 abort();
147 }
148
149 return visit_continue;
150 }
151
152 ir_visitor_status
153 ir_validate::visit_enter(ir_if *ir)
154 {
155 if (ir->condition->type != glsl_type::bool_type) {
156 printf("ir_if condition %s type instead of bool.\n",
157 ir->condition->type->name);
158 ir->print();
159 printf("\n");
160 abort();
161 }
162
163 return visit_continue;
164 }
165
166
167 ir_visitor_status
168 ir_validate::visit_enter(ir_function *ir)
169 {
170 /* Function definitions cannot be nested.
171 */
172 if (this->current_function != NULL) {
173 printf("Function definition nested inside another function "
174 "definition:\n");
175 printf("%s %p inside %s %p\n",
176 ir->name, (void *) ir,
177 this->current_function->name, (void *) this->current_function);
178 abort();
179 }
180
181 /* Store the current function hierarchy being traversed. This is used
182 * by the function signature visitor to ensure that the signatures are
183 * linked with the correct functions.
184 */
185 this->current_function = ir;
186
187 this->validate_ir(ir, this->data_enter);
188
189 /* Verify that all of the things stored in the list of signatures are,
190 * in fact, function signatures.
191 */
192 foreach_in_list(ir_instruction, sig, &ir->signatures) {
193 if (sig->ir_type != ir_type_function_signature) {
194 printf("Non-signature in signature list of function `%s'\n",
195 ir->name);
196 abort();
197 }
198 }
199
200 return visit_continue;
201 }
202
203 ir_visitor_status
204 ir_validate::visit_leave(ir_function *ir)
205 {
206 assert(ralloc_parent(ir->name) == ir);
207
208 this->current_function = NULL;
209 return visit_continue;
210 }
211
212 ir_visitor_status
213 ir_validate::visit_enter(ir_function_signature *ir)
214 {
215 if (this->current_function != ir->function()) {
216 printf("Function signature nested inside wrong function "
217 "definition:\n");
218 printf("%p inside %s %p instead of %s %p\n",
219 (void *) ir,
220 this->current_function->name, (void *) this->current_function,
221 ir->function_name(), (void *) ir->function());
222 abort();
223 }
224
225 if (ir->return_type == NULL) {
226 printf("Function signature %p for function %s has NULL return type.\n",
227 (void *) ir, ir->function_name());
228 abort();
229 }
230
231 this->validate_ir(ir, this->data_enter);
232
233 return visit_continue;
234 }
235
236 ir_visitor_status
237 ir_validate::visit_leave(ir_expression *ir)
238 {
239 for (unsigned i = ir->num_operands; i < 4; i++) {
240 assert(ir->operands[i] == NULL);
241 }
242
243 for (unsigned i = 0; i < ir->num_operands; i++) {
244 assert(ir->operands[i] != NULL);
245 }
246
247 switch (ir->operation) {
248 case ir_unop_bit_not:
249 assert(ir->operands[0]->type == ir->type);
250 break;
251 case ir_unop_logic_not:
252 assert(ir->type->is_boolean());
253 assert(ir->operands[0]->type->is_boolean());
254 break;
255
256 case ir_unop_neg:
257 assert(ir->type == ir->operands[0]->type);
258 break;
259
260 case ir_unop_abs:
261 case ir_unop_sign:
262 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT ||
263 ir->operands[0]->type->is_float_16_32_64() ||
264 ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
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());
286 assert(ir->type->base_type == GLSL_TYPE_INT);
287 break;
288 case ir_unop_f2u:
289 assert(ir->operands[0]->type->is_float());
290 assert(ir->type->base_type == GLSL_TYPE_UINT);
291 break;
292 case ir_unop_i2f:
293 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
294 assert(ir->type->is_float());
295 break;
296 case ir_unop_f2b:
297 assert(ir->operands[0]->type->is_float());
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());
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->base_type == GLSL_TYPE_INT);
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->base_type == GLSL_TYPE_INT);
320 break;
321 case ir_unop_u2f:
322 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
323 assert(ir->type->is_float());
324 break;
325 case ir_unop_i2u:
326 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
327 assert(ir->type->base_type == GLSL_TYPE_UINT);
328 break;
329 case ir_unop_u2i:
330 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
331 assert(ir->type->base_type == GLSL_TYPE_INT);
332 break;
333 case ir_unop_bitcast_i2f:
334 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
335 assert(ir->type->is_float());
336 break;
337 case ir_unop_bitcast_f2i:
338 assert(ir->operands[0]->type->is_float());
339 assert(ir->type->base_type == GLSL_TYPE_INT);
340 break;
341 case ir_unop_bitcast_u2f:
342 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
343 assert(ir->type->is_float());
344 break;
345 case ir_unop_bitcast_f2u:
346 assert(ir->operands[0]->type->is_float());
347 assert(ir->type->base_type == GLSL_TYPE_UINT);
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->base_type == GLSL_TYPE_INT);
369 break;
370 case ir_unop_u642i:
371 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
372 assert(ir->type->base_type == GLSL_TYPE_INT);
373 break;
374 case ir_unop_i642u:
375 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
376 assert(ir->type->base_type == GLSL_TYPE_UINT);
377 break;
378 case ir_unop_u642u:
379 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
380 assert(ir->type->base_type == GLSL_TYPE_UINT);
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->base_type == GLSL_TYPE_INT);
404 assert(ir->type->base_type == GLSL_TYPE_INT64);
405 break;
406 case ir_unop_u2i64:
407 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
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->base_type == GLSL_TYPE_INT);
424 assert(ir->type->base_type == GLSL_TYPE_UINT64);
425 break;
426 case ir_unop_u2u64:
427 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
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_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_32());
552 assert(ir->type->base_type == GLSL_TYPE_INT);
553 break;
554
555 case ir_unop_clz:
556 assert(ir->operands[0]->type == ir->type);
557 assert(ir->type->base_type == GLSL_TYPE_UINT);
558 break;
559
560 case ir_unop_noise:
561 /* XXX what can we assert here? */
562 break;
563
564 case ir_unop_interpolate_at_centroid:
565 assert(ir->operands[0]->type == ir->type);
566 assert(ir->operands[0]->type->is_float_16_32());
567 break;
568
569 case ir_unop_get_buffer_size:
570 assert(ir->type == glsl_type::int_type);
571 assert(ir->operands[0]->type == glsl_type::uint_type);
572 break;
573
574 case ir_unop_ssbo_unsized_array_length:
575 assert(ir->type == glsl_type::int_type);
576 assert(ir->operands[0]->type->is_array());
577 assert(ir->operands[0]->type->is_unsized_array());
578 break;
579
580 case ir_unop_d2f:
581 assert(ir->operands[0]->type->is_double());
582 assert(ir->type->is_float());
583 break;
584 case ir_unop_f2d:
585 assert(ir->operands[0]->type->is_float());
586 assert(ir->type->is_double());
587 break;
588 case ir_unop_f162f:
589 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
590 assert(ir->type->is_float());
591 break;
592 case ir_unop_f2f16:
593 case ir_unop_f2fmp:
594 assert(ir->operands[0]->type->is_float());
595 assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
596 break;
597 case ir_unop_d2i:
598 assert(ir->operands[0]->type->is_double());
599 assert(ir->type->base_type == GLSL_TYPE_INT);
600 break;
601 case ir_unop_i2d:
602 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
603 assert(ir->type->is_double());
604 break;
605 case ir_unop_d2u:
606 assert(ir->operands[0]->type->is_double());
607 assert(ir->type->base_type == GLSL_TYPE_UINT);
608 break;
609 case ir_unop_u2d:
610 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
611 assert(ir->type->is_double());
612 break;
613 case ir_unop_d2b:
614 assert(ir->operands[0]->type->is_double());
615 assert(ir->type->is_boolean());
616 break;
617
618 case ir_unop_frexp_sig:
619 assert(ir->operands[0]->type->is_float_16_32_64());
620 assert(ir->type->is_double());
621 break;
622 case ir_unop_frexp_exp:
623 assert(ir->operands[0]->type->is_float_16_32_64());
624 assert(ir->type->base_type == GLSL_TYPE_INT);
625 break;
626 case ir_unop_subroutine_to_int:
627 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
628 assert(ir->type->base_type == GLSL_TYPE_INT);
629 break;
630
631 case ir_unop_atan:
632 assert(ir->operands[0]->type->is_float_16_32_64());
633 assert(ir->type == ir->operands[0]->type);
634 break;
635
636 case ir_binop_add:
637 case ir_binop_sub:
638 case ir_binop_mul:
639 case ir_binop_div:
640 case ir_binop_mod:
641 case ir_binop_min:
642 case ir_binop_max:
643 case ir_binop_pow:
644 assert(ir->operands[0]->type->base_type ==
645 ir->operands[1]->type->base_type);
646
647 if (ir->operation == ir_binop_mul &&
648 (ir->type->base_type == GLSL_TYPE_UINT64 ||
649 ir->type->base_type == GLSL_TYPE_INT64) &&
650 (ir->operands[0]->type->base_type == GLSL_TYPE_INT ||
651 ir->operands[1]->type->base_type == GLSL_TYPE_INT ||
652 ir->operands[0]->type->base_type == GLSL_TYPE_UINT ||
653 ir->operands[1]->type->base_type == GLSL_TYPE_UINT)) {
654 assert(ir->operands[0]->type == ir->operands[1]->type);
655 break;
656 }
657
658 if (ir->operands[0]->type->is_scalar())
659 assert(ir->operands[1]->type == ir->type);
660 else if (ir->operands[1]->type->is_scalar())
661 assert(ir->operands[0]->type == ir->type);
662 else if (ir->operands[0]->type->is_vector() &&
663 ir->operands[1]->type->is_vector()) {
664 assert(ir->operands[0]->type == ir->operands[1]->type);
665 assert(ir->operands[0]->type == ir->type);
666 }
667 break;
668
669 case ir_binop_abs_sub:
670 assert(ir->operands[0]->type == ir->operands[1]->type);
671 assert(ir->operands[0]->type->is_integer_32_64());
672 assert(ir->operands[0]->type->vector_elements ==
673 ir->type->vector_elements);
674 assert(ir->type->base_type == GLSL_TYPE_UINT ||
675 ir->type->base_type == GLSL_TYPE_UINT64);
676 break;
677
678 case ir_binop_add_sat:
679 case ir_binop_sub_sat:
680 case ir_binop_avg:
681 case ir_binop_avg_round:
682 assert(ir->type == ir->operands[0]->type);
683 assert(ir->type == ir->operands[1]->type);
684 assert(ir->type->is_integer_32_64());
685 break;
686
687 case ir_binop_mul_32x16:
688 case ir_binop_imul_high:
689 assert(ir->type == ir->operands[0]->type);
690 assert(ir->type == ir->operands[1]->type);
691 assert(ir->type->is_integer_32());
692 break;
693
694 case ir_binop_carry:
695 case ir_binop_borrow:
696 assert(ir->type == ir->operands[0]->type);
697 assert(ir->type == ir->operands[1]->type);
698 assert(ir->type->base_type == GLSL_TYPE_UINT);
699 break;
700
701 case ir_binop_less:
702 case ir_binop_gequal:
703 case ir_binop_equal:
704 case ir_binop_nequal:
705 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
706 * ==, and != operators. The IR operators perform a component-wise
707 * comparison on scalar or vector types and return a boolean scalar or
708 * vector type of the same size.
709 */
710 assert(ir->type->is_boolean());
711 assert(ir->operands[0]->type == ir->operands[1]->type);
712 assert(ir->operands[0]->type->is_vector()
713 || ir->operands[0]->type->is_scalar());
714 assert(ir->operands[0]->type->vector_elements
715 == ir->type->vector_elements);
716 break;
717
718 case ir_binop_all_equal:
719 case ir_binop_any_nequal:
720 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
721 * return a scalar boolean. The IR matches that.
722 */
723 assert(ir->type == glsl_type::bool_type);
724 assert(ir->operands[0]->type == ir->operands[1]->type);
725 break;
726
727 case ir_binop_lshift:
728 case ir_binop_rshift:
729 assert(ir->operands[0]->type->is_integer_32_64() &&
730 ir->operands[1]->type->is_integer_32());
731 if (ir->operands[0]->type->is_scalar()) {
732 assert(ir->operands[1]->type->is_scalar());
733 }
734 if (ir->operands[0]->type->is_vector() &&
735 ir->operands[1]->type->is_vector()) {
736 assert(ir->operands[0]->type->components() ==
737 ir->operands[1]->type->components());
738 }
739 assert(ir->type == ir->operands[0]->type);
740 break;
741
742 case ir_binop_bit_and:
743 case ir_binop_bit_xor:
744 case ir_binop_bit_or:
745 assert(ir->operands[0]->type->base_type ==
746 ir->operands[1]->type->base_type);
747 assert(ir->type->is_integer_32_64());
748 if (ir->operands[0]->type->is_vector() &&
749 ir->operands[1]->type->is_vector()) {
750 assert(ir->operands[0]->type->vector_elements ==
751 ir->operands[1]->type->vector_elements);
752 }
753 break;
754
755 case ir_binop_logic_and:
756 case ir_binop_logic_xor:
757 case ir_binop_logic_or:
758 assert(ir->type->is_boolean());
759 assert(ir->operands[0]->type->is_boolean());
760 assert(ir->operands[1]->type->is_boolean());
761 break;
762
763 case ir_binop_dot:
764 assert(ir->type == glsl_type::float_type ||
765 ir->type == glsl_type::double_type ||
766 ir->type == glsl_type::float16_t_type);
767 assert(ir->operands[0]->type->is_float_16_32_64());
768 assert(ir->operands[0]->type->is_vector());
769 assert(ir->operands[0]->type == ir->operands[1]->type);
770 break;
771
772 case ir_binop_ubo_load:
773 assert(ir->operands[0]->type == glsl_type::uint_type);
774
775 assert(ir->operands[1]->type == glsl_type::uint_type);
776 break;
777
778 case ir_binop_ldexp:
779 assert(ir->operands[0]->type == ir->type);
780 assert(ir->operands[0]->type->is_float_16_32_64());
781 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
782 assert(ir->operands[0]->type->components() ==
783 ir->operands[1]->type->components());
784 break;
785
786 case ir_binop_vector_extract:
787 assert(ir->operands[0]->type->is_vector());
788 assert(ir->operands[1]->type->is_scalar()
789 && ir->operands[1]->type->is_integer_32());
790 break;
791
792 case ir_binop_interpolate_at_offset:
793 assert(ir->operands[0]->type == ir->type);
794 assert(ir->operands[0]->type->is_float());
795 assert(ir->operands[1]->type->components() == 2);
796 assert(ir->operands[1]->type->is_float());
797 break;
798
799 case ir_binop_interpolate_at_sample:
800 assert(ir->operands[0]->type == ir->type);
801 assert(ir->operands[0]->type->is_float());
802 assert(ir->operands[1]->type == glsl_type::int_type);
803 break;
804
805 case ir_binop_atan2:
806 assert(ir->operands[0]->type->is_float_16_32_64());
807 assert(ir->operands[1]->type == ir->operands[0]->type);
808 assert(ir->type == ir->operands[0]->type);
809 break;
810
811 case ir_triop_fma:
812 assert(ir->type->is_float_16_32_64());
813 assert(ir->type == ir->operands[0]->type);
814 assert(ir->type == ir->operands[1]->type);
815 assert(ir->type == ir->operands[2]->type);
816 break;
817
818 case ir_triop_lrp:
819 assert(ir->operands[0]->type->is_float_16_32_64());
820 assert(ir->operands[0]->type == ir->operands[1]->type);
821 assert(ir->operands[2]->type == ir->operands[0]->type ||
822 ir->operands[2]->type == glsl_type::float_type ||
823 ir->operands[2]->type == glsl_type::double_type ||
824 ir->operands[2]->type == glsl_type::float16_t_type);
825 break;
826
827 case ir_triop_csel:
828 assert(ir->operands[0]->type->is_boolean());
829 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
830 assert(ir->type == ir->operands[1]->type);
831 assert(ir->type == ir->operands[2]->type);
832 break;
833
834 case ir_triop_bitfield_extract:
835 assert(ir->type->is_integer_32());
836 assert(ir->operands[0]->type == ir->type);
837 assert(ir->operands[1]->type == ir->type);
838 assert(ir->operands[2]->type == ir->type);
839 break;
840
841 case ir_triop_vector_insert:
842 assert(ir->operands[0]->type->is_vector());
843 assert(ir->operands[1]->type->is_scalar());
844 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
845 assert(ir->operands[2]->type->is_scalar()
846 && ir->operands[2]->type->is_integer_32());
847 assert(ir->type == ir->operands[0]->type);
848 break;
849
850 case ir_quadop_bitfield_insert:
851 assert(ir->type->is_integer_32());
852 assert(ir->operands[0]->type == ir->type);
853 assert(ir->operands[1]->type == ir->type);
854 assert(ir->operands[2]->type == ir->type);
855 assert(ir->operands[3]->type == ir->type);
856 break;
857
858 case ir_quadop_vector:
859 /* The vector operator collects some number of scalars and generates a
860 * vector from them.
861 *
862 * - All of the operands must be scalar.
863 * - Number of operands must matche the size of the resulting vector.
864 * - Base type of the operands must match the base type of the result.
865 */
866 assert(ir->type->is_vector());
867 switch (ir->type->vector_elements) {
868 case 2:
869 assert(ir->operands[0]->type->is_scalar());
870 assert(ir->operands[0]->type->base_type == ir->type->base_type);
871 assert(ir->operands[1]->type->is_scalar());
872 assert(ir->operands[1]->type->base_type == ir->type->base_type);
873 assert(ir->operands[2] == NULL);
874 assert(ir->operands[3] == NULL);
875 break;
876 case 3:
877 assert(ir->operands[0]->type->is_scalar());
878 assert(ir->operands[0]->type->base_type == ir->type->base_type);
879 assert(ir->operands[1]->type->is_scalar());
880 assert(ir->operands[1]->type->base_type == ir->type->base_type);
881 assert(ir->operands[2]->type->is_scalar());
882 assert(ir->operands[2]->type->base_type == ir->type->base_type);
883 assert(ir->operands[3] == NULL);
884 break;
885 case 4:
886 assert(ir->operands[0]->type->is_scalar());
887 assert(ir->operands[0]->type->base_type == ir->type->base_type);
888 assert(ir->operands[1]->type->is_scalar());
889 assert(ir->operands[1]->type->base_type == ir->type->base_type);
890 assert(ir->operands[2]->type->is_scalar());
891 assert(ir->operands[2]->type->base_type == ir->type->base_type);
892 assert(ir->operands[3]->type->is_scalar());
893 assert(ir->operands[3]->type->base_type == ir->type->base_type);
894 break;
895 default:
896 /* The is_vector assertion above should prevent execution from ever
897 * getting here.
898 */
899 assert(!"Should not get here.");
900 break;
901 }
902 }
903
904 return visit_continue;
905 }
906
907 ir_visitor_status
908 ir_validate::visit_leave(ir_swizzle *ir)
909 {
910 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
911
912 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
913 if (chans[i] >= ir->val->type->vector_elements) {
914 printf("ir_swizzle @ %p specifies a channel not present "
915 "in the value.\n", (void *) ir);
916 ir->print();
917 abort();
918 }
919 }
920
921 return visit_continue;
922 }
923
924 ir_visitor_status
925 ir_validate::visit(ir_variable *ir)
926 {
927 /* An ir_variable is the one thing that can (and will) appear multiple times
928 * in an IR tree. It is added to the hashtable so that it can be used
929 * in the ir_dereference_variable handler to ensure that a variable is
930 * declared before it is dereferenced.
931 */
932 if (ir->name && ir->is_name_ralloced())
933 assert(ralloc_parent(ir->name) == ir);
934
935 _mesa_set_add(ir_set, ir);
936
937 /* If a variable is an array, verify that the maximum array index is in
938 * bounds. There was once an error in AST-to-HIR conversion that set this
939 * to be out of bounds.
940 */
941 if (ir->type->array_size() > 0) {
942 if (ir->data.max_array_access >= (int)ir->type->length) {
943 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
944 ir->data.max_array_access, ir->type->length - 1);
945 ir->print();
946 abort();
947 }
948 }
949
950 /* If a variable is an interface block (or an array of interface blocks),
951 * verify that the maximum array index for each interface member is in
952 * bounds.
953 */
954 if (ir->is_interface_instance()) {
955 const glsl_struct_field *fields =
956 ir->get_interface_type()->fields.structure;
957 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
958 if (fields[i].type->array_size() > 0 &&
959 !fields[i].implicit_sized_array) {
960 const int *const max_ifc_array_access =
961 ir->get_max_ifc_array_access();
962
963 assert(max_ifc_array_access != NULL);
964
965 if (max_ifc_array_access[i] >= (int)fields[i].type->length) {
966 printf("ir_variable has maximum access out of bounds for "
967 "field %s (%d vs %d)\n", fields[i].name,
968 max_ifc_array_access[i], fields[i].type->length);
969 ir->print();
970 abort();
971 }
972 }
973 }
974 }
975
976 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
977 printf("ir_variable didn't have an initializer, but has a constant "
978 "initializer value.\n");
979 ir->print();
980 abort();
981 }
982
983 if (ir->data.mode == ir_var_uniform
984 && is_gl_identifier(ir->name)
985 && ir->get_state_slots() == NULL) {
986 printf("built-in uniform has no state\n");
987 ir->print();
988 abort();
989 }
990
991 return visit_continue;
992 }
993
994 ir_visitor_status
995 ir_validate::visit_enter(ir_assignment *ir)
996 {
997 const ir_dereference *const lhs = ir->lhs;
998 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
999 if (ir->write_mask == 0) {
1000 printf("Assignment LHS is %s, but write mask is 0:\n",
1001 lhs->type->is_scalar() ? "scalar" : "vector");
1002 ir->print();
1003 abort();
1004 }
1005
1006 int lhs_components = 0;
1007 for (int i = 0; i < 4; i++) {
1008 if (ir->write_mask & (1 << i))
1009 lhs_components++;
1010 }
1011
1012 if (lhs_components != ir->rhs->type->vector_elements) {
1013 printf("Assignment count of LHS write mask channels enabled not\n"
1014 "matching RHS vector size (%d LHS, %d RHS).\n",
1015 lhs_components, ir->rhs->type->vector_elements);
1016 ir->print();
1017 abort();
1018 }
1019 }
1020
1021 this->validate_ir(ir, this->data_enter);
1022
1023 return visit_continue;
1024 }
1025
1026 ir_visitor_status
1027 ir_validate::visit_enter(ir_call *ir)
1028 {
1029 ir_function_signature *const callee = ir->callee;
1030
1031 if (callee->ir_type != ir_type_function_signature) {
1032 printf("IR called by ir_call is not ir_function_signature!\n");
1033 abort();
1034 }
1035
1036 if (ir->return_deref) {
1037 if (ir->return_deref->type != callee->return_type) {
1038 printf("callee type %s does not match return storage type %s\n",
1039 callee->return_type->name, ir->return_deref->type->name);
1040 abort();
1041 }
1042 } else if (callee->return_type != glsl_type::void_type) {
1043 printf("ir_call has non-void callee but no return storage\n");
1044 abort();
1045 }
1046
1047 const exec_node *formal_param_node = callee->parameters.get_head_raw();
1048 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
1049 while (true) {
1050 if (formal_param_node->is_tail_sentinel()
1051 != actual_param_node->is_tail_sentinel()) {
1052 printf("ir_call has the wrong number of parameters:\n");
1053 goto dump_ir;
1054 }
1055 if (formal_param_node->is_tail_sentinel()) {
1056 break;
1057 }
1058 const ir_variable *formal_param
1059 = (const ir_variable *) formal_param_node;
1060 const ir_rvalue *actual_param
1061 = (const ir_rvalue *) actual_param_node;
1062 if (formal_param->type != actual_param->type) {
1063 printf("ir_call parameter type mismatch:\n");
1064 goto dump_ir;
1065 }
1066 if (formal_param->data.mode == ir_var_function_out
1067 || formal_param->data.mode == ir_var_function_inout) {
1068 if (!actual_param->is_lvalue()) {
1069 printf("ir_call out/inout parameters must be lvalues:\n");
1070 goto dump_ir;
1071 }
1072 }
1073 formal_param_node = formal_param_node->next;
1074 actual_param_node = actual_param_node->next;
1075 }
1076
1077 return visit_continue;
1078
1079 dump_ir:
1080 ir->print();
1081 printf("callee:\n");
1082 callee->print();
1083 abort();
1084 return visit_stop;
1085 }
1086
1087 void
1088 ir_validate::validate_ir(ir_instruction *ir, void *data)
1089 {
1090 struct set *ir_set = (struct set *) data;
1091
1092 if (_mesa_set_search(ir_set, ir)) {
1093 printf("Instruction node present twice in ir tree:\n");
1094 ir->print();
1095 printf("\n");
1096 abort();
1097 }
1098 _mesa_set_add(ir_set, ir);
1099 }
1100
1101 #ifdef DEBUG
1102 static void
1103 check_node_type(ir_instruction *ir, void *data)
1104 {
1105 (void) data;
1106
1107 if (ir->ir_type >= ir_type_max) {
1108 printf("Instruction node with unset type\n");
1109 ir->print(); printf("\n");
1110 }
1111 ir_rvalue *value = ir->as_rvalue();
1112 if (value != NULL)
1113 assert(value->type != glsl_type::error_type);
1114 }
1115 #endif
1116
1117 void
1118 validate_ir_tree(exec_list *instructions)
1119 {
1120 /* We shouldn't have any reason to validate IR in a release build,
1121 * and it's half composed of assert()s anyway which wouldn't do
1122 * anything.
1123 */
1124 #ifdef DEBUG
1125 ir_validate v;
1126
1127 v.run(instructions);
1128
1129 foreach_in_list(ir_instruction, ir, instructions) {
1130 visit_tree(ir, check_node_type, NULL);
1131 }
1132 #endif
1133 }