liveBook · Manning

Tl.constexpr

As explained in GPU Programming with Triton

tl.constexpr is a Triton annotation that marks a kernel parameter as a compile-time constant for specialization.

`tl.constexpr` marks a Triton kernel parameter as a compile-time constant. Triton interprets the annotation during compilation and specializes generated code for its supplied value. For `BLOCK_SIZE`, this lets the compiler lay out loads, address arithmetic, and stores for a fixed chunk size. By contrast, marking a changing vector length as `tl.constexpr` would create redundant compiled variants, so `n_elements` remains a runtime argument. section 2.2.2

def add_kernel(
    x_ptr, y_ptr, z_ptr,
    n_elements,
    BLOCK_SIZE: tl.constexpr,
):

Kernel signature showing `BLOCK_SIZE` annotated with `tl.constexpr` and `n_elements` left as a runtime parameter. section 2.2.2

How GPU Programming with Triton covers it

How deep it goes
Traces Python annotation handling through specialized loads, address arithmetic, and stores.
Examples
Contrasts fixed BLOCK_SIZE with varying n_elements in a vector-add kernel signature.
What you can do after
Apply it via Triton to specialize a block size while leaving changing vector lengths as runtime arguments.
How it connects
Connects Python type annotations, Triton kernels, GPU pointers, runtime arguments, and kernel specialization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of GPU Programming with Triton. The section links point to the parts of the book it draws on.