You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
engine: skip the untaken branch of IF THEN ELSE in the VM #711
The VM evaluates both branches of IF THEN ELSE every step and then picks one.
Skipping the untaken branch would save work, but it needs forward-jump opcodes, which touch code generation, stack-depth validation, the opcode-fusion passes, the cached symbolic bytecode, and the wasm backend.
Status
Measured and judged not worth it for now.
On C-LEARN, skipping untaken branches would remove 15.9% of executed dispatches but only about 1.5% of retired instructions, because the skipped opcodes are mostly cheap loads and arithmetic.
Most of the dispatch saving has since been taken another way: the condition test and select are fused into single opcodes (SelectIf, SelectIfAssignCurr), removing 12% of executed dispatches with no control-flow changes.
One finding worth keeping: about 70% of the skippable work sits behind conditions that are constant for the whole run (policy switches).
Those could be resolved at compile time, which is a different and simpler change than runtime jumps.
Fix
Do not build forward jumps on their own.
Revisit if the VM moves to threaded dispatch (#601), where control flow is being restructured anyway.
Pointers
src/simlin-engine/src/compiler/codegen.rs: the Expr::If lowering
docs/design/engine-performance.md: the "Lazy If" item at the end of "Suggested ordering"
Measured NO-GO for now -- census data in docs/design/engine-performance.md (round 3, 2026-06-04), summarized:
A dispatch census (temporary eval_bytecode counters + a stack-effect branch-span reconstruction over the fused stream the VM actually executes) on the engine-vm-perf branch:
C-LEARN
WORLD3-03
executed dispatches/step (exact)
30,524
1,112
flow If sites / executed Ifs per step
1,679 / 1,873
24 / 24
dispatches lazy-IF would skip
4,859 (15.9%)
36 (3.25%)
instruction-share estimate of the skip
~1.5% (~35k of ~2.4M instr/step)
trivial
The 15.9% dispatch share collapses to ~1.5% instructions because 93% of the skippable opcodes are cheap scalar loads/binops (LoadVar 30%, BinStackVar 11%, LoadConstant 11%, ...; only 3.7% Lookup + 2.9% Apply), while per-step instruction count is dominated by per-element array/lookup/module work lazy-IF never touches. ~1.5% is below the ~4% build-layout measurement floor documented in the perf doc (negative result #3's methodology note), and this is the most cross-cutting change of the round-3 candidates (forward jumps touch the stack-depth validator, peephole/fusion jump maps, the symbolic layer, and wasmgen). The lowering also adds two jump dispatches per executed If back.
Two findings worth keeping:
69.8% of the skippable dispatches sit behind constant conditions (1,300 of 1,679 flow If sites take the same branch for the entire run -- policy switches). Those are compile-time / engine: compute run-invariant variables once per run, not every step #712-family targets (branch specialization), not runtime-jump targets.
Exact dispatch counts: C-LEARN executes ~30.5k dispatches/step, far below earlier loose estimates -- useful calibration for sizing any future dispatch-level work.
Revisit conditions: a mispredict-bound microarchitecture (the round-1 Ryzen profile, where the dispatch indirect branch dominates and the If/SetCond data-dependent selection has real misprediction cost), or as part of a threaded-dispatch rewrite (#601) where the control-flow structure changes anyway.
Correction to the measurement premise in this issue's NO-GO — the verdict is unchanged
The decision stands. Only its stated reason is wrong, and the wrong reason is the kind that shuts down further thought, so it is worth correcting where people will read it.
docs/design/engine-performance.md records the NO-GO as:
the instruction share is only ~1.5% (~35k of ~2.4M instr/step): below the ~4% layout-noise measurement floor
That applies a cycles/binary-layout noise floor to an instruction-count measurement. The two floors are three orders of magnitude apart.
Measured on the C-LEARN run across six independent build+run pairs of identical source (Ryzen 9950X):
channel
sd across builds
retired instructions
0.026%
branches
0.028%
cycles, quiet machine
1.65%
cycles, machine under load
9.9%–11%
A null control makes the same point directly — the identical binary run as both sides of an interleaved A/B, 5 rounds, medians:
instructions -0.003%
branches -0.004%
cycles -1.540% <- an apparent "win" from nothing
Retired instructions are a property of the program; cycles are a property of the machine executing it. So ~1.5% of instructions is roughly 58 sigma on that channel — comfortably measurable, not below any floor. The ~4% figure is real, but it bounds wall-clock/cycles claims from a single build pair, not instruction counts.
What the verdict actually rests on
Two reasons survive, and they are sufficient:
Small absolute win for the highest design cost of the three candidates. Lazy If needs forward-jump opcodes, which touch codegen, max_stack_depth join validation, the peephole/fusion jump maps, the symbolic layer, and wasmgen parity. ~1.5% of instructions does not pay for that.
The cheap part of the win is already taken. Fusing SetCond;If[;AssignCurr] into conditional-select opcodes removes 12.0% of executed dispatches against this issue's projected 15.9%, with none of that machinery — no jumps, no stack-depth join reasoning, no symbolic or wasmgen changes. It rests on the pair being adjacent by construction (compiler::codegen's Expr::If arm is the sole producer of both and emits them together; measured executed counts are exactly equal at 1,874,169 each on C-LEARN).
So the remaining upside here is the residual after that fusion, against the full forward-jump design cost.
Why this is worth a comment rather than a silent doc edit
A wrong measurement premise in a design doc does not get caught by review: reviewers check the reasoning against the stated premise, not the premise against the world. "Below the measurement floor" reads as a fact and ends the discussion. This one had already carried a verdict, which is the evidence it was doing damage rather than sitting inert.
Anyone picking this issue up would have read the invalid floor claim here first, so fixing only the doc would have preserved the trap in the place most likely to be read.
The general convention that would have prevented it: when a doc or issue records a measurement-derived verdict, state which channel the number came from. A bare "only ~1.5%" invites comparison against whatever floor the reader has in mind.
changed the title [-]engine: lazy IF evaluation — skip the untaken branch in the VM[/-][+]engine: skip the untaken branch of IF THEN ELSE in the VM[/+]on Oct 12, 2026
Measured at ~1.5% of retired instructions for the most invasive design, and most of the dispatch saving was already taken by SelectIf fusion; revisit only with #601.
Activity
bpowers commentedon Jun 4, 2026
Measured NO-GO for now -- census data in docs/design/engine-performance.md (round 3, 2026-06-04), summarized:
A dispatch census (temporary eval_bytecode counters + a stack-effect branch-span reconstruction over the fused stream the VM actually executes) on the engine-vm-perf branch:
The 15.9% dispatch share collapses to ~1.5% instructions because 93% of the skippable opcodes are cheap scalar loads/binops (LoadVar 30%, BinStackVar 11%, LoadConstant 11%, ...; only 3.7% Lookup + 2.9% Apply), while per-step instruction count is dominated by per-element array/lookup/module work lazy-IF never touches. ~1.5% is below the ~4% build-layout measurement floor documented in the perf doc (negative result #3's methodology note), and this is the most cross-cutting change of the round-3 candidates (forward jumps touch the stack-depth validator, peephole/fusion jump maps, the symbolic layer, and wasmgen). The lowering also adds two jump dispatches per executed If back.
Two findings worth keeping:
Revisit conditions: a mispredict-bound microarchitecture (the round-1 Ryzen profile, where the dispatch indirect branch dominates and the If/SetCond data-dependent selection has real misprediction cost), or as part of a threaded-dispatch rewrite (#601) where the control-flow structure changes anyway.
bpowers commentedon Aug 10, 2026
Correction to the measurement premise in this issue's NO-GO — the verdict is unchanged
The decision stands. Only its stated reason is wrong, and the wrong reason is the kind that shuts down further thought, so it is worth correcting where people will read it.
docs/design/engine-performance.mdrecords the NO-GO as:That applies a cycles/binary-layout noise floor to an instruction-count measurement. The two floors are three orders of magnitude apart.
Measured on the C-LEARN run across six independent build+run pairs of identical source (Ryzen 9950X):
A null control makes the same point directly — the identical binary run as both sides of an interleaved A/B, 5 rounds, medians:
Retired instructions are a property of the program; cycles are a property of the machine executing it. So ~1.5% of instructions is roughly 58 sigma on that channel — comfortably measurable, not below any floor. The ~4% figure is real, but it bounds wall-clock/cycles claims from a single build pair, not instruction counts.
What the verdict actually rests on
Two reasons survive, and they are sufficient:
Ifneeds forward-jump opcodes, which touch codegen,max_stack_depthjoin validation, the peephole/fusion jump maps, the symbolic layer, and wasmgen parity. ~1.5% of instructions does not pay for that.SetCond;If[;AssignCurr]into conditional-select opcodes removes 12.0% of executed dispatches against this issue's projected 15.9%, with none of that machinery — no jumps, no stack-depth join reasoning, no symbolic or wasmgen changes. It rests on the pair being adjacent by construction (compiler::codegen'sExpr::Ifarm is the sole producer of both and emits them together; measured executed counts are exactly equal at 1,874,169 each on C-LEARN).So the remaining upside here is the residual after that fusion, against the full forward-jump design cost.
Why this is worth a comment rather than a silent doc edit
A wrong measurement premise in a design doc does not get caught by review: reviewers check the reasoning against the stated premise, not the premise against the world. "Below the measurement floor" reads as a fact and ends the discussion. This one had already carried a verdict, which is the evidence it was doing damage rather than sitting inert.
Anyone picking this issue up would have read the invalid floor claim here first, so fixing only the doc would have preserved the trap in the place most likely to be read.
The general convention that would have prevented it: when a doc or issue records a measurement-derived verdict, state which channel the number came from. A bare "only ~1.5%" invites comparison against whatever floor the reader has in mind.
engine: performance pass on compile, simulation, and the LTM path (#1021
[-]engine: lazy IF evaluation — skip the untaken branch in the VM[/-][+]engine: skip the untaken branch of IF THEN ELSE in the VM[/+]bpowers commentedon Oct 12, 2026
Closing as won't fix.
Measured at ~1.5% of retired instructions for the most invasive design, and most of the dispatch saving was already taken by SelectIf fusion; revisit only with #601.