Skip to content

Commit 7b54976

Browse files
authored
[CodeGen][NewPM] Port RegisterUsageInfo to NPM (#113873)
And add to the codegen pipeline if ipra is enabled with a `RequireAnalysisPass` since this is a module pass.
1 parent 4e60075 commit 7b54976

File tree

9 files changed

+98
-22
lines changed

9 files changed

+98
-22
lines changed

llvm/include/llvm/CodeGen/RegisterUsageInfo.h

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "llvm/ADT/ArrayRef.h"
2222
#include "llvm/ADT/DenseMap.h"
23+
#include "llvm/IR/PassManager.h"
2324
#include "llvm/InitializePasses.h"
2425
#include "llvm/Pass.h"
2526
#include "llvm/PassRegistry.h"
@@ -31,21 +32,14 @@ namespace llvm {
3132
class Function;
3233
class TargetMachine;
3334

34-
class PhysicalRegisterUsageInfo : public ImmutablePass {
35+
class PhysicalRegisterUsageInfo {
3536
public:
36-
static char ID;
37-
38-
PhysicalRegisterUsageInfo() : ImmutablePass(ID) {
39-
PassRegistry &Registry = *PassRegistry::getPassRegistry();
40-
initializePhysicalRegisterUsageInfoPass(Registry);
41-
}
42-
4337
/// Set TargetMachine which is used to print analysis.
4438
void setTargetMachine(const TargetMachine &TM);
4539

46-
bool doInitialization(Module &M) override;
40+
bool doInitialization(Module &M);
4741

48-
bool doFinalization(Module &M) override;
42+
bool doFinalization(Module &M);
4943

5044
/// To store RegMask for given Function *.
5145
void storeUpdateRegUsageInfo(const Function &FP,
@@ -55,7 +49,10 @@ class PhysicalRegisterUsageInfo : public ImmutablePass {
5549
/// array if function is not known.
5650
ArrayRef<uint32_t> getRegUsageInfo(const Function &FP);
5751

58-
void print(raw_ostream &OS, const Module *M = nullptr) const override;
52+
void print(raw_ostream &OS, const Module *M = nullptr) const;
53+
54+
bool invalidate(Module &M, const PreservedAnalyses &PA,
55+
ModuleAnalysisManager::Invalidator &Inv);
5956

6057
private:
6158
/// A Dense map from Function * to RegMask.
@@ -66,6 +63,52 @@ class PhysicalRegisterUsageInfo : public ImmutablePass {
6663
const TargetMachine *TM = nullptr;
6764
};
6865

66+
class PhysicalRegisterUsageInfoWrapperLegacy : public ImmutablePass {
67+
std::unique_ptr<PhysicalRegisterUsageInfo> PRUI;
68+
69+
public:
70+
static char ID;
71+
PhysicalRegisterUsageInfoWrapperLegacy() : ImmutablePass(ID) {
72+
initializePhysicalRegisterUsageInfoWrapperLegacyPass(
73+
*PassRegistry::getPassRegistry());
74+
}
75+
76+
PhysicalRegisterUsageInfo &getPRUI() { return *PRUI; }
77+
const PhysicalRegisterUsageInfo &getPRUI() const { return *PRUI; }
78+
79+
bool doInitialization(Module &M) override {
80+
PRUI.reset(new PhysicalRegisterUsageInfo());
81+
return PRUI->doInitialization(M);
82+
}
83+
84+
bool doFinalization(Module &M) override { return PRUI->doFinalization(M); }
85+
86+
void print(raw_ostream &OS, const Module *M = nullptr) const override {
87+
PRUI->print(OS, M);
88+
}
89+
};
90+
91+
class PhysicalRegisterUsageAnalysis
92+
: public AnalysisInfoMixin<PhysicalRegisterUsageAnalysis> {
93+
friend AnalysisInfoMixin<PhysicalRegisterUsageAnalysis>;
94+
static AnalysisKey Key;
95+
96+
public:
97+
using Result = PhysicalRegisterUsageInfo;
98+
99+
PhysicalRegisterUsageInfo run(Module &M, ModuleAnalysisManager &);
100+
};
101+
102+
class PhysicalRegisterUsageInfoPrinterPass
103+
: public PassInfoMixin<PhysicalRegisterUsageInfoPrinterPass> {
104+
raw_ostream &OS;
105+
106+
public:
107+
explicit PhysicalRegisterUsageInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
108+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
109+
static bool isRequired() { return true; }
110+
};
111+
69112
} // end namespace llvm
70113

71114
#endif // LLVM_CODEGEN_REGISTERUSAGEINFO_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void initializePartiallyInlineLibCallsLegacyPassPass(PassRegistry &);
229229
void initializePatchableFunctionPass(PassRegistry &);
230230
void initializePeepholeOptimizerPass(PassRegistry &);
231231
void initializePhiValuesWrapperPassPass(PassRegistry &);
232-
void initializePhysicalRegisterUsageInfoPass(PassRegistry &);
232+
void initializePhysicalRegisterUsageInfoWrapperLegacyPass(PassRegistry &);
233233
void initializePlaceBackedgeSafepointsLegacyPassPass(PassRegistry &);
234234
void initializePostDomOnlyPrinterWrapperPassPass(PassRegistry &);
235235
void initializePostDomOnlyViewerWrapperPassPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "llvm/CodeGen/PHIElimination.h"
5555
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
5656
#include "llvm/CodeGen/RegAllocFast.h"
57+
#include "llvm/CodeGen/RegisterUsageInfo.h"
5758
#include "llvm/CodeGen/ReplaceWithVeclib.h"
5859
#include "llvm/CodeGen/SafeStack.h"
5960
#include "llvm/CodeGen/SelectOptimize.h"
@@ -906,9 +907,10 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
906907
addPass(LocalStackSlotAllocationPass());
907908
}
908909

909-
if (TM.Options.EnableIPRA)
910+
if (TM.Options.EnableIPRA) {
911+
addPass(RequireAnalysisPass<PhysicalRegisterUsageAnalysis, Module>());
910912
addPass(RegUsageInfoPropagationPass());
911-
913+
}
912914
// Run pre-ra passes.
913915
derived().addPreRegAlloc(addPass);
914916

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ MODULE_PASS("global-merge", GlobalMergePass(TM, GlobalMergeOptions()))
2828
MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass())
2929
MODULE_PASS("lower-emutls", LowerEmuTLSPass())
3030
MODULE_PASS("pre-isel-intrinsic-lowering", PreISelIntrinsicLoweringPass())
31+
MODULE_PASS("print<regusage>", PhysicalRegisterUsageInfoPrinterPass(dbgs()))
3132
MODULE_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass())
3233
MODULE_PASS("global-merge-func", GlobalMergeFuncPass())
3334
#undef MODULE_PASS

llvm/lib/CodeGen/RegUsageInfoCollector.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class RegUsageInfoCollector : public MachineFunctionPass {
4848
}
4949

5050
void getAnalysisUsage(AnalysisUsage &AU) const override {
51-
AU.addRequired<PhysicalRegisterUsageInfo>();
51+
AU.addRequired<PhysicalRegisterUsageInfoWrapperLegacy>();
5252
AU.setPreservesAll();
5353
MachineFunctionPass::getAnalysisUsage(AU);
5454
}
@@ -68,7 +68,7 @@ char RegUsageInfoCollector::ID = 0;
6868

6969
INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
7070
"Register Usage Information Collector", false, false)
71-
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
71+
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfoWrapperLegacy)
7272
INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
7373
"Register Usage Information Collector", false, false)
7474

@@ -129,7 +129,8 @@ bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
129129

130130
const Function &F = MF.getFunction();
131131

132-
PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
132+
PhysicalRegisterUsageInfo &PRUI =
133+
getAnalysis<PhysicalRegisterUsageInfoWrapperLegacy>().getPRUI();
133134
PRUI.setTargetMachine(TM);
134135

135136
LLVM_DEBUG(dbgs() << "Clobbered Registers: ");

llvm/lib/CodeGen/RegUsageInfoPropagate.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RegUsageInfoPropagation : public MachineFunctionPass {
5050
bool runOnMachineFunction(MachineFunction &MF) override;
5151

5252
void getAnalysisUsage(AnalysisUsage &AU) const override {
53-
AU.addRequired<PhysicalRegisterUsageInfo>();
53+
AU.addRequired<PhysicalRegisterUsageInfoWrapperLegacy>();
5454
AU.setPreservesAll();
5555
MachineFunctionPass::getAnalysisUsage(AU);
5656
}
@@ -75,7 +75,7 @@ class RegUsageInfoPropagation : public MachineFunctionPass {
7575

7676
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
7777
RUIP_NAME, false, false)
78-
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
78+
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfoWrapperLegacy)
7979
INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
8080
RUIP_NAME, false, false)
8181

@@ -97,7 +97,8 @@ static const Function *findCalledFunction(const Module &M,
9797

9898
bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
9999
const Module &M = *MF.getFunction().getParent();
100-
PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
100+
PhysicalRegisterUsageInfo *PRUI =
101+
&getAnalysis<PhysicalRegisterUsageInfoWrapperLegacy>().getPRUI();
101102

102103
LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
103104
<< " ++++++++++++++++++++ \n");

llvm/lib/CodeGen/RegisterUsageInfo.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#include "llvm/CodeGen/MachineOperand.h"
1717
#include "llvm/CodeGen/TargetRegisterInfo.h"
1818
#include "llvm/CodeGen/TargetSubtargetInfo.h"
19+
#include "llvm/IR/Analysis.h"
1920
#include "llvm/IR/Function.h"
2021
#include "llvm/IR/Module.h"
22+
#include "llvm/IR/PassManager.h"
2123
#include "llvm/Pass.h"
2224
#include "llvm/Support/CommandLine.h"
2325
#include "llvm/Support/raw_ostream.h"
@@ -32,10 +34,10 @@ static cl::opt<bool> DumpRegUsage(
3234
"print-regusage", cl::init(false), cl::Hidden,
3335
cl::desc("print register usage details collected for analysis."));
3436

35-
INITIALIZE_PASS(PhysicalRegisterUsageInfo, "reg-usage-info",
37+
INITIALIZE_PASS(PhysicalRegisterUsageInfoWrapperLegacy, "reg-usage-info",
3638
"Register Usage Information Storage", false, true)
3739

38-
char PhysicalRegisterUsageInfo::ID = 0;
40+
char PhysicalRegisterUsageInfoWrapperLegacy::ID = 0;
3941

4042
void PhysicalRegisterUsageInfo::setTargetMachine(const TargetMachine &TM) {
4143
this->TM = &TM;
@@ -97,3 +99,26 @@ void PhysicalRegisterUsageInfo::print(raw_ostream &OS, const Module *M) const {
9799
OS << "\n";
98100
}
99101
}
102+
103+
bool PhysicalRegisterUsageInfo::invalidate(
104+
Module &M, const PreservedAnalyses &PA,
105+
ModuleAnalysisManager::Invalidator &) {
106+
auto PAC = PA.getChecker<PhysicalRegisterUsageAnalysis>();
107+
return !PAC.preservedWhenStateless();
108+
}
109+
110+
AnalysisKey PhysicalRegisterUsageAnalysis::Key;
111+
PhysicalRegisterUsageInfo
112+
PhysicalRegisterUsageAnalysis::run(Module &M, ModuleAnalysisManager &) {
113+
PhysicalRegisterUsageInfo PRUI;
114+
PRUI.doInitialization(M);
115+
return PRUI;
116+
}
117+
118+
PreservedAnalyses
119+
PhysicalRegisterUsageInfoPrinterPass::run(Module &M,
120+
ModuleAnalysisManager &AM) {
121+
auto *PRUI = &AM.getResult<PhysicalRegisterUsageAnalysis>(M);
122+
PRUI->print(OS, &M);
123+
return PreservedAnalyses::all();
124+
}

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#include "llvm/CodeGen/PHIElimination.h"
121121
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
122122
#include "llvm/CodeGen/RegAllocFast.h"
123+
#include "llvm/CodeGen/RegisterUsageInfo.h"
123124
#include "llvm/CodeGen/SafeStack.h"
124125
#include "llvm/CodeGen/SelectOptimize.h"
125126
#include "llvm/CodeGen/ShadowStackGCLowering.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ MODULE_ANALYSIS("module-summary", ModuleSummaryIndexAnalysis())
3131
MODULE_ANALYSIS("no-op-module", NoOpModuleAnalysis())
3232
MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
3333
MODULE_ANALYSIS("profile-summary", ProfileSummaryAnalysis())
34+
MODULE_ANALYSIS("reg-usage", PhysicalRegisterUsageAnalysis())
3435
MODULE_ANALYSIS("stack-safety", StackSafetyGlobalAnalysis())
3536
MODULE_ANALYSIS("verify", VerifierAnalysis())
3637

@@ -129,6 +130,7 @@ MODULE_PASS("print<dxil-metadata>", DXILMetadataAnalysisPrinterPass(dbgs()))
129130
MODULE_PASS("print<dxil-resource>", DXILResourcePrinterPass(dbgs()))
130131
MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
131132
MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
133+
MODULE_PASS("print<reg-usage>", PhysicalRegisterUsageInfoPrinterPass(dbgs()))
132134
MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))
133135
MODULE_PASS("pseudo-probe-update", PseudoProbeUpdatePass())
134136
MODULE_PASS("recompute-globalsaa", RecomputeGlobalsAAPass())

0 commit comments

Comments
 (0)