Master the fundamental concepts of jit compilation through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksHotSpot runs interpreter, then C1 (fast JIT), then C2 (optimizing JIT). V8 uses Ignition plus TurboFan. Warmup compiles cheap code first; hot code recompiles with heavier opts. Tiered strategy balances startup and peak speed.
Counter on backward branch increments. Threshold triggers next tier. OSR enters optimized loop mid-execution. Higher tiers assume more, guard less, inline more.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will implement tiered JIT compilation with multiple optimization levels. This exercise requires profiling counters, triggering compilation at thresholds, and replacing lower-tier code with higher-tier versions.
Simulate a tiered JIT like V8 or HotSpot: every function starts in the interpreter, gets promoted to a quick baseline compiler once it's warm and to an optimising compiler once it's hot, can jump into optimised code in the middle of a long loop (on-stack replacement), and falls back when optimised code deoptimises.
cLoading…
Each function has a tier (interp → baseline → optimized) and a hotness counter, both starting at interp / 0.
optimized and invocation back-edges becomes exactly S, OSR: the tier becomes optimized immediately and the remaining back-edges run optimised. After the loop, tier-up check.interp with counter ≥ B → baseline; baseline with counter ≥ O → optimized (at most one step per check).optimized function: tier becomes baseline and the counter resets to 0.Cost (in work units) of running code in each tier:
| interp | baseline | optimized | |
|---|---|---|---|
| per call | 10 | 3 | 1 |
| per back-edge | 5 | 2 | 1 |
One line per tier change, in order:
cLoading…
C counts calls of that function from 1. Then one summary line per function, in order of first appearance:
cLoading…
Input:
cLoading…
Output:
cLoading…
(After the loop the counter is 9, one short of 10, so the promotion happens at call 5. Calls 1-2 cost 10 each, 3-4 cost 3 each, the 5 back-edges run in baseline at 2 each, and call 5 already runs optimized at 1.)
Hidden tests cover OSR from the baseline tier, a deopt followed by re-optimisation (and a deopt of code that is no longer optimized, which does nothing), and several functions interleaved.