rsltollvm
DESCRIPTION
RSL to LLVM compiler project proposalTRANSCRIPT
![Page 1: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/1.jpg)
RSL - LLVM compiler project proposal
25 May, 2008
Syoyo Fujita
![Page 2: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/2.jpg)
Terminology• RSL
• RenderMan Shading Language
• LLVM http://www.llvm.org/
• Compiler infrastructure
• AST
• Abstract Syntax Tree
• JIT
• Just In Time compiling
![Page 3: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/3.jpg)
Agenda•Project goal
•Architecture overview
•Compiling RSL
•Runtime
![Page 4: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/4.jpg)
Project goal (1/2)•Investigate a possibility of LLVM as a
shader VM
•JIT
•Run-time specialization
•Optimization
•For a global illumination setting, if possible.
![Page 5: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/5.jpg)
Project goal (2/2)•No full RSL implementation
•Do investigation with minimum equipment.
•Explore LLVM’s performance, functionality, etc. when it is applied to shader VM.
•Leave the experience into the document.
![Page 6: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/6.jpg)
Why LLVM?•Has SIMD instruction and x86/SSE
codegen support
•Has JIT support
•Has optimizer. It’s optimization is as good as gcc does
•Actively developing
![Page 7: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/7.jpg)
Expected benefit by using LLVM for shader VM
•Faster execution of shader
•JIT, partial evaluation, x86/SSE instruction
•Save out time
•Production-quality compiler optimization for FREE!
•Reusable shader VM code among aqsis, lucille, etc.
![Page 8: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/8.jpg)
How it would work?
![Page 9: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/9.jpg)
surfacematte( float Ka = 1; float Kd = 1;){ normal Nf = faceforward(normalize(N), I); Oi = Os; Ci = Os * Cs * (Ka * ambient() + Kd * diffuse(Nf));}
shader code
compiled shader code
RSL
LLVM bitcode
compiled builtin
LLVM bitcode
builtin functionsource
C/C++, etc
faceforward()normalize()...
Renderer
native
JIT-ed, specialized shader code
native
texture()diffuse()ambient()trace()...interact
call
+
JIT compile & specialization by LLVM
compile
compile
offline
runtime
![Page 10: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/10.jpg)
What we have to develop
•RSL -> LLVM compiler
•Runtime support
![Page 11: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/11.jpg)
![Page 12: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/12.jpg)
Compiling RSL
![Page 13: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/13.jpg)
+
a b
c
*
(mul (id c) (add a b))
(a + b) * c
RSL AST LLVM
%tmp0 = add float %a, %b%tmp1 = mul float %c, %tmp0
Parse Codegen
![Page 14: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/14.jpg)
slc.py•I’ve wrote simple RSL to LLVM IR
translator written in Python
•Directly emits LLVM IR assembly
•Construct AST internally
•Easy to add codegen other than LLVM, if required.
![Page 15: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/15.jpg)
$ cat input.slsurfacemyshader (){ Cs = Ci;}$ slc.py input.sl$ llvm-as input.ll -f$ llvm-dis input.bc...define void @myshader() { %tmp0 = load <4 x float>* @Ci ; <<4 x float>> [#uses=1] store <4 x float> %tmp0, <4 x float>* @Cs ret void}
![Page 16: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/16.jpg)
Compiling builtin function
• Self-contained functions
• normalize(), reflect(), ...
• Just compile into LLVM bitcode by using you favorite frontend(C/C++, etc)
• Functions which interacts renderer internal
• ambient(), trace(), illuminance(), ...
• Need a careful treatment
Need a investigation
![Page 17: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/17.jpg)
Note•External & built-in functions are
unresolved at this time.
•How to handle DSO?
•Also compile DSO into LLVM bitcode?
Need a investigation
![Page 18: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/18.jpg)
Runtime
![Page 19: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/19.jpg)
Runtime phase•Read shader bitcode
•Setting up function parameter
•JIT compile the shader function
•Specialize the shader function
•Call the shader function
![Page 20: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/20.jpg)
setup_shader( const char *shaderfile, // Compiled LLVM bitcode shader state_t *state, // shader env, shader param renderer_t *renderer) {m = LLVM_LoadModule(shaderfile);f = LLVM_GetFunction(m);sig = LLVM_GetFunctionSignature(f);
param = BindParameter(sig, state);
// JIT compile the shader with optimization// (Include partial evaluation),// then get the C function pointer(i.e. on the memory).entrypoint = LLVM_JITCompileWithOpt(f, param);
renderer->shader = entrypoint;}
C/C++, renderer runtime, pseudo code.
![Page 21: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/21.jpg)
shade( state_t *state, // [in] shadingpoint_t *points, // [inout] int npoints, renderer_t *renderer){
for (i = 0; i < npoints; i++) { (*renderer->shader)(state, points[i]);}
}
C/C++, renderer runtime, pseudo code.
![Page 22: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/22.jpg)
Shader specialization(1/2)
•Constant folding for uniform variable
•Let LLVM do that.
surface(float Ka){Cs = sqrt(Ka) * Ci;
}
surface(4.0){Cs = 2.0 * Ci;
}
SpecializedInput
![Page 23: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/23.jpg)
Shader specialization(2/2)
•Caching a varying variable(e.g. texture())
•G-buffer
•Things will be difficult for global illumination
•A lot of indirect references, buffers which makes impractical for caching
Need a investigation
![Page 24: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/24.jpg)
![Page 25: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/25.jpg)
Designing interface
![Page 26: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/26.jpg)
C/RSL/LLVM•What is the good interface for these
layer to access each other? e.g.
•Pass vector type as pointer or as value?
•Use opeque pointer?
•Requirement: portable, easy to specializable by LLVM
Need a investigation
![Page 27: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/27.jpg)
Polymorphic function•RSL has polymorphic function
•How to define interface for polymorphic function?
•A proposal:
•float noise(float) -> noise_ff
•vector noise(float, float) -> noise_vff
•vector noise(point) -> noise_vp
Need a investigation
![Page 28: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/28.jpg)
LLVM Code style•SPMD
•short vector SIMD
•SPSD
![Page 29: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/29.jpg)
SPMD code style•Pros
•SIMD optimization in LLVM level
•Fits reyes style renderer naturally
•aqsis
•Cons
•How to efficiently handle incoherency?
•trace(), if/while
•call a shader/DSO in the shader
Need a investigation
![Page 30: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/30.jpg)
surface myshade(){ Cs = Ci;}
myshader(shadpts *sps, int n){ for i = 0 to n: sps[i].Cs = sps[i].Ci}
shade(primitive *prim){ shadpts *pts; int n;
dice(prim, pts /* out */, &n /*out */); // fill input for (i = 0; i < n; i++) { pts[i].Ci = ... }
// call shader(*shader)(pts, n);
}
Pseudo LLVM code RSL
Pseudo Renderer code
![Page 31: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/31.jpg)
SPSD code style•Pros
•Work in most case
•Low overhead
•Fits raytracing-based renderer
•lucille
•Cons
•To compute derivative, we have to execute 3 or 4 instance at a time anyway
•short vector SIMD
![Page 32: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/32.jpg)
•It’s possible to emit 3 pattern of shader code at compiling phase
•SPMD(vector size: n) good for reyes
•4 SIMD(vector size: 4) good for raytracing
•Scalar(vector size: 1) good for incoherency: DSO call, if/while
•And more, synthesize appropriate shader code place by place, by investigating shader program.
•But needs sophisticated RSL compiler support.
![Page 33: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/33.jpg)
Note•Keep in mind that LLVM bitcode can
be modified/synthesizable at runtime phase.
•Tweaking parameter/ABI is easy
•No need to define ABI strictly.
![Page 34: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/34.jpg)
![Page 35: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/35.jpg)
Current status
![Page 36: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/36.jpg)
component progress
RSL to LLVM compiler 50 %
Self-containedBuiltin functions 5 %
complex builtin functions 0 %
Fake shader engine(fake renderer) w/ FLTK GUI 0 %
ABI design 0 %
Document 0 %
Highlevel, whole-pipeline optimization
if possible
![Page 37: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/37.jpg)
Project period
![Page 38: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/38.jpg)
• I’m seeing about a half year to finish this project since I have very limited vacant time after my day job I can spend for
• And also I have many thing to do within this very limited & precious time: e.g. coding my renderer, writing SIMD language, consult global illumination rendering as volunteer and so on.
• Although we could collaborate/help each other the internet with enthusiasts(e.g. register, c4f), I wish someone could invest/support financially/employ me so that I could accelerate and do this project professionally.
![Page 39: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/39.jpg)
Resouce•Discussion forum
http://lucille.lefora.com/2008/05/06/rsl-llvm-compiler-project-started/
•SVNhttp://lucille.svn.sourceforge.net/svnroot/lucille/angelina/rsl2llvm
![Page 40: Rsltollvm](https://reader038.vdocuments.mx/reader038/viewer/2022110306/554a143db4c9058c5d8b4d4b/html5/thumbnails/40.jpg)
Refernces• Brian Guenter and Todd B. Knoblock and Erik Ruf, Specializing
shaders, SIGGRAPH 1995.
• Jonathan Ragan-Kelley and Charlie Kilpatrick and Brian W. Smith and Doug Epps and Paul Green and Christophe Hery and Fr\'{e}do Durand, The lightspeed automatic interactive lighting preview system, SIGGRAPH 2007
• Conal Elliott, Programming Graphics Processors Functionally, Proceedings of the 2004 Haskell Workshophttp://conal.net/Vertigo/
• Chad Austin, Renaissance : A Functional Shading Language, Master's Thesishttp://aegisknight.org/articles