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_16_32_64() ||
264 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
265 assert(ir
->type
== ir
->operands
[0]->type
);
271 assert(ir
->type
->is_float_16_32_64());
272 assert(ir
->type
== ir
->operands
[0]->type
);
279 case ir_unop_saturate
:
280 assert(ir
->operands
[0]->type
->is_float_16_32());
281 assert(ir
->type
== ir
->operands
[0]->type
);
285 assert(ir
->operands
[0]->type
->is_float());
286 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
289 assert(ir
->operands
[0]->type
->is_float());
290 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
293 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
294 assert(ir
->type
->is_float());
297 assert(ir
->operands
[0]->type
->is_float());
298 assert(ir
->type
->is_boolean());
301 assert(ir
->operands
[0]->type
->base_type
==
303 assert(ir
->type
->is_boolean());
306 assert(ir
->operands
[0]->type
->is_boolean());
307 assert(ir
->type
->is_float());
310 assert(ir
->operands
[0]->type
->is_boolean());
311 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT16
);
314 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
315 assert(ir
->type
->is_boolean());
318 assert(ir
->operands
[0]->type
->is_boolean());
319 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
322 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
323 assert(ir
->type
->is_float());
326 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
327 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
330 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
331 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
333 case ir_unop_bitcast_i2f
:
334 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
335 assert(ir
->type
->is_float());
337 case ir_unop_bitcast_f2i
:
338 assert(ir
->operands
[0]->type
->is_float());
339 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
341 case ir_unop_bitcast_u2f
:
342 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
343 assert(ir
->type
->is_float());
345 case ir_unop_bitcast_f2u
:
346 assert(ir
->operands
[0]->type
->is_float());
347 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
350 case ir_unop_bitcast_u642d
:
351 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
352 assert(ir
->type
->is_double());
354 case ir_unop_bitcast_i642d
:
355 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
356 assert(ir
->type
->is_double());
358 case ir_unop_bitcast_d2u64
:
359 assert(ir
->operands
[0]->type
->is_double());
360 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
362 case ir_unop_bitcast_d2i64
:
363 assert(ir
->operands
[0]->type
->is_double());
364 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
367 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
368 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
371 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
372 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
375 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
376 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
379 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
380 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
383 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
384 assert(ir
->type
->is_boolean());
387 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
388 assert(ir
->type
->is_float());
391 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
392 assert(ir
->type
->is_float());
395 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
396 assert(ir
->type
->is_double());
399 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
400 assert(ir
->type
->is_double());
403 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
404 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
407 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
408 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
411 assert(ir
->operands
[0]->type
->is_boolean());
412 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
415 assert(ir
->operands
[0]->type
->is_float());
416 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
419 assert(ir
->operands
[0]->type
->is_double());
420 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
423 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
424 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
427 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
428 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
431 assert(ir
->operands
[0]->type
->is_float());
432 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
435 assert(ir
->operands
[0]->type
->is_double());
436 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
438 case ir_unop_u642i64
:
439 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
440 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
442 case ir_unop_i642u64
:
443 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
444 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
447 case ir_unop_round_even
:
451 assert(ir
->operands
[0]->type
->is_float_16_32_64());
452 assert(ir
->operands
[0]->type
== ir
->type
);
457 case ir_unop_dFdx_coarse
:
458 case ir_unop_dFdx_fine
:
460 case ir_unop_dFdy_coarse
:
461 case ir_unop_dFdy_fine
:
462 assert(ir
->operands
[0]->type
->is_float_16_32());
463 assert(ir
->operands
[0]->type
== ir
->type
);
466 case ir_unop_pack_snorm_2x16
:
467 case ir_unop_pack_unorm_2x16
:
468 case ir_unop_pack_half_2x16
:
469 assert(ir
->type
== glsl_type::uint_type
);
470 assert(ir
->operands
[0]->type
== glsl_type::vec2_type
);
473 case ir_unop_pack_snorm_4x8
:
474 case ir_unop_pack_unorm_4x8
:
475 assert(ir
->type
== glsl_type::uint_type
);
476 assert(ir
->operands
[0]->type
== glsl_type::vec4_type
);
479 case ir_unop_pack_double_2x32
:
480 assert(ir
->type
== glsl_type::double_type
);
481 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
484 case ir_unop_pack_int_2x32
:
485 assert(ir
->type
== glsl_type::int64_t_type
);
486 assert(ir
->operands
[0]->type
== glsl_type::ivec2_type
);
489 case ir_unop_pack_uint_2x32
:
490 assert(ir
->type
== glsl_type::uint64_t_type
);
491 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
494 case ir_unop_pack_sampler_2x32
:
495 assert(ir
->type
->is_sampler());
496 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
499 case ir_unop_pack_image_2x32
:
500 assert(ir
->type
->is_image());
501 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
504 case ir_unop_unpack_snorm_2x16
:
505 case ir_unop_unpack_unorm_2x16
:
506 case ir_unop_unpack_half_2x16
:
507 assert(ir
->type
== glsl_type::vec2_type
);
508 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
511 case ir_unop_unpack_snorm_4x8
:
512 case ir_unop_unpack_unorm_4x8
:
513 assert(ir
->type
== glsl_type::vec4_type
);
514 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
517 case ir_unop_unpack_double_2x32
:
518 assert(ir
->type
== glsl_type::uvec2_type
);
519 assert(ir
->operands
[0]->type
== glsl_type::double_type
);
522 case ir_unop_unpack_int_2x32
:
523 assert(ir
->type
== glsl_type::ivec2_type
);
524 assert(ir
->operands
[0]->type
== glsl_type::int64_t_type
);
527 case ir_unop_unpack_uint_2x32
:
528 assert(ir
->type
== glsl_type::uvec2_type
);
529 assert(ir
->operands
[0]->type
== glsl_type::uint64_t_type
);
532 case ir_unop_unpack_sampler_2x32
:
533 assert(ir
->type
== glsl_type::uvec2_type
);
534 assert(ir
->operands
[0]->type
->is_sampler());
537 case ir_unop_unpack_image_2x32
:
538 assert(ir
->type
== glsl_type::uvec2_type
);
539 assert(ir
->operands
[0]->type
->is_image());
542 case ir_unop_bitfield_reverse
:
543 assert(ir
->operands
[0]->type
== ir
->type
);
544 assert(ir
->type
->is_integer_32());
547 case ir_unop_bit_count
:
548 case ir_unop_find_msb
:
549 case ir_unop_find_lsb
:
550 assert(ir
->operands
[0]->type
->vector_elements
== ir
->type
->vector_elements
);
551 assert(ir
->operands
[0]->type
->is_integer_32());
552 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
556 assert(ir
->operands
[0]->type
== ir
->type
);
557 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
560 case ir_unop_interpolate_at_centroid
:
561 assert(ir
->operands
[0]->type
== ir
->type
);
562 assert(ir
->operands
[0]->type
->is_float_16_32());
565 case ir_unop_get_buffer_size
:
566 assert(ir
->type
== glsl_type::int_type
);
567 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
570 case ir_unop_ssbo_unsized_array_length
:
571 assert(ir
->type
== glsl_type::int_type
);
572 assert(ir
->operands
[0]->type
->is_array());
573 assert(ir
->operands
[0]->type
->is_unsized_array());
577 assert(ir
->operands
[0]->type
->is_double());
578 assert(ir
->type
->is_float());
581 assert(ir
->operands
[0]->type
->is_float());
582 assert(ir
->type
->is_double());
585 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT16
);
586 assert(ir
->type
->is_float());
590 assert(ir
->operands
[0]->type
->is_float());
591 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT16
);
594 assert(ir
->operands
[0]->type
->is_double());
595 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
598 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
599 assert(ir
->type
->is_double());
602 assert(ir
->operands
[0]->type
->is_double());
603 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
606 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
607 assert(ir
->type
->is_double());
610 assert(ir
->operands
[0]->type
->is_double());
611 assert(ir
->type
->is_boolean());
614 case ir_unop_frexp_sig
:
615 assert(ir
->operands
[0]->type
->is_float_16_32_64());
616 assert(ir
->type
->is_double());
618 case ir_unop_frexp_exp
:
619 assert(ir
->operands
[0]->type
->is_float_16_32_64());
620 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
622 case ir_unop_subroutine_to_int
:
623 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_SUBROUTINE
);
624 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
628 assert(ir
->operands
[0]->type
->is_float_16_32_64());
629 assert(ir
->type
== ir
->operands
[0]->type
);
640 assert(ir
->operands
[0]->type
->base_type
==
641 ir
->operands
[1]->type
->base_type
);
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
);
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
);
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
);
674 case ir_binop_add_sat
:
675 case ir_binop_sub_sat
:
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());
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());
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
);
698 case ir_binop_gequal
:
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.
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
);
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.
719 assert(ir
->type
== glsl_type::bool_type
);
720 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
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());
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());
735 assert(ir
->type
== ir
->operands
[0]->type
);
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
);
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());
760 assert(ir
->type
== glsl_type::float_type
||
761 ir
->type
== glsl_type::double_type
||
762 ir
->type
== glsl_type::float16_t_type
);
763 assert(ir
->operands
[0]->type
->is_float_16_32_64());
764 assert(ir
->operands
[0]->type
->is_vector());
765 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
768 case ir_binop_ubo_load
:
769 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
771 assert(ir
->operands
[1]->type
== glsl_type::uint_type
);
775 assert(ir
->operands
[0]->type
== ir
->type
);
776 assert(ir
->operands
[0]->type
->is_float_16_32_64());
777 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
);
778 assert(ir
->operands
[0]->type
->components() ==
779 ir
->operands
[1]->type
->components());
782 case ir_binop_vector_extract
:
783 assert(ir
->operands
[0]->type
->is_vector());
784 assert(ir
->operands
[1]->type
->is_scalar()
785 && ir
->operands
[1]->type
->is_integer_32());
788 case ir_binop_interpolate_at_offset
:
789 assert(ir
->operands
[0]->type
== ir
->type
);
790 assert(ir
->operands
[0]->type
->is_float());
791 assert(ir
->operands
[1]->type
->components() == 2);
792 assert(ir
->operands
[1]->type
->is_float());
795 case ir_binop_interpolate_at_sample
:
796 assert(ir
->operands
[0]->type
== ir
->type
);
797 assert(ir
->operands
[0]->type
->is_float());
798 assert(ir
->operands
[1]->type
== glsl_type::int_type
);
802 assert(ir
->operands
[0]->type
->is_float_16_32_64());
803 assert(ir
->operands
[1]->type
== ir
->operands
[0]->type
);
804 assert(ir
->type
== ir
->operands
[0]->type
);
808 assert(ir
->type
->is_float_16_32_64());
809 assert(ir
->type
== ir
->operands
[0]->type
);
810 assert(ir
->type
== ir
->operands
[1]->type
);
811 assert(ir
->type
== ir
->operands
[2]->type
);
815 assert(ir
->operands
[0]->type
->is_float_16_32_64());
816 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
817 assert(ir
->operands
[2]->type
== ir
->operands
[0]->type
||
818 ir
->operands
[2]->type
== glsl_type::float_type
||
819 ir
->operands
[2]->type
== glsl_type::double_type
||
820 ir
->operands
[2]->type
== glsl_type::float16_t_type
);
824 assert(ir
->operands
[0]->type
->is_boolean());
825 assert(ir
->type
->vector_elements
== ir
->operands
[0]->type
->vector_elements
);
826 assert(ir
->type
== ir
->operands
[1]->type
);
827 assert(ir
->type
== ir
->operands
[2]->type
);
830 case ir_triop_bitfield_extract
:
831 assert(ir
->type
->is_integer_32());
832 assert(ir
->operands
[0]->type
== ir
->type
);
833 assert(ir
->operands
[1]->type
== ir
->type
);
834 assert(ir
->operands
[2]->type
== ir
->type
);
837 case ir_triop_vector_insert
:
838 assert(ir
->operands
[0]->type
->is_vector());
839 assert(ir
->operands
[1]->type
->is_scalar());
840 assert(ir
->operands
[0]->type
->base_type
== ir
->operands
[1]->type
->base_type
);
841 assert(ir
->operands
[2]->type
->is_scalar()
842 && ir
->operands
[2]->type
->is_integer_32());
843 assert(ir
->type
== ir
->operands
[0]->type
);
846 case ir_quadop_bitfield_insert
:
847 assert(ir
->type
->is_integer_32());
848 assert(ir
->operands
[0]->type
== ir
->type
);
849 assert(ir
->operands
[1]->type
== ir
->type
);
850 assert(ir
->operands
[2]->type
== ir
->type
);
851 assert(ir
->operands
[3]->type
== ir
->type
);
854 case ir_quadop_vector
:
855 /* The vector operator collects some number of scalars and generates a
858 * - All of the operands must be scalar.
859 * - Number of operands must matche the size of the resulting vector.
860 * - Base type of the operands must match the base type of the result.
862 assert(ir
->type
->is_vector());
863 switch (ir
->type
->vector_elements
) {
865 assert(ir
->operands
[0]->type
->is_scalar());
866 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
867 assert(ir
->operands
[1]->type
->is_scalar());
868 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
869 assert(ir
->operands
[2] == NULL
);
870 assert(ir
->operands
[3] == NULL
);
873 assert(ir
->operands
[0]->type
->is_scalar());
874 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
875 assert(ir
->operands
[1]->type
->is_scalar());
876 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
877 assert(ir
->operands
[2]->type
->is_scalar());
878 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
879 assert(ir
->operands
[3] == NULL
);
882 assert(ir
->operands
[0]->type
->is_scalar());
883 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
884 assert(ir
->operands
[1]->type
->is_scalar());
885 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
886 assert(ir
->operands
[2]->type
->is_scalar());
887 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
888 assert(ir
->operands
[3]->type
->is_scalar());
889 assert(ir
->operands
[3]->type
->base_type
== ir
->type
->base_type
);
892 /* The is_vector assertion above should prevent execution from ever
895 assert(!"Should not get here.");
900 return visit_continue
;
904 ir_validate::visit_leave(ir_swizzle
*ir
)
906 unsigned int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
908 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
909 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
910 printf("ir_swizzle @ %p specifies a channel not present "
911 "in the value.\n", (void *) ir
);
917 return visit_continue
;
921 ir_validate::visit(ir_variable
*ir
)
923 /* An ir_variable is the one thing that can (and will) appear multiple times
924 * in an IR tree. It is added to the hashtable so that it can be used
925 * in the ir_dereference_variable handler to ensure that a variable is
926 * declared before it is dereferenced.
928 if (ir
->name
&& ir
->is_name_ralloced())
929 assert(ralloc_parent(ir
->name
) == ir
);
931 _mesa_set_add(ir_set
, ir
);
933 /* If a variable is an array, verify that the maximum array index is in
934 * bounds. There was once an error in AST-to-HIR conversion that set this
935 * to be out of bounds.
937 if (ir
->type
->array_size() > 0) {
938 if (ir
->data
.max_array_access
>= (int)ir
->type
->length
) {
939 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
940 ir
->data
.max_array_access
, ir
->type
->length
- 1);
946 /* If a variable is an interface block (or an array of interface blocks),
947 * verify that the maximum array index for each interface member is in
950 if (ir
->is_interface_instance()) {
951 const glsl_struct_field
*fields
=
952 ir
->get_interface_type()->fields
.structure
;
953 for (unsigned i
= 0; i
< ir
->get_interface_type()->length
; i
++) {
954 if (fields
[i
].type
->array_size() > 0 &&
955 !fields
[i
].implicit_sized_array
) {
956 const int *const max_ifc_array_access
=
957 ir
->get_max_ifc_array_access();
959 assert(max_ifc_array_access
!= NULL
);
961 if (max_ifc_array_access
[i
] >= (int)fields
[i
].type
->length
) {
962 printf("ir_variable has maximum access out of bounds for "
963 "field %s (%d vs %d)\n", fields
[i
].name
,
964 max_ifc_array_access
[i
], fields
[i
].type
->length
);
972 if (ir
->constant_initializer
!= NULL
&& !ir
->data
.has_initializer
) {
973 printf("ir_variable didn't have an initializer, but has a constant "
974 "initializer value.\n");
979 if (ir
->data
.mode
== ir_var_uniform
980 && is_gl_identifier(ir
->name
)
981 && ir
->get_state_slots() == NULL
) {
982 printf("built-in uniform has no state\n");
987 return visit_continue
;
991 ir_validate::visit_enter(ir_assignment
*ir
)
993 const ir_dereference
*const lhs
= ir
->lhs
;
994 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
995 if (ir
->write_mask
== 0) {
996 printf("Assignment LHS is %s, but write mask is 0:\n",
997 lhs
->type
->is_scalar() ? "scalar" : "vector");
1002 int lhs_components
= 0;
1003 for (int i
= 0; i
< 4; i
++) {
1004 if (ir
->write_mask
& (1 << i
))
1008 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
1009 printf("Assignment count of LHS write mask channels enabled not\n"
1010 "matching RHS vector size (%d LHS, %d RHS).\n",
1011 lhs_components
, ir
->rhs
->type
->vector_elements
);
1017 this->validate_ir(ir
, this->data_enter
);
1019 return visit_continue
;
1023 ir_validate::visit_enter(ir_call
*ir
)
1025 ir_function_signature
*const callee
= ir
->callee
;
1027 if (callee
->ir_type
!= ir_type_function_signature
) {
1028 printf("IR called by ir_call is not ir_function_signature!\n");
1032 if (ir
->return_deref
) {
1033 if (ir
->return_deref
->type
!= callee
->return_type
) {
1034 printf("callee type %s does not match return storage type %s\n",
1035 callee
->return_type
->name
, ir
->return_deref
->type
->name
);
1038 } else if (callee
->return_type
!= glsl_type::void_type
) {
1039 printf("ir_call has non-void callee but no return storage\n");
1043 const exec_node
*formal_param_node
= callee
->parameters
.get_head_raw();
1044 const exec_node
*actual_param_node
= ir
->actual_parameters
.get_head_raw();
1046 if (formal_param_node
->is_tail_sentinel()
1047 != actual_param_node
->is_tail_sentinel()) {
1048 printf("ir_call has the wrong number of parameters:\n");
1051 if (formal_param_node
->is_tail_sentinel()) {
1054 const ir_variable
*formal_param
1055 = (const ir_variable
*) formal_param_node
;
1056 const ir_rvalue
*actual_param
1057 = (const ir_rvalue
*) actual_param_node
;
1058 if (formal_param
->type
!= actual_param
->type
) {
1059 printf("ir_call parameter type mismatch:\n");
1062 if (formal_param
->data
.mode
== ir_var_function_out
1063 || formal_param
->data
.mode
== ir_var_function_inout
) {
1064 if (!actual_param
->is_lvalue()) {
1065 printf("ir_call out/inout parameters must be lvalues:\n");
1069 formal_param_node
= formal_param_node
->next
;
1070 actual_param_node
= actual_param_node
->next
;
1073 return visit_continue
;
1077 printf("callee:\n");
1084 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
1086 struct set
*ir_set
= (struct set
*) data
;
1088 if (_mesa_set_search(ir_set
, ir
)) {
1089 printf("Instruction node present twice in ir tree:\n");
1094 _mesa_set_add(ir_set
, ir
);
1099 check_node_type(ir_instruction
*ir
, void *data
)
1103 if (ir
->ir_type
>= ir_type_max
) {
1104 printf("Instruction node with unset type\n");
1105 ir
->print(); printf("\n");
1107 ir_rvalue
*value
= ir
->as_rvalue();
1109 assert(value
->type
!= glsl_type::error_type
);
1114 validate_ir_tree(exec_list
*instructions
)
1116 /* We shouldn't have any reason to validate IR in a release build,
1117 * and it's half composed of assert()s anyway which wouldn't do
1123 v
.run(instructions
);
1125 foreach_in_list(ir_instruction
, ir
, instructions
) {
1126 visit_tree(ir
, check_node_type
, NULL
);