From a204b5fac497e2ffbc6791f0f7de9d9d12c16d52 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 5 Mar 2021 01:27:52 +0200 Subject: [PATCH] Remove unused type declarations from GLSL during optimization The biggest impact of this right now is keeping the AST dump manageable. --- source/glsl/compiler.cpp | 1 + source/glsl/optimize.cpp | 34 ++++++++++++++++++++++++++++++++-- source/glsl/optimize.h | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 0f608d6d..172bdac6 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -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; } diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 137bd943..88a32399 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -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 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); } diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index c932767e..85f11780 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -165,6 +165,22 @@ private: virtual void visit(Iteration &); }; +/** Removes types which are not used anywhere. */ +class UnusedTypeRemover: private TraversingVisitor +{ +private: + std::set 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 -- 2.43.0