Skip to content

Runtime Control Flow

Ordinary if, for, and while inside a compiled function generate device control flow. Use legato.static for decisions and iteration performed during compilation; see Static Evaluation.

Conditions and ranges

An if or while condition must be a boolean scalar, such as count < bound. Integers are not implicitly converted to booleans. Use legato.range(start, end, step) for runtime for loops; step must be positive, and the range expression is evaluated once when the loop is entered.

Keep a runtime loop inside one execution context. On the current EVT0 backend, integer/index ordering comparisons must use nonnegative operands; signed ordering with negative values is not guaranteed. See Comparisons.

While and break

while tests its condition before entering the body and again after each completed iteration. A false initial condition executes no iterations. break exits the nearest while or for, preserves updates made before the break, and skips the rest of that body and the next condition evaluation.

The following frontend example carries two index values through a loop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import legato
import legato.model.bertha as bertha
from legato.language import arith


@legato.compile(backend=lambda ctx: bertha.Bertha(ctx, "bertha"))
def while_break_example(limit: legato.types.integer("int64")):
    with legato.get_context().get_device().get_top():
        count = arith.cast(0, legato.types.index())
        total = count
        bound = arith.cast(limit, legato.types.index())
        while count < bound:
            count = count + arith.cast(1, legato.types.index())
            if count == arith.cast(3, legato.types.index()):
                break
            temporary = count + arith.cast(2, legato.types.index())
            total = total + temporary
        yield count, total

For limit=7, the loop leaves count=3 and total=7: the third iteration breaks before the temporary value is added. yield exports values from the execution context; it does not return Python results from the kernel call. The control-flow notebook compiles this pattern and checks hscf.while, hscf.condition, and hscf.if. That check generates frontend IR and does not launch a device.

Carried state and limits

Values defined before the loop and reassigned in it become loop-carried state. For while and for loops with break, that state must be integer, index, or boolean scalars and must keep the same type across iterations. Tensor and floating-point carried state are not supported on these paths. A variable created inside a loop or branch is not visible outside that source block. Reassigning a for loop variable does not change the internal iteration index.

EVT0 and EVT1 support these runtime loop paths. The following constructs remain unsupported: continue, while ... else, for ... else, early return from the runtime loop body, and/or, and augmented assignment such as +=. A function called from the loop can use its own return.

A break cannot cross an inner device with context, a function or primitive boundary, or a static.for_each callback. Cross-device tensor channels still require statically scheduled messages and cannot be placed in runtime loops or branches; see Channel Operations. Runtime control flow alone does not guarantee visibility or progress for host/device or remote-memory polling.