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

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:

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

Guided practice

← Previous Next →