Skip to content

Commit 833269e

Browse files
committed
Analysis: Add RuntimeLibcall analysis pass
Currently RuntimeLibcallsInfo is a hardcoded list based on the triple. In the future the available libcall set should be dynamically modifiable with module flags. Note this isn't really used yet. TargetLowering is still constructing its own copy, and untangling that to use this requires several more steps.
1 parent dd74fdc commit 833269e

File tree

12 files changed

+129
-1
lines changed

12 files changed

+129
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===-- RuntimeLibcallInfo.h - Runtime library information ------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_ANALYSIS_RUNTIMELIBCALLINFO_H
10+
#define LLVM_ANALYSIS_RUNTIMELIBCALLINFO_H
11+
12+
#include "llvm/IR/RuntimeLibcalls.h"
13+
#include "llvm/Pass.h"
14+
15+
namespace llvm {
16+
17+
class LLVM_ABI RuntimeLibraryAnalysis
18+
: public AnalysisInfoMixin<RuntimeLibraryAnalysis> {
19+
public:
20+
using Result = RTLIB::RuntimeLibcallsInfo;
21+
22+
RuntimeLibraryAnalysis() = default;
23+
RuntimeLibraryAnalysis(RTLIB::RuntimeLibcallsInfo &&BaselineInfoImpl)
24+
: LibcallsInfo(std::move(BaselineInfoImpl)) {}
25+
explicit RuntimeLibraryAnalysis(const Triple &T) : LibcallsInfo(T) {}
26+
27+
LLVM_ABI RTLIB::RuntimeLibcallsInfo run(const Module &M,
28+
ModuleAnalysisManager &);
29+
30+
private:
31+
friend AnalysisInfoMixin<RuntimeLibraryAnalysis>;
32+
LLVM_ABI static AnalysisKey Key;
33+
34+
RTLIB::RuntimeLibcallsInfo LibcallsInfo;
35+
};
36+
37+
class LLVM_ABI RuntimeLibraryInfoWrapper : public ImmutablePass {
38+
RuntimeLibraryAnalysis RTLA;
39+
std::optional<RTLIB::RuntimeLibcallsInfo> RTLCI;
40+
41+
public:
42+
static char ID;
43+
RuntimeLibraryInfoWrapper();
44+
explicit RuntimeLibraryInfoWrapper(const Triple &T);
45+
explicit RuntimeLibraryInfoWrapper(const RTLIB::RuntimeLibcallsInfo &RTLCI);
46+
47+
const RTLIB::RuntimeLibcallsInfo &getRTLCI(const Module &M) {
48+
ModuleAnalysisManager DummyMAM;
49+
RTLCI = RTLA.run(M, DummyMAM);
50+
return *RTLCI;
51+
}
52+
53+
void getAnalysisUsage(AnalysisUsage &AU) const override;
54+
};
55+
56+
LLVM_ABI ModulePass *createRuntimeLibraryInfoWrapperPass();
57+
58+
} // namespace llvm
59+
60+
#endif

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SelectionDAGISel {
4646
public:
4747
TargetMachine &TM;
4848
const TargetLibraryInfo *LibInfo;
49+
const RTLIB::RuntimeLibcallsInfo *RuntimeLibCallInfo;
4950
std::unique_ptr<FunctionLoweringInfo> FuncInfo;
5051
std::unique_ptr<SwiftErrorValueTracking> SwiftError;
5152
MachineFunction *MF;

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// This file implements a common interface to work with library calls into a
1010
// runtime that may be emitted by a given backend.
1111
//
12+
// FIXME: This should probably move to Analysis
13+
//
1214
//===----------------------------------------------------------------------===//
1315

1416
#ifndef LLVM_IR_RUNTIME_LIBCALLS_H
@@ -20,6 +22,7 @@
2022
#include "llvm/ADT/StringTable.h"
2123
#include "llvm/IR/CallingConv.h"
2224
#include "llvm/IR/InstrTypes.h"
25+
#include "llvm/IR/PassManager.h"
2326
#include "llvm/Support/AtomicOrdering.h"
2427
#include "llvm/Support/CodeGen.h"
2528
#include "llvm/Support/Compiler.h"
@@ -74,6 +77,8 @@ struct RuntimeLibcallsInfo {
7477
public:
7578
friend class llvm::LibcallLoweringInfo;
7679

80+
RuntimeLibcallsInfo() = default;
81+
7782
explicit RuntimeLibcallsInfo(
7883
const Triple &TT,
7984
ExceptionHandling ExceptionModel = ExceptionHandling::None,
@@ -89,6 +94,11 @@ struct RuntimeLibcallsInfo {
8994
initLibcalls(TT, ExceptionModel, FloatABI, EABIVersion, ABIName);
9095
}
9196

97+
explicit RuntimeLibcallsInfo(const Module &M);
98+
99+
bool invalidate(Module &M, const PreservedAnalyses &PA,
100+
ModuleAnalysisManager::Invalidator &);
101+
92102
/// Get the libcall routine name for the specified libcall implementation.
93103
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
94104
if (CallImpl == RTLIB::Unsupported)

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ LLVM_ABI void initializeRemoveRedundantDebugValuesLegacyPass(PassRegistry &);
290290
LLVM_ABI void initializeRenameIndependentSubregsLegacyPass(PassRegistry &);
291291
LLVM_ABI void initializeReplaceWithVeclibLegacyPass(PassRegistry &);
292292
LLVM_ABI void initializeResetMachineFunctionPass(PassRegistry &);
293+
LLVM_ABI void initializeRuntimeLibraryInfoWrapperPass(PassRegistry &);
293294
LLVM_ABI void initializeSCEVAAWrapperPassPass(PassRegistry &);
294295
LLVM_ABI void initializeSROALegacyPassPass(PassRegistry &);
295296
LLVM_ABI void initializeSafeStackLegacyPassPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Analysis/BasicAliasAnalysis.h"
2121
#include "llvm/Analysis/CGSCCPassManager.h"
2222
#include "llvm/Analysis/ProfileSummaryInfo.h"
23+
#include "llvm/Analysis/RuntimeLibcallInfo.h"
2324
#include "llvm/Analysis/ScopedNoAliasAA.h"
2425
#include "llvm/Analysis/TargetTransformInfo.h"
2526
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
@@ -638,6 +639,8 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
638639
/*Force=*/true);
639640
addIRPass(RequireAnalysisPass<CollectorMetadataAnalysis, Module>(),
640641
/*Force=*/true);
642+
addIRPass(RequireAnalysisPass<RuntimeLibraryAnalysis, Module>(),
643+
/*Force=*/true);
641644
addISelPasses(addIRPass);
642645
}
643646

llvm/lib/Analysis/Analysis.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void llvm::initializeAnalysis(PassRegistry &Registry) {
6363
initializeRegionPrinterPass(Registry);
6464
initializeRegionOnlyViewerPass(Registry);
6565
initializeRegionOnlyPrinterPass(Registry);
66+
initializeRuntimeLibraryInfoWrapperPass(Registry);
6667
initializeSCEVAAWrapperPassPass(Registry);
6768
initializeScalarEvolutionWrapperPassPass(Registry);
6869
initializeStackSafetyGlobalInfoWrapperPassPass(Registry);

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ add_llvm_component_library(LLVMAnalysis
137137
RegionPass.cpp
138138
RegionPrinter.cpp
139139
ReplayInlineAdvisor.cpp
140+
RuntimeLibcallInfo.cpp
140141
ScalarEvolution.cpp
141142
ScalarEvolutionAliasAnalysis.cpp
142143
ScalarEvolutionDivision.cpp
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===- RuntimeLibcallInfo.cpp ---------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Analysis/RuntimeLibcallInfo.h"
10+
#include "llvm/InitializePasses.h"
11+
12+
using namespace llvm;
13+
14+
AnalysisKey RuntimeLibraryAnalysis::Key;
15+
16+
RTLIB::RuntimeLibcallsInfo
17+
RuntimeLibraryAnalysis::run(const Module &M, ModuleAnalysisManager &) {
18+
return RTLIB::RuntimeLibcallsInfo(M);
19+
}
20+
21+
INITIALIZE_PASS(RuntimeLibraryInfoWrapper, "runtime-library-info",
22+
"Runtime Library Function Analysis", false, true)
23+
24+
RuntimeLibraryInfoWrapper::RuntimeLibraryInfoWrapper()
25+
: ImmutablePass(ID), RTLA(RTLIB::RuntimeLibcallsInfo(Triple())) {}
26+
27+
char RuntimeLibraryInfoWrapper::ID = 0;
28+
29+
ModulePass *llvm::createRuntimeLibraryInfoWrapperPass() {
30+
return new RuntimeLibraryInfoWrapper();
31+
}
32+
33+
void RuntimeLibraryInfoWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
34+
AU.setPreservesAll();
35+
}
36+
37+
// Assume this is stable unless explicitly invalidated.
38+
bool RTLIB::RuntimeLibcallsInfo::invalidate(
39+
Module &M, const PreservedAnalyses &PA,
40+
ModuleAnalysisManager::Invalidator &) {
41+
auto PAC = PA.getChecker<RuntimeLibraryAnalysis>();
42+
return !PAC.preservedWhenStateless();
43+
}

llvm/lib/IR/RuntimeLibcalls.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "llvm/IR/RuntimeLibcalls.h"
1010
#include "llvm/ADT/FloatingPointMode.h"
1111
#include "llvm/ADT/StringTable.h"
12-
#include "llvm/IR/DataLayout.h"
12+
#include "llvm/IR/Module.h"
1313
#include "llvm/Support/Debug.h"
1414
#include "llvm/Support/xxhash.h"
1515
#include "llvm/TargetParser/ARMTargetParser.h"
@@ -25,6 +25,11 @@ using namespace RTLIB;
2525
#define DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
2626
#include "llvm/IR/RuntimeLibcalls.inc"
2727

28+
RuntimeLibcallsInfo::RuntimeLibcallsInfo(const Module &M)
29+
: RuntimeLibcallsInfo(M.getTargetTriple()) {
30+
// TODO: Consider module flags
31+
}
32+
2833
/// Set default libcall names. If a target wants to opt-out of a libcall it
2934
/// should be placed here.
3035
void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "llvm/Analysis/PostDominators.h"
6868
#include "llvm/Analysis/ProfileSummaryInfo.h"
6969
#include "llvm/Analysis/RegionInfo.h"
70+
#include "llvm/Analysis/RuntimeLibcallInfo.h"
7071
#include "llvm/Analysis/ScalarEvolution.h"
7172
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
7273
#include "llvm/Analysis/ScalarEvolutionDivision.h"

0 commit comments

Comments
 (0)