Skip to content

Commit f7a8d20

Browse files
authored
DeclareRuntimeLibcalls: Use RuntimeLibraryAnalysis (#167995)
Also add boilerplate to have a live instance when running opt configured from CommandFlags / TargetOptions.
1 parent 07740fb commit f7a8d20

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

llvm/include/llvm/Analysis/RuntimeLibcallInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class LLVM_ABI RuntimeLibraryAnalysis
3131
friend AnalysisInfoMixin<RuntimeLibraryAnalysis>;
3232
LLVM_ABI static AnalysisKey Key;
3333

34-
RTLIB::RuntimeLibcallsInfo LibcallsInfo;
34+
std::optional<RTLIB::RuntimeLibcallsInfo> LibcallsInfo;
3535
};
3636

3737
class LLVM_ABI RuntimeLibraryInfoWrapper : public ImmutablePass {

llvm/lib/Analysis/RuntimeLibcallInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ AnalysisKey RuntimeLibraryAnalysis::Key;
1515

1616
RTLIB::RuntimeLibcallsInfo
1717
RuntimeLibraryAnalysis::run(const Module &M, ModuleAnalysisManager &) {
18-
return RTLIB::RuntimeLibcallsInfo(M);
18+
if (!LibcallsInfo)
19+
LibcallsInfo = RTLIB::RuntimeLibcallsInfo(M);
20+
return *LibcallsInfo;
1921
}
2022

2123
INITIALIZE_PASS(RuntimeLibraryInfoWrapper, "runtime-library-info",

llvm/lib/Transforms/Utils/DeclareRuntimeLibcalls.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Transforms/Utils/DeclareRuntimeLibcalls.h"
14+
#include "llvm/Analysis/RuntimeLibcallInfo.h"
1415
#include "llvm/IR/Module.h"
1516
#include "llvm/IR/RuntimeLibcalls.h"
1617

@@ -49,7 +50,9 @@ static void mergeAttributes(LLVMContext &Ctx, const Module &M,
4950

5051
PreservedAnalyses DeclareRuntimeLibcallsPass::run(Module &M,
5152
ModuleAnalysisManager &MAM) {
52-
RTLIB::RuntimeLibcallsInfo RTLCI(M.getTargetTriple());
53+
const RTLIB::RuntimeLibcallsInfo &RTLCI =
54+
MAM.getResult<RuntimeLibraryAnalysis>(M);
55+
5356
LLVMContext &Ctx = M.getContext();
5457
const DataLayout &DL = M.getDataLayout();
5558
const Triple &TT = M.getTargetTriple();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; REQUIRES: arm-registered-target
2+
3+
; Make sure that codegen flags work to change the set of libcalls
4+
; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=arm-none-linux-gnueabi -float-abi=hard -exception-model=sjlj -meabi=4 < %s | FileCheck %s
5+
6+
; Depends on -exception-model
7+
; CHECK: declare arm_aapcs_vfpcc void @_Unwind_SjLj_Register(...)
8+
; CHECK: declare arm_aapcs_vfpcc void @_Unwind_SjLj_Resume(...)
9+
; CHECK: declare arm_aapcs_vfpcc void @_Unwind_SjLj_Unregister(...)
10+
11+
; Calling convention depends on -float-abi
12+
; CHECK: declare arm_aapcs_vfpcc void @__addtf3(...)
13+
14+
; memclr functions depend on -meabi
15+
; CHECK: declare arm_aapcscc void @__aeabi_memclr(...)
16+
; CHECK: declare arm_aapcscc void @__aeabi_memclr4(...)
17+
; CHECK: declare arm_aapcscc void @__aeabi_memclr8(...)

llvm/tools/opt/NewPMDriver.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/StringRef.h"
1919
#include "llvm/Analysis/AliasAnalysis.h"
2020
#include "llvm/Analysis/CGSCCPassManager.h"
21+
#include "llvm/Analysis/RuntimeLibcallInfo.h"
2122
#include "llvm/Analysis/TargetLibraryInfo.h"
2223
#include "llvm/Bitcode/BitcodeWriterPass.h"
2324
#include "llvm/Config/llvm-config.h"
@@ -351,9 +352,9 @@ static void registerEPCallbacks(PassBuilder &PB) {
351352

352353
bool llvm::runPassPipeline(
353354
StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
354-
ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
355-
ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
356-
ArrayRef<PassPlugin> PassPlugins,
355+
RTLIB::RuntimeLibcallsInfo &RTLCI, ToolOutputFile *Out,
356+
ToolOutputFile *ThinLTOLinkOut, ToolOutputFile *OptRemarkFile,
357+
StringRef PassPipeline, ArrayRef<PassPlugin> PassPlugins,
357358
ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks,
358359
OutputKind OK, VerifierKind VK, bool ShouldPreserveAssemblyUseListOrder,
359360
bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
@@ -416,6 +417,7 @@ bool llvm::runPassPipeline(
416417
FunctionAnalysisManager FAM;
417418
CGSCCAnalysisManager CGAM;
418419
ModuleAnalysisManager MAM;
420+
MAM.registerPass([&] { return RuntimeLibraryAnalysis(std::move(RTLCI)); });
419421

420422
PassInstrumentationCallbacks PIC;
421423
PrintPassOptions PrintPassOpts;

llvm/tools/opt/NewPMDriver.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class TargetMachine;
3131
class ToolOutputFile;
3232
class TargetLibraryInfoImpl;
3333

34+
namespace RTLIB {
35+
struct RuntimeLibcallsInfo;
36+
}
37+
3438
extern cl::opt<bool> DebugifyEach;
3539
extern cl::opt<std::string> DebugifyExport;
3640

@@ -67,9 +71,9 @@ void printPasses(raw_ostream &OS);
6771
/// nullptr.
6872
bool runPassPipeline(
6973
StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
70-
ToolOutputFile *Out, ToolOutputFile *ThinLinkOut,
71-
ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
72-
ArrayRef<PassPlugin> PassPlugins,
74+
RTLIB::RuntimeLibcallsInfo &RTLCI, ToolOutputFile *Out,
75+
ToolOutputFile *ThinLinkOut, ToolOutputFile *OptRemarkFile,
76+
StringRef PassPipeline, ArrayRef<PassPlugin> PassPlugins,
7377
ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks,
7478
opt_tool::OutputKind OK, opt_tool::VerifierKind VK,
7579
bool ShouldPreserveAssemblyUseListOrder,

llvm/tools/opt/optdriver.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Analysis/CallGraphSCCPass.h"
1818
#include "llvm/Analysis/LoopPass.h"
1919
#include "llvm/Analysis/RegionPass.h"
20+
#include "llvm/Analysis/RuntimeLibcallInfo.h"
2021
#include "llvm/Analysis/TargetLibraryInfo.h"
2122
#include "llvm/Analysis/TargetTransformInfo.h"
2223
#include "llvm/AsmParser/Parser.h"
@@ -672,6 +673,11 @@ optMain(int argc, char **argv,
672673
// Add an appropriate TargetLibraryInfo pass for the module's triple.
673674
TargetLibraryInfoImpl TLII(ModuleTriple);
674675

676+
// FIXME: Get ABI name from MCOptions
677+
RTLIB::RuntimeLibcallsInfo RTLCI(ModuleTriple, codegen::getExceptionModel(),
678+
codegen::getFloatABIForCalls(),
679+
codegen::getEABIVersion());
680+
675681
// The -disable-simplify-libcalls flag actually disables all builtin optzns.
676682
if (DisableSimplifyLibCalls)
677683
TLII.disableAllFunctions();
@@ -746,7 +752,7 @@ optMain(int argc, char **argv,
746752
// string. Hand off the rest of the functionality to the new code for that
747753
// layer.
748754
if (!runPassPipeline(
749-
argv[0], *M, TM.get(), &TLII, Out.get(), ThinLinkOut.get(),
755+
argv[0], *M, TM.get(), &TLII, RTLCI, Out.get(), ThinLinkOut.get(),
750756
RemarksFile.get(), Pipeline, PluginList, PassBuilderCallbacks, OK,
751757
VK, /* ShouldPreserveAssemblyUseListOrder */ false,
752758
/* ShouldPreserveBitcodeUseListOrder */ true, EmitSummaryIndex,

0 commit comments

Comments
 (0)