Repository navigation
feat(ir): rewriter operations for code motion, copying and inlining - #846
Closed
alexcgatea wants to merge 16 commits into
Closed
alexcgatea wants to merge 16 commits into
alexcgatea wants to merge 16 commits into
Conversation
Add replace_uses_if, replace_all_uses_except_stmt and replace_all_uses_except_stmts, which redirect only the use sites a predicate accepts. ReplacedUses is recorded only once the old value has no uses left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The event log now only grows while a Rewriter lives, so positions in events() stay valid. Tests track positions with an EventCursor helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moves record MovedStatement; walk_mut rejects moving the visited statement in link-following orders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ore/after Moves every non-terminator statement of a block next to an anchor and redirects its arguments; records SplicedBlock, which walk_mut checks. Drops the planned block move (step 3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moves every block of a CFG after an anchor block in another CFG; records SplicedCfg. walk_mut now records a VisitedPlace (block and CFG) before each visit to check splices against. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replaces the six insert_* methods with create(point, definition) and insert(point, stmt) for detached statements. UnGraph members are nodes or edge statements as their definition says. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clone_statement and clone_cfg make detached deep copies in two passes, mapping ids through an IRMap. create adopts the unowned bodies its definition names; detached CFGs never adopted are tombstoned on drop. Graph bodies are rejected until they can be cloned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A copied graph gets fresh ports, copied members in their roles and yields; its topology is built from the copied operands once their uses are recorded. Removes CopyOfGraphBody. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
inline_calls inlines one level of same-stage CallSpecialized calls in a CFG's blocks and returns how many. Single-block callees are copied in place; others are copied between the halves of a split block. Walking each block from last to first keeps a run linear. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
List splicing and cloning in the Rewriter module docs, count fifteen MutationEvent kinds, and remove the remaining-IR-operations plan now that it is done. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Renames check_point to check_insertion_pt, body_encloses_point to body_encloses_insertion_pt, and InsertionPoint parameters to insertion_pt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Placing a detached DiGraph member now adds edges to member slots that already read its results, and placing a detached UnGraph edge statement resyncs its wires. Documents that a detached statement never placed leaves its readers dangling. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mapping a block argument no longer drops it from the copy, which left incoming edges passing values to a block with fewer arguments. The liveness check skips values the copy defines, so a map can be reused after its earlier copy is gone. The inliner enters a multi-block copy with a branch passing the call's arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
alexcgatea
force-pushed
the
alex/remaining-ir-operations
branch
from
November 10, 2026 00:07
e0dd591 to
0962525
Compare
Collaborator
Author
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.
Summary
Adds the
Rewriteroperations needed for code motion and inlining — moving and splicing IR, deep copies, and statement creation at an insertion point — plus a one-level call inliner inkirin-functionthat runs in linear time.New operations
replace_uses_if,replace_all_uses_except_stmt(s)replace_all_usesrestricted to selected use sitesmove_before/move_aftersplice_block_before/splice_block_aftersplice_cfg_aftercreate(insertion_pt, definition)/insert(insertion_pt, stmt)InsertionPoint(Before,After,Terminator,DiGraph,UnGraph), or place a detached oneclone_statement,clone_cfg+IRMapIRMapentries, so a map can be reused, even after an earlier copy is gonekirin_function::inline_callsCallSpecializedcalls in a CFGAPI changes
insert_before/insert_after/insert_terminator/insert_node/insert_ungraph_node/insert_ungraph_edgeare replaced bycreate(InsertionPoint, definition). One method, with the insertion point as data. AnUnGraphmember is a node or an edge statement according to its definition, soUnexpectedEdgeStatement/ExpectedEdgeStatementare removed.insert(insertion_pt, stmt)only accepts a detached statement of thisRewriter, such as a clone (NotDetachedotherwise). Detached statements and CFGs that are never placed are tombstoned when theRewriterdrops; readers of their values are left with dangling operands, whichverify_derivedreports andrun_passquarantines.insertplaces it in a graph, it adds theDiGraphedges andUnGraphwire endpoints to consumers already there.createadopts the unowned bodies its definition names (e.g. a cloned CFG), with the same checks asreplace_statement.drain_eventsis replaced byinto_events(self), so the event log only grows while aRewriterlives, and positions in it stay valid.CFGInfo::parent()accessor.AnchorInsideMovedis renamedMovedIntoItself.Walk safety
Moving or splicing the statement being visited breaks pre-order, post-order and BFS walks, which find the next statement through the visited one.
walk_mutnow checks theRewriter's event log after each visit (MovedStatement,SplicedBlock,SplicedCfg, compared against aVisitedPlacerecorded before the visit) and reportsWalkError::VisitedMoved. Reverse BFS orders, which build their order up front, still allow it. A loop driving a cursor by hand recordsVisitedPlace::of(stage, stmt)and the event-log length before each visit, and callsWalkOrder::check_not_moved_sinceitself.Inlining
kirin-python's
Walk(Inline(...))scales as O(n²) (#554): each multi-block inline splits the call's block, and the split walks every statement after the call. In heavily inlined kernels most of the program ends up in one block, so every split costs close to the whole program. Calls are also visited first to last, so the same tail is moved again for every later call.inline_callsis built to stay linear:retbecomes a branch to the second half.inline_callsis public, so no single run can inline in a quadratic order.splice_cfg_afterrelinks them, with no list removal from the front.FunctionOps<T>(as_call/as_return) to tell the inliner which of its statements are calls and returns; a closure builds its branch op.Not included