[Mesa-dev] glsl2: optimizing unused struct assignments

Ian Romanick idr at freedesktop.org
Tue Aug 3 08:45:02 PDT 2010

Hash: SHA1

Aras Pranckevicius wrote:
> Hi,
> Currently GLSL2 optimizer can't remove assignments to struct members
> that are never used. After inlining, the struct is often not passed to
> any other functions as a whole; and some assignments to the members
> might be useless. For example, in this fragment shader assignment to
> s.unused could be optimized away. I guess then whole structure (of which
> only s.used is left) could be replaced with just a float temporary:
> struct foo {
>     float used;
>     float unused;
> };
> void main() {
>     float f = 1.0;
>     foo s;
>     s.used = f;
>     s.unused = f;
>     gl_FragColor = vec4(s.used);
> }
> Right now GLSL2 optimizer optimizes the above into this (GLSL output):
> struct foo {
>   float used;
>   float unused;
> };
> void main ()
> {
>   foo s;
>   s .used  = 1.000000; 
>   s .unused  = 1.000000; 
>   gl_FragColor  = s .used .xxxx; 
> }
> From the code, it seems that ir_variable_refcount only tracks references
> to "whole variables" (in this case, whole struct), and not to individual
> members. Any quick ideas / pitfalls how that can be extended, before I
> try to figure it out myself?

I believe the plan is to eventually break structures and arrays that are
not variably indexed into individual variables.  Your structure above
would be broken into s_used and s_unused.  The existing dead code paths
would take care of s_unused.  We'll need to do this break-up anyway to
do proper register assignment.

We'll have similar issue with code like:

uniform vec4 angles;
void main()
   vec4 v;

   v.x = sin(angles.x);
   v.y = cos(angles.y);
   v.z = tan(angles.z);
   v.w = 1/tan(angles.w);

   gl_Position = v.xyxy;

In this case v.zw is never used, and the (expensive) assignments should
be killed.
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/


More information about the mesa-dev mailing list