2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file ir_validate.cpp
27 * Attempts to verify that various invariants of the IR tree are true.
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.
37 #include "ir_hierarchical_visitor.h"
38 #include "util/hash_table.h"
39 #include "util/macros.h"
41 #include "compiler/glsl_types.h"
45 class ir_validate
: public ir_hierarchical_visitor
{
49 this->ir_set
= _mesa_pointer_set_create(NULL
);
51 this->current_function
= NULL
;
53 this->callback_enter
= ir_validate::validate_ir
;
54 this->data_enter
= ir_set
;
59 _mesa_set_destroy(this->ir_set
, NULL
);
62 virtual ir_visitor_status
visit(ir_variable
*v
);
63 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
);
65 virtual ir_visitor_status
visit_enter(ir_discard
*ir
);
66 virtual ir_visitor_status
visit_enter(ir_if
*ir
);
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
);
72 virtual ir_visitor_status
visit_leave(ir_expression
*ir
);
73 virtual ir_visitor_status
visit_leave(ir_swizzle
*ir
);
75 virtual ir_visitor_status
visit_enter(class ir_dereference_array
*);
77 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
78 virtual ir_visitor_status
visit_enter(ir_call
*ir
);
80 static void validate_ir(ir_instruction
*ir
, void *data
);
82 ir_function
*current_function
;
87 } /* anonymous namespace */
90 ir_validate::visit(ir_dereference_variable
*ir
)
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
);
98 if (_mesa_set_search(ir_set
, ir
->var
) == NULL
) {
99 printf("ir_dereference_variable @ %p specifies undeclared variable "
101 (void *) ir
, ir
->var
->name
, (void *) ir
->var
);
105 this->validate_ir(ir
, this->data_enter
);
107 return visit_continue
;
111 ir_validate::visit_enter(class ir_dereference_array
*ir
)
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 "
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
);
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
);
135 return visit_continue
;
139 ir_validate::visit_enter(ir_discard
*ir
)
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
);
149 return visit_continue
;
153 ir_validate::visit_enter(ir_if
*ir
)
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
);
163 return visit_continue
;
168 ir_validate::visit_enter(ir_function
*ir
)
170 /* Function definitions cannot be nested.
172 if (this->current_function
!= NULL
) {
173 printf("Function definition nested inside another function "
175 printf("%s %p inside %s %p\n",
176 ir
->name
, (void *) ir
,
177 this->current_function
->name
, (void *) this->current_function
);
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.
185 this->current_function
= ir
;
187 this->validate_ir(ir
, this->data_enter
);
189 /* Verify that all of the things stored in the list of signatures are,
190 * in fact, function signatures.
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",
200 return visit_continue
;
204 ir_validate::visit_leave(ir_function
*ir
)
206 assert(ralloc_parent(ir
->name
) == ir
);
208 this->current_function
= NULL
;
209 return visit_continue
;
213 ir_validate::visit_enter(ir_function_signature
*ir
)
215 if (this->current_function
!= ir
->function()) {
216 printf("Function signature nested inside wrong function "
218 printf("%p inside %s %p instead of %s %p\n",
220 this->current_function
->name
, (void *) this->current_function
,
221 ir
->function_name(), (void *) ir
->function());
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());
231 this->validate_ir(ir
, this->data_enter
);
233 return visit_continue
;
237 ir_validate::visit_leave(ir_expression
*ir
)
239 for (unsigned i
= ir
->num_operands
; i
< 4; i
++) {
240 assert(ir
->operands
[i
] == NULL
);
243 for (unsigned i
= 0; i
< ir
->num_operands
; i
++) {
244 assert(ir
->operands
[i
] != NULL
);
247 switch (ir
->operation
) {
248 case ir_unop_bit_not
:
249 assert(ir
->operands
[0]->type
== ir
->type
);
251 case ir_unop_logic_not
:
252 assert(ir
->type
->is_boolean());
253 assert(ir
->operands
[0]->type
->is_boolean());
257 assert(ir
->type
== ir
->operands
[0]->type
);
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
);
272 assert(ir
->type
->is_float() ||
273 ir
->type
->is_double());
274 assert(ir
->type
== ir
->operands
[0]->type
);
281 case ir_unop_saturate
:
282 assert(ir
->operands
[0]->type
->is_float());
283 assert(ir
->type
== ir
->operands
[0]->type
);
287 assert(ir
->operands
[0]->type
->is_float());
288 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
291 assert(ir
->operands
[0]->type
->is_float());
292 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
295 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
296 assert(ir
->type
->is_float());
299 assert(ir
->operands
[0]->type
->is_float());
300 assert(ir
->type
->is_boolean());
303 assert(ir
->operands
[0]->type
->base_type
==
305 assert(ir
->type
->is_boolean());
308 assert(ir
->operands
[0]->type
->is_boolean());
309 assert(ir
->type
->is_float());
312 assert(ir
->operands
[0]->type
->is_boolean());
313 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT16
);
316 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
317 assert(ir
->type
->is_boolean());
320 assert(ir
->operands
[0]->type
->is_boolean());
321 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
324 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
325 assert(ir
->type
->is_float());
328 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
329 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
332 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
333 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
335 case ir_unop_bitcast_i2f
:
336 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
337 assert(ir
->type
->is_float());
339 case ir_unop_bitcast_f2i
:
340 assert(ir
->operands
[0]->type
->is_float());
341 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
343 case ir_unop_bitcast_u2f
:
344 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
345 assert(ir
->type
->is_float());
347 case ir_unop_bitcast_f2u
:
348 assert(ir
->operands
[0]->type
->is_float());
349 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
352 case ir_unop_bitcast_u642d
:
353 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
354 assert(ir
->type
->is_double());
356 case ir_unop_bitcast_i642d
:
357 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
358 assert(ir
->type
->is_double());
360 case ir_unop_bitcast_d2u64
:
361 assert(ir
->operands
[0]->type
->is_double());
362 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
364 case ir_unop_bitcast_d2i64
:
365 assert(ir
->operands
[0]->type
->is_double());
366 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
369 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
370 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
373 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
374 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
377 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
378 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
381 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
382 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
385 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
386 assert(ir
->type
->is_boolean());
389 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
390 assert(ir
->type
->is_float());
393 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
394 assert(ir
->type
->is_float());
397 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
398 assert(ir
->type
->is_double());
401 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
402 assert(ir
->type
->is_double());
405 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
406 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
409 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
410 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
413 assert(ir
->operands
[0]->type
->is_boolean());
414 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
417 assert(ir
->operands
[0]->type
->is_float());
418 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
421 assert(ir
->operands
[0]->type
->is_double());
422 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
425 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
426 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
429 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
430 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
433 assert(ir
->operands
[0]->type
->is_float());
434 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
437 assert(ir
->operands
[0]->type
->is_double());
438 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
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
);
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
);
449 case ir_unop_round_even
:
453 assert(ir
->operands
[0]->type
->is_float() ||
454 ir
->operands
[0]->type
->is_double());
455 assert(ir
->operands
[0]->type
== ir
->type
);
460 case ir_unop_dFdx_coarse
:
461 case ir_unop_dFdx_fine
:
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
);
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
);
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
);
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
);
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
);
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
);
497 case ir_unop_pack_sampler_2x32
:
498 assert(ir
->type
->is_sampler());
499 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
502 case ir_unop_pack_image_2x32
:
503 assert(ir
->type
->is_image());
504 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
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
);
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
);
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
);
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
);
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
);
535 case ir_unop_unpack_sampler_2x32
:
536 assert(ir
->type
== glsl_type::uvec2_type
);
537 assert(ir
->operands
[0]->type
->is_sampler());
540 case ir_unop_unpack_image_2x32
:
541 assert(ir
->type
== glsl_type::uvec2_type
);
542 assert(ir
->operands
[0]->type
->is_image());
545 case ir_unop_bitfield_reverse
:
546 assert(ir
->operands
[0]->type
== ir
->type
);
547 assert(ir
->type
->is_integer_32());
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
);
559 assert(ir
->operands
[0]->type
== ir
->type
);
560 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
564 /* XXX what can we assert here? */
567 case ir_unop_interpolate_at_centroid
:
568 assert(ir
->operands
[0]->type
== ir
->type
);
569 assert(ir
->operands
[0]->type
->is_float());
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
);
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());
584 assert(ir
->operands
[0]->type
->is_double());
585 assert(ir
->type
->is_float());
588 assert(ir
->operands
[0]->type
->is_float());
589 assert(ir
->type
->is_double());
592 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT16
);
593 assert(ir
->type
->is_float());
596 assert(ir
->operands
[0]->type
->is_float());
597 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT16
);
600 assert(ir
->operands
[0]->type
->is_double());
601 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
604 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
605 assert(ir
->type
->is_double());
608 assert(ir
->operands
[0]->type
->is_double());
609 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
612 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
613 assert(ir
->type
->is_double());
616 assert(ir
->operands
[0]->type
->is_double());
617 assert(ir
->type
->is_boolean());
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());
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
);
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
);
636 assert(ir
->operands
[0]->type
->is_float() ||
637 ir
->operands
[0]->type
->is_double());
638 assert(ir
->type
== ir
->operands
[0]->type
);
649 assert(ir
->operands
[0]->type
->base_type
==
650 ir
->operands
[1]->type
->base_type
);
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
);
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
);
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
);
683 case ir_binop_add_sat
:
684 case ir_binop_sub_sat
:
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());
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());
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
);
707 case ir_binop_gequal
:
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.
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
);
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.
728 assert(ir
->type
== glsl_type::bool_type
);
729 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
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());
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());
744 assert(ir
->type
== ir
->operands
[0]->type
);
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
);
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());
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
);
777 case ir_binop_ubo_load
:
778 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
780 assert(ir
->operands
[1]->type
== glsl_type::uint_type
);
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());
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());
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());
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
866 case ir_quadop_vector
:
867 /* The vector operator collects some number of scalars and generates a
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.
874 assert(ir
->type
->is_vector());
875 switch (ir
->type
->vector_elements
) {
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
);
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
);
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
);
904 /* The is_vector assertion above should prevent execution from ever
907 assert(!"Should not get here.");
912 return visit_continue
;
916 ir_validate::visit_leave(ir_swizzle
*ir
)
918 unsigned int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
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
);
929 return visit_continue
;
933 ir_validate::visit(ir_variable
*ir
)
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.
940 if (ir
->name
&& ir
->is_name_ralloced())
941 assert(ralloc_parent(ir
->name
) == ir
);
943 _mesa_set_add(ir_set
, ir
);
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.
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);
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
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();
971 assert(max_ifc_array_access
!= NULL
);
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
);
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");
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");
999 return visit_continue
;
1003 ir_validate::visit_enter(ir_assignment
*ir
)
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");
1014 int lhs_components
= 0;
1015 for (int i
= 0; i
< 4; i
++) {
1016 if (ir
->write_mask
& (1 << i
))
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
);
1029 this->validate_ir(ir
, this->data_enter
);
1031 return visit_continue
;
1035 ir_validate::visit_enter(ir_call
*ir
)
1037 ir_function_signature
*const callee
= ir
->callee
;
1039 if (callee
->ir_type
!= ir_type_function_signature
) {
1040 printf("IR called by ir_call is not ir_function_signature!\n");
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
);
1050 } else if (callee
->return_type
!= glsl_type::void_type
) {
1051 printf("ir_call has non-void callee but no return storage\n");
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();
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");
1063 if (formal_param_node
->is_tail_sentinel()) {
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");
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");
1081 formal_param_node
= formal_param_node
->next
;
1082 actual_param_node
= actual_param_node
->next
;
1085 return visit_continue
;
1089 printf("callee:\n");
1096 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
1098 struct set
*ir_set
= (struct set
*) data
;
1100 if (_mesa_set_search(ir_set
, ir
)) {
1101 printf("Instruction node present twice in ir tree:\n");
1106 _mesa_set_add(ir_set
, ir
);
1111 check_node_type(ir_instruction
*ir
, void *data
)
1115 if (ir
->ir_type
>= ir_type_max
) {
1116 printf("Instruction node with unset type\n");
1117 ir
->print(); printf("\n");
1119 ir_rvalue
*value
= ir
->as_rvalue();
1121 assert(value
->type
!= glsl_type::error_type
);
1126 validate_ir_tree(exec_list
*instructions
)
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
1135 v
.run(instructions
);
1137 foreach_in_list(ir_instruction
, ir
, instructions
) {
1138 visit_tree(ir
, check_node_type
, NULL
);