Skip to content

[EVM] Support spills in recursive functions - #150

Merged
PavelKopyl merged 4 commits into
mainfrom
kpv-spills-in-recursive-functions
Oct 28, 2026
Merged

PavelKopyl merged 4 commits into
mainfrom
kpv-spills-in-recursive-functions

Conversation

@PavelKopyl

@PavelKopyl PavelKopyl commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

@PavelKopyl
PavelKopyl marked this pull request as draft September 19, 2026 09:38
@PavelKopyl
PavelKopyl force-pushed the kpv-spills-in-recursive-functions branch from 9c59e77 to 68a5871 Compare September 19, 2026 09:39
@PavelKopyl
PavelKopyl requested a lite review from Copilot September 19, 2026 11:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

There are confirmed correctness/maintainability issues in the solver’s entry-stack modeling and error reporting that can mis-model callee-saved spill slots and emit misleading fatal errors for non-recursive functions.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 2 Low severity

Open (3)
What changed in this PR

Adds support for register spills in recursive EVM functions by introducing callee-save handling for spill slots (so fixed spill addresses can be safely reused across activations), plus call-graph SCC tagging to distinguish re-entrant vs non-re-entrant calls, and a comprehensive suite of MIR tests covering the new behaviors.

Changes:

  • Mark recursive functions with a per-recursive-SCC id (evm-recursive-scc) and use it to detect potentially re-entrant calls.
  • Introduce CalleeSavedSlot tracking in the stack model and emit prologue/epilogue save/restore code for spills that cross re-entrant calls.
  • Add multiple MIR tests validating callee-save vs call-free spill behavior, return-path handling, and SCC-related re-entrancy distinctions.
File Description
llvm/​lib/​Target/​EVM/​EVMMarkRecursiveFunctions.cpp Adds evm-recursive-scc attribute assignment per recursive SCC.
llvm/​lib/​Target/​EVM/​EVMStackModel.h Introduces SK_CalleeSaved and CalleeSavedSlot plumbing + APIs for callee-save spill reasoning.
llvm/​lib/​Target/​EVM/​EVMStackModel.cpp Implements re-entrant call detection, callee-save spill creation, and return-stack integration.
llvm/​lib/​Target/​EVM/​EVMStackSolver.h Updates solver documentation around recursive spill strategy.
llvm/​lib/​Target/​EVM/​EVMStackSolver.cpp Enables spilling in recursive functions (after forced compression) and biases toward call-free spill candidates; models callee-saved slots at entry.
llvm/​lib/​Target/​EVM/​EVMStackifyCodeEmitter.h Adds emitter APIs for callee-save spill/load/restore and entry-block spill handling.
llvm/​lib/​Target/​EVM/​EVMStackifyCodeEmitter.cpp Implements callee-save prologue/restore emission and integrates it into stackification.
llvm/​test/​CodeGen/​EVM/​evm-recursive.ll Updates checks to validate evm-recursive-scc emission.
llvm/​test/​CodeGen/​EVM/​bps-recursive-func-spills.mir Adjusts regression test to expect spills (without callee-save) for recursive unreachable/no-RET case.
llvm/​test/​CodeGen/​EVM/​recursive-spills-args.mir New test for fused callee-save+spill of recursive function arguments.
llvm/​test/​CodeGen/​EVM/​recursive-spills-call-boundary.mir New test distinguishing callee-saved vs non-callee-saved spill slots around the recursive call.
llvm/​test/​CodeGen/​EVM/​recursive-spills-call-free.mir New test ensuring no callee-save code when all spilled live ranges are call-free.
llvm/​test/​CodeGen/​EVM/​recursive-spills-callee-save.mir New stress test ensuring multiple callee-saved spill slots are saved/restored correctly.
llvm/​test/​CodeGen/​EVM/​recursive-spills-ret.mir New test covering spilled register used by RET without requiring callee-save.
llvm/​test/​CodeGen/​EVM/​recursive-spills-scc-mutual.mir New test for mutual recursion SCC requiring callee-save across re-entrant call.
llvm/​test/​CodeGen/​EVM/​recursive-spills-scc-unrelated.mir New test ensuring calls into a different SCC don’t force callee-save.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread llvm/lib/Target/EVM/EVMStackSolver.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackSolver.cpp Resolved
Comment thread llvm/test/CodeGen/EVM/evm-recursive.ll Resolved
@PavelKopyl
PavelKopyl force-pushed the kpv-spills-in-recursive-functions branch 5 times, most recently from 1277832 to 28fbaad Compare September 21, 2026 21:25
@PavelKopyl
PavelKopyl marked this pull request as ready for review September 22, 2026 08:36
@PavelKopyl
PavelKopyl force-pushed the kpv-spills-in-recursive-functions branch from e6a2037 to c5beb79 Compare October 2, 2026 22:50

@vladimirradosavljevic vladimirradosavljevic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments, still need to do full review. Thanks for fixing this!

Comment thread llvm/lib/Target/EVM/EVMStackModel.cpp Resolved

@vladimirradosavljevic vladimirradosavljevic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, thanks for adding this support, it looks awesome! I have left some nits to address, and I will approve it after that.

Comment thread llvm/lib/Target/EVM/EVMStackModel.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackModel.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackModel.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackSolver.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackSolver.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackModel.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackModel.h Resolved
@PavelKopyl
PavelKopyl force-pushed the kpv-spills-in-recursive-functions branch from c5beb79 to 86a7566 Compare October 16, 2026 21:05

@vladimirradosavljevic vladimirradosavljevic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better now, thanks! I have some nits/questions to address, and it seems that there is an issue with the implementation, as some unit tests are failing.

Comment thread llvm/lib/Target/EVM/EVMStackSolver.cpp Resolved
Comment thread llvm/lib/Target/EVM/EVMStackModel.cpp Resolved
@PavelKopyl
PavelKopyl force-pushed the kpv-spills-in-recursive-functions branch 4 times, most recently from b46589f to 389c0dc Compare October 21, 2026 14:06

@vladimirradosavljevic vladimirradosavljevic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, amazing work!

…drops it

The spill stores of an instruction's definitions walk down from the
stack top. A spilled definition that the next layout does not hold is
stored without a copy and leaves the stack, which exposes the definition
below. The walk stops at the first definition that stays, and every
spilled definition below it is copied before its store, as before.
@PavelKopyl
PavelKopyl force-pushed the kpv-spills-in-recursive-functions branch from f8b6e39 to 48f7b89 Compare October 28, 2026 18:57
@PavelKopyl
PavelKopyl merged commit cdbe440 into main Oct 28, 2026
9 of 10 checks passed
@PavelKopyl
PavelKopyl deleted the kpv-spills-in-recursive-functions branch October 28, 2026 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants