Tensor and Mask Types
Tensor and Mask are the types that connect user-facing graph code to the SIR and SIMD backends.
The important distinction is this:
- small static tensors line up closely with array and vector lowering
- dynamic tensors are owning fat-pointer values with their own ABI and drop behavior
- masks are the boolean side of that model and have both static and dynamic forms
Layout overview
Current compiler and SIR shape contracts are centered around these mappings:
Tensor<f32, 4> -> Ty::Array(f32, 4) -> <4 x float>
Tensor<f32, 32> -> Ty::Array(f32, 32) -> [32 x float]
Tensor<f32> -> Ty::DynTensor(f32) -> internal pointer/length aggregate
Mask<4> -> Ty::Mask(4) -> <4 x i1>
Mask -> Ty::DynMask -> internal packed-mask aggregateThat split matters because static and dynamic tensors do not behave like a single interchangeable runtime object. See the dynamic mask representation table for the current host and GPU boundaries; it is not a stable public ABI.
Static Tensor<T, N>
Static tensors are compile-time sized.
In practice, the compiler lowers them through array machinery first:
- The inline SIMD eligibility rule requires a nonzero power-of-two lane count, at most 64 lanes and at most 64 bytes. Eligible operations can use LLVM vector values.
- Larger or ineligible values use other lowering paths, including memory arrays and loops. An LLVM vector can require several machine instructions; the threshold does not promise a single hardware register or instruction.
So Tensor<f32, 4> and Tensor<f32, 8> are good mental models for inline SIMD, while larger static tensors should be thought of as shaped arrays.
Example:
let a: Tensor<f32, 4> = [1.0, 2.0, 3.0, 4.0]
let b: Tensor<f32, 4> = [4.0, 3.0, 2.0, 1.0]
let c = a + bDynamic Tensor<T>
Dynamic tensors lower to an owning %VexDynTensor { ptr, i64 } layout.
The data buffer is heap-managed via the VUMI/VUMM allocator and participates in drop logic. Conversions into Tensor<T> from Span<T> involve an owned copy.
The reverse explicit cast has view semantics:
dynamicTensor as Span<T>is zero-copy and the borrow checker ties the span to the Tensor owner.staticTensor as Span<T>materializes scoped addressable storage for the SIMD value. LLVM can eliminate that storage when the view stays local, but the view cannot escape the frame.Span<T>.toTensor()andspan as Tensor<T>always produce independently owned dynamic storage; they never forge a Tensor owner over borrowed memory.
Example:
graph fn relu(x: Tensor<f32>): Tensor<f32> {
return (x > 0.0f32).select(x, 0.0f32)
}Tensor and Dynamic Tensor Operations
Start with the complete dynamic tensor examples. The following forms describe distinct operations; an operation may have a narrower dtype or backend domain than the tensor type itself.
| Form | Meaning and important constraint |
|---|---|
Tensor.fill<T>(count, value) | Allocate a dynamic tensor with count initialized elements. Count comes before value. |
Tensor.fill<T, N>(value) | Construct a fixed-size tensor; N is a compile-time dimension. |
tensor.len() | Logical element count; zero length does not permit any index. |
tensor[index] | Access an element within the logical extent. |
tensor.clone() | Independent owned dynamic storage. |
tensor as Span<T> | A borrowed view; keep the owner alive and obey its borrowing rules. |
tensor.gather(indices) | A result in the order specified by integer indices. |
tensor.scatter(indices, updates) | A new owned result; the source remains unchanged. |
scatter_add, scatter_mul, scatter_min, scatter_max | Combining updates with operation-specific conflict and numerical rules. |
The compiler also contains builder and transformation operations such as arange, linspace, sorting and prefix scans. Their argument and shape rules must be checked for the selected path; the existence of an internal SIR node is not a universal method signature. In particular, this guide does not teach fill(value, Vec<i32> shape) or Rust's *const () as Vex API syntax.
A raw address obtained through a supported pointer interface remains non-owning and carries no lifetime. Use the canonical Ptr<T> forms at a narrow boundary; never reinterpret a tensor aggregate as a foreign pointer or transfer its allocation to another allocator.
Mask<N> and Mask
Masks represent boolean results from comparisons and masking operations.
Mask<N> has a fixed logical width; its lowering and register use depend on the target. Dynamic Mask has runtime length and separate packed host storage; its GPU transport is described in the representation table.
Mask Operations
| Operation | Meaning |
|---|---|
select(whenTrue, whenFalse) | Lane-wise choice preserving the output extent; it does not suppress earlier evaluation of its arguments. |
any() / all() | Scalar tests for whether any/all logical lanes are true. |
countBits() | Number of true logical lanes. |
firstSet() | First true lane, with the empty-result convention of the selected mask API. |
toBitmask() / toBitmaskAt(offset) | Pack logical lanes into an integer window; a dynamic mask may exceed one word. |
Static and dynamic mask representation and integer-result widths need not be identical. Do not use a whole-mask packing operation as if it could retain an arbitrary number of lanes in a fixed-width integer. See the canonical mask contract before exchanging masks with native or GPU code.
Example:
let mask = data > 0.0
if mask.any() {
let first = mask.firstSet()
$println("First positive value index: {}", first)
}Hardware Lowering Summary
The compiler routes target workloads based on shapes and constraints:
- Eligible small static operations can use LLVM vectors; target legalization chooses registers and instructions.
- Dynamic operations can use loops, runtime support or compatible accelerator paths. Shape and capability checks determine the actual route.