Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize decl+return into return #283

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ let private simplifyBlock stmts =
Some (ForE (Some e, cond, inc, body))
| (Expr e, While (cond, body)) -> // a=0;while(i<5); -> for(a=0;i<5;);
Some (ForE(Some e, Some cond, None, body))
| Decl (_, [declElt]), Jump(JumpKeyword.Return, Some (Var v)) // int x=f();return x; -> return f();
when v.Name = declElt.name.Name && declElt.init.IsSome ->
Some (Jump(JumpKeyword.Return, declElt.init))
| _ -> None)

// Inline inner decl-less blocks. (Presence of decl could lead to redefinitions.) a();{b();}c(); -> a();b();c();
Expand Down
6 changes: 3 additions & 3 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ leizex.frag 2311 => 510.372
lunaquatic.frag 5257 => 1049.740
mandelbulb.frag 2403 => 547.468
ohanami.frag 3256 => 722.517
orchard.frag 5574 => 1034.487
orchard.frag 5537 => 1022.773
oscars_chair.frag 4651 => 986.364
robin.frag 6333 => 1055.863
robin.frag 6324 => 1055.655
slisesix.frag 4581 => 933.929
terrarium.frag 3634 => 747.342
the_real_party_is_in_your_pocket.frag 12168 => 1811.526
valley_ball.glsl 4386 => 888.496
yx_long_way_from_home.frag 2975 => 610.699
Total: 118596 => 21265.585
Total: 118550 => 21253.664
7 changes: 1 addition & 6 deletions tests/real/orchard.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,6 @@ vec3 getNormal(vec3 pos,float ds)
vec2 eps_zero=vec2(ds,0);
return normalize(vec3(map(pos+eps_zero.xyy,0.).x,map(pos+eps_zero.yxy,0.).x,map(pos+eps_zero.yyx,0.))-c);
}
vec3 getSky(vec3 dir)
{
vec3 col=mix(vec3(FOG_COLOUR),vec3(0,.4,.6),abs(dir.y));
return col;
}
mat3 viewMat(float ay,float az)
{
vec2 o,ca,sa;
Expand Down Expand Up @@ -284,7 +279,7 @@ void mainImage(out vec4 fragColour,vec2 fragCoord)

vec3 seedDir=normalize(vec3(0,0,1));
seedDir=viewMat(uv.y+sin(time*.5)*.4,uv.x+time*.5)*seedDir;
vec3 rd=seedDir,col=vec3(0),sky=getSky(rd);
vec3 rd=seedDir,col=vec3(0),sky=mix(vec3(FOG_COLOUR),vec3(0,.4,.6),abs(rd.y));
float alpha=marchScene(camera,rd,fragCoord);
vec4 mat;
if(alpha>0.)
Expand Down
3 changes: 1 addition & 2 deletions tests/real/robin.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ vec3 PostEffects(vec3 rgb,vec2 xy)
vec3 CameraPath(float t)
{
t+=5.;
vec3 p=vec3(1.4+sin(t)*3.5,-.2,5.-sin(t)*2.5);
return p;
return vec3(1.4+sin(t)*3.5,-.2,5.-sin(t)*2.5);
}
float TweetVolume(float t)
{
Expand Down
11 changes: 5 additions & 6 deletions tests/unit/inline.no.expected
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int arithmetic2()
}
int unusedVars()
{
int a=arithmetic(),b=13,c=10,d=c*3;
return d;
int a=arithmetic(),b=13,c=10;
return c*3;
}
int unusedVars2()
{
Expand All @@ -31,13 +31,12 @@ int unusedVars2()
}
int multiPass()
{
int one=1,two=one*2,three=two+1;
return three;
int one=1,two=one*2;
return two+1;
}
float multiPass2()
{
float b=9.;
return b;
return 9.;
}
int dont_inline_lvalue()
{
Expand Down