]> git.tdb.fi Git - libs/gl.git/commitdiff
Further improve inlining of GLSL functions
authorMikko Rasa <tdb@tdb.fi>
Mon, 1 Mar 2021 23:25:30 +0000 (01:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 1 Mar 2021 23:29:35 +0000 (01:29 +0200)
Functions of any lenght can now be inlined, as long as they have linear
control flow.

source/glsl/compiler.cpp
source/glsl/optimize.cpp
source/glsl/optimize.h

index 55a69f0df38ecaf1b681f9a698e6bbc2abed02da..1eeb36252ba899a8f8851798f2a393a1223cc05d 100644 (file)
@@ -244,8 +244,11 @@ Compiler::OptimizeResult Compiler::optimize(Stage &stage)
        ConstantConditionEliminator().apply(stage);
 
        bool any_inlined = FunctionInliner().apply(stage);
-       BlockHierarchyResolver().apply(stage);
-       VariableResolver().apply(stage);
+       if(any_inlined)
+       {
+               VariableResolver().apply(stage);
+               FunctionResolver().apply(stage);
+       }
 
        /* Removing variables or functions may cause things from the previous stage
        to become unused. */
index d6e5fa5592576ce3080bd46afb756d8e7dbb5c1e..d26c6e2d519179ac531dd2a635c85e605d98e9e8 100644 (file)
@@ -8,7 +8,8 @@ namespace GL {
 namespace SL {
 
 InlineableFunctionLocator::InlineableFunctionLocator():
-       in_function(0)
+       current_function(0),
+       return_count(0)
 { }
 
 void InlineableFunctionLocator::visit(FunctionCall &call)
@@ -21,7 +22,7 @@ void InlineableFunctionLocator::visit(FunctionCall &call)
        {
                unsigned &count = refcounts[def];
                ++count;
-               if(count>1 || def==in_function)
+               if(count>1 || def==current_function)
                        inlineable.erase(def);
        }
 
@@ -31,52 +32,147 @@ void InlineableFunctionLocator::visit(FunctionCall &call)
 void InlineableFunctionLocator::visit(FunctionDeclaration &func)
 {
        unsigned &count = refcounts[func.definition];
-       if(!count && func.parameters.empty())
+       if(count<=1 && func.parameters.empty())
                inlineable.insert(func.definition);
 
-       SetForScope<FunctionDeclaration *> set(in_function, &func);
+       SetForScope<FunctionDeclaration *> set(current_function, &func);
+       return_count = 0;
        TraversingVisitor::visit(func);
 }
 
+void InlineableFunctionLocator::visit(Conditional &)
+{
+       inlineable.erase(current_function);
+}
+
+void InlineableFunctionLocator::visit(Iteration &)
+{
+       inlineable.erase(current_function);
+}
+
+void InlineableFunctionLocator::visit(Return &)
+{
+       if(return_count)
+               inlineable.erase(current_function);
+       ++return_count;
+}
+
+
+InlineContentInjector::InlineContentInjector():
+       source_func(0),
+       remap_names(false),
+       deps_only(false)
+{ }
+
+const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgtb, const NodeList<Statement>::iterator &ins_pt, FunctionDeclaration &src)
+{
+       target_block = &tgtb;
+       source_func = &src;
+       for(NodeList<Statement>::iterator i=src.body.body.begin(); i!=src.body.body.end(); ++i)
+       {
+               inlined_statement = 0;
+               (*i)->visit(*this);
+               if(!inlined_statement)
+                       inlined_statement = (*i)->clone();
+
+               SetFlag set_remap(remap_names);
+               inlined_statement->visit(*this);
+               tgtb.body.insert(ins_pt, inlined_statement);
+       }
+
+       NodeReorderer().apply(stage, target_func, dependencies);
+
+       return result_name;
+}
+
+string InlineContentInjector::create_unused_name(const string &base, bool always_prefix)
+{
+       string result = base;
+       if(always_prefix || target_block->variables.count(result))
+               result = format("_%s_%s", source_func->name, base);
+       unsigned initial_size = result.size();
+       for(unsigned i=1; target_block->variables.count(result); ++i)
+       {
+               result.erase(initial_size);
+               result += format("_%d", i);
+       }
+       return result;
+}
 
-void InlineDependencyCollector::visit(VariableReference &var)
+void InlineContentInjector::visit(VariableReference &var)
 {
-       if(var.declaration)
+       if(remap_names)
+       {
+               map<string, VariableDeclaration *>::const_iterator i = variable_map.find(var.name);
+               if(i!=variable_map.end())
+                       var.name = i->second->name;
+       }
+       else if(var.declaration)
        {
+               SetFlag set_deps(deps_only);
                dependencies.insert(var.declaration);
                var.declaration->visit(*this);
        }
 }
 
-void InlineDependencyCollector::visit(InterfaceBlockReference &iface)
+void InlineContentInjector::visit(InterfaceBlockReference &iface)
 {
-       if(iface.declaration)
+       if(!remap_names && iface.declaration)
        {
+               SetFlag set_deps(deps_only);
                dependencies.insert(iface.declaration);
                iface.declaration->visit(*this);
        }
 }
 
-void InlineDependencyCollector::visit(FunctionCall &call)
+void InlineContentInjector::visit(FunctionCall &call)
 {
-       if(call.declaration)
+       if(!remap_names && call.declaration)
                dependencies.insert(call.declaration);
        TraversingVisitor::visit(call);
 }
 
-void InlineDependencyCollector::visit(VariableDeclaration &var)
+void InlineContentInjector::visit(VariableDeclaration &var)
 {
+       TraversingVisitor::visit(var);
+
        if(var.type_declaration)
        {
+               SetFlag set_deps(deps_only);
                dependencies.insert(var.type_declaration);
                var.type_declaration->visit(*this);
        }
+
+       if(!remap_names && !deps_only)
+       {
+               RefPtr<VariableDeclaration> inlined_var = var.clone();
+               inlined_var->name = create_unused_name(var.name, false);
+               inlined_statement = inlined_var;
+
+               variable_map[var.name] = inlined_var.get();
+       }
+}
+
+void InlineContentInjector::visit(Return &ret)
+{
+       TraversingVisitor::visit(ret);
+
+       if(ret.expression)
+       {
+               result_name = create_unused_name("return", true);
+               RefPtr<VariableDeclaration> var = new VariableDeclaration;
+               var->source = ret.source;
+               var->line = ret.line;
+               var->type = source_func->return_type;
+               var->name = result_name;
+               var->init_expression = ret.expression->clone();
+               inlined_statement = var;
+       }
 }
 
 
 FunctionInliner::FunctionInliner():
        current_function(0),
-       extract_result(0),
        any_inlined(false)
 { }
 
@@ -102,14 +198,11 @@ void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
 
 void FunctionInliner::visit(Block &block)
 {
-       if(extract_result)
-               --extract_result;
-
+       SetForScope<NodeList<Statement>::iterator> save_insert_point(insert_point, block.body.begin());
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
+               insert_point = i;
                (*i)->visit(*this);
-               if(extract_result)
-                       --extract_result;
        }
 }
 
@@ -143,16 +236,29 @@ void FunctionInliner::visit(FunctionCall &call)
 
        if(def && inlineable.count(def))
        {
-               extract_result = 2;
-               def->visit(*this);
+               string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, *def);
+
+               // This will later get removed by UnusedVariableRemover
+               if(result_name.empty())
+                       result_name = "msp_unused_from_inline";
 
-               if(inline_result)
-                       NodeReorderer().apply(*stage, *current_function, InlineDependencyCollector().apply(*def));
+               RefPtr<VariableReference> ref = new VariableReference;
+               ref->name = result_name;
+               inline_result = ref;
+
+               /* Inlined variables need to be resolved before this function can be
+               inlined further. */
+               inlineable.erase(current_function);
        }
        else
                inline_result = 0;
 }
 
+void FunctionInliner::visit(ExpressionStatement &expr)
+{
+       visit_and_inline(expr.expression);
+}
+
 void FunctionInliner::visit(VariableDeclaration &var)
 {
        if(var.init_expression)
@@ -168,10 +274,8 @@ void FunctionInliner::visit(FunctionDeclaration &func)
 
 void FunctionInliner::visit(Return &ret)
 {
-       TraversingVisitor::visit(ret);
-
-       if(extract_result)
-               inline_result = ret.expression->clone();
+       if(ret.expression)
+               visit_and_inline(ret.expression);
 }
 
 
index 1c044fb47f0ea5e9da9efdda39a75f5fbccfee58..390dd64ea98f668639e6733447f29ac5895368f2 100644 (file)
@@ -11,13 +11,15 @@ namespace GL {
 namespace SL {
 
 /** Finds functions which are candidates for inlining.  Currently this means
-functions which have no parameters and are only called once. */
+functions which have no parameters, contain no more than one return statement,
+and are only called once. */
 class InlineableFunctionLocator: private TraversingVisitor
 {
 private:
        std::map<FunctionDeclaration *, unsigned> refcounts;
        std::set<FunctionDeclaration *> inlineable;
-       FunctionDeclaration *in_function;
+       FunctionDeclaration *current_function;
+       unsigned return_count;
 
 public:
        InlineableFunctionLocator();
@@ -27,22 +29,39 @@ public:
 private:
        virtual void visit(FunctionCall &);
        virtual void visit(FunctionDeclaration &);
+       virtual void visit(Conditional &);
+       virtual void visit(Iteration &);
+       virtual void visit(Return &);
 };
 
-/** Collects declarations referenced by a function. */
-class InlineDependencyCollector: private TraversingVisitor
+/** Injects statements from one function into another.  Local variables are
+renamed to avoid conflicts.  After inlining, uses NodeReorderer to cause
+dependencies of the inlined statements to appear before the target function. */
+class InlineContentInjector: private TraversingVisitor
 {
 private:
+       FunctionDeclaration *source_func;
+       Block *target_block;
+       std::map<std::string, VariableDeclaration *> variable_map;
+       bool remap_names;
+       bool deps_only;
+       RefPtr<Statement> inlined_statement;
        std::set<Node *> dependencies;
+       std::string result_name;
 
 public:
-       const std::set<Node *> &apply(FunctionDeclaration &f) { f.visit(*this); return dependencies; }
+       InlineContentInjector();
+
+       const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionDeclaration &);
 
 private:
+       std::string create_unused_name(const std::string &, bool);
+
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(FunctionCall &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(Return &);
 };
 
 /** Inlines functions.  Internally uses InlineableFunctionLocator to find
@@ -54,7 +73,7 @@ private:
        Stage *stage;
        std::set<FunctionDeclaration *> inlineable;
        FunctionDeclaration *current_function;
-       unsigned extract_result;
+       NodeList<Statement>::iterator insert_point;
        RefPtr<Expression> inline_result;
        bool any_inlined;
 
@@ -71,6 +90,7 @@ private:
        virtual void visit(BinaryExpression &);
        virtual void visit(MemberAccess &);
        virtual void visit(FunctionCall &);
+       virtual void visit(ExpressionStatement &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(FunctionDeclaration &);
        virtual void visit(Return &);