glsl: Allow GLSL_TYPE_INT64 for ir_unop_abs and ir_unop_sign
[mesa.git] / src / compiler / 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 "compiler/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 assert(ir->type == ir->operands[0]->type);
250 break;
251
252 case ir_unop_abs:
253 case ir_unop_sign:
254 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT ||
255 ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
256 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE ||
257 ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
258 assert(ir->type == ir->operands[0]->type);
259 break;
260
261 case ir_unop_rcp:
262 case ir_unop_rsq:
263 case ir_unop_sqrt:
264 assert(ir->type->base_type == GLSL_TYPE_FLOAT ||
265 ir->type->base_type == GLSL_TYPE_DOUBLE);
266 assert(ir->type == ir->operands[0]->type);
267 break;
268
269 case ir_unop_exp:
270 case ir_unop_log:
271 case ir_unop_exp2:
272 case ir_unop_log2:
273 case ir_unop_saturate:
274 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
275 assert(ir->type == ir->operands[0]->type);
276 break;
277
278 case ir_unop_f2i:
279 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
280 assert(ir->type->base_type == GLSL_TYPE_INT);
281 break;
282 case ir_unop_f2u:
283 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
284 assert(ir->type->base_type == GLSL_TYPE_UINT);
285 break;
286 case ir_unop_i2f:
287 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
288 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
289 break;
290 case ir_unop_f2b:
291 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
292 assert(ir->type->base_type == GLSL_TYPE_BOOL);
293 break;
294 case ir_unop_b2f:
295 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
296 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
297 break;
298 case ir_unop_i2b:
299 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
300 assert(ir->type->base_type == GLSL_TYPE_BOOL);
301 break;
302 case ir_unop_b2i:
303 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
304 assert(ir->type->base_type == GLSL_TYPE_INT);
305 break;
306 case ir_unop_u2f:
307 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
308 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
309 break;
310 case ir_unop_i2u:
311 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
312 assert(ir->type->base_type == GLSL_TYPE_UINT);
313 break;
314 case ir_unop_u2i:
315 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
316 assert(ir->type->base_type == GLSL_TYPE_INT);
317 break;
318 case ir_unop_bitcast_i2f:
319 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
320 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
321 break;
322 case ir_unop_bitcast_f2i:
323 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
324 assert(ir->type->base_type == GLSL_TYPE_INT);
325 break;
326 case ir_unop_bitcast_u2f:
327 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
328 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
329 break;
330 case ir_unop_bitcast_f2u:
331 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
332 assert(ir->type->base_type == GLSL_TYPE_UINT);
333 break;
334
335 case ir_unop_bitcast_u642d:
336 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
337 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
338 break;
339 case ir_unop_bitcast_i642d:
340 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
341 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
342 break;
343 case ir_unop_bitcast_d2u64:
344 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
345 assert(ir->type->base_type == GLSL_TYPE_UINT64);
346 break;
347 case ir_unop_bitcast_d2i64:
348 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
349 assert(ir->type->base_type == GLSL_TYPE_INT64);
350 break;
351 case ir_unop_i642i:
352 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
353 assert(ir->type->base_type == GLSL_TYPE_INT);
354 break;
355 case ir_unop_u642i:
356 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
357 assert(ir->type->base_type == GLSL_TYPE_INT);
358 break;
359 case ir_unop_i642u:
360 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
361 assert(ir->type->base_type == GLSL_TYPE_UINT);
362 break;
363 case ir_unop_u642u:
364 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
365 assert(ir->type->base_type == GLSL_TYPE_UINT);
366 break;
367 case ir_unop_i642b:
368 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
369 assert(ir->type->base_type == GLSL_TYPE_BOOL);
370 break;
371 case ir_unop_i642f:
372 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
373 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
374 break;
375 case ir_unop_u642f:
376 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
377 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
378 break;
379 case ir_unop_i642d:
380 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
381 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
382 break;
383 case ir_unop_u642d:
384 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
385 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
386 break;
387 case ir_unop_i2i64:
388 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
389 assert(ir->type->base_type == GLSL_TYPE_INT64);
390 break;
391 case ir_unop_u2i64:
392 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
393 assert(ir->type->base_type == GLSL_TYPE_INT64);
394 break;
395 case ir_unop_b2i64:
396 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
397 assert(ir->type->base_type == GLSL_TYPE_INT64);
398 break;
399 case ir_unop_f2i64:
400 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
401 assert(ir->type->base_type == GLSL_TYPE_INT64);
402 break;
403 case ir_unop_d2i64:
404 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
405 assert(ir->type->base_type == GLSL_TYPE_INT64);
406 break;
407 case ir_unop_i2u64:
408 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
409 assert(ir->type->base_type == GLSL_TYPE_UINT64);
410 break;
411 case ir_unop_u2u64:
412 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
413 assert(ir->type->base_type == GLSL_TYPE_UINT64);
414 break;
415 case ir_unop_f2u64:
416 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
417 assert(ir->type->base_type == GLSL_TYPE_UINT64);
418 break;
419 case ir_unop_d2u64:
420 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
421 assert(ir->type->base_type == GLSL_TYPE_UINT64);
422 break;
423 case ir_unop_u642i64:
424 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64);
425 assert(ir->type->base_type == GLSL_TYPE_INT64);
426 break;
427 case ir_unop_i642u64:
428 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64);
429 assert(ir->type->base_type == GLSL_TYPE_UINT64);
430 break;
431 case ir_unop_trunc:
432 case ir_unop_round_even:
433 case ir_unop_ceil:
434 case ir_unop_floor:
435 case ir_unop_fract:
436 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
437 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
438 assert(ir->operands[0]->type == ir->type);
439 break;
440 case ir_unop_sin:
441 case ir_unop_cos:
442 case ir_unop_dFdx:
443 case ir_unop_dFdx_coarse:
444 case ir_unop_dFdx_fine:
445 case ir_unop_dFdy:
446 case ir_unop_dFdy_coarse:
447 case ir_unop_dFdy_fine:
448 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
449 assert(ir->operands[0]->type == ir->type);
450 break;
451
452 case ir_unop_pack_snorm_2x16:
453 case ir_unop_pack_unorm_2x16:
454 case ir_unop_pack_half_2x16:
455 assert(ir->type == glsl_type::uint_type);
456 assert(ir->operands[0]->type == glsl_type::vec2_type);
457 break;
458
459 case ir_unop_pack_snorm_4x8:
460 case ir_unop_pack_unorm_4x8:
461 assert(ir->type == glsl_type::uint_type);
462 assert(ir->operands[0]->type == glsl_type::vec4_type);
463 break;
464
465 case ir_unop_pack_double_2x32:
466 assert(ir->type == glsl_type::double_type);
467 assert(ir->operands[0]->type == glsl_type::uvec2_type);
468 break;
469
470 case ir_unop_pack_int_2x32:
471 assert(ir->type == glsl_type::int64_t_type);
472 assert(ir->operands[0]->type == glsl_type::ivec2_type);
473 break;
474
475 case ir_unop_pack_uint_2x32:
476 assert(ir->type == glsl_type::uint64_t_type);
477 assert(ir->operands[0]->type == glsl_type::uvec2_type);
478 break;
479
480 case ir_unop_unpack_snorm_2x16:
481 case ir_unop_unpack_unorm_2x16:
482 case ir_unop_unpack_half_2x16:
483 assert(ir->type == glsl_type::vec2_type);
484 assert(ir->operands[0]->type == glsl_type::uint_type);
485 break;
486
487 case ir_unop_unpack_snorm_4x8:
488 case ir_unop_unpack_unorm_4x8:
489 assert(ir->type == glsl_type::vec4_type);
490 assert(ir->operands[0]->type == glsl_type::uint_type);
491 break;
492
493 case ir_unop_unpack_double_2x32:
494 assert(ir->type == glsl_type::uvec2_type);
495 assert(ir->operands[0]->type == glsl_type::double_type);
496 break;
497
498 case ir_unop_unpack_int_2x32:
499 assert(ir->type == glsl_type::ivec2_type);
500 assert(ir->operands[0]->type == glsl_type::int64_t_type);
501 break;
502
503 case ir_unop_unpack_uint_2x32:
504 assert(ir->type == glsl_type::uvec2_type);
505 assert(ir->operands[0]->type == glsl_type::uint64_t_type);
506 break;
507
508 case ir_unop_bitfield_reverse:
509 assert(ir->operands[0]->type == ir->type);
510 assert(ir->type->is_integer());
511 break;
512
513 case ir_unop_bit_count:
514 case ir_unop_find_msb:
515 case ir_unop_find_lsb:
516 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
517 assert(ir->operands[0]->type->is_integer());
518 assert(ir->type->base_type == GLSL_TYPE_INT);
519 break;
520
521 case ir_unop_noise:
522 /* XXX what can we assert here? */
523 break;
524
525 case ir_unop_interpolate_at_centroid:
526 assert(ir->operands[0]->type == ir->type);
527 assert(ir->operands[0]->type->is_float());
528 break;
529
530 case ir_unop_get_buffer_size:
531 assert(ir->type == glsl_type::int_type);
532 assert(ir->operands[0]->type == glsl_type::uint_type);
533 break;
534
535 case ir_unop_ssbo_unsized_array_length:
536 assert(ir->type == glsl_type::int_type);
537 assert(ir->operands[0]->type->is_array());
538 assert(ir->operands[0]->type->is_unsized_array());
539 break;
540
541 case ir_unop_d2f:
542 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
543 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
544 break;
545 case ir_unop_f2d:
546 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
547 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
548 break;
549 case ir_unop_d2i:
550 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
551 assert(ir->type->base_type == GLSL_TYPE_INT);
552 break;
553 case ir_unop_i2d:
554 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
555 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
556 break;
557 case ir_unop_d2u:
558 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
559 assert(ir->type->base_type == GLSL_TYPE_UINT);
560 break;
561 case ir_unop_u2d:
562 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
563 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
564 break;
565 case ir_unop_d2b:
566 assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
567 assert(ir->type->base_type == GLSL_TYPE_BOOL);
568 break;
569
570 case ir_unop_frexp_sig:
571 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
572 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
573 assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
574 break;
575 case ir_unop_frexp_exp:
576 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
577 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
578 assert(ir->type->base_type == GLSL_TYPE_INT);
579 break;
580 case ir_unop_subroutine_to_int:
581 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
582 assert(ir->type->base_type == GLSL_TYPE_INT);
583 break;
584
585 case ir_unop_vote_any:
586 case ir_unop_vote_all:
587 case ir_unop_vote_eq:
588 assert(ir->type == glsl_type::bool_type);
589 assert(ir->operands[0]->type == glsl_type::bool_type);
590 break;
591
592 case ir_binop_add:
593 case ir_binop_sub:
594 case ir_binop_mul:
595 case ir_binop_div:
596 case ir_binop_mod:
597 case ir_binop_min:
598 case ir_binop_max:
599 case ir_binop_pow:
600 assert(ir->operands[0]->type->base_type ==
601 ir->operands[1]->type->base_type);
602
603 if (ir->operands[0]->type->is_scalar())
604 assert(ir->operands[1]->type == ir->type);
605 else if (ir->operands[1]->type->is_scalar())
606 assert(ir->operands[0]->type == ir->type);
607 else if (ir->operands[0]->type->is_vector() &&
608 ir->operands[1]->type->is_vector()) {
609 assert(ir->operands[0]->type == ir->operands[1]->type);
610 assert(ir->operands[0]->type == ir->type);
611 }
612 break;
613
614 case ir_binop_imul_high:
615 assert(ir->type == ir->operands[0]->type);
616 assert(ir->type == ir->operands[1]->type);
617 assert(ir->type->is_integer());
618 break;
619
620 case ir_binop_carry:
621 case ir_binop_borrow:
622 assert(ir->type == ir->operands[0]->type);
623 assert(ir->type == ir->operands[1]->type);
624 assert(ir->type->base_type == GLSL_TYPE_UINT);
625 break;
626
627 case ir_binop_less:
628 case ir_binop_greater:
629 case ir_binop_lequal:
630 case ir_binop_gequal:
631 case ir_binop_equal:
632 case ir_binop_nequal:
633 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
634 * ==, and != operators. The IR operators perform a component-wise
635 * comparison on scalar or vector types and return a boolean scalar or
636 * vector type of the same size.
637 */
638 assert(ir->type->base_type == GLSL_TYPE_BOOL);
639 assert(ir->operands[0]->type == ir->operands[1]->type);
640 assert(ir->operands[0]->type->is_vector()
641 || ir->operands[0]->type->is_scalar());
642 assert(ir->operands[0]->type->vector_elements
643 == ir->type->vector_elements);
644 break;
645
646 case ir_binop_all_equal:
647 case ir_binop_any_nequal:
648 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
649 * return a scalar boolean. The IR matches that.
650 */
651 assert(ir->type == glsl_type::bool_type);
652 assert(ir->operands[0]->type == ir->operands[1]->type);
653 break;
654
655 case ir_binop_lshift:
656 case ir_binop_rshift:
657 assert(ir->operands[0]->type->is_integer_32_64() &&
658 ir->operands[1]->type->is_integer());
659 if (ir->operands[0]->type->is_scalar()) {
660 assert(ir->operands[1]->type->is_scalar());
661 }
662 if (ir->operands[0]->type->is_vector() &&
663 ir->operands[1]->type->is_vector()) {
664 assert(ir->operands[0]->type->components() ==
665 ir->operands[1]->type->components());
666 }
667 assert(ir->type == ir->operands[0]->type);
668 break;
669
670 case ir_binop_bit_and:
671 case ir_binop_bit_xor:
672 case ir_binop_bit_or:
673 assert(ir->operands[0]->type->base_type ==
674 ir->operands[1]->type->base_type);
675 assert(ir->type->is_integer_32_64());
676 if (ir->operands[0]->type->is_vector() &&
677 ir->operands[1]->type->is_vector()) {
678 assert(ir->operands[0]->type->vector_elements ==
679 ir->operands[1]->type->vector_elements);
680 }
681 break;
682
683 case ir_binop_logic_and:
684 case ir_binop_logic_xor:
685 case ir_binop_logic_or:
686 assert(ir->type->base_type == GLSL_TYPE_BOOL);
687 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
688 assert(ir->operands[1]->type->base_type == GLSL_TYPE_BOOL);
689 break;
690
691 case ir_binop_dot:
692 assert(ir->type == glsl_type::float_type ||
693 ir->type == glsl_type::double_type);
694 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
695 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
696 assert(ir->operands[0]->type->is_vector());
697 assert(ir->operands[0]->type == ir->operands[1]->type);
698 break;
699
700 case ir_binop_ubo_load:
701 assert(ir->operands[0]->type == glsl_type::uint_type);
702
703 assert(ir->operands[1]->type == glsl_type::uint_type);
704 break;
705
706 case ir_binop_ldexp:
707 assert(ir->operands[0]->type == ir->type);
708 assert(ir->operands[0]->type->is_float() ||
709 ir->operands[0]->type->is_double());
710 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
711 assert(ir->operands[0]->type->components() ==
712 ir->operands[1]->type->components());
713 break;
714
715 case ir_binop_vector_extract:
716 assert(ir->operands[0]->type->is_vector());
717 assert(ir->operands[1]->type->is_scalar()
718 && ir->operands[1]->type->is_integer());
719 break;
720
721 case ir_binop_interpolate_at_offset:
722 assert(ir->operands[0]->type == ir->type);
723 assert(ir->operands[0]->type->is_float());
724 assert(ir->operands[1]->type->components() == 2);
725 assert(ir->operands[1]->type->is_float());
726 break;
727
728 case ir_binop_interpolate_at_sample:
729 assert(ir->operands[0]->type == ir->type);
730 assert(ir->operands[0]->type->is_float());
731 assert(ir->operands[1]->type == glsl_type::int_type);
732 break;
733
734 case ir_triop_fma:
735 assert(ir->type->base_type == GLSL_TYPE_FLOAT ||
736 ir->type->base_type == GLSL_TYPE_DOUBLE);
737 assert(ir->type == ir->operands[0]->type);
738 assert(ir->type == ir->operands[1]->type);
739 assert(ir->type == ir->operands[2]->type);
740 break;
741
742 case ir_triop_lrp:
743 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
744 ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
745 assert(ir->operands[0]->type == ir->operands[1]->type);
746 assert(ir->operands[2]->type == ir->operands[0]->type ||
747 ir->operands[2]->type == glsl_type::float_type ||
748 ir->operands[2]->type == glsl_type::double_type);
749 break;
750
751 case ir_triop_csel:
752 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
753 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
754 assert(ir->type == ir->operands[1]->type);
755 assert(ir->type == ir->operands[2]->type);
756 break;
757
758 case ir_triop_bitfield_extract:
759 assert(ir->type->is_integer());
760 assert(ir->operands[0]->type == ir->type);
761 assert(ir->operands[1]->type == ir->type);
762 assert(ir->operands[2]->type == ir->type);
763 break;
764
765 case ir_triop_vector_insert:
766 assert(ir->operands[0]->type->is_vector());
767 assert(ir->operands[1]->type->is_scalar());
768 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
769 assert(ir->operands[2]->type->is_scalar()
770 && ir->operands[2]->type->is_integer());
771 assert(ir->type == ir->operands[0]->type);
772 break;
773
774 case ir_quadop_bitfield_insert:
775 assert(ir->type->is_integer());
776 assert(ir->operands[0]->type == ir->type);
777 assert(ir->operands[1]->type == ir->type);
778 assert(ir->operands[2]->type == ir->type);
779 assert(ir->operands[3]->type == ir->type);
780 break;
781
782 case ir_quadop_vector:
783 /* The vector operator collects some number of scalars and generates a
784 * vector from them.
785 *
786 * - All of the operands must be scalar.
787 * - Number of operands must matche the size of the resulting vector.
788 * - Base type of the operands must match the base type of the result.
789 */
790 assert(ir->type->is_vector());
791 switch (ir->type->vector_elements) {
792 case 2:
793 assert(ir->operands[0]->type->is_scalar());
794 assert(ir->operands[0]->type->base_type == ir->type->base_type);
795 assert(ir->operands[1]->type->is_scalar());
796 assert(ir->operands[1]->type->base_type == ir->type->base_type);
797 assert(ir->operands[2] == NULL);
798 assert(ir->operands[3] == NULL);
799 break;
800 case 3:
801 assert(ir->operands[0]->type->is_scalar());
802 assert(ir->operands[0]->type->base_type == ir->type->base_type);
803 assert(ir->operands[1]->type->is_scalar());
804 assert(ir->operands[1]->type->base_type == ir->type->base_type);
805 assert(ir->operands[2]->type->is_scalar());
806 assert(ir->operands[2]->type->base_type == ir->type->base_type);
807 assert(ir->operands[3] == NULL);
808 break;
809 case 4:
810 assert(ir->operands[0]->type->is_scalar());
811 assert(ir->operands[0]->type->base_type == ir->type->base_type);
812 assert(ir->operands[1]->type->is_scalar());
813 assert(ir->operands[1]->type->base_type == ir->type->base_type);
814 assert(ir->operands[2]->type->is_scalar());
815 assert(ir->operands[2]->type->base_type == ir->type->base_type);
816 assert(ir->operands[3]->type->is_scalar());
817 assert(ir->operands[3]->type->base_type == ir->type->base_type);
818 break;
819 default:
820 /* The is_vector assertion above should prevent execution from ever
821 * getting here.
822 */
823 assert(!"Should not get here.");
824 break;
825 }
826 }
827
828 return visit_continue;
829 }
830
831 ir_visitor_status
832 ir_validate::visit_leave(ir_swizzle *ir)
833 {
834 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
835
836 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
837 if (chans[i] >= ir->val->type->vector_elements) {
838 printf("ir_swizzle @ %p specifies a channel not present "
839 "in the value.\n", (void *) ir);
840 ir->print();
841 abort();
842 }
843 }
844
845 return visit_continue;
846 }
847
848 ir_visitor_status
849 ir_validate::visit(ir_variable *ir)
850 {
851 /* An ir_variable is the one thing that can (and will) appear multiple times
852 * in an IR tree. It is added to the hashtable so that it can be used
853 * in the ir_dereference_variable handler to ensure that a variable is
854 * declared before it is dereferenced.
855 */
856 if (ir->name && ir->is_name_ralloced())
857 assert(ralloc_parent(ir->name) == ir);
858
859 _mesa_set_add(ir_set, ir);
860
861 /* If a variable is an array, verify that the maximum array index is in
862 * bounds. There was once an error in AST-to-HIR conversion that set this
863 * to be out of bounds.
864 */
865 if (ir->type->array_size() > 0) {
866 if (ir->data.max_array_access >= (int)ir->type->length) {
867 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
868 ir->data.max_array_access, ir->type->length - 1);
869 ir->print();
870 abort();
871 }
872 }
873
874 /* If a variable is an interface block (or an array of interface blocks),
875 * verify that the maximum array index for each interface member is in
876 * bounds.
877 */
878 if (ir->is_interface_instance()) {
879 const glsl_struct_field *fields =
880 ir->get_interface_type()->fields.structure;
881 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
882 if (fields[i].type->array_size() > 0 &&
883 !fields[i].implicit_sized_array) {
884 const int *const max_ifc_array_access =
885 ir->get_max_ifc_array_access();
886
887 assert(max_ifc_array_access != NULL);
888
889 if (max_ifc_array_access[i] >= (int)fields[i].type->length) {
890 printf("ir_variable has maximum access out of bounds for "
891 "field %s (%d vs %d)\n", fields[i].name,
892 max_ifc_array_access[i], fields[i].type->length);
893 ir->print();
894 abort();
895 }
896 }
897 }
898 }
899
900 if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
901 printf("ir_variable didn't have an initializer, but has a constant "
902 "initializer value.\n");
903 ir->print();
904 abort();
905 }
906
907 if (ir->data.mode == ir_var_uniform
908 && is_gl_identifier(ir->name)
909 && ir->get_state_slots() == NULL) {
910 printf("built-in uniform has no state\n");
911 ir->print();
912 abort();
913 }
914
915 return visit_continue;
916 }
917
918 ir_visitor_status
919 ir_validate::visit_enter(ir_assignment *ir)
920 {
921 const ir_dereference *const lhs = ir->lhs;
922 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
923 if (ir->write_mask == 0) {
924 printf("Assignment LHS is %s, but write mask is 0:\n",
925 lhs->type->is_scalar() ? "scalar" : "vector");
926 ir->print();
927 abort();
928 }
929
930 int lhs_components = 0;
931 for (int i = 0; i < 4; i++) {
932 if (ir->write_mask & (1 << i))
933 lhs_components++;
934 }
935
936 if (lhs_components != ir->rhs->type->vector_elements) {
937 printf("Assignment count of LHS write mask channels enabled not\n"
938 "matching RHS vector size (%d LHS, %d RHS).\n",
939 lhs_components, ir->rhs->type->vector_elements);
940 ir->print();
941 abort();
942 }
943 }
944
945 this->validate_ir(ir, this->data_enter);
946
947 return visit_continue;
948 }
949
950 ir_visitor_status
951 ir_validate::visit_enter(ir_call *ir)
952 {
953 ir_function_signature *const callee = ir->callee;
954
955 if (callee->ir_type != ir_type_function_signature) {
956 printf("IR called by ir_call is not ir_function_signature!\n");
957 abort();
958 }
959
960 if (ir->return_deref) {
961 if (ir->return_deref->type != callee->return_type) {
962 printf("callee type %s does not match return storage type %s\n",
963 callee->return_type->name, ir->return_deref->type->name);
964 abort();
965 }
966 } else if (callee->return_type != glsl_type::void_type) {
967 printf("ir_call has non-void callee but no return storage\n");
968 abort();
969 }
970
971 const exec_node *formal_param_node = callee->parameters.get_head_raw();
972 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
973 while (true) {
974 if (formal_param_node->is_tail_sentinel()
975 != actual_param_node->is_tail_sentinel()) {
976 printf("ir_call has the wrong number of parameters:\n");
977 goto dump_ir;
978 }
979 if (formal_param_node->is_tail_sentinel()) {
980 break;
981 }
982 const ir_variable *formal_param
983 = (const ir_variable *) formal_param_node;
984 const ir_rvalue *actual_param
985 = (const ir_rvalue *) actual_param_node;
986 if (formal_param->type != actual_param->type) {
987 printf("ir_call parameter type mismatch:\n");
988 goto dump_ir;
989 }
990 if (formal_param->data.mode == ir_var_function_out
991 || formal_param->data.mode == ir_var_function_inout) {
992 if (!actual_param->is_lvalue()) {
993 printf("ir_call out/inout parameters must be lvalues:\n");
994 goto dump_ir;
995 }
996 }
997 formal_param_node = formal_param_node->next;
998 actual_param_node = actual_param_node->next;
999 }
1000
1001 return visit_continue;
1002
1003 dump_ir:
1004 ir->print();
1005 printf("callee:\n");
1006 callee->print();
1007 abort();
1008 return visit_stop;
1009 }
1010
1011 void
1012 ir_validate::validate_ir(ir_instruction *ir, void *data)
1013 {
1014 struct set *ir_set = (struct set *) data;
1015
1016 if (_mesa_set_search(ir_set, ir)) {
1017 printf("Instruction node present twice in ir tree:\n");
1018 ir->print();
1019 printf("\n");
1020 abort();
1021 }
1022 _mesa_set_add(ir_set, ir);
1023 }
1024
1025 void
1026 check_node_type(ir_instruction *ir, void *data)
1027 {
1028 (void) data;
1029
1030 if (ir->ir_type >= ir_type_max) {
1031 printf("Instruction node with unset type\n");
1032 ir->print(); printf("\n");
1033 }
1034 ir_rvalue *value = ir->as_rvalue();
1035 if (value != NULL)
1036 assert(value->type != glsl_type::error_type);
1037 }
1038
1039 void
1040 validate_ir_tree(exec_list *instructions)
1041 {
1042 /* We shouldn't have any reason to validate IR in a release build,
1043 * and it's half composed of assert()s anyway which wouldn't do
1044 * anything.
1045 */
1046 #ifdef DEBUG
1047 ir_validate v;
1048
1049 v.run(instructions);
1050
1051 foreach_in_list(ir_instruction, ir, instructions) {
1052 visit_tree(ir, check_node_type, NULL);
1053 }
1054 #endif
1055 }