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