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

SIR Optimization Passes ​

SIR optimization transforms a data-parallel graph and produces analysis hints for a selected backend. A graph rewrite, an allocation plan and an executed GPU kernel are different outcomes. This page follows the function bodies in crates/vex-sir/src/passes/pipeline.rs; backend emission and runtime dispatch remain separate stages.

Pass Pipeline Order ​

SIR levelOrdered work in the shared optimizer
O0Legalization
O1Legalization → simplify → layout rewrite → dead-code elimination
O2Legalization → simplify → layout rewrite → fusion → dead-code elimination → analysis
O3Legalization → simplify → layout rewrite → fusion with a larger chain limit → simplify → layout rewrite → dead-code elimination → analysis

The O2/O3 analysis stage computes tiling strategies, matrix blocking, a memory plan and device-routing decisions. These are metadata in SirOptResult and SirOptHints; a backend must consume them before they can affect generated work. The selected level does not establish a launch count, register count or measured speedup. Legalization still runs at O0.

The optimize_sir_for_metal entry currently delegates to optimize_sir. Its name does not imply that a separate automatic NHWC conversion runs.

1. Simplify Pass ​

Simplification folds constants, recognizes common subexpressions and removes eligible redundant operations. For example, an expression containing constant 2.0 + 3.0 may use 5.0 in the residual graph. It need not have executed the addition at runtime before optimization.

Algebraic identities require the operation's actual numeric rules. For floating point, x * 0 and x - x do not universally equal positive zero: NaN, infinity and signed zero matter. A list of rewrite patterns is not permission for an application to discard those cases. Compare optimized output with a reference under the precision policy used by the selected backend.

2. Fusion Pass ​

Fusion can represent a compatible chain of element-wise operations as one FusedKernel, reducing intermediate values that would otherwise be materialized. The operations normally do depend on each other: a producer's result feeds a consumer. Other uses of that producer and operation constraints affect the fusion decision.

For an affine transform followed by clamping, the conceptual expression is:

text
input → add bias → multiply scale → clamp below at zero → output

A fused representation can compute each output lane without storing every intermediate array. It does not automatically promise one hardware launch. There is no public thread.x or threadgroup syntax implied by this diagram.

A reduction followed by normalization has a whole-input dependency: the normalization cannot use a complete sum until that sum exists. Combining the expressions into one graph does not remove that dependency or make a workgroup-local barrier a device-wide barrier. See the graph guide for an executable starting point.

3. Layout Pass ​

The wired LayoutRewritePass removes identity permutations and inverse permutation chains. It does not universally convert convolutions to NHWC or matrix operations to tiled storage merely because a device is a GPU.

Shape and layout are distinct: reshaping changes the logical dimensions; permuting dimensions may require a different access pattern or materialization. A consumer must interpret the resulting strides/layout consistently with its producer. Source-level flat storage alone does not encode a matrix shape.

4. Tiling Pass ​

Tiling analysis chooses strategies for working on bounded portions of a tensor. It can improve locality when emitted code reuses those portions. The number of tiles is not the number of kernel launches: one launch may contain many workgroups, and one workgroup may iterate over several tiles.

Edge tiles require bounds checks, including an incomplete reduction dimension. If a GPU implementation uses shared storage, all participating lanes must obey the barrier protocol. The tiled reference model explains these dependencies with non-multiple dimensions.

5. MatMul Pass ​

MatMulTilingPass.analyze produces blocking metadata. The existence of this analysis does not establish automatic Tensor Core, AMX or SME instruction use. That requires a compatible emitter, device, element type and matrix shape.

Precision conversion is a separate numerical decision. An f32 matrix operation is not permission to silently approximate it with integer or half-precision multiplication. Validate the exact input/output and accumulation types, as well as exceptional values and error tolerance.

6. Memory Pass ​

MemoryPlanningPass.plan analyzes buffer lifetimes and reuse. Two temporary buffers can share storage only when their live ranges and layout requirements permit it. A reusable-buffer plan is not itself a guarantee that all runtime allocations disappeared.

Memory planning should not be described as universally inserting prefetches, rewriting thread coordinates or removing GPU bank conflicts. Those are distinct backend transformations requiring their own emitted-code evidence.

7. Quantize Pass ​

The repository contains a QuantizationPass type, but the shared pipeline does not call it. There is no documented #[quantize(f16)] switch or --sir-quantize option that enables an automatic quantization workflow here. A deliberately quantized representation needs its own format, calibration and numerical acceptance criteria.

8. DCE (Dead Code Elimination) ​

Dead-code elimination removes graph nodes not needed by the outputs. It is particularly useful after fusion or simplification disconnects intermediate nodes. This graph reasoning does not authorize deleting arbitrary Vex function calls whose return values are unused: such functions may perform I/O or mutate observable state.

9. Legalize Pass ​

Legalization adapts operations and types to target capabilities. It runs before simplification and fusion so subsequent stages see the legalized graph. A backend may need casts or decomposed operations; some cases remain unsupported.

Narrowing is not numerically transparent. In particular, supported source f64 types do not prove preserved double precision on Metal or WGSL routes. See the backend reference and SIMD/SIMT contract for the concrete boundaries.

10. Autograd Pass ​

The shared SIR pipeline does not invoke its AutogradPass type. A plain forward graph fn does not automatically generate the training backward graph shown in older versions of this page. Vex's documented forward-mode autograd uses explicit @param, @val, @grad and @detach transforms; it is a separate surface.

Pass Configuration and Verification ​

Use documented compiler options, starting with vex compile --help and the CLI reference. For a native comparison:

sh
vex compile graph-add.vx -O0 --emit-llvm -o graph-add-o0
vex compile graph-add.vx -O2 --emit-llvm -o graph-add-o2

The graph-add.vx source is provided in the graph quick start. Compare output before examining IR for a performance claim. LLVM output describes the native module; it alone does not prove that a device kernel ran.

For development builds, VEX_FUSION_DEBUG=1 can expose graph/plan diagnostics. Record the compiler identity, environment, target and observed route alongside those diagnostics. Internal trace text is not a stable machine-readable API. The previously documented --sir-disable-fusion, --sir-disable-quantize, --sir-tile-size and --sir-profile flags are not the CLI surface.

Released under the MIT License.