cuda.tile.static_eval#

cuda.tile.static_eval(expr, /)#

Evaluates the given Python expression at compile time.

The surrounded expression is evaluated using standard Python semantics, not Tile semantics. It can reference global variables and local variables from the surrounding tile function.

If a referenced variable is a compile-time constant value, it will be represented with a corresponding Python object of that value. For example, a constant integer 3 will be passed as a plain int object of value 3.

If a referenced variable has dynamic value, such as a tile or an array, it will be passed as a proxy object that allows querying compile-time attributes and building symbolic expressions from it. For example, if x is a dynamic tile, it will be represented as a ct.Tile object. One can then use x.shape to obtain the tile shape as a tuple of integers; x + 1 will return a new proxy object that represents the symbolic expression x + 1, etc.

The surrounded expression is allowed to return a proxy object for a dynamic value. For example, this can be used to select one of multiple dynamic expressions based on a compile-time condition: if N is an integer constant and x, y are dynamic tiles, then one can write res = ct.static_eval(x + 1 if N % 2 == 0 else y - 1) to select either x + 1 or y - 1 at compile tile, depending on the parity of the integer constant N.

The symbolic expression building feature is limited to basic arithmetic operations. For example, ct.static_eval(ct.gather(array, 0)) will raise an error.

The surrounded expression must not assign to local variables (e.g., via the walrus operator :=).

Despite being declared as a function, static_eval() is treated like a keyword: it skips the translation of the surrounded expression according to the Tile semantics. Moreover, the expression is allowed to use the full Python syntax, unlike the rest of the Tile code, which is limited to a stricter subset of the language.