Part V — The Modern Experience Layer · Chapter 34 · Lesson 1 of 2
Tracing, unrolling and strength reduction
In PeaLang 3.1.39, the JIT compiler in pealang/jit_compiler.py is a skeleton for experiments, not an automatic hot-trace compiler. Built-in namespaces such as analysis and interop are available directly; a PeaLang import takes a quoted path to another .pea file, so import analysis is not valid syntax.
The instruction catalog
let jit = interop.import_python("pealang.jit_compiler")
let catalog = jit.INSTRUCTION_CATALOG
print catalog["DIV"].latency
print catalog["ADD"].throughput
INSTRUCTION_CATALOG is a Python-level dict of 15 register-machine operations: LOAD, STORE, ADD, SUB, MUL, DIV, MOD, POW, CALL, RETURN, JUMP, JMPZ, DEF, GETATTR and SETATTR. Each InstructionInfo records name, latency, throughput, reads and writes. InstructionInfo is not registered in analysis, so the example reaches the host module through the built-in interop namespace. The numbers are estimates, not a runtime cost model wired into execution.
Optimizing instruction sequences
let instructions = [
{"op": "LOAD", "arg": 2},
{"op": "POW", "arg1": "x", "arg2": 2}
]
let hints = {
"constant_folding": true,
"dead_code_elimination": true,
"inline_caching": true,
"loop_invariants": true,
"strength_reduction": true
}
let optimized = analysis.OptimizeInstructions(instructions, hints)
print optimized
The registered name is analysis.OptimizeInstructions (capital O and I). It calls jit_compiler.optimize_instructions(instructions, hints=None). All five flags default to true when the hint dict is omitted, but the implementations are deliberately narrow:
- constant_folding — folds a narrow
ADD/SUB/MUL/DIVfollowed bySTOREpattern when prior integerLOADs can be resolved. It is not a general expression evaluator. - dead_code_elimination — uses a simple liveness heuristic over
reads/writeskeys; it is not a full effect analysis. - inline_caching — inserts an
IC_CHECKmarker beforeGETATTR. There is no runtime inline cache behind the marker, and no polymorphic dispatch collapse. - loop_invariants — the hint key is
loop_invariants, notloop_invariant_hoist. In 3.1.39_loop_invariant_hoist()is a placeholder that returns the instructions unchanged. - strength_reduction — only rewrites
POWwitharg1 == 2intoMUL x x, andDIVwitharg2 == 2intoMUL x 0.5. There are no shift rewrites, repeated-addition consolidation, or reciprocal-multiply proofs.
Recording traces
let jit = interop.import_python("pealang.jit_compiler")
let trace = jit.record_trace("let x = 1", 10)
print trace
record_trace(source, max_records=100) takes a PeaLang source string, not a callable plus inputs. In 3.1.39 it raises ImportError before execution because it imports parse from pealang.runtime, which exports parse_source/compile_source but not parse. If that import were fixed, recorded would still never be populated and the function would return None. The skeleton JITTrace type has pc, instructions, guard and exit_count fields; it describes a planned trace format, not an observed hot path. Neither record_trace nor JITTrace is registered in analysis.
Native stub emission
let jit = interop.import_python("pealang.jit_compiler")
let stub = {"op": "MUL", "arg1": "x", "arg2": "x"}
let blob = jit.emit_native(stub)
print blob
emit_native(stub) returns a byte marker b"JIT_STUB", a four-byte payload length, and the JSON representation of the stub. It does not emit x86-64, WebAssembly or any other machine code. It is a host-side inspection hook only; the analysis namespace does not expose it.
Registration
let jit = interop.import_python("pealang.jit_compiler")
let ns = jit.build_jit_namespace()
print keys(ns)
build_jit_namespace() returns a Python dict containing the JIT skeleton names. analysis registers only OptimizeInstructions from that module; it does not expose record_trace, emit_native, InstructionInfo or JITTrace. Do not write from pealang.jit_compiler import ... inside a .pea script — that is Python syntax. Use interop.import_python as shown when you intentionally need the host module.
How to think about the JIT
- It is a skeleton. No automatic hot-loop detection, trace selection, deoptimization or trace aging is implemented in 3.1.39.
- The stable optimizer is bytecode-level.
analysis.BytecodeOptimizerapplies conservative passes (constant_folding,dead_push_pop,unreachable_code); its module reportsOPTIMIZER_VERSION = "3.1.38"with the core frozen at3.1.37. Check rewrites withanalysis.VerifyOptimization. - The fast interpreter is separate. It pre-binds a small side-effect-free bytecode subset and falls back to the VM for calls, imports, exceptions, iterators and object access. Verify parity with
analysis.VerifyFastParitybefore relying on it. - Measure before choosing.
analysis.benchmark_vm(source, iterations=100)gives median and p95 timings for a source string. There is no built-in automatic JIT speedup to assume.
Guided practice
- Run
analysis.benchmark_vmon a small source string and record the median and p95, not the best run. - Call
analysis.OptimizeInstructionswith the five correct hint keys; confirm that theloop_invariantsflag leaves the list unchanged in this release. - From Python host tooling, compile a module and call
analysis.VerifyFastParity; only keep the fast interpreter ifokis true. - Inspect
catalog["DIV"].latencyandcatalog["ADD"].latencythroughinterop.import_python, then explain why these are estimates rather than measurements.