8000 Recursively remove unused functions by laurentlb · Pull Request #199 · laurentlb/shader-minifier · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Recursively remove unused functions #199

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

Merged
merged 1 commit into from
Jan 1, 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
8 changes: 5 additions & 3 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,19 @@ let private computeAllDependencies code =
nodes


let removeUnused code =
let rec removeUnused code =
let nodes = computeAllDependencies code
let isUnused node =
let canBeRenamed = not (options.noRenamingList |> List.contains node.name) // noRenamingList includes "main"
let isCalled = (nodes |> List.exists (fun n -> n.callees |> List.contains node.name))
let isExternal = options.hlsl && node.funcType.semantics <> []
canBeRenamed && not isCalled && not isExternal
let unused = set [for node in nodes do if isUnused node then yield node.func]
code |> List.filter (function
| Function _ as t -> not (unused |> Set.contains t)
let mutable edited = false
let code = code |> List.filter (function
| Function _ as t -> if Set.contains t unused then edited <- true; false else true
| _ -> true)
if edited then removeUnused code else code

// reorder functions if there were forward declarations
let reorder code =
Expand Down
2 changes: 1 addition & 1 deletion tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
--no-remove-unused --no-renaming --format c-variables -o tests/unit/function_comma.expected tests/unit/function_comma.frag
--preserve-externals --format c-variables -o tests/real/mandelbulb.expected tests/real/mandelbulb.frag
--preserve-all-globals --format c-variables -o tests/real/to_the_road_of_ribbon.expected tests/real/to_the_road_of_ribbon.frag
--no-renaming-list rotatey --format c-variables -o tests/real/sult.expected tests/real/sult.frag
--no-renaming-list rotatey,main --format c-variables -o tests/real/sult.expected tests/real/sult.frag
--no-remove-unused --preserve-externals -o tests/unit/externals.preserved.expected tests/unit/externals.frag

# Multifile tests
Expand Down
63 changes: 41 additions & 22 deletions tests/real/sult.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,54 @@
*/
#ifndef SULT_EXPECTED_
# define SULT_EXPECTED_
# define VAR_resolution "m"
# define VAR_time "l"
# define VAR_resolution "r"
# define VAR_time "m"

const char *sult_frag =
"float v=5.,y=.9,a=0.,f=90.,e=0.;"
"vec3 r=vec3(1),c=vec3(0,0,1),t=vec3(0,0,1.5);"
"uniform vec2 m;"
"uniform float l;"
"vec3 rotatey(vec3 v,float m)"
"float y=5.,z=.9,x=0.,a=90.,s=0.;"
"vec3 v=vec3(1),n=vec3(0,0,1),f=vec3(0,0,1.5);"
"uniform vec2 r;"
"uniform float m;"
"vec3 rotatey(vec3 v,float y)"
"{"
"return vec3(v.x*cos(m)+v.z*sin(m),v.y,v.z*cos(m)-v.x*sin(m));"
"return vec3(v.x*cos(y)+v.z*sin(y),v.y,v.z*cos(y)-v.x*sin(y));"
"}"
"float u=0.,n=10.;"
"float s(vec3 m)"
"float c=0.,w=10.;"
"float e(vec3 v)"
"{"
"float z=l,y,a=0.,f,e,c;"
"float z=m,w,a=0.,g,o,f;"
"vec3 r;"
"m+=(sin(m.zxy*1.7+z)+sin(m.yzx+z*3.))*.2;"
"if(v<6.)"
"a=length(m.xyz*vec3(1,1,.1)-vec3(0,-.1,z*.15-.3))-.34;"
"v+=(sin(v.zxy*1.7+z)+sin(v.yzx+z*3.))*.2;"
"if(y<6.)"
"a=length(v.xyz*vec3(1,1,.1)-vec3(0,-.1,z*.15-.3))-.34;"
"else"
" a=length(m.xy+vec2(0,.7))-.3+(sin(m.z*17.+z*.6)+sin(m.z*2.)*6.)*.01;"
"m.xy=vec2(atan(m.x,m.y)*1.113,1.6-length(m.xy)-sin(z*2.)*.3);"
"r=fract(m.xzz+.5).xyz-.5;"
"r.y=(m.y-.35)*1.3;"
"y=max(abs(m.y-.3)-.05,abs(length(fract(m.xz)-.5)-.4)-.03);"
"u=step(a,y);"
"return min(min(y,a),m.y-.2);"
" a=length(v.xy+vec2(0,.7))-.3+(sin(v.z*17.+z*.6)+sin(v.z*2.)*6.)*.01;"
"v.xy=vec2(atan(v.x,v.y)*1.113,1.6-length(v.xy)-sin(z*2.)*.3);"
"r=fract(v.xzz+.5).xyz-.5;"
"r.y=(v.y-.35)*1.3;"
"w=max(abs(v.y-.3)-.05,abs(length(fract(v.xz)-.5)-.4)-.03);"
"c=step(a,w);"
"return min(min(w,a),v.y-.2);"
"}"
"vec3 x=vec3(.19,.2,.24),i=vec3(1),z=vec3(.45,.01,0),o=vec3(.17,0,0);";
"vec3 d=vec3(.19,.2,.24),o=vec3(1),l=vec3(.45,.01,0),g=vec3(.17,0,0);"
"void main()"
"{"
"vec2 t=-1.+2.*gl_FragCoord.xy/r.xy;"
"vec3 i=normalize(rotatey(rotatey(vec3(t.y*z,t.x*z*1.33,1),-x*.035).yxz,(a+s*m)*.035)),p=n+f*m,u,h,F;"
"float C=1.,b=0.,Z,Y,X=0.,W,V,U,T;"
"u=vec3(.01,0,0);"
"h=u.yyy;"
"while(C>.1)"
"{"
"for(Z=X,Y=1.;Z<w&&Y>.005;Z+=Y)"
"Y=e(p+i*Z);"
"if(Z<w)"
"p+=i*Z,W=e(p),U=c,F=normalize(-vec3(W-e(p+u.xyy),W-e(p+u.yxy),W-e(p+u.yyx))),V=clamp(e(p+F*.05)*4.+e(p+F*.1)*2.+.5,.1,1.),b=U*.3,F=normalize(F+step(4.,y)*c*sin(p.yzx*40.)*.05),i=reflect(i,F),T=clamp(dot(normalize(v),F),0.,1.),F=mix(mix(d,o,T),l*(T+.2),U)+vec3(.7*pow(clamp(dot(normalize(v),i),0.,1.),12.)),h+=C*mix(F*V,g,Z/w),C*=b*(1.-Z/w),X=.1;"
"else"
" h+=C*g,C=0.;"
"}"
"gl_FragColor.xyz=h;"
"gl_FragColor.w=1.;"
"}";

#endif // SULT_EXPECTED_
4 changes: 3 additions & 1 deletion tests/unit/unused_removal.frag
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
float f();

float actually_unreachable() { return 1.5; }

float f(){
float r = 1.;

return r;
return actually_unreachable();
}

vec3 g() { return vec3(0.); }
Expand Down
0