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