https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/153209
>From 93d86a580fdbb156bc1de4f97da63abf549ce671 Mon Sep 17 00:00:00 2001 From: Matt Arsenault <matthew.arsena...@amd.com> Date: Tue, 12 Aug 2025 20:50:21 +0900 Subject: [PATCH] RuntimeLibcalls: Return StringRef for libcall names Does not yet fully propagate this down into the TargetLowering uses, many of which are relying on null checks on the returned value. --- llvm/benchmarks/RuntimeLibcalls.cpp | 6 ++---- llvm/include/llvm/CodeGen/TargetLowering.h | 12 ++++++++---- llvm/include/llvm/IR/RuntimeLibcalls.h | 17 ++++++++--------- llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp | 2 +- llvm/lib/LTO/LTO.cpp | 2 +- .../WebAssemblyRuntimeLibcallSignatures.cpp | 4 ++-- .../Transforms/Utils/DeclareRuntimeLibcalls.cpp | 2 +- llvm/unittests/IR/RuntimeLibcallsTest.cpp | 2 +- 8 files changed, 24 insertions(+), 23 deletions(-) diff --git a/llvm/benchmarks/RuntimeLibcalls.cpp b/llvm/benchmarks/RuntimeLibcalls.cpp index 47f68abff1e0d..81a5a24ec8f93 100644 --- a/llvm/benchmarks/RuntimeLibcalls.cpp +++ b/llvm/benchmarks/RuntimeLibcalls.cpp @@ -22,10 +22,8 @@ static constexpr unsigned MaxFuncNameSize = 53; static std::vector<StringRef> getLibcallNameStringRefs() { std::vector<StringRef> Names(RTLIB::NumLibcallImpls); // Keep the strlens on the StringRef construction out of the benchmark loop. - for (RTLIB::LibcallImpl LC : RTLIB::libcall_impls()) { - const char *Name = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LC); - Names[LC] = StringRef(Name); - } + for (RTLIB::LibcallImpl LC : RTLIB::libcall_impls()) + Names[LC] = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LC); return Names; } diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h index 20e4dfaaff6e1..ed7495694cc70 100644 --- a/llvm/include/llvm/CodeGen/TargetLowering.h +++ b/llvm/include/llvm/CodeGen/TargetLowering.h @@ -3552,15 +3552,19 @@ class LLVM_ABI TargetLoweringBase { /// Get the libcall routine name for the specified libcall. const char *getLibcallName(RTLIB::Libcall Call) const { - return Libcalls.getLibcallName(Call); + // FIXME: Return StringRef + return Libcalls.getLibcallName(Call).data(); } /// Get the libcall routine name for the specified libcall implementation - const char *getLibcallImplName(RTLIB::LibcallImpl Call) const { - return Libcalls.getLibcallImplName(Call); + static StringRef getLibcallImplName(RTLIB::LibcallImpl Call) { + return RTLIB::RuntimeLibcallsInfo::getLibcallImplName(Call); } - const char *getMemcpyName() const { return Libcalls.getMemcpyName(); } + const char *getMemcpyName() const { + // FIXME: Return StringRef + return Libcalls.getMemcpyName().data(); + } /// Get the comparison predicate that's to be used to test the result of the /// comparison libcall against zero. This should only be used with diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.h b/llvm/include/llvm/IR/RuntimeLibcalls.h index 078098eeb7148..620774fd296e3 100644 --- a/llvm/include/llvm/IR/RuntimeLibcalls.h +++ b/llvm/include/llvm/IR/RuntimeLibcalls.h @@ -77,17 +77,15 @@ struct RuntimeLibcallsInfo { /// Get the libcall routine name for the specified libcall. // FIXME: This should be removed. Only LibcallImpl should have a name. - const char *getLibcallName(RTLIB::Libcall Call) const { + StringRef getLibcallName(RTLIB::Libcall Call) const { return getLibcallImplName(LibcallImpls[Call]); } /// Get the libcall routine name for the specified libcall implementation. - // FIXME: Change to return StringRef - static const char *getLibcallImplName(RTLIB::LibcallImpl CallImpl) { + static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) { if (CallImpl == RTLIB::Unsupported) - return nullptr; - return RuntimeLibcallImplNameTable[RuntimeLibcallNameOffsetTable[CallImpl]] - .data(); + return StringRef(); + return RuntimeLibcallImplNameTable[RuntimeLibcallNameOffsetTable[CallImpl]]; } /// Return the lowering's selection of implementation call for \p Call @@ -119,9 +117,10 @@ struct RuntimeLibcallsInfo { /// Return a function name compatible with RTLIB::MEMCPY, or nullptr if fully /// unsupported. - const char *getMemcpyName() const { - if (const char *Memcpy = getLibcallName(RTLIB::MEMCPY)) - return Memcpy; + StringRef getMemcpyName() const { + RTLIB::LibcallImpl Memcpy = getLibcallImpl(RTLIB::MEMCPY); + if (Memcpy != RTLIB::Unsupported) + return getLibcallImplName(Memcpy); // Fallback to memmove if memcpy isn't available. return getLibcallName(RTLIB::MEMMOVE); diff --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp index 9fa96e7372961..96c9cde622b45 100644 --- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp +++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp @@ -145,7 +145,7 @@ static bool lowerObjCCall(Function &F, RTLIB::LibcallImpl NewFn, // FIXME: When RuntimeLibcalls is an analysis, check if the function is really // supported, and go through RTLIB::Libcall. - const char *NewFnName = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(NewFn); + StringRef NewFnName = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(NewFn); // If we haven't already looked up this function, check to see if the // program already contains a function with this name. diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 0323b4d433b87..35d24c17bbd93 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -1422,7 +1422,7 @@ SmallVector<const char *> LTO::getRuntimeLibcallSymbols(const Triple &TT) { for (RTLIB::LibcallImpl Impl : LibcallImpls) { if (Impl != RTLIB::Unsupported) - LibcallSymbols.push_back(Libcalls.getLibcallImplName(Impl)); + LibcallSymbols.push_back(Libcalls.getLibcallImplName(Impl).data()); } return LibcallSymbols; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp index 4548a7520b3b4..45b0e7dc12263 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp @@ -533,8 +533,8 @@ struct StaticLibcallNameMap { // different libcalls. RTLIB::RuntimeLibcallsInfo RTCI(TT); for (RTLIB::Libcall LC : RTLIB::libcalls()) { - const char *NameLibcall = RTCI.getLibcallName(LC); - if (NameLibcall != nullptr && + StringRef NameLibcall = RTCI.getLibcallName(LC); + if (!NameLibcall.empty() && getRuntimeLibcallSignatures().Table[LC] != unsupported) { assert(!Map.contains(NameLibcall) && "duplicate libcall names in name map"); diff --git a/llvm/lib/Transforms/Utils/DeclareRuntimeLibcalls.cpp b/llvm/lib/Transforms/Utils/DeclareRuntimeLibcalls.cpp index 540039b7d2cbd..0642d51cd2c21 100644 --- a/llvm/lib/Transforms/Utils/DeclareRuntimeLibcalls.cpp +++ b/llvm/lib/Transforms/Utils/DeclareRuntimeLibcalls.cpp @@ -30,7 +30,7 @@ PreservedAnalyses DeclareRuntimeLibcallsPass::run(Module &M, FunctionType *FuncTy = FunctionType::get(Type::getVoidTy(Ctx), {}, /*IsVarArgs=*/true); - const char *FuncName = RTLCI.getLibcallImplName(Impl); + StringRef FuncName = RTLCI.getLibcallImplName(Impl); M.getOrInsertFunction(FuncName, FuncTy); } diff --git a/llvm/unittests/IR/RuntimeLibcallsTest.cpp b/llvm/unittests/IR/RuntimeLibcallsTest.cpp index 94ed56e92bd55..012316801859c 100644 --- a/llvm/unittests/IR/RuntimeLibcallsTest.cpp +++ b/llvm/unittests/IR/RuntimeLibcallsTest.cpp @@ -23,7 +23,7 @@ TEST(RuntimeLibcallsTest, LibcallImplByName) { RTLIB::RuntimeLibcallsInfo::lookupLibcallImplName("unsupported").empty()); for (RTLIB::LibcallImpl LC : RTLIB::libcall_impls()) { - const char *Name = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LC); + StringRef Name = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LC); EXPECT_TRUE(is_contained( RTLIB::RuntimeLibcallsInfo::lookupLibcallImplName(Name), LC)); } _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits