Skip to main content

GPU Graph control flow

Overview​

Use graph.if(predicate, body) to guard a sequence of operations and graph.loop(options, body) to repeat one. A body receives a GPUGraphScope with the same add, if, and loop methods as the graph. Return the scope to capture its steps. Each control-flow method returns the enclosing graph or scope for chaining.

const graph = new GPUGraph();
const active = graph.compiler.scalar('active', 'uint32');
const ready = graph.compiler.scalar('ready', 'uint32');
const keepGoing = {id: 'active', source: 'gpu' as const, value: active};
const hasUpdate = {id: 'ready', source: 'gpu' as const, value: ready};

graph
.add(gpuScalarLiteral, {output: active, value: 1})
.add(gpuScalarLiteral, {output: ready, value: 1})
.loop({id: 'solve', predicate: keepGoing, maximumIterations: 20}, scope =>
scope.if(hasUpdate, branch =>
branch.add(gpuScalarLiteral, {output: active, value: 0})
)
);

The branch clears active when ready is nonzero, so the next iteration stops. The body callbacks run once when you add the control flow. During execution, the GPU evaluates the predicates and runs the captured steps. Use named resources from the enclosing graph inside any nested scope.

Build a body separately​

Use graph.createScope() to assemble a detached scope. Pass it to if() or loop() directly, or add it to the graph as a named group. Each use captures the steps present at that moment. Additional calls to body.add() affect subsequent captures. Create the scope from the same graph that will use it.

const body = graph.createScope({id: 'update'}).add(gpuScalarLiteral, {
output: active,
value: 0
});

graph.if(hasUpdate, body, {id: 'apply-if-ready'});
graph.loop({id: 'repeat-update', predicate: keepGoing, maximumIterations: 4}, body);

At a glance

QuestionAnswer
ProblemRepresent bounded loops and conditional branches in a backend-independent program.
Reads / writesPredicates reference logical uint32 scalar state; child operations declare data use.
OwnershipControl operations own semantic hierarchy but no buffers or command submission.
Output contractA bounded operation subtree with explicit predicate and iteration semantics.
Expected workMaximum iterations provide a static upper bound for backend planning.
ChunksChild operations retain their declared logical shapes and chunk behavior.
Conditions / budgetsWebGPU lowers runtime predicates to bounded GPU-gated indirect dispatch sequences.
Neighborhoodlogical predicate + operation body → control-flow lowering → conditioned nodes.

Predicates and iteration bounds​

A GPUGraphPredicate references a logical uint32 scalar. Zero is false and any nonzero value is true. An optional expression describes the condition in reports.

const active = graph.compiler.scalar('active', 'uint32');
const keepGoing = {
id: 'residual-above-tolerance',
source: 'gpu' as const,
value: active,
expression: 'residualSquared > toleranceSquared'
};

Every loop requires a positive maximumIterations. Use minimumIterations for iterations that must execute before the predicate is tested. Omit the predicate to execute the bounded body a fixed number of times. For a convergence loop, update the predicate's scalar inside the body so the next iteration can see the new result.

Lowering and inspection​

On WebGPU, fixed-count loops expand into a bounded sequence of commands. For an if or a loop with a predicate, the compiler gates eligible compute commands with GPU-written indirect arguments. Each gated command needs exact dispatch geometry. See WebGPU runtime control for the execution contract.

The graph's operation tree retains the branch or loop and its captured children. The lowering report links any expanded command nodes to that semantic path. Set an explicit id on important branches and loops so reports identify them by name.