KatInterview

From Shader Source to GPU Code: Compilers and Modern Graphics APIs

System DesignModel answer
HardModel answerSystem Designshader-compilergraphics-apispir-v

Last updated

Describe how shader source becomes GPU machine code, contrast explicit and legacy graphics APIs, and sketch a small shader compiler with front end, IR, and SPIR-V back end.

You are discussing GPU graphics infrastructure with a senior engineer. Cover three connected parts.

  1. Compilation pipeline. Walk through how a shader written in HLSL or GLSL becomes code the GPU can run. Address:

    • front-end work (preprocessing, parsing, semantic checks),
    • the intermediate representation(s) used,
    • the optimization passes that matter for shaders,
    • reflection metadata and who consumes it,
    • back-end code generation.
  2. API comparison. Contrast a Direct3D 12-style explicit API with Vulkan and with OpenGL (or Direct3D 11) along three axes:

    • how resources are bound to shaders,
    • how commands are recorded and submitted,
    • how pipeline state is managed.
  3. Minimal compiler design. Propose a design for a small shader compiler, covering its front end and back end. State the language subset, the public interface, the major components and how you would test it.

Make your assumptions explicit (scale, number of shader variants, performance targets), explain the trade-offs, and note failure modes and how you would roll the system out.

Hints

Hint 1

Split the story into an offline stage (source to portable IR such as SPIR-V or DXIL) and a driver stage (IR to vendor ISA once pipeline state is known), and explain why the split exists.

Hint 2

For the API comparison, build one theme: explicit APIs move work from draw time to creation time and from the driver to the application. Then map each of the three axes onto that theme.

Hint 3

For the minimal compiler, keep the language tiny, choose SPIR-V as the target because it is validatable, build structured control flow directly in the IR, and describe how you would test with a validator, golden files and fuzzing.