Skip to content
Systems programming for what’s next.
— Vex

GPU and Compute with SIR ​

Silicon IR (SIR) is Vex's graph representation for data-parallel computation. It separates the computation from backend generation and execution. Start with an algorithm whose shape, element type and expected results are clear, then validate the chosen device path.

Graph-Based Compute Model ​

A graph fn is a computation candidate. It is not a manually launched kernel, and it does not guarantee that a GPU will be selected. The complete graph addition example shows the source, command and expected output for a small CPU-only starting point.

A multiply followed by a reduction can be represented as a map/reduce graph, but lowering depends on the exact source types and supported operations. In particular, converting a borrowed Span<T> to an owning Tensor<T> needs an owned materialization; do not hide that conversion in a performance example. See Tensor ownership.

Metal GPU Backend Architecture ​

Types and Precision ​

Metal generation emits MSL for supported SIR graphs. Metal has no native f64 lane type: development generator paths can narrow some operations to float, while other operations reject the type. Select a supported dtype and compare results against a reference using an explicit tolerance.

For floating-point workloads, include cancellation, NaN, infinity and signed zero when they matter to the application. Reduction order can change rounding because floating-point addition is not associative. A successful compile or a matching result on one input does not establish equivalence across backends.

Reduction and Buffer Planning ​

Some fused reductions use separate reduction and map passes with temporary scalar buffers. The number of passes, workgroup dimensions and buffer binding indices belong to the generated kernel and runtime descriptor. They are not fixed user-facing ABI rules. Do not hard-code internal buffer slots from an architecture diagram into a foreign integration.

Shared Memory ​

Unified physical memory can reduce transfer costs on Apple Silicon. It does not prove that a particular call is zero-copy: allocation, buffer wrapping, resource lifetime and synchronization still have costs. Measure the actual application boundary, including both cold and warm execution.

Runtime Deferred Dispatch ​

Runtime-sized work can use shape and cost information to choose among available routes. Selection requires more than an element-count threshold:

  • the graph's operations and dtypes must be supported;
  • the build must contain a compatible generator and call bridge;
  • the runtime must provide an executor and device;
  • the input and output layouts must match the execution descriptor.

A cost-model estimate is not a speedup guarantee. Threshold constants are implementation details and may differ between dispatch paths. Neither clamping a length nor applying noinline makes an invalid or dangling span safe; callers must satisfy the memory contract before dispatch.

Target Architectures and Backend Pipelines ​

PathWhat to establish
CPU scalar / SIMDSupported lowering, target features and a correct reference result.
MetalMSL support plus the native Metal runtime/device boundary.
CUDAPTX support plus the CUDA runtime/device boundary.
ROCmCompatible compiled HSACO; generated HIP text alone is insufficient.
SPIR-V / WebGPUMatching adapter capabilities, shader format and runtime bridge.
CoreML and other inference pathsSupported model operations, conversion and actual runtime placement.

The backend reference distinguishes generation from execution. Operating-system names alone do not identify a working route, and a native WebGPU path is not evidence of browser deployment.

Runtime Controls ​

The development compiler recognizes presence-based routing switches:

sh
# Request CPU-only routing while compiling and running this program.
VEX_NO_GPU=1 vex run graph-add.vx

# Development diagnostic: request GPU consideration where supported.
VEX_FORCE_GPU=1 vex run graph-add.vx

Unset these variables to restore default behavior; setting them to 0 is not equivalent to unsetting them in the inspected implementation. A force flag cannot install an absent executor or make unsupported operations valid.

VEX_GPU_BACKEND is route-specific. The SIR policy recognizes values including metal, cuda, rocm, spirv and wgsl; the native compiler bridge separately recognizes auto, metal and wgpu/webgpu. Do not assume a value is a stable end-to-end backend selector. Retain the selected-route diagnostics when testing an override. VEX_FUSION_DEBUG=1 can expose graph/plan diagnostics in development builds; their text is not a stable API.

A Reliable Validation Sequence ​

  1. Save the full input and expected output for a small reference case.
  2. Test the same algorithm with the requested production dtype and shapes.
  3. Include empty inputs and sizes around vector, tile and workgroup boundaries.
  4. Record backend, device, compiler hash and relevant configuration.
  5. Compare integer outputs exactly; define floating-point tolerances from the algorithm's requirements before measuring speed.
  6. Measure device execution separately from allocation, transfer and cold shader/model compilation.

Next Steps ​

Released under the MIT License.