Skip to content

Commit

Permalink
Inline const values of type int,float,bool
Browse files Browse the repository at this point in the history
e.g.
  const bool debug = false;
  const int steps = 5;

are inlined by default
  • Loading branch information
laurentlb committed Dec 31, 2022
1 parent 055e352 commit fe18e05
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 110 deletions.
19 changes: 14 additions & 5 deletions src/analyzer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,22 @@ let findInlinable block =
| (false, _), (false, _) -> ident.ToBeInlined <- true
| _ -> ()

let inlineAllConsts li =
let maybeInlineConsts li =
let mapInnerDecl = function
// Unconditional inlining of anything marked "const" -- trust that the
// compiler would have yelled if it weren't really really const, so we
// can brutishly just inline it.
| ({typeQ = tyQ}, defs) as d when List.contains "const" tyQ ->
for (def:DeclElt) in defs do def.name.ToBeInlined <- true
for (def:DeclElt) in defs do
// AggroInlining: unconditional inlining of anything marked "const".
// Note: this is unsafe if the init value depends on something mutable.
if options.aggroInlining then
def.name.ToBeInlined <- true
// Otherwise, inline only trivial constants.
else match def.init with
| Some (Var v) when v.Name = "true" || v.Name = "false" ->
def.name.ToBeInlined <- true
| Some (Int _)
| Some (Float _) ->
def.name.ToBeInlined <- true
| _ -> ()
d
| d -> d
let mapStmt = function
Expand Down
2 changes: 1 addition & 1 deletion src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ let simplify li =
// but we only need the information for aggroInlining so don't bother if
// it's off.
|> Analyzer.markLValues
|> if options.aggroInlining then Analyzer.inlineAllConsts else id
|> Analyzer.maybeInlineConsts
|> iterateSimplifyAndInline
|> List.choose (function
| TLDecl (ty, li) -> TLDecl (rwType ty, declsNotToInline li) |> Some
Expand Down
5 changes: 2 additions & 3 deletions tests/real/ed-209.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,10 @@ MarchData gunPod(vec3 p)
MarchData r;
setBodyMaterial(r);
p.yz+=vec2(.1,.45);
const float rr=.35,chamfer=.1;
vec3 pp=p;
pp.z=abs(pp.z)-.5;
r.d=sdCappedCone(pp,vec3(0),vec3(0,0,-chamfer),rr-chamfer,rr);
r.d=min(r.d,sdCappedCylinder(p,rr,.4));
r.d=sdCappedCone(pp,vec3(0),vec3(0,0,-.1),.25,.35);
r.d=min(r.d,sdCappedCylinder(p,.35,.4));
pp=vec3(p.x,.28-p.y,p.z);
r.d=min(r.d,sdTriPrism(pp,vec2(.1,.5)));
pp=p;
Expand Down
5 changes: 2 additions & 3 deletions tests/real/from-the-seas-to-the-stars.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,7 @@ void main()
if(coord.x<1280&&coord.y<720)
{
vec4 c=vec4(0);
const int sampleCount=8;
for(int sampleIndex=0;sampleIndex<sampleCount;++sampleIndex)
for(int sampleIndex=0;sampleIndex<8;++sampleIndex)
{
uint x=imageLoad(d0,coord,sampleIndex).x,idepth=x>>16,icol=x&65535;
if(idepth==0)
Expand All @@ -840,7 +839,7 @@ void main()
c.xyz+=clamp(col,0,1);
c.w+=1.;
}
c/=float(sampleCount);
c/=float(8);
vec3 bg=vec3(.02)*vec3(.7,.9,1)*.6;
if(time>=200)
bg*=max(0.,1.-(time-200)/7.);
Expand Down
136 changes: 67 additions & 69 deletions tests/real/mandelbulb.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,115 +7,113 @@
const char *mandelbulb_frag =
"uniform vec2 resolution;"
"uniform float time;"
"bool f(vec4 v,vec3 f,vec3 y,out vec2 i)"
"bool t(vec4 v,vec3 f,vec3 t,out vec2 i)"
"{"
"vec3 x=f-v.xyz;"
"float c=dot(x,y),e=dot(x,x)-v.w*v.w,o=c*c-e;"
"vec3 y=f-v.xyz;"
"float c=dot(y,t),e=dot(y,y)-v.w*v.w,o=c*c-e;"
"if(o<0.)"
"return false;"
"float t=sqrt(o);"
"i.x=-c-t;"
"i.y=-c+t;"
"float x=sqrt(o);"
"i.x=-c-x;"
"i.y=-c+x;"
"return true;"
"}"
"const int e=7;"
"const float y=100.;"
"bool f(vec3 v,out float f,out vec4 o)"
"bool t(vec3 v,out float f,out vec4 y)"
"{"
"vec4 i=vec4(100);"
"vec3 c=v;"
"float x=dot(c,c);"
"if(x>y)"
"return f=.5*log(x)/pow(8.,0.),o=vec4(1),false;"
"for(int t=1;t<e;t++)"
"float o=dot(c,c);"
"if(o>100.)"
"return f=.5*log(o)/pow(8.,0.),y=vec4(1),false;"
"for(int x=1;x<7;x++)"
"{"
"\n#if 0\n"
"float z=sqrt(dot(c,c)),s=acos(c.y/z),n=atan(c.x,c.z);"
"z=pow(z,8.);"
"float t=sqrt(dot(c,c)),s=acos(c.y/t),n=atan(c.x,c.z);"
"t=pow(t,8.);"
"s=s*8.;"
"n=n*8.;"
"c=v+z*vec3(sin(s)*sin(n),cos(s),sin(s)*cos(n));"
"c=v+t*vec3(sin(s)*sin(n),cos(s),sin(s)*cos(n));"
"\n#else\n"
"float d=c.x,m=d*d,p=m*m,l=c.y,r=l*l,a=c.z,w=a*a,g=w*w,u=m+w,q=inversesqrt(u*u*u*u*u*u*u),b=p+r*r+g-6.*r*w-6.*m*r+2.*w*m,F=m-r+w;"
"c.x=v.x+64.*d*l*a*(m-w)*F*(p-6.*m*w+g)*b*q;"
"c.y=v.y+-16.*r*u*F*F+b*b;"
"c.z=v.z+-8.*l*F*(p*p-28.*p*m*w+70.*p*g-28.*m*w*g+g*g)*b*q;"
"float d=c.x,z=d*d,m=z*z,p=c.y,e=p*p,l=c.z,w=l*l,r=w*w,g=z+w,a=inversesqrt(g*g*g*g*g*g*g),u=m+e*e+r-6.*e*w-6.*z*e+2.*w*z,q=z-e+w;"
"c.x=v.x+64.*d*p*l*(z-w)*q*(m-6.*z*w+r)*u*a;"
"c.y=v.y+-16.*e*g*q*q+u*u;"
"c.z=v.z+-8.*p*q*(m*m-28.*m*z*w+70.*m*r-28.*z*w*r+r*r)*u*a;"
"\n#endif\n"
"x=dot(c,c);"
"i=min(i,vec4(c.xyz*c.xyz,x));"
"if(x>y)"
"return o=i,f=.5*log(x)/pow(8.,float(t)),false;"
"o=dot(c,c);"
"i=min(i,vec4(c.xyz*c.xyz,o));"
"if(o>100.)"
"return y=i,f=.5*log(o)/pow(8.,float(x)),false;"
"}"
"o=i;"
"y=i;"
"f=0.;"
"return true;"
"}"
"bool f(vec3 v,vec3 c,out float o,float y,out vec3 x,out vec4 t,float u)"
"bool t(vec3 v,vec3 f,out float y,float c,out vec3 o,out vec4 x,float g)"
"{"
"vec4 i=vec4(0,0,0,1.25);"
"vec2 e;"
"if(!f(i,v,c,e))"
"vec2 s;"
"if(!t(i,v,f,s))"
"return false;"
"if(e.y<.001)"
"if(s.y<.001)"
"return false;"
"if(e.x<.001)"
"e.x=.001;"
"if(e.y>y)"
"e.y=y;"
"float n;"
"vec3 s;"
"vec4 m;"
"float w=1./sqrt(1.+u*u);"
"for(float r=e.x;r<e.y;)"
"if(s.x<.001)"
"s.x=.001;"
"if(s.y>c)"
"s.y=c;"
"float e;"
"vec3 n;"
"vec4 z;"
"float w=1./sqrt(1.+g*g);"
"for(float m=s.x;m<s.y;)"
"{"
"vec3 z=v+c*r;"
"float d=clamp(.001*r*w,1e-6,.005),p=d*.1;"
"vec4 q;"
"vec3 r=v+f*m;"
"float d=clamp(.001*m*w,1e-6,.005),p=d*.1;"
"vec4 a;"
"float l;"
"if(f(z,l,m))"
"return o=r,x=normalize(s),t=m,true;"
"float g;"
"f(z+vec3(p,0,0),g,q);"
"float a;"
"f(z+vec3(0,p,0),a,q);"
"if(t(r,l,z))"
"return y=m,o=normalize(n),x=z,true;"
"float u;"
"t(r+vec3(p,0,0),u,a);"
"float q;"
"t(r+vec3(0,p,0),q,a);"
"float b;"
"f(z+vec3(0,0,p),b,q);"
"s=vec3(g-l,a-l,b-l);"
"n=.5*l*p/length(s);"
"if(n<d)"
"return t=m,x=normalize(s),o=r,true;"
"r+=n;"
"t(r+vec3(0,0,p),b,a);"
"n=vec3(u-l,q-l,b-l);"
"e=.5*l*p/length(n);"
"if(e<d)"
"return x=z,o=normalize(n),y=m,true;"
"m+=e;"
"}"
"return false;"
"}"
"void main()"
"{"
"vec2 v=-1.+2.*gl_FragCoord.xy/resolution.xy,c=v*vec2(1.33,1);"
"vec3 x=vec3(.577),o=vec3(-.707,0,.707);"
"vec3 f=vec3(.577),o=vec3(-.707,0,.707);"
"float y=1.,s=1.4+.2*cos(6.28318*time/20.);"
"vec3 z=vec3(s*sin(6.28318*time/20.),.3-.4*sin(6.28318*time/20.),s*cos(6.28318*time/20.)),p=vec3(0,.1,0),m=normalize(p-z),t=vec3(0,1,0),r=normalize(cross(m,t)),w=normalize(cross(r,m)),e=normalize(c.x*r+c.y*w+1.5*m),l,i;"
"vec4 u;"
"float b;"
"if(!f(z,e,b,1e20,l,u,y))"
"i=1.3*vec3(1,.98,.9)*(.7+.3*e.y);"
"vec3 n=vec3(s*sin(6.28318*time/20.),.3-.4*sin(6.28318*time/20.),s*cos(6.28318*time/20.)),r=vec3(0,.1,0),z=normalize(r-n),x=vec3(0,1,0),p=normalize(cross(z,x)),e=normalize(cross(p,z)),w=normalize(c.x*p+c.y*e+1.5*z),g,i;"
"vec4 l;"
"float u;"
"if(!t(n,w,u,1e20,g,l,y))"
"i=1.3*vec3(1,.98,.9)*(.7+.3*w.y);"
"else"
"{"
"vec3 n=z+b*e;"
"float d=clamp(.2+.8*dot(x,l),0.,1.);"
"vec3 m=n+u*w;"
"float d=clamp(.2+.8*dot(f,g),0.,1.);"
"d=d*d;"
"float F=clamp(.3+.7*dot(o,l),0.,1.),a=clamp(1.25*u.w-.4,0.,1.);"
"float q=clamp(.3+.7*dot(o,g),0.,1.),a=clamp(1.25*l.w-.4,0.,1.);"
"a=a*a*.5+.5*a;"
"float q;"
"vec3 g;"
"float b;"
"vec3 F;"
"vec4 C;"
"if(d>.001)"
"if(f(n,x,q,1e20,g,C,y))"
"if(t(m,f,b,1e20,F,C,y))"
"d=.1;"
"i=vec3(1);"
"i=mix(i,vec3(.8,.6,.2),sqrt(u.x)*1.25);"
"i=mix(i,vec3(.8,.3,.3),sqrt(u.y)*1.25);"
"i=mix(i,vec3(.7,.4,.3),sqrt(u.z)*1.25);"
"i*=(.5+.5*l.y)*vec3(.14,.15,.16)*.8+d*vec3(1,.85,.4)+.5*F*vec3(.08,.1,.14);"
"i=mix(i,vec3(.8,.6,.2),sqrt(l.x)*1.25);"
"i=mix(i,vec3(.8,.3,.3),sqrt(l.y)*1.25);"
"i=mix(i,vec3(.7,.4,.3),sqrt(l.z)*1.25);"
"i*=(.5+.5*g.y)*vec3(.14,.15,.16)*.8+d*vec3(1,.85,.4)+.5*q*vec3(.08,.1,.14);"
"i*=vec3(pow(a,.8),pow(a,1.),pow(a,1.1));"
"i=1.5*(i*.15+.85*sqrt(i));"
"}"
Expand Down
53 changes: 26 additions & 27 deletions tests/real/yx_long_way_from_home.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,36 @@ const char *yx_long_way_from_home_frag =
"vec3 h(vec3 v,vec3 x)"
"{"
"vec3 y=normalize(vec3(.3,.36,-1));"
"const float z=1e-4;"
"const vec3 n=vec3(1,.6,.2)*2.;"
"vec3 r=vec3(1),c=vec3(0);"
"for(int m=0;m<10;++m)"
"const vec3 z=vec3(1,.6,.2)*2.;"
"vec3 n=vec3(1),c=vec3(0);"
"for(int r=0;r<10;++r)"
"{"
"vec3 t,l;"
"float a;"
"if(d(v,x,t,l,a))"
"{"
"float p=1.;"
"vec3 g=vec3(1);"
"float m=1.;"
"vec3 p=vec3(1);"
"if(f==1)"
"g=vec3(.7);"
"p*=p;"
"p=vec3(.7);"
"m*=m;"
"{"
"v=t+l*.002;"
"vec3 h=reflect(x,l),o=e(l,1.);"
"x=normalize(mix(h,o,p));"
"r*=g;"
"x=normalize(mix(h,o,m));"
"n*=p;"
"}"
"vec3 o=d(y,z);"
"vec3 o=d(y,1e-4);"
"float h=dot(l,o);"
"vec3 b,k;"
"float S;"
"if(h>0.&&!d(t+l*.002,o,b,k,S))"
"c+=r*h*n;"
"vec3 g,k;"
"float b;"
"if(h>0.&&!d(t+l*.002,o,g,k,b))"
"c+=n*h*z;"
"i=s(i.y);"
"}"
"else"
" if(abs(a)>.1)"
"return c+d(x)*r;"
"return c+d(x)*n;"
"else"
" break;"
"}"
Expand All @@ -176,20 +175,20 @@ const char *yx_long_way_from_home_frag =
"v.x*=iResolution.x/iResolution.y;"
"const vec3 f=vec3(-4,2,3),y=vec3(0);"
"const float z=distance(f,y);"
"const vec2 r=vec2(1,2)*.015;"
"vec3 c=vec3(0),o=normalize(vec3(v,2));"
"vec2 t=d();"
"c.xy+=t*r;"
"o.xy-=t*r*o.z/z;"
"const vec2 p=vec2(1,2)*.015;"
"vec3 c=vec3(0),r=normalize(vec3(v,2));"
"vec2 m=d();"
"c.xy+=m*p;"
"r.xy-=m*p*r.z/z;"
"vec3 l=y-f;"
"float a=-atan(l.y,length(l.xz)),m=-atan(l.x,l.z);"
"float a=-atan(l.y,length(l.xz)),o=-atan(l.x,l.z);"
"c.yz*=n(a);"
"o.yz*=n(a);"
"c.xz*=n(m);"
"o.xz*=n(m);"
"r.yz*=n(a);"
"c.xz*=n(o);"
"r.xz*=n(o);"
"c+=f;"
"vec4 g=vec4(h(c,o),1);"
"gl_FragColor=!isnan(g.x)&&g.x>=0.?g:vec4(0);"
"vec4 t=vec4(h(c,r),1);"
"gl_FragColor=!isnan(t.x)&&t.x>=0.?t:vec4(0);"
"}";

#endif // YX_LONG_WAY_FROM_HOME_FRAG_EXPECTED_
10 changes: 10 additions & 0 deletions tests/unit/conditionals.frag
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ bool and2() {
bool and3() {
return success() && true;
}

const int debug = false;

int foo() {
int a = 1;
if (debug) {
a = 2;
}
return a;
}
5 changes: 5 additions & 0 deletions tests/unit/conditionals.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ bool and3()
{
return success();
}
int foo()
{
int a=1;
return a;
}
3 changes: 1 addition & 2 deletions tests/unit/inline-aggro.expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ float inl4()
const float f=acos(-1.);
return 2.*f*.75;
}
const float foo=123.;
float inl5()
{
return foo+456.;
return 579.;
}
const float bar=acos(-1.),baz=2.*bar;
float inl6()
Expand Down

0 comments on commit fe18e05

Please sign in to comment.