[llvm] [TailCallElim] Do not mark a call tail when it is handed the frame (PR #218797)
Matt Turner via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 04:07:26 PDT 2026
https://github.com/mattst88 updated https://github.com/llvm/llvm-project/pull/218797
>From 7df2f1b65882c15061d3bbff5e1b6e750243244d Mon Sep 17 00:00:00 2001
From: Matt Turner <mattst88 at gmail.com>
Date: Wed, 5 Aug 2026 15:49:01 -0400
Subject: [PATCH] [TailCallElim] Do not mark a call tail when it is handed the
frame
markTails refuses to mark a call tail if it is passed an alloca or a byval
argument, but it missed the intrinsics that return an address in the current
frame, such as llvm.frameaddress(0), llvm.localaddress and llvm.stacksave. The
frame is torn down before a tail callee runs, so the callee received a dangling
pointer. gcc.c-torture/execute/frame-address.c aborts because of this.
llvm.stackrestore is no longer treated as an escape, since it does not capture
its argument. Otherwise calls after a VLA scope would lose their tail marking.
LangRef now states that a tail callee may not access the caller's stack frame.
Assisted-by: Claude Code
---
llvm/docs/LangRef.md | 19 ++-
.../Scalar/TailRecursionElimination.cpp | 57 +++++--
.../Transforms/TailCallElim/frame-address.ll | 154 ++++++++++++++++++
.../Transforms/TailCallElim/stackrestore.ll | 47 ++++++
4 files changed, 260 insertions(+), 17 deletions(-)
create mode 100644 llvm/test/Transforms/TailCallElim/frame-address.ll
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index ee1600b96f1dd..e1cc308809fa6 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -13730,9 +13730,13 @@ This instruction requires several arguments:
the return value of the callee is returned to the caller's caller, even
if a void return type is in use.
- Both markers imply that the callee does not access allocas, va_args, or
- byval arguments from the caller. As an exception to that, an alloca or byval
- argument may be passed to the callee as a byval argument, which can be
+ Both markers imply that the callee does not access any value derived from
+ the caller's stack frame, which is torn down before the callee runs. That
+ covers allocas, va_args, and byval arguments, and equally an address of the
+ frame itself, including pointers returned by intrinsics such as
+ `llvm.frameaddress` with a level of zero, `llvm.localaddress`, or
+ `llvm.stacksave` evaluated in the caller. As an exception, an alloca or
+ byval argument may be passed to the callee as a byval argument, which can be
dereferenced inside the callee. For example:
```llvm
@@ -13787,6 +13791,15 @@ This instruction requires several arguments:
tail call void @take_ptr(ptr %x)
ret void
}
+
+ ; Invalid (assuming @take_ptr dereferences the pointer), because the frame
+ ; @frameaddress names is torn down before @take_ptr runs.
+ define void @invalid_frameaddress() {
+ entry:
+ %fp = call ptr @llvm.frameaddress.p0(i32 0)
+ tail call void @take_ptr(ptr %fp)
+ ret void
+ }
```
Calls marked `musttail` must obey the following additional rules:
diff --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
index 880b0bda90d70..f1a79b1a200a0 100644
--- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -155,10 +155,10 @@ static bool canTRE(Function &F) {
}
namespace {
-struct AllocaDerivedValueTracker {
+struct LocalStackValueTracker {
// Start at a root value and walk its use-def chain to mark calls that use the
- // value or a derived value in AllocaUsers, and places where it may escape in
- // EscapePoints.
+ // value or a derived value in LocalStackUsers, and places where it may
+ // escape in EscapePoints.
void walk(Value *Root) {
SmallVector<Use *, 32> Worklist;
SmallPtrSet<Use *, 32> Visited;
@@ -181,6 +181,12 @@ struct AllocaDerivedValueTracker {
case Instruction::Call:
case Instruction::Invoke: {
auto &CB = cast<CallBase>(*I);
+ // llvm.stackrestore does not capture its argument, but it is not marked
+ // nocapture because of its unusual memory semantics. Treating it as an
+ // escape would block tail calls after every VLA scope.
+ if (auto *II = dyn_cast<IntrinsicInst>(I);
+ II && II->getIntrinsicID() == Intrinsic::stackrestore)
+ continue;
// If the alloca-derived argument is passed byval it is not an escape
// point, or a use of an alloca. Calling with byval copies the contents
// of the alloca into argument registers or stack slots, which exist
@@ -223,8 +229,8 @@ struct AllocaDerivedValueTracker {
}
void callUsesLocalStack(CallBase &CB, bool IsNocapture) {
- // Add it to the list of alloca users.
- AllocaUsers.insert(&CB);
+ // Add it to the list of calls that use the local stack.
+ LocalStackUsers.insert(&CB);
// If it's nocapture then it can't capture this alloca.
if (IsNocapture)
@@ -235,26 +241,48 @@ struct AllocaDerivedValueTracker {
EscapePoints.insert(&CB);
}
- SmallPtrSet<Instruction *, 32> AllocaUsers;
+ SmallPtrSet<Instruction *, 32> LocalStackUsers;
SmallPtrSet<Instruction *, 32> EscapePoints;
};
} // namespace
+/// Returns true if \p II returns an address in the current function's frame.
+static bool returnsCurrentFrameAddress(const IntrinsicInst *II) {
+ if (!II)
+ return false;
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::frameaddress:
+ // A non-zero level refers to a caller's frame, which outlives a tail call.
+ return cast<ConstantInt>(II->getArgOperand(0))->isZero();
+ case Intrinsic::addressofreturnaddress:
+ case Intrinsic::eh_dwarf_cfa:
+ case Intrinsic::localaddress:
+ case Intrinsic::sponentry:
+ case Intrinsic::stackaddress:
+ case Intrinsic::stacksave:
+ case Intrinsic::swift_async_context_addr:
+ return true;
+ default:
+ return false;
+ }
+}
+
static bool markTails(Function &F, OptimizationRemarkEmitter *ORE,
ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) {
if (F.callsFunctionThatReturnsTwice())
return false;
- // The local stack holds all alloca instructions and all byval arguments.
- AllocaDerivedValueTracker Tracker;
+ // The local stack holds allocas and byval arguments, and frame-address
+ // intrinsics point into it.
+ LocalStackValueTracker Tracker;
for (Argument &Arg : F.args()) {
if (Arg.hasByValAttr())
Tracker.walk(&Arg);
}
- for (auto &BB : F) {
- for (auto &I : BB)
- if (AllocaInst *AI = dyn_cast<AllocaInst>(&I))
- Tracker.walk(AI);
+ for (Instruction &I : instructions(F)) {
+ if (isa<AllocaInst>(&I) ||
+ returnsCurrentFrameAddress(dyn_cast<IntrinsicInst>(&I)))
+ Tracker.walk(&I);
}
bool Modified = false;
@@ -320,7 +348,7 @@ static bool markTails(Function &F, OptimizationRemarkEmitter *ORE,
// global anyhow.
//
// Note that this runs whether we know an alloca has escaped or not. If
- // it has, then we can't trust Tracker.AllocaUsers to be accurate.
+ // it has, then we can't trust Tracker.LocalStackUsers to be accurate.
bool SafeToTail = true;
for (auto &Arg : CI->args()) {
if (isa<Constant>(Arg.getUser()))
@@ -343,7 +371,8 @@ static bool markTails(Function &F, OptimizationRemarkEmitter *ORE,
}
}
- if (!IsNoTail && Escaped == UNESCAPED && !Tracker.AllocaUsers.count(CI))
+ if (!IsNoTail && Escaped == UNESCAPED &&
+ !Tracker.LocalStackUsers.count(CI))
DeferredTails.push_back(CI);
}
diff --git a/llvm/test/Transforms/TailCallElim/frame-address.ll b/llvm/test/Transforms/TailCallElim/frame-address.ll
new file mode 100644
index 0000000000000..b80f67118a9c1
--- /dev/null
+++ b/llvm/test/Transforms/TailCallElim/frame-address.ll
@@ -0,0 +1,154 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=tailcallelim -S | FileCheck %s
+
+; A call that is passed an address in the current frame must not be marked tail.
+
+declare ptr @llvm.frameaddress.p0(i32)
+declare ptr @llvm.returnaddress.p0(i32)
+declare ptr @llvm.addressofreturnaddress.p0()
+declare ptr @llvm.eh.dwarf.cfa(i32)
+declare ptr @llvm.localaddress()
+declare ptr @llvm.sponentry.p0()
+declare ptr @llvm.stackaddress.p0()
+declare ptr @llvm.stacksave.p0()
+declare ptr @llvm.swift.async.context.addr()
+declare void @callee(ptr)
+
+define void @pass_frameaddress() {
+; CHECK-LABEL: define void @pass_frameaddress() {
+; CHECK-NEXT: [[FA:%.*]] = tail call ptr @llvm.frameaddress.p0(i32 0)
+; CHECK-NEXT: call void @callee(ptr [[FA]])
+; CHECK-NEXT: ret void
+;
+ %fa = call ptr @llvm.frameaddress.p0(i32 0)
+ call void @callee(ptr %fa)
+ ret void
+}
+
+; A non-zero level refers to a caller's frame.
+define void @pass_parent_frameaddress() {
+; CHECK-LABEL: define void @pass_parent_frameaddress() {
+; CHECK-NEXT: [[FA:%.*]] = tail call ptr @llvm.frameaddress.p0(i32 1)
+; CHECK-NEXT: tail call void @callee(ptr [[FA]])
+; CHECK-NEXT: ret void
+;
+ %fa = call ptr @llvm.frameaddress.p0(i32 1)
+ call void @callee(ptr %fa)
+ ret void
+}
+
+define void @pass_addressofreturnaddress() {
+; CHECK-LABEL: define void @pass_addressofreturnaddress() {
+; CHECK-NEXT: [[RA:%.*]] = tail call ptr @llvm.addressofreturnaddress.p0()
+; CHECK-NEXT: call void @callee(ptr [[RA]])
+; CHECK-NEXT: ret void
+;
+ %ra = call ptr @llvm.addressofreturnaddress.p0()
+ call void @callee(ptr %ra)
+ ret void
+}
+
+define void @pass_dwarf_cfa() {
+; CHECK-LABEL: define void @pass_dwarf_cfa() {
+; CHECK-NEXT: [[CFA:%.*]] = tail call ptr @llvm.eh.dwarf.cfa(i32 0)
+; CHECK-NEXT: call void @callee(ptr [[CFA]])
+; CHECK-NEXT: ret void
+;
+ %cfa = call ptr @llvm.eh.dwarf.cfa(i32 0)
+ call void @callee(ptr %cfa)
+ ret void
+}
+
+define void @pass_localaddress() {
+; CHECK-LABEL: define void @pass_localaddress() {
+; CHECK-NEXT: [[LA:%.*]] = tail call ptr @llvm.localaddress()
+; CHECK-NEXT: call void @callee(ptr [[LA]])
+; CHECK-NEXT: ret void
+;
+ %la = call ptr @llvm.localaddress()
+ call void @callee(ptr %la)
+ ret void
+}
+
+define void @pass_sponentry() {
+; CHECK-LABEL: define void @pass_sponentry() {
+; CHECK-NEXT: [[SP:%.*]] = tail call ptr @llvm.sponentry.p0()
+; CHECK-NEXT: call void @callee(ptr [[SP]])
+; CHECK-NEXT: ret void
+;
+ %sp = call ptr @llvm.sponentry.p0()
+ call void @callee(ptr %sp)
+ ret void
+}
+
+define void @pass_stackaddress() {
+; CHECK-LABEL: define void @pass_stackaddress() {
+; CHECK-NEXT: [[SA:%.*]] = tail call ptr @llvm.stackaddress.p0()
+; CHECK-NEXT: call void @callee(ptr [[SA]])
+; CHECK-NEXT: ret void
+;
+ %sa = call ptr @llvm.stackaddress.p0()
+ call void @callee(ptr %sa)
+ ret void
+}
+
+define void @pass_stacksave() {
+; CHECK-LABEL: define void @pass_stacksave() {
+; CHECK-NEXT: [[SS:%.*]] = tail call ptr @llvm.stacksave.p0()
+; CHECK-NEXT: call void @callee(ptr [[SS]])
+; CHECK-NEXT: ret void
+;
+ %ss = call ptr @llvm.stacksave.p0()
+ call void @callee(ptr %ss)
+ ret void
+}
+
+define void @pass_swift_async_context_addr() {
+; CHECK-LABEL: define void @pass_swift_async_context_addr() {
+; CHECK-NEXT: [[CTX:%.*]] = tail call ptr @llvm.swift.async.context.addr()
+; CHECK-NEXT: call void @callee(ptr [[CTX]])
+; CHECK-NEXT: ret void
+;
+ %ctx = call ptr @llvm.swift.async.context.addr()
+ call void @callee(ptr %ctx)
+ ret void
+}
+
+; llvm.returnaddress is a code address.
+define void @pass_returnaddress() {
+; CHECK-LABEL: define void @pass_returnaddress() {
+; CHECK-NEXT: [[RA:%.*]] = tail call ptr @llvm.returnaddress.p0(i32 0)
+; CHECK-NEXT: tail call void @callee(ptr [[RA]])
+; CHECK-NEXT: ret void
+;
+ %ra = call ptr @llvm.returnaddress.p0(i32 0)
+ call void @callee(ptr %ra)
+ ret void
+}
+
+define void @pass_frameaddress_gep() {
+; CHECK-LABEL: define void @pass_frameaddress_gep() {
+; CHECK-NEXT: [[FA:%.*]] = tail call ptr @llvm.frameaddress.p0(i32 0)
+; CHECK-NEXT: [[P:%.*]] = getelementptr i8, ptr [[FA]], i64 8
+; CHECK-NEXT: call void @callee(ptr [[P]])
+; CHECK-NEXT: ret void
+;
+ %fa = call ptr @llvm.frameaddress.p0(i32 0)
+ %p = getelementptr i8, ptr %fa, i64 8
+ call void @callee(ptr %p)
+ ret void
+}
+
+define i1 @keep_frameaddress_local(ptr %p) {
+; CHECK-LABEL: define i1 @keep_frameaddress_local(
+; CHECK-SAME: ptr [[P:%.*]]) {
+; CHECK-NEXT: [[FA:%.*]] = tail call ptr @llvm.frameaddress.p0(i32 0)
+; CHECK-NEXT: tail call void @callee(ptr [[P]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[FA]], [[P]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %fa = call ptr @llvm.frameaddress.p0(i32 0)
+ call void @callee(ptr %p)
+ %cmp = icmp ult ptr %fa, %p
+ ret i1 %cmp
+}
diff --git a/llvm/test/Transforms/TailCallElim/stackrestore.ll b/llvm/test/Transforms/TailCallElim/stackrestore.ll
index 55b1ec933e1ec..8d376aeb596a2 100644
--- a/llvm/test/Transforms/TailCallElim/stackrestore.ll
+++ b/llvm/test/Transforms/TailCallElim/stackrestore.ll
@@ -10,5 +10,52 @@ entry:
ret void
}
+; llvm.stackrestore does not capture its argument, so the call after it can
+; still be marked tail.
+define void @stackrestore_is_not_an_escape(ptr %p) {
+; CHECK-LABEL: define void @stackrestore_is_not_an_escape(
+; CHECK: tail call void @callee
+;
+entry:
+ %ss = call ptr @llvm.stacksave.p0()
+ call void @llvm.stackrestore.p0(ptr %ss)
+ call void @callee(ptr %p)
+ ret void
+}
+
+define void @stackrestore_of_a_phi_is_not_an_escape(ptr %p, i1 %c) {
+; CHECK-LABEL: define void @stackrestore_of_a_phi_is_not_an_escape(
+; CHECK: tail call void @callee
+;
+entry:
+ %ss = call ptr @llvm.stacksave.p0()
+ br i1 %c, label %then, label %else
+
+then:
+ %ss2 = call ptr @llvm.stacksave.p0()
+ br label %join
+
+else:
+ br label %join
+
+join:
+ %sink = phi ptr [ %ss2, %then ], [ %ss, %else ]
+ call void @llvm.stackrestore.p0(ptr %sink)
+ call void @callee(ptr %p)
+ ret void
+}
+
+define void @stackrestore_of_an_alloca_is_not_an_escape(ptr %p) {
+; CHECK-LABEL: define void @stackrestore_of_an_alloca_is_not_an_escape(
+; CHECK: tail call void @callee
+;
+entry:
+ %a = alloca i8
+ call void @llvm.stackrestore.p0(ptr %a)
+ call void @callee(ptr %p)
+ ret void
+}
+
+declare void @callee(ptr)
declare ptr @llvm.stacksave.p0()
declare void @llvm.stackrestore.p0(ptr)
More information about the llvm-commits
mailing list