Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

A Driver or a Compiler?

A Driver or a Compiler?

I said earlier that a driver is a bit of code that makes a physical device emulate an abstract machine. The same is true of a compiler. Any programming language provides an abstract model of how a computer works, and it is the responsibility of the compiler to implement this abstract model on top of a real architecture.

When it comes to a modern graphics card, the distinction is even fuzzier. A lot of the commands that you give to a graphics card are really programs. These are typically written in something like OpenGL's GLSL, Direct3D's HLSL, or even in something not intended for graphics, like OpenCL. In a Gallium driver, these are first transformed into TGSI by the state tracker and are then transformed into something that will run on the card by the driver.

One of the existing Gallium drivers adds yet another abstract machine to the mix: LLVM. The Low Level Virtual Machine (LLVM) is a compiler infrastructure that provides a simple virtual instruction set, a large set of optimizations that run on this instruction set, and a set of code generators that emit native code for various architectures.

Using LLVM for graphics isn't a new idea. Apple's OS X 10.5 included an LLVM-based GLSL compiler for machines that didn't have hardware support for shaders. This took the existing interpreter code, where each GLSL instruction was compiled to a function in C, and compiled it to LLVM's intermediate representation (IR). The driver then compiled the GLSL programs to LLVM IR and linked them to the functions from the interpreter. The optimization passes then inlined all the interpreter functions, performed various other optimizations, and emitted native AltiVec or SSE code.

There is no theoretical reason why LLVM should only be used for CPU-based fall-back code, and this is one area where a few projects have been working. If you provide a code generation backend for LLVM that emits the native instruction set for a GPU, then you get to use all of the LLVM optimizations for free. You also get some nice advantages, such as being able to compile any language that has an LLVM frontend for your GPU. Of course, you can't expect arbitrary C code to run well on a GPU, but components of video codecs that are heavy on vector arithmetic and light on conditional branches could almost certainly be turned into shader programs like this with very little effort.

  • + Share This
  • 🔖 Save To Your Account