Added few more stubs so that control reaches to DestroyDevice().
[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 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
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
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
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
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
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
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
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
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
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
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_d2f:
637 assert(ir->operands[0]->type->is_double());
638 assert(ir->type->is_float());
639 break;
640 case ir_unop_f2d:
641 assert(ir->operands[0]->type->is_float());
642 assert(ir->type->is_double());
643 break;
644 case ir_unop_f162f:
645 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT16);
646 assert(ir->type->is_float());
647 break;
648 case ir_unop_f2f16:
649 case ir_unop_f2fmp:
650 assert(ir->operands[0]->type->is_float());
651 assert(ir->type->base_type == GLSL_TYPE_FLOAT16);
652 break;
653 case ir_unop_i2i:
654 assert(ir->operands[0]->type->is_int_16_32());
655 assert(ir->type->is_int_16_32());
656 assert(ir->type->base_type != ir->operands[0]->type->base_type);
657 break;
658 case ir_unop_u2u:
659 assert(ir->operands[0]->type->is_uint_16_32());
660 assert(ir->type->is_uint_16_32());
661 assert(ir->type->base_type != ir->operands[0]->type->base_type);
662 break;
663 case ir_unop_i2imp:
664 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
665 assert(ir->type->base_type == GLSL_TYPE_INT16);
666 break;
667 case ir_unop_u2ump:
668 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
669 assert(ir->type->base_type == GLSL_TYPE_UINT16);
670 break;
671 case ir_unop_d2i:
672 assert(ir->operands[0]->type->is_double());
673 assert(ir->type->is_int_16_32());
674 break;
675 case ir_unop_i2d:
676 assert(ir->operands[0]->type->is_int_16_32());
677 assert(ir->type->is_double());
678 break;
679 case ir_unop_d2u:
680 assert(ir->operands[0]->type->is_double());
681 assert(ir->type->is_uint_16_32());
682 break;
683 case ir_unop_u2d:
684 assert(ir->operands[0]->type->is_uint_16_32());
685 assert(ir->type->is_double());
686 break;
687 case ir_unop_d2b:
688 assert(ir->operands[0]->type->is_double());
689 assert(ir->type->is_boolean());
690 break;
691
692 case ir_unop_frexp_sig:
693 assert(ir->operands[0]->type->is_float_32_64());
694 assert(ir->type->is_double());
695 break;
696 case ir_unop_frexp_exp:
697 assert(ir->operands[0]->type->is_float_32_64());
698 assert(ir->type->base_type == GLSL_TYPE_INT);
699 break;
700 case ir_unop_subroutine_to_int:
701 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
702 assert(ir->type->base_type == GLSL_TYPE_INT);
703 break;
704
705 case ir_unop_atan:
706 assert(ir->operands[0]->type->is_float_16_32_64());
707 assert(ir->type == ir->operands[0]->type);
708 break;
709
710 case ir_binop_add:
711 case ir_binop_sub:
712 case ir_binop_mul:
713 case ir_binop_div:
714 case ir_binop_mod:
715 case ir_binop_min:
716 case ir_binop_max:
717 case ir_binop_pow:
718 assert(ir->operands[0]->type->base_type ==
719 ir->operands[1]->type->base_type);
720
721 if (ir->operation == ir_binop_mul &&
722 (ir->type->base_type == GLSL_TYPE_UINT64 ||
723 ir->type->base_type == GLSL_TYPE_INT64) &&
724 (ir->operands[0]->type->is_int_16_32()||
725 ir->operands[1]->type->is_int_16_32()||
726 ir->operands[0]->type->is_uint_16_32() ||
727 ir->operands[1]->type->is_uint_16_32())) {
728 assert(ir->operands[0]->type == ir->operands[1]->type);
729 break;
730 }
731
732 if (ir->operands[0]->type->is_scalar())
733 assert(ir->operands[1]->type == ir->type);
734 else if (ir->operands[1]->type->is_scalar())
735 assert(ir->operands[0]->type == ir->type);
736 else if (ir->operands[0]->type->is_vector() &&
737 ir->operands[1]->type->is_vector()) {
738 assert(ir->operands[0]->type == ir->operands[1]->type);
739 assert(ir->operands[0]->type == ir->type);
740 }
741 break;
742
743 case ir_binop_abs_sub:
744 assert(ir->operands[0]->type == ir->operands[1]->type);
745 assert(ir->operands[0]->type->is_integer_16_32_64());
746 assert(ir->operands[0]->type->vector_elements ==
747 ir->type->vector_elements);
748 assert(ir->type->is_uint_16_32_64());
749 break;
750
751 case ir_binop_add_sat:
752 case ir_binop_sub_sat:
753 case ir_binop_avg:
754 case ir_binop_avg_round:
755 assert(ir->type == ir->operands[0]->type);
756 assert(ir->type == ir->operands[1]->type);
757 assert(ir->type->is_integer_16_32_64());
758 break;
759
760 case ir_binop_mul_32x16:
761 case ir_binop_imul_high:
762 assert(ir->type == ir->operands[0]->type);
763 assert(ir->type == ir->operands[1]->type);
764 assert(ir->type->is_integer_32());
765 break;
766
767 case ir_binop_carry:
768 case ir_binop_borrow:
769 assert(ir->type == ir->operands[0]->type);
770 assert(ir->type == ir->operands[1]->type);
771 assert(ir->type->base_type == GLSL_TYPE_UINT);
772 break;
773
774 case ir_binop_less:
775 case ir_binop_gequal:
776 case ir_binop_equal:
777 case ir_binop_nequal:
778 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
779 * ==, and != operators. The IR operators perform a component-wise
780 * comparison on scalar or vector types and return a boolean scalar or
781 * vector type of the same size.
782 */
783 assert(ir->type->is_boolean());
784 assert(ir->operands[0]->type == ir->operands[1]->type);
785 assert(ir->operands[0]->type->is_vector()
786 || ir->operands[0]->type->is_scalar());
787 assert(ir->operands[0]->type->vector_elements
788 == ir->type->vector_elements);
789 break;
790
791 case ir_binop_all_equal:
792 case ir_binop_any_nequal:
793 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
794 * return a scalar boolean. The IR matches that.
795 */
796 assert(ir->type == glsl_type::bool_type);
797 assert(ir->operands[0]->type == ir->operands[1]->type);
798 break;
799
800 case ir_binop_lshift:
801 case ir_binop_rshift:
802 assert(ir->operands[0]->type->is_integer_16_32_64() &&
803 ir->operands[1]->type->is_integer_16_32());
804 if (ir->operands[0]->type->is_scalar()) {
805 assert(ir->operands[1]->type->is_scalar());
806 }
807 if (ir->operands[0]->type->is_vector() &&
808 ir->operands[1]->type->is_vector()) {
809 assert(ir->operands[0]->type->components() ==
810 ir->operands[1]->type->components());
811 }
812 assert(ir->type == ir->operands[0]->type);
813 break;
814
815 case ir_binop_bit_and:
816 case ir_binop_bit_xor:
817 case ir_binop_bit_or:
818 assert(ir->operands[0]->type->base_type ==
819 ir->operands[1]->type->base_type);
820 assert(ir->type->is_integer_16_32_64());
821 if (ir->operands[0]->type->is_vector() &&
822 ir->operands[1]->type->is_vector()) {
823 assert(ir->operands[0]->type->vector_elements ==
824 ir->operands[1]->type->vector_elements);
825 }
826 break;
827
828 case ir_binop_logic_and:
829 case ir_binop_logic_xor:
830 case ir_binop_logic_or:
831 assert(ir->type->is_boolean());
832 assert(ir->operands[0]->type->is_boolean());
833 assert(ir->operands[1]->type->is_boolean());
834 break;
835
836 case ir_binop_dot:
837 assert(ir->type == glsl_type::float_type ||
838 ir->type == glsl_type::double_type ||
839 ir->type == glsl_type::float16_t_type);
840 assert(ir->operands[0]->type->is_float_16_32_64());
841 assert(ir->operands[0]->type->is_vector());
842 assert(ir->operands[0]->type == ir->operands[1]->type);
843 break;
844
845 case ir_binop_ubo_load:
846 assert(ir->operands[0]->type == glsl_type::uint_type);
847
848 assert(ir->operands[1]->type == glsl_type::uint_type);
849 break;
850
851 case ir_binop_ldexp:
852 assert(ir->operands[0]->type == ir->type);
853 assert(ir->operands[0]->type->is_float_32_64());
854 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
855 assert(ir->operands[0]->type->components() ==
856 ir->operands[1]->type->components());
857 break;
858
859 case ir_binop_vector_extract:
860 assert(ir->operands[0]->type->is_vector());
861 assert(ir->operands[1]->type->is_scalar()
862 && ir->operands[1]->type->is_integer_16_32());
863 break;
864
865 case ir_binop_interpolate_at_offset:
866 assert(ir->operands[0]->type == ir->type);
867 assert(ir->operands[0]->type->is_float_16_32());
868 assert(ir->operands[1]->type->components() == 2);
869 assert(ir->operands[1]->type->is_float_16_32());
870 break;
871
872 case ir_binop_interpolate_at_sample:
873 assert(ir->operands[0]->type == ir->type);
874 assert(ir->operands[0]->type->is_float_16_32());
875 assert(ir->operands[1]->type == glsl_type::int_type ||
876 ir->operands[1]->type == glsl_type::int16_t_type);
877 break;
878
879 case ir_binop_atan2:
880 assert(ir->operands[0]->type->is_float_16_32_64());
881 assert(ir->operands[1]->type == ir->operands[0]->type);
882 assert(ir->type == ir->operands[0]->type);
883 break;
884
885 case ir_triop_fma:
886 assert(ir->type->is_float_16_32_64());
887 assert(ir->type == ir->operands[0]->type);
888 assert(ir->type == ir->operands[1]->type);
889 assert(ir->type == ir->operands[2]->type);
890 break;
891
892 case ir_triop_lrp:
893 assert(ir->operands[0]->type->is_float_16_32_64());
894 assert(ir->operands[0]->type == ir->operands[1]->type);
895 assert(ir->operands[2]->type == ir->operands[0]->type ||
896 ir->operands[2]->type == glsl_type::float_type ||
897 ir->operands[2]->type == glsl_type::double_type ||
898 ir->operands[2]->type == glsl_type::float16_t_type);
899 break;
900
901 case ir_triop_csel:
902 assert(ir->operands[0]->type->is_boolean());
903 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
904 assert(ir->type == ir->operands[1]->type);
905 assert(ir->type == ir->operands[2]->type);
906 break;
907
908 case ir_triop_bitfield_extract:
909 assert(ir->type->is_integer_16_32());
910 assert(ir->operands[0]->type == ir->type);
911 assert(ir->operands[1]->type == ir->type);
912 assert(ir->operands[2]->type == ir->type);
913 break;
914
915 case ir_triop_vector_insert:
916 assert(ir->operands[0]->type->is_vector());
917 assert(ir->operands[1]->type->is_scalar());
918 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
919 assert(ir->operands[2]->type->is_scalar()
920 && ir->operands[2]->type->is_integer_16_32());
921 assert(ir->type == ir->operands[0]->type);
922 break;
923
924 case ir_quadop_bitfield_insert:
925 assert(ir->type->is_integer_16_32());
926 assert(ir->operands[0]->type == ir->type);
927 assert(ir->operands[1]->type == ir->type);
928 assert(ir->operands[2]->type == ir->type);
929 assert(ir->operands[3]->type == ir->type);
930 break;
931
932 case ir_quadop_vector:
933 /* The vector operator collects some number of scalars and generates a
934 * vector from them.
935 *
936 * - All of the operands must be scalar.
937 * - Number of operands must matche the size of the resulting vector.
938 * - Base type of the operands must match the base type of the result.
939 */
940 assert(ir->type->is_vector());
941 switch (ir->type->vector_elements) {
942 case 2:
943 assert(ir->operands[0]->type->is_scalar());
944 assert(ir->operands[0]->type->base_type == ir->type->base_type);
945 assert(ir->operands[1]->type->is_scalar());
946 assert(ir->operands[1]->type->base_type == ir->type->base_type);
947 assert(ir->operands[2] == NULL);
948 assert(ir->operands[3] == NULL);
949 break;
950 case 3:
951 assert(ir->operands[0]->type->is_scalar());
952 assert(ir->operands[0]->type->base_type == ir->type->base_type);
953 assert(ir->operands[1]->type->is_scalar());
954 assert(ir->operands[1]->type->base_type == ir->type->base_type);
955 assert(ir->operands[2]->type->is_scalar());
956 assert(ir->operands[2]->type->base_type == ir->type->base_type);
957 assert(ir->operands[3] == NULL);
958 break;
959 case 4:
960 assert(ir->operands[0]->type->is_scalar());
961 assert(ir->operands[0]->type->base_type == ir->type->base_type);
962 assert(ir->operands[1]->type->is_scalar());
963 assert(ir->operands[1]->type->base_type == ir->type->base_type);
964 assert(ir->operands[2]->type->is_scalar());
965 assert(ir->operands[2]->type->base_type == ir->type->base_type);
966 assert(ir->operands[3]->type->is_scalar());
967 assert(ir->operands[3]->type->base_type == ir->type->base_type);
968 break;
969 default:
970 /* The is_vector assertion above should prevent execution from ever
971 * getting here.
972 */
973 assert(!"Should not get here.");
974 break;
975 }
976 }
977
978 return visit_continue;
979 }
980
981 ir_visitor_status
982 ir_validate::visit_leave(ir_swizzle *ir)
983 {
984 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
985
986 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
987 if (chans[i] >= ir->val->type->vector_elements) {
988 printf("ir_swizzle @ %p specifies a channel not present "
989 "in the value.\n", (void *) ir);
990 ir->print();
991 abort();
992 }
993 }
994
995 return visit_continue;
996 }
997
998 ir_visitor_status
999 ir_validate::visit(ir_variable *ir)
1000 {
1001 /* An ir_variable is the one thing that can (and will) appear multiple times
1002 * in an IR tree. It is added to the hashtable so that it can be used
1003 * in the ir_dereference_variable handler to ensure that a variable is
1004 * declared before it is dereferenced.
1005 */
1006 if (ir->name && ir->is_name_ralloced())
1007 assert(ralloc_parent(ir->name) == ir);
1008
1009 _mesa_set_add(ir_set, ir);
1010
1011 /* If a variable is an array, verify that the maximum array index is in
1012 * bounds. There was once an error in AST-to-HIR conversion that set this
1013 * to be out of bounds.
1014 */
1015 if (ir->type->array_size() > 0) {
1016 if (ir->data.max_array_access >= (int)ir->type->length) {
1017 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
1018 ir->data.max_array_access, ir->type->length - 1);
1019 ir->print();
1020 abort();
1021 }
1022 }
1023
1024 /* If a variable is an interface block (or an array of interface blocks),
1025 * verify that the maximum array index for each interface member is in
1026 * bounds.
1027 */
1028 if (ir->is_interface_instance()) {
1029 const glsl_struct_field *fields =
1030 ir->get_interface_type()->fields.structure;
1031 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
1032 if (fields[i].type->array_size() > 0 &&
1033 !fields[i].implicit_sized_array) {
1034 const int *const max_ifc_array_access =
1035 ir->get_max_ifc_array_access();
1036
1037 assert(max_ifc_array_access != NULL);
1038
1039 if (max_ifc_array_access[i] >= (int)fields[i].type->length) {
1040 printf("ir_variable has maximum access out of bounds for "
1041 "field %s (%d vs %d)\n", fields[i].name,
1042 max_ifc_array_access[i], fields[i].type->length);
1043 ir->print();
1044 abort();
1045 }
1046 }
1047 }
1048 }
1049
1050 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
1051 printf("ir_variable didn't have an initializer, but has a constant "
1052 "initializer value.\n");
1053 ir->print();
1054 abort();
1055 }
1056
1057 if (ir->data.mode == ir_var_uniform
1058 && is_gl_identifier(ir->name)
1059 && ir->get_state_slots() == NULL) {
1060 printf("built-in uniform has no state\n");
1061 ir->print();
1062 abort();
1063 }
1064
1065 return visit_continue;
1066 }
1067
1068 ir_visitor_status
1069 ir_validate::visit_enter(ir_assignment *ir)
1070 {
1071 const ir_dereference *const lhs = ir->lhs;
1072 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
1073 if (ir->write_mask == 0) {
1074 printf("Assignment LHS is %s, but write mask is 0:\n",
1075 lhs->type->is_scalar() ? "scalar" : "vector");
1076 ir->print();
1077 abort();
1078 }
1079
1080 int lhs_components = 0;
1081 for (int i = 0; i < 4; i++) {
1082 if (ir->write_mask & (1 << i))
1083 lhs_components++;
1084 }
1085
1086 if (lhs_components != ir->rhs->type->vector_elements) {
1087 printf("Assignment count of LHS write mask channels enabled not\n"
1088 "matching RHS vector size (%d LHS, %d RHS).\n",
1089 lhs_components, ir->rhs->type->vector_elements);
1090 ir->print();
1091 abort();
1092 }
1093 }
1094
1095 if (lhs->type->base_type != ir->rhs->type->base_type) {
1096 printf("Assignment LHS and RHS base types are different:\n");
1097 lhs->print();
1098 printf("\n");
1099 ir->rhs->print();
1100 printf("\n");
1101 abort();
1102 }
1103
1104 this->validate_ir(ir, this->data_enter);
1105
1106 return visit_continue;
1107 }
1108
1109 ir_visitor_status
1110 ir_validate::visit_enter(ir_call *ir)
1111 {
1112 ir_function_signature *const callee = ir->callee;
1113
1114 if (callee->ir_type != ir_type_function_signature) {
1115 printf("IR called by ir_call is not ir_function_signature!\n");
1116 abort();
1117 }
1118
1119 if (ir->return_deref) {
1120 if (ir->return_deref->type != callee->return_type) {
1121 printf("callee type %s does not match return storage type %s\n",
1122 callee->return_type->name, ir->return_deref->type->name);
1123 abort();
1124 }
1125 } else if (callee->return_type != glsl_type::void_type) {
1126 printf("ir_call has non-void callee but no return storage\n");
1127 abort();
1128 }
1129
1130 const exec_node *formal_param_node = callee->parameters.get_head_raw();
1131 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
1132 while (true) {
1133 if (formal_param_node->is_tail_sentinel()
1134 != actual_param_node->is_tail_sentinel()) {
1135 printf("ir_call has the wrong number of parameters:\n");
1136 goto dump_ir;
1137 }
1138 if (formal_param_node->is_tail_sentinel()) {
1139 break;
1140 }
1141 const ir_variable *formal_param
1142 = (const ir_variable *) formal_param_node;
1143 const ir_rvalue *actual_param
1144 = (const ir_rvalue *) actual_param_node;
1145 if (formal_param->type != actual_param->type) {
1146 printf("ir_call parameter type mismatch:\n");
1147 goto dump_ir;
1148 }
1149 if (formal_param->data.mode == ir_var_function_out
1150 || formal_param->data.mode == ir_var_function_inout) {
1151 if (!actual_param->is_lvalue()) {
1152 printf("ir_call out/inout parameters must be lvalues:\n");
1153 goto dump_ir;
1154 }
1155 }
1156 formal_param_node = formal_param_node->next;
1157 actual_param_node = actual_param_node->next;
1158 }
1159
1160 return visit_continue;
1161
1162 dump_ir:
1163 ir->print();
1164 printf("callee:\n");
1165 callee->print();
1166 abort();
1167 return visit_stop;
1168 }
1169
1170 void
1171 ir_validate::validate_ir(ir_instruction *ir, void *data)
1172 {
1173 struct set *ir_set = (struct set *) data;
1174
1175 if (_mesa_set_search(ir_set, ir)) {
1176 printf("Instruction node present twice in ir tree:\n");
1177 ir->print();
1178 printf("\n");
1179 abort();
1180 }
1181 _mesa_set_add(ir_set, ir);
1182 }
1183
1184 static void
1185 check_node_type(ir_instruction *ir, void *data)
1186 {
1187 (void) data;
1188
1189 if (ir->ir_type >= ir_type_max) {
1190 printf("Instruction node with unset type\n");
1191 ir->print(); printf("\n");
1192 }
1193 ir_rvalue *value = ir->as_rvalue();
1194 if (value != NULL)
1195 assert(value->type != glsl_type::error_type);
1196 }
1197
1198 void
1199 validate_ir_tree(exec_list *instructions)
1200 {
1201 /* We shouldn't have any reason to validate IR in a release build,
1202 * and it's half composed of assert()s anyway which wouldn't do
1203 * anything.
1204 */
1205 #ifndef DEBUG
1206 if (!env_var_as_boolean("GLSL_VALIDATE", false))
1207 return;
1208 #endif
1209 ir_validate v;
1210
1211 v.run(instructions);
1212
1213 foreach_in_list(ir_instruction, ir, instructions) {
1214 visit_tree(ir, check_node_type, NULL);
1215 }
1216 }