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