LLVM as an OpenGL backend

Would it be feasible to compile LLVM IR into shading language assembler? If so, is this already being done?

This would provide the obvious benefits of not having to differentiate between GLSL and HLSL (et al.) and the pleasure of using some other language to write and test the shader’s that have support as an LLVM frontend.

I’ve googled and found nothing about this. The only LLVM and OpenGL talk I can find it about Apple using it in Leopard but I don’t think that’s the same thing as this is it?

There may so much fundamentally wrong with my understanding of the LLVM stack that comes through by asking this question so I’m sorry in advance if that’s the case!


It would certainly be possible for a restricted subset of LLVM IR, but non-trivial. GLSL and HLSL are fairly restrictive languages compared to, say, C, and many operations in LLVM IR would be very difficult to express in them.

In general, GLSL and HLSL are much more similar to each other than they are to LLVM IR, so your life would likely be much better if you defined some intermediate form between the two than trying to round-trip through LLVM IR.


Would it be feasible to compile LLVM IR into shading language assembler? If
so, is this already being done?

The R600 backend does this in conjunction with the Open Source mesa3D
project: http://www.mesa3d.org/

Mesa has a glsl frontend that is used to emit LLVM IR for newer AMD
GPUs, which is fed into the R600 backend to produce native GPU code.

Besides hardware accelerated drivers, the Mesa3d project also comes with a
software based rasterizer, which does the same thing, but instead of the
R600 backend it uses some of llvm's CPU backends.


I think he was asking about the other direction, from LLVM IR —> shading languages?


Yeah that’s right, like how Emscripten does LLVM → JavaScript.

How do you mean restictive, ie with memory allocation and the like? What sort of IR ops would not be representable in something like GLSL?

If you’re really just looking for a way to write your shaders once and have them work everywhere, you might want to look at some of the tools game developers are using.

Aras from Unity was using this tool (https://github.com/aras-p/hlsl2glslfork) at one point to translate HLSL to GLSL. He also has a nice blog post about cross-platform shaders from last year (http://aras-p.info/blog/2014/03/28/cross-platform-shaders-in-2014/).