d3d1x: fix progs linking if not all EGL platforms are enabled
[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
71 virtual ir_visitor_status visit_enter(ir_assignment *ir);
72
73 static void validate_ir(ir_instruction *ir, void *data);
74
75 ir_function *current_function;
76
77 struct hash_table *ht;
78 };
79
80
81 ir_visitor_status
82 ir_validate::visit(ir_dereference_variable *ir)
83 {
84 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
85 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
86 (void *) ir, (void *) ir->var);
87 abort();
88 }
89
90 if (hash_table_find(ht, ir->var) == NULL) {
91 printf("ir_dereference_variable @ %p specifies undeclared variable "
92 "`%s' @ %p\n",
93 (void *) ir, ir->var->name, (void *) ir->var);
94 abort();
95 }
96
97 this->validate_ir(ir, this->data);
98
99 return visit_continue;
100 }
101
102 ir_visitor_status
103 ir_validate::visit(ir_if *ir)
104 {
105 if (ir->condition->type != glsl_type::bool_type) {
106 printf("ir_if condition %s type instead of bool.\n",
107 ir->condition->type->name);
108 ir->print();
109 printf("\n");
110 abort();
111 }
112
113 return visit_continue;
114 }
115
116
117 ir_visitor_status
118 ir_validate::visit_leave(ir_loop *ir)
119 {
120 if (ir->counter != NULL) {
121 if ((ir->from == NULL) || (ir->from == NULL) || (ir->increment == NULL)) {
122 printf("ir_loop has invalid loop controls:\n"
123 " counter: %p\n"
124 " from: %p\n"
125 " to: %p\n"
126 " increment: %p\n",
127 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
128 (void *) ir->increment);
129 abort();
130 }
131
132 if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
133 printf("ir_loop has invalid comparitor %d\n", ir->cmp);
134 abort();
135 }
136 } else {
137 if ((ir->from != NULL) || (ir->from != NULL) || (ir->increment != NULL)) {
138 printf("ir_loop has invalid loop controls:\n"
139 " counter: %p\n"
140 " from: %p\n"
141 " to: %p\n"
142 " increment: %p\n",
143 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
144 (void *) ir->increment);
145 abort();
146 }
147 }
148
149 return visit_continue;
150 }
151
152
153 ir_visitor_status
154 ir_validate::visit_enter(ir_function *ir)
155 {
156 /* Function definitions cannot be nested.
157 */
158 if (this->current_function != NULL) {
159 printf("Function definition nested inside another function "
160 "definition:\n");
161 printf("%s %p inside %s %p\n",
162 ir->name, (void *) ir,
163 this->current_function->name, (void *) this->current_function);
164 abort();
165 }
166
167 /* Store the current function hierarchy being traversed. This is used
168 * by the function signature visitor to ensure that the signatures are
169 * linked with the correct functions.
170 */
171 this->current_function = ir;
172
173 this->validate_ir(ir, this->data);
174
175 return visit_continue;
176 }
177
178 ir_visitor_status
179 ir_validate::visit_leave(ir_function *ir)
180 {
181 assert(talloc_parent(ir->name) == ir);
182
183 this->current_function = NULL;
184 return visit_continue;
185 }
186
187 ir_visitor_status
188 ir_validate::visit_enter(ir_function_signature *ir)
189 {
190 if (this->current_function != ir->function()) {
191 printf("Function signature nested inside wrong function "
192 "definition:\n");
193 printf("%p inside %s %p instead of %s %p\n",
194 (void *) ir,
195 this->current_function->name, (void *) this->current_function,
196 ir->function_name(), (void *) ir->function());
197 abort();
198 }
199
200 this->validate_ir(ir, this->data);
201
202 return visit_continue;
203 }
204
205 ir_visitor_status
206 ir_validate::visit_leave(ir_expression *ir)
207 {
208 switch (ir->operation) {
209 case ir_unop_bit_not:
210 assert(ir->operands[0]->type == ir->type);
211 break;
212 case ir_unop_logic_not:
213 assert(ir->type->base_type == GLSL_TYPE_BOOL);
214 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
215 break;
216
217 case ir_unop_neg:
218 case ir_unop_abs:
219 case ir_unop_sign:
220 case ir_unop_rcp:
221 case ir_unop_rsq:
222 case ir_unop_sqrt:
223 assert(ir->type == ir->operands[0]->type);
224 break;
225
226 case ir_unop_exp:
227 case ir_unop_log:
228 case ir_unop_exp2:
229 case ir_unop_log2:
230 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
231 assert(ir->type == ir->operands[0]->type);
232 break;
233
234 case ir_unop_f2i:
235 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
236 assert(ir->type->base_type == GLSL_TYPE_INT);
237 break;
238 case ir_unop_i2f:
239 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
240 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
241 break;
242 case ir_unop_f2b:
243 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
244 assert(ir->type->base_type == GLSL_TYPE_BOOL);
245 break;
246 case ir_unop_b2f:
247 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
248 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
249 break;
250 case ir_unop_i2b:
251 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
252 assert(ir->type->base_type == GLSL_TYPE_BOOL);
253 break;
254 case ir_unop_b2i:
255 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
256 assert(ir->type->base_type == GLSL_TYPE_INT);
257 break;
258 case ir_unop_u2f:
259 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
260 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
261 break;
262
263 case ir_unop_any:
264 assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
265 assert(ir->type == glsl_type::bool_type);
266 break;
267
268 case ir_unop_trunc:
269 case ir_unop_ceil:
270 case ir_unop_floor:
271 case ir_unop_fract:
272 case ir_unop_sin:
273 case ir_unop_cos:
274 case ir_unop_dFdx:
275 case ir_unop_dFdy:
276 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
277 assert(ir->operands[0]->type == ir->type);
278 break;
279
280 case ir_unop_noise:
281 /* XXX what can we assert here? */
282 break;
283
284 case ir_binop_add:
285 case ir_binop_sub:
286 case ir_binop_mul:
287 case ir_binop_div:
288 case ir_binop_mod:
289 case ir_binop_min:
290 case ir_binop_max:
291 case ir_binop_pow:
292 if (ir->operands[0]->type->is_scalar())
293 assert(ir->operands[1]->type == ir->type);
294 else if (ir->operands[1]->type->is_scalar())
295 assert(ir->operands[0]->type == ir->type);
296 else if (ir->operands[0]->type->is_vector() &&
297 ir->operands[1]->type->is_vector()) {
298 assert(ir->operands[0]->type == ir->operands[1]->type);
299 assert(ir->operands[0]->type == ir->type);
300 }
301 break;
302
303 case ir_binop_less:
304 case ir_binop_greater:
305 case ir_binop_lequal:
306 case ir_binop_gequal:
307 case ir_binop_equal:
308 case ir_binop_nequal:
309 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
310 * ==, and != operators. The IR operators perform a component-wise
311 * comparison on scalar or vector types and return a boolean scalar or
312 * vector type of the same size.
313 */
314 assert(ir->type->base_type == GLSL_TYPE_BOOL);
315 assert(ir->operands[0]->type == ir->operands[1]->type);
316 assert(ir->operands[0]->type->is_vector()
317 || ir->operands[0]->type->is_scalar());
318 assert(ir->operands[0]->type->vector_elements
319 == ir->type->vector_elements);
320 break;
321
322 case ir_binop_all_equal:
323 case ir_binop_any_nequal:
324 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
325 * return a scalar boolean. The IR matches that.
326 */
327 assert(ir->type == glsl_type::bool_type);
328 assert(ir->operands[0]->type == ir->operands[1]->type);
329 break;
330
331 case ir_binop_lshift:
332 case ir_binop_rshift:
333 case ir_binop_bit_and:
334 case ir_binop_bit_xor:
335 case ir_binop_bit_or:
336 assert(ir->operands[0]->type == ir->operands[1]->type);
337 assert(ir->type == ir->operands[0]->type);
338 assert(ir->type->base_type == GLSL_TYPE_INT ||
339 ir->type->base_type == GLSL_TYPE_UINT);
340 break;
341
342 case ir_binop_logic_and:
343 case ir_binop_logic_xor:
344 case ir_binop_logic_or:
345 assert(ir->type == glsl_type::bool_type);
346 assert(ir->operands[0]->type == glsl_type::bool_type);
347 assert(ir->operands[1]->type == glsl_type::bool_type);
348 break;
349
350 case ir_binop_dot:
351 assert(ir->type == glsl_type::float_type);
352 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
353 assert(ir->operands[0]->type->is_vector());
354 assert(ir->operands[0]->type == ir->operands[1]->type);
355 break;
356
357 case ir_binop_cross:
358 assert(ir->operands[0]->type == glsl_type::vec3_type);
359 assert(ir->operands[1]->type == glsl_type::vec3_type);
360 assert(ir->type == glsl_type::vec3_type);
361 break;
362 }
363
364 return visit_continue;
365 }
366
367 ir_visitor_status
368 ir_validate::visit(ir_variable *ir)
369 {
370 /* An ir_variable is the one thing that can (and will) appear multiple times
371 * in an IR tree. It is added to the hashtable so that it can be used
372 * in the ir_dereference_variable handler to ensure that a variable is
373 * declared before it is dereferenced.
374 */
375 if (ir->name)
376 assert(talloc_parent(ir->name) == ir);
377
378 hash_table_insert(ht, ir, ir);
379 return visit_continue;
380 }
381
382 ir_visitor_status
383 ir_validate::visit_enter(ir_assignment *ir)
384 {
385 const ir_dereference *const lhs = ir->lhs;
386 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
387 if (ir->write_mask == 0) {
388 printf("Assignment LHS is %s, but write mask is 0:\n",
389 lhs->type->is_scalar() ? "scalar" : "vector");
390 ir->print();
391 abort();
392 }
393
394 int lhs_components = 0;
395 for (int i = 0; i < 4; i++) {
396 if (ir->write_mask & (1 << i))
397 lhs_components++;
398 }
399
400 if (lhs_components != ir->rhs->type->vector_elements) {
401 printf("Assignment count of LHS write mask channels enabled not\n"
402 "matching RHS vector size (%d LHS, %d RHS).\n",
403 lhs_components, ir->rhs->type->vector_elements);
404 ir->print();
405 abort();
406 }
407 }
408
409 this->validate_ir(ir, this->data);
410
411 return visit_continue;
412 }
413
414 void
415 ir_validate::validate_ir(ir_instruction *ir, void *data)
416 {
417 struct hash_table *ht = (struct hash_table *) data;
418
419 if (hash_table_find(ht, ir)) {
420 printf("Instruction node present twice in ir tree:\n");
421 ir->print();
422 printf("\n");
423 abort();
424 }
425 hash_table_insert(ht, ir, ir);
426 }
427
428 void
429 check_node_type(ir_instruction *ir, void *data)
430 {
431 (void) data;
432
433 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
434 printf("Instruction node with unset type\n");
435 ir->print(); printf("\n");
436 }
437 assert(ir->type != glsl_type::error_type);
438 }
439
440 void
441 validate_ir_tree(exec_list *instructions)
442 {
443 ir_validate v;
444
445 v.run(instructions);
446
447 foreach_iter(exec_list_iterator, iter, *instructions) {
448 ir_instruction *ir = (ir_instruction *)iter.get();
449
450 visit_tree(ir, check_node_type, NULL);
451 }
452 }