]> git.tdb.fi Git - libs/gl.git/commitdiff
Rearrange secondary operations in GLSL compiler
authorMikko Rasa <tdb@tdb.fi>
Sat, 20 Feb 2021 18:20:47 +0000 (20:20 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 20 Feb 2021 18:22:14 +0000 (20:22 +0200)
These are now performed internally by the main operation visitor.

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

index 347e16ad301dba1881b604713a68476095ccf0f2..35977cb42be534077bca6c9f3427fbbcc158d443 100644 (file)
@@ -114,7 +114,7 @@ void Compiler::append_module(Module &mod, DataFile::Collection *res)
        vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
        for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
                import(res, (*i)->module);
-       NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
+       NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
 
        append_stage(mod.shared);
        for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
@@ -184,15 +184,12 @@ bool Compiler::optimize(Stage &stage)
 {
        ConstantConditionEliminator().apply(stage);
 
-       set<FunctionDeclaration *> inlineable = InlineableFunctionLocator().apply(stage);
-       FunctionInliner(inlineable).apply(stage);
+       FunctionInliner().apply(stage);
 
-       set<Node *> unused = UnusedVariableLocator().apply(stage);
-       set<Node *> 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)
index dfb17b26799bc2845d063ad9618356f2f80be8d9..7bbe14da382b6dbd54fb23ce113187f7e8616de3 100644 (file)
@@ -39,11 +39,16 @@ void InlineableFunctionLocator::visit(FunctionDeclaration &func)
 }
 
 
-FunctionInliner::FunctionInliner(const set<FunctionDeclaration *> &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<Expression> &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<Node *> &UnusedVariableRemover::apply(Stage &stage)
+bool UnusedVariableRemover::apply(Stage &stage)
 {
        variables.push_back(BlockVariableMap());
        visit(stage.content);
@@ -230,10 +235,12 @@ const set<Node *> &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<VariableDeclaration *, Node *>::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<Node *> 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<Node *> 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);
 
index 01508f450257ac4bee4b00dffd4e062449f3e8a5..2d1b20c99e72d7b1361508cc9bdda14c2424952b 100644 (file)
@@ -36,9 +36,9 @@ private:
        RefPtr<Expression> inline_result;
 
 public:
-       FunctionInliner(const std::set<FunctionDeclaration *> &);
+       FunctionInliner();
 
-       void apply(Stage &s) { visit(s.content); }
+       void apply(Stage &);
 
 private:
        void visit_and_inline(RefPtr<Expression> &);
@@ -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<Node *> &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<Node *> unused_nodes;
        std::set<FunctionDeclaration *> used_definitions;
 
 public:
-       const std::set<Node *> &apply(Stage &s) { visit(s.content); return unused_nodes; }
+       bool apply(Stage &s);
 
 private:
        virtual void visit(FunctionCall &);
index 91e75b7006b40cf0d621e0a3498c5cf2e5431fe1..98777ae9b0541f97af5b4156a7725235d9cb24d6 100644 (file)
@@ -146,14 +146,15 @@ void BlockModifier::visit(Block &block)
 }
 
 
-NodeRemover::NodeRemover(const set<Node *> &r):
+NodeRemover::NodeRemover():
        stage(0),
-       to_remove(r)
+       to_remove(0)
 { }
 
-void NodeRemover::apply(Stage &s)
+void NodeRemover::apply(Stage &s, const set<Node *> &tr)
 {
        stage = &s;
+       to_remove = &tr;
        visit(s.content);
 }
 
@@ -162,7 +163,7 @@ void NodeRemover::visit(Block &block)
        for(NodeList<Statement>::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);
 }
index 756584c2e262289783a6f58ec75cbfc2c6ec5a46..8bfcac0fd490b85bb2db1c32dfca371a4e786363 100644 (file)
@@ -89,7 +89,7 @@ private:
        std::vector<T *> nodes;
 
 public:
-       const std::vector<T *> &apply(Stage &s) { visit(s.content); return nodes; }
+       const std::vector<T *> &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<Node *> to_remove;
+       const std::set<Node *> *to_remove;
 
 public:
-       NodeRemover(const std::set<Node *> &);
+       NodeRemover();
 
-       void apply(Stage &);
+       void apply(Stage &, const std::set<Node *> &);
 
 private:
        using TraversingVisitor::visit;