[llvm] d77f07d - [TailCallElim] Don’t mark llvm.stackrestore with tail-call (#101352)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 5 08:47:58 PDT 2024


Author: Shimin Cui
Date: 2024-08-05T11:47:53-04:00
New Revision: d77f07d166618d0f33500b518144ada99be2f13f

URL: https://github.com/llvm/llvm-project/commit/d77f07d166618d0f33500b518144ada99be2f13f
DIFF: https://github.com/llvm/llvm-project/commit/d77f07d166618d0f33500b518144ada99be2f13f.diff

LOG: [TailCallElim] Don’t mark llvm.stackrestore with tail-call  (#101352)

This is to teach tailcallelim transformation not to mark
llvm.stackrestore with tail-call, as the intrinsic call can modify
unescaped allocas from the caller.
 
This fixes a problem found with our downstream testing. The problem can
also be shown when running the test case
llvm/test/Transforms/MemCpyOpt/stackrestore with passes=”tailcallelim,
memcpyopt”.

Added: 
    llvm/test/Transforms/TailCallElim/stackrestore.ll

Modified: 
    llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
index 1b3e6d9549b82..53e486f3dc6cd 100644
--- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -243,6 +243,12 @@ static bool markTails(Function &F, OptimizationRemarkEmitter *ORE) {
           isa<PseudoProbeInst>(&I))
         continue;
 
+      // Bail out for intrinsic stackrestore call because it can modify
+      // unescaped allocas.
+      if (auto *II = dyn_cast<IntrinsicInst>(CI))
+        if (II->getIntrinsicID() == Intrinsic::stackrestore)
+          continue;
+
       // Special-case operand bundles "clang.arc.attachedcall", "ptrauth", and
       // "kcfi".
       bool IsNoTail = CI->isNoTailCall() ||

diff  --git a/llvm/test/Transforms/TailCallElim/stackrestore.ll b/llvm/test/Transforms/TailCallElim/stackrestore.ll
new file mode 100644
index 0000000000000..55b1ec933e1ec
--- /dev/null
+++ b/llvm/test/Transforms/TailCallElim/stackrestore.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S -passes=tailcallelim < %s | FileCheck %s
+
+define void @foo() {
+; CHECK-LABEL: define void @foo()
+; CHECK-NOT:   tail call void @llvm.stackrestore.p0
+;
+entry:
+  %0 = call ptr @llvm.stacksave.p0()
+  call void @llvm.stackrestore.p0(ptr %0)
+  ret void
+}
+
+declare ptr @llvm.stacksave.p0()
+declare void @llvm.stackrestore.p0(ptr)


        


More information about the llvm-commits mailing list