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