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"
31 #include "util/half_float.h"
33 static void print_type(FILE *f
, const glsl_type
*t
);
36 ir_instruction::print(void) const
42 ir_instruction::fprint(FILE *f
) const
44 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
46 ir_print_visitor
v(f
);
47 deconsted
->accept(&v
);
52 _mesa_print_ir(FILE *f
, exec_list
*instructions
,
53 struct _mesa_glsl_parse_state
*state
)
56 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
57 const glsl_type
*const s
= state
->user_structures
[i
];
59 fprintf(f
, "(structure (%s) (%s@%p) (%u) (\n",
60 s
->name
, s
->name
, (void *) s
, s
->length
);
62 for (unsigned j
= 0; j
< s
->length
; j
++) {
64 print_type(f
, s
->fields
.structure
[j
].type
);
65 fprintf(f
, ")(%s))\n", s
->fields
.structure
[j
].name
);
73 foreach_in_list(ir_instruction
, ir
, instructions
) {
75 if (ir
->ir_type
!= ir_type_function
)
82 fprint_ir(FILE *f
, const void *instruction
)
84 const ir_instruction
*ir
= (const ir_instruction
*)instruction
;
90 ir_print_visitor::ir_print_visitor(FILE *f
)
94 printable_names
= _mesa_pointer_hash_table_create(NULL
);
95 symbols
= _mesa_symbol_table_ctor();
96 mem_ctx
= ralloc_context(NULL
);
99 ir_print_visitor::~ir_print_visitor()
101 _mesa_hash_table_destroy(printable_names
, NULL
);
102 _mesa_symbol_table_dtor(symbols
);
103 ralloc_free(mem_ctx
);
106 void ir_print_visitor::indent(void)
108 for (int i
= 0; i
< indentation
; i
++)
113 ir_print_visitor::unique_name(ir_variable
*var
)
115 /* var->name can be NULL in function prototypes when a type is given for a
116 * parameter but no name is given. In that case, just return an empty
117 * string. Don't worry about tracking the generated name in the printable
118 * names hash because this is the only scope where it can ever appear.
120 if (var
->name
== NULL
) {
121 static unsigned arg
= 1;
122 return ralloc_asprintf(this->mem_ctx
, "parameter@%u", arg
++);
125 /* Do we already have a name for this variable? */
126 struct hash_entry
* entry
=
127 _mesa_hash_table_search(this->printable_names
, var
);
130 return (const char *) entry
->data
;
133 /* If there's no conflict, just use the original name */
134 const char* name
= NULL
;
135 if (_mesa_symbol_table_find_symbol(this->symbols
, var
->name
) == NULL
) {
138 static unsigned i
= 1;
139 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
141 _mesa_hash_table_insert(this->printable_names
, var
, (void *) name
);
142 _mesa_symbol_table_add_symbol(this->symbols
, name
, var
);
147 print_type(FILE *f
, const glsl_type
*t
)
150 fprintf(f
, "(array ");
151 print_type(f
, t
->fields
.array
);
152 fprintf(f
, " %u)", t
->length
);
153 } else if (t
->is_struct() && !is_gl_identifier(t
->name
)) {
154 fprintf(f
, "%s@%p", t
->name
, (void *) t
);
156 fprintf(f
, "%s", t
->name
);
160 void ir_print_visitor::visit(ir_rvalue
*)
165 void ir_print_visitor::visit(ir_variable
*ir
)
167 fprintf(f
, "(declare ");
169 char binding
[32] = {0};
170 if (ir
->data
.binding
)
171 snprintf(binding
, sizeof(binding
), "binding=%i ", ir
->data
.binding
);
174 if (ir
->data
.location
!= -1)
175 snprintf(loc
, sizeof(loc
), "location=%i ", ir
->data
.location
);
177 char component
[32] = {0};
178 if (ir
->data
.explicit_component
|| ir
->data
.location_frac
!= 0)
179 snprintf(component
, sizeof(component
), "component=%i ",
180 ir
->data
.location_frac
);
182 char stream
[32] = {0};
183 if (ir
->data
.stream
& (1u << 31)) {
184 if (ir
->data
.stream
& ~(1u << 31)) {
185 snprintf(stream
, sizeof(stream
), "stream(%u,%u,%u,%u) ",
186 ir
->data
.stream
& 3, (ir
->data
.stream
>> 2) & 3,
187 (ir
->data
.stream
>> 4) & 3, (ir
->data
.stream
>> 6) & 3);
189 } else if (ir
->data
.stream
) {
190 snprintf(stream
, sizeof(stream
), "stream%u ", ir
->data
.stream
);
193 char image_format
[32] = {0};
194 if (ir
->data
.image_format
) {
195 snprintf(image_format
, sizeof(image_format
), "format=%x ",
196 ir
->data
.image_format
);
199 const char *const cent
= (ir
->data
.centroid
) ? "centroid " : "";
200 const char *const samp
= (ir
->data
.sample
) ? "sample " : "";
201 const char *const patc
= (ir
->data
.patch
) ? "patch " : "";
202 const char *const inv
= (ir
->data
.invariant
) ? "invariant " : "";
203 const char *const explicit_inv
= (ir
->data
.explicit_invariant
) ? "explicit_invariant " : "";
204 const char *const prec
= (ir
->data
.precise
) ? "precise " : "";
205 const char *const bindless
= (ir
->data
.bindless
) ? "bindless " : "";
206 const char *const bound
= (ir
->data
.bound
) ? "bound " : "";
207 const char *const memory_read_only
= (ir
->data
.memory_read_only
) ? "readonly " : "";
208 const char *const memory_write_only
= (ir
->data
.memory_write_only
) ? "writeonly " : "";
209 const char *const memory_coherent
= (ir
->data
.memory_coherent
) ? "coherent " : "";
210 const char *const memory_volatile
= (ir
->data
.memory_volatile
) ? "volatile " : "";
211 const char *const memory_restrict
= (ir
->data
.memory_restrict
) ? "restrict " : "";
212 const char *const mode
[] = { "", "uniform ", "shader_storage ",
213 "shader_shared ", "shader_in ", "shader_out ",
214 "in ", "out ", "inout ",
215 "const_in ", "sys ", "temporary " };
216 STATIC_ASSERT(ARRAY_SIZE(mode
) == ir_var_mode_count
);
217 const char *const interp
[] = { "", "smooth", "flat", "noperspective", "explicit" };
218 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_MODE_COUNT
);
220 fprintf(f
, "(%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s) ",
221 binding
, loc
, component
, cent
, bindless
, bound
,
222 image_format
, memory_read_only
, memory_write_only
,
223 memory_coherent
, memory_volatile
, memory_restrict
,
224 samp
, patc
, inv
, explicit_inv
, prec
, mode
[ir
->data
.mode
],
226 interp
[ir
->data
.interpolation
]);
228 print_type(f
, ir
->type
);
229 fprintf(f
, " %s)", unique_name(ir
));
233 void ir_print_visitor::visit(ir_function_signature
*ir
)
235 _mesa_symbol_table_push_scope(symbols
);
236 fprintf(f
, "(signature ");
239 print_type(f
, ir
->return_type
);
243 fprintf(f
, "(parameters\n");
246 foreach_in_list(ir_variable
, inst
, &ir
->parameters
) {
261 foreach_in_list(ir_instruction
, inst
, &ir
->body
) {
270 _mesa_symbol_table_pop_scope(symbols
);
274 void ir_print_visitor::visit(ir_function
*ir
)
276 fprintf(f
, "(%s function %s\n", ir
->is_subroutine
? "subroutine" : "", ir
->name
);
278 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
289 void ir_print_visitor::visit(ir_expression
*ir
)
291 fprintf(f
, "(expression ");
293 print_type(f
, ir
->type
);
295 fprintf(f
, " %s ", ir_expression_operation_strings
[ir
->operation
]);
297 for (unsigned i
= 0; i
< ir
->num_operands
; i
++) {
298 ir
->operands
[i
]->accept(this);
305 void ir_print_visitor::visit(ir_texture
*ir
)
307 fprintf(f
, "(%s ", ir
->opcode_string());
309 if (ir
->op
== ir_samples_identical
) {
310 ir
->sampler
->accept(this);
312 ir
->coordinate
->accept(this);
317 print_type(f
, ir
->type
);
320 ir
->sampler
->accept(this);
323 if (ir
->op
!= ir_txs
&& ir
->op
!= ir_query_levels
&&
324 ir
->op
!= ir_texture_samples
) {
325 ir
->coordinate
->accept(this);
329 if (ir
->offset
!= NULL
) {
330 ir
->offset
->accept(this);
338 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&&
339 ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
&&
340 ir
->op
!= ir_query_levels
&& ir
->op
!= ir_texture_samples
) {
342 ir
->projector
->accept(this);
346 if (ir
->shadow_comparator
) {
348 ir
->shadow_comparator
->accept(this);
359 case ir_query_levels
:
360 case ir_texture_samples
:
363 ir
->lod_info
.bias
->accept(this);
368 ir
->lod_info
.lod
->accept(this);
371 ir
->lod_info
.sample_index
->accept(this);
375 ir
->lod_info
.grad
.dPdx
->accept(this);
377 ir
->lod_info
.grad
.dPdy
->accept(this);
381 ir
->lod_info
.component
->accept(this);
383 case ir_samples_identical
:
384 unreachable("ir_samples_identical was already handled");
390 void ir_print_visitor::visit(ir_swizzle
*ir
)
392 const unsigned swiz
[4] = {
399 fprintf(f
, "(swiz ");
400 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
401 fprintf(f
, "%c", "xyzw"[swiz
[i
]]);
404 ir
->val
->accept(this);
409 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
411 ir_variable
*var
= ir
->variable_referenced();
412 fprintf(f
, "(var_ref %s) ", unique_name(var
));
416 void ir_print_visitor::visit(ir_dereference_array
*ir
)
418 fprintf(f
, "(array_ref ");
419 ir
->array
->accept(this);
420 ir
->array_index
->accept(this);
425 void ir_print_visitor::visit(ir_dereference_record
*ir
)
427 fprintf(f
, "(record_ref ");
428 ir
->record
->accept(this);
430 const char *field_name
=
431 ir
->record
->type
->fields
.structure
[ir
->field_idx
].name
;
432 fprintf(f
, " %s) ", field_name
);
436 void ir_print_visitor::visit(ir_assignment
*ir
)
438 fprintf(f
, "(assign ");
441 ir
->condition
->accept(this);
446 for (unsigned i
= 0; i
< 4; i
++) {
447 if ((ir
->write_mask
& (1 << i
)) != 0) {
454 fprintf(f
, " (%s) ", mask
);
456 ir
->lhs
->accept(this);
460 ir
->rhs
->accept(this);
465 print_float_constant(FILE *f
, float val
)
468 /* 0.0 == -0.0, so print with %f to get the proper sign. */
469 fprintf(f
, "%f", val
);
470 else if (fabs(val
) < 0.000001f
)
471 fprintf(f
, "%a", val
);
472 else if (fabs(val
) > 1000000.0f
)
473 fprintf(f
, "%e", val
);
475 fprintf(f
, "%f", val
);
478 void ir_print_visitor::visit(ir_constant
*ir
)
480 fprintf(f
, "(constant ");
481 print_type(f
, ir
->type
);
484 if (ir
->type
->is_array()) {
485 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
486 ir
->get_array_element(i
)->accept(this);
487 } else if (ir
->type
->is_struct()) {
488 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
489 fprintf(f
, "(%s ", ir
->type
->fields
.structure
[i
].name
);
490 ir
->get_record_field(i
)->accept(this);
494 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
497 switch (ir
->type
->base_type
) {
498 case GLSL_TYPE_UINT16
:fprintf(f
, "%u", ir
->value
.u16
[i
]); break;
499 case GLSL_TYPE_INT16
: fprintf(f
, "%d", ir
->value
.i16
[i
]); break;
500 case GLSL_TYPE_UINT
: fprintf(f
, "%u", ir
->value
.u
[i
]); break;
501 case GLSL_TYPE_INT
: fprintf(f
, "%d", ir
->value
.i
[i
]); break;
502 case GLSL_TYPE_FLOAT
:
503 print_float_constant(f
, ir
->value
.f
[i
]);
505 case GLSL_TYPE_FLOAT16
:
506 print_float_constant(f
, _mesa_half_to_float(ir
->value
.f16
[i
]));
508 case GLSL_TYPE_SAMPLER
:
509 case GLSL_TYPE_IMAGE
:
510 case GLSL_TYPE_UINT64
:
511 fprintf(f
, "%" PRIu64
, ir
->value
.u64
[i
]);
513 case GLSL_TYPE_INT64
: fprintf(f
, "%" PRIi64
, ir
->value
.i64
[i
]); break;
514 case GLSL_TYPE_BOOL
: fprintf(f
, "%d", ir
->value
.b
[i
]); break;
515 case GLSL_TYPE_DOUBLE
:
516 if (ir
->value
.d
[i
] == 0.0)
517 /* 0.0 == -0.0, so print with %f to get the proper sign. */
518 fprintf(f
, "%.1f", ir
->value
.d
[i
]);
519 else if (fabs(ir
->value
.d
[i
]) < 0.000001)
520 fprintf(f
, "%a", ir
->value
.d
[i
]);
521 else if (fabs(ir
->value
.d
[i
]) > 1000000.0)
522 fprintf(f
, "%e", ir
->value
.d
[i
]);
524 fprintf(f
, "%f", ir
->value
.d
[i
]);
527 unreachable("Invalid constant type");
536 ir_print_visitor::visit(ir_call
*ir
)
538 fprintf(f
, "(call %s ", ir
->callee_name());
539 if (ir
->return_deref
)
540 ir
->return_deref
->accept(this);
542 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
550 ir_print_visitor::visit(ir_return
*ir
)
552 fprintf(f
, "(return");
554 ir_rvalue
*const value
= ir
->get_value();
565 ir_print_visitor::visit(ir_discard
*ir
)
567 fprintf(f
, "(discard ");
569 if (ir
->condition
!= NULL
) {
571 ir
->condition
->accept(this);
579 ir_print_visitor::visit(ir_demote
*ir
)
581 fprintf(f
, "(demote)");
586 ir_print_visitor::visit(ir_if
*ir
)
589 ir
->condition
->accept(this);
594 foreach_in_list(ir_instruction
, inst
, &ir
->then_instructions
) {
605 if (!ir
->else_instructions
.is_empty()) {
609 foreach_in_list(ir_instruction
, inst
, &ir
->else_instructions
) {
624 ir_print_visitor::visit(ir_loop
*ir
)
626 fprintf(f
, "(loop (\n");
629 foreach_in_list(ir_instruction
, inst
, &ir
->body_instructions
) {
641 ir_print_visitor::visit(ir_loop_jump
*ir
)
643 fprintf(f
, "%s", ir
->is_break() ? "break" : "continue");
647 ir_print_visitor::visit(ir_emit_vertex
*ir
)
649 fprintf(f
, "(emit-vertex ");
650 ir
->stream
->accept(this);
655 ir_print_visitor::visit(ir_end_primitive
*ir
)
657 fprintf(f
, "(end-primitive ");
658 ir
->stream
->accept(this);
663 ir_print_visitor::visit(ir_barrier
*)
665 fprintf(f
, "(barrier)\n");