Skip to content

Commit 358122f

Browse files
michaelmaitlandaaryanshukla
authored andcommitted
[RISCV][GISEL] Do not initialize GlobalISel objects unless needed (llvm#98233)
Prior to this commit, we created the GlobalISel objects in the RISCVSubtarget constructor, even if we are not running GlobalISel. This patch moves creation of the GlobalISel objects into their getters, which ensures that we only create these objects if they are actually needed. This helps since some of the constructors of the GlobalISel objects have a significant amount of code. We make the `unique_ptr`s `mutable` since GlobalISel passes only have access to `const TargetSubtargetInfo` through `MF.getSubtarget()`. This patch is tested by the fact that all existing RISC-V GlobalISel tests remain passing.
1 parent ec548a5 commit 358122f

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

llvm/lib/Target/RISCV/RISCVSubtarget.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,32 @@ RISCVSubtarget::RISCVSubtarget(const Triple &TT, StringRef CPU,
9797
RVVVectorBitsMin(RVVVectorBitsMin), RVVVectorBitsMax(RVVVectorBitsMax),
9898
FrameLowering(
9999
initializeSubtargetDependencies(TT, CPU, TuneCPU, FS, ABIName)),
100-
InstrInfo(*this), RegInfo(getHwMode()), TLInfo(TM, *this) {
101-
CallLoweringInfo.reset(new RISCVCallLowering(*getTargetLowering()));
102-
Legalizer.reset(new RISCVLegalizerInfo(*this));
103-
104-
auto *RBI = new RISCVRegisterBankInfo(getHwMode());
105-
RegBankInfo.reset(RBI);
106-
InstSelector.reset(createRISCVInstructionSelector(
107-
*static_cast<const RISCVTargetMachine *>(&TM), *this, *RBI));
108-
}
100+
InstrInfo(*this), RegInfo(getHwMode()), TLInfo(TM, *this) {}
109101

110102
const CallLowering *RISCVSubtarget::getCallLowering() const {
103+
if (!CallLoweringInfo)
104+
CallLoweringInfo.reset(new RISCVCallLowering(*getTargetLowering()));
111105
return CallLoweringInfo.get();
112106
}
113107

114108
InstructionSelector *RISCVSubtarget::getInstructionSelector() const {
109+
if (!InstSelector) {
110+
InstSelector.reset(createRISCVInstructionSelector(
111+
*static_cast<const RISCVTargetMachine *>(&TLInfo.getTargetMachine()),
112+
*this, *static_cast<const RISCVRegisterBankInfo *>(getRegBankInfo())));
113+
}
115114
return InstSelector.get();
116115
}
117116

118117
const LegalizerInfo *RISCVSubtarget::getLegalizerInfo() const {
118+
if (!Legalizer)
119+
Legalizer.reset(new RISCVLegalizerInfo(*this));
119120
return Legalizer.get();
120121
}
121122

122123
const RegisterBankInfo *RISCVSubtarget::getRegBankInfo() const {
124+
if (!RegBankInfo)
125+
RegBankInfo.reset(new RISCVRegisterBankInfo(getHwMode()));
123126
return RegBankInfo.get();
124127
}
125128

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
245245

246246
protected:
247247
// GlobalISel related APIs.
248-
std::unique_ptr<CallLowering> CallLoweringInfo;
249-
std::unique_ptr<InstructionSelector> InstSelector;
250-
std::unique_ptr<LegalizerInfo> Legalizer;
251-
std::unique_ptr<RegisterBankInfo> RegBankInfo;
248+
mutable std::unique_ptr<CallLowering> CallLoweringInfo;
249+
mutable std::unique_ptr<InstructionSelector> InstSelector;
250+
mutable std::unique_ptr<LegalizerInfo> Legalizer;
251+
mutable std::unique_ptr<RegisterBankInfo> RegBankInfo;
252252

253253
// Return the known range for the bit length of RVV data registers as set
254254
// at the command line. A value of 0 means nothing is known about that particular

0 commit comments

Comments
 (0)