PeaBox Lab › PeaLang › Learn › Chapter 34 › Lesson 1

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:

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

Guided practice

← Previous Next →