I found a morning today to play a bit more with the Firtree compiler re-write. As you will remember, I’m replacing the current GLSL focussed hacky compiler with something a little nicer based on LLVM. The idea being that kernels are compiled to an LLVM IR, LLVM does it’s magic, linking happens and the backends just have to deal with one monolithic blob of LLVM. All the optimisation is handled by LLVM.
Today I implemented for/do/while loops. In Firtree it is a requirement that these loops be unwindable at runtime since not all backends will support loops. This also neatly avoids the problem of locking one’s graphics card with an infinite loop. After a few false starts I can now present this kernel:
kernel vec4 testKernel() {
int c = 5;
for(int dx=-3; dx<=3; dx++) {
for(int dy=-3; dy<=3; dy++) {
for(int dz=-3; dz<=3; dz++) {
c = dx+dy+dz;
}
}
}
int a = 10;
int d = 10;
while(a > 3) {
int f = 5;
while(f > 2) {
--f;
--d;
}
--a;
}
int b = 5;
do { --b; } while(b>0);
return vec4(a,b,c,d);
}
And have this result:
define internal <4 x float> @testKernel(<2 x float> %__this__destCoord) nounwind readnone {
entry:
ret <4 x float> < float 3.000000e+00, float 0.000000e+00, float 9.000000e+00, float -1.100000e+01 >
}
This is a definite improvement over the 139-line monster that the compiler spits out pre-optimisation.





