Skip to content

Commit

Permalink
Fix reuseExistingVarDecl trigger condition (#463)
Browse files Browse the repository at this point in the history
* reuseExistingVarDecl can inhibit other optimizations, it should only
happen in the second pass.
* reuseExistingVarDecl seems safe to use even when not at the root
level.
  • Loading branch information
eldritchconundrum authored Sep 29, 2024
1 parent cfeae09 commit 660f8bb
Show file tree
Hide file tree
Showing 21 changed files with 203 additions and 209 deletions.
5 changes: 2 additions & 3 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ type private RewriterImpl(options: Options.Options, optimizationPass: Optimizati
// Reuse an existing local variable declaration that won't be used anymore, instead of introducing a new one.
// The reused identifier gets compressed better, and the declaration is sometimes removed.
// float d1 = f(); float d2 = g(); -> float d1 = f(); d1 = g();
// We only do this when the reused declaration is at the same level as the removed declaration.
let reuseExistingVarDecl blockLevel b =
let tryReplaceWithPrecedingAndFollowing f (xs : Stmt list) =
let rec go f preceding = function
Expand Down Expand Up @@ -801,9 +802,7 @@ type private RewriterImpl(options: Options.Options, optimizationPass: Optimizati
else removeUnusedAssignments blockLevel b

let b =
if optimizationPass <> OptimizationPass.Second &&
// We ensure control flow analysis is trivial by only doing this at the root block level.
(match blockLevel with BlockLevel.FunctionRoot _ -> false | _ -> true) || hasPreprocessor
if optimizationPass <> OptimizationPass.Second || hasPreprocessor
then b
else reuseExistingVarDecl blockLevel b

Expand Down
22 changes: 11 additions & 11 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
clod.frag (and others) 8696 => 1484.471
mouton/mouton.vert (and others) 16569 => 2405.061
audio-flight-v2.frag 4471 => 874.321
clod.frag (and others) 8692 => 1483.915
mouton/mouton.vert (and others) 16569 => 2403.743
audio-flight-v2.frag 4469 => 873.544
buoy.frag 3977 => 597.317
controllable-machinery.frag 7671 => 1220.647
ed-209.frag 7665 => 1345.543
ed-209.frag 7661 => 1340.206
elevated.hlsl 3405 => 604.101
endeavour.frag 2567 => 530.460
from-the-seas-to-the-stars.frag 14173 => 2278.764
frozen-wasteland.frag 4455 => 791.550
kinder_painter.frag 2832 => 442.132
leizex.frag 2252 => 506.309
lunaquatic.frag 5227 => 1044.673
mandelbulb.frag 2325 => 539.673
lunaquatic.frag 5223 => 1047.141
mandelbulb.frag 2309 => 533.408
ohanami.frag 3179 => 703.261
orchard.frag 5384 => 1002.066
orchard.frag 5380 => 1000.024
oscars_chair.frag 4648 => 986.069
robin.frag 6188 => 1038.630
slisesix.frag 4433 => 884.925
robin.frag 6183 => 1038.390
slisesix.frag 4429 => 890.365
terrarium.frag 3571 => 744.738
the_real_party_is_in_your_pocket.frag 11966 => 1765.100
valley_ball.glsl 4307 => 881.820
yx_long_way_from_home.frag 2901 => 592.799
Total: 132862 => 23264.432
yx_long_way_from_home.frag 2885 => 586.201
Total: 132803 => 23249.205
22 changes: 11 additions & 11 deletions tests/real/audio-flight-v2.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ vec2 map(vec3 p,float sg)
float obj=fBox(o,vec3(.15*offWobble))-.015;
if(obj<res.x)
res=vec2(obj,11),g_hp=o;
float pid=mod(floor((q.z+3.625)/7.25),18.);
pid=pid<12.?
pid<6.?
float pid=floor((q.z+3.625)/7.25),trackmod=mod(pid,18.);
pid=trackmod<12.?
trackmod<6.?
4.:
6.:
10.;
Expand All @@ -130,8 +130,8 @@ vec2 map(vec3 p,float sg)
tun=floor(fs.xz)+.5;
vec2 neighbour=tun+edge(fs.xz-tun);
pid=floor(tun.y);
float height=smoothstep(.001,1.,texture(iChannel0,vec2(mod(pid,16.)*.0465,.1)).x);
ga=height;
trackmod=smoothstep(.001,1.,texture(iChannel0,vec2(mod(pid,16.)*.0465,.1)).x);
ga=trackmod;
float ids=pMod(s.z,7.25);
vec3 qid=pMod(q,7.25);
ids=mod(ids,2.);
Expand All @@ -142,7 +142,7 @@ vec2 map(vec3 p,float sg)
2.:
2.:
5.:
floor(height*1.45));
floor(trackmod*1.45));
float d4a=length(o.xy-vec2(2.5,1.75))-.1,d4=length(o.xy-vec2(2.5,1.75))-.04+.027+.027*sin(o.z-time*4.5);
if(d4<res.x)
res=vec2(d4,12),g_hp=p;
Expand All @@ -154,16 +154,16 @@ vec2 map(vec3 p,float sg)
d1.x=min(d4a,d1.x);
if(d1.x<res.x)
res=d1.xy,g_hp=p;
float me=fBox(fs-vec3(0,0,tun.y),vec3(.05,.15+height,.25)),next=fBox(fs-vec3(0,0,neighbour.y),vec3(.05,.001+height,.25));
height=min(me,next);
if(height<res.x)
res=vec2(height,4),g_hp=p;
float me=fBox(fs-vec3(0,0,tun.y),vec3(.05,.15+trackmod,.25)),next=fBox(fs-vec3(0,0,neighbour.y),vec3(.05,.001+trackmod,.25));
trackmod=min(me,next);
if(trackmod<res.x)
res=vec2(trackmod,4),g_hp=p;
if(sg==1.)
beams+=1e-4/(3e-6+d4*d4);
if(sg==1.&&ids<1.)
flight+=.00025/(1e-7+d4a*d4a);
if(sg==1.&&pid<.1)
glow+=.00015/(2e-6+height*height);
glow+=.00015/(2e-6+trackmod*trackmod);
if(sg==1.&&tm<13.25)
objglow+=5e-4/(5e-4+obj*obj);
return res;
Expand Down
21 changes: 10 additions & 11 deletions tests/real/buoy.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ vec3 ShadeBoat(vec3 pos,vec3 ray)
pos=vec3(dot(pos,boatRight),dot(pos,boatUp),dot(pos,boatForward));
vec3 lightDir=normalize(vec3(-2,3,1));
float aa=4./iResolution.x;
pos=mix(vec3(1,.8,.08),mix(vec3(.04),mix(mix(vec3(1),vec3(.04),smoothstep(-aa*4.,aa*4.,cos(atan(pos.x,pos.z)*6.))),mix(vec3(.04),vec3(1,.01,0),smoothstep(.25-aa,.25,abs(pos.y))),smoothstep(.2-aa*1.5,.2,abs(pos.y))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.6))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.65)))*(smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1));
lightDir=pow(max(0.,dot(norm,normalize(lightDir-ray))),1e2)*1e2/32.*vec3(1);
vec3 rr=reflect(ray,norm);
lightDir+=mix(vec3(0,.04,.04),Sky(rr),smoothstep(-.1,.1,rr.y));
return mix(pos,lightDir,mix(.001,1.,pow(1.-abs(dot(norm,ray)),5.)));
pos=mix(vec3(1,.8,.08),mix(vec3(.04),mix(mix(vec3(1),vec3(.04),smoothstep(-aa*4.,aa*4.,cos(atan(pos.x,pos.z)*6.))),mix(vec3(.04),vec3(1,.01,0),smoothstep(.25-aa,.25,abs(pos.y))),smoothstep(.2-aa*1.5,.2,abs(pos.y))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.6))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.65)));
vec3 specular=pow(max(0.,dot(norm,normalize(lightDir-ray))),1e2)*1e2/32.*vec3(1),rr=reflect(ray,norm);
specular+=mix(vec3(0,.04,.04),Sky(rr),smoothstep(-.1,.1,rr.y));
return mix(pos*(smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1)),specular,mix(.001,1.,pow(1.-abs(dot(norm,ray)),5.)));
}
float OceanDistanceField(vec3 pos)
{
Expand Down Expand Up @@ -173,16 +172,16 @@ vec3 ShadeOcean(vec3 pos,vec3 ray,vec2 fragCoord)
vec3 norm=OceanNormal(pos);
float ndotr=dot(ray,norm);
vec3 reflectedRay=ray-2.*norm*ndotr;
norm=normalize(ray+(-cos(1.33*acos(-ndotr))-ndotr)*norm);
ray=Sky(reflectedRay);
ray=normalize(ray+(-cos(1.33*acos(-ndotr))-ndotr)*norm);
norm=Sky(reflectedRay);
float t=TraceBoat(pos-0.*reflectedRay,reflectedRay);
if(t>0.)
ray=ShadeBoat(pos+t*reflectedRay,reflectedRay);
t=TraceBoat(pos-0.*norm,norm);
norm=ShadeBoat(pos+t*reflectedRay,reflectedRay);
t=TraceBoat(pos-0.*ray,ray);
reflectedRay=vec3(0,.04,.04);
if(t>0.)
reflectedRay=mix(reflectedRay,ShadeBoat(pos+t*norm,norm),exp(-t));
reflectedRay=mix(reflectedRay,ray,pow(1.-abs(ndotr),5.));
reflectedRay=mix(reflectedRay,ShadeBoat(pos+t*ray,ray),exp(-t));
reflectedRay=mix(reflectedRay,norm,pow(1.-abs(ndotr),5.));
return mix(reflectedRay,vec3(1),WaveCrests(pos,fragCoord));
}
void mainImage(out vec4 fragColor,vec2 fragCoord)
Expand Down
10 changes: 5 additions & 5 deletions tests/real/ed-209.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ float sdTriPrism(vec3 p)
float sdCappedCone(vec3 p)
{
vec3 b=vec3(0,0,-.1),a=vec3(0);
float rba=.1,baba=dot(b-a,b-a),paba=dot(p-a,b-a)/baba,x=sqrt(dot(p-a,p-a)-paba*paba*baba),cax=max(0.,x-(paba<.5?
float baba=dot(b-a,b-a),paba=dot(p-a,b-a)/baba,x=sqrt(dot(p-a,p-a)-paba*paba*baba),cax=max(0.,x-(paba<.5?
.25:
.35)),cay=abs(paba-.5)-.5,f=clamp((rba*(x-.25)+paba*baba)/(rba*rba+baba),0.,1.);
x=x-.25-f*rba;
rba=paba-f;
.35)),cay=abs(paba-.5)-.5,f=clamp((.1*(x-.25)+paba*baba)/(.01+baba),0.,1.);
x=x-.25-f*.1;
paba-=f;
return(x<0.&&cay<0.?
-1.:
1.)*sqrt(min(cax*cax+cay*cay*baba,x*x+rba*rba*baba));
1.)*sqrt(min(cax*cax+cay*cay*baba,x*x+paba*paba*baba));
}
float sdCappedCylinder(vec3 p,float h,float r)
{
Expand Down
14 changes: 7 additions & 7 deletions tests/real/heart.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub const HEART_FRAG: &'static [u8] = b"\
void main()\
{\
vec2 f=(2.*gl_FragCoord.xy-y)/y.y;\
float u=mod(v,2.)/2.,a=pow(u,.2)*.5+.5;\
a-=a*.2*sin(u*6.2831*5.)*exp(-u*6.);\
float g=mod(v,2.)/2.,a=pow(g,.2)*.5+.5;\
a-=a*.2*sin(g*6.2831*5.)*exp(-g*6.);\
f*=vec2(.5,1.5)+a*vec2(.5,-.5);\
a=atan(f.x,f.y)/3.141593;\
u=length(f);\
a=abs(a);\
a=(13.*a-22.*a*a+10.*a*a*a)/(6.-5.*a);\
gl_FragColor=vec4(step(u,a)*pow(1.-u/a,.25),0,0,1);\
g=atan(f.x,f.y)/3.141593;\
a=length(f);\
g=abs(g);\
g=(13.*g-22.*g*g+10.*g*g*g)/(6.-5.*g);\
gl_FragColor=vec4(step(a,g)*pow(1.-a/g,.25),0,0,1);\
}\0";
14 changes: 7 additions & 7 deletions tests/real/heart.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const char *heart_frag =
"void main()"
"{"
"vec2 f=(2.*gl_FragCoord.xy-y)/y.y;"
"float u=mod(v,2.)/2.,a=pow(u,.2)*.5+.5;"
"a-=a*.2*sin(u*6.2831*5.)*exp(-u*6.);"
"float g=mod(v,2.)/2.,a=pow(g,.2)*.5+.5;"
"a-=a*.2*sin(g*6.2831*5.)*exp(-g*6.);"
"f*=vec2(.5,1.5)+a*vec2(.5,-.5);"
"a=atan(f.x,f.y)/3.141593;"
"u=length(f);"
"a=abs(a);"
"a=(13.*a-22.*a*a+10.*a*a*a)/(6.-5.*a);"
"gl_FragColor=vec4(step(u,a)*pow(1.-u/a,.25),0,0,1);"
"g=atan(f.x,f.y)/3.141593;"
"a=length(f);"
"g=abs(g);"
"g=(13.*g-22.*g*g+10.*g*g*g)/(6.-5.*g);"
"gl_FragColor=vec4(step(a,g)*pow(1.-a/g,.25),0,0,1);"
"}";

#endif // HEART_FRAG_EXPECTED_
26 changes: 13 additions & 13 deletions tests/real/kinder_painter.expected
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@
"b[3]=vec4(.5,.5,.7,3);"
"b[4]=vec4(1,.9,.9,2);"
"b[5]=vec4(1,.9,.9,2);"
"float x=.15*z-6.2831*s.x/v.x,u=2.+3.*s.y/v.y;"
"vec2 a=vec2(cos(x),sin(x));"
"vec3 n=normalize(vec3(y.x*a.x-a.y,y.y,a.x+y.x*a.y)),e=vec3(u*a.y,0,-u*a.x);"
"u=m(e,n,i,w);"
"e+=n*u;"
"f.xyz=vec3(0,1.5,-3)-e;"
"float x=.15*z-6.2831*s.x/v.x,a=2.+3.*s.y/v.y;"
"vec2 g=vec2(cos(x),sin(x));"
"vec3 n=normalize(vec3(y.x*g.x-g.y,y.y,g.x+y.x*g.y)),u=vec3(a*g.y,0,-a*g.x);"
"x=m(u,n,i,w);"
"u+=n*x;"
"f.xyz=vec3(0,1.5,-3)-u;"
"f.w=length(f.xyz);"
"f.xyz=f.xyz/f.w;"
"w=d(e,i,w,n,f,o);"
"vec4 g,C;"
"u=m(e,o.xyz,i,g);"
"e+=o.xyz*u;"
"f.xyz=vec3(0,1.5,-1)-e;"
"w=d(u,i,w,n,f,o);"
"vec4 t,e;"
"x=m(u,o.xyz,i,t);"
"u+=o.xyz*x;"
"f.xyz=vec3(0,1.5,-1)-u;"
"f.w=length(f.xyz);"
"f.xyz=f.xyz/f.w;"
"g=d(e,i,g,o.xyz,f,C);"
"w=mix(w,g,.5-.5*o.w);"
"t=d(u,i,t,o.xyz,f,e);"
"w=mix(w,t,.5-.5*o.w);"
"gl_FragColor=w;"
"}",

Expand Down
9 changes: 4 additions & 5 deletions tests/real/lunaquatic.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ vec3 calculateSky(vec3 ro,vec3 rd,int addPlanet)
{
vec3 color=max(vec3(0),(max(vec3(0),pow(dot(lightDir,rd),6.))*.7-rd.y)*mix(vec3(1,.5,0),vec3(1),min(1.,lightDir.y*1.5))+lightDir.y*3.);
float phi=atan(rd.x,rd.z),theta=acos(rd.y/length(rd));
theta=smoothstep(0.,.5,norm(.5*smoothrnd(3e2*vec2(phi,theta)))+norm(.75*smoothrnd(5e2*vec2(phi,theta)))-1.25)*saturate(1.-lightDir.y*5.);
phi=smoothstep(0.,.5,norm(.5*smoothrnd(3e2*vec2(phi,theta)))+norm(.75*smoothrnd(5e2*vec2(phi,theta)))-1.25)*saturate(1.-lightDir.y*5.);
if(addPlanet>0)
{
vec4 p=calcPlanet(ro,rd);
color+=theta*saturate(1.-p.w)+p.xyz;
color+=phi*saturate(1.-p.w)+p.xyz;
}
rd.xy+=ro.xy*1e-4;
return color+(calculateSkySub(normalize(rd+vec3(sin(Y.z*.1),0,cos(Y.z*.1))*.1)*3.)+calculateSkySub(normalize(rd+vec3(sin(Y.z*.1),0,cos(Y.z*.1))*.2)*5.)*.1+calculateSkySub(normalize(rd+vec3(sin(Y.z*.1),0,cos(Y.z*.1))*.4)*7.)*.1-calculateSkySub(normalize(rd+vec3(sin(Y.z*.2),0,0)*.5))*1.5)*saturate(rd.y+.5);
Expand All @@ -128,9 +128,8 @@ float isoSurface(vec3 p)
}
float traceIso(vec3 ro,vec3 rd,float mint,float maxt)
{
float s=99.,lt,liso,exact;
s=(maxt-mint)/s;
for(float t=mint;t<maxt;t+=s)
float lt,liso,exact,delt=(maxt-mint)/99.;
for(float t=mint;t<maxt;t+=delt)
{
vec3 p=ro+t*rd;
float iso=isoSurface(p);
Expand Down
43 changes: 20 additions & 23 deletions tests/real/mandelbulb.expected
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const char *mandelbulb_frag =
"}"
"bool f(vec3 i,vec3 s,out float y,out vec3 t,out vec4 o)"
"{"
"float p=1.;"
"vec2 e;"
"if(!f(i,s,e))"
"return false;"
Expand All @@ -62,24 +61,24 @@ const char *mandelbulb_frag =
"float c;"
"vec3 x;"
"vec4 n;"
"p=1./sqrt(1.+p*p);"
"float w=1./sqrt(2.);"
"for(float f=e.x;f<e.y;)"
"{"
"vec3 z=i+s*f;"
"float d=clamp(.001*f*p,1e-6,.005),w=d*.1;"
"float p=clamp(.001*f*w,1e-6,.005),d=p*.1;"
"vec4 r;"
"float l;"
"if(v(z,l,n))"
"return y=f,t=normalize(x),o=n,true;"
"float m;"
"v(z+vec3(w,0,0),m,r);"
"v(z+vec3(d,0,0),m,r);"
"float g;"
"v(z+vec3(0,w,0),g,r);"
"v(z+vec3(0,d,0),g,r);"
"float C;"
"v(z+vec3(0,0,w),C,r);"
"v(z+vec3(0,0,d),C,r);"
"x=vec3(m-l,g-l,C-l);"
"c=.5*l*w/length(x);"
"if(c<d)"
"c=.5*l*d/length(x);"
"if(c<p)"
"return o=n,t=normalize(x),y=f,true;"
"f+=c;"
"}"
Expand All @@ -90,31 +89,29 @@ const char *mandelbulb_frag =
"vec2 v=-1.+2.*gl_FragCoord.xy/resolution.xy,i=v*vec2(1.33,1);"
"vec3 y=vec3(.577);"
"float t=1.4+.2*cos(6.28318*time/20.);"
"vec3 c=vec3(t*sin(6.28318*time/20.),.3-.4*sin(6.28318*time/20.),t*cos(6.28318*time/20.)),e=normalize(vec3(0,.1,0)-c),s=normalize(cross(e,vec3(0,1,0)));"
"s=normalize(i.x*s+i.y*normalize(cross(s,e))+1.5*e);"
"vec3 x;"
"vec3 c=vec3(t*sin(6.28318*time/20.),.3-.4*sin(6.28318*time/20.),t*cos(6.28318*time/20.)),s=normalize(vec3(0,.1,0)-c),e=normalize(cross(s,vec3(0,1,0))),d=normalize(i.x*e+i.y*normalize(cross(e,s))+1.5*s);"
"vec4 o;"
"if(f(c,s,t,e,o))"
"if(f(c,d,t,e,o))"
"{"
"vec3 i=c+t*s;"
"vec3 i=c+t*d;"
"float v=clamp(.2+.8*dot(y,e),0.,1.);"
"v*=v;"
"float p=clamp(.3+.7*dot(vec3(-.707,0,.707),e),0.,1.),w=clamp(1.25*o.w-.4,0.,1.);"
"w=w*w*.5+.5*w;"
"float p=clamp(.3+.7*dot(vec3(-.707,0,.707),e),0.,1.),n=clamp(1.25*o.w-.4,0.,1.);"
"n=n*n*.5+.5*n;"
"float r;"
"vec3 n;"
"vec4 g;"
"vec3 C;"
"vec4 x;"
"if(v>.001)"
"if(f(i,y,r,n,g))"
"if(f(i,y,r,C,x))"
"v=.1;"
"x=mix(mix(mix(vec3(1),vec3(.8,.6,.2),sqrt(o.x)*1.25),vec3(.8,.3,.3),sqrt(o.y)*1.25),vec3(.7,.4,.3),sqrt(o.z)*1.25)*((.5+.5*e.y)*vec3(.14,.15,.16)*.8+v*vec3(1,.85,.4)+.5*p*vec3(.08,.1,.14))*vec3(pow(w,.8),w,pow(w,1.1));"
"x=1.5*(x*.15+.85*sqrt(x));"
"s=mix(mix(mix(vec3(1),vec3(.8,.6,.2),sqrt(o.x)*1.25),vec3(.8,.3,.3),sqrt(o.y)*1.25),vec3(.7,.4,.3),sqrt(o.z)*1.25)*((.5+.5*e.y)*vec3(.14,.15,.16)*.8+v*vec3(1,.85,.4)+.5*p*vec3(.08,.1,.14))*vec3(pow(n,.8),n,pow(n,1.1));"
"s=1.5*(s*.15+.85*sqrt(s));"
"}"
"else"
" x=1.3*vec3(1,.98,.9)*(.7+.3*s.y);"
" s=1.3*vec3(1,.98,.9)*(.7+.3*d.y);"
"v=v*.5+.5;"
"x=clamp(x*(.7+4.8*v.x*v.y*(1.-v.x)*(1.-v.y)),0.,1.);"
"gl_FragColor=vec4(x,1);"
"s=clamp(s*(.7+4.8*v.x*v.y*(1.-v.x)*(1.-v.y)),0.,1.);"
"gl_FragColor=vec4(s,1);"
"}";

#endif // MANDELBULB_EXPECTED_
Loading

0 comments on commit 660f8bb

Please sign in to comment.