2 * Copyright © 2016 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 */
26 #include "ir_hierarchical_visitor.h"
27 #include "ir_builder_print_visitor.h"
28 #include "compiler/glsl_types.h"
29 #include "glsl_parser_extras.h"
30 #include "main/macros.h"
31 #include "util/hash_table.h"
32 #include "util/u_string.h"
34 class ir_builder_print_visitor
: public ir_hierarchical_visitor
{
36 ir_builder_print_visitor(FILE *f
);
37 virtual ~ir_builder_print_visitor();
41 virtual ir_visitor_status
visit(class ir_variable
*);
42 virtual ir_visitor_status
visit(class ir_dereference_variable
*);
43 virtual ir_visitor_status
visit(class ir_constant
*);
44 virtual ir_visitor_status
visit(class ir_loop_jump
*);
46 virtual ir_visitor_status
visit_enter(class ir_if
*);
48 virtual ir_visitor_status
visit_enter(class ir_loop
*);
49 virtual ir_visitor_status
visit_leave(class ir_loop
*);
51 virtual ir_visitor_status
visit_enter(class ir_function_signature
*);
52 virtual ir_visitor_status
visit_leave(class ir_function_signature
*);
54 virtual ir_visitor_status
visit_enter(class ir_expression
*);
56 virtual ir_visitor_status
visit_enter(class ir_assignment
*);
57 virtual ir_visitor_status
visit_leave(class ir_assignment
*);
59 virtual ir_visitor_status
visit_leave(class ir_call
*);
60 virtual ir_visitor_status
visit_leave(class ir_swizzle
*);
61 virtual ir_visitor_status
visit_leave(class ir_return
*);
64 void print_with_indent(const char *fmt
, ...);
65 void print_without_indent(const char *fmt
, ...);
67 void print_without_declaration(const ir_rvalue
*ir
);
68 void print_without_declaration(const ir_constant
*ir
);
69 void print_without_declaration(const ir_dereference_variable
*ir
);
70 void print_without_declaration(const ir_swizzle
*ir
);
71 void print_without_declaration(const ir_expression
*ir
);
73 unsigned next_ir_index
;
76 * Mapping from ir_instruction * -> index used in the generated C code
79 hash_table
*index_map
;
86 /* An operand is "simple" if it can be compactly printed on one line.
89 is_simple_operand(const ir_rvalue
*ir
, unsigned depth
= 1)
94 switch (ir
->ir_type
) {
95 case ir_type_dereference_variable
:
98 case ir_type_constant
: {
99 if (ir
->type
== glsl_type::uint_type
||
100 ir
->type
== glsl_type::int_type
||
101 ir
->type
== glsl_type::float_type
||
102 ir
->type
== glsl_type::bool_type
)
105 const ir_constant
*const c
= (ir_constant
*) ir
;
106 ir_constant_data all_zero
;
107 memset(&all_zero
, 0, sizeof(all_zero
));
109 return memcmp(&c
->value
, &all_zero
, sizeof(all_zero
)) == 0;
112 case ir_type_swizzle
: {
113 const ir_swizzle
*swiz
= (ir_swizzle
*) ir
;
114 return swiz
->mask
.num_components
== 1 &&
115 is_simple_operand(swiz
->val
, depth
);
118 case ir_type_expression
: {
119 const ir_expression
*expr
= (ir_expression
*) ir
;
121 for (unsigned i
= 0; i
< expr
->num_operands
; i
++) {
122 if (!is_simple_operand(expr
->operands
[i
], depth
- 1))
135 _mesa_print_builder_for_ir(FILE *f
, exec_list
*instructions
)
137 ir_builder_print_visitor
v(f
);
141 ir_builder_print_visitor::ir_builder_print_visitor(FILE *f
)
142 : next_ir_index(1), f(f
), indentation(0)
145 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
148 ir_builder_print_visitor::~ir_builder_print_visitor()
150 _mesa_hash_table_destroy(index_map
, NULL
);
153 void ir_builder_print_visitor::indent(void)
155 for (int i
= 0; i
< indentation
; i
++)
160 ir_builder_print_visitor::print_with_indent(const char *fmt
, ...)
167 vfprintf(f
, fmt
, ap
);
172 ir_builder_print_visitor::print_without_indent(const char *fmt
, ...)
177 vfprintf(f
, fmt
, ap
);
182 ir_builder_print_visitor::print_without_declaration(const ir_rvalue
*ir
)
184 switch (ir
->ir_type
) {
185 case ir_type_dereference_variable
:
186 print_without_declaration((ir_dereference_variable
*) ir
);
188 case ir_type_constant
:
189 print_without_declaration((ir_constant
*) ir
);
191 case ir_type_swizzle
:
192 print_without_declaration((ir_swizzle
*) ir
);
194 case ir_type_expression
:
195 print_without_declaration((ir_expression
*) ir
);
198 unreachable("Invalid IR type.");
203 ir_builder_print_visitor::visit(ir_variable
*ir
)
205 const unsigned my_index
= next_ir_index
++;
207 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
209 const char *mode_str
;
210 switch (ir
->data
.mode
) {
211 case ir_var_auto
: mode_str
= "ir_var_auto"; break;
212 case ir_var_uniform
: mode_str
= "ir_var_uniform"; break;
213 case ir_var_shader_storage
: mode_str
= "ir_var_shader_storage"; break;
214 case ir_var_shader_shared
: mode_str
= "ir_var_shader_shared"; break;
215 case ir_var_shader_in
: mode_str
= "ir_var_shader_in"; break;
216 case ir_var_shader_out
: mode_str
= "ir_var_shader_out"; break;
217 case ir_var_function_in
: mode_str
= "ir_var_function_in"; break;
218 case ir_var_function_out
: mode_str
= "ir_var_function_out"; break;
219 case ir_var_function_inout
: mode_str
= "ir_var_function_inout"; break;
220 case ir_var_const_in
: mode_str
= "ir_var_const_in"; break;
221 case ir_var_system_value
: mode_str
= "ir_var_system_value"; break;
222 case ir_var_temporary
: mode_str
= "ir_var_temporary"; break;
224 unreachable("Invalid variable mode");
227 if (ir
->data
.mode
== ir_var_temporary
) {
228 print_with_indent("ir_variable *const r%04X = body.make_temp(glsl_type::%s_type, \"%s\");\n",
233 print_with_indent("ir_variable *const r%04X = new(mem_ctx) ir_variable(glsl_type::%s_type, \"%s\", %s);\n",
239 switch (ir
->data
.mode
) {
240 case ir_var_function_in
:
241 case ir_var_function_out
:
242 case ir_var_function_inout
:
243 case ir_var_const_in
:
244 print_with_indent("sig_parameters.push_tail(r%04X);\n", my_index
);
247 print_with_indent("body.emit(r%04X);\n", my_index
);
252 return visit_continue
;
256 ir_builder_print_visitor::print_without_declaration(const ir_dereference_variable
*ir
)
258 const struct hash_entry
*const he
=
259 _mesa_hash_table_search(index_map
, ir
->var
);
261 print_without_indent("r%04X", (unsigned)(uintptr_t) he
->data
);
265 ir_builder_print_visitor::visit(ir_dereference_variable
*ir
)
267 const struct hash_entry
*const he
=
268 _mesa_hash_table_search(index_map
, ir
->var
);
271 _mesa_hash_table_insert(index_map
, ir
, he
->data
);
273 return visit_continue
;
277 ir_builder_print_visitor::visit_enter(ir_function_signature
*ir
)
280 return visit_continue_with_parent
;
282 print_with_indent("ir_function_signature *\n"
283 "%s(void *mem_ctx, builtin_available_predicate avail)\n"
285 ir
->function_name());
287 print_with_indent("ir_function_signature *const sig =\n");
288 print_with_indent(" new(mem_ctx) ir_function_signature(glsl_type::%s_type, avail);\n",
289 ir
->return_type
->name
);
291 print_with_indent("ir_factory body(&sig->body, mem_ctx);\n");
292 print_with_indent("sig->is_defined = true;\n\n");
294 if (!ir
->parameters
.is_empty())
295 print_with_indent("exec_list sig_parameters;\n\n");
297 return visit_continue
;
301 ir_builder_print_visitor::visit_leave(ir_function_signature
*ir
)
303 if (!ir
->parameters
.is_empty())
304 print_with_indent("sig->replace_parameters(&sig_parameters);\n");
306 print_with_indent("return sig;\n");
308 print_with_indent("}\n");
309 return visit_continue
;
313 ir_builder_print_visitor::print_without_declaration(const ir_constant
*ir
)
315 if (ir
->type
->is_scalar()) {
316 switch (ir
->type
->base_type
) {
318 print_without_indent("body.constant(%uu)", ir
->value
.u
[0]);
321 print_without_indent("body.constant(int(%d))", ir
->value
.i
[0]);
323 case GLSL_TYPE_FLOAT
:
324 print_without_indent("body.constant(%ff)", ir
->value
.f
[0]);
327 print_without_indent("body.constant(%s)",
328 ir
->value
.i
[0] != 0 ? "true" : "false");
335 ir_constant_data all_zero
;
336 memset(&all_zero
, 0, sizeof(all_zero
));
338 if (memcmp(&ir
->value
, &all_zero
, sizeof(all_zero
)) == 0) {
339 print_without_indent("ir_constant::zero(mem_ctx, glsl_type::%s_type)",
345 ir_builder_print_visitor::visit(ir_constant
*ir
)
347 const unsigned my_index
= next_ir_index
++;
349 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
351 if (ir
->type
== glsl_type::uint_type
||
352 ir
->type
== glsl_type::int_type
||
353 ir
->type
== glsl_type::float_type
||
354 ir
->type
== glsl_type::bool_type
) {
355 print_with_indent("ir_constant *const r%04X = ", my_index
);
356 print_without_declaration(ir
);
357 print_without_indent(";\n");
358 return visit_continue
;
361 ir_constant_data all_zero
;
362 memset(&all_zero
, 0, sizeof(all_zero
));
364 if (memcmp(&ir
->value
, &all_zero
, sizeof(all_zero
)) == 0) {
365 print_with_indent("ir_constant *const r%04X = ", my_index
);
366 print_without_declaration(ir
);
367 print_without_indent(";\n");
369 print_with_indent("ir_constant_data r%04X_data;\n", my_index
);
370 print_with_indent("memset(&r%04X_data, 0, sizeof(ir_constant_data));\n",
372 for (unsigned i
= 0; i
< 16; i
++) {
373 switch (ir
->type
->base_type
) {
375 if (ir
->value
.u
[i
] != 0)
376 print_with_indent("r%04X_data.u[%u] = %u;\n",
377 my_index
, i
, ir
->value
.u
[i
]);
380 if (ir
->value
.i
[i
] != 0)
381 print_with_indent("r%04X_data.i[%u] = %i;\n",
382 my_index
, i
, ir
->value
.i
[i
]);
384 case GLSL_TYPE_FLOAT
:
385 if (ir
->value
.u
[i
] != 0)
386 print_with_indent("r%04X_data.u[%u] = 0x%08x; /* %f */\n",
392 case GLSL_TYPE_DOUBLE
: {
395 STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
397 memcpy(&v
, &ir
->value
.d
[i
], sizeof(v
));
399 print_with_indent("r%04X_data.u64[%u] = 0x%016" PRIx64
"; /* %g */\n",
400 my_index
, i
, v
, ir
->value
.d
[i
]);
403 case GLSL_TYPE_UINT64
:
404 if (ir
->value
.u64
[i
] != 0)
405 print_with_indent("r%04X_data.u64[%u] = %" PRIu64
";\n",
410 case GLSL_TYPE_INT64
:
411 if (ir
->value
.i64
[i
] != 0)
412 print_with_indent("r%04X_data.i64[%u] = %" PRId64
";\n",
418 if (ir
->value
.u
[i
] != 0)
419 print_with_indent("r%04X_data.u[%u] = 1;\n", my_index
, i
);
422 unreachable("Invalid constant type");
426 print_with_indent("ir_constant *const r%04X = new(mem_ctx) ir_constant(glsl_type::%s_type, &r%04X_data);\n",
432 return visit_continue
;
436 ir_builder_print_visitor::print_without_declaration(const ir_swizzle
*ir
)
438 const struct hash_entry
*const he
=
439 _mesa_hash_table_search(index_map
, ir
->val
);
441 if (ir
->mask
.num_components
== 1) {
442 static const char swiz
[4] = { 'x', 'y', 'z', 'w' };
444 if (is_simple_operand(ir
->val
)) {
445 print_without_indent("swizzle_%c(", swiz
[ir
->mask
.x
]);
446 print_without_declaration(ir
->val
);
447 print_without_indent(")");
449 print_without_indent("swizzle_%c(r%04X)",
451 (unsigned)(uintptr_t) he
->data
);
454 static const char swiz
[4] = { 'X', 'Y', 'Z', 'W' };
456 print_without_indent("swizzle(r%04X, MAKE_SWIZZLE4(SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c), %u)",
457 (unsigned)(uintptr_t) he
->data
,
462 ir
->mask
.num_components
);
467 ir_builder_print_visitor::visit_leave(ir_swizzle
*ir
)
469 const unsigned my_index
= next_ir_index
++;
471 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
473 print_with_indent("ir_swizzle *const r%04X = ", my_index
);
474 print_without_declaration(ir
);
475 print_without_indent(";\n");
477 return visit_continue
;
481 ir_builder_print_visitor::visit_enter(ir_assignment
*ir
)
483 ir_expression
*const rhs_expr
= ir
->rhs
->as_expression();
485 if (!is_simple_operand(ir
->rhs
) && rhs_expr
== NULL
)
486 return visit_continue
;
488 if (rhs_expr
!= NULL
) {
489 const unsigned num_op
= rhs_expr
->num_operands
;
491 for (unsigned i
= 0; i
< num_op
; i
++) {
492 if (is_simple_operand(rhs_expr
->operands
[i
]))
495 rhs_expr
->operands
[i
]->accept(this);
501 this->in_assignee
= true;
502 s
= ir
->lhs
->accept(this);
503 this->in_assignee
= false;
504 if (s
!= visit_continue
)
505 return (s
== visit_continue_with_parent
) ? visit_continue
: s
;
507 assert(ir
->condition
== NULL
);
509 const struct hash_entry
*const he_lhs
=
510 _mesa_hash_table_search(index_map
, ir
->lhs
);
512 print_with_indent("body.emit(assign(r%04X, ",
513 (unsigned)(uintptr_t) he_lhs
->data
);
514 print_without_declaration(ir
->rhs
);
515 print_without_indent(", 0x%02x));\n\n", ir
->write_mask
);
517 return visit_continue_with_parent
;
521 ir_builder_print_visitor::visit_leave(ir_assignment
*ir
)
523 const struct hash_entry
*const he_lhs
=
524 _mesa_hash_table_search(index_map
, ir
->lhs
);
526 const struct hash_entry
*const he_rhs
=
527 _mesa_hash_table_search(index_map
, ir
->rhs
);
529 assert(ir
->condition
== NULL
);
531 print_with_indent("body.emit(assign(r%04X, r%04X, 0x%02x));\n\n",
532 (unsigned)(uintptr_t) he_lhs
->data
,
533 (unsigned)(uintptr_t) he_rhs
->data
,
536 return visit_continue
;
540 ir_builder_print_visitor::print_without_declaration(const ir_expression
*ir
)
542 const unsigned num_op
= ir
->num_operands
;
544 static const char *const arity
[] = {
545 "", "unop", "binop", "triop", "quadop"
548 switch (ir
->operation
) {
553 case ir_binop_imul_high
:
555 case ir_binop_gequal
:
557 case ir_binop_nequal
:
558 case ir_binop_lshift
:
559 case ir_binop_rshift
:
560 case ir_binop_bit_and
:
561 case ir_binop_bit_xor
:
562 case ir_binop_bit_or
:
563 case ir_binop_logic_and
:
564 case ir_binop_logic_xor
:
565 case ir_binop_logic_or
:
566 print_without_indent("%s(",
567 ir_expression_operation_enum_strings
[ir
->operation
]);
570 print_without_indent("expr(ir_%s_%s, ",
572 ir_expression_operation_enum_strings
[ir
->operation
]);
576 for (unsigned i
= 0; i
< num_op
; i
++) {
577 if (is_simple_operand(ir
->operands
[i
]))
578 print_without_declaration(ir
->operands
[i
]);
580 const struct hash_entry
*const he
=
581 _mesa_hash_table_search(index_map
, ir
->operands
[i
]);
583 print_without_indent("r%04X", (unsigned)(uintptr_t) he
->data
);
587 print_without_indent(", ");
590 print_without_indent(")");
594 ir_builder_print_visitor::visit_enter(ir_expression
*ir
)
596 const unsigned num_op
= ir
->num_operands
;
598 for (unsigned i
= 0; i
< num_op
; i
++) {
599 if (is_simple_operand(ir
->operands
[i
]))
602 ir
->operands
[i
]->accept(this);
605 const unsigned my_index
= next_ir_index
++;
607 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
609 print_with_indent("ir_expression *const r%04X = ", my_index
);
610 print_without_declaration(ir
);
611 print_without_indent(";\n");
613 return visit_continue_with_parent
;
617 ir_builder_print_visitor::visit_enter(ir_if
*ir
)
619 const unsigned my_index
= next_ir_index
++;
621 print_with_indent("/* IF CONDITION */\n");
623 ir_visitor_status s
= ir
->condition
->accept(this);
624 if (s
!= visit_continue
)
625 return (s
== visit_continue_with_parent
) ? visit_continue
: s
;
627 const struct hash_entry
*const he
=
628 _mesa_hash_table_search(index_map
, ir
->condition
);
630 print_with_indent("ir_if *f%04X = new(mem_ctx) ir_if(operand(r%04X).val);\n",
632 (unsigned)(uintptr_t) he
->data
);
633 print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
637 print_with_indent("/* THEN INSTRUCTIONS */\n");
638 print_with_indent("body.instructions = &f%04X->then_instructions;\n\n",
641 if (s
!= visit_continue_with_parent
) {
642 s
= visit_list_elements(this, &ir
->then_instructions
);
647 print_without_indent("\n");
649 if (!ir
->else_instructions
.is_empty()) {
650 print_with_indent("/* ELSE INSTRUCTIONS */\n");
651 print_with_indent("body.instructions = &f%04X->else_instructions;\n\n",
654 if (s
!= visit_continue_with_parent
) {
655 s
= visit_list_elements(this, &ir
->else_instructions
);
660 print_without_indent("\n");
665 print_with_indent("body.instructions = f%04X_parent_instructions;\n",
667 print_with_indent("body.emit(f%04X);\n\n",
669 print_with_indent("/* END IF */\n\n");
671 return visit_continue_with_parent
;
675 ir_builder_print_visitor::visit_leave(ir_return
*ir
)
677 const struct hash_entry
*const he
=
678 _mesa_hash_table_search(index_map
, ir
->value
);
680 print_with_indent("body.emit(ret(r%04X));\n\n",
681 (unsigned)(uintptr_t) he
->data
);
683 return visit_continue
;
687 ir_builder_print_visitor::visit_leave(ir_call
*ir
)
689 const unsigned my_index
= next_ir_index
++;
691 print_without_indent("\n");
692 print_with_indent("/* CALL %s */\n", ir
->callee_name());
693 print_with_indent("exec_list r%04X_parameters;\n", my_index
);
695 foreach_in_list(ir_dereference_variable
, param
, &ir
->actual_parameters
) {
696 const struct hash_entry
*const he
=
697 _mesa_hash_table_search(index_map
, param
);
699 print_with_indent("r%04X_parameters.push_tail(operand(r%04X).val);\n",
701 (unsigned)(uintptr_t) he
->data
);
704 char return_deref_string
[32];
705 if (ir
->return_deref
) {
706 const struct hash_entry
*const he
=
707 _mesa_hash_table_search(index_map
, ir
->return_deref
);
709 util_snprintf(return_deref_string
, sizeof(return_deref_string
),
710 "operand(r%04X).val",
711 (unsigned)(uintptr_t) he
->data
);
713 strcpy(return_deref_string
, "NULL");
716 print_with_indent("body.emit(new(mem_ctx) ir_call(shader->symbols->get_function(\"%s\"),\n",
718 print_with_indent(" %s, &r%04X_parameters);\n\n",
721 return visit_continue
;
725 ir_builder_print_visitor::visit_enter(ir_loop
*ir
)
727 const unsigned my_index
= next_ir_index
++;
729 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
731 print_with_indent("/* LOOP BEGIN */\n");
732 print_with_indent("ir_loop *f%04X = new(mem_ctx) ir_loop();\n", my_index
);
733 print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
738 print_with_indent("body.instructions = &f%04X->body_instructions;\n\n",
741 return visit_continue
;
745 ir_builder_print_visitor::visit_leave(ir_loop
*ir
)
747 const struct hash_entry
*const he
=
748 _mesa_hash_table_search(index_map
, ir
);
752 print_with_indent("/* LOOP END */\n\n");
753 print_with_indent("body.instructions = f%04X_parent_instructions;\n",
754 (unsigned)(uintptr_t) he
->data
);
755 print_with_indent("body.emit(f%04X);\n\n",
756 (unsigned)(uintptr_t) he
->data
);
758 return visit_continue
;
762 ir_builder_print_visitor::visit(ir_loop_jump
*ir
)
764 print_with_indent("body.emit(new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_%s));\n\n",
765 ir
->is_break() ? "break" : "continue");
766 return visit_continue
;