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"
40 #include "glsl_types.h"
44 class ir_validate
: public ir_hierarchical_visitor
{
48 this->ir_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
49 _mesa_key_pointer_equal
);
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 printf("ir_dereference_array @ %p does not specify an array or a "
122 if (!ir
->array_index
->type
->is_scalar()) {
123 printf("ir_dereference_array @ %p does not have scalar index: %s\n",
124 (void *) ir
, ir
->array_index
->type
->name
);
128 if (!ir
->array_index
->type
->is_integer()) {
129 printf("ir_dereference_array @ %p does not have integer index: %s\n",
130 (void *) ir
, ir
->array_index
->type
->name
);
134 return visit_continue
;
138 ir_validate::visit_enter(ir_discard
*ir
)
140 if (ir
->condition
&& ir
->condition
->type
!= glsl_type::bool_type
) {
141 printf("ir_discard condition %s type instead of bool.\n",
142 ir
->condition
->type
->name
);
148 return visit_continue
;
152 ir_validate::visit_enter(ir_if
*ir
)
154 if (ir
->condition
->type
!= glsl_type::bool_type
) {
155 printf("ir_if condition %s type instead of bool.\n",
156 ir
->condition
->type
->name
);
162 return visit_continue
;
167 ir_validate::visit_enter(ir_function
*ir
)
169 /* Function definitions cannot be nested.
171 if (this->current_function
!= NULL
) {
172 printf("Function definition nested inside another function "
174 printf("%s %p inside %s %p\n",
175 ir
->name
, (void *) ir
,
176 this->current_function
->name
, (void *) this->current_function
);
180 /* Store the current function hierarchy being traversed. This is used
181 * by the function signature visitor to ensure that the signatures are
182 * linked with the correct functions.
184 this->current_function
= ir
;
186 this->validate_ir(ir
, this->data_enter
);
188 /* Verify that all of the things stored in the list of signatures are,
189 * in fact, function signatures.
191 foreach_in_list(ir_instruction
, sig
, &ir
->signatures
) {
192 if (sig
->ir_type
!= ir_type_function_signature
) {
193 printf("Non-signature in signature list of function `%s'\n",
199 return visit_continue
;
203 ir_validate::visit_leave(ir_function
*ir
)
205 assert(ralloc_parent(ir
->name
) == ir
);
207 this->current_function
= NULL
;
208 return visit_continue
;
212 ir_validate::visit_enter(ir_function_signature
*ir
)
214 if (this->current_function
!= ir
->function()) {
215 printf("Function signature nested inside wrong function "
217 printf("%p inside %s %p instead of %s %p\n",
219 this->current_function
->name
, (void *) this->current_function
,
220 ir
->function_name(), (void *) ir
->function());
224 if (ir
->return_type
== NULL
) {
225 printf("Function signature %p for function %s has NULL return type.\n",
226 (void *) ir
, ir
->function_name());
230 this->validate_ir(ir
, this->data_enter
);
232 return visit_continue
;
236 ir_validate::visit_leave(ir_expression
*ir
)
238 switch (ir
->operation
) {
239 case ir_unop_bit_not
:
240 assert(ir
->operands
[0]->type
== ir
->type
);
242 case ir_unop_logic_not
:
243 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
244 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
253 assert(ir
->type
== ir
->operands
[0]->type
);
260 case ir_unop_saturate
:
261 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
262 assert(ir
->type
== ir
->operands
[0]->type
);
266 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
267 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
270 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
271 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
274 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
275 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
278 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
279 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
282 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
283 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
286 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
287 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
290 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
291 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
294 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
295 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
298 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
299 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
302 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
303 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
305 case ir_unop_bitcast_i2f
:
306 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
307 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
309 case ir_unop_bitcast_f2i
:
310 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
311 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
313 case ir_unop_bitcast_u2f
:
314 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
315 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
317 case ir_unop_bitcast_f2u
:
318 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
319 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
323 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
324 assert(ir
->type
== glsl_type::bool_type
);
328 case ir_unop_round_even
:
332 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
333 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
334 assert(ir
->operands
[0]->type
== ir
->type
);
339 case ir_unop_dFdx_coarse
:
340 case ir_unop_dFdx_fine
:
342 case ir_unop_dFdy_coarse
:
343 case ir_unop_dFdy_fine
:
344 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
345 assert(ir
->operands
[0]->type
== ir
->type
);
348 case ir_unop_pack_snorm_2x16
:
349 case ir_unop_pack_unorm_2x16
:
350 case ir_unop_pack_half_2x16
:
351 assert(ir
->type
== glsl_type::uint_type
);
352 assert(ir
->operands
[0]->type
== glsl_type::vec2_type
);
355 case ir_unop_pack_snorm_4x8
:
356 case ir_unop_pack_unorm_4x8
:
357 assert(ir
->type
== glsl_type::uint_type
);
358 assert(ir
->operands
[0]->type
== glsl_type::vec4_type
);
361 case ir_unop_pack_double_2x32
:
362 assert(ir
->type
== glsl_type::double_type
);
363 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
366 case ir_unop_unpack_snorm_2x16
:
367 case ir_unop_unpack_unorm_2x16
:
368 case ir_unop_unpack_half_2x16
:
369 assert(ir
->type
== glsl_type::vec2_type
);
370 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
373 case ir_unop_unpack_snorm_4x8
:
374 case ir_unop_unpack_unorm_4x8
:
375 assert(ir
->type
== glsl_type::vec4_type
);
376 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
379 case ir_unop_unpack_half_2x16_split_x
:
380 case ir_unop_unpack_half_2x16_split_y
:
381 assert(ir
->type
== glsl_type::float_type
);
382 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
385 case ir_unop_unpack_double_2x32
:
386 assert(ir
->type
== glsl_type::uvec2_type
);
387 assert(ir
->operands
[0]->type
== glsl_type::double_type
);
390 case ir_unop_bitfield_reverse
:
391 assert(ir
->operands
[0]->type
== ir
->type
);
392 assert(ir
->type
->is_integer());
395 case ir_unop_bit_count
:
396 case ir_unop_find_msb
:
397 case ir_unop_find_lsb
:
398 assert(ir
->operands
[0]->type
->vector_elements
== ir
->type
->vector_elements
);
399 assert(ir
->operands
[0]->type
->is_integer());
400 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
404 /* XXX what can we assert here? */
407 case ir_unop_interpolate_at_centroid
:
408 assert(ir
->operands
[0]->type
== ir
->type
);
409 assert(ir
->operands
[0]->type
->is_float());
412 case ir_unop_get_buffer_size
:
413 assert(ir
->type
== glsl_type::int_type
);
414 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
417 case ir_unop_ssbo_unsized_array_length
:
418 assert(ir
->type
== glsl_type::int_type
);
419 assert(ir
->operands
[0]->type
->is_array());
420 assert(ir
->operands
[0]->type
->is_unsized_array());
424 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
425 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
428 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
429 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
432 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
433 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
436 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
437 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
440 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
441 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
444 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
445 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
448 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
449 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
452 case ir_unop_frexp_sig
:
453 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
454 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
455 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
457 case ir_unop_frexp_exp
:
458 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
459 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
460 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
462 case ir_unop_subroutine_to_int
:
463 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_SUBROUTINE
);
464 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
474 assert(ir
->operands
[0]->type
->base_type
==
475 ir
->operands
[1]->type
->base_type
);
477 if (ir
->operands
[0]->type
->is_scalar())
478 assert(ir
->operands
[1]->type
== ir
->type
);
479 else if (ir
->operands
[1]->type
->is_scalar())
480 assert(ir
->operands
[0]->type
== ir
->type
);
481 else if (ir
->operands
[0]->type
->is_vector() &&
482 ir
->operands
[1]->type
->is_vector()) {
483 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
484 assert(ir
->operands
[0]->type
== ir
->type
);
488 case ir_binop_imul_high
:
489 assert(ir
->type
== ir
->operands
[0]->type
);
490 assert(ir
->type
== ir
->operands
[1]->type
);
491 assert(ir
->type
->is_integer());
495 case ir_binop_borrow
:
496 assert(ir
->type
== ir
->operands
[0]->type
);
497 assert(ir
->type
== ir
->operands
[1]->type
);
498 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
502 case ir_binop_greater
:
503 case ir_binop_lequal
:
504 case ir_binop_gequal
:
506 case ir_binop_nequal
:
507 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
508 * ==, and != operators. The IR operators perform a component-wise
509 * comparison on scalar or vector types and return a boolean scalar or
510 * vector type of the same size.
512 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
513 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
514 assert(ir
->operands
[0]->type
->is_vector()
515 || ir
->operands
[0]->type
->is_scalar());
516 assert(ir
->operands
[0]->type
->vector_elements
517 == ir
->type
->vector_elements
);
520 case ir_binop_all_equal
:
521 case ir_binop_any_nequal
:
522 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
523 * return a scalar boolean. The IR matches that.
525 assert(ir
->type
== glsl_type::bool_type
);
526 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
529 case ir_binop_lshift
:
530 case ir_binop_rshift
:
531 assert(ir
->operands
[0]->type
->is_integer() &&
532 ir
->operands
[1]->type
->is_integer());
533 if (ir
->operands
[0]->type
->is_scalar()) {
534 assert(ir
->operands
[1]->type
->is_scalar());
536 if (ir
->operands
[0]->type
->is_vector() &&
537 ir
->operands
[1]->type
->is_vector()) {
538 assert(ir
->operands
[0]->type
->components() ==
539 ir
->operands
[1]->type
->components());
541 assert(ir
->type
== ir
->operands
[0]->type
);
544 case ir_binop_bit_and
:
545 case ir_binop_bit_xor
:
546 case ir_binop_bit_or
:
547 assert(ir
->operands
[0]->type
->base_type
==
548 ir
->operands
[1]->type
->base_type
);
549 assert(ir
->type
->is_integer());
550 if (ir
->operands
[0]->type
->is_vector() &&
551 ir
->operands
[1]->type
->is_vector()) {
552 assert(ir
->operands
[0]->type
->vector_elements
==
553 ir
->operands
[1]->type
->vector_elements
);
557 case ir_binop_logic_and
:
558 case ir_binop_logic_xor
:
559 case ir_binop_logic_or
:
560 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
561 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
562 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_BOOL
);
566 assert(ir
->type
== glsl_type::float_type
||
567 ir
->type
== glsl_type::double_type
);
568 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
569 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
570 assert(ir
->operands
[0]->type
->is_vector());
571 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
574 case ir_binop_pack_half_2x16_split
:
575 assert(ir
->type
== glsl_type::uint_type
);
576 assert(ir
->operands
[0]->type
== glsl_type::float_type
);
577 assert(ir
->operands
[1]->type
== glsl_type::float_type
);
581 assert(ir
->type
->is_integer());
582 assert(ir
->operands
[0]->type
->is_integer());
583 assert(ir
->operands
[1]->type
->is_integer());
586 case ir_binop_ubo_load
:
587 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
589 assert(ir
->operands
[1]->type
== glsl_type::uint_type
);
593 assert(ir
->operands
[0]->type
== ir
->type
);
594 assert(ir
->operands
[0]->type
->is_float() ||
595 ir
->operands
[0]->type
->is_double());
596 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
);
597 assert(ir
->operands
[0]->type
->components() ==
598 ir
->operands
[1]->type
->components());
601 case ir_binop_vector_extract
:
602 assert(ir
->operands
[0]->type
->is_vector());
603 assert(ir
->operands
[1]->type
->is_scalar()
604 && ir
->operands
[1]->type
->is_integer());
607 case ir_binop_interpolate_at_offset
:
608 assert(ir
->operands
[0]->type
== ir
->type
);
609 assert(ir
->operands
[0]->type
->is_float());
610 assert(ir
->operands
[1]->type
->components() == 2);
611 assert(ir
->operands
[1]->type
->is_float());
614 case ir_binop_interpolate_at_sample
:
615 assert(ir
->operands
[0]->type
== ir
->type
);
616 assert(ir
->operands
[0]->type
->is_float());
617 assert(ir
->operands
[1]->type
== glsl_type::int_type
);
621 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
||
622 ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
623 assert(ir
->type
== ir
->operands
[0]->type
);
624 assert(ir
->type
== ir
->operands
[1]->type
);
625 assert(ir
->type
== ir
->operands
[2]->type
);
629 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
630 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
631 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
632 assert(ir
->operands
[2]->type
== ir
->operands
[0]->type
||
633 ir
->operands
[2]->type
== glsl_type::float_type
||
634 ir
->operands
[2]->type
== glsl_type::double_type
);
638 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
639 assert(ir
->type
->vector_elements
== ir
->operands
[0]->type
->vector_elements
);
640 assert(ir
->type
== ir
->operands
[1]->type
);
641 assert(ir
->type
== ir
->operands
[2]->type
);
645 assert(ir
->operands
[0]->type
->is_integer());
646 assert(ir
->operands
[1]->type
== ir
->operands
[2]->type
);
647 assert(ir
->operands
[1]->type
== ir
->type
);
650 case ir_triop_bitfield_extract
:
651 assert(ir
->operands
[0]->type
== ir
->type
);
652 assert(ir
->operands
[1]->type
== glsl_type::int_type
);
653 assert(ir
->operands
[2]->type
== glsl_type::int_type
);
656 case ir_triop_vector_insert
:
657 assert(ir
->operands
[0]->type
->is_vector());
658 assert(ir
->operands
[1]->type
->is_scalar());
659 assert(ir
->operands
[0]->type
->base_type
== ir
->operands
[1]->type
->base_type
);
660 assert(ir
->operands
[2]->type
->is_scalar()
661 && ir
->operands
[2]->type
->is_integer());
662 assert(ir
->type
== ir
->operands
[0]->type
);
665 case ir_quadop_bitfield_insert
:
666 assert(ir
->operands
[0]->type
== ir
->type
);
667 assert(ir
->operands
[1]->type
== ir
->type
);
668 assert(ir
->operands
[2]->type
== glsl_type::int_type
);
669 assert(ir
->operands
[3]->type
== glsl_type::int_type
);
672 case ir_quadop_vector
:
673 /* The vector operator collects some number of scalars and generates a
676 * - All of the operands must be scalar.
677 * - Number of operands must matche the size of the resulting vector.
678 * - Base type of the operands must match the base type of the result.
680 assert(ir
->type
->is_vector());
681 switch (ir
->type
->vector_elements
) {
683 assert(ir
->operands
[0]->type
->is_scalar());
684 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
685 assert(ir
->operands
[1]->type
->is_scalar());
686 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
687 assert(ir
->operands
[2] == NULL
);
688 assert(ir
->operands
[3] == NULL
);
691 assert(ir
->operands
[0]->type
->is_scalar());
692 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
693 assert(ir
->operands
[1]->type
->is_scalar());
694 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
695 assert(ir
->operands
[2]->type
->is_scalar());
696 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
697 assert(ir
->operands
[3] == NULL
);
700 assert(ir
->operands
[0]->type
->is_scalar());
701 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
702 assert(ir
->operands
[1]->type
->is_scalar());
703 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
704 assert(ir
->operands
[2]->type
->is_scalar());
705 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
706 assert(ir
->operands
[3]->type
->is_scalar());
707 assert(ir
->operands
[3]->type
->base_type
== ir
->type
->base_type
);
710 /* The is_vector assertion above should prevent execution from ever
713 assert(!"Should not get here.");
718 return visit_continue
;
722 ir_validate::visit_leave(ir_swizzle
*ir
)
724 unsigned int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
726 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
727 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
728 printf("ir_swizzle @ %p specifies a channel not present "
729 "in the value.\n", (void *) ir
);
735 return visit_continue
;
739 ir_validate::visit(ir_variable
*ir
)
741 /* An ir_variable is the one thing that can (and will) appear multiple times
742 * in an IR tree. It is added to the hashtable so that it can be used
743 * in the ir_dereference_variable handler to ensure that a variable is
744 * declared before it is dereferenced.
746 if (ir
->name
&& ir
->is_name_ralloced())
747 assert(ralloc_parent(ir
->name
) == ir
);
749 _mesa_set_add(ir_set
, ir
);
751 /* If a variable is an array, verify that the maximum array index is in
752 * bounds. There was once an error in AST-to-HIR conversion that set this
753 * to be out of bounds.
755 if (ir
->type
->array_size() > 0) {
756 if (ir
->data
.max_array_access
>= ir
->type
->length
) {
757 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
758 ir
->data
.max_array_access
, ir
->type
->length
- 1);
764 /* If a variable is an interface block (or an array of interface blocks),
765 * verify that the maximum array index for each interface member is in
768 if (ir
->is_interface_instance()) {
769 const glsl_struct_field
*fields
=
770 ir
->get_interface_type()->fields
.structure
;
771 for (unsigned i
= 0; i
< ir
->get_interface_type()->length
; i
++) {
772 if (fields
[i
].type
->array_size() > 0) {
773 const unsigned *const max_ifc_array_access
=
774 ir
->get_max_ifc_array_access();
776 assert(max_ifc_array_access
!= NULL
);
778 if (max_ifc_array_access
[i
] >= fields
[i
].type
->length
) {
779 printf("ir_variable has maximum access out of bounds for "
780 "field %s (%d vs %d)\n", fields
[i
].name
,
781 max_ifc_array_access
[i
], fields
[i
].type
->length
);
789 if (ir
->constant_initializer
!= NULL
&& !ir
->data
.has_initializer
) {
790 printf("ir_variable didn't have an initializer, but has a constant "
791 "initializer value.\n");
796 if (ir
->data
.mode
== ir_var_uniform
797 && is_gl_identifier(ir
->name
)
798 && ir
->get_state_slots() == NULL
) {
799 printf("built-in uniform has no state\n");
804 return visit_continue
;
808 ir_validate::visit_enter(ir_assignment
*ir
)
810 const ir_dereference
*const lhs
= ir
->lhs
;
811 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
812 if (ir
->write_mask
== 0) {
813 printf("Assignment LHS is %s, but write mask is 0:\n",
814 lhs
->type
->is_scalar() ? "scalar" : "vector");
819 int lhs_components
= 0;
820 for (int i
= 0; i
< 4; i
++) {
821 if (ir
->write_mask
& (1 << i
))
825 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
826 printf("Assignment count of LHS write mask channels enabled not\n"
827 "matching RHS vector size (%d LHS, %d RHS).\n",
828 lhs_components
, ir
->rhs
->type
->vector_elements
);
834 this->validate_ir(ir
, this->data_enter
);
836 return visit_continue
;
840 ir_validate::visit_enter(ir_call
*ir
)
842 ir_function_signature
*const callee
= ir
->callee
;
844 if (callee
->ir_type
!= ir_type_function_signature
) {
845 printf("IR called by ir_call is not ir_function_signature!\n");
849 if (ir
->return_deref
) {
850 if (ir
->return_deref
->type
!= callee
->return_type
) {
851 printf("callee type %s does not match return storage type %s\n",
852 callee
->return_type
->name
, ir
->return_deref
->type
->name
);
855 } else if (callee
->return_type
!= glsl_type::void_type
) {
856 printf("ir_call has non-void callee but no return storage\n");
860 const exec_node
*formal_param_node
= callee
->parameters
.head
;
861 const exec_node
*actual_param_node
= ir
->actual_parameters
.head
;
863 if (formal_param_node
->is_tail_sentinel()
864 != actual_param_node
->is_tail_sentinel()) {
865 printf("ir_call has the wrong number of parameters:\n");
868 if (formal_param_node
->is_tail_sentinel()) {
871 const ir_variable
*formal_param
872 = (const ir_variable
*) formal_param_node
;
873 const ir_rvalue
*actual_param
874 = (const ir_rvalue
*) actual_param_node
;
875 if (formal_param
->type
!= actual_param
->type
) {
876 printf("ir_call parameter type mismatch:\n");
879 if (formal_param
->data
.mode
== ir_var_function_out
880 || formal_param
->data
.mode
== ir_var_function_inout
) {
881 if (!actual_param
->is_lvalue()) {
882 printf("ir_call out/inout parameters must be lvalues:\n");
886 formal_param_node
= formal_param_node
->next
;
887 actual_param_node
= actual_param_node
->next
;
890 return visit_continue
;
901 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
903 struct set
*ir_set
= (struct set
*) data
;
905 if (_mesa_set_search(ir_set
, ir
)) {
906 printf("Instruction node present twice in ir tree:\n");
911 _mesa_set_add(ir_set
, ir
);
915 check_node_type(ir_instruction
*ir
, void *data
)
919 if (ir
->ir_type
>= ir_type_max
) {
920 printf("Instruction node with unset type\n");
921 ir
->print(); printf("\n");
923 ir_rvalue
*value
= ir
->as_rvalue();
925 assert(value
->type
!= glsl_type::error_type
);
929 validate_ir_tree(exec_list
*instructions
)
931 /* We shouldn't have any reason to validate IR in a release build,
932 * and it's half composed of assert()s anyway which wouldn't do
940 foreach_in_list(ir_instruction
, ir
, instructions
) {
941 visit_tree(ir
, check_node_type
, NULL
);