bugc: add function inlining pass (inline transform, L2)#230
Merged
Conversation
Adds InliningStep (level 2, first — after L1 fold, before CSE/TCO/ JumpOpt) that replaces calls to eligible internal functions with a copy of the callee body spliced into the caller. Every inlined instruction is annotated transform:["inline"] via addTransform, and the body is bracketed by a virtual invoke (jump:true, identity, no target — the #213 optional-target signal) / virtual return, so a debugger can reconstruct a virtual activation. This lights up `inline` in the tracer, completing the transform set. v1 eligibility (correctness over coverage; follow-ups noted): - internal, non-recursive, single-return, LEAF callee; - applied at all call sites; callee deleted once fully inlined; - NOT inlined into self-recursive / TCO'd callers: inlining a helper into a tail-recursive call's arguments rewrites count(succ(n)) -> count(n+1), which the tail-call optimizer mishandles (pre-existing bug, tracked separately). Guarding this keeps the tailcall demos pristine. Return values use dest-substitution (not a continuation phi), which is robust to L3 block-merging; deep clones use structuredClone to preserve bigint const values. Updates optimizer-contexts tests: leaf helpers (add/dbl/addThree) now inline at L2+ (no real caller JUMP; inline activation instead), while recursive/multi-return functions (fact, isEven/isOdd, count) are untouched. Deferred to follow-ups: non-leaf/nested callees, multi-return, size-threshold (non-leaf) inlining.
Contributor
|
gnidan
added a commit
that referenced
this pull request
Jul 3, 2026
…hip) (#233) Amend the invoke spec's "Reconstructing activations" section to resolve an ambiguity #230's inline pass exposed: an inlined body's invoke/return markers must bracket the body (invoke on the first instruction, return on the last), never duplicate across interior instructions. Duplicated boundary markers push/pop spurious activations. - State the push/pop display semantics: invoke opens inclusive of its instruction, return closes after its instruction. - Require bracketed emission; permit the single-instruction body (entry==exit) to carry both invoke and return, processed push-then-pop. - Separate the two concerns explicitly: push/pop determines an activation's lifetime; membership determines which open activation an instruction belongs to. An activation stays open across non-member (interleaved caller) instructions. No schema change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the function inlining pass and lights up
inlinein the tracer — completing the transform set (fold/tailcall/coalesce/inline). Emits per #229's frozen contract via the sharedaddTransformhelper.What it does
InliningStep(level 2, first — after L1 fold, before CSE/TCO/JumpOpt) replaces calls to eligible internal functions with a copy of the callee body spliced into the caller (α-rename, param→arg substitution, return→continuation via dest-substitution, callee deleted once fully inlined). Emission:jump:true, identity, notarget— the format: make invoke.target optional for internal calls #213 optional-target signal) on the first inlined instruction;transform:["inline"]on every body instruction (per-instruction membership, reorder-robust);arguments/dataomitted in v1 (per contract).v1 scope (correctness over coverage — follow-ups noted)
Eligible callee = internal, non-recursive, single-return, leaf; applied at all call sites.
count(succ(n))→count(n+1), which the tail-call optimizer mishandles (a pre-existing bugc bug — reproduces with inlining disabled; tracked separately). This guard keeps the tailcall demos pristine.Correctness notes
structuredClone(preservesbigintconst values).Test changes
inlining.test.ts: leaf single-return + multiple-sites correct at all levels with inline markers; TCO-protection (succ not inlined into count; count stays correct + tailcall).optimizer-contexts.test.ts: leaf helpers (add/dbl/addThree) now inline at L2+ (no real caller JUMP → inline activation); recursive/multi-return functions (fact, isEven/isOdd, count/succ) untouched.Verification
yarn build+ full bugc suite green (420 passed, 22 pre-existing skips).functionCallAndReturn(add) → inlined + correct;mutualRecursion→ untouched;count(succ)→ not inlined, tailcall intact, correct.