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
->get_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_without_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_without_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_without_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 /* FIXME: This won't actually work until ARB_gpu_shader_int64
401 print_without_indent("r%04X_data.u64[%u] = 0x%016" PRIx64
"; /* %g */\n",
402 my_index
, i
, v
, ir
->value
.d
[i
]);
406 if (ir
->value
.u
[i
] != 0)
407 print_without_indent("r%04X_data.u[%u] = 1;\n", my_index
, i
);
410 unreachable("Invalid constant type");
414 print_with_indent("ir_constant *const r%04X = new(mem_ctx) ir_constant(glsl_type::%s_type, &r%04X_data);\n",
420 return visit_continue
;
424 ir_builder_print_visitor::print_without_declaration(const ir_swizzle
*ir
)
426 const struct hash_entry
*const he
=
427 _mesa_hash_table_search(index_map
, ir
->val
);
429 if (ir
->mask
.num_components
== 1) {
430 static const char swiz
[4] = { 'x', 'y', 'z', 'w' };
432 if (is_simple_operand(ir
->val
)) {
433 print_without_indent("swizzle_%c(", swiz
[ir
->mask
.x
]);
434 print_without_declaration(ir
->val
);
435 print_without_indent(")");
437 print_without_indent("swizzle_%c(r%04X)",
439 (unsigned)(uintptr_t) he
->data
);
442 static const char swiz
[4] = { 'X', 'Y', 'Z', 'W' };
444 print_without_indent("swizzle(r%04X, MAKE_SWIZZLE4(SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c), %u)",
445 (unsigned)(uintptr_t) he
->data
,
450 ir
->mask
.num_components
);
455 ir_builder_print_visitor::visit_leave(ir_swizzle
*ir
)
457 const unsigned my_index
= next_ir_index
++;
459 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
461 print_with_indent("ir_swizzle *const r%04X = ", my_index
);
462 print_without_declaration(ir
);
463 print_without_indent(";\n");
465 return visit_continue
;
469 ir_builder_print_visitor::visit_enter(ir_assignment
*ir
)
471 ir_expression
*const rhs_expr
= ir
->rhs
->as_expression();
473 if (!is_simple_operand(ir
->rhs
) && rhs_expr
== NULL
)
474 return visit_continue
;
476 if (rhs_expr
!= NULL
) {
477 const unsigned num_op
= rhs_expr
->get_num_operands();
479 for (unsigned i
= 0; i
< num_op
; i
++) {
480 if (is_simple_operand(rhs_expr
->operands
[i
]))
483 rhs_expr
->operands
[i
]->accept(this);
489 this->in_assignee
= true;
490 s
= ir
->lhs
->accept(this);
491 this->in_assignee
= false;
492 if (s
!= visit_continue
)
493 return (s
== visit_continue_with_parent
) ? visit_continue
: s
;
495 assert(ir
->condition
== NULL
);
497 const struct hash_entry
*const he_lhs
=
498 _mesa_hash_table_search(index_map
, ir
->lhs
);
500 print_with_indent("body.emit(assign(r%04X, ",
501 (unsigned)(uintptr_t) he_lhs
->data
);
502 print_without_declaration(ir
->rhs
);
503 print_without_indent(", 0x%02x));\n\n", ir
->write_mask
);
505 return visit_continue_with_parent
;
509 ir_builder_print_visitor::visit_leave(ir_assignment
*ir
)
511 const struct hash_entry
*const he_lhs
=
512 _mesa_hash_table_search(index_map
, ir
->lhs
);
514 const struct hash_entry
*const he_rhs
=
515 _mesa_hash_table_search(index_map
, ir
->rhs
);
517 assert(ir
->condition
== NULL
);
519 print_with_indent("body.emit(assign(r%04X, r%04X, 0x%02x));\n\n",
520 (unsigned)(uintptr_t) he_lhs
->data
,
521 (unsigned)(uintptr_t) he_rhs
->data
,
524 return visit_continue
;
528 ir_builder_print_visitor::print_without_declaration(const ir_expression
*ir
)
530 const unsigned num_op
= ir
->get_num_operands();
532 static const char *const arity
[] = {
533 "", "unop", "binop", "triop", "quadop"
536 switch (ir
->operation
) {
541 case ir_binop_imul_high
:
543 case ir_binop_greater
:
544 case ir_binop_lequal
:
545 case ir_binop_gequal
:
547 case ir_binop_nequal
:
548 case ir_binop_lshift
:
549 case ir_binop_rshift
:
550 case ir_binop_bit_and
:
551 case ir_binop_bit_xor
:
552 case ir_binop_bit_or
:
553 case ir_binop_logic_and
:
554 case ir_binop_logic_xor
:
555 case ir_binop_logic_or
:
556 print_without_indent("%s(",
557 ir_expression_operation_enum_strings
[ir
->operation
]);
560 print_without_indent("expr(ir_%s_%s, ",
562 ir_expression_operation_enum_strings
[ir
->operation
]);
566 for (unsigned i
= 0; i
< num_op
; i
++) {
567 if (is_simple_operand(ir
->operands
[i
]))
568 print_without_declaration(ir
->operands
[i
]);
570 const struct hash_entry
*const he
=
571 _mesa_hash_table_search(index_map
, ir
->operands
[i
]);
573 print_without_indent("r%04X", (unsigned)(uintptr_t) he
->data
);
577 print_without_indent(", ");
580 print_without_indent(")");
584 ir_builder_print_visitor::visit_enter(ir_expression
*ir
)
586 const unsigned num_op
= ir
->get_num_operands();
588 for (unsigned i
= 0; i
< num_op
; i
++) {
589 if (is_simple_operand(ir
->operands
[i
]))
592 ir
->operands
[i
]->accept(this);
595 const unsigned my_index
= next_ir_index
++;
597 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
599 print_with_indent("ir_expression *const r%04X = ", my_index
);
600 print_without_declaration(ir
);
601 print_without_indent(";\n");
603 return visit_continue_with_parent
;
607 ir_builder_print_visitor::visit_enter(ir_if
*ir
)
609 const unsigned my_index
= next_ir_index
++;
611 print_with_indent("/* IF CONDITION */\n");
613 ir_visitor_status s
= ir
->condition
->accept(this);
614 if (s
!= visit_continue
)
615 return (s
== visit_continue_with_parent
) ? visit_continue
: s
;
617 const struct hash_entry
*const he
=
618 _mesa_hash_table_search(index_map
, ir
->condition
);
620 print_with_indent("ir_if *f%04X = new(mem_ctx) ir_if(operand(r%04X).val);\n",
622 (unsigned)(uintptr_t) he
->data
);
623 print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
627 print_with_indent("/* THEN INSTRUCTIONS */\n");
628 print_with_indent("body.instructions = &f%04X->then_instructions;\n\n",
631 if (s
!= visit_continue_with_parent
) {
632 s
= visit_list_elements(this, &ir
->then_instructions
);
637 print_without_indent("\n");
639 if (!ir
->else_instructions
.is_empty()) {
640 print_with_indent("/* ELSE INSTRUCTIONS */\n");
641 print_with_indent("body.instructions = &f%04X->else_instructions;\n\n",
644 if (s
!= visit_continue_with_parent
) {
645 s
= visit_list_elements(this, &ir
->else_instructions
);
650 print_without_indent("\n");
655 print_with_indent("body.instructions = f%04X_parent_instructions;\n",
657 print_with_indent("body.emit(f%04X);\n\n",
659 print_with_indent("/* END IF */\n\n");
661 return visit_continue_with_parent
;
665 ir_builder_print_visitor::visit_leave(ir_return
*ir
)
667 const struct hash_entry
*const he
=
668 _mesa_hash_table_search(index_map
, ir
->value
);
670 print_with_indent("body.emit(ret(r%04X));\n\n",
671 (unsigned)(uintptr_t) he
->data
);
673 return visit_continue
;
677 ir_builder_print_visitor::visit_leave(ir_call
*ir
)
679 const unsigned my_index
= next_ir_index
++;
681 print_without_indent("\n");
682 print_with_indent("/* CALL %s */\n", ir
->callee_name());
683 print_with_indent("exec_list r%04X_parameters;\n", my_index
);
685 foreach_in_list(ir_dereference_variable
, param
, &ir
->actual_parameters
) {
686 const struct hash_entry
*const he
=
687 _mesa_hash_table_search(index_map
, param
);
689 print_with_indent("r%04X_parameters.push_tail(operand(r%04X).val);\n",
691 (unsigned)(uintptr_t) he
->data
);
694 char return_deref_string
[32];
695 if (ir
->return_deref
) {
696 const struct hash_entry
*const he
=
697 _mesa_hash_table_search(index_map
, ir
->return_deref
);
699 snprintf(return_deref_string
, sizeof(return_deref_string
),
700 "operand(r%04X).val",
701 (unsigned)(uintptr_t) he
->data
);
703 strcpy(return_deref_string
, "NULL");
706 print_with_indent("body.emit(new(mem_ctx) ir_call(shader->symbols->get_function(\"%s\"),\n",
708 print_with_indent(" %s, &r%04X_parameters);\n\n",
711 return visit_continue
;
715 ir_builder_print_visitor::visit_enter(ir_loop
*ir
)
717 const unsigned my_index
= next_ir_index
++;
719 _mesa_hash_table_insert(index_map
, ir
, (void *)(uintptr_t) my_index
);
721 print_with_indent("/* LOOP BEGIN */\n");
722 print_with_indent("ir_loop *f%04X = new(mem_ctx) ir_loop();\n", my_index
);
723 print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
728 print_with_indent("body.instructions = &f%04X->body_instructions;\n\n",
731 return visit_continue
;
735 ir_builder_print_visitor::visit_leave(ir_loop
*ir
)
737 const struct hash_entry
*const he
=
738 _mesa_hash_table_search(index_map
, ir
);
742 print_with_indent("/* LOOP END */\n\n");
743 print_with_indent("body.instructions = f%04X_parent_instructions;\n",
744 (unsigned)(uintptr_t) he
->data
);
745 print_with_indent("body.emit(f%04X);\n\n",
746 (unsigned)(uintptr_t) he
->data
);
748 return visit_continue
;
752 ir_builder_print_visitor::visit(ir_loop_jump
*ir
)
754 print_with_indent("body.emit(new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_%s));\n\n",
755 ir
->is_break() ? "break" : "continue");
756 return visit_continue
;