Skip to content

Conversation

@arsenm
Copy link
Contributor

@arsenm arsenm commented Nov 7, 2025

These were in TargetLibraryInfo, but missing from RuntimeLibcalls.
This only adds the cases that already have the non-chk variants
already. Copies the enabled-by-default logic from TargetLibraryInfo,
which is probably overly permissive. Only isPS opts-out.

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2025

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-selectiondag

Author: Matt Arsenault (arsenm)

Changes

These were in TargetLibraryInfo, but missing from RuntimeLibcalls.
This only adds the cases that already have the non-chk variants
already. Copies the enabled-by-default logic from TargetLibraryInfo,
which is probably overly permissive. Only isPS opts-out.


Full diff: https://github.com/llvm/llvm-project/pull/167053.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (+15-2)
  • (modified) llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll (+4)
  • (added) llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll (+6)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 6186e0bf21895..8b94c537caf95 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -35,6 +35,9 @@ def isNotOSLinuxAndNotOSOpenBSD : RuntimeLibcallPredicate<
 def isNotOSAIXAndNotOSOpenBSD : RuntimeLibcallPredicate<
   [{!TT.isOSAIX() && !TT.isOSOpenBSD()}]>;
 
+def isNotPS : RuntimeLibcallPredicate<
+  [{!TT.isPS()}]>;
+
 // OpenBSD uses __guard_local. AIX uses __ssp_canary_word, MSVC/Windows
 // Itanium uses __security_cookie
 def hasStackChkFail : RuntimeLibcallPredicate<
@@ -375,8 +378,11 @@ foreach FPTy = ["F32", "F64", "F128", "PPCF128"] in {
 // Memory
 def MEMCMP : RuntimeLibcall;
 def MEMCPY : RuntimeLibcall;
+def MEMCPY_CHK : RuntimeLibcall;
 def MEMMOVE : RuntimeLibcall;
+def MEMMOVE_CHK : RuntimeLibcall;
 def MEMSET : RuntimeLibcall;
+def MEMSET_CHK : RuntimeLibcall;
 def CALLOC : RuntimeLibcall;
 def BZERO : RuntimeLibcall;
 def STRLEN : RuntimeLibcall;
@@ -1092,6 +1098,10 @@ def memcpy : RuntimeLibcallImpl<MEMCPY>;
 def memmove : RuntimeLibcallImpl<MEMMOVE>;
 def memset : RuntimeLibcallImpl<MEMSET>;
 
+def __memcpy_chk : RuntimeLibcallImpl<MEMCPY_CHK>;
+def __memmove_chk : RuntimeLibcallImpl<MEMMOVE_CHK>;
+def __memset_chk : RuntimeLibcallImpl<MEMSET_CHK>;
+
 // DSEPass can emit calloc if it finds a pair of malloc/memset
 def calloc : RuntimeLibcallImpl<CALLOC>;
 
@@ -2625,8 +2635,10 @@ defvar X86_F128_Libcalls = LibcallImpls<(add LibmF128Libcalls, LibmF128FiniteLib
 
 defvar SinCosF32F64Libcalls = LibcallImpls<(add sincosf, sincos), hasSinCos_f32_f64>;
 
+defvar MemChkLibcalls = [__memcpy_chk, __memset_chk, __memmove_chk];
+
 defvar X86CommonLibcalls =
-  (add (sub WinDefaultLibcallImpls, WindowsDivRemMulLibcallOverrides),
+  (add (sub WinDefaultLibcallImpls, WindowsDivRemMulLibcallOverrides, MemChkLibcalls),
        DarwinSinCosStret, DarwinExp10,
        X86_F128_Libcalls,
        LibmHasSinCosF80, // FIXME: Depends on long double
@@ -2642,7 +2654,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,
-       DefaultStackProtector
+       DefaultStackProtector,
+       LibcallImpls<(add MemChkLibcalls), isNotPS>
      );
 
 defvar Windows32DivRemMulCalls =
diff --git a/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll
index be8cae261c7bf..db0cc24c287bc 100644
--- a/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll
+++ b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll
@@ -12,6 +12,10 @@ define float @sinf(float %x) {
 
 ; CHECK: declare void @_Unwind_Resume(...)
 
+; CHECK: declare void @__memcpy_chk(...)
+; CHECK: declare void @__memmove_chk(...)
+; CHECK: declare void @__memset_chk(...)
+
 ; CHECK: declare void @__umodti3(...)
 
 ; CHECK: declare void @acosf(...)
diff --git a/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll
new file mode 100644
index 0000000000000..bcdcc63400f72
--- /dev/null
+++ b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll
@@ -0,0 +1,6 @@
+; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=x86_64-scei-ps4 < %s | FileCheck %s
+; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=x86_64-scei-ps5 < %s | FileCheck %s
+
+; CHECK-NOT: __memcpy_chk
+; CHECK-NOT: __memset_chk
+; CHECK-NOT: __memmove_chk

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

These were in TargetLibraryInfo, but missing from RuntimeLibcalls.
This only adds the cases that already have the non-chk variants
already. Copies the enabled-by-default logic from TargetLibraryInfo,
which is probably overly permissive. Only isPS opts-out.


Full diff: https://github.com/llvm/llvm-project/pull/167053.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (+15-2)
  • (modified) llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll (+4)
  • (added) llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll (+6)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 6186e0bf21895..8b94c537caf95 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -35,6 +35,9 @@ def isNotOSLinuxAndNotOSOpenBSD : RuntimeLibcallPredicate<
 def isNotOSAIXAndNotOSOpenBSD : RuntimeLibcallPredicate<
   [{!TT.isOSAIX() && !TT.isOSOpenBSD()}]>;
 
+def isNotPS : RuntimeLibcallPredicate<
+  [{!TT.isPS()}]>;
+
 // OpenBSD uses __guard_local. AIX uses __ssp_canary_word, MSVC/Windows
 // Itanium uses __security_cookie
 def hasStackChkFail : RuntimeLibcallPredicate<
@@ -375,8 +378,11 @@ foreach FPTy = ["F32", "F64", "F128", "PPCF128"] in {
 // Memory
 def MEMCMP : RuntimeLibcall;
 def MEMCPY : RuntimeLibcall;
+def MEMCPY_CHK : RuntimeLibcall;
 def MEMMOVE : RuntimeLibcall;
+def MEMMOVE_CHK : RuntimeLibcall;
 def MEMSET : RuntimeLibcall;
+def MEMSET_CHK : RuntimeLibcall;
 def CALLOC : RuntimeLibcall;
 def BZERO : RuntimeLibcall;
 def STRLEN : RuntimeLibcall;
@@ -1092,6 +1098,10 @@ def memcpy : RuntimeLibcallImpl<MEMCPY>;
 def memmove : RuntimeLibcallImpl<MEMMOVE>;
 def memset : RuntimeLibcallImpl<MEMSET>;
 
+def __memcpy_chk : RuntimeLibcallImpl<MEMCPY_CHK>;
+def __memmove_chk : RuntimeLibcallImpl<MEMMOVE_CHK>;
+def __memset_chk : RuntimeLibcallImpl<MEMSET_CHK>;
+
 // DSEPass can emit calloc if it finds a pair of malloc/memset
 def calloc : RuntimeLibcallImpl<CALLOC>;
 
@@ -2625,8 +2635,10 @@ defvar X86_F128_Libcalls = LibcallImpls<(add LibmF128Libcalls, LibmF128FiniteLib
 
 defvar SinCosF32F64Libcalls = LibcallImpls<(add sincosf, sincos), hasSinCos_f32_f64>;
 
+defvar MemChkLibcalls = [__memcpy_chk, __memset_chk, __memmove_chk];
+
 defvar X86CommonLibcalls =
-  (add (sub WinDefaultLibcallImpls, WindowsDivRemMulLibcallOverrides),
+  (add (sub WinDefaultLibcallImpls, WindowsDivRemMulLibcallOverrides, MemChkLibcalls),
        DarwinSinCosStret, DarwinExp10,
        X86_F128_Libcalls,
        LibmHasSinCosF80, // FIXME: Depends on long double
@@ -2642,7 +2654,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,
-       DefaultStackProtector
+       DefaultStackProtector,
+       LibcallImpls<(add MemChkLibcalls), isNotPS>
      );
 
 defvar Windows32DivRemMulCalls =
diff --git a/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll
index be8cae261c7bf..db0cc24c287bc 100644
--- a/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll
+++ b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/basic.ll
@@ -12,6 +12,10 @@ define float @sinf(float %x) {
 
 ; CHECK: declare void @_Unwind_Resume(...)
 
+; CHECK: declare void @__memcpy_chk(...)
+; CHECK: declare void @__memmove_chk(...)
+; CHECK: declare void @__memset_chk(...)
+
 ; CHECK: declare void @__umodti3(...)
 
 ; CHECK: declare void @acosf(...)
diff --git a/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll
new file mode 100644
index 0000000000000..bcdcc63400f72
--- /dev/null
+++ b/llvm/test/Transforms/Util/DeclareRuntimeLibcalls/ps.ll
@@ -0,0 +1,6 @@
+; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=x86_64-scei-ps4 < %s | FileCheck %s
+; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=x86_64-scei-ps5 < %s | FileCheck %s
+
+; CHECK-NOT: __memcpy_chk
+; CHECK-NOT: __memset_chk
+; CHECK-NOT: __memmove_chk

Copy link
Contributor Author

arsenm commented Nov 7, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-libm-functions-from-targetlibraryinfo branch from 1dadec1 to 8ddc3c6 Compare November 8, 2025 02:34
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-memcpy-memmove-memset-chk-functions branch from ff7fa3a to 31b8490 Compare November 8, 2025 02:34
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-memcpy-memmove-memset-chk-functions branch from 31b8490 to e551019 Compare November 10, 2025 18:18
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-libm-functions-from-targetlibraryinfo branch from 8ddc3c6 to ae48224 Compare November 10, 2025 18:18
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-memcpy-memmove-memset-chk-functions branch from e551019 to 9b770d3 Compare November 10, 2025 19:22
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-libm-functions-from-targetlibraryinfo branch from ae48224 to 5a3c882 Compare November 10, 2025 19:22
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM

@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-libm-functions-from-targetlibraryinfo branch from 5a3c882 to dc9eae3 Compare November 12, 2025 00:57
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-memcpy-memmove-memset-chk-functions branch from 9b770d3 to abbd23c Compare November 12, 2025 00:57
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-libm-functions-from-targetlibraryinfo branch from dc9eae3 to cf7c5ea Compare November 12, 2025 02:11
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-memcpy-memmove-memset-chk-functions branch from abbd23c to 06269dd Compare November 12, 2025 02:11
These were in TargetLibraryInfo, but missing from RuntimeLibcalls.
This only adds the cases that already have the non-chk variants
already. Copies the enabled-by-default logic from TargetLibraryInfo,
which is probably overly permissive. Only isPS opts-out.
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-memcpy-memmove-memset-chk-functions branch from 06269dd to abf1fe1 Compare November 12, 2025 03:24
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-libm-functions-from-targetlibraryinfo branch from cf7c5ea to 57b5e69 Compare November 12, 2025 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants