Skip to main content

Mojo function

global_constant

global_constant[T: AnyType, //, value: T]() -> ref [StaticConstantOrigin] T

Creates a reference to a compile-time constant value.

This function uses the MLIR pop.global_constant operation to create a reference to a compile-time value without materializing the entire value at runtime. This is particularly useful for large lookup tables where you want to avoid materializing the entire table when accessing individual elements.

Examples:

# Create a reference to a constant array and access elements
alias lookup_table = InlineArray[Int, 4](1, 2, 3, 4)
var element = global_constant[lookup_table]()[2]  # Access without materializing entire array
print(element)  # Prints: 3
# Use with more complex compile-time values
fn compute(x: Int) -> Int:
    return x * 2 + 1

alias data = InlineArray[Int, 3](1, compute(5), 100)
ref data_ref = global_constant[data]()
print(data_ref[0], data_ref[1], data_ref[2])  # Prints: 1 11 100

Parameters:

  • T (AnyType): The type of the constant value.
  • value (T): The compile-time constant value.

Returns:

ref: A reference to the global constant.

Was this page helpful?