tilus.Script.range

Contents

tilus.Script.range

static Script.range(start, end=None, step=None, /, *, unroll)[source]

Create an iterator used in a for loop.

This function creates an iterator that can be used in a for loop. It is similar to the built-in range function, but provides additional control like unrolling the loop.

Parameters:
  • start (Expr | int) – The starting value of the iterator.

  • end (Expr | int, optional) – The end value of the iterator. If not provided, it is assumed to be 0 and start is used as the end value.

  • step (Expr | int, optional) – The step value of the iterator. If not provided, it defaults to 1.

  • unroll (Literal["all"] | int, optional) – The unrolling factor for the loop. If set to “all”, the loop will be fully unrolled. If set to an integer, the loop will be unrolled by that factor. If not provided, no unrolling hint will be applied.

Returns:

ret – The iterator that can be used in a for loop. It yields Var objects representing the loop indices.

Return type:

Iterable[Var]

Examples

We can use this function to create a for loop iterator, similar to the built-in range function:

# the following two loops are equivalent
for i in range(10):
    ...
for i in self.range(10):
    ...

# we can also specify the start, end, and step values
for i in range(1, 10, 2):
    ...
for i in self.range(1, 10, 2):
    ...

# we can also specify the unrolling factor
# unroll the loop completely
for i in self.range(1, 10, 2, unroll="all"):
    ...

# or unroll the loop by a factor of 4
for i in self.range(1, 10, 2, unroll=4):
    ...