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 "compiler/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 !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 switch (ir
->operation
) {
240 case ir_unop_bit_not
:
241 assert(ir
->operands
[0]->type
== ir
->type
);
243 case ir_unop_logic_not
:
244 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
245 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
254 assert(ir
->type
== ir
->operands
[0]->type
);
261 case ir_unop_saturate
:
262 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
263 assert(ir
->type
== ir
->operands
[0]->type
);
267 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
268 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
271 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
272 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
275 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
276 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
279 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
280 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
283 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
284 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
287 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
288 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
291 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
292 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
295 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
296 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
299 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
300 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
303 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
304 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
306 case ir_unop_bitcast_i2f
:
307 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
308 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
310 case ir_unop_bitcast_f2i
:
311 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
312 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
314 case ir_unop_bitcast_u2f
:
315 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
316 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
318 case ir_unop_bitcast_f2u
:
319 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
320 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
324 case ir_unop_round_even
:
328 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
329 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
330 assert(ir
->operands
[0]->type
== ir
->type
);
335 case ir_unop_dFdx_coarse
:
336 case ir_unop_dFdx_fine
:
338 case ir_unop_dFdy_coarse
:
339 case ir_unop_dFdy_fine
:
340 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
341 assert(ir
->operands
[0]->type
== ir
->type
);
344 case ir_unop_pack_snorm_2x16
:
345 case ir_unop_pack_unorm_2x16
:
346 case ir_unop_pack_half_2x16
:
347 assert(ir
->type
== glsl_type::uint_type
);
348 assert(ir
->operands
[0]->type
== glsl_type::vec2_type
);
351 case ir_unop_pack_snorm_4x8
:
352 case ir_unop_pack_unorm_4x8
:
353 assert(ir
->type
== glsl_type::uint_type
);
354 assert(ir
->operands
[0]->type
== glsl_type::vec4_type
);
357 case ir_unop_pack_double_2x32
:
358 assert(ir
->type
== glsl_type::double_type
);
359 assert(ir
->operands
[0]->type
== glsl_type::uvec2_type
);
362 case ir_unop_unpack_snorm_2x16
:
363 case ir_unop_unpack_unorm_2x16
:
364 case ir_unop_unpack_half_2x16
:
365 assert(ir
->type
== glsl_type::vec2_type
);
366 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
369 case ir_unop_unpack_snorm_4x8
:
370 case ir_unop_unpack_unorm_4x8
:
371 assert(ir
->type
== glsl_type::vec4_type
);
372 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
375 case ir_unop_unpack_double_2x32
:
376 assert(ir
->type
== glsl_type::uvec2_type
);
377 assert(ir
->operands
[0]->type
== glsl_type::double_type
);
380 case ir_unop_bitfield_reverse
:
381 assert(ir
->operands
[0]->type
== ir
->type
);
382 assert(ir
->type
->is_integer());
385 case ir_unop_bit_count
:
386 case ir_unop_find_msb
:
387 case ir_unop_find_lsb
:
388 assert(ir
->operands
[0]->type
->vector_elements
== ir
->type
->vector_elements
);
389 assert(ir
->operands
[0]->type
->is_integer());
390 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
394 /* XXX what can we assert here? */
397 case ir_unop_interpolate_at_centroid
:
398 assert(ir
->operands
[0]->type
== ir
->type
);
399 assert(ir
->operands
[0]->type
->is_float());
402 case ir_unop_get_buffer_size
:
403 assert(ir
->type
== glsl_type::int_type
);
404 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
407 case ir_unop_ssbo_unsized_array_length
:
408 assert(ir
->type
== glsl_type::int_type
);
409 assert(ir
->operands
[0]->type
->is_array());
410 assert(ir
->operands
[0]->type
->is_unsized_array());
414 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
415 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
418 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
419 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
422 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
423 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
426 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
);
427 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
430 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
431 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
434 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
);
435 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
438 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
439 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
442 case ir_unop_frexp_sig
:
443 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
444 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
445 assert(ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
447 case ir_unop_frexp_exp
:
448 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
449 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
450 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
452 case ir_unop_subroutine_to_int
:
453 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_SUBROUTINE
);
454 assert(ir
->type
->base_type
== GLSL_TYPE_INT
);
464 assert(ir
->operands
[0]->type
->base_type
==
465 ir
->operands
[1]->type
->base_type
);
467 if (ir
->operands
[0]->type
->is_scalar())
468 assert(ir
->operands
[1]->type
== ir
->type
);
469 else if (ir
->operands
[1]->type
->is_scalar())
470 assert(ir
->operands
[0]->type
== ir
->type
);
471 else if (ir
->operands
[0]->type
->is_vector() &&
472 ir
->operands
[1]->type
->is_vector()) {
473 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
474 assert(ir
->operands
[0]->type
== ir
->type
);
478 case ir_binop_imul_high
:
479 assert(ir
->type
== ir
->operands
[0]->type
);
480 assert(ir
->type
== ir
->operands
[1]->type
);
481 assert(ir
->type
->is_integer());
485 case ir_binop_borrow
:
486 assert(ir
->type
== ir
->operands
[0]->type
);
487 assert(ir
->type
== ir
->operands
[1]->type
);
488 assert(ir
->type
->base_type
== GLSL_TYPE_UINT
);
492 case ir_binop_greater
:
493 case ir_binop_lequal
:
494 case ir_binop_gequal
:
496 case ir_binop_nequal
:
497 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
498 * ==, and != operators. The IR operators perform a component-wise
499 * comparison on scalar or vector types and return a boolean scalar or
500 * vector type of the same size.
502 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
503 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
504 assert(ir
->operands
[0]->type
->is_vector()
505 || ir
->operands
[0]->type
->is_scalar());
506 assert(ir
->operands
[0]->type
->vector_elements
507 == ir
->type
->vector_elements
);
510 case ir_binop_all_equal
:
511 case ir_binop_any_nequal
:
512 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
513 * return a scalar boolean. The IR matches that.
515 assert(ir
->type
== glsl_type::bool_type
);
516 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
519 case ir_binop_lshift
:
520 case ir_binop_rshift
:
521 assert(ir
->operands
[0]->type
->is_integer() &&
522 ir
->operands
[1]->type
->is_integer());
523 if (ir
->operands
[0]->type
->is_scalar()) {
524 assert(ir
->operands
[1]->type
->is_scalar());
526 if (ir
->operands
[0]->type
->is_vector() &&
527 ir
->operands
[1]->type
->is_vector()) {
528 assert(ir
->operands
[0]->type
->components() ==
529 ir
->operands
[1]->type
->components());
531 assert(ir
->type
== ir
->operands
[0]->type
);
534 case ir_binop_bit_and
:
535 case ir_binop_bit_xor
:
536 case ir_binop_bit_or
:
537 assert(ir
->operands
[0]->type
->base_type
==
538 ir
->operands
[1]->type
->base_type
);
539 assert(ir
->type
->is_integer());
540 if (ir
->operands
[0]->type
->is_vector() &&
541 ir
->operands
[1]->type
->is_vector()) {
542 assert(ir
->operands
[0]->type
->vector_elements
==
543 ir
->operands
[1]->type
->vector_elements
);
547 case ir_binop_logic_and
:
548 case ir_binop_logic_xor
:
549 case ir_binop_logic_or
:
550 assert(ir
->type
->base_type
== GLSL_TYPE_BOOL
);
551 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
552 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_BOOL
);
556 assert(ir
->type
== glsl_type::float_type
||
557 ir
->type
== glsl_type::double_type
);
558 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
559 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
560 assert(ir
->operands
[0]->type
->is_vector());
561 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
564 case ir_binop_ubo_load
:
565 assert(ir
->operands
[0]->type
== glsl_type::uint_type
);
567 assert(ir
->operands
[1]->type
== glsl_type::uint_type
);
571 assert(ir
->operands
[0]->type
== ir
->type
);
572 assert(ir
->operands
[0]->type
->is_float() ||
573 ir
->operands
[0]->type
->is_double());
574 assert(ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
);
575 assert(ir
->operands
[0]->type
->components() ==
576 ir
->operands
[1]->type
->components());
579 case ir_binop_vector_extract
:
580 assert(ir
->operands
[0]->type
->is_vector());
581 assert(ir
->operands
[1]->type
->is_scalar()
582 && ir
->operands
[1]->type
->is_integer());
585 case ir_binop_interpolate_at_offset
:
586 assert(ir
->operands
[0]->type
== ir
->type
);
587 assert(ir
->operands
[0]->type
->is_float());
588 assert(ir
->operands
[1]->type
->components() == 2);
589 assert(ir
->operands
[1]->type
->is_float());
592 case ir_binop_interpolate_at_sample
:
593 assert(ir
->operands
[0]->type
== ir
->type
);
594 assert(ir
->operands
[0]->type
->is_float());
595 assert(ir
->operands
[1]->type
== glsl_type::int_type
);
599 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
||
600 ir
->type
->base_type
== GLSL_TYPE_DOUBLE
);
601 assert(ir
->type
== ir
->operands
[0]->type
);
602 assert(ir
->type
== ir
->operands
[1]->type
);
603 assert(ir
->type
== ir
->operands
[2]->type
);
607 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
608 ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
609 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
610 assert(ir
->operands
[2]->type
== ir
->operands
[0]->type
||
611 ir
->operands
[2]->type
== glsl_type::float_type
||
612 ir
->operands
[2]->type
== glsl_type::double_type
);
616 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
617 assert(ir
->type
->vector_elements
== ir
->operands
[0]->type
->vector_elements
);
618 assert(ir
->type
== ir
->operands
[1]->type
);
619 assert(ir
->type
== ir
->operands
[2]->type
);
622 case ir_triop_bitfield_extract
:
623 assert(ir
->type
->is_integer());
624 assert(ir
->operands
[0]->type
== ir
->type
);
625 assert(ir
->operands
[1]->type
== ir
->type
);
626 assert(ir
->operands
[2]->type
== ir
->type
);
629 case ir_triop_vector_insert
:
630 assert(ir
->operands
[0]->type
->is_vector());
631 assert(ir
->operands
[1]->type
->is_scalar());
632 assert(ir
->operands
[0]->type
->base_type
== ir
->operands
[1]->type
->base_type
);
633 assert(ir
->operands
[2]->type
->is_scalar()
634 && ir
->operands
[2]->type
->is_integer());
635 assert(ir
->type
== ir
->operands
[0]->type
);
638 case ir_quadop_bitfield_insert
:
639 assert(ir
->type
->is_integer());
640 assert(ir
->operands
[0]->type
== ir
->type
);
641 assert(ir
->operands
[1]->type
== ir
->type
);
642 assert(ir
->operands
[2]->type
== ir
->type
);
643 assert(ir
->operands
[3]->type
== ir
->type
);
646 case ir_quadop_vector
:
647 /* The vector operator collects some number of scalars and generates a
650 * - All of the operands must be scalar.
651 * - Number of operands must matche the size of the resulting vector.
652 * - Base type of the operands must match the base type of the result.
654 assert(ir
->type
->is_vector());
655 switch (ir
->type
->vector_elements
) {
657 assert(ir
->operands
[0]->type
->is_scalar());
658 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
659 assert(ir
->operands
[1]->type
->is_scalar());
660 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
661 assert(ir
->operands
[2] == NULL
);
662 assert(ir
->operands
[3] == NULL
);
665 assert(ir
->operands
[0]->type
->is_scalar());
666 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
667 assert(ir
->operands
[1]->type
->is_scalar());
668 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
669 assert(ir
->operands
[2]->type
->is_scalar());
670 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
671 assert(ir
->operands
[3] == NULL
);
674 assert(ir
->operands
[0]->type
->is_scalar());
675 assert(ir
->operands
[0]->type
->base_type
== ir
->type
->base_type
);
676 assert(ir
->operands
[1]->type
->is_scalar());
677 assert(ir
->operands
[1]->type
->base_type
== ir
->type
->base_type
);
678 assert(ir
->operands
[2]->type
->is_scalar());
679 assert(ir
->operands
[2]->type
->base_type
== ir
->type
->base_type
);
680 assert(ir
->operands
[3]->type
->is_scalar());
681 assert(ir
->operands
[3]->type
->base_type
== ir
->type
->base_type
);
684 /* The is_vector assertion above should prevent execution from ever
687 assert(!"Should not get here.");
692 return visit_continue
;
696 ir_validate::visit_leave(ir_swizzle
*ir
)
698 unsigned int chans
[4] = {ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
700 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
701 if (chans
[i
] >= ir
->val
->type
->vector_elements
) {
702 printf("ir_swizzle @ %p specifies a channel not present "
703 "in the value.\n", (void *) ir
);
709 return visit_continue
;
713 ir_validate::visit(ir_variable
*ir
)
715 /* An ir_variable is the one thing that can (and will) appear multiple times
716 * in an IR tree. It is added to the hashtable so that it can be used
717 * in the ir_dereference_variable handler to ensure that a variable is
718 * declared before it is dereferenced.
720 if (ir
->name
&& ir
->is_name_ralloced())
721 assert(ralloc_parent(ir
->name
) == ir
);
723 _mesa_set_add(ir_set
, ir
);
725 /* If a variable is an array, verify that the maximum array index is in
726 * bounds. There was once an error in AST-to-HIR conversion that set this
727 * to be out of bounds.
729 if (ir
->type
->array_size() > 0) {
730 if (ir
->data
.max_array_access
>= ir
->type
->length
) {
731 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
732 ir
->data
.max_array_access
, ir
->type
->length
- 1);
738 /* If a variable is an interface block (or an array of interface blocks),
739 * verify that the maximum array index for each interface member is in
742 if (ir
->is_interface_instance()) {
743 const glsl_struct_field
*fields
=
744 ir
->get_interface_type()->fields
.structure
;
745 for (unsigned i
= 0; i
< ir
->get_interface_type()->length
; i
++) {
746 if (fields
[i
].type
->array_size() > 0) {
747 const unsigned *const max_ifc_array_access
=
748 ir
->get_max_ifc_array_access();
750 assert(max_ifc_array_access
!= NULL
);
752 if (max_ifc_array_access
[i
] >= fields
[i
].type
->length
) {
753 printf("ir_variable has maximum access out of bounds for "
754 "field %s (%d vs %d)\n", fields
[i
].name
,
755 max_ifc_array_access
[i
], fields
[i
].type
->length
);
763 if (ir
->constant_initializer
!= NULL
&& !ir
->data
.has_initializer
) {
764 printf("ir_variable didn't have an initializer, but has a constant "
765 "initializer value.\n");
770 if (ir
->data
.mode
== ir_var_uniform
771 && is_gl_identifier(ir
->name
)
772 && ir
->get_state_slots() == NULL
) {
773 printf("built-in uniform has no state\n");
778 return visit_continue
;
782 ir_validate::visit_enter(ir_assignment
*ir
)
784 const ir_dereference
*const lhs
= ir
->lhs
;
785 if (lhs
->type
->is_scalar() || lhs
->type
->is_vector()) {
786 if (ir
->write_mask
== 0) {
787 printf("Assignment LHS is %s, but write mask is 0:\n",
788 lhs
->type
->is_scalar() ? "scalar" : "vector");
793 int lhs_components
= 0;
794 for (int i
= 0; i
< 4; i
++) {
795 if (ir
->write_mask
& (1 << i
))
799 if (lhs_components
!= ir
->rhs
->type
->vector_elements
) {
800 printf("Assignment count of LHS write mask channels enabled not\n"
801 "matching RHS vector size (%d LHS, %d RHS).\n",
802 lhs_components
, ir
->rhs
->type
->vector_elements
);
808 this->validate_ir(ir
, this->data_enter
);
810 return visit_continue
;
814 ir_validate::visit_enter(ir_call
*ir
)
816 ir_function_signature
*const callee
= ir
->callee
;
818 if (callee
->ir_type
!= ir_type_function_signature
) {
819 printf("IR called by ir_call is not ir_function_signature!\n");
823 if (ir
->return_deref
) {
824 if (ir
->return_deref
->type
!= callee
->return_type
) {
825 printf("callee type %s does not match return storage type %s\n",
826 callee
->return_type
->name
, ir
->return_deref
->type
->name
);
829 } else if (callee
->return_type
!= glsl_type::void_type
) {
830 printf("ir_call has non-void callee but no return storage\n");
834 const exec_node
*formal_param_node
= callee
->parameters
.head
;
835 const exec_node
*actual_param_node
= ir
->actual_parameters
.head
;
837 if (formal_param_node
->is_tail_sentinel()
838 != actual_param_node
->is_tail_sentinel()) {
839 printf("ir_call has the wrong number of parameters:\n");
842 if (formal_param_node
->is_tail_sentinel()) {
845 const ir_variable
*formal_param
846 = (const ir_variable
*) formal_param_node
;
847 const ir_rvalue
*actual_param
848 = (const ir_rvalue
*) actual_param_node
;
849 if (formal_param
->type
!= actual_param
->type
) {
850 printf("ir_call parameter type mismatch:\n");
853 if (formal_param
->data
.mode
== ir_var_function_out
854 || formal_param
->data
.mode
== ir_var_function_inout
) {
855 if (!actual_param
->is_lvalue()) {
856 printf("ir_call out/inout parameters must be lvalues:\n");
860 formal_param_node
= formal_param_node
->next
;
861 actual_param_node
= actual_param_node
->next
;
864 return visit_continue
;
875 ir_validate::validate_ir(ir_instruction
*ir
, void *data
)
877 struct set
*ir_set
= (struct set
*) data
;
879 if (_mesa_set_search(ir_set
, ir
)) {
880 printf("Instruction node present twice in ir tree:\n");
885 _mesa_set_add(ir_set
, ir
);
889 check_node_type(ir_instruction
*ir
, void *data
)
893 if (ir
->ir_type
>= ir_type_max
) {
894 printf("Instruction node with unset type\n");
895 ir
->print(); printf("\n");
897 ir_rvalue
*value
= ir
->as_rvalue();
899 assert(value
->type
!= glsl_type::error_type
);
903 validate_ir_tree(exec_list
*instructions
)
905 /* We shouldn't have any reason to validate IR in a release build,
906 * and it's half composed of assert()s anyway which wouldn't do
914 foreach_in_list(ir_instruction
, ir
, instructions
) {
915 visit_tree(ir
, check_node_type
, NULL
);