From ced18c244f874638dc17ae486802bb3bb01ecf71 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 7 Dec 2016 02:31:45 +0200 Subject: [PATCH] Recognize backward references in GLSL loops A lone assignment at the end of a loop isn't unused if the next iteration uses the value. --- source/programcompiler.cpp | 16 +++++++++++++--- source/programcompiler.h | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index c7da6c55..4e4b4814 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -1212,7 +1212,8 @@ void ProgramCompiler::ConstantConditionEliminator::visit(Iteration &iter) ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): aggregate(0), assignment(0), - assignment_target(false) + assignment_target(false), + iteration(false) { } void ProgramCompiler::UnusedVariableLocator::apply(Stage &s) @@ -1233,6 +1234,9 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) if(assignment_target) return; + if(iteration) + used_in_iteration.insert(var.declaration); + for(vector::iterator j=assignments.end(); j!=assignments.begin(); ) { --j; @@ -1277,7 +1281,8 @@ void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign) void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool self_ref) { - unused_nodes.insert(&node); + if(!used_in_iteration.count(&var)) + unused_nodes.insert(&node); BlockAssignmentMap &block_assignments = assignments.back(); AssignmentList &var_assignments = block_assignments[&var]; if(!self_ref) @@ -1404,8 +1409,13 @@ void ProgramCompiler::UnusedVariableLocator::visit(Conditional &cond) void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter) { assignments.push_back(BlockAssignmentMap()); - TraversingVisitor::visit(iter); + { + SetFlag set(iteration); + TraversingVisitor::visit(iter); + } merge_down_assignments(); + if(!iteration) + used_in_iteration.clear(); } diff --git a/source/programcompiler.h b/source/programcompiler.h index e43b3a2d..db63eb36 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -262,6 +262,8 @@ private: std::vector assignments; ProgramSyntax::Assignment *assignment; bool assignment_target; + bool iteration; + std::set used_in_iteration; UnusedVariableLocator(); -- 2.45.2