High-Level IR (HIR)
Package: pkg/ir/hir (Phase 92)
The High-Level IR is a typed, desugared representation of Karkain programs. It sits between the parser AST and the SSA IR, and serves the backend codegen. HIR resolves types, carries generics, and separates host vs. device code.
Purpose
The HIR exists to bridge the parser AST and the SSA optimizer:
The AST is production-ready but verbose and untyped.
The SSA IR is low-level and register-based.
The HIR provides a typed, high-level statement/expression tree that is cheap to build from the AST and cheap to lower into SSA.
Expressions
The Expr structure carries 55 expression kinds (ExprKind). The
original Phase 92 design documented 20 core kinds; the enum has since
grown along with the language surface:
Kind |
Notes |
|---|---|
|
Literals ( |
|
Identifier reference ( |
|
Binary operation ( |
|
Unary operation ( |
|
Function call ( |
|
|
|
|
|
|
|
|
|
|
|
Member access ( |
|
|
|
|
|
Memory operations |
|
Option/Result handling |
|
Match expression |
|
|
|
|
|
|
|
Quantum operations |
|
Concurrency / green threading |
|
GPU global id query |
|
Tensor operations |
|
Reflection, derive macros, attributes, compile-time evaluation |
Statements
16 statement kinds (StmtKind):
Kind |
Notes |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Deferred execution |
|
|
|
|
Types
19 type kinds (TypeKind):
Kind |
Notes |
|---|---|
|
Optional |
|
Optional |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Placeholder until type resolution |
Each Type carries Kind, optional Elem/KeyType/
ValType, Fields (structs), Variants (enums), Name,
Bits, Shape, and Qubits.
Module structure
BuildHIR() (pkg/ir/hir/build.go) lowers a parser Program
into a Module:
type Module struct {
Functions []*Function
Structs []*StructDecl
Enums []*EnumDecl
Traits []*TraitDecl
Impls []*ImplDecl
Globals []*GlobalVar
Imports []string
}
A Function carries params, return type, generics, captures, body
statements, and the source line for diagnostics. Structure and enum
declarations preserve public visibility and generic parameters.
Debug output
HIR nodes expose Type.String() and the module supports
Format()-style output for debugging. Type rendering matches the
spec language: i64/f64 for primitive ints and floats, tensor
<T, shape> for tensors, circuit<N>/qubit<N> for quantum
types, and Option<T>/Result<T> for option and result types.