]> git.tdb.fi Git - libs/gl.git/commitdiff
Track composite assignments and references in UnusedVariableRemover
authorMikko Rasa <tdb@tdb.fi>
Mon, 5 Apr 2021 13:46:11 +0000 (16:46 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 5 Apr 2021 13:46:11 +0000 (16:46 +0300)
source/glsl/optimize.cpp
source/glsl/optimize.h
tests/glsl/unused_component_assignment.glsl [new file with mode: 0644]

index 57182019012b731ba330e7fc38a1e86f1f2eeeaf..927047cc7c1e387890bf4a3d1a4a784e2039c7ac 100644 (file)
@@ -963,7 +963,8 @@ UnusedVariableRemover::UnusedVariableRemover():
        interface_block(0),
        r_assignment(0),
        assignment_target(false),
-       r_side_effects(false)
+       r_side_effects(false),
+       composite_reference(false)
 { }
 
 bool UnusedVariableRemover::apply(Stage &s)
@@ -1008,18 +1009,80 @@ void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &n
        if(!assignment_target)
        {
                for(vector<AssignmentInfo *>::const_iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
-                       (*i)->used_by.push_back(&node);
+               {
+                       bool covered = true;
+                       for(unsigned j=0; (covered && j<(*i)->target.chain_len && j<target.chain_len); ++j)
+                       {
+                               Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>((*i)->target.chain[j]&0xC0);
+                               Assignment::Target::ChainType type2 = static_cast<Assignment::Target::ChainType>(target.chain[j]&0xC0);
+                               if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE)
+                               {
+                                       unsigned index1 = (*i)->target.chain[j]&0x3F;
+                                       unsigned index2 = target.chain[j]&0x3F;
+                                       if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE)
+                                               covered = index1&index2;
+                                       else if(type1==Assignment::Target::ARRAY && index1<4)
+                                               covered = index2&(1<<index1);
+                                       else if(type2==Assignment::Target::ARRAY && index2<4)
+                                               covered = index1&(1<<index2);
+                                       /* If it's some other combination (shouldn't happen), leave
+                                       covered as true */
+                               }
+                               else
+                                       covered = ((*i)->target.chain[j]==target.chain[j]);
+                       }
+                       if(covered)
+                               (*i)->used_by.push_back(&node);
+               }
        }
 }
 
 void UnusedVariableRemover::visit(VariableReference &var)
 {
-       referenced(var.declaration, var);
+       if(composite_reference)
+               r_reference.declaration = var.declaration;
+       else
+               referenced(var.declaration, var);
 }
 
 void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
 {
-       referenced(iface.declaration, iface);
+       if(composite_reference)
+               r_reference.declaration = iface.declaration;
+       else
+               referenced(iface.declaration, iface);
+}
+
+void UnusedVariableRemover::visit_composite(Expression &expr)
+{
+       if(!composite_reference)
+               r_reference = Assignment::Target();
+
+       SetFlag set_composite(composite_reference);
+       expr.visit(*this);
+}
+
+void UnusedVariableRemover::visit(MemberAccess &memacc)
+{
+       visit_composite(*memacc.left);
+
+       add_to_chain(r_reference, Assignment::Target::MEMBER, memacc.index);
+
+       if(!composite_reference && r_reference.declaration)
+               referenced(r_reference, memacc);
+}
+
+void UnusedVariableRemover::visit(Swizzle &swizzle)
+{
+       visit_composite(*swizzle.left);
+
+       unsigned mask = 0;
+       for(unsigned i=0; i<swizzle.count; ++i)
+               mask |= 1<<swizzle.components[i];
+       add_to_chain(r_reference, Assignment::Target::SWIZZLE, mask);
+
+       if(!composite_reference && r_reference.declaration)
+               referenced(r_reference, swizzle);
 }
 
 void UnusedVariableRemover::visit(UnaryExpression &unary)
@@ -1033,12 +1096,30 @@ void UnusedVariableRemover::visit(BinaryExpression &binary)
 {
        if(binary.oper->token[0]=='[')
        {
-               binary.left->visit(*this);
-               SetFlag set(assignment_target, false);
-               binary.right->visit(*this);
+               visit_composite(*binary.left);
+
+               {
+                       SetFlag clear_assignment(assignment_target, false);
+                       SetFlag clear_composite(composite_reference, false);
+                       binary.right->visit(*this);
+               }
+
+               add_to_chain(r_reference, Assignment::Target::ARRAY, 0x3F);
+
+               if(!composite_reference && r_reference.declaration)
+                       referenced(r_reference, binary);
        }
        else
+       {
+               SetFlag clear_composite(composite_reference, false);
                TraversingVisitor::visit(binary);
+       }
+}
+
+void UnusedVariableRemover::visit(TernaryExpression &ternary)
+{
+       SetFlag clear_composite(composite_reference, false);
+       TraversingVisitor::visit(ternary);
 }
 
 void UnusedVariableRemover::visit(Assignment &assign)
@@ -1054,6 +1135,7 @@ void UnusedVariableRemover::visit(Assignment &assign)
 
 void UnusedVariableRemover::visit(FunctionCall &call)
 {
+       SetFlag clear_composite(composite_reference, false);
        TraversingVisitor::visit(call);
        /* Treat function calls as having side effects so expression statements
        consisting of nothing but a function call won't be optimized away. */
index 1504acb224e27dd5290947e9700fa657750ba433..249a859a03763c6e198b9c0f04020dc0589ea360 100644 (file)
@@ -280,6 +280,8 @@ private:
        Assignment *r_assignment;
        bool assignment_target;
        bool r_side_effects;
+       bool composite_reference;
+       Assignment::Target r_reference;
        std::set<Node *> unused_nodes;
 
 public:
@@ -291,9 +293,13 @@ private:
        void referenced(const Assignment::Target &, Node &);
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
+       void visit_composite(Expression &);
+       virtual void visit(MemberAccess &);
+       virtual void visit(Swizzle &);
        virtual void visit(UnaryExpression &);
        virtual void visit(BinaryExpression &);
        virtual void visit(Assignment &);
+       virtual void visit(TernaryExpression &);
        virtual void visit(FunctionCall &);
        void record_assignment(const Assignment::Target &, Node &);
        virtual void visit(ExpressionStatement &);
diff --git a/tests/glsl/unused_component_assignment.glsl b/tests/glsl/unused_component_assignment.glsl
new file mode 100644 (file)
index 0000000..0febd56
--- /dev/null
@@ -0,0 +1,33 @@
+uniform mat4 mvp;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+int main()
+{
+       vec4 c0 = mvp[0]*position;
+       vec4 c1 = mvp[1]*position;
+       vec4 c2 = mvp[2]*position;
+       vec4 c3 = mvp[3]*position;
+       vec4 clip_pos;
+       clip_pos.x = c0.x+c1.x+c2.x+c3.x;
+       clip_pos.y = c0.y+c1.y+c2.y+c3.y;
+       clip_pos.z = c0.z+c1.z+c2.z+c3.z;
+       clip_pos.w = c0.w+c1.w+c2.w+c3.w;
+       gl_Position = vec4(clip_pos.xy, 0.0, 1.0);
+}
+
+/* Expected output: vertex
+layout(location=0) uniform mat4 mvp;
+layout(location=0) in vec4 position;
+int main()
+{
+  vec4 c0 = mvp[0]*position;
+  vec4 c1 = mvp[1]*position;
+  vec4 c2 = mvp[2]*position;
+  vec4 c3 = mvp[3]*position;
+  vec4 clip_pos;
+  clip_pos.x = c0.x+c1.x+c2.x+c3.x;
+  clip_pos.y = c0.y+c1.y+c2.y+c3.y;
+  gl_Position = vec4(clip_pos.xy, 0.0, 1.0);
+}
+*/