[llvm] SafeStack: Emit __safestack_pointer_address call through RuntimeLibcalls (PR #147916)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 15 02:53:07 PDT 2025
https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/147916
>From 3ec4dc97379e3e40962d9af1d55035105f787248 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 10 Jul 2025 15:09:37 +0900
Subject: [PATCH] SafeStack: Emit __safestack_pointer_address call through
RuntimeLibcalls
Stop using hardcoded function named and check availability. This only fixes
the forced usage via command line in the pass itself; the implementations
inside of TargetLoweringBase hide additional call emission.
---
llvm/include/llvm/IR/RuntimeLibcalls.td | 6 ++++++
llvm/lib/CodeGen/SafeStack.cpp | 10 +++++++++-
.../NVPTX/safestack-pointer-address-libcall-error.ll | 12 ++++++++++++
3 files changed, 27 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 989e2e7e7631a..a6254eafa4906 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -362,6 +362,9 @@ defset list<RuntimeLibcall> LibCalls__OutOfLineAtomic = {
def STACKPROTECTOR_CHECK_FAIL : RuntimeLibcall;
def STACK_SMASH_HANDLER : RuntimeLibcall;
+// Safe stack
+def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
+
// Deoptimization
def DEOPTIMIZE : RuntimeLibcall;
@@ -702,6 +705,9 @@ foreach lc = LibCalls__atomic in {
// Stack Protector Fail
def __stack_chk_fail : RuntimeLibcallImpl<STACKPROTECTOR_CHECK_FAIL>;
+// Safe stack.
+def __safestack_pointer_address : RuntimeLibcallImpl<SAFESTACK_POINTER_ADDRESS>;
+
// Deoptimization
def __llvm_deoptimize : RuntimeLibcallImpl<DEOPTIMIZE>;
diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp
index a6c1a76f53f39..996207034d076 100644
--- a/llvm/lib/CodeGen/SafeStack.cpp
+++ b/llvm/lib/CodeGen/SafeStack.cpp
@@ -799,8 +799,16 @@ bool SafeStack::run() {
IRB.SetCurrentDebugLocation(
DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP));
if (SafeStackUsePointerAddress) {
+ const char *SafestackPointerAddressName =
+ TL.getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
+ if (!SafestackPointerAddressName) {
+ F.getContext().emitError(
+ "no libcall available for safestack pointer address");
+ return false;
+ }
+
FunctionCallee Fn = F.getParent()->getOrInsertFunction(
- "__safestack_pointer_address", IRB.getPtrTy(0));
+ SafestackPointerAddressName, IRB.getPtrTy(0));
UnsafeStackPtr = IRB.CreateCall(Fn);
} else {
UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
diff --git a/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
new file mode 100644
index 0000000000000..9bf84585e5468
--- /dev/null
+++ b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
@@ -0,0 +1,12 @@
+; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s
+
+; CHECK: error: no libcall available for safestack pointer address
+define void @foo(i32 %t) #0 {
+ %vla = alloca i32, i32 %t, align 4
+ call void @baz(ptr %vla)
+ ret void
+}
+
+declare void @baz(ptr)
+
+attributes #0 = { nounwind safestack sspstrong }
More information about the llvm-commits
mailing list