Zig's self-hosted SPIR-V backend (Zig)
A SPIR-V code generator written in Zig inside the Zig compiler, lowering Zig's own typed IR (AIR) straight to SPIR-V without LLVM, so that ordinary Zig, with address spaces, calling conventions and a handful of builtins, compiles to Vulkan shaders and OpenCL kernels.
| Field | Value |
|---|---|
| Language | Zig (the compiler and the shader source are both Zig) |
| License | MIT (Expat) — LICENSE |
| Repository | codeberg.org/ziglang/zig (moved from GitHub to Codeberg); pinned at d924f83bf3e1a5726d9eca1e9d501aece08107c5 (master, 2026-10-17) |
| Documentation | Language reference @SpirvType; Zig devlog, "SPIR-V Backend Progress" (2026-06-26); Ali Cheraghi, "Zig and GPUs" |
| Category | Single-source peer: general-purpose systems language with a native SPIR-V backend |
| Latest version | Zig 0.17.0 (tagged 2026-10-01); the pin is 0.17.0-349-gd924f83bf3 |
| Targets | spirv32/spirv64 × vulkan, opengl, opencl (src/link/Spirv.zig); no DXIL, MSL, WGSL or GLSL output |
| Backend | Self-hosted by default in Debug (selfHostedBackendIsAsRobustAsLlvm is true for SPIR-V); release modes prefer LLVM unless -fno-llvm |
| Maintenance | One active maintainer (Ali Cheraghi); the SPIR-V behaviour-test CI target has been disabled since 2025-09-08 (test/tests.zig) |
NOTE
Two revisions matter. The local clone's earlier HEAD, 1bcd8d9fe60f72849254b7f74d9ea0f48eae6aaa (2026-06-02), still has std.gpu, a std.gpu.executionMode() helper that emits OpExecutionMode through inline assembly, and a structured_cfg module option. The June 2026 overhaul described in the devlog replaced all three; this deep-dive cites the later pin throughout and names the older one only where the change is itself the finding.
Overview
What it solves
Zig wants one language for CPU and GPU without a second toolchain. Its GPU story has two halves: the LLVM backend emits PTX and AMDGCN for vendor runtimes, and a self-hosted SPIR-V backend emits SPIR-V for Vulkan, OpenGL and OpenCL. The backend consumes the same AIR every other self-hosted backend (x86_64, aarch64, wasm) consumes, so the whole language front end — comptime, generics as functions returning types, @Vector, error unions, optionals — is available to shader code; whatever the backend cannot lower becomes a compile error or a TODO (SPIR-V) failure.
For a single-source D design this is the most direct analogue of the question sparkles:shaders faces: what happens when a general-purpose systems language, whose programs assume flat memory and a generic address space, is pointed at Vulkan's logical addressing model. Zig answers it without LLVM, which isolates the language-level problems from the LLVM-specific ones the LLVM SPIR-V backend deep-dive covers.
Design philosophy
The backend's author states the two central problems plainly (blog, 2025-04-17):
"One core challenge in targeting SPIR-V from a general-purpose language like Zig is the need to explicitly specify address spaces (a.k.a. storage classes). Zig supports this via the addrspace keyword, but nearly all Zig code assumes pointers live in the generic address space. That works fine until you need to lower to SPIR-V, where we sometimes rely on OpPtrCastToGeneric. Unfortunately, Vulkan doesn't support that, so as a temporary workaround, all pointers are assumed to be local (Function) memory."
"For example, unlike Zig or OpenCL, some Vulkan instructions (fma, sqrt, exp, log) do NOT guarantee correctly rounded results."
and the stated goal, after "Composite integers":
"spirv-val should never fail on a binary emitted by Zig. As Zen says, 'Compile errors are better than runtime crashes'."
The June 2026 overhaul pushed the same philosophy into the compiler itself: anything that used to be hand-written SPIR-V in inline assembly — execution modes, capabilities, extensions — is now a typed language construct or a target feature, and the assembler rejects the hand-written form (Assembler.zig):
// src/codegen/spirv/Assembler.zig
.OpExecutionMode, .OpExecutionModeId => {
return ass.fail(ass.inst.inst_offset, "cannot set execution mode in assembly", .{});
},
.OpCapability, .OpExtension => {
return ass.fail(ass.inst.inst_offset, "cannot declare capabilities or extensions in assembly; use -mcpu instead", .{});
},How it works
A shader is an export fn with a SPIR-V calling convention. Interface variables are extern globals (or @extern pointers) in a GPU address space, decorated through std.lang.ExternOptions.decoration. The minimal shape, from the compile test test/cases/callconv_spirv.zig:
export fn vert() callconv(.spirv_vertex) void {}
export fn frag() callconv(.{ .spirv_fragment = .{ .depth_assumption = .greater } }) void {}
export fn comp() callconv(.{ .spirv_kernel = .{ .x = 8, .y = 8, .z = 1 } }) void {}
export fn task() callconv(.{ .spirv_task = .{ .x = 1, .y = 1, .z = 1 } }) void {}
export fn mesh() callconv(.{ .spirv_mesh = .{ .stage_output = .output_lines, .max_primitives = 1, .max_vertices = 2, .x = 1, .y = 1, .z = 1 } }) void {}
// compile
// target=spirv32-vulkan
// cpu_features=vulkan_v1_2+mesh_shading_extResources are declared as externs. From test/behavior/spirv.zig:
const Image = @SpirvType(.{ .image = .{
.usage = .{ .sampled = u32 }, .format = .unknown, .dim = .@"2d",
.depth = .unknown, .arrayed = false, .multisampled = false, .access = .unknown,
} });
const SampledImage = @SpirvType(.{ .sampled_image = Image });
const RuntimeArray = @SpirvType(.{ .runtime_array = u32 });
const RuntimeArrayBuf = extern struct { e: RuntimeArray };
const sampled_image = @extern(*addrspace(.constant) const SampledImage, .{
.name = "sampled_image",
.decoration = .{ .descriptor = .{ .set = 0, .binding = 1 } },
});
const runtime_array = @extern(*addrspace(.storage_buffer) const RuntimeArrayBuf, .{
.name = "runtime_array",
.decoration = .{ .descriptor = .{ .set = 0, .binding = 3 } },
});The pipeline, end to end:
- Sema validates the SPIR-V-specific surface: calling-convention options (
"kernel workgroup dimensions must be at least 1"), decorations against address spaces ("'location' decoration requires 'input' or 'output' address space",extern_spirv_decoration_validation.zig), and@SpirvTypeplacement ("struct field of type '@SpirvType(.runtime_array, u32)' must be the last field",directly_embedding_spirv_type_in_struct_and_union.zig). CodeGen.zig(source) lowers each function's AIR into aMirvalue. Since June 2026 these jobs run on the compiler's thread pool like every other self-hosted backend (devlog).- The linker (
src/link/Spirv.zig,Flush.zig) stitchesMirs and any external.spvobjects into one module, deduplicates types, prunes dead code, and emits the module header from target data:OpCapabilityfrom the CPU feature set,OpMemoryModel,OpEntryPointwith a computed interface list, andOpExecutionModefrom each entry point's calling-convention payload.
Execution model and stages
Stages are calling conventions in std.lang.CallingConvention (lib/std/lang.zig): spirv_device (an ordinary callable function), spirv_vertex, spirv_kernel: SpirvKernelOptions, spirv_fragment: SpirvFragmentOptions, spirv_task: SpirvKernelOptions and spirv_mesh: SpirvMeshOptions. The payload is the execution-mode data:
| Calling convention | Payload fields | Execution modes emitted (Flush.zig) |
|---|---|---|
spirv_kernel | x, y, z | LocalSize; model GLCompute (Vulkan/OpenGL) or Kernel (OpenCL) |
spirv_fragment | pixel_centered_integer, depth_assumption (none/greater/less/unchanged) | OriginUpperLeft on Vulkan, OriginLowerLeft otherwise; PixelCenterInteger; Depth* |
spirv_vertex | none | none |
spirv_task | x, y, z | LocalSize; model TaskEXT |
spirv_mesh | stage_output, max_primitives, max_vertices, x, y, z | OutputVertices, OutputPrimitivesEXT, LocalSize, Output{Points,LinesEXT,TrianglesEXT} |
Every export of a function with a stage convention becomes an OpEntryPoint; its export name is the entry-point name. The fragment origin is chosen by the target OS, not by the user — the convention carries no origin field.
Graphics stages exist only on Vulkan and OpenGL: on spirv64-opencl, each of spirv_fragment, spirv_vertex, spirv_task and spirv_mesh is rejected with "calling convention 'spirv_fragment' not supported by compiler backend 'stage2_spirv'" (callconv_spirv_on_unsupported_platform.zig). There are no geometry, tessellation or ray-tracing conventions; their absence is the coverage boundary, not an oversight hidden elsewhere.
Built-in inputs and outputs are not entry parameters but externs in std.spirv (lib/std/spirv.zig), matched to BuiltIn by name: a nav whose name parses as a spec.BuiltIn tag gets OpDecorate … BuiltIn (CodeGen.zig, std.meta.stringToEnum(spec.BuiltIn, …)):
// lib/std/spirv.zig
pub const position_out = @extern(*addrspace(.output) @Vector(4, f32), .{ .name = "position" });
pub extern const frag_coord: @Vector(4, f32) addrspace(.input);
pub extern var frag_depth: f32 addrspace(.output);
pub extern const global_invocation_id: @Vector(3, u32) addrspace(.input);front_facing and sample_mask are still commented TODOs in that file. Compute indices also have language builtins (@workItemId, @workGroupId, @workGroupSize), lowered by builtin3D to the LocalInvocationId, WorkgroupId and WorkgroupSize built-ins.
Type system and generic programming
The backend inherits Zig's type system whole; the restrictions are in lowering.
- Vectors.
@Vector(N, T)becomesOpTypeVectoronly whenTis a bool, int or float and2 <= N <= 4(orNis 8 or 16 with thevector16feature); any other vector is lowered as an array (isSpvVector,CodeGen.zig). There is no swizzle syntax and no matrix type in the language or instd.spirv. - Two representations. Every type has a
.directform (a SPIR-V value) and an.indirectform (as stored in memory);bool, for instance, isOpTypeBooldirectly but au1integer in memory, because SPIR-V booleans have no storage size. - Composite integers. Integers wider than the target's limb (64 bits with
int64or onspirv64, else 32) are lowered as arrays of limbs, with arithmetic expanded per limb; many operations on them are stillcg.todo("div_floor for composite integers","implement composite int switch"). - Floats.
f16andf64require thefloat16/float64target features, otherwise"'f64' is not supported on the current SPIR-V feature set";f80/f128are rejected. - Opaque SPIR-V types.
@SpirvType(options: std.lang.Type.Spirv)returnssampler,image(usage, format, dimensionality, depth, access, arrayed, multisampled),sampled_imageorruntime_arraytypes. The devlog calls it the answer to "the longest-standing blocker for writing shaders". Using it on a CPU target is a compile error:"builtin @SpirvType is only available when targeting SPIR-V; targeted CPU architecture is x86_64". - Generics.
comptimeparameters and type-returning functions monomorphise before AIR, so the backend sees only concrete code — the same position D templates put an LDC backend in. Function pointers are not represented: their indirect form is ausizeplaceholder ("TODO: Represent function pointers properly.").
Resources, interfaces and memory layout
Address spaces are the resource model. std.lang.AddressSpace carries GPU members (global, constant, param, shared, local, private, input, output, uniform, push_constant, storage_buffer, physical_storage_buffer), and CodeGen.storageClass maps them onto SPIR-V storage classes:
// src/codegen/spirv/CodeGen.zig — storageClass
.generic => .function,
.global => switch (target.os.tag) {
.opencl, .amdhsa => .cross_workgroup,
else => .storage_buffer,
},
.constant => .uniform_constant,
.shared => .workgroup,
.local => .function,
.private => .private,The first line is the generic-address-space workaround from the blog: any pointer whose type does not name an address space is a Function pointer. Code that is generic over "a pointer" therefore compiles only when the pointee really is function-local; passing a pointer into a storage buffer through it is a type error resolved by @addrSpaceCast, whose own documentation says the cast "may be a no-op, a complex operation, or illegal" depending on target (langref). The language reference entry for the addrspace keyword itself reads TODO add documentation for addrspace at the pin.
Decorations are a closed union, ExternOptions.Decoration = { location: u32, flat: u32, descriptor: { set, binding } }. flat emits Location plus Flat; there is no noperspective, centroid, sample, invariant or Component. Thread-local globals (threadlocal var) become Private OpVariables with an initializer — the natural per-invocation reading of TLS.
Layout. For uniform, push_constant, storage_buffer and physical_storage_buffer externs, decorateLayout writes Offset decorations from Zig's own ABI (ty.structFieldOffset) and ArrayStride from abiSize; packed structs are skipped, unions get explicit padding arrays, and Block is applied to the root. Nothing in decorateLayout computes or checks std140/std430 rules: the layout is correct for Vulkan exactly when the user's extern struct already satisfies the rules the validator applies. A const storage-buffer extern is decorated NonWritable; image access qualifiers map to NonWritable/NonReadable.
Addressing model. Flush.emitMemoryModel picks Logical for spirv32-vulkan and OpenGL, but PhysicalStorageBuffer64 for spirv64-vulkan, adding the PhysicalStorageBufferAddresses capability and SPV_KHR_physical_storage_buffer automatically. Integer-to-pointer casts are permitted on Vulkan only into physical_storage_buffer; otherwise "cannot cast integer to pointer with address space '…'". The architecture choice (spirv32 vs spirv64) thus silently selects whether buffer device addresses are in play.
Built-in library and intrinsics
There is no shading-language standard library. Zig's math builtins lower to the target's extended instruction set — GLSL.std.450 on Vulkan/OpenGL, OpenCL.std on OpenCL (importExtendedSet) — and std.spirv (source) adds a thin layer written in SPIR-V inline assembly:
std.spirv function | SPIR-V it emits |
|---|---|
controlBarrier, memoryBarrier, workgroupBarrier | OpControlBarrier, OpMemoryBarrier |
specConst(T, default, spec_id) | OpSpecConstant{True,False,}/…Composite + SpecId |
imageSampleImplicitLod(sampled_image, coord) | OpLoad + OpImageSampleImplicitLod |
imageQuerySize, imageWrite | OpImageQuerySize, OpImageWrite |
setMeshOutputs | OpSetMeshOutputsEXT |
Each wrapper reflects on its argument with @typeInfo(...).spirv and turns misuse into a @compileError ("Can not implicitly sample a sampled image that was multisampled"). Missing at the pin: explicit-LOD and gradient sampling, derivatives, subgroup operations, atomics beyond what the language's @atomicRmw lowers to, and every GLSL-level compound (mix, smoothstep, reflect).
Precision is the finding. Zig's semantics for @mulAdd, @sqrt, @exp, @log are correctly rounded (as OpenCL's are); Vulkan's GLSL.std.450 versions are not. The backend maps them anyway and leaves notes in the source:
// src/codegen/spirv/CodeGen.zig
// NOTE: Vulkan's FMA does not meet Zig's nor OpenCL's precision guarantees and needs
// to be emulated.
.vulkan, .opengl => @backingInt(spec.GlslOpcode.Fma),// Note: We'll need to check these for floating point accuracy
// Vulkan does not put tight requirements on these, for correction
// we might want to emulate them at some point.
.vulkan, .opengl => @backingInt(@as(spec.GlslOpcode, switch (op) { … .sqrt => .Sqrt, .exp => .Exp, … })),So a Zig function that computes @sqrt(x) gives a different, implementation-bounded answer on a Vulkan GPU from the one it gives on the CPU, and nothing in the type system or the build says so. The Vulkan precision table is the contract the GPU actually meets.
Control flow, memory model and restrictions
Structured control flow is generated, not recovered. Zig's AIR is already block-structured (block, loop, cond_br, switch_br, br to an enclosing block), so the backend emits OpSelectionMerge/OpLoopMerge directly while walking it. A Block is either a selection — which "can't use early exits", so closing it "requires a 'merge ladder' of nested OpSelectionMerge instructions, one per pending merge" — or a loop, which "early-exit[s] by jumping to the loop merge label" (CodeGen.zig). Breaks out of nested selections are routed through a next-block id compared at each merge (structuredNextBlock/structuredBreak). There is no structuriser pass over an unstructured CFG. At the older pin, an unstructured mode existed for OpenCL behind structured_cfg (default false only for opencl, src/Package/Module.zig at 1bcd8d9f); at the current pin the option is gone and all SPIR-V is structured.
Memory model. OpMemoryModel is GLSL450 on Vulkan/OpenGL and OpenCL on OpenCL — not Vulkan. Barriers take explicit Scope and MemorySemantics (a packed u32 mirroring the SPIR-V bits) through std.spirv.
Restrictions (each a cg.fail/cg.todo at the pin unless stated):
- No function pointers or indirect calls (placeholder
usize); no recursion support of any kind, since SPIR-VShaderhas no call stack and nothing emulates one. - No
memcpy("TODO implement airMemcpy for spirv"), no alignment on variables outside OpenCL, no opaque types outside OpenCL, no field access into packed structs or unions, inline assembly with at most one returned output. - In logical addressing, composites with pointer members cannot be built with
OpCompositeConstruct;constructCompositespills to a local variable, stores each field, and loads. - Behaviour-test skips: 897
stage2_spirv) return error.SkipZigTestlines acrosstest/behavior/against 2140test "…"declarations (counted withgit grepat the pin; one skip line per skipped test or helper, so the ratio is approximate). The devlog reports 49% of behaviour tests passing onspirv64-vulkanafter the overhaul; the blog reported ~50% on Vulkan 1.2 and ~75% on OpenCL in April 2025, adding that "these numbers are not expected to get significantly higher since a lot of these tests are not meant to be working in GPUs anyway."
Compilation pipeline and targets
.zig ──► AstGen ──► ZIR ──► Sema ──► AIR ──► spirv/CodeGen.zig ──► Mir
│
external .spv objects ───────────────────────► link/Spirv.zig + Flush.zig
│
module.spvInvocation, from the blog (the flags predate the overhaul, but the target triples and -ofmt=spirv -fno-llvm are unchanged):
zig build-obj -target spirv64-vulkan-none -mcpu vulkan_v1_2+int64 -ofmt=spirv -fno-llvm kernel.zig
zig build-obj -target spirv64-opencl-none -mcpu opencl_v2+int64 -ofmt=spirv -fno-llvm kernel.zig- Capabilities are target features.
Flush.emitCapabilitiesemitsShader(orKernel+Addresses), then oneOpCapabilityper SPIR-V capability whose name is astd.Target.spirv.Featureenabled by-mcpu, and likewise for extensions; the dependency chains come from SPIRV-Headers (devlog). A feature the code needs but-mcpudid not enable is a compile error at the use (f64, mesh conventions), not a validation error later. - Object files and linking.
.spvfiles are object files; when emitting an object (build-obj) that exports symbols, theLinkagecapability is emitted, so separately compiled modules can be linked. - Interface lists are computed.
emitEntryPointswalks each entry point's call graph, collecting every non-Functionvariable it reaches (onlyInput/Outputbefore SPIR-V 1.4, all of them from 1.4). - Output is SPIR-V only. No GLSL, HLSL, MSL or WGSL; reaching those is left to translators such as SPIRV-Cross, naga or Tint. Note that
spirv64-vulkanoutput usesPhysicalStorageBuffer64addressing, which is outside the subset some of them accept. - Two backends can emit SPIR-V.
hasLlvmSupportacceptsspirv32/spirv64, andsrc/Compilation/Config.zigprefers LLVM "for release builds" whenever the user has not chosen; in Debug the self-hosted backend wins becauseselfHostedBackendIsAsRobustAsLlvmreturnstruefor SPIR-V. Everything this deep-dive describes is the self-hosted path, which is why the blog's commands pass-fno-llvm; a release build without it goes through LLVM's SPIR-V target instead (see LLVM SPIR-V backend).
Host integration and CPU execution
There is no host API. Zig ships no Vulkan, OpenCL or CUDA runtime binding in std; the blog lists "Provide bindings to CUDA/HIP runtime" as future work, and its complete example points at a third-party project (snektron/shallenge). The host loads the .spv through whatever Vulkan or OpenCL binding it uses and must reproduce the set/binding/location numbers written in the @extern decorations by hand — no reflection data is emitted.
CPU execution is "compile the same file for another target", with limits. Plain Zig functions with the default calling convention compile for any target, so helper logic can be shared between a CPU build and a shader build. Everything that is SPIR-V-specific refuses to compile for the CPU: @SpirvType is a compile error on x86_64, the spirv_* calling conventions are rejected outside SPIR-V, and std.spirv is extern declarations in SPIR-V-only address spaces plus SPIR-V inline assembly. There is no CPU implementation of sampling, barriers or built-ins, and — the precision finding above — no promise that the shared helpers compute the same values.
Tests run as kernels, but not in CI. generateTestEntryPoint lowers each Zig test into its own entry point that stores the returned anyerror into a buffer, with the comment "The SPIR-V backend is not yet advanced enough to support the std testing infrastructure. In order to be able to run tests, we 'temporarily' lower test kernels into separate entry-points. The test executor will then be able to invoke these to run the tests."; the linker writes the error names into an OpSourceExtension so the executor can print them. The executor is not in the tree, and test/tests.zig only builds SPIR-V test binaries (// Don't run spirv binaries) — and the Vulkan entry is commented out: "Disabled due to no active maintainer (feel free to fix the failures and then re-enable at any time). The failures occur due to changing AIR from the frontend, and backend being incomplete." Compile-only cases in test/cases/ still run.
Lessons for sparkles:shaders
- Adopt: execution-mode data on the entry-point attribute, never in inline assembly. Zig's move from
std.gpu.executionMode()tocallconv(.{ .spirv_fragment = .{ .depth_assumption = .greater } })— with the assembler then rejectingOpExecutionMode— is the shape a D@fragment(...)/@compute(x, y, z)UDA payload should have: a typed struct the compiler validates ("kernel workgroup dimensions must be at least 1") and emits. - Adopt: capabilities from the target, checked at the use. Zig derives
OpCapability/OpExtensionfrom-mcpuand fails at thef64or mesh use when the feature is off. A D device module should declare its required features (a module-level UDA) and the build should reject uses outside them, rather than lettingspirv-valor a translator find out. - Match, and test: the GPU's precision, not the host's. Zig's
@sqrtand@mulAddare correctly rounded on the CPU and are not on Vulkan, and the backend only leaves a note. The vocabulary must state GLSL/Vulkan precision as its contract and the agreement oracle must judge by it — exactly what decisionD12indecisions.mddoes with the Vulkan precision table. Do not expose anfmawhose CPU and GPU meanings differ without saying so in its type or name. - Avoid: a generic pointer that silently means
Function. "All pointers are assumed to be local (Function) memory" turns every generic-pointer helper into a latent compile error or miscompile on buffers. D shader code should pass resources byrefor by typed wrappers (dcompute'sGlobalPointer!T-style, seedcompute.md) and monomorphise per storage class, with a clear diagnostic when an untyped pointer reaches a non-Functionresource. - Avoid: layout taken from the host ABI without a check. Zig decorates
Offsetfrom its own struct layout and leaves std140/std430 conformance to the user. A D design should derive offsets for the chosen block layout from one struct andstatic assertthem against the host layout (theSHV2decision forvec3generalises), so a mismatch is a D compile error. - Adopt: map D's thread-local module variables to
Private. Zig lowersthreadlocal varto aPrivateOpVariable; D's module-level variables are thread-local by default, so the same mapping gives them per-invocation meaning on the device instead of rejecting them. - Avoid: an untested backend. Zig's SPIR-V behaviour suite was disabled when its maintainer stepped away and bit-rotted until a rescue overhaul. Every
sparkles:shadersrequirement already has a test oracle (testing.md); the lesson is to keep a GPU-executing conformance run in CI (software rasterizer orlavapipe), not compile-only checks.
Strengths
- No LLVM in the path: AIR is block-structured, so structured control flow falls out of codegen with no structuriser, and the backend controls every emitted instruction.
- The full language front end (
comptime, generics,@Vector, error unions) is available to shader code; monomorphisation happens before the backend. - Typed, validated configuration: calling-convention payloads, closed decoration unions checked against address spaces in Sema,
@SpirvTypeplacement rules, and target-feature-driven capabilities, each with a precise compile error. - Graphics, compute, task and mesh stages from one compiler; separate compilation and linking of
.spvobjects. - Source comments state the semantic gaps honestly (FMA precision, function pointers, the test executor) — the backend's limits are discoverable from the code.
Weaknesses
- A generic pointer is a
Functionpointer; generic Zig code over pointers does not port to resources without@addrSpaceCastor rewrites. - Vulkan math is mapped to
GLSL.std.450despite Zig's correctly rounded semantics; no emulation, no diagnostic. - No std140/std430 layout derivation or check; layout correctness is the user's.
- Thin built-in library: implicit-LOD sampling, image size/write, barriers, spec constants; no derivatives, subgroups, explicit-LOD sampling or GLSL compounds, no swizzles, no matrices.
- No host runtime, no reflection output, no CPU implementation of the GPU-specific surface.
- Roughly half the behaviour suite passes on Vulkan, the CI target is disabled, and the backend depends on one maintainer; it bit-rotted once already (2025-09 to 2026-06).
- SPIR-V only;
spirv64-vulkandefaults to physical addressing, narrowing what translators accept.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
| Self-hosted backend from AIR, not LLVM | AIR is structured and typed; no dependence on LLVM's SPIR-V maturity | Every instruction, type and layout rule is hand-written and maintained by one person |
| Stages and execution modes as calling conventions | One typed place for stage data; Sema validates it; assembler forbids bypasses | New stages need language changes (std.lang), not library additions |
| GPU storage classes as Zig address spaces | Reuses an existing language feature; pointer types carry the storage class | generic defaults to Function, so idiomatic generic-pointer code breaks on resources |
Interface via extern globals with a closed Decoration union | Mirrors SPIR-V's global OpVariable model; easy to validate | No interpolation qualifiers beyond flat; bindings are hand-numbered and not reflected to the host |
Capabilities and extensions from -mcpu features | Same mechanism as every other target; dependency chains from SPIRV-Headers | Users must know which feature names a shader needs |
Map math builtins straight to GLSL.std.450 | Uses hardware instructions; fast | CPU and GPU results differ beyond Zig's documented semantics, silently |
Layout from Zig's ABI (structFieldOffset) | One layout for host and device structs; no second layout engine | std140/std430 conformance is unchecked |
| Tests lowered to per-test entry points with an error buffer | Reuses the behaviour suite without std.testing on the GPU | Needs an out-of-tree executor; CI now only compiles |
Sources
- Zig repository on Codeberg, pinned at
d924f83bf3e1a5726d9eca1e9d501aece08107c5:src/codegen/spirv/CodeGen.zig— storage classes, layout, structured CFG, FMA/precision notes, test entry pointssrc/codegen/spirv/Assembler.zig— rejection ofOpExecutionMode/OpCapabilitysrc/link/Spirv.zig·src/link/Spirv/Flush.zig— capabilities, memory model, entry points, execution modeslib/std/spirv.zig— built-ins, barriers, spec constants, image wrapperslib/std/lang.zig—CallingConvention,AddressSpace,Type.Spirv,ExternOptionssrc/target.zig— backend selection ·test/tests.zig— disabled SPIR-V targettest/behavior/spirv.zig·test/cases/callconv_spirv.zig·extern_spirv_decoration_validation.zig·directly_embedding_spirv_type_in_struct_and_union.zig·callconv_spirv_on_unsupported_platform.zigdoc/langref.html.in—@SpirvType,@addrSpaceCast- Earlier pin
1bcd8d9fe60f72849254b7f74d9ea0f48eae6aaa:lib/std/gpu.zig— the removedexecutionMode()helper
- Ali Cheraghi, "Zig and GPUs" (2025-04-17)
- Zig devlog 2026, "SPIR-V Backend Progress" (2026-06-26)
- Vulkan specification — precision of SPIR-V instructions
- Related: LLVM SPIR-V backend · rust-gpu · Vcc / Shady · dcompute · SPIRV-Cross · concepts · comparison · catalog index
- Sparkles:
sparkles:shadersSPEC · decisions · testing