Skip to main content

Crate bhc_tensor_ir

Crate bhc_tensor_ir 

Source
Expand description

§BHC Tensor IR

This crate defines the Tensor Intermediate Representation for the Basel Haskell Compiler. Tensor IR is specifically designed for numeric optimization and provides the foundation for guaranteed fusion and vectorization.

§Overview

Tensor IR is the key to BHC’s numeric performance. It captures:

  • Shape and stride information: For layout-aware optimization
  • Element types (dtypes): For unboxed numeric computation
  • Operation structure: For fusion analysis
  • Aliasing information: For safe in-place updates

§H26-SPEC Section 7 Compliance

Per the H26 specification, every tensor operation in Tensor IR must track:

PropertyDescription
dtypeElement type (Float32, Float64, Int32, etc.)
shapeDimension sizes
stridesByte strides per dimension
layoutMemory layout (contiguous, strided, tiled)
aliasAliasing/ownership information

§IR Pipeline Position

Source Code
    |
    v
[Parse/AST]
    |
    v
[HIR]
    |
    v
[Core IR]   <- General purpose optimizations
    |
    | (Numeric Profile only)
    v
[Tensor IR] <- This crate: shape-aware, fusion-ready
    |
    v
[Loop IR]   <- Explicit iteration

§Guaranteed Fusion Patterns

Per H26-SPEC Section 8, these patterns MUST fuse:

  1. map f (map g x) -> single traversal
  2. zipWith f (map g a) (map h b) -> single traversal
  3. sum (map f x) -> single traversal
  4. foldl' op z (map f x) -> single traversal

§Main Types

§See Also

  • bhc-core: Core IR that lowers to Tensor IR
  • bhc-loop-ir: Loop IR for explicit iteration
  • H26-SPEC Section 7: Tensor Model
  • H26-SPEC Section 8: Fusion Laws

Modules§

fusion
Tensor fusion pass.
lower
Core IR to Tensor IR lowering pass.

Structs§

AllocInfo
Allocation information for a kernel.
Axis
An axis specification.
BufferId
A unique identifier for buffers (memory allocations).
ConvSpec
Convolution specification.
FoldFn
A fold function.
FusionInfo
Fusion information for debugging and reporting.
Kernel
A fused computation kernel.
KernelId
A unique identifier for kernels.
LoopInfo
Information about a single loop.
LoopNest
A simple loop nest representation.
MapFn
A map function (element-wise transformation).
Permutation
A permutation for transpose.
Shape
Tensor shape (list of dimensions).
SliceRange
A range within a slice.
SliceSpec
A slice specification.
Strides
Memory strides for each dimension.
TensorId
A unique identifier for tensor operations.
TensorMeta
Tensor metadata per H26-SPEC Section 7.3.
TensorRef
A reference to a tensor value.
TileInfo
Tiling information for cache-friendly layouts.
ZipFn
A zip function (combining two elements).

Enums§

AllocRegion
Memory regions for allocation.
BinaryOp
Binary operations.
ConstantOp
A constant tensor operation.
DType
Tensor element types (data types).
DeviceTarget
Target device for GPU memory allocation.
Dim
A dimension size (may be static or dynamic).
FusionBlockReason
Why fusion was blocked.
FusionDecision
A fusion decision made by the compiler.
KernelBody
The body of a kernel.
Layout
Memory layout of a tensor.
MaterializeReason
Why a tensor was materialized.
ReduceOp
Reduction operations.
ScalarValue
A scalar value.
TensorIrError
Errors in Tensor IR operations.
TensorOp
Tensor operations in the IR.
UnaryOp
Unary operations.