nir/spirv: Move CF emit code into vtn_cfg.c
[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_trunc:
324 case ir_unop_round_even:
325 case ir_unop_ceil:
326 case ir_unop_floor:
327 case ir_unop_fract:
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);
331 break;
332 case ir_unop_sin:
333 case ir_unop_cos:
334 case ir_unop_dFdx:
335 case ir_unop_dFdx_coarse:
336 case ir_unop_dFdx_fine:
337 case ir_unop_dFdy:
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);
342 break;
343
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);
349 break;
350
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);
355 break;
356
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);
360 break;
361
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);
367 break;
368
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);
373 break;
374
375 case ir_unop_unpack_half_2x16_split_x:
376 case ir_unop_unpack_half_2x16_split_y:
377 assert(ir->type == glsl_type::float_type);
378 assert(ir->operands[0]->type == glsl_type::uint_type);
379 break;
380
381 case ir_unop_unpack_double_2x32:
382 assert(ir->type == glsl_type::uvec2_type);
383 assert(ir->operands[0]->type == glsl_type::double_type);
384 break;
385
386 case ir_unop_bitfield_reverse:
387 assert(ir->operands[0]->type == ir->type);
388 assert(ir->type->is_integer());
389 break;
390
391 case ir_unop_bit_count:
392 case ir_unop_find_msb:
393 case ir_unop_find_lsb:
394 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
395 assert(ir->operands[0]->type->is_integer());
396 assert(ir->type->base_type == GLSL_TYPE_INT);
397 break;
398
399 case ir_unop_noise:
400 /* XXX what can we assert here? */
401 break;
402
403 case ir_unop_interpolate_at_centroid:
404 assert(ir->operands[0]->type == ir->type);
405 assert(ir->operands[0]->type->is_float());
406 break;
407
408 case ir_unop_get_buffer_size:
409 assert(ir->type == glsl_type::int_type);
410 assert(ir->operands[0]->type == glsl_type::uint_type);
411 break;
412
413 case ir_unop_ssbo_unsized_array_length:
414 assert(ir->type == glsl_type::int_type);
415 assert(ir->operands[0]->type->is_array());
416 assert(ir->operands[0]->type->is_unsized_array());
417 break;
418
419 case ir_unop_d2f:
420 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
421 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
422 break;
423 case ir_unop_f2d:
424 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
425 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
426 break;
427 case ir_unop_d2i:
428 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
429 assert(ir->type->base_type == GLSL_TYPE_INT);
430 break;
431 case ir_unop_i2d:
432 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
433 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
434 break;
435 case ir_unop_d2u:
436 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
437 assert(ir->type->base_type == GLSL_TYPE_UINT);
438 break;
439 case ir_unop_u2d:
440 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
441 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
442 break;
443 case ir_unop_d2b:
444 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
445 assert(ir->type->base_type == GLSL_TYPE_BOOL);
446 break;
447
448 case ir_unop_frexp_sig:
449 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
450 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
451 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
452 break;
453 case ir_unop_frexp_exp:
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_INT);
457 break;
458 case ir_unop_subroutine_to_int:
459 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
460 assert(ir->type->base_type == GLSL_TYPE_INT);
461 break;
462 case ir_binop_add:
463 case ir_binop_sub:
464 case ir_binop_mul:
465 case ir_binop_div:
466 case ir_binop_mod:
467 case ir_binop_min:
468 case ir_binop_max:
469 case ir_binop_pow:
470 assert(ir->operands[0]->type->base_type ==
471 ir->operands[1]->type->base_type);
472
473 if (ir->operands[0]->type->is_scalar())
474 assert(ir->operands[1]->type == ir->type);
475 else if (ir->operands[1]->type->is_scalar())
476 assert(ir->operands[0]->type == ir->type);
477 else if (ir->operands[0]->type->is_vector() &&
478 ir->operands[1]->type->is_vector()) {
479 assert(ir->operands[0]->type == ir->operands[1]->type);
480 assert(ir->operands[0]->type == ir->type);
481 }
482 break;
483
484 case ir_binop_imul_high:
485 assert(ir->type == ir->operands[0]->type);
486 assert(ir->type == ir->operands[1]->type);
487 assert(ir->type->is_integer());
488 break;
489
490 case ir_binop_carry:
491 case ir_binop_borrow:
492 assert(ir->type == ir->operands[0]->type);
493 assert(ir->type == ir->operands[1]->type);
494 assert(ir->type->base_type == GLSL_TYPE_UINT);
495 break;
496
497 case ir_binop_less:
498 case ir_binop_greater:
499 case ir_binop_lequal:
500 case ir_binop_gequal:
501 case ir_binop_equal:
502 case ir_binop_nequal:
503 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
504 * ==, and != operators. The IR operators perform a component-wise
505 * comparison on scalar or vector types and return a boolean scalar or
506 * vector type of the same size.
507 */
508 assert(ir->type->base_type == GLSL_TYPE_BOOL);
509 assert(ir->operands[0]->type == ir->operands[1]->type);
510 assert(ir->operands[0]->type->is_vector()
511 || ir->operands[0]->type->is_scalar());
512 assert(ir->operands[0]->type->vector_elements
513 == ir->type->vector_elements);
514 break;
515
516 case ir_binop_all_equal:
517 case ir_binop_any_nequal:
518 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
519 * return a scalar boolean. The IR matches that.
520 */
521 assert(ir->type == glsl_type::bool_type);
522 assert(ir->operands[0]->type == ir->operands[1]->type);
523 break;
524
525 case ir_binop_lshift:
526 case ir_binop_rshift:
527 assert(ir->operands[0]->type->is_integer() &&
528 ir->operands[1]->type->is_integer());
529 if (ir->operands[0]->type->is_scalar()) {
530 assert(ir->operands[1]->type->is_scalar());
531 }
532 if (ir->operands[0]->type->is_vector() &&
533 ir->operands[1]->type->is_vector()) {
534 assert(ir->operands[0]->type->components() ==
535 ir->operands[1]->type->components());
536 }
537 assert(ir->type == ir->operands[0]->type);
538 break;
539
540 case ir_binop_bit_and:
541 case ir_binop_bit_xor:
542 case ir_binop_bit_or:
543 assert(ir->operands[0]->type->base_type ==
544 ir->operands[1]->type->base_type);
545 assert(ir->type->is_integer());
546 if (ir->operands[0]->type->is_vector() &&
547 ir->operands[1]->type->is_vector()) {
548 assert(ir->operands[0]->type->vector_elements ==
549 ir->operands[1]->type->vector_elements);
550 }
551 break;
552
553 case ir_binop_logic_and:
554 case ir_binop_logic_xor:
555 case ir_binop_logic_or:
556 assert(ir->type->base_type == GLSL_TYPE_BOOL);
557 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
558 assert(ir->operands[1]->type->base_type == GLSL_TYPE_BOOL);
559 break;
560
561 case ir_binop_dot:
562 assert(ir->type == glsl_type::float_type ||
563 ir->type == glsl_type::double_type);
564 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
565 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
566 assert(ir->operands[0]->type->is_vector());
567 assert(ir->operands[0]->type == ir->operands[1]->type);
568 break;
569
570 case ir_binop_pack_half_2x16_split:
571 assert(ir->type == glsl_type::uint_type);
572 assert(ir->operands[0]->type == glsl_type::float_type);
573 assert(ir->operands[1]->type == glsl_type::float_type);
574 break;
575
576 case ir_binop_bfm:
577 assert(ir->type->is_integer());
578 assert(ir->operands[0]->type->is_integer());
579 assert(ir->operands[1]->type->is_integer());
580 break;
581
582 case ir_binop_ubo_load:
583 assert(ir->operands[0]->type == glsl_type::uint_type);
584
585 assert(ir->operands[1]->type == glsl_type::uint_type);
586 break;
587
588 case ir_binop_ldexp:
589 assert(ir->operands[0]->type == ir->type);
590 assert(ir->operands[0]->type->is_float() ||
591 ir->operands[0]->type->is_double());
592 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
593 assert(ir->operands[0]->type->components() ==
594 ir->operands[1]->type->components());
595 break;
596
597 case ir_binop_vector_extract:
598 assert(ir->operands[0]->type->is_vector());
599 assert(ir->operands[1]->type->is_scalar()
600 && ir->operands[1]->type->is_integer());
601 break;
602
603 case ir_binop_interpolate_at_offset:
604 assert(ir->operands[0]->type == ir->type);
605 assert(ir->operands[0]->type->is_float());
606 assert(ir->operands[1]->type->components() == 2);
607 assert(ir->operands[1]->type->is_float());
608 break;
609
610 case ir_binop_interpolate_at_sample:
611 assert(ir->operands[0]->type == ir->type);
612 assert(ir->operands[0]->type->is_float());
613 assert(ir->operands[1]->type == glsl_type::int_type);
614 break;
615
616 case ir_triop_fma:
617 assert(ir->type->base_type == GLSL_TYPE_FLOAT ||
618 ir->type->base_type == GLSL_TYPE_DOUBLE);
619 assert(ir->type == ir->operands[0]->type);
620 assert(ir->type == ir->operands[1]->type);
621 assert(ir->type == ir->operands[2]->type);
622 break;
623
624 case ir_triop_lrp:
625 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
626 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
627 assert(ir->operands[0]->type == ir->operands[1]->type);
628 assert(ir->operands[2]->type == ir->operands[0]->type ||
629 ir->operands[2]->type == glsl_type::float_type ||
630 ir->operands[2]->type == glsl_type::double_type);
631 break;
632
633 case ir_triop_csel:
634 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
635 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
636 assert(ir->type == ir->operands[1]->type);
637 assert(ir->type == ir->operands[2]->type);
638 break;
639
640 case ir_triop_bfi:
641 assert(ir->operands[0]->type->is_integer());
642 assert(ir->operands[1]->type == ir->operands[2]->type);
643 assert(ir->operands[1]->type == ir->type);
644 break;
645
646 case ir_triop_bitfield_extract:
647 assert(ir->operands[0]->type == ir->type);
648 assert(ir->operands[1]->type == glsl_type::int_type);
649 assert(ir->operands[2]->type == glsl_type::int_type);
650 break;
651
652 case ir_triop_vector_insert:
653 assert(ir->operands[0]->type->is_vector());
654 assert(ir->operands[1]->type->is_scalar());
655 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
656 assert(ir->operands[2]->type->is_scalar()
657 && ir->operands[2]->type->is_integer());
658 assert(ir->type == ir->operands[0]->type);
659 break;
660
661 case ir_quadop_bitfield_insert:
662 assert(ir->operands[0]->type == ir->type);
663 assert(ir->operands[1]->type == ir->type);
664 assert(ir->operands[2]->type == glsl_type::int_type);
665 assert(ir->operands[3]->type == glsl_type::int_type);
666 break;
667
668 case ir_quadop_vector:
669 /* The vector operator collects some number of scalars and generates a
670 * vector from them.
671 *
672 * - All of the operands must be scalar.
673 * - Number of operands must matche the size of the resulting vector.
674 * - Base type of the operands must match the base type of the result.
675 */
676 assert(ir->type->is_vector());
677 switch (ir->type->vector_elements) {
678 case 2:
679 assert(ir->operands[0]->type->is_scalar());
680 assert(ir->operands[0]->type->base_type == ir->type->base_type);
681 assert(ir->operands[1]->type->is_scalar());
682 assert(ir->operands[1]->type->base_type == ir->type->base_type);
683 assert(ir->operands[2] == NULL);
684 assert(ir->operands[3] == NULL);
685 break;
686 case 3:
687 assert(ir->operands[0]->type->is_scalar());
688 assert(ir->operands[0]->type->base_type == ir->type->base_type);
689 assert(ir->operands[1]->type->is_scalar());
690 assert(ir->operands[1]->type->base_type == ir->type->base_type);
691 assert(ir->operands[2]->type->is_scalar());
692 assert(ir->operands[2]->type->base_type == ir->type->base_type);
693 assert(ir->operands[3] == NULL);
694 break;
695 case 4:
696 assert(ir->operands[0]->type->is_scalar());
697 assert(ir->operands[0]->type->base_type == ir->type->base_type);
698 assert(ir->operands[1]->type->is_scalar());
699 assert(ir->operands[1]->type->base_type == ir->type->base_type);
700 assert(ir->operands[2]->type->is_scalar());
701 assert(ir->operands[2]->type->base_type == ir->type->base_type);
702 assert(ir->operands[3]->type->is_scalar());
703 assert(ir->operands[3]->type->base_type == ir->type->base_type);
704 break;
705 default:
706 /* The is_vector assertion above should prevent execution from ever
707 * getting here.
708 */
709 assert(!"Should not get here.");
710 break;
711 }
712 }
713
714 return visit_continue;
715 }
716
717 ir_visitor_status
718 ir_validate::visit_leave(ir_swizzle *ir)
719 {
720 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
721
722 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
723 if (chans[i] >= ir->val->type->vector_elements) {
724 printf("ir_swizzle @ %p specifies a channel not present "
725 "in the value.\n", (void *) ir);
726 ir->print();
727 abort();
728 }
729 }
730
731 return visit_continue;
732 }
733
734 ir_visitor_status
735 ir_validate::visit(ir_variable *ir)
736 {
737 /* An ir_variable is the one thing that can (and will) appear multiple times
738 * in an IR tree. It is added to the hashtable so that it can be used
739 * in the ir_dereference_variable handler to ensure that a variable is
740 * declared before it is dereferenced.
741 */
742 if (ir->name && ir->is_name_ralloced())
743 assert(ralloc_parent(ir->name) == ir);
744
745 _mesa_set_add(ir_set, ir);
746
747 /* If a variable is an array, verify that the maximum array index is in
748 * bounds. There was once an error in AST-to-HIR conversion that set this
749 * to be out of bounds.
750 */
751 if (ir->type->array_size() > 0) {
752 if (ir->data.max_array_access >= ir->type->length) {
753 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
754 ir->data.max_array_access, ir->type->length - 1);
755 ir->print();
756 abort();
757 }
758 }
759
760 /* If a variable is an interface block (or an array of interface blocks),
761 * verify that the maximum array index for each interface member is in
762 * bounds.
763 */
764 if (ir->is_interface_instance()) {
765 const glsl_struct_field *fields =
766 ir->get_interface_type()->fields.structure;
767 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
768 if (fields[i].type->array_size() > 0) {
769 const unsigned *const max_ifc_array_access =
770 ir->get_max_ifc_array_access();
771
772 assert(max_ifc_array_access != NULL);
773
774 if (max_ifc_array_access[i] >= fields[i].type->length) {
775 printf("ir_variable has maximum access out of bounds for "
776 "field %s (%d vs %d)\n", fields[i].name,
777 max_ifc_array_access[i], fields[i].type->length);
778 ir->print();
779 abort();
780 }
781 }
782 }
783 }
784
785 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
786 printf("ir_variable didn't have an initializer, but has a constant "
787 "initializer value.\n");
788 ir->print();
789 abort();
790 }
791
792 if (ir->data.mode == ir_var_uniform
793 && is_gl_identifier(ir->name)
794 && ir->get_state_slots() == NULL) {
795 printf("built-in uniform has no state\n");
796 ir->print();
797 abort();
798 }
799
800 return visit_continue;
801 }
802
803 ir_visitor_status
804 ir_validate::visit_enter(ir_assignment *ir)
805 {
806 const ir_dereference *const lhs = ir->lhs;
807 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
808 if (ir->write_mask == 0) {
809 printf("Assignment LHS is %s, but write mask is 0:\n",
810 lhs->type->is_scalar() ? "scalar" : "vector");
811 ir->print();
812 abort();
813 }
814
815 int lhs_components = 0;
816 for (int i = 0; i < 4; i++) {
817 if (ir->write_mask & (1 << i))
818 lhs_components++;
819 }
820
821 if (lhs_components != ir->rhs->type->vector_elements) {
822 printf("Assignment count of LHS write mask channels enabled not\n"
823 "matching RHS vector size (%d LHS, %d RHS).\n",
824 lhs_components, ir->rhs->type->vector_elements);
825 ir->print();
826 abort();
827 }
828 }
829
830 this->validate_ir(ir, this->data_enter);
831
832 return visit_continue;
833 }
834
835 ir_visitor_status
836 ir_validate::visit_enter(ir_call *ir)
837 {
838 ir_function_signature *const callee = ir->callee;
839
840 if (callee->ir_type != ir_type_function_signature) {
841 printf("IR called by ir_call is not ir_function_signature!\n");
842 abort();
843 }
844
845 if (ir->return_deref) {
846 if (ir->return_deref->type != callee->return_type) {
847 printf("callee type %s does not match return storage type %s\n",
848 callee->return_type->name, ir->return_deref->type->name);
849 abort();
850 }
851 } else if (callee->return_type != glsl_type::void_type) {
852 printf("ir_call has non-void callee but no return storage\n");
853 abort();
854 }
855
856 const exec_node *formal_param_node = callee->parameters.head;
857 const exec_node *actual_param_node = ir->actual_parameters.head;
858 while (true) {
859 if (formal_param_node->is_tail_sentinel()
860 != actual_param_node->is_tail_sentinel()) {
861 printf("ir_call has the wrong number of parameters:\n");
862 goto dump_ir;
863 }
864 if (formal_param_node->is_tail_sentinel()) {
865 break;
866 }
867 const ir_variable *formal_param
868 = (const ir_variable *) formal_param_node;
869 const ir_rvalue *actual_param
870 = (const ir_rvalue *) actual_param_node;
871 if (formal_param->type != actual_param->type) {
872 printf("ir_call parameter type mismatch:\n");
873 goto dump_ir;
874 }
875 if (formal_param->data.mode == ir_var_function_out
876 || formal_param->data.mode == ir_var_function_inout) {
877 if (!actual_param->is_lvalue()) {
878 printf("ir_call out/inout parameters must be lvalues:\n");
879 goto dump_ir;
880 }
881 }
882 formal_param_node = formal_param_node->next;
883 actual_param_node = actual_param_node->next;
884 }
885
886 return visit_continue;
887
888 dump_ir:
889 ir->print();
890 printf("callee:\n");
891 callee->print();
892 abort();
893 return visit_stop;
894 }
895
896 void
897 ir_validate::validate_ir(ir_instruction *ir, void *data)
898 {
899 struct set *ir_set = (struct set *) data;
900
901 if (_mesa_set_search(ir_set, ir)) {
902 printf("Instruction node present twice in ir tree:\n");
903 ir->print();
904 printf("\n");
905 abort();
906 }
907 _mesa_set_add(ir_set, ir);
908 }
909
910 void
911 check_node_type(ir_instruction *ir, void *data)
912 {
913 (void) data;
914
915 if (ir->ir_type >= ir_type_max) {
916 printf("Instruction node with unset type\n");
917 ir->print(); printf("\n");
918 }
919 ir_rvalue *value = ir->as_rvalue();
920 if (value != NULL)
921 assert(value->type != glsl_type::error_type);
922 }
923
924 void
925 validate_ir_tree(exec_list *instructions)
926 {
927 /* We shouldn't have any reason to validate IR in a release build,
928 * and it's half composed of assert()s anyway which wouldn't do
929 * anything.
930 */
931 #ifdef DEBUG
932 ir_validate v;
933
934 v.run(instructions);
935
936 foreach_in_list(ir_instruction, ir, instructions) {
937 visit_tree(ir, check_node_type, NULL);
938 }
939 #endif
940 }