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"
33 class ir_builder_print_visitor
: public ir_hierarchical_visitor
{
35 ir_builder_print_visitor(FILE *f
);
36 virtual ~ir_builder_print_visitor();
40 virtual ir_visitor_status
visit(class ir_variable
*);
41 virtual ir_visitor_status
visit(class ir_dereference_variable
*);
42 virtual ir_visitor_status
visit(class ir_constant
*);
43 virtual ir_visitor_status
visit(class ir_loop_jump
*);
45 virtual ir_visitor_status
visit_enter(class ir_if
*);
47 virtual ir_visitor_status
visit_enter(class ir_loop
*);
48 virtual ir_visitor_status
visit_leave(class ir_loop
*);
50 virtual ir_visitor_status
visit_enter(class ir_function_signature
*);
51 virtual ir_visitor_status
visit_leave(class ir_function_signature
*);
53 virtual ir_visitor_status
visit_enter(class ir_expression
*);
55 virtual ir_visitor_status
visit_enter(class ir_assignment
*);
56 virtual ir_visitor_status
visit_leave(class ir_assignment
*);
58 virtual ir_visitor_status
visit_leave(class ir_call
*);
59 virtual ir_visitor_status
visit_leave(class ir_swizzle
*);
60 virtual ir_visitor_status
visit_leave(class ir_return
*);
63 void print_with_indent(const char *fmt
, ...);
64 void print_without_indent(const char *fmt
, ...);
66 void print_without_declaration(const ir_rvalue
*ir
);
67 void print_without_declaration(const ir_constant
*ir
);
68 void print_without_declaration(const ir_dereference_variable
*ir
);
69 void print_without_declaration(const ir_swizzle
*ir
);
70 void print_without_declaration(const ir_expression
*ir
);
72 unsigned next_ir_index
;
75 * Mapping from ir_instruction * -> index used in the generated C code
78 hash_table
*index_map
;
85 /* An operand is "simple" if it can be compactly printed on one line.
88 is_simple_operand(const ir_rvalue
*ir
, unsigned depth
= 1)
93 switch (ir
->ir_type
) {
94 case ir_type_dereference_variable
:
97 case ir_type_constant
: {
98 if (ir
->type
== glsl_type::uint_type
||
99 ir
->type
== glsl_type::int_type
||
100 ir
->type
== glsl_type::float_type
||
101 ir
->type
== glsl_type::bool_type
)
104 const ir_constant
*const c
= (ir_constant
*) ir
;
105 ir_constant_data all_zero
;
106 memset(&all_zero
, 0, sizeof(all_zero
));
108 return memcmp(&c
->value
, &all_zero
, sizeof(all_zero
)) == 0;
111 case ir_type_swizzle
: {
112 const ir_swizzle
*swiz
= (ir_swizzle
*) ir
;
113 return swiz
->mask
.num_components
== 1 &&
114 is_simple_operand(swiz
->val
, depth
);
117 case ir_type_expression
: {
118 const ir_expression
*expr
= (ir_expression
*) ir
;
120 for (unsigned i
= 0; i
< expr
->num_operands
; i
++) {
121 if (!is_simple_operand(expr
->operands
[i
], depth
- 1))
134 _mesa_print_builder_for_ir(FILE *f
, exec_list
*instructions
)
136 ir_builder_print_visitor
v(f
);
140 ir_builder_print_visitor::ir_builder_print_visitor(FILE *f
)
141 : next_ir_index(1), f(f
), indentation(0)
144 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
147 ir_builder_print_visitor::~ir_builder_print_visitor()
149 _mesa_hash_table_destroy(index_map
, NULL
);
152 void ir_builder_print_visitor::indent(void)
154 for (int i
= 0; i
< indentation
; i
++)
159 ir_builder_print_visitor::print_with_indent(const char *fmt
, ...)
166 vfprintf(f
, fmt
, ap
);
171 ir_builder_print_visitor::print_without_indent(const char *fmt
, ...)
176 vfprintf(f
, fmt
, ap
);
181 ir_builder_print_visitor::print_without_declaration(const ir_rvalue
*ir
)
183 switch (ir
->ir_type
) {
184 case ir_type_dereference_variable
:
185 print_without_declaration((ir_dereference_variable
*) ir
);
187 case ir_type_constant
:
188 print_without_declaration((ir_constant
*) ir
);
190 case ir_type_swizzle
:
191 print_without_declaration((ir_swizzle
*) ir
);
193 case ir_type_expression
:
194 print_without_declaration((ir_expression
*) ir
);
197 unreachable("Invalid IR type.");
202 ir_builder_print_visitor::visit(ir_variable
*ir
)
204 const unsigned my_index
= next_ir_index
++;
206 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
208 const char *mode_str
;
209 switch (ir
->data
.mode
) {
210 case ir_var_auto
: mode_str
= "ir_var_auto"; break;
211 case ir_var_uniform
: mode_str
= "ir_var_uniform"; break;
212 case ir_var_shader_storage
: mode_str
= "ir_var_shader_storage"; break;
213 case ir_var_shader_shared
: mode_str
= "ir_var_shader_shared"; break;
214 case ir_var_shader_in
: mode_str
= "ir_var_shader_in"; break;
215 case ir_var_shader_out
: mode_str
= "ir_var_shader_out"; break;
216 case ir_var_function_in
: mode_str
= "ir_var_function_in"; break;
217 case ir_var_function_out
: mode_str
= "ir_var_function_out"; break;
218 case ir_var_function_inout
: mode_str
= "ir_var_function_inout"; break;
219 case ir_var_const_in
: mode_str
= "ir_var_const_in"; break;
220 case ir_var_system_value
: mode_str
= "ir_var_system_value"; break;
221 case ir_var_temporary
: mode_str
= "ir_var_temporary"; break;
223 unreachable("Invalid variable mode");
226 if (ir
->data
.mode
== ir_var_temporary
) {
227 print_with_indent("ir_variable *const r%04X = body.make_temp(glsl_type::%s_type, \"%s\");\n",
232 print_with_indent("ir_variable *const r%04X = new(mem_ctx) ir_variable(glsl_type::%s_type, \"%s\", %s);\n",
238 switch (ir
->data
.mode
) {
239 case ir_var_function_in
:
240 case ir_var_function_out
:
241 case ir_var_function_inout
:
242 case ir_var_const_in
:
243 print_with_indent("sig_parameters.push_tail(r%04X);\n", my_index
);
246 print_with_indent("body.emit(r%04X);\n", my_index
);
251 return visit_continue
;
255 ir_builder_print_visitor::print_without_declaration(const ir_dereference_variable
*ir
)
257 const struct hash_entry
*const he
=
258 _mesa_hash_table_search(index_map
, ir
->var
);
260 print_without_indent("r%04X", (unsigned)(uintptr_t) he
->data
);
264 ir_builder_print_visitor::visit(ir_dereference_variable
*ir
)
266 const struct hash_entry
*const he
=
267 _mesa_hash_table_search(index_map
, ir
->var
);
270 _mesa_hash_table_insert(index_map
, ir
, he
->data
);
272 return visit_continue
;
276 ir_builder_print_visitor::visit_enter(ir_function_signature
*ir
)
279 return visit_continue_with_parent
;
281 print_with_indent("ir_function_signature *\n"
282 "%s(void *mem_ctx, builtin_available_predicate avail)\n"
284 ir
->function_name());
286 print_with_indent("ir_function_signature *const sig =\n");
287 print_with_indent(" new(mem_ctx) ir_function_signature(glsl_type::%s_type, avail);\n",
288 ir
->return_type
->name
);
290 print_with_indent("ir_factory body(&sig->body, mem_ctx);\n");
291 print_with_indent("sig->is_defined = true;\n\n");
293 if (!ir
->parameters
.is_empty())
294 print_with_indent("exec_list sig_parameters;\n\n");
296 return visit_continue
;
300 ir_builder_print_visitor::visit_leave(ir_function_signature
*ir
)
302 if (!ir
->parameters
.is_empty())
303 print_with_indent("sig->replace_parameters(&sig_parameters);\n");
305 print_with_indent("return sig;\n");
307 print_with_indent("}\n");
308 return visit_continue
;
312 ir_builder_print_visitor::print_without_declaration(const ir_constant
*ir
)
314 if (ir
->type
->is_scalar()) {
315 switch (ir
->type
->base_type
) {
317 print_without_indent("body.constant(%uu)", ir
->value
.u
[0]);
320 print_without_indent("body.constant(int(%d))", ir
->value
.i
[0]);
322 case GLSL_TYPE_FLOAT
:
323 print_without_indent("body.constant(%ff)", ir
->value
.f
[0]);
326 print_without_indent("body.constant(%s)",
327 ir
->value
.i
[0] != 0 ? "true" : "false");
334 ir_constant_data all_zero
;
335 memset(&all_zero
, 0, sizeof(all_zero
));
337 if (memcmp(&ir
->value
, &all_zero
, sizeof(all_zero
)) == 0) {
338 print_without_indent("ir_constant::zero(mem_ctx, glsl_type::%s_type)",
344 ir_builder_print_visitor::visit(ir_constant
*ir
)
346 const unsigned my_index
= next_ir_index
++;
348 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
350 if (ir
->type
== glsl_type::uint_type
||
351 ir
->type
== glsl_type::int_type
||
352 ir
->type
== glsl_type::float_type
||
353 ir
->type
== glsl_type::bool_type
) {
354 print_with_indent("ir_constant *const r%04X = ", my_index
);
355 print_without_declaration(ir
);
356 print_without_indent(";\n");
357 return visit_continue
;
360 ir_constant_data all_zero
;
361 memset(&all_zero
, 0, sizeof(all_zero
));
363 if (memcmp(&ir
->value
, &all_zero
, sizeof(all_zero
)) == 0) {
364 print_with_indent("ir_constant *const r%04X = ", my_index
);
365 print_without_declaration(ir
);
366 print_without_indent(";\n");
368 print_with_indent("ir_constant_data r%04X_data;\n", my_index
);
369 print_with_indent("memset(&r%04X_data, 0, sizeof(ir_constant_data));\n",
371 for (unsigned i
= 0; i
< 16; i
++) {
372 switch (ir
->type
->base_type
) {
374 if (ir
->value
.u
[i
] != 0)
375 print_with_indent("r%04X_data.u[%u] = %u;\n",
376 my_index
, i
, ir
->value
.u
[i
]);
379 if (ir
->value
.i
[i
] != 0)
380 print_with_indent("r%04X_data.i[%u] = %i;\n",
381 my_index
, i
, ir
->value
.i
[i
]);
383 case GLSL_TYPE_FLOAT
:
384 if (ir
->value
.u
[i
] != 0)
385 print_with_indent("r%04X_data.u[%u] = 0x%08x; /* %f */\n",
391 case GLSL_TYPE_DOUBLE
: {
394 STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
396 memcpy(&v
, &ir
->value
.d
[i
], sizeof(v
));
398 print_with_indent("r%04X_data.u64[%u] = 0x%016" PRIx64
"; /* %g */\n",
399 my_index
, i
, v
, ir
->value
.d
[i
]);
402 case GLSL_TYPE_UINT64
:
403 if (ir
->value
.u64
[i
] != 0)
404 print_with_indent("r%04X_data.u64[%u] = %" PRIu64
";\n",
409 case GLSL_TYPE_INT64
:
410 if (ir
->value
.i64
[i
] != 0)
411 print_with_indent("r%04X_data.i64[%u] = %" PRId64
";\n",
417 if (ir
->value
.u
[i
] != 0)
418 print_with_indent("r%04X_data.u[%u] = 1;\n", my_index
, i
);
421 unreachable("Invalid constant type");
425 print_with_indent("ir_constant *const r%04X = new(mem_ctx) ir_constant(glsl_type::%s_type, &r%04X_data);\n",
431 return visit_continue
;
435 ir_builder_print_visitor::print_without_declaration(const ir_swizzle
*ir
)
437 const struct hash_entry
*const he
=
438 _mesa_hash_table_search(index_map
, ir
->val
);
440 if (ir
->mask
.num_components
== 1) {
441 static const char swiz
[4] = { 'x', 'y', 'z', 'w' };
443 if (is_simple_operand(ir
->val
)) {
444 print_without_indent("swizzle_%c(", swiz
[ir
->mask
.x
]);
445 print_without_declaration(ir
->val
);
446 print_without_indent(")");
448 print_without_indent("swizzle_%c(r%04X)",
450 (unsigned)(uintptr_t) he
->data
);
453 static const char swiz
[4] = { 'X', 'Y', 'Z', 'W' };
455 print_without_indent("swizzle(r%04X, MAKE_SWIZZLE4(SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c), %u)",
456 (unsigned)(uintptr_t) he
->data
,
461 ir
->mask
.num_components
);
466 ir_builder_print_visitor::visit_leave(ir_swizzle
*ir
)
468 const unsigned my_index
= next_ir_index
++;
470 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
472 print_with_indent("ir_swizzle *const r%04X = ", my_index
);
473 print_without_declaration(ir
);
474 print_without_indent(";\n");
476 return visit_continue
;
480 ir_builder_print_visitor::visit_enter(ir_assignment
*ir
)
482 ir_expression
*const rhs_expr
= ir
->rhs
->as_expression();
484 if (!is_simple_operand(ir
->rhs
) && rhs_expr
== NULL
)
485 return visit_continue
;
487 if (rhs_expr
!= NULL
) {
488 const unsigned num_op
= rhs_expr
->num_operands
;
490 for (unsigned i
= 0; i
< num_op
; i
++) {
491 if (is_simple_operand(rhs_expr
->operands
[i
]))
494 rhs_expr
->operands
[i
]->accept(this);
500 this->in_assignee
= true;
501 s
= ir
->lhs
->accept(this);
502 this->in_assignee
= false;
503 if (s
!= visit_continue
)
504 return (s
== visit_continue_with_parent
) ? visit_continue
: s
;
506 assert(ir
->condition
== NULL
);
508 const struct hash_entry
*const he_lhs
=
509 _mesa_hash_table_search(index_map
, ir
->lhs
);
511 print_with_indent("body.emit(assign(r%04X, ",
512 (unsigned)(uintptr_t) he_lhs
->data
);
513 print_without_declaration(ir
->rhs
);
514 print_without_indent(", 0x%02x));\n\n", ir
->write_mask
);
516 return visit_continue_with_parent
;
520 ir_builder_print_visitor::visit_leave(ir_assignment
*ir
)
522 const struct hash_entry
*const he_lhs
=
523 _mesa_hash_table_search(index_map
, ir
->lhs
);
525 const struct hash_entry
*const he_rhs
=
526 _mesa_hash_table_search(index_map
, ir
->rhs
);
528 assert(ir
->condition
== NULL
);
530 print_with_indent("body.emit(assign(r%04X, r%04X, 0x%02x));\n\n",
531 (unsigned)(uintptr_t) he_lhs
->data
,
532 (unsigned)(uintptr_t) he_rhs
->data
,
535 return visit_continue
;
539 ir_builder_print_visitor::print_without_declaration(const ir_expression
*ir
)
541 const unsigned num_op
= ir
->num_operands
;
543 static const char *const arity
[] = {
544 "", "unop", "binop", "triop", "quadop"
547 switch (ir
->operation
) {
552 case ir_binop_imul_high
:
554 case ir_binop_greater
:
555 case ir_binop_lequal
:
556 case ir_binop_gequal
:
558 case ir_binop_nequal
:
559 case ir_binop_lshift
:
560 case ir_binop_rshift
:
561 case ir_binop_bit_and
:
562 case ir_binop_bit_xor
:
563 case ir_binop_bit_or
:
564 case ir_binop_logic_and
:
565 case ir_binop_logic_xor
:
566 case ir_binop_logic_or
:
567 print_without_indent("%s(",
568 ir_expression_operation_enum_strings
[ir
->operation
]);
571 print_without_indent("expr(ir_%s_%s, ",
573 ir_expression_operation_enum_strings
[ir
->operation
]);
577 for (unsigned i
= 0; i
< num_op
; i
++) {
578 if (is_simple_operand(ir
->operands
[i
]))
579 print_without_declaration(ir
->operands
[i
]);
581 const struct hash_entry
*const he
=
582 _mesa_hash_table_search(index_map
, ir
->operands
[i
]);
584 print_without_indent("r%04X", (unsigned)(uintptr_t) he
->data
);
588 print_without_indent(", ");
591 print_without_indent(")");
595 ir_builder_print_visitor::visit_enter(ir_expression
*ir
)
597 const unsigned num_op
= ir
->num_operands
;
599 for (unsigned i
= 0; i
< num_op
; i
++) {
600 if (is_simple_operand(ir
->operands
[i
]))
603 ir
->operands
[i
]->accept(this);
606 const unsigned my_index
= next_ir_index
++;
608 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
610 print_with_indent("ir_expression *const r%04X = ", my_index
);
611 print_without_declaration(ir
);
612 print_without_indent(";\n");
614 return visit_continue_with_parent
;
618 ir_builder_print_visitor::visit_enter(ir_if
*ir
)
620 const unsigned my_index
= next_ir_index
++;
622 print_with_indent("/* IF CONDITION */\n");
624 ir_visitor_status s
= ir
->condition
->accept(this);
625 if (s
!= visit_continue
)
626 return (s
== visit_continue_with_parent
) ? visit_continue
: s
;
628 const struct hash_entry
*const he
=
629 _mesa_hash_table_search(index_map
, ir
->condition
);
631 print_with_indent("ir_if *f%04X = new(mem_ctx) ir_if(operand(r%04X).val);\n",
633 (unsigned)(uintptr_t) he
->data
);
634 print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
638 print_with_indent("/* THEN INSTRUCTIONS */\n");
639 print_with_indent("body.instructions = &f%04X->then_instructions;\n\n",
642 if (s
!= visit_continue_with_parent
) {
643 s
= visit_list_elements(this, &ir
->then_instructions
);
648 print_without_indent("\n");
650 if (!ir
->else_instructions
.is_empty()) {
651 print_with_indent("/* ELSE INSTRUCTIONS */\n");
652 print_with_indent("body.instructions = &f%04X->else_instructions;\n\n",
655 if (s
!= visit_continue_with_parent
) {
656 s
= visit_list_elements(this, &ir
->else_instructions
);
661 print_without_indent("\n");
666 print_with_indent("body.instructions = f%04X_parent_instructions;\n",
668 print_with_indent("body.emit(f%04X);\n\n",
670 print_with_indent("/* END IF */\n\n");
672 return visit_continue_with_parent
;
676 ir_builder_print_visitor::visit_leave(ir_return
*ir
)
678 const struct hash_entry
*const he
=
679 _mesa_hash_table_search(index_map
, ir
->value
);
681 print_with_indent("body.emit(ret(r%04X));\n\n",
682 (unsigned)(uintptr_t) he
->data
);
684 return visit_continue
;
688 ir_builder_print_visitor::visit_leave(ir_call
*ir
)
690 const unsigned my_index
= next_ir_index
++;
692 print_without_indent("\n");
693 print_with_indent("/* CALL %s */\n", ir
->callee_name());
694 print_with_indent("exec_list r%04X_parameters;\n", my_index
);
696 foreach_in_list(ir_dereference_variable
, param
, &ir
->actual_parameters
) {
697 const struct hash_entry
*const he
=
698 _mesa_hash_table_search(index_map
, param
);
700 print_with_indent("r%04X_parameters.push_tail(operand(r%04X).val);\n",
702 (unsigned)(uintptr_t) he
->data
);
705 char return_deref_string
[32];
706 if (ir
->return_deref
) {
707 const struct hash_entry
*const he
=
708 _mesa_hash_table_search(index_map
, ir
->return_deref
);
710 snprintf(return_deref_string
, sizeof(return_deref_string
),
711 "operand(r%04X).val",
712 (unsigned)(uintptr_t) he
->data
);
714 strcpy(return_deref_string
, "NULL");
717 print_with_indent("body.emit(new(mem_ctx) ir_call(shader->symbols->get_function(\"%s\"),\n",
719 print_with_indent(" %s, &r%04X_parameters);\n\n",
722 return visit_continue
;
726 ir_builder_print_visitor::visit_enter(ir_loop
*ir
)
728 const unsigned my_index
= next_ir_index
++;
730 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
732 print_with_indent("/* LOOP BEGIN */\n");
733 print_with_indent("ir_loop *f%04X = new(mem_ctx) ir_loop();\n", my_index
);
734 print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
739 print_with_indent("body.instructions = &f%04X->body_instructions;\n\n",
742 return visit_continue
;
746 ir_builder_print_visitor::visit_leave(ir_loop
*ir
)
748 const struct hash_entry
*const he
=
749 _mesa_hash_table_search(index_map
, ir
);
753 print_with_indent("/* LOOP END */\n\n");
754 print_with_indent("body.instructions = f%04X_parent_instructions;\n",
755 (unsigned)(uintptr_t) he
->data
);
756 print_with_indent("body.emit(f%04X);\n\n",
757 (unsigned)(uintptr_t) he
->data
);
759 return visit_continue
;
763 ir_builder_print_visitor::visit(ir_loop_jump
*ir
)
765 print_with_indent("body.emit(new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_%s));\n\n",
766 ir
->is_break() ? "break" : "continue");
767 return visit_continue
;