From: Mikko Rasa Date: Sat, 20 Feb 2021 18:20:47 +0000 (+0200) Subject: Rearrange secondary operations in GLSL compiler X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ff8a0248f1a3c0c1f48f670867bc9106c898c55b;p=libs%2Fgl.git Rearrange secondary operations in GLSL compiler These are now performed internally by the main operation visitor. --- diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 347e16ad..35977cb4 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -114,7 +114,7 @@ void Compiler::append_module(Module &mod, DataFile::Collection *res) vector imports = NodeGatherer().apply(mod.shared); for(vector::iterator i=imports.begin(); i!=imports.end(); ++i) import(res, (*i)->module); - NodeRemover(set(imports.begin(), imports.end())).apply(mod.shared); + NodeRemover().apply(mod.shared, set(imports.begin(), imports.end())); append_stage(mod.shared); for(list::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i) @@ -184,15 +184,12 @@ bool Compiler::optimize(Stage &stage) { ConstantConditionEliminator().apply(stage); - set inlineable = InlineableFunctionLocator().apply(stage); - FunctionInliner(inlineable).apply(stage); + FunctionInliner().apply(stage); - set unused = UnusedVariableLocator().apply(stage); - set unused2 = UnusedFunctionLocator().apply(stage); - unused.insert(unused2.begin(), unused2.end()); - NodeRemover(unused).apply(stage); + bool result = UnusedVariableRemover().apply(stage); + result |= UnusedFunctionRemover().apply(stage); - return !unused.empty(); + return result; } void Compiler::finalize(Stage &stage) diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index dfb17b26..7bbe14da 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -39,11 +39,16 @@ void InlineableFunctionLocator::visit(FunctionDeclaration &func) } -FunctionInliner::FunctionInliner(const set &in): - inlineable(in), +FunctionInliner::FunctionInliner(): extract_result(0) { } +void FunctionInliner::apply(Stage &stage) +{ + inlineable = InlineableFunctionLocator().apply(stage); + visit(stage.content); +} + void FunctionInliner::visit_and_inline(RefPtr &ptr) { inline_result = 0; @@ -198,14 +203,14 @@ void ConstantConditionEliminator::visit(Iteration &iter) } -UnusedVariableLocator::VariableInfo::VariableInfo(): +UnusedVariableRemover::VariableInfo::VariableInfo(): local(false), conditionally_assigned(false), referenced(false) { } -UnusedVariableLocator::UnusedVariableLocator(): +UnusedVariableRemover::UnusedVariableRemover(): aggregate(0), assignment(0), assignment_target(false), @@ -213,7 +218,7 @@ UnusedVariableLocator::UnusedVariableLocator(): global_scope(true) { } -const set &UnusedVariableRemover::apply(Stage &stage) +bool UnusedVariableRemover::apply(Stage &stage) { variables.push_back(BlockVariableMap()); visit(stage.content); @@ -230,10 +235,12 @@ const set &UnusedVariableRemover::apply(Stage &stage) } variables.pop_back(); - return unused_nodes; + NodeRemover().apply(stage, unused_nodes); + + return !unused_nodes.empty(); } -void UnusedVariableLocator::visit(VariableReference &var) +void UnusedVariableRemover::visit(VariableReference &var) { map::iterator i = aggregates.find(var.declaration); if(i!=aggregates.end()) @@ -247,13 +254,13 @@ void UnusedVariableLocator::visit(VariableReference &var) } } -void UnusedVariableLocator::visit(MemberAccess &memacc) +void UnusedVariableRemover::visit(MemberAccess &memacc) { TraversingVisitor::visit(memacc); unused_nodes.erase(memacc.declaration); } -void UnusedVariableLocator::visit(BinaryExpression &binary) +void UnusedVariableRemover::visit(BinaryExpression &binary) { if(binary.oper=="[") { @@ -267,7 +274,7 @@ void UnusedVariableLocator::visit(BinaryExpression &binary) TraversingVisitor::visit(binary); } -void UnusedVariableLocator::visit(Assignment &assign) +void UnusedVariableRemover::visit(Assignment &assign) { { assign_to_subscript = false; @@ -278,7 +285,7 @@ void UnusedVariableLocator::visit(Assignment &assign) assignment = &assign; } -void UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool chained) +void UnusedVariableRemover::record_assignment(VariableDeclaration &var, Node &node, bool chained) { VariableInfo &var_info = variables.back()[&var]; if(!chained) @@ -287,7 +294,7 @@ void UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &no var_info.conditionally_assigned = false; } -void UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_unused) +void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_unused) { if(mark_unused) { @@ -297,7 +304,7 @@ void UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_ var_info.assignments.clear(); } -void UnusedVariableLocator::visit(ExpressionStatement &expr) +void UnusedVariableRemover::visit(ExpressionStatement &expr) { assignment = 0; TraversingVisitor::visit(expr); @@ -305,14 +312,14 @@ void UnusedVariableLocator::visit(ExpressionStatement &expr) record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript)); } -void UnusedVariableLocator::visit(StructDeclaration &strct) +void UnusedVariableRemover::visit(StructDeclaration &strct) { SetForScope set(aggregate, &strct); unused_nodes.insert(&strct); TraversingVisitor::visit(strct); } -void UnusedVariableLocator::visit(VariableDeclaration &var) +void UnusedVariableRemover::visit(VariableDeclaration &var) { if(aggregate) aggregates[&var] = aggregate; @@ -326,14 +333,14 @@ void UnusedVariableLocator::visit(VariableDeclaration &var) TraversingVisitor::visit(var); } -void UnusedVariableLocator::visit(InterfaceBlock &iface) +void UnusedVariableRemover::visit(InterfaceBlock &iface) { SetForScope set(aggregate, &iface); unused_nodes.insert(&iface); TraversingVisitor::visit(iface); } -void UnusedVariableLocator::visit(FunctionDeclaration &func) +void UnusedVariableRemover::visit(FunctionDeclaration &func) { variables.push_back(BlockVariableMap()); @@ -352,7 +359,7 @@ void UnusedVariableLocator::visit(FunctionDeclaration &func) merge_down_variables(); } -void UnusedVariableLocator::merge_down_variables() +void UnusedVariableRemover::merge_down_variables() { BlockVariableMap &parent_variables = variables[variables.size()-2]; BlockVariableMap &block_variables = variables.back(); @@ -381,7 +388,7 @@ void UnusedVariableLocator::merge_down_variables() variables.pop_back(); } -void UnusedVariableLocator::visit(Conditional &cond) +void UnusedVariableRemover::visit(Conditional &cond) { cond.condition->visit(*this); variables.push_back(BlockVariableMap()); @@ -414,7 +421,7 @@ void UnusedVariableLocator::visit(Conditional &cond) merge_down_variables(); } -void UnusedVariableLocator::visit(Iteration &iter) +void UnusedVariableRemover::visit(Iteration &iter) { variables.push_back(BlockVariableMap()); TraversingVisitor::visit(iter); @@ -428,7 +435,14 @@ void UnusedVariableLocator::visit(Iteration &iter) } -void UnusedFunctionLocator::visit(FunctionCall &call) +bool UnusedFunctionRemover::apply(Stage &stage) +{ + visit(stage.content); + NodeRemover().apply(stage, unused_nodes); + return !unused_nodes.empty(); +} + +void UnusedFunctionRemover::visit(FunctionCall &call) { TraversingVisitor::visit(call); @@ -437,7 +451,7 @@ void UnusedFunctionLocator::visit(FunctionCall &call) used_definitions.insert(call.declaration->definition); } -void UnusedFunctionLocator::visit(FunctionDeclaration &func) +void UnusedFunctionRemover::visit(FunctionDeclaration &func) { TraversingVisitor::visit(func); diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 01508f45..2d1b20c9 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -36,9 +36,9 @@ private: RefPtr inline_result; public: - FunctionInliner(const std::set &); + FunctionInliner(); - void apply(Stage &s) { visit(s.content); } + void apply(Stage &); private: void visit_and_inline(RefPtr &); @@ -75,7 +75,7 @@ private: using BlockModifier::visit; }; -class UnusedVariableLocator: private TraversingVisitor +class UnusedVariableRemover: private TraversingVisitor { private: struct VariableInfo @@ -100,9 +100,9 @@ private: bool global_scope; public: - UnusedVariableLocator(); + UnusedVariableRemover(); - const std::set &apply(Stage &); + bool apply(Stage &); private: virtual void visit(VariableReference &); @@ -122,14 +122,14 @@ private: using TraversingVisitor::visit; }; -class UnusedFunctionLocator: private TraversingVisitor +class UnusedFunctionRemover: private TraversingVisitor { private: std::set unused_nodes; std::set used_definitions; public: - const std::set &apply(Stage &s) { visit(s.content); return unused_nodes; } + bool apply(Stage &s); private: virtual void visit(FunctionCall &); diff --git a/source/glsl/visitor.cpp b/source/glsl/visitor.cpp index 91e75b70..98777ae9 100644 --- a/source/glsl/visitor.cpp +++ b/source/glsl/visitor.cpp @@ -146,14 +146,15 @@ void BlockModifier::visit(Block &block) } -NodeRemover::NodeRemover(const set &r): +NodeRemover::NodeRemover(): stage(0), - to_remove(r) + to_remove(0) { } -void NodeRemover::apply(Stage &s) +void NodeRemover::apply(Stage &s, const set &tr) { stage = &s; + to_remove = &tr; visit(s.content); } @@ -162,7 +163,7 @@ void NodeRemover::visit(Block &block) for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ) { (*i)->visit(*this); - if(to_remove.count(i->get())) + if(to_remove->count(i->get())) block.body.erase(i++); else ++i; @@ -171,7 +172,7 @@ void NodeRemover::visit(Block &block) void NodeRemover::visit(VariableDeclaration &var) { - if(to_remove.count(&var)) + if(to_remove->count(&var)) { stage->in_variables.erase(var.name); stage->out_variables.erase(var.name); @@ -179,13 +180,13 @@ void NodeRemover::visit(VariableDeclaration &var) if(var.linked_declaration) var.linked_declaration->linked_declaration = 0; } - else if(var.init_expression && to_remove.count(var.init_expression.get())) + else if(var.init_expression && to_remove->count(var.init_expression.get())) var.init_expression = 0; } void NodeRemover::visit(Iteration &iter) { - if(to_remove.count(iter.init_statement.get())) + if(to_remove->count(iter.init_statement.get())) iter.init_statement = 0; TraversingVisitor::visit(iter); } diff --git a/source/glsl/visitor.h b/source/glsl/visitor.h index 756584c2..8bfcac0f 100644 --- a/source/glsl/visitor.h +++ b/source/glsl/visitor.h @@ -89,7 +89,7 @@ private: std::vector nodes; public: - const std::vector &apply(Stage &s) { visit(s.content); return nodes; } + const std::vector &apply(Stage &s) { nodes.clear(); visit(s.content); return nodes; } private: using TraversingVisitor::visit; @@ -100,12 +100,12 @@ class NodeRemover: private TraversingVisitor { private: Stage *stage; - std::set to_remove; + const std::set *to_remove; public: - NodeRemover(const std::set &); + NodeRemover(); - void apply(Stage &); + void apply(Stage &, const std::set &); private: using TraversingVisitor::visit;