# SIR Backend Reference

Silicon IR (SIR) describes computations independently of a particular device.
A backend translates supported graph nodes to a target representation. An
executor must then load that representation, bind buffers, dispatch work, and
return results. These are separate capabilities: a generated shader is not
proof that an application can execute it on a selected device.

## Backend Overview

The following is an implementation map for the 0.4.0 documentation. Support
must be checked for the actual operations, element types, shapes, build
features and runtime device. It is not an all-operations compatibility table.

| Backend family | Generated representation or role | Execution boundary |
| --- | --- | --- |
| Metal | Metal Shading Language | The native Metal executor requires a macOS build with Metal support and an available device. |
| CUDA | PTX | The CUDA executor requires the compiled CUDA feature, driver/device support and compatible generated PTX. |
| ROCm | HIP source; native execution consumes HSACO | HIP text is not an executable code object. The direct graph execution path rejects attempts to use it without the required compilation step. |
| SPIR-V / Vulkan | SPIR-V modules and Vulkan-oriented generation | Module features must match the selected adapter, including subgroup operations. |
| WGSL / WebGPU | WGSL generation and a separate WebGPU runtime path | Native WebGPU, browser integration, shader generation and the compiler call bridge have distinct requirements. |
| OpenCL | OpenCL-oriented generation | Inspect the selected runtime integration; backend presence does not establish a driver or executor. |
| CoreML | CoreML model generation and an optional NPU dispatch path | Requires the macOS CoreML feature and a model supported by the runtime. The framework chooses device placement. |
| OpenVINO | Inference-oriented generation | Check model/operator support and runtime integration separately. |
| CPU SIMD / scalar | LLVM vector or scalar lowering | A CPU route must support the graph too; an unsupported GPU graph is not automatically executable on every CPU path. |

See [SIMD/SIMT execution contracts](../guide/simd/simd-simt-contract.md) for the
operation-specific mask, subgroup and compiler/runtime boundaries.

## Metal Backend

The Metal generator emits MSL for supported SIR nodes. Buffer layout, resource
binding, dtype support and synchronization are part of the generated-kernel
contract. Apple Silicon unified memory does not eliminate allocation, resource
wrapping, synchronization or possible copies in the actual integration.

Metal does not provide a native `f64` lane type. Some development generator
paths narrow `F64` nodes while others reject them. Use an explicitly supported
dtype and validate numerical error; do not treat CPU `f64` results as the
implicit semantics of a Metal `float` computation.

## CUDA Backend

PTX generation and CUDA execution are separate steps. Instruction selection
also depends on GPU capabilities and graph shape. A matrix expression does not
promise Tensor Core use, a particular tile size, or a cuBLAS call. Inspect the
emitted artifact and measure the actual workload before making such claims.

## ROCm Backend

The source contains a HIP generator and a native module executor that consumes
HSACO. The direct `execute_sir_graph` path explicitly rejects ROCm execution
when only generated HIP text is available. A deployment needs a compatible
compiled code object and its corresponding launch descriptor; renaming source
text to a binary artifact cannot supply that boundary.

## SPIR-V Backend

SPIR-V is an intermediate representation. A generated module must declare only
the capabilities available on the selected executor. Subgroup ballot,
arithmetic, barriers and width assumptions are checked separately. Driver
acceptance and output correctness remain device-level checks.

## WGSL Backend

WGSL generation does not imply that a Vex executable automatically runs inside
a browser. A WebGPU integration must arrange adapter/device creation, buffer
transfer, shader compilation and dispatch. Limits such as workgroup size and
shared-memory capacity come from the actual device, not a universal constant
in a tutorial.

<a id="vulkan-backend"></a>
<a id="opencl-backend"></a>
<a id="openvino-backend"></a>
<a id="coreml-backend"></a>
## Other Targets

Vulkan, OpenCL, OpenVINO and CoreML have distinct adapters and supported
operation subsets. Select the target from evidence for the intended workload,
including conversion cost and output precision. A compiler build containing a
generator is not a declaration that every runtime integration is complete.

## CPU SIMD Backend (Fallback)

LLVM can lower supported vector operations to target instructions or scalar
sequences. Vector width, instruction count and math-library calls depend on
the target features and optimization settings. Keep a known-correct CPU
implementation available for result comparison; that reference may be a
normal loop rather than the same graph routed through another backend.

## Backend Selection

### Automatic Selection

Dispatch considers graph support, estimated work, build configuration and
runtime capability. There is no documented universal ranking such as
“Metal, then CUDA, then every other backend”, and no stable `launch ... with
threads` syntax taught by this guide. Start with the complete
[graph-function example](../guide/fusion/graph.md#quick-start).

### Selection and capability checks

Compile-time `#Target.os()` and `#Target.arch()` describe the requested binary
target. They do not probe a GPU installed on a user's machine. Runtime backend
selection and compiler call-bridge selection must agree on the actual artifact
and ABI. Environment overrides are development controls, with route-specific
meanings; see [Runtime Controls](../guide/gpu/index.md#runtime-controls).

## SIR Optimization Passes

Fusion, simplification, layout selection, tiling, dead-code elimination and
other passes operate on graphs they support. Their presence does not imply
that every pass runs for every program. Fusion may reduce intermediate work,
while tiling can change temporary memory and synchronization requirements.
Precision-changing transformations require an explicit numerical contract.

## Validation Workflow

1. Define input shapes, dtypes, aliasing and ownership requirements.
2. Compare with a reference using small, empty and boundary-sized inputs.
3. Record the actual selected route, device, driver and compiler identity.
4. Exercise the production shape range, including tails and exceptional values.
5. Measure transfer, compilation, warm execution and synchronization separately.

The owning implementation areas are `crates/vex-sir/src/codegen/backends/`,
`crates/vex-sir/src/runtime/gpu_executor/factory.rs`,
`crates/vex-sir/src/dispatch/policy.rs` and the compiler call bridges under
`crates/vex-compiler/src/codegen_hir/`. Consult the matching source revision
when diagnosing a boundary failure.

## Related Pages

- [SIR and Backends](sir-and-backends.md)
- [SIR Passes Deep Dive](sir-passes-deep-dive.md)
- [GPU Programming](../guide/gpu/index.md)
- [Graph Functions](../guide/fusion/graph.md)
