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