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"
34 ir_instruction::print(void) const
40 ir_instruction::fprint(FILE *f
) const
42 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
44 ir_print_visitor
v(f
);
45 deconsted
->accept(&v
);
50 _mesa_print_ir(FILE *f
, exec_list
*instructions
,
51 struct _mesa_glsl_parse_state
*state
)
54 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
55 const glsl_type
*const s
= state
->user_structures
[i
];
57 fprintf(f
, "(structure (%s) (%s@%p) (%u) (\n",
58 s
->name
, s
->name
, (void *) s
, s
->length
);
60 for (unsigned j
= 0; j
< s
->length
; j
++) {
62 glsl_print_type(f
, s
->fields
.structure
[j
].type
);
63 fprintf(f
, ")(%s))\n", s
->fields
.structure
[j
].name
);
71 foreach_in_list(ir_instruction
, ir
, instructions
) {
73 if (ir
->ir_type
!= ir_type_function
)
80 fprint_ir(FILE *f
, const void *instruction
)
82 const ir_instruction
*ir
= (const ir_instruction
*)instruction
;
88 ir_print_visitor::ir_print_visitor(FILE *f
)
92 printable_names
= _mesa_pointer_hash_table_create(NULL
);
93 symbols
= _mesa_symbol_table_ctor();
94 mem_ctx
= ralloc_context(NULL
);
97 ir_print_visitor::~ir_print_visitor()
99 _mesa_hash_table_destroy(printable_names
, NULL
);
100 _mesa_symbol_table_dtor(symbols
);
101 ralloc_free(mem_ctx
);
104 void ir_print_visitor::indent(void)
106 for (int i
= 0; i
< indentation
; i
++)
111 ir_print_visitor::unique_name(ir_variable
*var
)
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.
118 if (var
->name
== NULL
) {
119 static unsigned arg
= 1;
120 return ralloc_asprintf(this->mem_ctx
, "parameter@%u", arg
++);
123 /* Do we already have a name for this variable? */
124 struct hash_entry
* entry
=
125 _mesa_hash_table_search(this->printable_names
, var
);
128 return (const char *) entry
->data
;
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
) {
136 static unsigned i
= 1;
137 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
139 _mesa_hash_table_insert(this->printable_names
, var
, (void *) name
);
140 _mesa_symbol_table_add_symbol(this->symbols
, name
, var
);
144 void ir_print_visitor::visit(ir_rvalue
*)
149 void ir_print_visitor::visit(ir_variable
*ir
)
151 fprintf(f
, "(declare ");
153 char binding
[32] = {0};
154 if (ir
->data
.binding
)
155 snprintf(binding
, sizeof(binding
), "binding=%i ", ir
->data
.binding
);
158 if (ir
->data
.location
!= -1)
159 snprintf(loc
, sizeof(loc
), "location=%i ", ir
->data
.location
);
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
);
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);
173 } else if (ir
->data
.stream
) {
174 snprintf(stream
, sizeof(stream
), "stream%u ", ir
->data
.stream
);
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
);
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" };
202 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_MODE_COUNT
);
203 const char *const precision
[] = { "", "highp ", "mediump ", "lowp "};
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
],
211 interp
[ir
->data
.interpolation
], precision
[ir
->data
.precision
]);
213 glsl_print_type(f
, ir
->type
);
214 fprintf(f
, " %s)", unique_name(ir
));
216 if (ir
->constant_initializer
) {
218 visit(ir
->constant_initializer
);
221 if (ir
->constant_value
) {
223 visit(ir
->constant_value
);
228 void ir_print_visitor::visit(ir_function_signature
*ir
)
230 _mesa_symbol_table_push_scope(symbols
);
231 fprintf(f
, "(signature ");
234 glsl_print_type(f
, ir
->return_type
);
238 fprintf(f
, "(parameters\n");
241 foreach_in_list(ir_variable
, inst
, &ir
->parameters
) {
256 foreach_in_list(ir_instruction
, inst
, &ir
->body
) {
265 _mesa_symbol_table_pop_scope(symbols
);
269 void ir_print_visitor::visit(ir_function
*ir
)
271 fprintf(f
, "(%s function %s\n", ir
->is_subroutine
? "subroutine" : "", ir
->name
);
273 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
284 void ir_print_visitor::visit(ir_expression
*ir
)
286 fprintf(f
, "(expression ");
288 glsl_print_type(f
, ir
->type
);
290 fprintf(f
, " %s ", ir_expression_operation_strings
[ir
->operation
]);
292 for (unsigned i
= 0; i
< ir
->num_operands
; i
++) {
293 ir
->operands
[i
]->accept(this);
300 void ir_print_visitor::visit(ir_texture
*ir
)
302 fprintf(f
, "(%s ", ir
->opcode_string());
304 if (ir
->op
== ir_samples_identical
) {
305 ir
->sampler
->accept(this);
307 ir
->coordinate
->accept(this);
312 glsl_print_type(f
, ir
->type
);
315 ir
->sampler
->accept(this);
318 if (ir
->op
!= ir_txs
&& ir
->op
!= ir_query_levels
&&
319 ir
->op
!= ir_texture_samples
) {
320 ir
->coordinate
->accept(this);
324 if (ir
->offset
!= NULL
) {
325 ir
->offset
->accept(this);
333 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&&
334 ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
&&
335 ir
->op
!= ir_query_levels
&& ir
->op
!= ir_texture_samples
) {
337 ir
->projector
->accept(this);
341 if (ir
->shadow_comparator
) {
343 ir
->shadow_comparator
->accept(this);
354 case ir_query_levels
:
355 case ir_texture_samples
:
358 ir
->lod_info
.bias
->accept(this);
363 ir
->lod_info
.lod
->accept(this);
366 ir
->lod_info
.sample_index
->accept(this);
370 ir
->lod_info
.grad
.dPdx
->accept(this);
372 ir
->lod_info
.grad
.dPdy
->accept(this);
376 ir
->lod_info
.component
->accept(this);
378 case ir_samples_identical
:
379 unreachable("ir_samples_identical was already handled");
385 void ir_print_visitor::visit(ir_swizzle
*ir
)
387 const unsigned swiz
[4] = {
394 fprintf(f
, "(swiz ");
395 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
396 fprintf(f
, "%c", "xyzw"[swiz
[i
]]);
399 ir
->val
->accept(this);
404 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
406 ir_variable
*var
= ir
->variable_referenced();
407 fprintf(f
, "(var_ref %s) ", unique_name(var
));
411 void ir_print_visitor::visit(ir_dereference_array
*ir
)
413 fprintf(f
, "(array_ref ");
414 ir
->array
->accept(this);
415 ir
->array_index
->accept(this);
420 void ir_print_visitor::visit(ir_dereference_record
*ir
)
422 fprintf(f
, "(record_ref ");
423 ir
->record
->accept(this);
425 const char *field_name
=
426 ir
->record
->type
->fields
.structure
[ir
->field_idx
].name
;
427 fprintf(f
, " %s) ", field_name
);
431 void ir_print_visitor::visit(ir_assignment
*ir
)
433 fprintf(f
, "(assign ");
436 ir
->condition
->accept(this);
441 for (unsigned i
= 0; i
< 4; i
++) {
442 if ((ir
->write_mask
& (1 << i
)) != 0) {
449 fprintf(f
, " (%s) ", mask
);
451 ir
->lhs
->accept(this);
455 ir
->rhs
->accept(this);
460 print_float_constant(FILE *f
, float val
)
463 /* 0.0 == -0.0, so print with %f to get the proper sign. */
464 fprintf(f
, "%f", val
);
465 else if (fabs(val
) < 0.000001f
)
466 fprintf(f
, "%a", val
);
467 else if (fabs(val
) > 1000000.0f
)
468 fprintf(f
, "%e", val
);
470 fprintf(f
, "%f", val
);
473 void ir_print_visitor::visit(ir_constant
*ir
)
475 fprintf(f
, "(constant ");
476 glsl_print_type(f
, ir
->type
);
479 if (ir
->type
->is_array()) {
480 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
481 ir
->get_array_element(i
)->accept(this);
482 } else if (ir
->type
->is_struct()) {
483 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
484 fprintf(f
, "(%s ", ir
->type
->fields
.structure
[i
].name
);
485 ir
->get_record_field(i
)->accept(this);
489 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
492 switch (ir
->type
->base_type
) {
493 case GLSL_TYPE_UINT16
:fprintf(f
, "%u", ir
->value
.u16
[i
]); break;
494 case GLSL_TYPE_INT16
: fprintf(f
, "%d", ir
->value
.i16
[i
]); break;
495 case GLSL_TYPE_UINT
: fprintf(f
, "%u", ir
->value
.u
[i
]); break;
496 case GLSL_TYPE_INT
: fprintf(f
, "%d", ir
->value
.i
[i
]); break;
497 case GLSL_TYPE_FLOAT
:
498 print_float_constant(f
, ir
->value
.f
[i
]);
500 case GLSL_TYPE_FLOAT16
:
501 print_float_constant(f
, _mesa_half_to_float(ir
->value
.f16
[i
]));
503 case GLSL_TYPE_SAMPLER
:
504 case GLSL_TYPE_IMAGE
:
505 case GLSL_TYPE_UINT64
:
506 fprintf(f
, "%" PRIu64
, ir
->value
.u64
[i
]);
508 case GLSL_TYPE_INT64
: fprintf(f
, "%" PRIi64
, ir
->value
.i64
[i
]); break;
509 case GLSL_TYPE_BOOL
: fprintf(f
, "%d", ir
->value
.b
[i
]); break;
510 case GLSL_TYPE_DOUBLE
:
511 if (ir
->value
.d
[i
] == 0.0)
512 /* 0.0 == -0.0, so print with %f to get the proper sign. */
513 fprintf(f
, "%.1f", ir
->value
.d
[i
]);
514 else if (fabs(ir
->value
.d
[i
]) < 0.000001)
515 fprintf(f
, "%a", ir
->value
.d
[i
]);
516 else if (fabs(ir
->value
.d
[i
]) > 1000000.0)
517 fprintf(f
, "%e", ir
->value
.d
[i
]);
519 fprintf(f
, "%f", ir
->value
.d
[i
]);
522 unreachable("Invalid constant type");
531 ir_print_visitor::visit(ir_call
*ir
)
533 fprintf(f
, "(call %s ", ir
->callee_name());
534 if (ir
->return_deref
)
535 ir
->return_deref
->accept(this);
537 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
545 ir_print_visitor::visit(ir_return
*ir
)
547 fprintf(f
, "(return");
549 ir_rvalue
*const value
= ir
->get_value();
560 ir_print_visitor::visit(ir_discard
*ir
)
562 fprintf(f
, "(discard ");
564 if (ir
->condition
!= NULL
) {
566 ir
->condition
->accept(this);
574 ir_print_visitor::visit(ir_demote
*ir
)
576 fprintf(f
, "(demote)");
581 ir_print_visitor::visit(ir_if
*ir
)
584 ir
->condition
->accept(this);
589 foreach_in_list(ir_instruction
, inst
, &ir
->then_instructions
) {
600 if (!ir
->else_instructions
.is_empty()) {
604 foreach_in_list(ir_instruction
, inst
, &ir
->else_instructions
) {
619 ir_print_visitor::visit(ir_loop
*ir
)
621 fprintf(f
, "(loop (\n");
624 foreach_in_list(ir_instruction
, inst
, &ir
->body_instructions
) {
636 ir_print_visitor::visit(ir_loop_jump
*ir
)
638 fprintf(f
, "%s", ir
->is_break() ? "break" : "continue");
642 ir_print_visitor::visit(ir_emit_vertex
*ir
)
644 fprintf(f
, "(emit-vertex ");
645 ir
->stream
->accept(this);
650 ir_print_visitor::visit(ir_end_primitive
*ir
)
652 fprintf(f
, "(end-primitive ");
653 ir
->stream
->accept(this);
658 ir_print_visitor::visit(ir_barrier
*)
660 fprintf(f
, "(barrier)\n");