Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / ir_validate.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file ir_validate.cpp
26 *
27 * Attempts to verify that various invariants of the IR tree are true.
28 *
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.
34 */
35
36 #include "ir.h"
37 #include "ir_hierarchical_visitor.h"
38 #include "util/hash_table.h"
39 #include "util/set.h"
40 #include "glsl_types.h"
41
42 namespace {
43
44 class ir_validate : public ir_hierarchical_visitor {
45 public:
46 ir_validate()
47 {
48 this->ir_set = _mesa_set_create(NULL, _mesa_hash_pointer,
49 _mesa_key_pointer_equal);
50
51 this->current_function = NULL;
52
53 this->callback_enter = ir_validate::validate_ir;
54 this->data_enter = ir_set;
55 }
56
57 ~ir_validate()
58 {
59 _mesa_set_destroy(this->ir_set, NULL);
60 }
61
62 virtual ir_visitor_status visit(ir_variable *v);
63 virtual ir_visitor_status visit(ir_dereference_variable *ir);
64
65 virtual ir_visitor_status visit_enter(ir_discard *ir);
66 virtual ir_visitor_status visit_enter(ir_if *ir);
67
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);
71
72 virtual ir_visitor_status visit_leave(ir_expression *ir);
73 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
74
75 virtual ir_visitor_status visit_enter(class ir_dereference_array *);
76
77 virtual ir_visitor_status visit_enter(ir_assignment *ir);
78 virtual ir_visitor_status visit_enter(ir_call *ir);
79
80 static void validate_ir(ir_instruction *ir, void *data);
81
82 ir_function *current_function;
83
84 struct set *ir_set;
85 };
86
87 } /* anonymous namespace */
88
89 ir_visitor_status
90 ir_validate::visit(ir_dereference_variable *ir)
91 {
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);
95 abort();
96 }
97
98 if (_mesa_set_search(ir_set, ir->var) == NULL) {
99 printf("ir_dereference_variable @ %p specifies undeclared variable "
100 "`%s' @ %p\n",
101 (void *) ir, ir->var->name, (void *) ir->var);
102 abort();
103 }
104
105 this->validate_ir(ir, this->data_enter);
106
107 return visit_continue;
108 }
109
110 ir_visitor_status
111 ir_validate::visit_enter(class ir_dereference_array *ir)
112 {
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 "
115 "matrix\n",
116 (void *) ir);
117 ir->print();
118 printf("\n");
119 abort();
120 }
121
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);
125 abort();
126 }
127
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);
131 abort();
132 }
133
134 return visit_continue;
135 }
136
137 ir_visitor_status
138 ir_validate::visit_enter(ir_discard *ir)
139 {
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);
143 ir->print();
144 printf("\n");
145 abort();
146 }
147
148 return visit_continue;
149 }
150
151 ir_visitor_status
152 ir_validate::visit_enter(ir_if *ir)
153 {
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);
157 ir->print();
158 printf("\n");
159 abort();
160 }
161
162 return visit_continue;
163 }
164
165
166 ir_visitor_status
167 ir_validate::visit_enter(ir_function *ir)
168 {
169 /* Function definitions cannot be nested.
170 */
171 if (this->current_function != NULL) {
172 printf("Function definition nested inside another function "
173 "definition:\n");
174 printf("%s %p inside %s %p\n",
175 ir->name, (void *) ir,
176 this->current_function->name, (void *) this->current_function);
177 abort();
178 }
179
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.
183 */
184 this->current_function = ir;
185
186 this->validate_ir(ir, this->data_enter);
187
188 /* Verify that all of the things stored in the list of signatures are,
189 * in fact, function signatures.
190 */
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",
194 ir->name);
195 abort();
196 }
197 }
198
199 return visit_continue;
200 }
201
202 ir_visitor_status
203 ir_validate::visit_leave(ir_function *ir)
204 {
205 assert(ralloc_parent(ir->name) == ir);
206
207 this->current_function = NULL;
208 return visit_continue;
209 }
210
211 ir_visitor_status
212 ir_validate::visit_enter(ir_function_signature *ir)
213 {
214 if (this->current_function != ir->function()) {
215 printf("Function signature nested inside wrong function "
216 "definition:\n");
217 printf("%p inside %s %p instead of %s %p\n",
218 (void *) ir,
219 this->current_function->name, (void *) this->current_function,
220 ir->function_name(), (void *) ir->function());
221 abort();
222 }
223
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());
227 abort();
228 }
229
230 this->validate_ir(ir, this->data_enter);
231
232 return visit_continue;
233 }
234
235 ir_visitor_status
236 ir_validate::visit_leave(ir_expression *ir)
237 {
238 switch (ir->operation) {
239 case ir_unop_bit_not:
240 assert(ir->operands[0]->type == ir->type);
241 break;
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);
245 break;
246
247 case ir_unop_neg:
248 case ir_unop_abs:
249 case ir_unop_sign:
250 case ir_unop_rcp:
251 case ir_unop_rsq:
252 case ir_unop_sqrt:
253 assert(ir->type == ir->operands[0]->type);
254 break;
255
256 case ir_unop_exp:
257 case ir_unop_log:
258 case ir_unop_exp2:
259 case ir_unop_log2:
260 case ir_unop_saturate:
261 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
262 assert(ir->type == ir->operands[0]->type);
263 break;
264
265 case ir_unop_f2i:
266 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
267 assert(ir->type->base_type == GLSL_TYPE_INT);
268 break;
269 case ir_unop_f2u:
270 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
271 assert(ir->type->base_type == GLSL_TYPE_UINT);
272 break;
273 case ir_unop_i2f:
274 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
275 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
276 break;
277 case ir_unop_f2b:
278 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
279 assert(ir->type->base_type == GLSL_TYPE_BOOL);
280 break;
281 case ir_unop_b2f:
282 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
283 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
284 break;
285 case ir_unop_i2b:
286 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
287 assert(ir->type->base_type == GLSL_TYPE_BOOL);
288 break;
289 case ir_unop_b2i:
290 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
291 assert(ir->type->base_type == GLSL_TYPE_INT);
292 break;
293 case ir_unop_u2f:
294 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
295 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
296 break;
297 case ir_unop_i2u:
298 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
299 assert(ir->type->base_type == GLSL_TYPE_UINT);
300 break;
301 case ir_unop_u2i:
302 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
303 assert(ir->type->base_type == GLSL_TYPE_INT);
304 break;
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);
308 break;
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);
312 break;
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);
316 break;
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);
320 break;
321
322 case ir_unop_any:
323 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
324 assert(ir->type == glsl_type::bool_type);
325 break;
326
327 case ir_unop_trunc:
328 case ir_unop_round_even:
329 case ir_unop_ceil:
330 case ir_unop_floor:
331 case ir_unop_fract:
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);
335 break;
336 case ir_unop_sin:
337 case ir_unop_cos:
338 case ir_unop_dFdx:
339 case ir_unop_dFdx_coarse:
340 case ir_unop_dFdx_fine:
341 case ir_unop_dFdy:
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);
346 break;
347
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);
353 break;
354
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);
359 break;
360
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);
364 break;
365
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);
371 break;
372
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);
377 break;
378
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);
383 break;
384
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);
388 break;
389
390 case ir_unop_bitfield_reverse:
391 assert(ir->operands[0]->type == ir->type);
392 assert(ir->type->is_integer());
393 break;
394
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);
401 break;
402
403 case ir_unop_noise:
404 /* XXX what can we assert here? */
405 break;
406
407 case ir_unop_interpolate_at_centroid:
408 assert(ir->operands[0]->type == ir->type);
409 assert(ir->operands[0]->type->is_float());
410 break;
411
412 case ir_unop_d2f:
413 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
414 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
415 break;
416 case ir_unop_f2d:
417 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
418 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
419 break;
420 case ir_unop_d2i:
421 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
422 assert(ir->type->base_type == GLSL_TYPE_INT);
423 break;
424 case ir_unop_i2d:
425 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
426 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
427 break;
428 case ir_unop_d2u:
429 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
430 assert(ir->type->base_type == GLSL_TYPE_UINT);
431 break;
432 case ir_unop_u2d:
433 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
434 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
435 break;
436 case ir_unop_d2b:
437 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
438 assert(ir->type->base_type == GLSL_TYPE_BOOL);
439 break;
440
441 case ir_unop_frexp_sig:
442 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
443 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
444 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
445 break;
446 case ir_unop_frexp_exp:
447 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
448 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
449 assert(ir->type->base_type == GLSL_TYPE_INT);
450 break;
451 case ir_unop_subroutine_to_int:
452 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
453 assert(ir->type->base_type == GLSL_TYPE_INT);
454 break;
455 case ir_binop_add:
456 case ir_binop_sub:
457 case ir_binop_mul:
458 case ir_binop_div:
459 case ir_binop_mod:
460 case ir_binop_min:
461 case ir_binop_max:
462 case ir_binop_pow:
463 assert(ir->operands[0]->type->base_type ==
464 ir->operands[1]->type->base_type);
465
466 if (ir->operands[0]->type->is_scalar())
467 assert(ir->operands[1]->type == ir->type);
468 else if (ir->operands[1]->type->is_scalar())
469 assert(ir->operands[0]->type == ir->type);
470 else if (ir->operands[0]->type->is_vector() &&
471 ir->operands[1]->type->is_vector()) {
472 assert(ir->operands[0]->type == ir->operands[1]->type);
473 assert(ir->operands[0]->type == ir->type);
474 }
475 break;
476
477 case ir_binop_imul_high:
478 assert(ir->type == ir->operands[0]->type);
479 assert(ir->type == ir->operands[1]->type);
480 assert(ir->type->is_integer());
481 break;
482
483 case ir_binop_carry:
484 case ir_binop_borrow:
485 assert(ir->type == ir->operands[0]->type);
486 assert(ir->type == ir->operands[1]->type);
487 assert(ir->type->base_type == GLSL_TYPE_UINT);
488 break;
489
490 case ir_binop_less:
491 case ir_binop_greater:
492 case ir_binop_lequal:
493 case ir_binop_gequal:
494 case ir_binop_equal:
495 case ir_binop_nequal:
496 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
497 * ==, and != operators. The IR operators perform a component-wise
498 * comparison on scalar or vector types and return a boolean scalar or
499 * vector type of the same size.
500 */
501 assert(ir->type->base_type == GLSL_TYPE_BOOL);
502 assert(ir->operands[0]->type == ir->operands[1]->type);
503 assert(ir->operands[0]->type->is_vector()
504 || ir->operands[0]->type->is_scalar());
505 assert(ir->operands[0]->type->vector_elements
506 == ir->type->vector_elements);
507 break;
508
509 case ir_binop_all_equal:
510 case ir_binop_any_nequal:
511 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
512 * return a scalar boolean. The IR matches that.
513 */
514 assert(ir->type == glsl_type::bool_type);
515 assert(ir->operands[0]->type == ir->operands[1]->type);
516 break;
517
518 case ir_binop_lshift:
519 case ir_binop_rshift:
520 assert(ir->operands[0]->type->is_integer() &&
521 ir->operands[1]->type->is_integer());
522 if (ir->operands[0]->type->is_scalar()) {
523 assert(ir->operands[1]->type->is_scalar());
524 }
525 if (ir->operands[0]->type->is_vector() &&
526 ir->operands[1]->type->is_vector()) {
527 assert(ir->operands[0]->type->components() ==
528 ir->operands[1]->type->components());
529 }
530 assert(ir->type == ir->operands[0]->type);
531 break;
532
533 case ir_binop_bit_and:
534 case ir_binop_bit_xor:
535 case ir_binop_bit_or:
536 assert(ir->operands[0]->type->base_type ==
537 ir->operands[1]->type->base_type);
538 assert(ir->type->is_integer());
539 if (ir->operands[0]->type->is_vector() &&
540 ir->operands[1]->type->is_vector()) {
541 assert(ir->operands[0]->type->vector_elements ==
542 ir->operands[1]->type->vector_elements);
543 }
544 break;
545
546 case ir_binop_logic_and:
547 case ir_binop_logic_xor:
548 case ir_binop_logic_or:
549 assert(ir->type->base_type == GLSL_TYPE_BOOL);
550 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
551 assert(ir->operands[1]->type->base_type == GLSL_TYPE_BOOL);
552 break;
553
554 case ir_binop_dot:
555 assert(ir->type == glsl_type::float_type ||
556 ir->type == glsl_type::double_type);
557 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
558 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
559 assert(ir->operands[0]->type->is_vector());
560 assert(ir->operands[0]->type == ir->operands[1]->type);
561 break;
562
563 case ir_binop_pack_half_2x16_split:
564 assert(ir->type == glsl_type::uint_type);
565 assert(ir->operands[0]->type == glsl_type::float_type);
566 assert(ir->operands[1]->type == glsl_type::float_type);
567 break;
568
569 case ir_binop_bfm:
570 assert(ir->type->is_integer());
571 assert(ir->operands[0]->type->is_integer());
572 assert(ir->operands[1]->type->is_integer());
573 break;
574
575 case ir_binop_ubo_load:
576 assert(ir->operands[0]->type == glsl_type::uint_type);
577
578 assert(ir->operands[1]->type == glsl_type::uint_type);
579 break;
580
581 case ir_binop_ldexp:
582 assert(ir->operands[0]->type == ir->type);
583 assert(ir->operands[0]->type->is_float() ||
584 ir->operands[0]->type->is_double());
585 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
586 assert(ir->operands[0]->type->components() ==
587 ir->operands[1]->type->components());
588 break;
589
590 case ir_binop_vector_extract:
591 assert(ir->operands[0]->type->is_vector());
592 assert(ir->operands[1]->type->is_scalar()
593 && ir->operands[1]->type->is_integer());
594 break;
595
596 case ir_binop_interpolate_at_offset:
597 assert(ir->operands[0]->type == ir->type);
598 assert(ir->operands[0]->type->is_float());
599 assert(ir->operands[1]->type->components() == 2);
600 assert(ir->operands[1]->type->is_float());
601 break;
602
603 case ir_binop_interpolate_at_sample:
604 assert(ir->operands[0]->type == ir->type);
605 assert(ir->operands[0]->type->is_float());
606 assert(ir->operands[1]->type == glsl_type::int_type);
607 break;
608
609 case ir_triop_fma:
610 assert(ir->type->base_type == GLSL_TYPE_FLOAT ||
611 ir->type->base_type == GLSL_TYPE_DOUBLE);
612 assert(ir->type == ir->operands[0]->type);
613 assert(ir->type == ir->operands[1]->type);
614 assert(ir->type == ir->operands[2]->type);
615 break;
616
617 case ir_triop_lrp:
618 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
619 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
620 assert(ir->operands[0]->type == ir->operands[1]->type);
621 assert(ir->operands[2]->type == ir->operands[0]->type ||
622 ir->operands[2]->type == glsl_type::float_type ||
623 ir->operands[2]->type == glsl_type::double_type);
624 break;
625
626 case ir_triop_csel:
627 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
628 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
629 assert(ir->type == ir->operands[1]->type);
630 assert(ir->type == ir->operands[2]->type);
631 break;
632
633 case ir_triop_bfi:
634 assert(ir->operands[0]->type->is_integer());
635 assert(ir->operands[1]->type == ir->operands[2]->type);
636 assert(ir->operands[1]->type == ir->type);
637 break;
638
639 case ir_triop_bitfield_extract:
640 assert(ir->operands[0]->type == ir->type);
641 assert(ir->operands[1]->type == glsl_type::int_type);
642 assert(ir->operands[2]->type == glsl_type::int_type);
643 break;
644
645 case ir_triop_vector_insert:
646 assert(ir->operands[0]->type->is_vector());
647 assert(ir->operands[1]->type->is_scalar());
648 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
649 assert(ir->operands[2]->type->is_scalar()
650 && ir->operands[2]->type->is_integer());
651 assert(ir->type == ir->operands[0]->type);
652 break;
653
654 case ir_quadop_bitfield_insert:
655 assert(ir->operands[0]->type == ir->type);
656 assert(ir->operands[1]->type == ir->type);
657 assert(ir->operands[2]->type == glsl_type::int_type);
658 assert(ir->operands[3]->type == glsl_type::int_type);
659 break;
660
661 case ir_quadop_vector:
662 /* The vector operator collects some number of scalars and generates a
663 * vector from them.
664 *
665 * - All of the operands must be scalar.
666 * - Number of operands must matche the size of the resulting vector.
667 * - Base type of the operands must match the base type of the result.
668 */
669 assert(ir->type->is_vector());
670 switch (ir->type->vector_elements) {
671 case 2:
672 assert(ir->operands[0]->type->is_scalar());
673 assert(ir->operands[0]->type->base_type == ir->type->base_type);
674 assert(ir->operands[1]->type->is_scalar());
675 assert(ir->operands[1]->type->base_type == ir->type->base_type);
676 assert(ir->operands[2] == NULL);
677 assert(ir->operands[3] == NULL);
678 break;
679 case 3:
680 assert(ir->operands[0]->type->is_scalar());
681 assert(ir->operands[0]->type->base_type == ir->type->base_type);
682 assert(ir->operands[1]->type->is_scalar());
683 assert(ir->operands[1]->type->base_type == ir->type->base_type);
684 assert(ir->operands[2]->type->is_scalar());
685 assert(ir->operands[2]->type->base_type == ir->type->base_type);
686 assert(ir->operands[3] == NULL);
687 break;
688 case 4:
689 assert(ir->operands[0]->type->is_scalar());
690 assert(ir->operands[0]->type->base_type == ir->type->base_type);
691 assert(ir->operands[1]->type->is_scalar());
692 assert(ir->operands[1]->type->base_type == ir->type->base_type);
693 assert(ir->operands[2]->type->is_scalar());
694 assert(ir->operands[2]->type->base_type == ir->type->base_type);
695 assert(ir->operands[3]->type->is_scalar());
696 assert(ir->operands[3]->type->base_type == ir->type->base_type);
697 break;
698 default:
699 /* The is_vector assertion above should prevent execution from ever
700 * getting here.
701 */
702 assert(!"Should not get here.");
703 break;
704 }
705 }
706
707 return visit_continue;
708 }
709
710 ir_visitor_status
711 ir_validate::visit_leave(ir_swizzle *ir)
712 {
713 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
714
715 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
716 if (chans[i] >= ir->val->type->vector_elements) {
717 printf("ir_swizzle @ %p specifies a channel not present "
718 "in the value.\n", (void *) ir);
719 ir->print();
720 abort();
721 }
722 }
723
724 return visit_continue;
725 }
726
727 ir_visitor_status
728 ir_validate::visit(ir_variable *ir)
729 {
730 /* An ir_variable is the one thing that can (and will) appear multiple times
731 * in an IR tree. It is added to the hashtable so that it can be used
732 * in the ir_dereference_variable handler to ensure that a variable is
733 * declared before it is dereferenced.
734 */
735 if (ir->name && ir->is_name_ralloced())
736 assert(ralloc_parent(ir->name) == ir);
737
738 _mesa_set_add(ir_set, ir);
739
740 /* If a variable is an array, verify that the maximum array index is in
741 * bounds. There was once an error in AST-to-HIR conversion that set this
742 * to be out of bounds.
743 */
744 if (ir->type->array_size() > 0) {
745 if (ir->data.max_array_access >= ir->type->length) {
746 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
747 ir->data.max_array_access, ir->type->length - 1);
748 ir->print();
749 abort();
750 }
751 }
752
753 /* If a variable is an interface block (or an array of interface blocks),
754 * verify that the maximum array index for each interface member is in
755 * bounds.
756 */
757 if (ir->is_interface_instance()) {
758 const glsl_struct_field *fields =
759 ir->get_interface_type()->fields.structure;
760 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
761 if (fields[i].type->array_size() > 0) {
762 const unsigned *const max_ifc_array_access =
763 ir->get_max_ifc_array_access();
764
765 assert(max_ifc_array_access != NULL);
766
767 if (max_ifc_array_access[i] >= fields[i].type->length) {
768 printf("ir_variable has maximum access out of bounds for "
769 "field %s (%d vs %d)\n", fields[i].name,
770 max_ifc_array_access[i], fields[i].type->length);
771 ir->print();
772 abort();
773 }
774 }
775 }
776 }
777
778 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
779 printf("ir_variable didn't have an initializer, but has a constant "
780 "initializer value.\n");
781 ir->print();
782 abort();
783 }
784
785 if (ir->data.mode == ir_var_uniform
786 && is_gl_identifier(ir->name)
787 && ir->get_state_slots() == NULL) {
788 printf("built-in uniform has no state\n");
789 ir->print();
790 abort();
791 }
792
793 return visit_continue;
794 }
795
796 ir_visitor_status
797 ir_validate::visit_enter(ir_assignment *ir)
798 {
799 const ir_dereference *const lhs = ir->lhs;
800 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
801 if (ir->write_mask == 0) {
802 printf("Assignment LHS is %s, but write mask is 0:\n",
803 lhs->type->is_scalar() ? "scalar" : "vector");
804 ir->print();
805 abort();
806 }
807
808 int lhs_components = 0;
809 for (int i = 0; i < 4; i++) {
810 if (ir->write_mask & (1 << i))
811 lhs_components++;
812 }
813
814 if (lhs_components != ir->rhs->type->vector_elements) {
815 printf("Assignment count of LHS write mask channels enabled not\n"
816 "matching RHS vector size (%d LHS, %d RHS).\n",
817 lhs_components, ir->rhs->type->vector_elements);
818 ir->print();
819 abort();
820 }
821 }
822
823 this->validate_ir(ir, this->data_enter);
824
825 return visit_continue;
826 }
827
828 ir_visitor_status
829 ir_validate::visit_enter(ir_call *ir)
830 {
831 ir_function_signature *const callee = ir->callee;
832
833 if (callee->ir_type != ir_type_function_signature) {
834 printf("IR called by ir_call is not ir_function_signature!\n");
835 abort();
836 }
837
838 if (ir->return_deref) {
839 if (ir->return_deref->type != callee->return_type) {
840 printf("callee type %s does not match return storage type %s\n",
841 callee->return_type->name, ir->return_deref->type->name);
842 abort();
843 }
844 } else if (callee->return_type != glsl_type::void_type) {
845 printf("ir_call has non-void callee but no return storage\n");
846 abort();
847 }
848
849 const exec_node *formal_param_node = callee->parameters.head;
850 const exec_node *actual_param_node = ir->actual_parameters.head;
851 while (true) {
852 if (formal_param_node->is_tail_sentinel()
853 != actual_param_node->is_tail_sentinel()) {
854 printf("ir_call has the wrong number of parameters:\n");
855 goto dump_ir;
856 }
857 if (formal_param_node->is_tail_sentinel()) {
858 break;
859 }
860 const ir_variable *formal_param
861 = (const ir_variable *) formal_param_node;
862 const ir_rvalue *actual_param
863 = (const ir_rvalue *) actual_param_node;
864 if (formal_param->type != actual_param->type) {
865 printf("ir_call parameter type mismatch:\n");
866 goto dump_ir;
867 }
868 if (formal_param->data.mode == ir_var_function_out
869 || formal_param->data.mode == ir_var_function_inout) {
870 if (!actual_param->is_lvalue()) {
871 printf("ir_call out/inout parameters must be lvalues:\n");
872 goto dump_ir;
873 }
874 }
875 formal_param_node = formal_param_node->next;
876 actual_param_node = actual_param_node->next;
877 }
878
879 return visit_continue;
880
881 dump_ir:
882 ir->print();
883 printf("callee:\n");
884 callee->print();
885 abort();
886 return visit_stop;
887 }
888
889 void
890 ir_validate::validate_ir(ir_instruction *ir, void *data)
891 {
892 struct set *ir_set = (struct set *) data;
893
894 if (_mesa_set_search(ir_set, ir)) {
895 printf("Instruction node present twice in ir tree:\n");
896 ir->print();
897 printf("\n");
898 abort();
899 }
900 _mesa_set_add(ir_set, ir);
901 }
902
903 void
904 check_node_type(ir_instruction *ir, void *data)
905 {
906 (void) data;
907
908 if (ir->ir_type >= ir_type_max) {
909 printf("Instruction node with unset type\n");
910 ir->print(); printf("\n");
911 }
912 ir_rvalue *value = ir->as_rvalue();
913 if (value != NULL)
914 assert(value->type != glsl_type::error_type);
915 }
916
917 void
918 validate_ir_tree(exec_list *instructions)
919 {
920 /* We shouldn't have any reason to validate IR in a release build,
921 * and it's half composed of assert()s anyway which wouldn't do
922 * anything.
923 */
924 #ifdef DEBUG
925 ir_validate v;
926
927 v.run(instructions);
928
929 foreach_in_list(ir_instruction, ir, instructions) {
930 visit_tree(ir, check_node_type, NULL);
931 }
932 #endif
933 }