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

Graph Functions ​

A graph fn describes a computation the compiler can lower to a Silicon IR (SIR) graph. Depending on the supported operations, compiler build, and runtime environment, it may optimize that graph and select an execution path. Neither GPU execution nor fusion is guaranteed by the keyword.

Quick Start ​

Save this complete example as graph-add.vx:

vex
graph fn add(a: [i32; 4], b: [i32; 4]): [i32; 4] {
    return a + b;
}

fn main(): i32 {
    let x: [i32; 4] = [1, 2, 3, 4];
    let y: [i32; 4] = [5, 6, 7, 8];
    let result = add(x, y);
    $println(result[0]);
    $println(result[1]);
    $println(result[2]);
    $println(result[3]);
    return 0;
}
sh
VEX_NO_GPU=1 vex run graph-add.vx

Expected standard output:

text
6
8
10
12

VEX_NO_GPU=1 requests CPU-only routing for this run. The source expresses four independent additions; the chosen scalar or vector implementation is a compiler decision. Inspect the generated plan when the execution route matters.

Calibrating a batch ​

A sensor batch often needs more than one arithmetic operation. This graph subtracts a baseline and applies a per-lane gain. The caller then summarizes those calibrated values with an ordinary reduction and threshold mask:

vex
graph fn calibrate(raw: [i32; 4], baseline: [i32; 4], gain: [i32; 4]): [i32; 4] {
    return (raw - baseline) * gain;
}
fn main(): i32 {
    let raw: [i32; 4] = [12, 13, 14, 15];
    let baseline: [i32; 4] = [10, 10, 10, 10];
    let gain: [i32; 4] = [10, 10, 10, 10];
    let calibrated = calibrate(raw, baseline, gain);
    let total = <+ calibrated;
    let high = calibrated > 35;
    $println(calibrated[0]); $println(calibrated[3]);
    $println(total); $println(high.countBits());
    if calibrated[0] != 20 || calibrated[3] != 50 || total != 140 || high.countBits() != 2 { return 1; }
    return 0;
}

Run VEX_NO_GPU=1 vex run graph-calibration.vx. Expected output:

text
20
50
140
2

The four calibrated lanes are [20, 30, 40, 50]. Their total is 140, and two lanes exceed 35. The fixed array types require the raw values, baselines, and gains to have matching extents. A per-lane gain allows different sensor calibration coefficients without changing the graph structure.

The graph returns application data; the caller decides how to classify and report it. Keeping that boundary explicit also gives a straightforward reference calculation: lane i should equal (raw[i] - baseline[i]) * gain[i]. Use values whose intermediate results fit the selected integer type. For floating-point calibration, specify acceptable error and rounding behavior.

The expression exposes subtraction and multiplication together to SIR, which can optimize supported combinations. It does not promise a specific number of instructions, allocations, or launches. For a small fixed batch, compare the ordinary array expression with the graph form before choosing a performance strategy.

Why Graph Functions? ​

A graph gives the optimizer an opportunity to analyze operations together. Compatible operations may be fused to avoid intermediate work, but the number of loops, allocations, or kernel launches depends on the actual lowering and chosen backend. Measure the compiled program rather than assuming a speedup.

Supported Operations ​

Graph operations preserve their element types and shapes. A computation can be planned only when its operations have a compatible lowering and backend. Keep allocation, I/O, reporting, and application control flow outside the graph unless their graph semantics are explicitly supported. This creates a useful boundary between a numerical transformation and the code that consumes it.

Nested Graph Functions ​

Calls and cross-function optimization depend on the compiler's current lowering support. Check your actual program rather than assuming every graph call is inlined or fused.

Hardware Dispatch ​

SIR has dispatch policies for different execution paths. Backend availability and support for a particular graph affect the plan; input size alone does not guarantee a particular GPU or SIMD route. VEX_NO_GPU=1 requests CPU-only routing; selecting a GPU-capable host alone does not determine how a particular graph executes.

Analytical Fusion & DAG Compilation Pipeline ​

The Lowering Flow ​

For supported graph bodies, the compiler lowers operations into an SIR graph, applies available optimization passes, and plans a backend. A map followed by a reduction is a possible fusion candidate, not a promise that a given expression will lower or fuse.

The FusedKernel Structure ​

SIR represents some fused operations with a FusedKernel containing inputs and an operation chain. Backend support determines whether and how that representation can be emitted. See the SIR pipeline for more context.

Inline Fusion vs Graph Functions ​

An expression and a graph function give the compiler different scopes to analyze. Both remain subject to the lowering and backend support of the operations involved.

Debug ​

For a development build, VEX_FUSION_DEBUG=1 vex run graph-add.vx can print graph and plan diagnostics. These diagnostics describe the observed build and run, and are not a stable API. The examples above use VEX_NO_GPU=1 for CPU-only routing.

For a conceptual tiled matrix multiplication and the distinction between a graph and a manual GPU kernel, see Graph Functions and GPU Execution.

Released under the MIT License.