<div dir="ltr">On Fri, Jan 31, 2014 at 11:55 AM, Paul Berry <span dir="ltr"><<a href="mailto:stereotype441@gmail.com" target="_blank">stereotype441@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="h5">On 22 January 2014 09:16, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br>
</div></div><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Right now we are being basically as naive as possible, and inserting<br>
more copies than necessary. It is possible to implement a more<br>
sophisticated algorithm later, although extending the current copy<br>
propagation pass to support loops better and/or relying on backends to<br>
do copy propagation may make this unecessary.<br>
---<br>
src/glsl/Makefile.sources | 1 +<br>
src/glsl/ir_optimization.h | 1 +<br>
src/glsl/opt_from_ssa.cpp | 198 +++++++++++++++++++++++++++++++++++++++++++++<br>
3 files changed, 200 insertions(+)<br>
create mode 100644 src/glsl/opt_from_ssa.cpp<br>
<br>
diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
index 961784b..55859ed 100644<br>
--- a/src/glsl/Makefile.sources<br>
+++ b/src/glsl/Makefile.sources<br>
@@ -94,6 +94,7 @@ LIBGLSL_FILES = \<br>
$(GLSL_SRCDIR)/opt_dead_functions.cpp \<br>
$(GLSL_SRCDIR)/opt_flatten_nested_if_blocks.cpp \<br>
$(GLSL_SRCDIR)/opt_flip_matrices.cpp \<br>
+ $(GLSL_SRCDIR)/opt_from_ssa.cpp \<br>
$(GLSL_SRCDIR)/opt_function_inlining.cpp \<br>
$(GLSL_SRCDIR)/opt_if_simplification.cpp \<br>
$(GLSL_SRCDIR)/opt_noop_swizzle.cpp \<br>
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h<br>
index 92c8b57..9c0ff31 100644<br>
--- a/src/glsl/ir_optimization.h<br>
+++ b/src/glsl/ir_optimization.h<br>
@@ -66,6 +66,7 @@ enum lower_packing_builtins_op {<br>
};<br>
<br>
void convert_to_ssa(exec_list *instructions);<br>
+void convert_from_ssa(exec_list *instructions);<br>
<br>
bool do_common_optimization(exec_list *ir, bool linked,<br>
bool uniform_locations_assigned,<br>
diff --git a/src/glsl/opt_from_ssa.cpp b/src/glsl/opt_from_ssa.cpp<br>
new file mode 100644<br>
index 0000000..6071c45<br>
--- /dev/null<br>
+++ b/src/glsl/opt_from_ssa.cpp<br>
@@ -0,0 +1,198 @@<br>
+/*<br>
+ * Copyright © 2013 Connor Abbott (<a href="mailto:connor@abbott.cx" target="_blank">connor@abbott.cx</a>)<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER<br>
+ * DEALINGS IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "ir.h"<br>
+#include "ir_builder.h"<br>
+<br>
+/**<br>
+ * \file opt_from_ssa.cpp<br>
+ *<br>
+ * This file removes all the SSA temporaries and phi nodes from a program. It<br>
+ * immplements Method I of the paper "Translating out of Single Static<br>
+ * Assignment Form" by Sreedhar et. al., a naive method that inserts many more<br>
+ * copies than necessary; it is assumed that later copy propagation passes will<br>
+ * clean up the result of this pass.<br>
+ */<br>
+<br>
+using namespace ir_builder;<br>
+<br>
+static ir_variable *<br>
+insert_decl(exec_list *instrs, const glsl_type *type, void *mem_ctx)<br>
+{<br>
+ ir_variable *var = new(mem_ctx) ir_variable(type, "phi_temp",<br>
+ ir_var_temporary);<br>
+ instrs->push_head(var);<br>
+ return var;<br>
+}<br>
+<br>
+static void<br>
+eliminate_phi_if(ir_phi_if *phi, ir_if *ir, exec_list *instrs)<br>
+{<br>
+ ir_variable *var = insert_decl(instrs, phi->dest->type, ralloc_parent(ir));<br>
+<br>
+ /*<br>
+ * This converts the destination of the phi node into a non-SSA variable,<br>
+ * which ir_from_ssa_visitor::visit(ir_dereference_variable *) would normally<br>
+ * do. We need to do this here because otherwise, the assignment we're<br>
+ * inserting here will get skipped by the list visitor macro and it won't<br>
+ * get converted.<br>
+ */ <br></blockquote><div><br></div></div></div><div>I found this comment confusing because the use of "otherwise" seems to imply that what we're doing here will cause the list visitor to visit the new node; in fact what is happening is that we're compensating for the fact that it won't. How about instead saying something like "We need to do this here because the list visitor uses foreach_list_safe(), so it will skip any nodes we insert."<br>
</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+<br>
+ ir->insert_after(phi->dest);<br>
+ phi->dest->insert_after(assign(phi->dest, var));<br>
+ phi->dest->data.mode = ir_var_temporary; <br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+<br>
+ if (phi->if_src != NULL)<br>
+ ir->then_instructions.push_tail(assign(var, phi->if_src));<br>
+<br>
+ if (phi->else_src != NULL)<br>
+ ir->else_instructions.push_tail(assign(var, phi->else_src));<br>
+<br>
+ phi->remove();<br>
+}<br>
+<br>
+static void<br>
+eliminate_phi_loop_begin(ir_phi_loop_begin *phi, ir_loop *ir, exec_list *instrs)<br>
+{<br>
+ ir_variable *var = insert_decl(instrs, phi->dest->type, ralloc_parent(ir));<br>
+ ir->body_instructions.push_head(phi->dest);<br>
+ phi->dest->insert_after(assign(phi->dest, var));<br>
+ phi->dest->data.mode = ir_var_temporary;<br>
+<br>
+ if (phi->enter_src != NULL)<br>
+ ir->insert_before(assign(var, phi->enter_src));<br>
+<br>
+ if (phi->repeat_src != NULL)<br>
+ ir->body_instructions.push_tail(assign(var, phi->repeat_src));<br>
+<br>
+ foreach_list(n, &phi->continue_srcs) {<br>
+ ir_phi_jump_src *src = (ir_phi_jump_src *) n;<br>
+<br>
+ if (src->src != NULL)<br>
+ src->jump->insert_before(assign(var, src->src));<br>
+ }<br>
+<br>
+ phi->remove();<br>
+}<br>
+<br>
+static void<br>
+eliminate_phi_loop_end(ir_phi_loop_end *phi, ir_loop *ir, exec_list *instrs)<br>
+{<br>
+ ir_variable *var = insert_decl(instrs, phi->dest->type, ralloc_parent(ir));<br>
+ ir->insert_after(phi->dest);<br>
+ phi->dest->insert_after(assign(phi->dest, var));<br>
+ phi->dest->data.mode = ir_var_temporary;<br>
+<br>
+ foreach_list(n, &phi->break_srcs) {<br>
+ ir_phi_jump_src *src = (ir_phi_jump_src *) n;<br>
+<br>
+ if (src->src != NULL)<br>
+ src->jump->insert_before(assign(var, src->src));<br>
+ }<br>
+<br>
+ phi->remove();<br>
+}<br>
+<br>
+namespace {<br>
+<br>
+class ir_from_ssa_visitor : public ir_hierarchical_visitor<br>
+{<br>
+public:<br>
+ ir_from_ssa_visitor(exec_list *base_instrs) : base_instrs(base_instrs)<br>
+ {<br>
+ }<br>
+<br>
+ virtual ir_visitor_status visit_leave(ir_if *);<br>
+ virtual ir_visitor_status visit_enter(ir_loop *);<br>
+ virtual ir_visitor_status visit_leave(ir_loop *);<br>
+ virtual ir_visitor_status visit(ir_dereference_variable *);<br>
+<br>
+private:<br>
+ exec_list *base_instrs;<br>
+};<br>
+<br>
+};<br>
+<br>
+ir_visitor_status<br>
+ir_from_ssa_visitor::visit_leave(ir_if *ir)<br>
+{<br>
+ foreach_list_safe(n, &ir->phi_nodes) {<br>
+ eliminate_phi_if((ir_phi_if *) n, ir, this->base_instrs);<br>
+ }<br>
+<br>
+ return visit_continue;<br>
+}<br>
+<br>
+ir_visitor_status<br>
+ir_from_ssa_visitor::visit_enter(ir_loop *ir)<br>
+{<br>
+ foreach_list_safe(n, &ir->begin_phi_nodes) {<br>
+ eliminate_phi_loop_begin((ir_phi_loop_begin *) n, ir, this->base_instrs);<br>
+ }<br>
+<br>
+ return visit_continue;<br>
+}<br>
+<br>
+ir_visitor_status<br>
+ir_from_ssa_visitor::visit_leave(ir_loop *ir)<br>
+{<br>
+ foreach_list_safe(n, &ir->end_phi_nodes) {<br>
+ eliminate_phi_loop_end((ir_phi_loop_end *) n, ir, this->base_instrs);<br>
+ }<br>
+<br>
+ return visit_continue;<br>
+}<br>
+<br>
+ir_visitor_status<br>
+ir_from_ssa_visitor::visit(ir_dereference_variable *ir)<br>
+{<br>
+ if (this->in_assignee && ir->var->data.mode == ir_var_temporary_ssa) {<br>
+ this->base_ir->insert_before(ir->var);<br>
+ ir->var->data.mode = ir_var_temporary;<br>
+ }<br>
+<br>
+ return visit_continue;<br>
+}<br>
+<br>
+static void<br>
+convert_from_ssa_function(exec_list *instructions)<br>
+{<br>
+ ir_from_ssa_visitor v(instructions);<br>
+ v.run(instructions);<br>
+}<br>
+<br>
+void<br>
+convert_from_ssa(exec_list *instructions)<br>
+{<br>
+ foreach_list(node, instructions) {<br>
+ ir_instruction *ir = (ir_instruction *) node;<br>
+ ir_function *f = ir->as_function();<br>
+ if (f) {<br>
+ foreach_list(sig_node, &f->signatures) {<br>
+ ir_function_signature *sig = (ir_function_signature *) sig_node;<br>
+<br>
+ convert_from_ssa_function(&sig->body);<br>
+ }<br>
+ }<br>
+ }<br>
+}<br></blockquote><div><br></div></div></div><div>It looks like the only reason you need to manually walk through the functions and signatures is so that you can make sure that ir_from_ssa_visitor::base_instrs always points to the correct function's instruction list. Normally the way we achieve that sort of thing is to do:<br>
<br>ir_visitor_status<br>ir_from_ssa_visitor::visit_enter(ir_function_signature *ir)<br>{<br> this->base_instrs = &ir->body;<br> return visit_continue;<br>}<br><br>ir_visitor_status<br>ir_from_ssa_visitor::visit_leave(ir_function_signature *ir)<br>
{<br> this->base_instrs = NULL;<br> return visit_continue;<br>}<br><br></div><div>That way you could get rid of convert_from_ssa_function() and convert_from_ssa() would just become:<br><br>void<br>convert_from_ssa(exec_list *instructions)<br>
{<br> ir_from_ssa_visitor v;<br> v.run(instructions);<br>}<br></div></div></div></div></blockquote><div><br></div><div style>OK... I think I got this code by copy-pasting from opt_to_ssa.cpp (which I got from local_dce.cpp IIRC), where the intention was to avoid processing global statements; it doesn't matter if we process global statements here, though, since we won't do anything.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br><br></div><div>I don't have really strong feelings about it, though. Provided that the confusing comment in eliminate_phi_if() is fixed, this patch is:<br>
<br>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com" target="_blank">stereotype441@gmail.com</a>><br></div></div></div></div>
</blockquote></div><br></div></div>