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 #include <inttypes.h> /* for PRIx64 macro */
25 #include "ir_print_visitor.h"
26 #include "compiler/glsl_types.h"
27 #include "glsl_parser_extras.h"
28 #include "main/macros.h"
29 #include "util/hash_table.h"
30 #include "util/u_string.h"
31 #include "util/half_float.h"
32
33 void
print(void) const34 ir_instruction::print(void) const
35 {
36 this->fprint(stdout);
37 }
38
39 void
fprint(FILE * f) const40 ir_instruction::fprint(FILE *f) const
41 {
42 ir_instruction *deconsted = const_cast<ir_instruction *>(this);
43
44 ir_print_visitor v(f);
45 deconsted->accept(&v);
46 }
47
48 extern "C" {
49 void
_mesa_print_ir(FILE * f,exec_list * instructions,struct _mesa_glsl_parse_state * state)50 _mesa_print_ir(FILE *f, exec_list *instructions,
51 struct _mesa_glsl_parse_state *state)
52 {
53 if (state) {
54 for (unsigned i = 0; i < state->num_user_structures; i++) {
55 const glsl_type *const s = state->user_structures[i];
56
57 fprintf(f, "(structure (%s) (%s@%p) (%u) (\n",
58 s->name, s->name, (void *) s, s->length);
59
60 for (unsigned j = 0; j < s->length; j++) {
61 fprintf(f, "\t((");
62 glsl_print_type(f, s->fields.structure[j].type);
63 fprintf(f, ")(%s))\n", s->fields.structure[j].name);
64 }
65
66 fprintf(f, ")\n");
67 }
68 }
69
70 fprintf(f, "(\n");
71 foreach_in_list(ir_instruction, ir, instructions) {
72 ir->fprint(f);
73 if (ir->ir_type != ir_type_function)
74 fprintf(f, "\n");
75 }
76 fprintf(f, ")\n");
77 }
78
79 void
fprint_ir(FILE * f,const void * instruction)80 fprint_ir(FILE *f, const void *instruction)
81 {
82 const ir_instruction *ir = (const ir_instruction *)instruction;
83 ir->fprint(f);
84 }
85
86 } /* extern "C" */
87
ir_print_visitor(FILE * f)88 ir_print_visitor::ir_print_visitor(FILE *f)
89 : f(f)
90 {
91 indentation = 0;
92 printable_names = _mesa_pointer_hash_table_create(NULL);
93 symbols = _mesa_symbol_table_ctor();
94 mem_ctx = ralloc_context(NULL);
95 }
96
~ir_print_visitor()97 ir_print_visitor::~ir_print_visitor()
98 {
99 _mesa_hash_table_destroy(printable_names, NULL);
100 _mesa_symbol_table_dtor(symbols);
101 ralloc_free(mem_ctx);
102 }
103
indent(void)104 void ir_print_visitor::indent(void)
105 {
106 for (int i = 0; i < indentation; i++)
107 fprintf(f, " ");
108 }
109
110 const char *
unique_name(ir_variable * var)111 ir_print_visitor::unique_name(ir_variable *var)
112 {
113 /* var->name can be NULL in function prototypes when a type is given for a
114 * parameter but no name is given. In that case, just return an empty
115 * string. Don't worry about tracking the generated name in the printable
116 * names hash because this is the only scope where it can ever appear.
117 */
118 if (var->name == NULL) {
119 static unsigned arg = 1;
120 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++);
121 }
122
123 /* Do we already have a name for this variable? */
124 struct hash_entry * entry =
125 _mesa_hash_table_search(this->printable_names, var);
126
127 if (entry != NULL) {
128 return (const char *) entry->data;
129 }
130
131 /* If there's no conflict, just use the original name */
132 const char* name = NULL;
133 if (_mesa_symbol_table_find_symbol(this->symbols, var->name) == NULL) {
134 name = var->name;
135 } else {
136 static unsigned i = 1;
137 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
138 }
139 _mesa_hash_table_insert(this->printable_names, var, (void *) name);
140 _mesa_symbol_table_add_symbol(this->symbols, name, var);
141 return name;
142 }
143
visit(ir_rvalue *)144 void ir_print_visitor::visit(ir_rvalue *)
145 {
146 fprintf(f, "error");
147 }
148
visit(ir_variable * ir)149 void ir_print_visitor::visit(ir_variable *ir)
150 {
151 fprintf(f, "(declare ");
152
153 char binding[32] = {0};
154 if (ir->data.binding)
155 snprintf(binding, sizeof(binding), "binding=%i ", ir->data.binding);
156
157 char loc[32] = {0};
158 if (ir->data.location != -1)
159 snprintf(loc, sizeof(loc), "location=%i ", ir->data.location);
160
161 char component[32] = {0};
162 if (ir->data.explicit_component || ir->data.location_frac != 0)
163 snprintf(component, sizeof(component), "component=%i ",
164 ir->data.location_frac);
165
166 char stream[32] = {0};
167 if (ir->data.stream & (1u << 31)) {
168 if (ir->data.stream & ~(1u << 31)) {
169 snprintf(stream, sizeof(stream), "stream(%u,%u,%u,%u) ",
170 ir->data.stream & 3, (ir->data.stream >> 2) & 3,
171 (ir->data.stream >> 4) & 3, (ir->data.stream >> 6) & 3);
172 }
173 } else if (ir->data.stream) {
174 snprintf(stream, sizeof(stream), "stream%u ", ir->data.stream);
175 }
176
177 char image_format[32] = {0};
178 if (ir->data.image_format) {
179 snprintf(image_format, sizeof(image_format), "format=%x ",
180 ir->data.image_format);
181 }
182
183 const char *const cent = (ir->data.centroid) ? "centroid " : "";
184 const char *const samp = (ir->data.sample) ? "sample " : "";
185 const char *const patc = (ir->data.patch) ? "patch " : "";
186 const char *const inv = (ir->data.invariant) ? "invariant " : "";
187 const char *const explicit_inv = (ir->data.explicit_invariant) ? "explicit_invariant " : "";
188 const char *const prec = (ir->data.precise) ? "precise " : "";
189 const char *const bindless = (ir->data.bindless) ? "bindless " : "";
190 const char *const bound = (ir->data.bound) ? "bound " : "";
191 const char *const memory_read_only = (ir->data.memory_read_only) ? "readonly " : "";
192 const char *const memory_write_only = (ir->data.memory_write_only) ? "writeonly " : "";
193 const char *const memory_coherent = (ir->data.memory_coherent) ? "coherent " : "";
194 const char *const memory_volatile = (ir->data.memory_volatile) ? "volatile " : "";
195 const char *const memory_restrict = (ir->data.memory_restrict) ? "restrict " : "";
196 const char *const mode[] = { "", "uniform ", "shader_storage ",
197 "shader_shared ", "shader_in ", "shader_out ",
198 "in ", "out ", "inout ",
199 "const_in ", "sys ", "temporary " };
200 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
201 const char *const interp[] = { "", "smooth", "flat", "noperspective", "explicit", "color" };
202 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_MODE_COUNT);
203 const char *const precision[] = { "", "highp ", "mediump ", "lowp "};
204
205 fprintf(f, "(%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s) ",
206 binding, loc, component, cent, bindless, bound,
207 image_format, memory_read_only, memory_write_only,
208 memory_coherent, memory_volatile, memory_restrict,
209 samp, patc, inv, explicit_inv, prec, mode[ir->data.mode],
210 stream,
211 interp[ir->data.interpolation], precision[ir->data.precision]);
212
213 glsl_print_type(f, ir->type);
214 fprintf(f, " %s)", unique_name(ir));
215
216 if (ir->constant_initializer) {
217 fprintf(f, " ");
218 visit(ir->constant_initializer);
219 }
220
221 if (ir->constant_value) {
222 fprintf(f, " ");
223 visit(ir->constant_value);
224 }
225 }
226
227
visit(ir_function_signature * ir)228 void ir_print_visitor::visit(ir_function_signature *ir)
229 {
230 _mesa_symbol_table_push_scope(symbols);
231 fprintf(f, "(signature ");
232 indentation++;
233
234 glsl_print_type(f, ir->return_type);
235 fprintf(f, "\n");
236 indent();
237
238 fprintf(f, "(parameters\n");
239 indentation++;
240
241 foreach_in_list(ir_variable, inst, &ir->parameters) {
242 indent();
243 inst->accept(this);
244 fprintf(f, "\n");
245 }
246 indentation--;
247
248 indent();
249 fprintf(f, ")\n");
250
251 indent();
252
253 fprintf(f, "(\n");
254 indentation++;
255
256 foreach_in_list(ir_instruction, inst, &ir->body) {
257 indent();
258 inst->accept(this);
259 fprintf(f, "\n");
260 }
261 indentation--;
262 indent();
263 fprintf(f, "))\n");
264 indentation--;
265 _mesa_symbol_table_pop_scope(symbols);
266 }
267
268
visit(ir_function * ir)269 void ir_print_visitor::visit(ir_function *ir)
270 {
271 fprintf(f, "(%s function %s\n", ir->is_subroutine ? "subroutine" : "", ir->name);
272 indentation++;
273 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
274 indent();
275 sig->accept(this);
276 fprintf(f, "\n");
277 }
278 indentation--;
279 indent();
280 fprintf(f, ")\n\n");
281 }
282
283
visit(ir_expression * ir)284 void ir_print_visitor::visit(ir_expression *ir)
285 {
286 fprintf(f, "(expression ");
287
288 glsl_print_type(f, ir->type);
289
290 fprintf(f, " %s ", ir_expression_operation_strings[ir->operation]);
291
292 for (unsigned i = 0; i < ir->num_operands; i++) {
293 ir->operands[i]->accept(this);
294 }
295
296 fprintf(f, ") ");
297 }
298
299
visit(ir_texture * ir)300 void ir_print_visitor::visit(ir_texture *ir)
301 {
302 fprintf(f, "(%s ", ir->opcode_string());
303
304 if (ir->op == ir_samples_identical) {
305 ir->sampler->accept(this);
306 fprintf(f, " ");
307 ir->coordinate->accept(this);
308 fprintf(f, ")");
309 return;
310 }
311
312 glsl_print_type(f, ir->type);
313 fprintf(f, " ");
314
315 ir->sampler->accept(this);
316 fprintf(f, " ");
317
318 if (ir->op != ir_txs && ir->op != ir_query_levels &&
319 ir->op != ir_texture_samples) {
320 ir->coordinate->accept(this);
321
322 fprintf(f, " ");
323
324 if (ir->op != ir_lod && ir->op != ir_samples_identical)
325 fprintf(f, "%d ", ir->is_sparse);
326
327 if (ir->offset != NULL) {
328 ir->offset->accept(this);
329 } else {
330 fprintf(f, "0");
331 }
332
333 fprintf(f, " ");
334 }
335
336 if (ir->op != ir_txf && ir->op != ir_txf_ms &&
337 ir->op != ir_txs && ir->op != ir_tg4 &&
338 ir->op != ir_query_levels && ir->op != ir_texture_samples) {
339 if (ir->projector)
340 ir->projector->accept(this);
341 else
342 fprintf(f, "1");
343
344 if (ir->shadow_comparator) {
345 fprintf(f, " ");
346 ir->shadow_comparator->accept(this);
347 } else {
348 fprintf(f, " ()");
349 }
350 }
351
352 if (ir->op == ir_tex || ir->op == ir_txb || ir->op == ir_txd) {
353 if (ir->clamp) {
354 fprintf(f, " ");
355 ir->clamp->accept(this);
356 } else {
357 fprintf(f, " ()");
358 }
359 }
360
361 fprintf(f, " ");
362 switch (ir->op)
363 {
364 case ir_tex:
365 case ir_lod:
366 case ir_query_levels:
367 case ir_texture_samples:
368 break;
369 case ir_txb:
370 ir->lod_info.bias->accept(this);
371 break;
372 case ir_txl:
373 case ir_txf:
374 case ir_txs:
375 ir->lod_info.lod->accept(this);
376 break;
377 case ir_txf_ms:
378 ir->lod_info.sample_index->accept(this);
379 break;
380 case ir_txd:
381 fprintf(f, "(");
382 ir->lod_info.grad.dPdx->accept(this);
383 fprintf(f, " ");
384 ir->lod_info.grad.dPdy->accept(this);
385 fprintf(f, ")");
386 break;
387 case ir_tg4:
388 ir->lod_info.component->accept(this);
389 break;
390 case ir_samples_identical:
391 unreachable("ir_samples_identical was already handled");
392 };
393 fprintf(f, ")");
394 }
395
396
visit(ir_swizzle * ir)397 void ir_print_visitor::visit(ir_swizzle *ir)
398 {
399 const unsigned swiz[4] = {
400 ir->mask.x,
401 ir->mask.y,
402 ir->mask.z,
403 ir->mask.w,
404 };
405
406 fprintf(f, "(swiz ");
407 for (unsigned i = 0; i < ir->mask.num_components; i++) {
408 fprintf(f, "%c", "xyzw"[swiz[i]]);
409 }
410 fprintf(f, " ");
411 ir->val->accept(this);
412 fprintf(f, ")");
413 }
414
415
visit(ir_dereference_variable * ir)416 void ir_print_visitor::visit(ir_dereference_variable *ir)
417 {
418 ir_variable *var = ir->variable_referenced();
419 fprintf(f, "(var_ref %s) ", unique_name(var));
420 }
421
422
visit(ir_dereference_array * ir)423 void ir_print_visitor::visit(ir_dereference_array *ir)
424 {
425 fprintf(f, "(array_ref ");
426 ir->array->accept(this);
427 ir->array_index->accept(this);
428 fprintf(f, ") ");
429 }
430
431
visit(ir_dereference_record * ir)432 void ir_print_visitor::visit(ir_dereference_record *ir)
433 {
434 fprintf(f, "(record_ref ");
435 ir->record->accept(this);
436
437 const char *field_name =
438 ir->record->type->fields.structure[ir->field_idx].name;
439 fprintf(f, " %s) ", field_name);
440 }
441
442
visit(ir_assignment * ir)443 void ir_print_visitor::visit(ir_assignment *ir)
444 {
445 fprintf(f, "(assign ");
446
447 char mask[5];
448 unsigned j = 0;
449
450 for (unsigned i = 0; i < 4; i++) {
451 if ((ir->write_mask & (1 << i)) != 0) {
452 mask[j] = "xyzw"[i];
453 j++;
454 }
455 }
456 mask[j] = '\0';
457
458 fprintf(f, " (%s) ", mask);
459
460 ir->lhs->accept(this);
461
462 fprintf(f, " ");
463
464 ir->rhs->accept(this);
465 fprintf(f, ") ");
466 }
467
468 static void
print_float_constant(FILE * f,float val)469 print_float_constant(FILE *f, float val)
470 {
471 if (val == 0.0f)
472 /* 0.0 == -0.0, so print with %f to get the proper sign. */
473 fprintf(f, "%f", val);
474 else if (fabs(val) < 0.000001f)
475 fprintf(f, "%a", val);
476 else if (fabs(val) > 1000000.0f)
477 fprintf(f, "%e", val);
478 else
479 fprintf(f, "%f", val);
480 }
481
visit(ir_constant * ir)482 void ir_print_visitor::visit(ir_constant *ir)
483 {
484 fprintf(f, "(constant ");
485 glsl_print_type(f, ir->type);
486 fprintf(f, " (");
487
488 if (ir->type->is_array()) {
489 for (unsigned i = 0; i < ir->type->length; i++)
490 ir->get_array_element(i)->accept(this);
491 } else if (ir->type->is_struct()) {
492 for (unsigned i = 0; i < ir->type->length; i++) {
493 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
494 ir->get_record_field(i)->accept(this);
495 fprintf(f, ")");
496 }
497 } else {
498 for (unsigned i = 0; i < ir->type->components(); i++) {
499 if (i != 0)
500 fprintf(f, " ");
501 switch (ir->type->base_type) {
502 case GLSL_TYPE_UINT16:fprintf(f, "%u", ir->value.u16[i]); break;
503 case GLSL_TYPE_INT16: fprintf(f, "%d", ir->value.i16[i]); break;
504 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
505 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
506 case GLSL_TYPE_FLOAT:
507 print_float_constant(f, ir->value.f[i]);
508 break;
509 case GLSL_TYPE_FLOAT16:
510 print_float_constant(f, _mesa_half_to_float(ir->value.f16[i]));
511 break;
512 case GLSL_TYPE_SAMPLER:
513 case GLSL_TYPE_IMAGE:
514 case GLSL_TYPE_UINT64:
515 fprintf(f, "%" PRIu64, ir->value.u64[i]);
516 break;
517 case GLSL_TYPE_INT64: fprintf(f, "%" PRIi64, ir->value.i64[i]); break;
518 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
519 case GLSL_TYPE_DOUBLE:
520 if (ir->value.d[i] == 0.0)
521 /* 0.0 == -0.0, so print with %f to get the proper sign. */
522 fprintf(f, "%.1f", ir->value.d[i]);
523 else if (fabs(ir->value.d[i]) < 0.000001)
524 fprintf(f, "%a", ir->value.d[i]);
525 else if (fabs(ir->value.d[i]) > 1000000.0)
526 fprintf(f, "%e", ir->value.d[i]);
527 else
528 fprintf(f, "%f", ir->value.d[i]);
529 break;
530 default:
531 unreachable("Invalid constant type");
532 }
533 }
534 }
535 fprintf(f, ")) ");
536 }
537
538
539 void
visit(ir_call * ir)540 ir_print_visitor::visit(ir_call *ir)
541 {
542 fprintf(f, "(call %s ", ir->callee_name());
543 if (ir->return_deref)
544 ir->return_deref->accept(this);
545 fprintf(f, " (");
546 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
547 param->accept(this);
548 }
549 fprintf(f, "))\n");
550 }
551
552
553 void
visit(ir_return * ir)554 ir_print_visitor::visit(ir_return *ir)
555 {
556 fprintf(f, "(return");
557
558 ir_rvalue *const value = ir->get_value();
559 if (value) {
560 fprintf(f, " ");
561 value->accept(this);
562 }
563
564 fprintf(f, ")");
565 }
566
567
568 void
visit(ir_discard * ir)569 ir_print_visitor::visit(ir_discard *ir)
570 {
571 fprintf(f, "(discard ");
572
573 if (ir->condition != NULL) {
574 fprintf(f, " ");
575 ir->condition->accept(this);
576 }
577
578 fprintf(f, ")");
579 }
580
581
582 void
visit(ir_demote * ir)583 ir_print_visitor::visit(ir_demote *ir)
584 {
585 fprintf(f, "(demote)");
586 }
587
588
589 void
visit(ir_if * ir)590 ir_print_visitor::visit(ir_if *ir)
591 {
592 fprintf(f, "(if ");
593 ir->condition->accept(this);
594
595 fprintf(f, "(\n");
596 indentation++;
597
598 foreach_in_list(ir_instruction, inst, &ir->then_instructions) {
599 indent();
600 inst->accept(this);
601 fprintf(f, "\n");
602 }
603
604 indentation--;
605 indent();
606 fprintf(f, ")\n");
607
608 indent();
609 if (!ir->else_instructions.is_empty()) {
610 fprintf(f, "(\n");
611 indentation++;
612
613 foreach_in_list(ir_instruction, inst, &ir->else_instructions) {
614 indent();
615 inst->accept(this);
616 fprintf(f, "\n");
617 }
618 indentation--;
619 indent();
620 fprintf(f, "))\n");
621 } else {
622 fprintf(f, "())\n");
623 }
624 }
625
626
627 void
visit(ir_loop * ir)628 ir_print_visitor::visit(ir_loop *ir)
629 {
630 fprintf(f, "(loop (\n");
631 indentation++;
632
633 foreach_in_list(ir_instruction, inst, &ir->body_instructions) {
634 indent();
635 inst->accept(this);
636 fprintf(f, "\n");
637 }
638 indentation--;
639 indent();
640 fprintf(f, "))\n");
641 }
642
643
644 void
visit(ir_loop_jump * ir)645 ir_print_visitor::visit(ir_loop_jump *ir)
646 {
647 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
648 }
649
650 void
visit(ir_emit_vertex * ir)651 ir_print_visitor::visit(ir_emit_vertex *ir)
652 {
653 fprintf(f, "(emit-vertex ");
654 ir->stream->accept(this);
655 fprintf(f, ")\n");
656 }
657
658 void
visit(ir_end_primitive * ir)659 ir_print_visitor::visit(ir_end_primitive *ir)
660 {
661 fprintf(f, "(end-primitive ");
662 ir->stream->accept(this);
663 fprintf(f, ")\n");
664 }
665
666 void
visit(ir_barrier *)667 ir_print_visitor::visit(ir_barrier *)
668 {
669 fprintf(f, "(barrier)\n");
670 }
671