[llvm] TargetLowering: Replace android triple check with libcall check (PR #148800)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 15 07:28:34 PDT 2025


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/148800

>From 3e4f6932df6639dce303fcff9e3173962e888885 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 10 Jul 2025 15:55:44 +0900
Subject: [PATCH] TargetLowering: Replace android triple check with libcall
 check

Instead of directly checking if the target is android, check if
__safestack_pointer_address is available and configure android
to have the call.
---
 llvm/include/llvm/IR/RuntimeLibcalls.td | 11 ++++++---
 llvm/lib/CodeGen/TargetLoweringBase.cpp | 33 +++++++++----------------
 2 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 11926d4128fcf..c0a37c6edf4f9 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -24,7 +24,7 @@ def isNotOSMSVCRT : RuntimeLibcallPredicate<"!TT.isOSMSVCRT()">;
 def isPS : RuntimeLibcallPredicate<"TT.isPS()">;
 def isNotOSWindowsOrIsCygwinMinGW
   : RuntimeLibcallPredicate<"!TT.isOSWindows() || TT.isOSCygMing()">;
-
+def isAndroid : RuntimeLibcallPredicate<"TT.isAndroid()">;
 
 def isGNUEnvironment : RuntimeLibcallPredicate<"TT.isGNUEnvironment()">;
 def darwinHasSinCosStret : RuntimeLibcallPredicate<"darwinHasSinCosStret(TT)">;
@@ -1135,6 +1135,8 @@ defvar LibmHasLdexpF80 = LibcallImpls<(add ldexp_f80), isNotOSWindowsOrIsCygwinM
 defvar LibmHasFrexpF128 = LibcallImpls<(add frexp_f128), isNotOSWindowsOrIsCygwinMinGW>;
 defvar LibmHasLdexpF128 = LibcallImpls<(add ldexp_f128), isNotOSWindowsOrIsCygwinMinGW>;
 
+defvar LibcSafestackPointerAddress =
+    LibcallImpls<(add __safestack_pointer_address), isAndroid>;
 
 //===----------------------------------------------------------------------===//
 // Objective-C Runtime Libcalls
@@ -1211,7 +1213,8 @@ def AArch64SystemLibrary : SystemRuntimeLibrary<
        LibcallImpls<(add Int128RTLibcalls), isAArch64_ILP64>,
        LibcallImpls<(add bzero), isOSDarwin>,
        DarwinExp10, DarwinSinCosStret,
-       LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128)
+       LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
+       LibcSafestackPointerAddress)
 >;
 
 // Prepend a # to every name
@@ -1482,6 +1485,7 @@ def ARMSystemLibrary
            AEABIDivRemCalls,
            DarwinSinCosStret, DarwinExp10,
            LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
+           LibcSafestackPointerAddress,
 
            // Use divmod compiler-rt calls for iOS 5.0 and later.
            LibcallImpls<(add __divmodsi4, __udivmodsi4),
@@ -2125,7 +2129,8 @@ defvar X86CommonLibcalls =
        // FIXME: MSVCRT doesn't have powi. The f128 case is added as a
        // hack for one test relying on it.
        __powitf2_f128,
-       LibcallImpls<(add MostPowI), isNotOSMSVCRT>
+       LibcallImpls<(add MostPowI), isNotOSMSVCRT>,
+       LibcSafestackPointerAddress
 );
 
 defvar Windows32DivRemMulCalls =
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index d4a34555ed820..85a507fca65d7 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -1965,27 +1965,18 @@ TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
 
 Value *
 TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
-  // FIXME: Can this triple check be replaced with SAFESTACK_POINTER_ADDRESS
-  // being available?
-  if (!TM.getTargetTriple().isAndroid())
-    return getDefaultSafeStackPointerLocation(IRB, true);
-
-  Module *M = IRB.GetInsertBlock()->getParent()->getParent();
-  auto *PtrTy = PointerType::getUnqual(M->getContext());
-
-  const char *SafestackPointerAddressName =
-      getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
-  if (!SafestackPointerAddressName) {
-    M->getContext().emitError(
-        "no libcall available for safestack pointer address");
-    return PoisonValue::get(PtrTy);
-  }
-
-  // Android provides a libc function to retrieve the address of the current
-  // thread's unsafe stack pointer.
-  FunctionCallee Fn =
-      M->getOrInsertFunction(SafestackPointerAddressName, PtrTy);
-  return IRB.CreateCall(Fn);
+  if (const char *SafestackPointerAddressName =
+          getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS)) {
+    // Android provides a libc function to retrieve the address of the current
+    // thread's unsafe stack pointer.
+    Module *M = IRB.GetInsertBlock()->getParent()->getParent();
+    auto *PtrTy = PointerType::getUnqual(M->getContext());
+    FunctionCallee Fn =
+        M->getOrInsertFunction(SafestackPointerAddressName, PtrTy);
+    return IRB.CreateCall(Fn);
+  }
+
+  return getDefaultSafeStackPointerLocation(IRB, true);
 }
 
 //===----------------------------------------------------------------------===//



More information about the llvm-commits mailing list