Typed SSA IR
Package: pkg/ir/ssa (Phases 93–94)
The SSA IR is a register-based, single-assignment intermediate representation used by the Go front end. It underpins the optimizer pipeline and is verified for well-formedness before code generation.
Core register types
The base SSA Type is a uint8 with seven values:
Type |
Meaning |
|---|---|
|
No value |
|
Integer (i64 semantics) |
|
Float (float64) |
|
Boolean |
|
String |
|
Dynamic boxed Karkain value |
|
Raw pointer ( |
Extended typed types
typed.go adds precise bit-width types on top of the legacy enum:
IType (8/16/32/64 bits), FType (16/32/64 bits), PtrType
(typed element), ArrayType (fixed length), TensorType (hardware
tensor for NPU/GPU), and FuncType (indirect calls / closures).
TypeRegistry
TypeRegistry maps between the legacy enum and the typed system,
resolving 14 primitive type names:
void→Voidi8/i16/i32/i64→If16/f32/f64→Fbool→Boolstr/string→Strptr→Ptreverything else →
Value
Type helpers
TypeBits(t)— bit width of a register type (I=64, F=64, Bool=1, Ptr=64, Str=0, compound=0).TypeWidth(t)—(bits + 7) / 8bytes.TypeAlign(t)— min(width, 8) bytes, default 1.TypeIsInteger/TypeIsFloat/TypeIsNumeric/TypeIsPointer— type predicates.TypeIsCompatible(a, b)— same type, integer↔integer, float↔float, or any interaction withValue(dynamic boxing).TypePromote(a, b)— numeric promotion following standard rules: same → same; eitherValue→Value; either float →F; both integer →I; otherwiseValue.
Instructions
An Instr has an opcode, optional destination register, result type,
operands, and (for branches) targets and block args:
OpConst— materialize a constant (i64, f64, bool, str).OpBinOp—%d = binop %a OP %bover the validated operator set (+ - * / % == != < > <= >= && ||).OpUnOp—%d = unop OP %a(-,!).OpCall/OpCallVoid— direct calls, typed results or void.OpBr— conditional branch with block arguments.OpJmp— unconditional jump with block arguments.OpRet— return (value or void).OpIndexGet/OpIndexSet— array indexing.OpPrint— I/O print.OpLoad/OpStore— variable cell read/write.OpRawC— escape hatch: opaque pre-rendered C expression or statement.
Blocks use named BlockParam entries (replacing phi nodes). A valid
block must end in exactly one terminator (br/jmp/ret).
Dominance tree
dom.go implements the iterative immediate-dominator algorithm of
Cooper, Harvey, and Kennedy (“A Simple, Fast Dominance Algorithm”).
BuildDominatorTree(fn) *DomTree—IDommaps each block to its immediate dominator; the entry block dominates itself.DomChildren— the inverse mapping for tree traversal.Dominates(a, b)/StrictlyDominates(a, b)— ancestry checks over the IDom chain.DominanceFrontier(fn, block)— blocks where the block’s dominance ends (used by the optimizer).DomTreeDepth(name)— tree depth of a block.
Liveness analysis
liveness.go computes per-block live in/out sets via backward
iterative dataflow.
LiveIn/LiveOut/LiveGen/LiveKillmaps per block.Ranges— per-registerLiveRange(def site, last use site, defining block).TotalRegs— distinct register count in the function.NumLiveAt(block, index)— register pressure at a program point.Interferences(reg)— registers whose live ranges overlap.IsDead(reg)— defined but never used.
SSA verifier
verify.go checks module well-formedness (originally Phase 53):
Every function’s entry block is first in
Blocks.Every block ends in exactly one terminator.
No instructions after the terminator.
Branch targets exist and receive the correct number of block args.
Register single-assignment — each
Destis written at most once per function (SSA discipline).BinOpoperators are known;rawchas a non-empty payload; calls have a target.
Optimization pipeline
pipeline.go chains passes in a fixed order:
mem2reg → fold-const → cse → dce → licm → sroa
The orchestrator iterates to a fixpoint with a maximum of 10
iterations; each pass reports whether it changed the function.
RunModule applies the pipeline to every function. RunWithStats
records per-pass PassStats.
Passes
Pass |
Details |
|---|---|
|
Promotes |
|
Compile-time evaluation of |
|
Block-local common subexpression elimination over pure ops (const, binop, unop); later uses rewritten to the earlier result. |
|
Dead code elimination. Unused-result instructions are removed; side-effecting ops (call, callvoid, index_set, print, rawc, terminators) are preserved. Iterates to fixpoint for dead-use chains. |
|
Loop-invariant code motion over natural loops found via back-edges (branch target dominates branch source). Pure, loop-invariant computations are hoisted into the loop header. |
|
Scalar replacement of aggregates (simplified). Recognizes
constant-index |
Phase history
Phase 93 — Typed SSA IR: TypeRegistry, TypeBits, TypePromote, TypeIsCompatible, dominance tree, liveness, SSA verifier (30 tests).
Phase 94 — Optimization pipeline: Mem2Reg, FoldConst, CSE, DCE, LICM, fixpoint orchestration, statistics (49 tests including benchmarks).