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
intobject 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
xis a dynamic tile, it will be represented as act.Tileobject. One can then usex.shapeto obtain the tile shape as a tuple of integers;x + 1will return a new proxy object that represents the symbolic expressionx + 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
Nis an integer constant andx,yare dynamic tiles, then one can writeres = ct.static_eval(x + 1 if N % 2 == 0 else y - 1)to select eitherx + 1ory - 1at compile tile, depending on the parity of the integer constantN.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.