[Mesa-dev] [PATCH v2] glsl: Fix handling of array dereferences of vectors in opt_dead_code_local

Ian Romanick idr at freedesktop.org
Wed Feb 20 13:18:09 PST 2013


On 02/11/2013 04:31 PM, Eric Anholt wrote:
> Ian Romanick <idr at freedesktop.org> writes:
>
>> From: Ian Romanick <ian.d.romanick at intel.com>
>>
>> Three parts to the fix:
>>
>> 1. If the array dereference is a constant index, figure out the real
>> write mask.
>>
>> 2. If the array dereference not is a constant index, assume the
>> assignment may generate any component.
>>
>> 3. If the array dereference not is a constant index, assume the
>> assigment will not kill any previous component write.
>>
>> v2: Fix accidental setting of debug flag (noticed by Aras
>> Pranckevičius).  Fix idiotic typo added after last build.
>
> OK, here's the testcase I came up with today:
>
> vec4 v;
> v.xyzw = vec4(1, 2, 3, 4);
>     entry.ir.writemask == xyzw
>     entry.available = xyzw
> gl_FragColor = vec4(v.xyz, 1);
>     entry.ir.writemask = xyzw,
>     entry.available = w
> v.zw = vec2(2.0);
>     entry.ir.writemask = xy;
>     entry.available = 0;
>     So now we delete it!
>
> Yeah, this code could have used more comments.

I made a shader_runner test out of this, and it passes before and after 
my patch.  Your original case doesn't use variable indexing of the 
vector, so my patch wouldn't have any affect on it.  Right?

I tried a couple other variations, and I couldn't get it to fail.

Here's the debug output from the first pass of the optimization loop 
from this opt_dead_code_local:

(declare () vec4 tmp)
(declare (temporary ) vec4 vec_ctor)
(assign  (w) (var_ref vec_ctor)  (constant float (4.000000)) )
looking for vec_ctor.0x8 to remove
add vec_ctor
current entries
     vec_ctor (0x8)
(assign  (xyz) (var_ref vec_ctor)  (swiz xyz (var_ref vertex) ))
looking for vec_ctor.0x7 to remove
vec_ctor 0x8 - 0x0 = 0x8
add vec_ctor
current entries
     vec_ctor (0x8)
     vec_ctor (0x7)
(declare (temporary ) vec4 assignment_tmp)
(assign  (xyzw) (var_ref tmp)  (var_ref vec_ctor) )
kill vec_ctor (0x8 - 0xffffffff)
kill vec_ctor (0x7 - 0xffffffff)
looking for tmp.0xf to remove
add tmp
current entries
     tmp (0xf)
(declare (temporary ) vec4 vec_ctor)
(assign  (w) (var_ref vec_ctor)  (constant float (1.000000)) )
looking for vec_ctor.0x8 to remove
add vec_ctor
current entries
     tmp (0xf)
     vec_ctor (0x8)
(assign  (xyz) (var_ref vec_ctor)  (swiz xyz (swiz xyz (var_ref 
vec_ctor at 2) )))
looking for vec_ctor.0x7 to remove
vec_ctor 0x8 - 0x0 = 0x8
add vec_ctor
current entries
     tmp (0xf)
     vec_ctor (0x8)
     vec_ctor (0x7)
(declare (temporary ) vec4 assignment_tmp)
(assign  (xyzw) (var_ref assignment_tmp)  (expression vec4 * (var_ref 
mvp) (var_ref vec_ctor) ) )
kill vec_ctor (0x8 - 0xffffffff)
kill vec_ctor (0x7 - 0xffffffff)
looking for assignment_tmp.0xf to remove
add assignment_tmp
current entries
     tmp (0xf)
     assignment_tmp (0xf)
(assign  (xyzw) (var_ref gl_Position)  (var_ref assignment_tmp) )
kill assignment_tmp (0xf - 0xffffffff)
looking for gl_Position.0xf to remove
add gl_Position
current entries
     tmp (0xf)
     gl_Position (0xf)
(declare (temporary ) float assignment_tmp)
(assign  (x) (var_ref assignment_tmp)  (constant float (1.000000)) )
looking for assignment_tmp.0x1 to remove
add assignment_tmp
current entries
     tmp (0xf)
     gl_Position (0xf)
     assignment_tmp (0x1)
(assign  (x) (array_ref (var_ref tmp) (expression int + (var_ref i) 
(expression int neg (constant int (1)) ) ) )  (var_ref assignment_tmp) )
kill assignment_tmp (0x1 - 0xffffffff)
add tmp
current entries
     tmp (0xf)
     gl_Position (0xf)
     tmp (0xf)
(declare (temporary ) float assignment_tmp)
(assign  (x) (var_ref assignment_tmp)  (constant float (0.000000)) )
looking for assignment_tmp.0x1 to remove
add assignment_tmp
current entries
     tmp (0xf)
     gl_Position (0xf)
     tmp (0xf)
     assignment_tmp (0x1)
(assign  (x) (array_ref (var_ref tmp) (var_ref i) )  (var_ref 
assignment_tmp) )
kill assignment_tmp (0x1 - 0xffffffff)
add tmp
current entries
     tmp (0xf)
     gl_Position (0xf)
     tmp (0xf)
     tmp (0xf)
(declare (temporary ) vec4 assignment_tmp)
(assign  (xyzw) (var_ref assignment_tmp)  (swiz wzwz (var_ref tmp) ))
kill tmp (0xf - 0xffffffff)
kill tmp (0xf - 0xffffffff)
kill tmp (0xf - 0xffffffff)
looking for assignment_tmp.0xf to remove
add assignment_tmp
current entries
     gl_Position (0xf)
     assignment_tmp (0xf)
(assign  (xyzw) (var_ref color)  (var_ref assignment_tmp) )
kill assignment_tmp (0xf - 0xffffffff)
looking for color.0xf to remove
add color
current entries
     gl_Position (0xf)
     color (0xf)

-------------- next part --------------
[require]
GLSL >= 1.20

[vertex shader]
#version 120
attribute vec3 vertex;
uniform mat4 mvp = mat4(1.);
uniform int i = 3;
varying vec4 color;

void main()
{
    vec4 tmp;

    tmp = vec4(vertex, 4);
    gl_Position = mvp * vec4(tmp.xyz, 1);

    tmp[i-1] = 1.;
    tmp[i] = 0.;
    color = tmp.wzwz;
}

[fragment shader]
varying vec4 color;
void main()
{
    gl_FragColor = color;
}

[vertex data]
vertex/float/3
 1.0  1.0  1.0
-1.0  1.0  1.0
-1.0 -1.0  1.0
 1.0 -1.0  1.0

[test]
draw arrays GL_TRIANGLE_FAN 0 4
relative probe rgb (.5, .5) (0.0, 1.0, 0.0)


More information about the mesa-dev mailing list