Skip to content

RuntimeLibcalls: Really move default libcall handling to tablegen #148780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Jul 15, 2025

Hack in the default setting so it's consistently generated like
the other cases. Maintain a list of targets where this applies.
The alternative would require new infrastructure to sort the system
library initialization in some way.

I wanted the unhandled target case to be treated as a fatal
error, but it turns out there's a hack in IRSymtab using
RuntimeLibcalls, which will fail out in many tests that
do not have a triple set. Many of the failures are simply
running llvm-as with no triple, which probably should not
depend on knowing an accurate set of calls.

Copy link
Contributor Author

arsenm commented Jul 15, 2025

@arsenm arsenm marked this pull request as ready for review July 15, 2025 05:23
@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-tablegen
@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-backend-x86

Author: Matt Arsenault (arsenm)

Changes

Hack in the default setting so it's consistently generated like
the other cases. Maintain a list of targets where this applies.
The alternative would require new infrastructure to sort the system
library initialization in some way.

I wanted the unhandled target case to be treated as a fatal
error, but it turns out there's a hack in IRSymtab using
RuntimeLibcalls, which will fail out in many tests that
do not have a triple set. Many of the failures are simply
running llvm-as with no triple, which probably should not
depend on knowing an accurate set of calls.


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

5 Files Affected:

  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.h (-5)
  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (+33-3)
  • (modified) llvm/lib/IR/RuntimeLibcalls.cpp (+3-39)
  • (modified) llvm/test/TableGen/RuntimeLibcallEmitter.td (+1-13)
  • (modified) llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp (+5-30)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.h b/llvm/include/llvm/IR/RuntimeLibcalls.h
index 85db45e27e912..7f04a7f4ab5fd 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.h
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.h
@@ -128,9 +128,6 @@ struct RuntimeLibcallsInfo {
   }
 
 private:
-  static const RTLIB::LibcallImpl
-      DefaultLibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1];
-
   /// Stores the implementation choice for each each libcall.
   RTLIB::LibcallImpl LibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {
       RTLIB::Unsupported};
@@ -178,8 +175,6 @@ struct RuntimeLibcallsInfo {
     return hasSinCos(TT) || TT.isPS();
   }
 
-  LLVM_ABI void initDefaultLibCallImpls();
-
   /// Generated by tablegen.
   void setTargetRuntimeLibcallSets(const Triple &TT,
                                    FloatABI::ABIType FloatABI);
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 8ab63bd3d6b93..af339e2a55cf2 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -1581,7 +1581,9 @@ def HexagonSystemLibrary
     (add (sub DefaultLibcallImpls32,
     __adddf3, __divsf3, __udivsi3, __udivdi3,
     __umoddi3, __divdf3, __muldf3, __divsi3, __subdf3, sqrtf,
-    __divdi3, __umodsi3, __moddi3, __modsi3), HexagonLibcalls)>;
+    __divdi3, __umodsi3, __moddi3, __modsi3), HexagonLibcalls,
+    LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
+    exp10f, exp10, exp10l_f128)>;
 
 //===----------------------------------------------------------------------===//
 // Lanai Runtime Libcalls
@@ -2128,8 +2130,12 @@ def __memcpy_4 : RuntimeLibcallImpl<MEMCPY_ALIGN_4>;
 
 def isXCore : RuntimeLibcallPredicate<"TT.getArch() == Triple::xcore">;
 def XCoreSystemLibrary
-    : SystemRuntimeLibrary<isXCore, (add DefaultRuntimeLibcallImpls,
-                                        __memcpy_4)>;
+    : SystemRuntimeLibrary<isXCore,
+    (add DefaultRuntimeLibcallImpls,
+         exp10f, exp10, exp10l_f128,
+         __memcpy_4,
+         LibcallImpls<(add LibmF128Libcalls, LibmF128FiniteLibcalls), isGNUEnvironment>
+)>;
 
 //===----------------------------------------------------------------------===//
 // ZOS Runtime Libcalls
@@ -2246,3 +2252,27 @@ def WasmSystemLibrary
            CompilerRTOnlyInt64Libcalls, CompilerRTOnlyInt128Libcalls,
            exp10f, exp10,
            emscripten_return_address)>;
+
+//===----------------------------------------------------------------------===//
+// Legacy Default Runtime Libcalls
+//===----------------------------------------------------------------------===//
+
+// TODO: Should make every target explicit.
+def isDefaultLibcallArch : RuntimeLibcallPredicate<[{
+  TT.isMIPS() || TT.isLoongArch() || TT.isVE() || TT.isBPF() ||
+  TT.getArch() == Triple::csky || TT.getArch() == Triple::arc ||
+  TT.getArch() == Triple::m68k ||  TT.getArch() == Triple::xtensa ||
+  (TT.isSystemZ() && !TT.isOSzOS()) ||
+  TT.isSPIRV() || TT.isDXIL()
+}]>;
+
+
+def isArch64Bit : RuntimeLibcallPredicate<[{TT.isArch64Bit()}]>;
+def LegacyDefaultSystemLibrary
+    : SystemRuntimeLibrary<isDefaultLibcallArch,
+      (add DefaultRuntimeLibcallImpls,
+         LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
+         exp10f, exp10, exp10l_f128,
+         __powisf2, __powidf2, __powitf2_f128,
+         LibcallImpls<(add Int128RTLibcalls), isArch64Bit>
+)>;
diff --git a/llvm/lib/IR/RuntimeLibcalls.cpp b/llvm/lib/IR/RuntimeLibcalls.cpp
index e3e62726f54bc..b389ab0187d2a 100644
--- a/llvm/lib/IR/RuntimeLibcalls.cpp
+++ b/llvm/lib/IR/RuntimeLibcalls.cpp
@@ -8,6 +8,9 @@
 
 #include "llvm/IR/RuntimeLibcalls.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
+
+#define DEBUG_TYPE "runtime-libcalls-info"
 
 using namespace llvm;
 using namespace RTLIB;
@@ -62,12 +65,6 @@ static void setARMLibcallNames(RuntimeLibcallsInfo &Info, const Triple &TT,
     Info.setLibcallImplCallingConv(Impl, CallingConv::ARM_AAPCS);
 }
 
-void RTLIB::RuntimeLibcallsInfo::initDefaultLibCallImpls() {
-  std::memcpy(LibcallImpls, DefaultLibcallImpls, sizeof(LibcallImpls));
-  static_assert(sizeof(LibcallImpls) == sizeof(DefaultLibcallImpls),
-                "libcall array size should match");
-}
-
 /// Set default libcall names. If a target wants to opt-out of a libcall it
 /// should be placed here.
 void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
@@ -76,10 +73,6 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
                                        EABI EABIVersion, StringRef ABIName) {
   setTargetRuntimeLibcallSets(TT, FloatABI);
 
-  // Early exit for targets that have fully ported to tablegen.
-  if (TT.isAMDGPU() || TT.isNVPTX() || TT.isWasm())
-    return;
-
   if (TT.isX86() || TT.isVE() || TT.isARM() || TT.isThumb()) {
     if (ExceptionModel == ExceptionHandling::SjLj)
       setLibcallImpl(RTLIB::UNWIND_RESUME, RTLIB::_Unwind_SjLj_Resume);
@@ -92,46 +85,17 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
     // FIXME: What about other targets?
     setLibcallImpl(RTLIB::FPEXT_F16_F32, RTLIB::__extendhfsf2);
     setLibcallImpl(RTLIB::FPROUND_F32_F16, RTLIB::__truncsfhf2);
-
-    if (!darwinHasExp10(TT)) {
-      setLibcallImpl(RTLIB::EXP10_F32, RTLIB::Unsupported);
-      setLibcallImpl(RTLIB::EXP10_F64, RTLIB::Unsupported);
-    }
   }
 
   if (TT.isOSOpenBSD()) {
     setLibcallImpl(RTLIB::STACKPROTECTOR_CHECK_FAIL, RTLIB::Unsupported);
   }
 
-  // Skip default manual processing for targets that have been mostly ported to
-  // tablegen for now. Eventually the rest of this should be deleted.
-  if (TT.isX86() || TT.isAArch64() || TT.isWasm() || TT.isPPC())
-    return;
-
   if (TT.isARM() || TT.isThumb()) {
     setARMLibcallNames(*this, TT, FloatABI, EABIVersion);
     return;
   }
 
-  if (hasSinCos(TT)) {
-    setLibcallImpl(RTLIB::SINCOS_F32, RTLIB::sincosf);
-    setLibcallImpl(RTLIB::SINCOS_F64, RTLIB::sincos);
-    setLibcallImpl(RTLIB::SINCOS_F128, RTLIB::sincos_f128);
-  }
-
-  setLibcallImpl(RTLIB::EXP10_F32, RTLIB::exp10f);
-  setLibcallImpl(RTLIB::EXP10_F64, RTLIB::exp10);
-  setLibcallImpl(RTLIB::EXP10_F128, RTLIB::exp10l_f128);
-
-  // These libcalls are only available in compiler-rt, not libgcc.
-  if (TT.isArch64Bit()) {
-    setLibcallImpl(RTLIB::SHL_I128, RTLIB::__ashlti3);
-    setLibcallImpl(RTLIB::SRL_I128, RTLIB::__lshrti3);
-    setLibcallImpl(RTLIB::SRA_I128, RTLIB::__ashrti3);
-    setLibcallImpl(RTLIB::MUL_I128, RTLIB::__multi3);
-    setLibcallImpl(RTLIB::MULO_I64, RTLIB::__mulodi4);
-  }
-
   if (TT.getArch() == Triple::ArchType::msp430) {
     setLibcallImplCallingConv(RTLIB::__mspabi_mpyll,
                               CallingConv::MSP430_BUILTIN);
diff --git a/llvm/test/TableGen/RuntimeLibcallEmitter.td b/llvm/test/TableGen/RuntimeLibcallEmitter.td
index a0061afab1db0..61c0468fbb7a1 100644
--- a/llvm/test/TableGen/RuntimeLibcallEmitter.td
+++ b/llvm/test/TableGen/RuntimeLibcallEmitter.td
@@ -104,18 +104,6 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
 // CHECK-NEXT: #endif
 
 // CHECK: #ifdef GET_INIT_RUNTIME_LIBCALL_NAMES
-// CHECK-NEXT: const RTLIB::LibcallImpl llvm::RTLIB::RuntimeLibcallsInfo::DefaultLibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {
-// CHECK-NEXT:   RTLIB::Unsupported, // RTLIB::BZERO
-// CHECK-NEXT:   RTLIB::Unsupported, // RTLIB::CALLOC
-// CHECK-NEXT:   RTLIB::Unsupported, // RTLIB::MEMCPY
-// CHECK-NEXT:   RTLIB::Unsupported, // RTLIB::MEMSET
-// CHECK-NEXT:   RTLIB::__ashlsi3, // RTLIB::SHL_I32
-// CHECK-NEXT:   RTLIB::sqrtl_f80, // RTLIB::SQRT_F80
-// CHECK-NEXT:   RTLIB::sqrtl_f128, // RTLIB::SQRT_F128
-// CHECK-NEXT:   RTLIB::__lshrdi3, // RTLIB::SRL_I64
-// CHECK-NEXT:   RTLIB::Unsupported
-// CHECK-NEXT: };
-// CHECK-EMPTY:
 // CHECK-NEXT: const char *const llvm::RTLIB::RuntimeLibcallsInfo::LibCallImplNames[RTLIB::NumLibcallImpls] = {
 // CHECK-NEXT: nullptr, // RTLIB::Unsupported
 // CHECK-NEXT: "___memcpy", // RTLIB::___memcpy
@@ -236,7 +224,7 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
 // CHECK-EMPTY:
 // CHECK-NEXT:   return;
 // CHECK-NEXT: }
-// CHECK-NEXT:  initDefaultLibCallImpls();
+// CHECK-NEXT:  LLVM_DEBUG(dbgs() << "no system runtime library applied to target \'" << TT.str() << "\'\n");
 // CHECK-NEXT: }
 // CHECK-EMPTY:
 // CHECK: #endif
diff --git a/llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp b/llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp
index 68bd1b5c9cb21..11f2075263a94 100644
--- a/llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp
@@ -307,29 +307,6 @@ void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
     raw_ostream &OS) const {
   // TODO: Emit libcall names as string offset table.
 
-  OS << "const RTLIB::LibcallImpl "
-        "llvm::RTLIB::RuntimeLibcallsInfo::"
-        "DefaultLibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {\n";
-
-  for (const RuntimeLibcall &LibCall : RuntimeLibcallDefList) {
-    auto I = LibCallToDefaultImpl.find(&LibCall);
-    if (I == LibCallToDefaultImpl.end()) {
-      OS << "  RTLIB::Unsupported,";
-    } else {
-      const RuntimeLibcallImpl *LibCallImpl = I->second;
-      OS << "  ";
-      LibCallImpl->emitEnumEntry(OS);
-      OS << ',';
-    }
-
-    OS << " // ";
-    LibCall.emitEnumEntry(OS);
-    OS << '\n';
-  }
-
-  OS << "  RTLIB::Unsupported\n"
-        "};\n\n";
-
   // Emit the implementation names
   OS << "const char *const llvm::RTLIB::RuntimeLibcallsInfo::"
         "LibCallImplNames[RTLIB::NumLibcallImpls] = {\n"
@@ -528,13 +505,11 @@ void RuntimeLibcallEmitter::emitSystemRuntimeLibrarySetCalls(
     TopLevelPredicate.emitEndIf(OS);
   }
 
-  // Fallback to the old default set for manual table entries.
-  //
-  // TODO: Remove this when targets have switched to using generated tables by
-  // default.
-  OS << "  initDefaultLibCallImpls();\n";
-
-  OS << "}\n\n";
+  // FIXME: This should be a fatal error. A few contexts are improperly relying
+  // on RuntimeLibcalls constructed with fully unknown triples.
+  OS << "  LLVM_DEBUG(dbgs() << \"no system runtime library applied to target "
+        "\\'\" << TT.str() << \"\\'\\n\");\n"
+        "}\n\n";
 }
 
 void RuntimeLibcallEmitter::run(raw_ostream &OS) {

@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/use-tablegen-default-handling branch from 14d4d03 to ef419d6 Compare July 15, 2025 06:19
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/stop-opt-out-exp10 branch from a64799d to c35ac3c Compare July 15, 2025 06:19
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/stop-opt-out-exp10 branch from c35ac3c to 465db65 Compare July 15, 2025 07:20
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/use-tablegen-default-handling branch from ef419d6 to f5462a6 Compare July 15, 2025 07:20
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/stop-opt-out-exp10 branch from 465db65 to 9c031f5 Compare July 28, 2025 02:43
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/use-tablegen-default-handling branch from f5462a6 to 5843fba Compare July 28, 2025 02:43
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/stop-opt-out-exp10 branch from 9c031f5 to b1c46d2 Compare August 3, 2025 00:41
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/use-tablegen-default-handling branch from 5843fba to a8d3505 Compare August 3, 2025 00:41
Base automatically changed from users/arsenm/runtime-libcalls/stop-opt-out-exp10 to main August 3, 2025 15:08
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Is dropping sincos on AVR here intentional?

@arsenm
Copy link
Contributor Author

arsenm commented Aug 3, 2025

Is dropping sincos on AVR here intentional?

Yes, I had a PR to add baseline tests and apparently there's no Linux or gnu system to worry about

@arsenm
Copy link
Contributor Author

arsenm commented Aug 3, 2025

AVR also has the strange reuse of the f64 names for the f32 sin and cos

Hack in the default setting so it's consistently generated like
the other cases. Maintain a list of targets where this applies.
The alternative would require new infrastructure to sort the system
library initialization in some way.

I wanted the unhandled target case to be treated as a fatal
error, but it turns out there's a hack in IRSymtab using
RuntimeLibcalls, which will fail out in many tests that
do not have a triple set. Many of the failures are simply
running llvm-as with no triple, which probably should not
depend on knowing an accurate set of calls.
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/use-tablegen-default-handling branch from a8d3505 to 744161e Compare August 3, 2025 16:32
@tgross35
Copy link
Contributor

tgross35 commented Aug 3, 2025

AVR also has the strange reuse of the f64 names for the f32 sin and cos

I haven’t looked but would guess this platform and MSP have pretty much the same issue as f128 on 32-bit/x86, since float and double are the same size. If there was a way to indicate that a C23 math library was available (from the other PR threads), some of this could be resolved by always emitting sqrtf32 and sqrtf64 for binary32 and binary64 float.

Not that many people are using f64/f128 on AVR, some of the math routines are the entire flash space…

@arsenm
Copy link
Contributor Author

arsenm commented Aug 3, 2025

AVR also has the strange reuse of the f64 names for the f32 sin and cos

I haven’t looked but would guess this platform and MSP have pretty much the same issue as f128 on 32-bit/x86, since float and double are the same size.

I'm not really sure what the issue is. The implementation is very underdeveloped if it's that broad. The starting state only bothered to handle sin and cos

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM. Looks like the behavior changes on AVR, MSP430 and XCore are intentional.

@arsenm arsenm merged commit b2f0ffd into main Aug 3, 2025
9 checks passed
@arsenm arsenm deleted the users/arsenm/runtime-libcalls/use-tablegen-default-handling branch August 3, 2025 23:32
@tgross35
Copy link
Contributor

tgross35 commented Aug 4, 2025

I'm not really sure what the issue is. The implementation is very underdeveloped if it's that broad. The starting state only bothered to handle sin and cos

There is no issue in the PR here, this looks fine. The problem is when trying to use LLVM's double which AIUI is always f64, but platform double on AVR is f32. So llvm.*.f64 calls symbol sqrt with a f64, but that's a f32 symbol on the platform https://llvm.godbolt.org/z/f6sdP4MxT. (Basically the same problem as with f128 using long double symbols). The only way to resolve it is to lower LLVM's double math to C23 sqrtf64 rather than sqrt on AVR.

Underdeveloped is accurate here, it's not relevant for a whole lot of users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants