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()) {
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
->is_boolean());
304 assert(ir
->type
->is_float());
307 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
308 assert(ir
->type
->is_boolean());
311 assert(ir
->operands
[0]->type
->is_boolean());
312 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
315 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
316 assert(ir
->type
->is_float());
319 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
320 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
323 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
324 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
326 case ir_unop_bitcast_i2f
:
327 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
328 assert(ir
->type
->is_float());
330 case ir_unop_bitcast_f2i
:
331 assert(ir
->operands
[0]->type
->is_float());
332 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
334 case ir_unop_bitcast_u2f
:
335 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
336 assert(ir
->type
->is_float());
338 case ir_unop_bitcast_f2u
:
339 assert(ir
->operands
[0]->type
->is_float());
340 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
343 case ir_unop_bitcast_u642d
:
344 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
345 assert(ir
->type
->is_double());
347 case ir_unop_bitcast_i642d
:
348 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
349 assert(ir
->type
->is_double());
351 case ir_unop_bitcast_d2u64
:
352 assert(ir
->operands
[0]->type
->is_double());
353 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
355 case ir_unop_bitcast_d2i64
:
356 assert(ir
->operands
[0]->type
->is_double());
357 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
360 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
361 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
364 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
365 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
368 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
369 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
372 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
373 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
376 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
377 assert(ir
->type
->is_boolean());
380 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
381 assert(ir
->type
->is_float());
384 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
385 assert(ir
->type
->is_float());
388 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
389 assert(ir
->type
->is_double());
392 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
393 assert(ir
->type
->is_double());
396 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
397 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
400 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
401 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
404 assert(ir
->operands
[0]->type
->is_boolean());
405 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
408 assert(ir
->operands
[0]->type
->is_float());
409 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
412 assert(ir
->operands
[0]->type
->is_double());
413 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
416 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
417 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
420 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
421 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
424 assert(ir
->operands
[0]->type
->is_float());
425 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
428 assert(ir
->operands
[0]->type
->is_double());
429 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
431 case ir_unop_u642i64
:
432 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT64
);
433 assert(ir
->type
->base_type
== GLSL_TYPE_INT64
);
435 case ir_unop_i642u64
:
436 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT64
);
437 assert(ir
->type
->base_type
== GLSL_TYPE_UINT64
);
440 case ir_unop_round_even
:
444 assert(ir
->operands
[0]->type
->is_float() ||
445 ir
->operands
[0]->type
->is_double());
446 assert(ir
->operands
[0]->type
== ir
->type
);
451 case ir_unop_dFdx_coarse
:
452 case ir_unop_dFdx_fine
:
454 case ir_unop_dFdy_coarse
:
455 case ir_unop_dFdy_fine
:
456 assert(ir
->operands
[0]->type
->is_float());
457 assert(ir
->operands
[0]->type
== ir
->type
);
460 case ir_unop_pack_snorm_2x16
:
461 case ir_unop_pack_unorm_2x16
:
462 case ir_unop_pack_half_2x16
:
463 assert(ir
->type
== glsl_type::uint_type
);
464 assert(ir
->operands
[0]->type
== glsl_type::vec2_type
);
467 case ir_unop_pack_snorm_4x8
:
468 case ir_unop_pack_unorm_4x8
:
469 assert(ir
->type
== glsl_type::uint_type
);
470 assert(ir
->operands
[0]->type
== glsl_type::vec4_type
);
473 case ir_unop_pack_double_2x32
:
474 assert(ir
->type
== glsl_type::double_type
);
475 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
478 case ir_unop_pack_int_2x32
:
479 assert(ir
->type
== glsl_type::int64_t_type
);
480 assert(ir
->operands
[0]->type
== glsl_type::ivec2_type
);
483 case ir_unop_pack_uint_2x32
:
484 assert(ir
->type
== glsl_type::uint64_t_type
);
485 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
488 case ir_unop_pack_sampler_2x32
:
489 assert(ir
->type
->is_sampler());
490 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
493 case ir_unop_pack_image_2x32
:
494 assert(ir
->type
->is_image());
495 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
498 case ir_unop_unpack_snorm_2x16
:
499 case ir_unop_unpack_unorm_2x16
:
500 case ir_unop_unpack_half_2x16
:
501 assert(ir
->type
== glsl_type::vec2_type
);
502 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
505 case ir_unop_unpack_snorm_4x8
:
506 case ir_unop_unpack_unorm_4x8
:
507 assert(ir
->type
== glsl_type::vec4_type
);
508 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
511 case ir_unop_unpack_double_2x32
:
512 assert(ir
->type
== glsl_type::uvec2_type
);
513 assert(ir
->operands
[0]->type
== glsl_type::double_type
);
516 case ir_unop_unpack_int_2x32
:
517 assert(ir
->type
== glsl_type::ivec2_type
);
518 assert(ir
->operands
[0]->type
== glsl_type::int64_t_type
);
521 case ir_unop_unpack_uint_2x32
:
522 assert(ir
->type
== glsl_type::uvec2_type
);
523 assert(ir
->operands
[0]->type
== glsl_type::uint64_t_type
);
526 case ir_unop_unpack_sampler_2x32
:
527 assert(ir
->type
== glsl_type::uvec2_type
);
528 assert(ir
->operands
[0]->type
->is_sampler());
531 case ir_unop_unpack_image_2x32
:
532 assert(ir
->type
== glsl_type::uvec2_type
);
533 assert(ir
->operands
[0]->type
->is_image());
536 case ir_unop_bitfield_reverse
:
537 assert(ir
->operands
[0]->type
== ir
->type
);
538 assert(ir
->type
->is_integer());
541 case ir_unop_bit_count
:
542 case ir_unop_find_msb
:
543 case ir_unop_find_lsb
:
544 assert(ir
->operands
[0]->type
->vector_elements
== ir
->type
->vector_elements
);
545 assert(ir
->operands
[0]->type
->is_integer());
546 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
550 /* XXX what can we assert here? */
553 case ir_unop_interpolate_at_centroid
:
554 assert(ir
->operands
[0]->type
== ir
->type
);
555 assert(ir
->operands
[0]->type
->is_float());
558 case ir_unop_get_buffer_size
:
559 assert(ir
->type
== glsl_type::int_type
);
560 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
563 case ir_unop_ssbo_unsized_array_length
:
564 assert(ir
->type
== glsl_type::int_type
);
565 assert(ir
->operands
[0]->type
->is_array());
566 assert(ir
->operands
[0]->type
->is_unsized_array());
570 assert(ir
->operands
[0]->type
->is_double());
571 assert(ir
->type
->is_float());
574 assert(ir
->operands
[0]->type
->is_float());
575 assert(ir
->type
->is_double());
578 assert(ir
->operands
[0]->type
->is_double());
579 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
582 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
583 assert(ir
->type
->is_double());
586 assert(ir
->operands
[0]->type
->is_double());
587 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
590 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
591 assert(ir
->type
->is_double());
594 assert(ir
->operands
[0]->type
->is_double());
595 assert(ir
->type
->is_boolean());
598 case ir_unop_frexp_sig
:
599 assert(ir
->operands
[0]->type
->is_float() ||
600 ir
->operands
[0]->type
->is_double());
601 assert(ir
->type
->is_double());
603 case ir_unop_frexp_exp
:
604 assert(ir
->operands
[0]->type
->is_float() ||
605 ir
->operands
[0]->type
->is_double());
606 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
608 case ir_unop_subroutine_to_int
:
609 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_SUBROUTINE
);
610 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
621 assert(ir
->operands
[0]->type
->base_type
==
622 ir
->operands
[1]->type
->base_type
);
624 if (ir
->operation
== ir_binop_mul
&&
625 (ir
->type
->base_type
== GLSL_TYPE_UINT64
||
626 ir
->type
->base_type
== GLSL_TYPE_INT64
) &&
627 (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
||
628 ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
||
629 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
||
630 ir
->operands
[1]->type
->base_type
== GLSL_TYPE_UINT
)) {
631 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
635 if (ir
->operands
[0]->type
->is_scalar())
636 assert(ir
->operands
[1]->type
== ir
->type
);
637 else if (ir
->operands
[1]->type
->is_scalar())
638 assert(ir
->operands
[0]->type
== ir
->type
);
639 else if (ir
->operands
[0]->type
->is_vector() &&
640 ir
->operands
[1]->type
->is_vector()) {
641 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
642 assert(ir
->operands
[0]->type
== ir
->type
);
646 case ir_binop_imul_high
:
647 assert(ir
->type
== ir
->operands
[0]->type
);
648 assert(ir
->type
== ir
->operands
[1]->type
);
649 assert(ir
->type
->is_integer());
653 case ir_binop_borrow
:
654 assert(ir
->type
== ir
->operands
[0]->type
);
655 assert(ir
->type
== ir
->operands
[1]->type
);
656 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
660 case ir_binop_gequal
:
662 case ir_binop_nequal
:
663 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
664 * ==, and != operators. The IR operators perform a component-wise
665 * comparison on scalar or vector types and return a boolean scalar or
666 * vector type of the same size.
668 assert(ir
->type
->is_boolean());
669 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
670 assert(ir
->operands
[0]->type
->is_vector()
671 || ir
->operands
[0]->type
->is_scalar());
672 assert(ir
->operands
[0]->type
->vector_elements
673 == ir
->type
->vector_elements
);
676 case ir_binop_all_equal
:
677 case ir_binop_any_nequal
:
678 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
679 * return a scalar boolean. The IR matches that.
681 assert(ir
->type
== glsl_type::bool_type
);
682 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
685 case ir_binop_lshift
:
686 case ir_binop_rshift
:
687 assert(ir
->operands
[0]->type
->is_integer_32_64() &&
688 ir
->operands
[1]->type
->is_integer());
689 if (ir
->operands
[0]->type
->is_scalar()) {
690 assert(ir
->operands
[1]->type
->is_scalar());
692 if (ir
->operands
[0]->type
->is_vector() &&
693 ir
->operands
[1]->type
->is_vector()) {
694 assert(ir
->operands
[0]->type
->components() ==
695 ir
->operands
[1]->type
->components());
697 assert(ir
->type
== ir
->operands
[0]->type
);
700 case ir_binop_bit_and
:
701 case ir_binop_bit_xor
:
702 case ir_binop_bit_or
:
703 assert(ir
->operands
[0]->type
->base_type
==
704 ir
->operands
[1]->type
->base_type
);
705 assert(ir
->type
->is_integer_32_64());
706 if (ir
->operands
[0]->type
->is_vector() &&
707 ir
->operands
[1]->type
->is_vector()) {
708 assert(ir
->operands
[0]->type
->vector_elements
==
709 ir
->operands
[1]->type
->vector_elements
);
713 case ir_binop_logic_and
:
714 case ir_binop_logic_xor
:
715 case ir_binop_logic_or
:
716 assert(ir
->type
->is_boolean());
717 assert(ir
->operands
[0]->type
->is_boolean());
718 assert(ir
->operands
[1]->type
->is_boolean());
722 assert(ir
->type
== glsl_type::float_type
||
723 ir
->type
== glsl_type::double_type
);
724 assert(ir
->operands
[0]->type
->is_float() ||
725 ir
->operands
[0]->type
->is_double());
726 assert(ir
->operands
[0]->type
->is_vector());
727 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
730 case ir_binop_ubo_load
:
731 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
733 assert(ir
->operands
[1]->type
== glsl_type::uint_type
);
737 assert(ir
->operands
[0]->type
== ir
->type
);
738 assert(ir
->operands
[0]->type
->is_float() ||
739 ir
->operands
[0]->type
->is_double());
740 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
);
741 assert(ir
->operands
[0]->type
->components() ==
742 ir
->operands
[1]->type
->components());
745 case ir_binop_vector_extract
:
746 assert(ir
->operands
[0]->type
->is_vector());
747 assert(ir
->operands
[1]->type
->is_scalar()
748 && ir
->operands
[1]->type
->is_integer());
751 case ir_binop_interpolate_at_offset
:
752 assert(ir
->operands
[0]->type
== ir
->type
);
753 assert(ir
->operands
[0]->type
->is_float());
754 assert(ir
->operands
[1]->type
->components() == 2);
755 assert(ir
->operands
[1]->type
->is_float());
758 case ir_binop_interpolate_at_sample
:
759 assert(ir
->operands
[0]->type
== ir
->type
);
760 assert(ir
->operands
[0]->type
->is_float());
761 assert(ir
->operands
[1]->type
== glsl_type::int_type
);
765 assert(ir
->type
->is_float() ||
766 ir
->type
->is_double());
767 assert(ir
->type
== ir
->operands
[0]->type
);
768 assert(ir
->type
== ir
->operands
[1]->type
);
769 assert(ir
->type
== ir
->operands
[2]->type
);
773 assert(ir
->operands
[0]->type
->is_float() ||
774 ir
->operands
[0]->type
->is_double());
775 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
776 assert(ir
->operands
[2]->type
== ir
->operands
[0]->type
||
777 ir
->operands
[2]->type
== glsl_type::float_type
||
778 ir
->operands
[2]->type
== glsl_type::double_type
);
782 assert(ir
->operands
[0]->type
->is_boolean());
783 assert(ir
->type
->vector_elements
== ir
->operands
[0]->type
->vector_elements
);
784 assert(ir
->type
== ir
->operands
[1]->type
);
785 assert(ir
->type
== ir
->operands
[2]->type
);
788 case ir_triop_bitfield_extract
:
789 assert(ir
->type
->is_integer());
790 assert(ir
->operands
[0]->type
== ir
->type
);
791 assert(ir
->operands
[1]->type
== ir
->type
);
792 assert(ir
->operands
[2]->type
== ir
->type
);
795 case ir_triop_vector_insert
:
796 assert(ir
->operands
[0]->type
->is_vector());
797 assert(ir
->operands
[1]->type
->is_scalar());
798 assert(ir
->operands
[0]->type
->base_type
== ir
->operands
[1]->type
->base_type
);
799 assert(ir
->operands
[2]->type
->is_scalar()
800 && ir
->operands
[2]->type
->is_integer());
801 assert(ir
->type
== ir
->operands
[0]->type
);
804 case ir_quadop_bitfield_insert
:
805 assert(ir
->type
->is_integer());
806 assert(ir
->operands
[0]->type
== ir
->type
);
807 assert(ir
->operands
[1]->type
== ir
->type
);
808 assert(ir
->operands
[2]->type
== ir
->type
);
809 assert(ir
->operands
[3]->type
== ir
->type
);
812 case ir_quadop_vector
:
813 /* The vector operator collects some number of scalars and generates a
816 * - All of the operands must be scalar.
817 * - Number of operands must matche the size of the resulting vector.
818 * - Base type of the operands must match the base type of the result.
820 assert(ir
->type
->is_vector());
821 switch (ir
->type
->vector_elements
) {
823 assert(ir
->operands
[0]->type
->is_scalar());
824 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
825 assert(ir
->operands
[1]->type
->is_scalar());
826 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
827 assert(ir
->operands
[2] == NULL
);
828 assert(ir
->operands
[3] == NULL
);
831 assert(ir
->operands
[0]->type
->is_scalar());
832 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
833 assert(ir
->operands
[1]->type
->is_scalar());
834 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
835 assert(ir
->operands
[2]->type
->is_scalar());
836 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
837 assert(ir
->operands
[3] == NULL
);
840 assert(ir
->operands
[0]->type
->is_scalar());
841 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
842 assert(ir
->operands
[1]->type
->is_scalar());
843 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
844 assert(ir
->operands
[2]->type
->is_scalar());
845 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
846 assert(ir
->operands
[3]->type
->is_scalar());
847 assert(ir
->operands
[3]->type
->base_type
== ir
->type
->base_type
);
850 /* The is_vector assertion above should prevent execution from ever
853 assert(!"Should not get here.");
858 return visit_continue
;
862 ir_validate::visit_leave(ir_swizzle
*ir
)
864 unsigned int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
866 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
867 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
868 printf("ir_swizzle @ %p specifies a channel not present "
869 "in the value.\n", (void *) ir
);
875 return visit_continue
;
879 ir_validate::visit(ir_variable
*ir
)
881 /* An ir_variable is the one thing that can (and will) appear multiple times
882 * in an IR tree. It is added to the hashtable so that it can be used
883 * in the ir_dereference_variable handler to ensure that a variable is
884 * declared before it is dereferenced.
886 if (ir
->name
&& ir
->is_name_ralloced())
887 assert(ralloc_parent(ir
->name
) == ir
);
889 _mesa_set_add(ir_set
, ir
);
891 /* If a variable is an array, verify that the maximum array index is in
892 * bounds. There was once an error in AST-to-HIR conversion that set this
893 * to be out of bounds.
895 if (ir
->type
->array_size() > 0) {
896 if (ir
->data
.max_array_access
>= (int)ir
->type
->length
) {
897 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
898 ir
->data
.max_array_access
, ir
->type
->length
- 1);
904 /* If a variable is an interface block (or an array of interface blocks),
905 * verify that the maximum array index for each interface member is in
908 if (ir
->is_interface_instance()) {
909 const glsl_struct_field
*fields
=
910 ir
->get_interface_type()->fields
.structure
;
911 for (unsigned i
= 0; i
< ir
->get_interface_type()->length
; i
++) {
912 if (fields
[i
].type
->array_size() > 0 &&
913 !fields
[i
].implicit_sized_array
) {
914 const int *const max_ifc_array_access
=
915 ir
->get_max_ifc_array_access();
917 assert(max_ifc_array_access
!= NULL
);
919 if (max_ifc_array_access
[i
] >= (int)fields
[i
].type
->length
) {
920 printf("ir_variable has maximum access out of bounds for "
921 "field %s (%d vs %d)\n", fields
[i
].name
,
922 max_ifc_array_access
[i
], fields
[i
].type
->length
);
930 if (ir
->constant_initializer
!= NULL
&& !ir
->data
.has_initializer
) {
931 printf("ir_variable didn't have an initializer, but has a constant "
932 "initializer value.\n");
937 if (ir
->data
.mode
== ir_var_uniform
938 && is_gl_identifier(ir
->name
)
939 && ir
->get_state_slots() == NULL
) {
940 printf("built-in uniform has no state\n");
945 return visit_continue
;
949 ir_validate::visit_enter(ir_assignment
*ir
)
951 const ir_dereference
*const lhs
= ir
->lhs
;
952 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
953 if (ir
->write_mask
== 0) {
954 printf("Assignment LHS is %s, but write mask is 0:\n",
955 lhs
->type
->is_scalar() ? "scalar" : "vector");
960 int lhs_components
= 0;
961 for (int i
= 0; i
< 4; i
++) {
962 if (ir
->write_mask
& (1 << i
))
966 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
967 printf("Assignment count of LHS write mask channels enabled not\n"
968 "matching RHS vector size (%d LHS, %d RHS).\n",
969 lhs_components
, ir
->rhs
->type
->vector_elements
);
975 this->validate_ir(ir
, this->data_enter
);
977 return visit_continue
;
981 ir_validate::visit_enter(ir_call
*ir
)
983 ir_function_signature
*const callee
= ir
->callee
;
985 if (callee
->ir_type
!= ir_type_function_signature
) {
986 printf("IR called by ir_call is not ir_function_signature!\n");
990 if (ir
->return_deref
) {
991 if (ir
->return_deref
->type
!= callee
->return_type
) {
992 printf("callee type %s does not match return storage type %s\n",
993 callee
->return_type
->name
, ir
->return_deref
->type
->name
);
996 } else if (callee
->return_type
!= glsl_type::void_type
) {
997 printf("ir_call has non-void callee but no return storage\n");
1001 const exec_node
*formal_param_node
= callee
->parameters
.get_head_raw();
1002 const exec_node
*actual_param_node
= ir
->actual_parameters
.get_head_raw();
1004 if (formal_param_node
->is_tail_sentinel()
1005 != actual_param_node
->is_tail_sentinel()) {
1006 printf("ir_call has the wrong number of parameters:\n");
1009 if (formal_param_node
->is_tail_sentinel()) {
1012 const ir_variable
*formal_param
1013 = (const ir_variable
*) formal_param_node
;
1014 const ir_rvalue
*actual_param
1015 = (const ir_rvalue
*) actual_param_node
;
1016 if (formal_param
->type
!= actual_param
->type
) {
1017 printf("ir_call parameter type mismatch:\n");
1020 if (formal_param
->data
.mode
== ir_var_function_out
1021 || formal_param
->data
.mode
== ir_var_function_inout
) {
1022 if (!actual_param
->is_lvalue()) {
1023 printf("ir_call out/inout parameters must be lvalues:\n");
1027 formal_param_node
= formal_param_node
->next
;
1028 actual_param_node
= actual_param_node
->next
;
1031 return visit_continue
;
1035 printf("callee:\n");
1042 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
1044 struct set
*ir_set
= (struct set
*) data
;
1046 if (_mesa_set_search(ir_set
, ir
)) {
1047 printf("Instruction node present twice in ir tree:\n");
1052 _mesa_set_add(ir_set
, ir
);
1055 MAYBE_UNUSED
static void
1056 check_node_type(ir_instruction
*ir
, void *data
)
1060 if (ir
->ir_type
>= ir_type_max
) {
1061 printf("Instruction node with unset type\n");
1062 ir
->print(); printf("\n");
1064 ir_rvalue
*value
= ir
->as_rvalue();
1066 assert(value
->type
!= glsl_type::error_type
);
1070 validate_ir_tree(exec_list
*instructions
)
1072 /* We shouldn't have any reason to validate IR in a release build,
1073 * and it's half composed of assert()s anyway which wouldn't do
1079 v
.run(instructions
);
1081 foreach_in_list(ir_instruction
, ir
, instructions
) {
1082 visit_tree(ir
, check_node_type
, NULL
);