From 241cf36a6d7735706804bb3c517529bbe078f1ee Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 8 Nov 2021 02:01:26 +0200 Subject: [PATCH] Process loop initialization outside the body in UnusedVariableRemover If it's processed inside the scope of the loop, assignments to the iteration variable may get discarded. --- source/glsl/optimize.cpp | 8 ++++++- tests/glsl/complex_loop_increment.glsl | 30 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/glsl/complex_loop_increment.glsl diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 3cb10e86..8008465e 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -1574,8 +1574,14 @@ void UnusedVariableRemover::visit(Iteration &iter) vector saved_refs; swap(loop_ext_refs, saved_refs); { + if(iter.init_statement) + iter.init_statement->visit(*this); SetForScope set_loop(in_loop, in_loop+1); - TraversingVisitor::visit(iter); + if(iter.condition) + iter.condition->visit(*this); + iter.body.visit(*this); + if(iter.loop_expression) + iter.loop_expression->visit(*this); } swap(loop_ext_refs, saved_refs); diff --git a/tests/glsl/complex_loop_increment.glsl b/tests/glsl/complex_loop_increment.glsl new file mode 100644 index 00000000..fa0ea86e --- /dev/null +++ b/tests/glsl/complex_loop_increment.glsl @@ -0,0 +1,30 @@ +#pragma MSP stage(vertex) +void main() +{ + int n = 0; + for(int i=10; i!=1; ) + { + if((i&1)==0) + i /= 2; + else + i = 3*i+1; + ++n; + } + gl_Position = vec4(n); +} + +/* Expected output: vertex +void main() +{ + int n = 0; + for(int i = 10; i!=1;) + { + if((i&1)==0) + i /= 2; + else + i = 3*i+1; + ++n; + } + gl_Position = vec4(0.0); +} +*/ -- 2.43.0