glsl: Function signatures cannot have NULL return type
[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 <inttypes.h>
37 #include "ir.h"
38 #include "ir_hierarchical_visitor.h"
39 #include "program/hash_table.h"
40 #include "glsl_types.h"
41
42 class ir_validate : public ir_hierarchical_visitor {
43 public:
44 ir_validate()
45 {
46 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
47 hash_table_pointer_compare);
48
49 this->current_function = NULL;
50
51 this->callback = ir_validate::validate_ir;
52 this->data = ht;
53 }
54
55 ~ir_validate()
56 {
57 hash_table_dtor(this->ht);
58 }
59
60 virtual ir_visitor_status visit(ir_variable *v);
61 virtual ir_visitor_status visit(ir_dereference_variable *ir);
62 virtual ir_visitor_status visit(ir_if *ir);
63
64 virtual ir_visitor_status visit_leave(ir_loop *ir);
65 virtual ir_visitor_status visit_enter(ir_function *ir);
66 virtual ir_visitor_status visit_leave(ir_function *ir);
67 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
68
69 virtual ir_visitor_status visit_leave(ir_expression *ir);
70 virtual ir_visitor_status visit_leave(ir_swizzle *ir);
71
72 virtual ir_visitor_status visit_enter(ir_assignment *ir);
73
74 static void validate_ir(ir_instruction *ir, void *data);
75
76 ir_function *current_function;
77
78 struct hash_table *ht;
79 };
80
81
82 ir_visitor_status
83 ir_validate::visit(ir_dereference_variable *ir)
84 {
85 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
86 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
87 (void *) ir, (void *) ir->var);
88 abort();
89 }
90
91 if (hash_table_find(ht, ir->var) == NULL) {
92 printf("ir_dereference_variable @ %p specifies undeclared variable "
93 "`%s' @ %p\n",
94 (void *) ir, ir->var->name, (void *) ir->var);
95 abort();
96 }
97
98 this->validate_ir(ir, this->data);
99
100 return visit_continue;
101 }
102
103 ir_visitor_status
104 ir_validate::visit(ir_if *ir)
105 {
106 if (ir->condition->type != glsl_type::bool_type) {
107 printf("ir_if condition %s type instead of bool.\n",
108 ir->condition->type->name);
109 ir->print();
110 printf("\n");
111 abort();
112 }
113
114 return visit_continue;
115 }
116
117
118 ir_visitor_status
119 ir_validate::visit_leave(ir_loop *ir)
120 {
121 if (ir->counter != NULL) {
122 if ((ir->from == NULL) || (ir->from == NULL) || (ir->increment == NULL)) {
123 printf("ir_loop has invalid loop controls:\n"
124 " counter: %p\n"
125 " from: %p\n"
126 " to: %p\n"
127 " increment: %p\n",
128 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
129 (void *) ir->increment);
130 abort();
131 }
132
133 if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
134 printf("ir_loop has invalid comparitor %d\n", ir->cmp);
135 abort();
136 }
137 } else {
138 if ((ir->from != NULL) || (ir->from != NULL) || (ir->increment != NULL)) {
139 printf("ir_loop has invalid loop controls:\n"
140 " counter: %p\n"
141 " from: %p\n"
142 " to: %p\n"
143 " increment: %p\n",
144 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
145 (void *) ir->increment);
146 abort();
147 }
148 }
149
150 return visit_continue;
151 }
152
153
154 ir_visitor_status
155 ir_validate::visit_enter(ir_function *ir)
156 {
157 /* Function definitions cannot be nested.
158 */
159 if (this->current_function != NULL) {
160 printf("Function definition nested inside another function "
161 "definition:\n");
162 printf("%s %p inside %s %p\n",
163 ir->name, (void *) ir,
164 this->current_function->name, (void *) this->current_function);
165 abort();
166 }
167
168 /* Store the current function hierarchy being traversed. This is used
169 * by the function signature visitor to ensure that the signatures are
170 * linked with the correct functions.
171 */
172 this->current_function = ir;
173
174 this->validate_ir(ir, this->data);
175
176 return visit_continue;
177 }
178
179 ir_visitor_status
180 ir_validate::visit_leave(ir_function *ir)
181 {
182 assert(ralloc_parent(ir->name) == ir);
183
184 this->current_function = NULL;
185 return visit_continue;
186 }
187
188 ir_visitor_status
189 ir_validate::visit_enter(ir_function_signature *ir)
190 {
191 if (this->current_function != ir->function()) {
192 printf("Function signature nested inside wrong function "
193 "definition:\n");
194 printf("%p inside %s %p instead of %s %p\n",
195 (void *) ir,
196 this->current_function->name, (void *) this->current_function,
197 ir->function_name(), (void *) ir->function());
198 abort();
199 }
200
201 if (ir->return_type == NULL) {
202 printf("Function signature %p for function %s has NULL return type.\n",
203 ir, ir->function_name());
204 abort();
205 }
206
207 this->validate_ir(ir, this->data);
208
209 return visit_continue;
210 }
211
212 ir_visitor_status
213 ir_validate::visit_leave(ir_expression *ir)
214 {
215 switch (ir->operation) {
216 case ir_unop_bit_not:
217 assert(ir->operands[0]->type == ir->type);
218 break;
219 case ir_unop_logic_not:
220 assert(ir->type->base_type == GLSL_TYPE_BOOL);
221 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
222 break;
223
224 case ir_unop_neg:
225 case ir_unop_abs:
226 case ir_unop_sign:
227 case ir_unop_rcp:
228 case ir_unop_rsq:
229 case ir_unop_sqrt:
230 assert(ir->type == ir->operands[0]->type);
231 break;
232
233 case ir_unop_exp:
234 case ir_unop_log:
235 case ir_unop_exp2:
236 case ir_unop_log2:
237 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
238 assert(ir->type == ir->operands[0]->type);
239 break;
240
241 case ir_unop_f2i:
242 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
243 assert(ir->type->base_type == GLSL_TYPE_INT);
244 break;
245 case ir_unop_i2f:
246 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
247 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
248 break;
249 case ir_unop_f2b:
250 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
251 assert(ir->type->base_type == GLSL_TYPE_BOOL);
252 break;
253 case ir_unop_b2f:
254 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
255 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
256 break;
257 case ir_unop_i2b:
258 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
259 assert(ir->type->base_type == GLSL_TYPE_BOOL);
260 break;
261 case ir_unop_b2i:
262 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
263 assert(ir->type->base_type == GLSL_TYPE_INT);
264 break;
265 case ir_unop_u2f:
266 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
267 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
268 break;
269
270 case ir_unop_any:
271 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
272 assert(ir->type == glsl_type::bool_type);
273 break;
274
275 case ir_unop_trunc:
276 case ir_unop_round_even:
277 case ir_unop_ceil:
278 case ir_unop_floor:
279 case ir_unop_fract:
280 case ir_unop_sin:
281 case ir_unop_cos:
282 case ir_unop_sin_reduced:
283 case ir_unop_cos_reduced:
284 case ir_unop_dFdx:
285 case ir_unop_dFdy:
286 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
287 assert(ir->operands[0]->type == ir->type);
288 break;
289
290 case ir_unop_noise:
291 /* XXX what can we assert here? */
292 break;
293
294 case ir_binop_add:
295 case ir_binop_sub:
296 case ir_binop_mul:
297 case ir_binop_div:
298 case ir_binop_mod:
299 case ir_binop_min:
300 case ir_binop_max:
301 case ir_binop_pow:
302 if (ir->operands[0]->type->is_scalar())
303 assert(ir->operands[1]->type == ir->type);
304 else if (ir->operands[1]->type->is_scalar())
305 assert(ir->operands[0]->type == ir->type);
306 else if (ir->operands[0]->type->is_vector() &&
307 ir->operands[1]->type->is_vector()) {
308 assert(ir->operands[0]->type == ir->operands[1]->type);
309 assert(ir->operands[0]->type == ir->type);
310 }
311 break;
312
313 case ir_binop_less:
314 case ir_binop_greater:
315 case ir_binop_lequal:
316 case ir_binop_gequal:
317 case ir_binop_equal:
318 case ir_binop_nequal:
319 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
320 * ==, and != operators. The IR operators perform a component-wise
321 * comparison on scalar or vector types and return a boolean scalar or
322 * vector type of the same size.
323 */
324 assert(ir->type->base_type == GLSL_TYPE_BOOL);
325 assert(ir->operands[0]->type == ir->operands[1]->type);
326 assert(ir->operands[0]->type->is_vector()
327 || ir->operands[0]->type->is_scalar());
328 assert(ir->operands[0]->type->vector_elements
329 == ir->type->vector_elements);
330 break;
331
332 case ir_binop_all_equal:
333 case ir_binop_any_nequal:
334 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
335 * return a scalar boolean. The IR matches that.
336 */
337 assert(ir->type == glsl_type::bool_type);
338 assert(ir->operands[0]->type == ir->operands[1]->type);
339 break;
340
341 case ir_binop_lshift:
342 case ir_binop_rshift:
343 assert(ir->operands[0]->type->is_integer() &&
344 ir->operands[1]->type->is_integer());
345 if (ir->operands[0]->type->is_scalar()) {
346 assert(ir->operands[1]->type->is_scalar());
347 }
348 if (ir->operands[0]->type->is_vector() &&
349 ir->operands[1]->type->is_vector()) {
350 assert(ir->operands[0]->type->components() ==
351 ir->operands[1]->type->components());
352 }
353 assert(ir->type == ir->operands[0]->type);
354 break;
355
356 case ir_binop_bit_and:
357 case ir_binop_bit_xor:
358 case ir_binop_bit_or:
359 assert(ir->operands[0]->type->base_type ==
360 ir->operands[1]->type->base_type);
361 assert(ir->type->is_integer());
362 if (ir->operands[0]->type->is_vector() &&
363 ir->operands[1]->type->is_vector()) {
364 assert(ir->operands[0]->type->vector_elements ==
365 ir->operands[1]->type->vector_elements);
366 }
367 break;
368
369 case ir_binop_logic_and:
370 case ir_binop_logic_xor:
371 case ir_binop_logic_or:
372 assert(ir->type == glsl_type::bool_type);
373 assert(ir->operands[0]->type == glsl_type::bool_type);
374 assert(ir->operands[1]->type == glsl_type::bool_type);
375 break;
376
377 case ir_binop_dot:
378 assert(ir->type == glsl_type::float_type);
379 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
380 assert(ir->operands[0]->type->is_vector());
381 assert(ir->operands[0]->type == ir->operands[1]->type);
382 break;
383
384 case ir_quadop_vector:
385 /* The vector operator collects some number of scalars and generates a
386 * vector from them.
387 *
388 * - All of the operands must be scalar.
389 * - Number of operands must matche the size of the resulting vector.
390 * - Base type of the operands must match the base type of the result.
391 */
392 assert(ir->type->is_vector());
393 switch (ir->type->vector_elements) {
394 case 2:
395 assert(ir->operands[0]->type->is_scalar());
396 assert(ir->operands[0]->type->base_type == ir->type->base_type);
397 assert(ir->operands[1]->type->is_scalar());
398 assert(ir->operands[1]->type->base_type == ir->type->base_type);
399 assert(ir->operands[2] == NULL);
400 assert(ir->operands[3] == NULL);
401 break;
402 case 3:
403 assert(ir->operands[0]->type->is_scalar());
404 assert(ir->operands[0]->type->base_type == ir->type->base_type);
405 assert(ir->operands[1]->type->is_scalar());
406 assert(ir->operands[1]->type->base_type == ir->type->base_type);
407 assert(ir->operands[2]->type->is_scalar());
408 assert(ir->operands[2]->type->base_type == ir->type->base_type);
409 assert(ir->operands[3] == NULL);
410 break;
411 case 4:
412 assert(ir->operands[0]->type->is_scalar());
413 assert(ir->operands[0]->type->base_type == ir->type->base_type);
414 assert(ir->operands[1]->type->is_scalar());
415 assert(ir->operands[1]->type->base_type == ir->type->base_type);
416 assert(ir->operands[2]->type->is_scalar());
417 assert(ir->operands[2]->type->base_type == ir->type->base_type);
418 assert(ir->operands[3]->type->is_scalar());
419 assert(ir->operands[3]->type->base_type == ir->type->base_type);
420 break;
421 default:
422 /* The is_vector assertion above should prevent execution from ever
423 * getting here.
424 */
425 assert(!"Should not get here.");
426 break;
427 }
428 }
429
430 return visit_continue;
431 }
432
433 ir_visitor_status
434 ir_validate::visit_leave(ir_swizzle *ir)
435 {
436 int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
437
438 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
439 if (chans[i] >= ir->val->type->vector_elements) {
440 printf("ir_swizzle @ %p specifies a channel not present "
441 "in the value.\n", (void *) ir);
442 ir->print();
443 abort();
444 }
445 }
446
447 return visit_continue;
448 }
449
450 ir_visitor_status
451 ir_validate::visit(ir_variable *ir)
452 {
453 /* An ir_variable is the one thing that can (and will) appear multiple times
454 * in an IR tree. It is added to the hashtable so that it can be used
455 * in the ir_dereference_variable handler to ensure that a variable is
456 * declared before it is dereferenced.
457 */
458 if (ir->name)
459 assert(ralloc_parent(ir->name) == ir);
460
461 hash_table_insert(ht, ir, ir);
462 return visit_continue;
463 }
464
465 ir_visitor_status
466 ir_validate::visit_enter(ir_assignment *ir)
467 {
468 const ir_dereference *const lhs = ir->lhs;
469 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
470 if (ir->write_mask == 0) {
471 printf("Assignment LHS is %s, but write mask is 0:\n",
472 lhs->type->is_scalar() ? "scalar" : "vector");
473 ir->print();
474 abort();
475 }
476
477 int lhs_components = 0;
478 for (int i = 0; i < 4; i++) {
479 if (ir->write_mask & (1 << i))
480 lhs_components++;
481 }
482
483 if (lhs_components != ir->rhs->type->vector_elements) {
484 printf("Assignment count of LHS write mask channels enabled not\n"
485 "matching RHS vector size (%d LHS, %d RHS).\n",
486 lhs_components, ir->rhs->type->vector_elements);
487 ir->print();
488 abort();
489 }
490 }
491
492 this->validate_ir(ir, this->data);
493
494 return visit_continue;
495 }
496
497 void
498 ir_validate::validate_ir(ir_instruction *ir, void *data)
499 {
500 struct hash_table *ht = (struct hash_table *) data;
501
502 if (hash_table_find(ht, ir)) {
503 printf("Instruction node present twice in ir tree:\n");
504 ir->print();
505 printf("\n");
506 abort();
507 }
508 hash_table_insert(ht, ir, ir);
509 }
510
511 void
512 check_node_type(ir_instruction *ir, void *data)
513 {
514 (void) data;
515
516 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
517 printf("Instruction node with unset type\n");
518 ir->print(); printf("\n");
519 }
520 assert(ir->type != glsl_type::error_type);
521 }
522
523 void
524 validate_ir_tree(exec_list *instructions)
525 {
526 ir_validate v;
527
528 v.run(instructions);
529
530 foreach_iter(exec_list_iterator, iter, *instructions) {
531 ir_instruction *ir = (ir_instruction *)iter.get();
532
533 visit_tree(ir, check_node_type, NULL);
534 }
535 }