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
fix(inline): correct recursive inline miscompile (fib returned 38)
The depth-1 recursive inline pass produced a module that
miscompiled at the LLVM tier: `main()` returned `Int(38)` instead
of `Int(102334155)`. Two coupled bugs.
First, `substitute_operands` only rewrote operand HirIds. For the
standard inliner (caller != callee) this was invisible — the
callee's HirIds are disjoint from the caller's, so cloned
instruction results landed on unique ids either way. For the
recursive inliner the snapshot IS a clone of the live function,
so cloned instructions carried the live function's HirIds as
results, producing duplicate SSA definitions. The LLVM backend's
value_map keyed on HirId then overwrote the live definition with
the cloned one. Fix: substitute results too. No behaviour change
for the standard inliner — the result is the same fresh id that
was already pre-registered in `caller.values`.
Second, when the original call block was split twice (once per
inline, reverse-inst-idx order so earlier indices stay valid),
the post-block of the LATER inline ended up holding instructions
that the EARLIER inline's cloned blocks consumed — but the
EARLIER inline's blocks were inserted first into the function's
`IndexMap`. The LLVM backend processes blocks in IndexMap order
and silently falls back to `const_zero` for any HirValue it
hasn't yet seen, so the cloned `Lt(n - 2, 2)` cond folded to
`Lt(0, 2) = true`, the body collapsed to `return n - 2`, and
`fib(40) = 40 - 2 = 38`. Fix: a CFG-DFS reorder of `func.blocks`
at the end of the pass, so every block follows its predecessors
in iteration order. SSA dominance then guarantees value_map is
populated before any use.
Bench impact unchanged from the original landing — fib(40) ~344
ms exec, mandelbrot ~409 ms, all kernels return the right values.
248 compiler tests pass.
0 commit comments