Part V — The Modern Experience Layer · Chapter 34 · Lesson 1 of 2
Tracing, unrolling and strength reduction
Chapter 32 covered AOT bytecode v2 compilation. v2.2.0 adds the dynamic layer: the JIT watches execution, finds hot traces, and compiles them harder.
The instruction catalog
import analysis
let info = analysis.InstructionInfo # per-op latency & throughput metadata
InstructionInfo is a catalog of every bytecode op with latency and throughput estimates — the table the optimizer consults before rewriting anything. Reading it tells you which operations are expensive before you profile: divisions and loads from memory cost more than arithmetic on registers, which is why strength reduction targets exactly those.
Optimizing instruction sequences
let optimized = analysis.optimize_instructions(program, {
"constant_fold": true,
"dead_code": true,
"inline_caching": true,
"loop_invariant_hoist": true,
"strength_reduction": true
})
Five passes, each independently switchable and each semantics-preserving:
- constant_fold — evaluate compile-time-known expressions once instead of per iteration.
- dead_code_elimination — remove instructions whose results are never observed. Sound because the bytecode is static-verified before this runs.
- inline_caching — cache the resolved target of repeated dynamic dispatches. Polymorphic call sites (method dispatch, function values) collapse into a predictable comparison + direct call; the slow path stays, just rarely taken.
- loop_invariant_hoist — lift unchanging computation out of loop bodies. Legal only because the VM's effect analysis tracks reads and writes precisely.
- strength_reduction — replace expensive ops with cheaper equivalents: multiplies by powers of two become shifts; repeated additions become multiplies; divides reciprocal-multiply when the divisor is provably nonzero.
Recording traces
let trace = analysis.record_trace(fn(x) {
return 3 * x + 1
}, [1, 2, 3, 1000])
record_trace executes the function over representative inputs and records the executed instruction path as a JITTrace — a linear sequence annotated with the concrete types actually observed. Traces, not whole functions, are the compilation unit: the hot path pays for compilation, the cold path does not. Branchy functions produce multiple traces; the runtime picks the hottest.
Native stub emission
let native = analysis.emit_native(stub)
emit_native turns a trace stub into native code bytes. You will rarely call this directly in application code — the runtime does — but it is public and inspectable, because a JIT you cannot introspect is a debugging dead end. Disassemble the emitted stub when a hot loop underperforms; the mismatch between expectation and bytes is usually visible.
Registration
from pealang.jit_compiler import build_jit_namespace
ns = build_jit_namespace()
build_jit_namespace() exposes the JIT API as a namespace dict; in practice the analysis namespace registers these functions directly, and that is the path you should use — analysis.optimize_instructions, analysis.record_trace, analysis.emit_native. Direct imports from pealang.jit_compiler are for the runtime team and for conformance testing.
How to think about the JIT
- It is a scalpel, not a sledgehammer. Hot loops over numeric kernels: large wins. I/O-bound glue: flat numbers. Profile before you reach for it.
- Deoptimization is a feature. If assumptions baked into a trace stop holding, execution falls back to the interpreter — correctness is never negotiable, ever, under any flag.
- Traces age. Behavior shifts invalidate traces; the runtime re-records. Do not bake assumptions about global mutable state into hot functions.
- Warmup matters. Benchmarks without warmup measure trace recording, not steady state.
Guided practice
- Run
benchmark_vm(Chapter 32) on a recursive list sum with and without JIT passes; record both numbers. - Use
record_traceon your hottest function; count the dispatches that inline caching collapsed. - Verify traced and untraced executions agree on 1,000 varied inputs — this is what the differential suite does continuously for the whole language.