]> git.tdb.fi Git - libs/gl.git/commitdiff
Process loop initialization outside the body in UnusedVariableRemover
authorMikko Rasa <tdb@tdb.fi>
Mon, 8 Nov 2021 00:01:26 +0000 (02:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 8 Nov 2021 00:01:26 +0000 (02:01 +0200)
If it's processed inside the scope of the loop, assignments to the
iteration variable may get discarded.

source/glsl/optimize.cpp
tests/glsl/complex_loop_increment.glsl [new file with mode: 0644]

index 3cb10e862458903a0d07c2da1eec7465e3299ac5..8008465e91344642f76e4fb66ae4a135e8bce66f 100644 (file)
@@ -1574,8 +1574,14 @@ void UnusedVariableRemover::visit(Iteration &iter)
        vector<Node *> saved_refs;
        swap(loop_ext_refs, saved_refs);
        {
+               if(iter.init_statement)
+                       iter.init_statement->visit(*this);
                SetForScope<unsigned> 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 (file)
index 0000000..fa0ea86
--- /dev/null
@@ -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);
+}
+*/