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