Skip to content

Static Operations

Static

Static operations are operations evaluated in compile-time. This code does not get directly translated into runnable binary, but it can be used to create code that can be translated into binary.

for_each

1
for_each(iterable: Value, func: Value)

Apply a function to each element of an iterable at compile time.

This loop is evaluated statically; the body will be unrolled during compilation.

Parameters:

  • iterable (Value): Interpreted iterable value. Typically created with legato.range.
  • func (Value): Interpreted callable taking one argument (the current element) and returning an interpreted callable that accepts the CodeGenerator when invoked by the compiler.

Returns:

  • result (Value): An interpreted callable that, when executed by the compiler, evaluates the unrolled loop.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@legato.compile(backend=get_backend)
def fn():
    device = legato.get_context().get_device()
    with device.get_all_cores():
        legato.static.for_each(legato.range(0, 10), lambda x: x + 1)

@legato.compile(backend=get_backend)
def fn2():
    device = legato.get_context().get_device()
    def add_one(x):
        return x + 1
    with device.get_all_cores():
        legato.static.for_each(legato.range(0, 10), add_one)

empty_list

1
empty_list(dtype: Value)

Create an empty list of the given type at compile time.

Parameters:

  • dtype (Value): Constexpr value representing the type of the list.

Returns:

  • result (Value): Constexpr value representing the empty list.

Example:

1
2
3
@legato.compile(backend=get_backend)
def fn():
    values = legato.static.empty_list(legato.types.integer("int64"))

fold_back

1
fold_back(iterable: Value, func: Value, initial: Value)

Fold an iterable from the right at compile time.

Parameters:

  • iterable (Value): Interpreted iterable value to fold.
  • func (Value): Interpreted callable of two arguments (acc, current) returning the updated accumulator as an interpreted value. Note the reversed argument order compared to fold.
  • initial (Value): Interpreted initial accumulator value.

Returns:

  • result (Value): An interpreted value representing the right-fold result.

Example:

1
2
3
4
@legato.compile(backend=get_backend)
def fn():
    # Right-fold over [0, 1, 2, 3)
    result = legato.static.fold_back(legato.range(0, 4), lambda acc, x: acc + x, 0)

fold

1
fold(iterable: Value, func: Value, initial: Value)

Fold an iterable into a single value at compile time.

Parameters:

  • iterable (Value): Interpreted iterable value to fold, e.g. from legato.range.
  • func (Value): Interpreted callable of two arguments (current, acc) returning the updated accumulator as an interpreted value.
  • initial (Value): Interpreted initial accumulator value.

Returns:

  • result (Value): An interpreted value representing the folded result.

Example:

1
2
3
4
5
6
7
8
@legato.compile(backend=get_backend)
def fn():
    device = legato.get_context().get_device()
    # Compute 0 + 1 + 2 + ... + 9 at compile time
    total = legato.static.fold(legato.range(0, 10), lambda x, acc: x + acc, 0)
    with device.get_all_cores():
        x = total
    return

append

1
append(lhs_list: Value, value: Value)

Append a value to a list at compile time.

Parameters:

  • lhs_list (Value): Constexpr value representing the list to append to.
  • value (Value): Constexpr value representing the value to append.

Returns:

  • result (Value): Constexpr value representing the list with the value appended.

Example:

1
2
3
4
@legato.compile(backend=get_backend)
def fn():
    my_list = legato.static.append(legato.static.empty_list(legato.types.integer("int64")), 1) # [1]
    my_list = legato.static.append(my_list, 2) # [1, 2]

if_else

1
if_else(condition: Value, if_true: Value, if_false: Value)

Compile-time conditional that executes one of two branches.

Parameters:

  • condition (Value): Interpreted boolean condition.
  • if_true (Value): Interpreted callable returning a zero-arg function to be executed when condition is true.
  • if_false (Value): Interpreted callable returning a zero-arg function to be executed when condition is false.

Returns:

  • result (Value): An interpreted callable that, when run by the compiler, executes the selected branch.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
@legato.compile(backend=get_backend)
def fn():
    device = legato.get_context().get_device()
    with device.get_all_cores():
        condition = True
        x = 0
        x = x - 1

        def on_true():
            return x + 1

        def on_false():
            return x + 2

        legato.static.if_else(condition, on_true, on_false)
        x = -100
        condition = False
        legato.static.if_else(condition, on_true, on_false)