2 * Copyright © 2010 Intel Corporation
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:
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
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.
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"
32 static void print_type(FILE *f
, const glsl_type
*t
);
35 ir_instruction::print(void) const
41 ir_instruction::fprint(FILE *f
) const
43 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
45 ir_print_visitor
v(f
);
46 deconsted
->accept(&v
);
51 _mesa_print_ir(FILE *f
, exec_list
*instructions
,
52 struct _mesa_glsl_parse_state
*state
)
55 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
56 const glsl_type
*const s
= state
->user_structures
[i
];
58 fprintf(f
, "(structure (%s) (%s@%p) (%u) (\n",
59 s
->name
, s
->name
, (void *) s
, s
->length
);
61 for (unsigned j
= 0; j
< s
->length
; j
++) {
63 print_type(f
, s
->fields
.structure
[j
].type
);
64 fprintf(f
, ")(%s))\n", s
->fields
.structure
[j
].name
);
72 foreach_in_list(ir_instruction
, ir
, instructions
) {
74 if (ir
->ir_type
!= ir_type_function
)
81 fprint_ir(FILE *f
, const void *instruction
)
83 const ir_instruction
*ir
= (const ir_instruction
*)instruction
;
89 ir_print_visitor::ir_print_visitor(FILE *f
)
93 printable_names
= _mesa_pointer_hash_table_create(NULL
);
94 symbols
= _mesa_symbol_table_ctor();
95 mem_ctx
= ralloc_context(NULL
);
98 ir_print_visitor::~ir_print_visitor()
100 _mesa_hash_table_destroy(printable_names
, NULL
);
101 _mesa_symbol_table_dtor(symbols
);
102 ralloc_free(mem_ctx
);
105 void ir_print_visitor::indent(void)
107 for (int i
= 0; i
< indentation
; i
++)
112 ir_print_visitor::unique_name(ir_variable
*var
)
114 /* var->name can be NULL in function prototypes when a type is given for a
115 * parameter but no name is given. In that case, just return an empty
116 * string. Don't worry about tracking the generated name in the printable
117 * names hash because this is the only scope where it can ever appear.
119 if (var
->name
== NULL
) {
120 static unsigned arg
= 1;
121 return ralloc_asprintf(this->mem_ctx
, "parameter@%u", arg
++);
124 /* Do we already have a name for this variable? */
125 struct hash_entry
* entry
=
126 _mesa_hash_table_search(this->printable_names
, var
);
129 return (const char *) entry
->data
;
132 /* If there's no conflict, just use the original name */
133 const char* name
= NULL
;
134 if (_mesa_symbol_table_find_symbol(this->symbols
, var
->name
) == NULL
) {
137 static unsigned i
= 1;
138 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
140 _mesa_hash_table_insert(this->printable_names
, var
, (void *) name
);
141 _mesa_symbol_table_add_symbol(this->symbols
, name
, var
);
146 print_type(FILE *f
, const glsl_type
*t
)
149 fprintf(f
, "(array ");
150 print_type(f
, t
->fields
.array
);
151 fprintf(f
, " %u)", t
->length
);
152 } else if (t
->is_struct() && !is_gl_identifier(t
->name
)) {
153 fprintf(f
, "%s@%p", t
->name
, (void *) t
);
155 fprintf(f
, "%s", t
->name
);
159 void ir_print_visitor::visit(ir_rvalue
*)
164 void ir_print_visitor::visit(ir_variable
*ir
)
166 fprintf(f
, "(declare ");
168 char binding
[32] = {0};
169 if (ir
->data
.binding
)
170 snprintf(binding
, sizeof(binding
), "binding=%i ", ir
->data
.binding
);
173 if (ir
->data
.location
!= -1)
174 snprintf(loc
, sizeof(loc
), "location=%i ", ir
->data
.location
);
176 char component
[32] = {0};
177 if (ir
->data
.explicit_component
|| ir
->data
.location_frac
!= 0)
178 snprintf(component
, sizeof(component
), "component=%i ",
179 ir
->data
.location_frac
);
181 char stream
[32] = {0};
182 if (ir
->data
.stream
& (1u << 31)) {
183 if (ir
->data
.stream
& ~(1u << 31)) {
184 snprintf(stream
, sizeof(stream
), "stream(%u,%u,%u,%u) ",
185 ir
->data
.stream
& 3, (ir
->data
.stream
>> 2) & 3,
186 (ir
->data
.stream
>> 4) & 3, (ir
->data
.stream
>> 6) & 3);
188 } else if (ir
->data
.stream
) {
189 snprintf(stream
, sizeof(stream
), "stream%u ", ir
->data
.stream
);
192 char image_format
[32] = {0};
193 if (ir
->data
.image_format
) {
194 snprintf(image_format
, sizeof(image_format
), "format=%x ",
195 ir
->data
.image_format
);
198 const char *const cent
= (ir
->data
.centroid
) ? "centroid " : "";
199 const char *const samp
= (ir
->data
.sample
) ? "sample " : "";
200 const char *const patc
= (ir
->data
.patch
) ? "patch " : "";
201 const char *const inv
= (ir
->data
.invariant
) ? "invariant " : "";
202 const char *const explicit_inv
= (ir
->data
.explicit_invariant
) ? "explicit_invariant " : "";
203 const char *const prec
= (ir
->data
.precise
) ? "precise " : "";
204 const char *const bindless
= (ir
->data
.bindless
) ? "bindless " : "";
205 const char *const bound
= (ir
->data
.bound
) ? "bound " : "";
206 const char *const memory_read_only
= (ir
->data
.memory_read_only
) ? "readonly " : "";
207 const char *const memory_write_only
= (ir
->data
.memory_write_only
) ? "writeonly " : "";
208 const char *const memory_coherent
= (ir
->data
.memory_coherent
) ? "coherent " : "";
209 const char *const memory_volatile
= (ir
->data
.memory_volatile
) ? "volatile " : "";
210 const char *const memory_restrict
= (ir
->data
.memory_restrict
) ? "restrict " : "";
211 const char *const mode
[] = { "", "uniform ", "shader_storage ",
212 "shader_shared ", "shader_in ", "shader_out ",
213 "in ", "out ", "inout ",
214 "const_in ", "sys ", "temporary " };
215 STATIC_ASSERT(ARRAY_SIZE(mode
) == ir_var_mode_count
);
216 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
217 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_MODE_COUNT
);
219 fprintf(f
, "(%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s) ",
220 binding
, loc
, component
, cent
, bindless
, bound
,
221 image_format
, memory_read_only
, memory_write_only
,
222 memory_coherent
, memory_volatile
, memory_restrict
,
223 samp
, patc
, inv
, explicit_inv
, prec
, mode
[ir
->data
.mode
],
225 interp
[ir
->data
.interpolation
]);
227 print_type(f
, ir
->type
);
228 fprintf(f
, " %s)", unique_name(ir
));
232 void ir_print_visitor::visit(ir_function_signature
*ir
)
234 _mesa_symbol_table_push_scope(symbols
);
235 fprintf(f
, "(signature ");
238 print_type(f
, ir
->return_type
);
242 fprintf(f
, "(parameters\n");
245 foreach_in_list(ir_variable
, inst
, &ir
->parameters
) {
260 foreach_in_list(ir_instruction
, inst
, &ir
->body
) {
269 _mesa_symbol_table_pop_scope(symbols
);
273 void ir_print_visitor::visit(ir_function
*ir
)
275 fprintf(f
, "(%s function %s\n", ir
->is_subroutine
? "subroutine" : "", ir
->name
);
277 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
288 void ir_print_visitor::visit(ir_expression
*ir
)
290 fprintf(f
, "(expression ");
292 print_type(f
, ir
->type
);
294 fprintf(f
, " %s ", ir_expression_operation_strings
[ir
->operation
]);
296 for (unsigned i
= 0; i
< ir
->num_operands
; i
++) {
297 ir
->operands
[i
]->accept(this);
304 void ir_print_visitor::visit(ir_texture
*ir
)
306 fprintf(f
, "(%s ", ir
->opcode_string());
308 if (ir
->op
== ir_samples_identical
) {
309 ir
->sampler
->accept(this);
311 ir
->coordinate
->accept(this);
316 print_type(f
, ir
->type
);
319 ir
->sampler
->accept(this);
322 if (ir
->op
!= ir_txs
&& ir
->op
!= ir_query_levels
&&
323 ir
->op
!= ir_texture_samples
) {
324 ir
->coordinate
->accept(this);
328 if (ir
->offset
!= NULL
) {
329 ir
->offset
->accept(this);
337 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&&
338 ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
&&
339 ir
->op
!= ir_query_levels
&& ir
->op
!= ir_texture_samples
) {
341 ir
->projector
->accept(this);
345 if (ir
->shadow_comparator
) {
347 ir
->shadow_comparator
->accept(this);
358 case ir_query_levels
:
359 case ir_texture_samples
:
362 ir
->lod_info
.bias
->accept(this);
367 ir
->lod_info
.lod
->accept(this);
370 ir
->lod_info
.sample_index
->accept(this);
374 ir
->lod_info
.grad
.dPdx
->accept(this);
376 ir
->lod_info
.grad
.dPdy
->accept(this);
380 ir
->lod_info
.component
->accept(this);
382 case ir_samples_identical
:
383 unreachable("ir_samples_identical was already handled");
389 void ir_print_visitor::visit(ir_swizzle
*ir
)
391 const unsigned swiz
[4] = {
398 fprintf(f
, "(swiz ");
399 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
400 fprintf(f
, "%c", "xyzw"[swiz
[i
]]);
403 ir
->val
->accept(this);
408 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
410 ir_variable
*var
= ir
->variable_referenced();
411 fprintf(f
, "(var_ref %s) ", unique_name(var
));
415 void ir_print_visitor::visit(ir_dereference_array
*ir
)
417 fprintf(f
, "(array_ref ");
418 ir
->array
->accept(this);
419 ir
->array_index
->accept(this);
424 void ir_print_visitor::visit(ir_dereference_record
*ir
)
426 fprintf(f
, "(record_ref ");
427 ir
->record
->accept(this);
429 const char *field_name
=
430 ir
->record
->type
->fields
.structure
[ir
->field_idx
].name
;
431 fprintf(f
, " %s) ", field_name
);
435 void ir_print_visitor::visit(ir_assignment
*ir
)
437 fprintf(f
, "(assign ");
440 ir
->condition
->accept(this);
445 for (unsigned i
= 0; i
< 4; i
++) {
446 if ((ir
->write_mask
& (1 << i
)) != 0) {
453 fprintf(f
, " (%s) ", mask
);
455 ir
->lhs
->accept(this);
459 ir
->rhs
->accept(this);
464 void ir_print_visitor::visit(ir_constant
*ir
)
466 fprintf(f
, "(constant ");
467 print_type(f
, ir
->type
);
470 if (ir
->type
->is_array()) {
471 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
472 ir
->get_array_element(i
)->accept(this);
473 } else if (ir
->type
->is_struct()) {
474 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
475 fprintf(f
, "(%s ", ir
->type
->fields
.structure
[i
].name
);
476 ir
->get_record_field(i
)->accept(this);
480 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
483 switch (ir
->type
->base_type
) {
484 case GLSL_TYPE_UINT
: fprintf(f
, "%u", ir
->value
.u
[i
]); break;
485 case GLSL_TYPE_INT
: fprintf(f
, "%d", ir
->value
.i
[i
]); break;
486 case GLSL_TYPE_FLOAT
:
487 if (ir
->value
.f
[i
] == 0.0f
)
488 /* 0.0 == -0.0, so print with %f to get the proper sign. */
489 fprintf(f
, "%f", ir
->value
.f
[i
]);
490 else if (fabs(ir
->value
.f
[i
]) < 0.000001f
)
491 fprintf(f
, "%a", ir
->value
.f
[i
]);
492 else if (fabs(ir
->value
.f
[i
]) > 1000000.0f
)
493 fprintf(f
, "%e", ir
->value
.f
[i
]);
495 fprintf(f
, "%f", ir
->value
.f
[i
]);
497 case GLSL_TYPE_SAMPLER
:
498 case GLSL_TYPE_IMAGE
:
499 case GLSL_TYPE_UINT64
:
500 fprintf(f
, "%" PRIu64
, ir
->value
.u64
[i
]);
502 case GLSL_TYPE_INT64
: fprintf(f
, "%" PRIi64
, ir
->value
.i64
[i
]); break;
503 case GLSL_TYPE_BOOL
: fprintf(f
, "%d", ir
->value
.b
[i
]); break;
504 case GLSL_TYPE_DOUBLE
:
505 if (ir
->value
.d
[i
] == 0.0)
506 /* 0.0 == -0.0, so print with %f to get the proper sign. */
507 fprintf(f
, "%.1f", ir
->value
.d
[i
]);
508 else if (fabs(ir
->value
.d
[i
]) < 0.000001)
509 fprintf(f
, "%a", ir
->value
.d
[i
]);
510 else if (fabs(ir
->value
.d
[i
]) > 1000000.0)
511 fprintf(f
, "%e", ir
->value
.d
[i
]);
513 fprintf(f
, "%f", ir
->value
.d
[i
]);
516 unreachable("Invalid constant type");
525 ir_print_visitor::visit(ir_call
*ir
)
527 fprintf(f
, "(call %s ", ir
->callee_name());
528 if (ir
->return_deref
)
529 ir
->return_deref
->accept(this);
531 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
539 ir_print_visitor::visit(ir_return
*ir
)
541 fprintf(f
, "(return");
543 ir_rvalue
*const value
= ir
->get_value();
554 ir_print_visitor::visit(ir_discard
*ir
)
556 fprintf(f
, "(discard ");
558 if (ir
->condition
!= NULL
) {
560 ir
->condition
->accept(this);
568 ir_print_visitor::visit(ir_demote
*ir
)
570 fprintf(f
, "(demote)");
575 ir_print_visitor::visit(ir_if
*ir
)
578 ir
->condition
->accept(this);
583 foreach_in_list(ir_instruction
, inst
, &ir
->then_instructions
) {
594 if (!ir
->else_instructions
.is_empty()) {
598 foreach_in_list(ir_instruction
, inst
, &ir
->else_instructions
) {
613 ir_print_visitor::visit(ir_loop
*ir
)
615 fprintf(f
, "(loop (\n");
618 foreach_in_list(ir_instruction
, inst
, &ir
->body_instructions
) {
630 ir_print_visitor::visit(ir_loop_jump
*ir
)
632 fprintf(f
, "%s", ir
->is_break() ? "break" : "continue");
636 ir_print_visitor::visit(ir_emit_vertex
*ir
)
638 fprintf(f
, "(emit-vertex ");
639 ir
->stream
->accept(this);
644 ir_print_visitor::visit(ir_end_primitive
*ir
)
646 fprintf(f
, "(end-primitive ");
647 ir
->stream
->accept(this);
652 ir_print_visitor::visit(ir_barrier
*)
654 fprintf(f
, "(barrier)\n");