From: Mikko Rasa Date: Mon, 1 Mar 2021 23:25:30 +0000 (+0200) Subject: Further improve inlining of GLSL functions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=d5c7c7f0b15c407b3da2184936e6deed18554c6a;p=libs%2Fgl.git Further improve inlining of GLSL functions Functions of any lenght can now be inlined, as long as they have linear control flow. --- diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 55a69f0d..1eeb3625 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -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. */ diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index d6e5fa55..d26c6e2d 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -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 set(in_function, &func); + SetForScope 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::iterator &ins_pt, FunctionDeclaration &src) +{ + target_block = &tgtb; + source_func = &src; + for(NodeList::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::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 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 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 &ptr) void FunctionInliner::visit(Block &block) { - if(extract_result) - --extract_result; - + SetForScope::iterator> save_insert_point(insert_point, block.body.begin()); for(NodeList::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 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); } diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 1c044fb4..390dd64e 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -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 refcounts; std::set 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 variable_map; + bool remap_names; + bool deps_only; + RefPtr inlined_statement; std::set dependencies; + std::string result_name; public: - const std::set &apply(FunctionDeclaration &f) { f.visit(*this); return dependencies; } + InlineContentInjector(); + + const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::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 inlineable; FunctionDeclaration *current_function; - unsigned extract_result; + NodeList::iterator insert_point; RefPtr 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 &);