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/debug.h"
39 #include "util/hash_table.h"
40 #include "util/macros.h"
42 #include "compiler/glsl_types.h"
46 class ir_validate
: public ir_hierarchical_visitor
{
50 this->ir_set
= _mesa_pointer_set_create(NULL
);
52 this->current_function
= NULL
;
54 this->callback_enter
= ir_validate::validate_ir
;
55 this->data_enter
= ir_set
;
60 _mesa_set_destroy(this->ir_set
, NULL
);
63 virtual ir_visitor_status
visit(ir_variable
*v
);
64 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
);
66 virtual ir_visitor_status
visit_enter(ir_discard
*ir
);
67 virtual ir_visitor_status
visit_enter(ir_if
*ir
);
69 virtual ir_visitor_status
visit_enter(ir_function
*ir
);
70 virtual ir_visitor_status
visit_leave(ir_function
*ir
);
71 virtual ir_visitor_status
visit_enter(ir_function_signature
*ir
);
72 virtual ir_visitor_status
visit_enter(ir_return
*ir
);
74 virtual ir_visitor_status
visit_leave(ir_expression
*ir
);
75 virtual ir_visitor_status
visit_leave(ir_swizzle
*ir
);
77 virtual ir_visitor_status
visit_enter(class ir_dereference_array
*);
78 virtual ir_visitor_status
visit_enter(class ir_dereference_record
*);
80 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
81 virtual ir_visitor_status
visit_enter(ir_call
*ir
);
83 static void validate_ir(ir_instruction
*ir
, void *data
);
85 ir_function
*current_function
;
90 } /* anonymous namespace */
93 ir_validate::visit(ir_dereference_variable
*ir
)
95 if ((ir
->var
== NULL
) || (ir
->var
->as_variable() == NULL
)) {
96 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
97 (void *) ir
, (void *) ir
->var
);
101 /* Compare types without arrays, because one side can be sized and
104 if (ir
->var
->type
->without_array() != ir
->type
->without_array()) {
105 printf("ir_dereference_variable type is not equal to variable type: ");
111 if (_mesa_set_search(ir_set
, ir
->var
) == NULL
) {
112 printf("ir_dereference_variable @ %p specifies undeclared variable "
114 (void *) ir
, ir
->var
->name
, (void *) ir
->var
);
118 this->validate_ir(ir
, this->data_enter
);
120 return visit_continue
;
124 ir_validate::visit_enter(class ir_dereference_array
*ir
)
126 if (!ir
->array
->type
->is_array() && !ir
->array
->type
->is_matrix() &&
127 !ir
->array
->type
->is_vector()) {
128 printf("ir_dereference_array @ %p does not specify an array, a vector "
136 if (ir
->array
->type
->is_array()) {
137 if (ir
->array
->type
->fields
.array
!= ir
->type
) {
138 printf("ir_dereference_array type is not equal to the array "
144 } else if (ir
->array
->type
->base_type
!= ir
->type
->base_type
) {
145 printf("ir_dereference_array base types are not equal: ");
151 if (!ir
->array_index
->type
->is_scalar()) {
152 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
153 (void *) ir
, ir
->array_index
->type
->name
);
157 if (!ir
->array_index
->type
->is_integer_16_32()) {
158 printf("ir_dereference_array @ %p does not have integer index: %s\n",
159 (void *) ir
, ir
->array_index
->type
->name
);
163 return visit_continue
;
167 ir_validate::visit_enter(class ir_dereference_record
*ir
)
169 if (!ir
->record
->type
->is_struct() && !ir
->record
->type
->is_interface()) {
170 printf("ir_dereference_record @ %p does not specify a record\n",
177 if (ir
->record
->type
->fields
.structure
[ir
->field_idx
].type
!= ir
->type
) {
178 printf("ir_dereference_record type is not equal to the record "
185 return visit_continue
;
189 ir_validate::visit_enter(ir_discard
*ir
)
191 if (ir
->condition
&& ir
->condition
->type
!= glsl_type::bool_type
) {
192 printf("ir_discard condition %s type instead of bool.\n",
193 ir
->condition
->type
->name
);
199 return visit_continue
;
203 ir_validate::visit_enter(ir_if
*ir
)
205 if (ir
->condition
->type
!= glsl_type::bool_type
) {
206 printf("ir_if condition %s type instead of bool.\n",
207 ir
->condition
->type
->name
);
213 return visit_continue
;
218 ir_validate::visit_enter(ir_function
*ir
)
220 /* Function definitions cannot be nested.
222 if (this->current_function
!= NULL
) {
223 printf("Function definition nested inside another function "
225 printf("%s %p inside %s %p\n",
226 ir
->name
, (void *) ir
,
227 this->current_function
->name
, (void *) this->current_function
);
231 /* Store the current function hierarchy being traversed. This is used
232 * by the function signature visitor to ensure that the signatures are
233 * linked with the correct functions.
235 this->current_function
= ir
;
237 this->validate_ir(ir
, this->data_enter
);
239 /* Verify that all of the things stored in the list of signatures are,
240 * in fact, function signatures.
242 foreach_in_list(ir_instruction
, sig
, &ir
->signatures
) {
243 if (sig
->ir_type
!= ir_type_function_signature
) {
244 printf("Non-signature in signature list of function `%s'\n",
250 return visit_continue
;
254 ir_validate::visit_leave(ir_function
*ir
)
256 assert(ralloc_parent(ir
->name
) == ir
);
258 this->current_function
= NULL
;
259 return visit_continue
;
263 ir_validate::visit_enter(ir_function_signature
*ir
)
265 if (this->current_function
!= ir
->function()) {
266 printf("Function signature nested inside wrong function "
268 printf("%p inside %s %p instead of %s %p\n",
270 this->current_function
->name
, (void *) this->current_function
,
271 ir
->function_name(), (void *) ir
->function());
275 if (ir
->return_type
== NULL
) {
276 printf("Function signature %p for function %s has NULL return type.\n",
277 (void *) ir
, ir
->function_name());
281 this->validate_ir(ir
, this->data_enter
);
283 return visit_continue
;
287 ir_validate::visit_enter(ir_return
*ir
)
289 if (!this->current_function
) {
290 printf("Return statement outside of a function\n");
294 return visit_continue
;
298 ir_validate::visit_leave(ir_expression
*ir
)
300 for (unsigned i
= ir
->num_operands
; i
< 4; i
++) {
301 assert(ir
->operands
[i
] == NULL
);
304 for (unsigned i
= 0; i
< ir
->num_operands
; i
++) {
305 assert(ir
->operands
[i
] != NULL
);
308 switch (ir
->operation
) {
309 case ir_unop_bit_not
:
310 assert(ir
->operands
[0]->type
== ir
->type
);
312 case ir_unop_logic_not
:
313 assert(ir
->type
->is_boolean());
314 assert(ir
->operands
[0]->type
->is_boolean());
318 assert(ir
->type
== ir
->operands
[0]->type
);
323 assert(ir
->operands
[0]->type
->is_int_16_32_64() ||
324 ir
->operands
[0]->type
->is_float_16_32_64());
325 assert(ir
->type
== ir
->operands
[0]->type
);
331 assert(ir
->type
->is_float_16_32_64());
332 assert(ir
->type
== ir
->operands
[0]->type
);
339 case ir_unop_saturate
:
340 assert(ir
->operands
[0]->type
->is_float_16_32());
341 assert(ir
->type
== ir
->operands
[0]->type
);
345 assert(ir
->operands
[0]->type
->is_float_16_32());
346 assert(ir
->type
->is_int_16_32());
349 assert(ir
->operands
[0]->type
->is_float_16_32());
350 assert(ir
->type
->is_uint_16_32());
353 assert(ir
->operands
[0]->type
->is_int_16_32());
354 assert(ir
->type
->is_float_16_32());
357 assert(ir
->operands
[0]->type
->is_float_16_32());
358 assert(ir
->type
->is_boolean());
361 assert(ir
->operands
[0]->type
->base_type
==
363 assert(ir
->type
->is_boolean());
366 assert(ir
->operands
[0]->type
->is_boolean());
367 assert(ir
->type
->is_float_16_32());
370 assert(ir
->operands
[0]->type
->is_boolean());
371 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT16
);
374 assert(ir
->operands
[0]->type
->is_int_16_32());
375 assert(ir
->type
->is_boolean());
378 assert(ir
->operands
[0]->type
->is_boolean());
379 assert(ir
->type
->is_int_16_32());
382 assert(ir
->operands
[0]->type
->is_uint_16_32());
383 assert(ir
->type
->is_float_16_32());
386 assert(ir
->operands
[0]->type
->is_int_16_32());
387 assert(ir
->type
->is_uint_16_32());
390 assert(ir
->operands
[0]->type
->is_uint_16_32());
391 assert(ir
->type
->is_int_16_32());
393 case ir_unop_bitcast_i2f
:
394 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
395 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
397 case ir_unop_bitcast_f2i
:
398 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
399 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
401 case ir_unop_bitcast_u2f
:
402 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
403 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
405 case ir_unop_bitcast_f2u
:
406 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
407 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
410 case ir_unop_bitcast_u642d
:
411 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
412 assert(ir
->type
->is_double());
414 case ir_unop_bitcast_i642d
:
415 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
416 assert(ir
->type
->is_double());
418 case ir_unop_bitcast_d2u64
:
419 assert(ir
->operands
[0]->type
->is_double());
420 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
422 case ir_unop_bitcast_d2i64
:
423 assert(ir
->operands
[0]->type
->is_double());
424 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
427 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
428 assert(ir
->type
->is_int_16_32());
431 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
432 assert(ir
->type
->is_int_16_32());
435 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
436 assert(ir
->type
->is_uint_16_32());
439 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
440 assert(ir
->type
->is_uint_16_32());
443 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
444 assert(ir
->type
->is_boolean());
447 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
448 assert(ir
->type
->is_float());
451 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
452 assert(ir
->type
->is_float());
455 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
456 assert(ir
->type
->is_double());
459 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
460 assert(ir
->type
->is_double());
463 assert(ir
->operands
[0]->type
->is_int_16_32());
464 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
467 assert(ir
->operands
[0]->type
->is_uint_16_32());
468 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
471 assert(ir
->operands
[0]->type
->is_boolean());
472 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
475 assert(ir
->operands
[0]->type
->is_float());
476 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
479 assert(ir
->operands
[0]->type
->is_double());
480 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
483 assert(ir
->operands
[0]->type
->is_int_16_32());
484 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
487 assert(ir
->operands
[0]->type
->is_uint_16_32());
488 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
491 assert(ir
->operands
[0]->type
->is_float());
492 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
495 assert(ir
->operands
[0]->type
->is_double());
496 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
498 case ir_unop_u642i64
:
499 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
500 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
502 case ir_unop_i642u64
:
503 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
504 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
507 case ir_unop_round_even
:
511 assert(ir
->operands
[0]->type
->is_float_16_32_64());
512 assert(ir
->operands
[0]->type
== ir
->type
);
517 case ir_unop_dFdx_coarse
:
518 case ir_unop_dFdx_fine
:
520 case ir_unop_dFdy_coarse
:
521 case ir_unop_dFdy_fine
:
522 assert(ir
->operands
[0]->type
->is_float_16_32());
523 assert(ir
->operands
[0]->type
== ir
->type
);
526 case ir_unop_pack_snorm_2x16
:
527 case ir_unop_pack_unorm_2x16
:
528 case ir_unop_pack_half_2x16
:
529 assert(ir
->type
== glsl_type::uint_type
);
530 assert(ir
->operands
[0]->type
== glsl_type::vec2_type
);
533 case ir_unop_pack_snorm_4x8
:
534 case ir_unop_pack_unorm_4x8
:
535 assert(ir
->type
== glsl_type::uint_type
);
536 assert(ir
->operands
[0]->type
== glsl_type::vec4_type
);
539 case ir_unop_pack_double_2x32
:
540 assert(ir
->type
== glsl_type::double_type
);
541 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
544 case ir_unop_pack_int_2x32
:
545 assert(ir
->type
== glsl_type::int64_t_type
);
546 assert(ir
->operands
[0]->type
== glsl_type::ivec2_type
);
549 case ir_unop_pack_uint_2x32
:
550 assert(ir
->type
== glsl_type::uint64_t_type
);
551 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
554 case ir_unop_pack_sampler_2x32
:
555 assert(ir
->type
->is_sampler());
556 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
559 case ir_unop_pack_image_2x32
:
560 assert(ir
->type
->is_image());
561 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
564 case ir_unop_unpack_snorm_2x16
:
565 case ir_unop_unpack_unorm_2x16
:
566 case ir_unop_unpack_half_2x16
:
567 assert(ir
->type
== glsl_type::vec2_type
);
568 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
571 case ir_unop_unpack_snorm_4x8
:
572 case ir_unop_unpack_unorm_4x8
:
573 assert(ir
->type
== glsl_type::vec4_type
);
574 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
577 case ir_unop_unpack_double_2x32
:
578 assert(ir
->type
== glsl_type::uvec2_type
);
579 assert(ir
->operands
[0]->type
== glsl_type::double_type
);
582 case ir_unop_unpack_int_2x32
:
583 assert(ir
->type
== glsl_type::ivec2_type
);
584 assert(ir
->operands
[0]->type
== glsl_type::int64_t_type
);
587 case ir_unop_unpack_uint_2x32
:
588 assert(ir
->type
== glsl_type::uvec2_type
);
589 assert(ir
->operands
[0]->type
== glsl_type::uint64_t_type
);
592 case ir_unop_unpack_sampler_2x32
:
593 assert(ir
->type
== glsl_type::uvec2_type
);
594 assert(ir
->operands
[0]->type
->is_sampler());
597 case ir_unop_unpack_image_2x32
:
598 assert(ir
->type
== glsl_type::uvec2_type
);
599 assert(ir
->operands
[0]->type
->is_image());
602 case ir_unop_bitfield_reverse
:
603 assert(ir
->operands
[0]->type
== ir
->type
);
604 assert(ir
->type
->is_integer_32());
607 case ir_unop_bit_count
:
608 case ir_unop_find_msb
:
609 case ir_unop_find_lsb
:
610 assert(ir
->operands
[0]->type
->vector_elements
== ir
->type
->vector_elements
);
611 assert(ir
->operands
[0]->type
->is_integer_16_32());
612 assert(ir
->type
->is_int_16_32());
616 assert(ir
->operands
[0]->type
== ir
->type
);
617 assert(ir
->type
->is_uint_16_32());
620 case ir_unop_interpolate_at_centroid
:
621 assert(ir
->operands
[0]->type
== ir
->type
);
622 assert(ir
->operands
[0]->type
->is_float_16_32());
625 case ir_unop_get_buffer_size
:
626 assert(ir
->type
== glsl_type::int_type
);
627 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
630 case ir_unop_ssbo_unsized_array_length
:
631 assert(ir
->type
== glsl_type::int_type
);
632 assert(ir
->operands
[0]->type
->is_array());
633 assert(ir
->operands
[0]->type
->is_unsized_array());
637 assert(ir
->operands
[0]->type
->is_double());
638 assert(ir
->type
->is_float());
641 assert(ir
->operands
[0]->type
->is_float());
642 assert(ir
->type
->is_double());
645 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT16
);
646 assert(ir
->type
->is_float());
650 assert(ir
->operands
[0]->type
->is_float());
651 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT16
);
654 assert(ir
->operands
[0]->type
->is_int_16_32());
655 assert(ir
->type
->is_int_16_32());
656 assert(ir
->type
->base_type
!= ir
->operands
[0]->type
->base_type
);
659 assert(ir
->operands
[0]->type
->is_uint_16_32());
660 assert(ir
->type
->is_uint_16_32());
661 assert(ir
->type
->base_type
!= ir
->operands
[0]->type
->base_type
);
664 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
665 assert(ir
->type
->base_type
== GLSL_TYPE_INT16
);
668 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
669 assert(ir
->type
->base_type
== GLSL_TYPE_UINT16
);
672 assert(ir
->operands
[0]->type
->is_double());
673 assert(ir
->type
->is_int_16_32());
676 assert(ir
->operands
[0]->type
->is_int_16_32());
677 assert(ir
->type
->is_double());
680 assert(ir
->operands
[0]->type
->is_double());
681 assert(ir
->type
->is_uint_16_32());
684 assert(ir
->operands
[0]->type
->is_uint_16_32());
685 assert(ir
->type
->is_double());
688 assert(ir
->operands
[0]->type
->is_double());
689 assert(ir
->type
->is_boolean());
692 case ir_unop_frexp_sig
:
693 assert(ir
->operands
[0]->type
->is_float_32_64());
694 assert(ir
->type
->is_double());
696 case ir_unop_frexp_exp
:
697 assert(ir
->operands
[0]->type
->is_float_32_64());
698 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
700 case ir_unop_subroutine_to_int
:
701 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_SUBROUTINE
);
702 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
706 assert(ir
->operands
[0]->type
->is_float_16_32_64());
707 assert(ir
->type
== ir
->operands
[0]->type
);
718 assert(ir
->operands
[0]->type
->base_type
==
719 ir
->operands
[1]->type
->base_type
);
721 if (ir
->operation
== ir_binop_mul
&&
722 (ir
->type
->base_type
== GLSL_TYPE_UINT64
||
723 ir
->type
->base_type
== GLSL_TYPE_INT64
) &&
724 (ir
->operands
[0]->type
->is_int_16_32()||
725 ir
->operands
[1]->type
->is_int_16_32()||
726 ir
->operands
[0]->type
->is_uint_16_32() ||
727 ir
->operands
[1]->type
->is_uint_16_32())) {
728 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
732 if (ir
->operands
[0]->type
->is_scalar())
733 assert(ir
->operands
[1]->type
== ir
->type
);
734 else if (ir
->operands
[1]->type
->is_scalar())
735 assert(ir
->operands
[0]->type
== ir
->type
);
736 else if (ir
->operands
[0]->type
->is_vector() &&
737 ir
->operands
[1]->type
->is_vector()) {
738 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
739 assert(ir
->operands
[0]->type
== ir
->type
);
743 case ir_binop_abs_sub
:
744 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
745 assert(ir
->operands
[0]->type
->is_integer_16_32_64());
746 assert(ir
->operands
[0]->type
->vector_elements
==
747 ir
->type
->vector_elements
);
748 assert(ir
->type
->is_uint_16_32_64());
751 case ir_binop_add_sat
:
752 case ir_binop_sub_sat
:
754 case ir_binop_avg_round
:
755 assert(ir
->type
== ir
->operands
[0]->type
);
756 assert(ir
->type
== ir
->operands
[1]->type
);
757 assert(ir
->type
->is_integer_16_32_64());
760 case ir_binop_mul_32x16
:
761 case ir_binop_imul_high
:
762 assert(ir
->type
== ir
->operands
[0]->type
);
763 assert(ir
->type
== ir
->operands
[1]->type
);
764 assert(ir
->type
->is_integer_32());
768 case ir_binop_borrow
:
769 assert(ir
->type
== ir
->operands
[0]->type
);
770 assert(ir
->type
== ir
->operands
[1]->type
);
771 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
775 case ir_binop_gequal
:
777 case ir_binop_nequal
:
778 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
779 * ==, and != operators. The IR operators perform a component-wise
780 * comparison on scalar or vector types and return a boolean scalar or
781 * vector type of the same size.
783 assert(ir
->type
->is_boolean());
784 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
785 assert(ir
->operands
[0]->type
->is_vector()
786 || ir
->operands
[0]->type
->is_scalar());
787 assert(ir
->operands
[0]->type
->vector_elements
788 == ir
->type
->vector_elements
);
791 case ir_binop_all_equal
:
792 case ir_binop_any_nequal
:
793 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
794 * return a scalar boolean. The IR matches that.
796 assert(ir
->type
== glsl_type::bool_type
);
797 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
800 case ir_binop_lshift
:
801 case ir_binop_rshift
:
802 assert(ir
->operands
[0]->type
->is_integer_16_32_64() &&
803 ir
->operands
[1]->type
->is_integer_16_32());
804 if (ir
->operands
[0]->type
->is_scalar()) {
805 assert(ir
->operands
[1]->type
->is_scalar());
807 if (ir
->operands
[0]->type
->is_vector() &&
808 ir
->operands
[1]->type
->is_vector()) {
809 assert(ir
->operands
[0]->type
->components() ==
810 ir
->operands
[1]->type
->components());
812 assert(ir
->type
== ir
->operands
[0]->type
);
815 case ir_binop_bit_and
:
816 case ir_binop_bit_xor
:
817 case ir_binop_bit_or
:
818 assert(ir
->operands
[0]->type
->base_type
==
819 ir
->operands
[1]->type
->base_type
);
820 assert(ir
->type
->is_integer_16_32_64());
821 if (ir
->operands
[0]->type
->is_vector() &&
822 ir
->operands
[1]->type
->is_vector()) {
823 assert(ir
->operands
[0]->type
->vector_elements
==
824 ir
->operands
[1]->type
->vector_elements
);
828 case ir_binop_logic_and
:
829 case ir_binop_logic_xor
:
830 case ir_binop_logic_or
:
831 assert(ir
->type
->is_boolean());
832 assert(ir
->operands
[0]->type
->is_boolean());
833 assert(ir
->operands
[1]->type
->is_boolean());
837 assert(ir
->type
== glsl_type::float_type
||
838 ir
->type
== glsl_type::double_type
||
839 ir
->type
== glsl_type::float16_t_type
);
840 assert(ir
->operands
[0]->type
->is_float_16_32_64());
841 assert(ir
->operands
[0]->type
->is_vector());
842 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
845 case ir_binop_ubo_load
:
846 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
848 assert(ir
->operands
[1]->type
== glsl_type::uint_type
);
852 assert(ir
->operands
[0]->type
== ir
->type
);
853 assert(ir
->operands
[0]->type
->is_float_32_64());
854 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
);
855 assert(ir
->operands
[0]->type
->components() ==
856 ir
->operands
[1]->type
->components());
859 case ir_binop_vector_extract
:
860 assert(ir
->operands
[0]->type
->is_vector());
861 assert(ir
->operands
[1]->type
->is_scalar()
862 && ir
->operands
[1]->type
->is_integer_16_32());
865 case ir_binop_interpolate_at_offset
:
866 assert(ir
->operands
[0]->type
== ir
->type
);
867 assert(ir
->operands
[0]->type
->is_float_16_32());
868 assert(ir
->operands
[1]->type
->components() == 2);
869 assert(ir
->operands
[1]->type
->is_float_16_32());
872 case ir_binop_interpolate_at_sample
:
873 assert(ir
->operands
[0]->type
== ir
->type
);
874 assert(ir
->operands
[0]->type
->is_float_16_32());
875 assert(ir
->operands
[1]->type
== glsl_type::int_type
||
876 ir
->operands
[1]->type
== glsl_type::int16_t_type
);
880 assert(ir
->operands
[0]->type
->is_float_16_32_64());
881 assert(ir
->operands
[1]->type
== ir
->operands
[0]->type
);
882 assert(ir
->type
== ir
->operands
[0]->type
);
886 assert(ir
->type
->is_float_16_32_64());
887 assert(ir
->type
== ir
->operands
[0]->type
);
888 assert(ir
->type
== ir
->operands
[1]->type
);
889 assert(ir
->type
== ir
->operands
[2]->type
);
893 assert(ir
->operands
[0]->type
->is_float_16_32_64());
894 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
895 assert(ir
->operands
[2]->type
== ir
->operands
[0]->type
||
896 ir
->operands
[2]->type
== glsl_type::float_type
||
897 ir
->operands
[2]->type
== glsl_type::double_type
||
898 ir
->operands
[2]->type
== glsl_type::float16_t_type
);
902 assert(ir
->operands
[0]->type
->is_boolean());
903 assert(ir
->type
->vector_elements
== ir
->operands
[0]->type
->vector_elements
);
904 assert(ir
->type
== ir
->operands
[1]->type
);
905 assert(ir
->type
== ir
->operands
[2]->type
);
908 case ir_triop_bitfield_extract
:
909 assert(ir
->type
->is_integer_16_32());
910 assert(ir
->operands
[0]->type
== ir
->type
);
911 assert(ir
->operands
[1]->type
== ir
->type
);
912 assert(ir
->operands
[2]->type
== ir
->type
);
915 case ir_triop_vector_insert
:
916 assert(ir
->operands
[0]->type
->is_vector());
917 assert(ir
->operands
[1]->type
->is_scalar());
918 assert(ir
->operands
[0]->type
->base_type
== ir
->operands
[1]->type
->base_type
);
919 assert(ir
->operands
[2]->type
->is_scalar()
920 && ir
->operands
[2]->type
->is_integer_16_32());
921 assert(ir
->type
== ir
->operands
[0]->type
);
924 case ir_quadop_bitfield_insert
:
925 assert(ir
->type
->is_integer_16_32());
926 assert(ir
->operands
[0]->type
== ir
->type
);
927 assert(ir
->operands
[1]->type
== ir
->type
);
928 assert(ir
->operands
[2]->type
== ir
->type
);
929 assert(ir
->operands
[3]->type
== ir
->type
);
932 case ir_quadop_vector
:
933 /* The vector operator collects some number of scalars and generates a
936 * - All of the operands must be scalar.
937 * - Number of operands must matche the size of the resulting vector.
938 * - Base type of the operands must match the base type of the result.
940 assert(ir
->type
->is_vector());
941 switch (ir
->type
->vector_elements
) {
943 assert(ir
->operands
[0]->type
->is_scalar());
944 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
945 assert(ir
->operands
[1]->type
->is_scalar());
946 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
947 assert(ir
->operands
[2] == NULL
);
948 assert(ir
->operands
[3] == NULL
);
951 assert(ir
->operands
[0]->type
->is_scalar());
952 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
953 assert(ir
->operands
[1]->type
->is_scalar());
954 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
955 assert(ir
->operands
[2]->type
->is_scalar());
956 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
957 assert(ir
->operands
[3] == NULL
);
960 assert(ir
->operands
[0]->type
->is_scalar());
961 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
962 assert(ir
->operands
[1]->type
->is_scalar());
963 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
964 assert(ir
->operands
[2]->type
->is_scalar());
965 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
966 assert(ir
->operands
[3]->type
->is_scalar());
967 assert(ir
->operands
[3]->type
->base_type
== ir
->type
->base_type
);
970 /* The is_vector assertion above should prevent execution from ever
973 assert(!"Should not get here.");
978 return visit_continue
;
982 ir_validate::visit_leave(ir_swizzle
*ir
)
984 unsigned int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
986 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
987 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
988 printf("ir_swizzle @ %p specifies a channel not present "
989 "in the value.\n", (void *) ir
);
995 return visit_continue
;
999 ir_validate::visit(ir_variable
*ir
)
1001 /* An ir_variable is the one thing that can (and will) appear multiple times
1002 * in an IR tree. It is added to the hashtable so that it can be used
1003 * in the ir_dereference_variable handler to ensure that a variable is
1004 * declared before it is dereferenced.
1006 if (ir
->name
&& ir
->is_name_ralloced())
1007 assert(ralloc_parent(ir
->name
) == ir
);
1009 _mesa_set_add(ir_set
, ir
);
1011 /* If a variable is an array, verify that the maximum array index is in
1012 * bounds. There was once an error in AST-to-HIR conversion that set this
1013 * to be out of bounds.
1015 if (ir
->type
->array_size() > 0) {
1016 if (ir
->data
.max_array_access
>= (int)ir
->type
->length
) {
1017 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
1018 ir
->data
.max_array_access
, ir
->type
->length
- 1);
1024 /* If a variable is an interface block (or an array of interface blocks),
1025 * verify that the maximum array index for each interface member is in
1028 if (ir
->is_interface_instance()) {
1029 const glsl_struct_field
*fields
=
1030 ir
->get_interface_type()->fields
.structure
;
1031 for (unsigned i
= 0; i
< ir
->get_interface_type()->length
; i
++) {
1032 if (fields
[i
].type
->array_size() > 0 &&
1033 !fields
[i
].implicit_sized_array
) {
1034 const int *const max_ifc_array_access
=
1035 ir
->get_max_ifc_array_access();
1037 assert(max_ifc_array_access
!= NULL
);
1039 if (max_ifc_array_access
[i
] >= (int)fields
[i
].type
->length
) {
1040 printf("ir_variable has maximum access out of bounds for "
1041 "field %s (%d vs %d)\n", fields
[i
].name
,
1042 max_ifc_array_access
[i
], fields
[i
].type
->length
);
1050 if (ir
->constant_initializer
!= NULL
&& !ir
->data
.has_initializer
) {
1051 printf("ir_variable didn't have an initializer, but has a constant "
1052 "initializer value.\n");
1057 if (ir
->data
.mode
== ir_var_uniform
1058 && is_gl_identifier(ir
->name
)
1059 && ir
->get_state_slots() == NULL
) {
1060 printf("built-in uniform has no state\n");
1065 return visit_continue
;
1069 ir_validate::visit_enter(ir_assignment
*ir
)
1071 const ir_dereference
*const lhs
= ir
->lhs
;
1072 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
1073 if (ir
->write_mask
== 0) {
1074 printf("Assignment LHS is %s, but write mask is 0:\n",
1075 lhs
->type
->is_scalar() ? "scalar" : "vector");
1080 int lhs_components
= 0;
1081 for (int i
= 0; i
< 4; i
++) {
1082 if (ir
->write_mask
& (1 << i
))
1086 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
1087 printf("Assignment count of LHS write mask channels enabled not\n"
1088 "matching RHS vector size (%d LHS, %d RHS).\n",
1089 lhs_components
, ir
->rhs
->type
->vector_elements
);
1095 if (lhs
->type
->base_type
!= ir
->rhs
->type
->base_type
) {
1096 printf("Assignment LHS and RHS base types are different:\n");
1104 this->validate_ir(ir
, this->data_enter
);
1106 return visit_continue
;
1110 ir_validate::visit_enter(ir_call
*ir
)
1112 ir_function_signature
*const callee
= ir
->callee
;
1114 if (callee
->ir_type
!= ir_type_function_signature
) {
1115 printf("IR called by ir_call is not ir_function_signature!\n");
1119 if (ir
->return_deref
) {
1120 if (ir
->return_deref
->type
!= callee
->return_type
) {
1121 printf("callee type %s does not match return storage type %s\n",
1122 callee
->return_type
->name
, ir
->return_deref
->type
->name
);
1125 } else if (callee
->return_type
!= glsl_type::void_type
) {
1126 printf("ir_call has non-void callee but no return storage\n");
1130 const exec_node
*formal_param_node
= callee
->parameters
.get_head_raw();
1131 const exec_node
*actual_param_node
= ir
->actual_parameters
.get_head_raw();
1133 if (formal_param_node
->is_tail_sentinel()
1134 != actual_param_node
->is_tail_sentinel()) {
1135 printf("ir_call has the wrong number of parameters:\n");
1138 if (formal_param_node
->is_tail_sentinel()) {
1141 const ir_variable
*formal_param
1142 = (const ir_variable
*) formal_param_node
;
1143 const ir_rvalue
*actual_param
1144 = (const ir_rvalue
*) actual_param_node
;
1145 if (formal_param
->type
!= actual_param
->type
) {
1146 printf("ir_call parameter type mismatch:\n");
1149 if (formal_param
->data
.mode
== ir_var_function_out
1150 || formal_param
->data
.mode
== ir_var_function_inout
) {
1151 if (!actual_param
->is_lvalue()) {
1152 printf("ir_call out/inout parameters must be lvalues:\n");
1156 formal_param_node
= formal_param_node
->next
;
1157 actual_param_node
= actual_param_node
->next
;
1160 return visit_continue
;
1164 printf("callee:\n");
1171 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
1173 struct set
*ir_set
= (struct set
*) data
;
1175 if (_mesa_set_search(ir_set
, ir
)) {
1176 printf("Instruction node present twice in ir tree:\n");
1181 _mesa_set_add(ir_set
, ir
);
1185 check_node_type(ir_instruction
*ir
, void *data
)
1189 if (ir
->ir_type
>= ir_type_max
) {
1190 printf("Instruction node with unset type\n");
1191 ir
->print(); printf("\n");
1193 ir_rvalue
*value
= ir
->as_rvalue();
1195 assert(value
->type
!= glsl_type::error_type
);
1199 validate_ir_tree(exec_list
*instructions
)
1201 /* We shouldn't have any reason to validate IR in a release build,
1202 * and it's half composed of assert()s anyway which wouldn't do
1206 if (!env_var_as_boolean("GLSL_VALIDATE", false))
1211 v
.run(instructions
);
1213 foreach_in_list(ir_instruction
, ir
, instructions
) {
1214 visit_tree(ir
, check_node_type
, NULL
);