Module Tolk_uop.Const
Typed compile-time constants.
A constant pairs a scalar payload with its Dtype.Val.t. Direct integer and floating-point constructors validate that the dtype is scalar (vector width 1) and matches the payload kind. Dtype-directed construction can keep vector dtypes for scalar-broadcast constants. Integer payloads are always stored as int64 regardless of the integer width; floating-point payloads are stored as native float and are only truncated to dtype's precision on emission, not on construction.
Constants participate in pattern matching and constant folding through view. The Invalid payload is the absorbing element of ALU folding and represents masked-out lanes (see invalid).
Types
type view = | Bool of bool(*Boolean payload.
*)| Int of int64(*Integer payload. Both signed and unsigned dtypes are stored in a single
*)int64slot; unsigned values are held as their raw 64-bit bit pattern (e.g.uint64max isInt64.minus_one).| Float of float(*Floating-point payload in native double precision.
*)| Invalid(*Sentinel for masked-out or undefined values. Absorbing under ALU folding: any ALU op with an
*)Invalidoperand folds toInvalid.
Read-only constant payload. Obtain via view.
Accessors
val dtype : t -> Dtype.Val.tdtype c is the dtype of c. It may be a vector dtype for scalar-broadcast or invalid vector constants.
Constructors
val bool : bool -> tbool b is the boolean constant b with dtype Dtype.Val.bool.
val int : Dtype.Val.t -> int -> tint dtype n is int64 dtype (Int64.of_int n). See int64.
Raises Invalid_argument if dtype is not a scalar integer dtype.
val int64 : Dtype.Val.t -> int64 -> tint64 dtype n is the integer constant n tagged with dtype. The value is stored verbatim; no range-checking or truncation is performed against dtype's width.
Raises Invalid_argument if dtype is not a scalar integer dtype (as per Dtype.Val.is_int, which accepts Dtype.Val.weakint and all signed and unsigned integer types).
val float : Dtype.Val.t -> float -> tfloat dtype x is the floating-point constant x tagged with dtype. The value is stored verbatim; narrowing to dtype's precision is deferred to emission via Dtype.truncate_float.
Raises Invalid_argument if dtype is not a scalar floating-point dtype.
val invalid : ?dtype:Dtype.Val.t -> unit -> tinvalid ?dtype () is the Invalid sentinel. dtype defaults to Dtype.Val.weakint. The dtype is unchecked: any scalar or vector dtype is accepted so that Invalid can propagate through typed IR positions.
val of_scalar : Dtype.Val.t -> Dtype.storage_scalar -> tof_scalar dtype x coerces storage scalar x according to dtype, like tinygrad's DType.const: floating-point dtypes produce canonicalized Float payloads, bool dtypes produce Bool, and all other dtypes produce integer payloads. Vector dtypes keep their vector dtype while carrying the scalar payload, matching tinygrad scalar broadcasts. Float-to-integer conversion follows Int64.of_float.
val of_view : Dtype.Val.t -> view -> tPredicates and comparisons
equal a b is true iff a and b carry the same dtype and the same payload. Floating-point payloads are compared by their int64 bit pattern, so NaN equals NaN and -0.0 differs from 0.0.
compare a b is a total order, keyed first by Dtype.Val.compare then by payload. Float payloads are ordered by their int64 bit pattern (same caveat as equal); Bool < Int < Float < Invalid within a given dtype.
Formatting
val to_string : t -> stringto_string c is a compact value:dtype representation using Dtype.Val.to_string for the tag (e.g. "42:i32", "3.14:f32", "true:bool", "Invalid:weakint").
Dtype-aware helpers
All helpers take a scalar dtype and raise Invalid_argument via the underlying constructor if dtype is not scalar.
val zero : Dtype.Val.t -> tzero dtype is the additive identity: 0.0 for floats, false for Dtype.Val.bool, 0 for integers.
val one : Dtype.Val.t -> tone dtype is the multiplicative identity: 1.0 for floats, true for Dtype.Val.bool, 1 for integers.
val min_value : Dtype.Val.t -> tmin_value dtype is the smallest representable value of dtype: -infinity for floats, false for bools, Dtype.min for integers. It is the identity element of a max-reduction over dtype.
val max_value : Dtype.Val.t -> tmax_value dtype is the largest representable value of dtype: infinity for floats, true for bools, Dtype.max for integers. It is the identity element of a min-reduction over dtype.