]> git.tdb.fi Git - libs/gl.git/commitdiff
Remove unused type declarations from GLSL during optimization
authorMikko Rasa <tdb@tdb.fi>
Thu, 4 Mar 2021 23:27:52 +0000 (01:27 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 23:00:31 +0000 (01:00 +0200)
The biggest impact of this right now is keeping the AST dump manageable.

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

index 0f608d6d600313682577df96a3c7e2cccd2e5226..172bdac629251d8ecd3ab49a9c75cec394dd9d2e 100644 (file)
@@ -286,6 +286,7 @@ Compiler::OptimizeResult Compiler::optimize(Stage &stage)
        to become unused. */
        bool any_removed = UnusedVariableRemover().apply(stage);
        any_removed |= UnusedFunctionRemover().apply(stage);
+       any_removed |= UnusedTypeRemover().apply(stage);
 
        return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
 }
index 137bd94343190f275eebbaa877dfa554b4785a50..88a323992636b0642d05d4134ee81d68bdebce9c 100644 (file)
@@ -622,6 +622,38 @@ UnusedVariableRemover::VariableInfo::VariableInfo():
 { }
 
 
+bool UnusedTypeRemover::apply(Stage &stage)
+{
+       stage.content.visit(*this);
+       NodeRemover().apply(stage, unused_nodes);
+       return !unused_nodes.empty();
+}
+
+void UnusedTypeRemover::visit(BasicTypeDeclaration &type)
+{
+       if(type.base_type)
+               unused_nodes.erase(type.base_type);
+       unused_nodes.insert(&type);
+}
+
+void UnusedTypeRemover::visit(ImageTypeDeclaration &type)
+{
+       if(type.base_type)
+               unused_nodes.erase(type.base_type);
+       unused_nodes.insert(&type);
+}
+
+void UnusedTypeRemover::visit(StructDeclaration &strct)
+{
+       unused_nodes.insert(&strct);
+}
+
+void UnusedTypeRemover::visit(VariableDeclaration &var)
+{
+       unused_nodes.erase(var.type_declaration);
+}
+
+
 UnusedVariableRemover::UnusedVariableRemover():
        aggregate(0),
        r_assignment(0),
@@ -760,7 +792,6 @@ void UnusedVariableRemover::visit(ExpressionStatement &expr)
 void UnusedVariableRemover::visit(StructDeclaration &strct)
 {
        SetForScope<Node *> set(aggregate, &strct);
-       unused_nodes.insert(&strct);
        TraversingVisitor::visit(strct);
 }
 
@@ -774,7 +805,6 @@ void UnusedVariableRemover::visit(VariableDeclaration &var)
                if(var.init_expression)
                        record_assignment(var, *var.init_expression, false);
        }
-       unused_nodes.erase(var.type_declaration);
        TraversingVisitor::visit(var);
 }
 
index c932767e99807325f159372e626895aafa213c5b..85f11780baf45a8b77401aecccee255215a0bb2f 100644 (file)
@@ -165,6 +165,22 @@ private:
        virtual void visit(Iteration &);
 };
 
+/** Removes types which are not used anywhere. */
+class UnusedTypeRemover: private TraversingVisitor
+{
+private:
+       std::set<Node *> unused_nodes;
+
+public:
+       bool apply(Stage &);
+
+private:
+       virtual void visit(BasicTypeDeclaration &);
+       virtual void visit(ImageTypeDeclaration &);
+       virtual void visit(StructDeclaration &);
+       virtual void visit(VariableDeclaration &);
+};
+
 /** Removes variable declarations with no references to them.  Assignment
 statements where the result is not used are also removed. */
 class UnusedVariableRemover: private TraversingVisitor