Skip to content

Commit 40251fe

Browse files
committed
[BPF][NewPM] Make BPFTargetMachine properly adjust NPM optimizer pipeline
This involves porting BPFAbstractMemberAccess and BPFPreserveDIType to NPM, then adding them BPFTargetMachine::registerPassBuilderCallbacks (the NPM equivalent of adjustPassManager()). Reviewed By: yonghong-song, asbirlea Differential Revision: https://reviews.llvm.org/D88855
1 parent 8df17b4 commit 40251fe

File tree

6 files changed

+105
-46
lines changed

6 files changed

+105
-46
lines changed

llvm/lib/Target/BPF/BPF.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIB_TARGET_BPF_BPF_H
1111

1212
#include "MCTargetDesc/BPFMCTargetDesc.h"
13+
#include "llvm/IR/PassManager.h"
1314
#include "llvm/Target/TargetMachine.h"
1415

1516
namespace llvm {
@@ -28,13 +29,27 @@ FunctionPass *createBPFMIPreEmitCheckingPass();
2829

2930
void initializeBPFCheckAndAdjustIRPass(PassRegistry&);
3031

31-
void initializeBPFAbstractMemberAccessPass(PassRegistry&);
32+
void initializeBPFAbstractMemberAccessLegacyPassPass(PassRegistry &);
3233
void initializeBPFPreserveDITypePass(PassRegistry&);
3334
void initializeBPFMISimplifyPatchablePass(PassRegistry&);
3435
void initializeBPFMIPeepholePass(PassRegistry&);
3536
void initializeBPFMIPeepholeTruncElimPass(PassRegistry&);
3637
void initializeBPFMIPreEmitPeepholePass(PassRegistry&);
3738
void initializeBPFMIPreEmitCheckingPass(PassRegistry&);
38-
}
39+
40+
class BPFAbstractMemberAccessPass
41+
: public PassInfoMixin<BPFAbstractMemberAccessPass> {
42+
BPFTargetMachine *TM;
43+
44+
public:
45+
BPFAbstractMemberAccessPass(BPFTargetMachine *TM) : TM(TM) {}
46+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
47+
};
48+
49+
class BPFPreserveDITypePass : public PassInfoMixin<BPFPreserveDITypePass> {
50+
public:
51+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
52+
};
53+
} // namespace llvm
3954

4055
#endif

llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include "llvm/IR/Instructions.h"
8484
#include "llvm/IR/IntrinsicsBPF.h"
8585
#include "llvm/IR/Module.h"
86+
#include "llvm/IR/PassManager.h"
8687
#include "llvm/IR/Type.h"
8788
#include "llvm/IR/User.h"
8889
#include "llvm/IR/Value.h"
@@ -113,18 +114,11 @@ Instruction *BPFCoreSharedInfo::insertPassThrough(Module *M, BasicBlock *BB,
113114
using namespace llvm;
114115

115116
namespace {
116-
117-
class BPFAbstractMemberAccess final : public FunctionPass {
118-
bool runOnFunction(Function &F) override;
119-
117+
class BPFAbstractMemberAccess final {
120118
public:
121-
static char ID;
122-
TargetMachine *TM;
123-
// Add optional BPFTargetMachine parameter so that BPF backend can add the phase
124-
// with target machine to find out the endianness. The default constructor (without
125-
// parameters) is used by the pass manager for managing purposes.
126-
BPFAbstractMemberAccess(BPFTargetMachine *TM = nullptr)
127-
: FunctionPass(ID), TM(TM) {}
119+
BPFAbstractMemberAccess(BPFTargetMachine *TM) : TM(TM) {}
120+
121+
bool run(Function &F);
128122

129123
struct CallInfo {
130124
uint32_t Kind;
@@ -143,6 +137,7 @@ class BPFAbstractMemberAccess final : public FunctionPass {
143137
BPFPreserveFieldInfoAI = 4,
144138
};
145139

140+
TargetMachine *TM;
146141
const DataLayout *DL = nullptr;
147142
Module *M = nullptr;
148143

@@ -183,17 +178,36 @@ class BPFAbstractMemberAccess final : public FunctionPass {
183178
uint64_t getConstant(const Value *IndexValue);
184179
bool transformGEPChain(CallInst *Call, CallInfo &CInfo);
185180
};
181+
182+
class BPFAbstractMemberAccessLegacyPass final : public FunctionPass {
183+
BPFTargetMachine *TM;
184+
185+
bool runOnFunction(Function &F) override {
186+
return BPFAbstractMemberAccess(TM).run(F);
187+
}
188+
189+
public:
190+
static char ID;
191+
192+
// Add optional BPFTargetMachine parameter so that BPF backend can add the
193+
// phase with target machine to find out the endianness. The default
194+
// constructor (without parameters) is used by the pass manager for managing
195+
// purposes.
196+
BPFAbstractMemberAccessLegacyPass(BPFTargetMachine *TM = nullptr)
197+
: FunctionPass(ID), TM(TM) {}
198+
};
199+
186200
} // End anonymous namespace
187201

188-
char BPFAbstractMemberAccess::ID = 0;
189-
INITIALIZE_PASS(BPFAbstractMemberAccess, DEBUG_TYPE,
202+
char BPFAbstractMemberAccessLegacyPass::ID = 0;
203+
INITIALIZE_PASS(BPFAbstractMemberAccessLegacyPass, DEBUG_TYPE,
190204
"BPF Abstract Member Access", false, false)
191205

192206
FunctionPass *llvm::createBPFAbstractMemberAccess(BPFTargetMachine *TM) {
193-
return new BPFAbstractMemberAccess(TM);
207+
return new BPFAbstractMemberAccessLegacyPass(TM);
194208
}
195209

196-
bool BPFAbstractMemberAccess::runOnFunction(Function &F) {
210+
bool BPFAbstractMemberAccess::run(Function &F) {
197211
LLVM_DEBUG(dbgs() << "********** Abstract Member Accesses **********\n");
198212

199213
M = F.getParent();
@@ -1096,3 +1110,9 @@ bool BPFAbstractMemberAccess::doTransformation(Function &F) {
10961110

10971111
return removePreserveAccessIndexIntrinsic(F) || Transformed;
10981112
}
1113+
1114+
PreservedAnalyses
1115+
BPFAbstractMemberAccessPass::run(Function &F, FunctionAnalysisManager &AM) {
1116+
return BPFAbstractMemberAccess(TM).run(F) ? PreservedAnalyses::none()
1117+
: PreservedAnalyses::all();
1118+
}

llvm/lib/Target/BPF/BPFPreserveDIType.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/IR/Instruction.h"
1818
#include "llvm/IR/Instructions.h"
1919
#include "llvm/IR/Module.h"
20+
#include "llvm/IR/PassManager.h"
2021
#include "llvm/IR/Type.h"
2122
#include "llvm/IR/User.h"
2223
#include "llvm/IR/Value.h"
@@ -33,41 +34,15 @@ using namespace llvm;
3334

3435
namespace {
3536

36-
class BPFPreserveDIType final : public FunctionPass {
37-
bool runOnFunction(Function &F) override;
38-
39-
public:
40-
static char ID;
41-
BPFPreserveDIType() : FunctionPass(ID) {}
42-
43-
private:
44-
Module *M = nullptr;
45-
46-
bool doTransformation(Function &F);
47-
};
48-
} // End anonymous namespace
49-
50-
char BPFPreserveDIType::ID = 0;
51-
INITIALIZE_PASS(BPFPreserveDIType, DEBUG_TYPE, "BPF Preserve Debuginfo Type",
52-
false, false)
53-
54-
FunctionPass *llvm::createBPFPreserveDIType() { return new BPFPreserveDIType(); }
55-
56-
bool BPFPreserveDIType::runOnFunction(Function &F) {
37+
static bool BPFPreserveDITypeImpl(Function &F) {
5738
LLVM_DEBUG(dbgs() << "********** preserve debuginfo type **********\n");
5839

59-
M = F.getParent();
60-
if (!M)
61-
return false;
40+
Module *M = F.getParent();
6241

6342
// Bail out if no debug info.
6443
if (M->debug_compile_units().empty())
6544
return false;
6645

67-
return doTransformation(F);
68-
}
69-
70-
bool BPFPreserveDIType::doTransformation(Function &F) {
7146
std::vector<CallInst *> PreserveDITypeCalls;
7247

7348
for (auto &BB : F) {
@@ -135,3 +110,30 @@ bool BPFPreserveDIType::doTransformation(Function &F) {
135110

136111
return true;
137112
}
113+
114+
class BPFPreserveDIType final : public FunctionPass {
115+
bool runOnFunction(Function &F) override;
116+
117+
public:
118+
static char ID;
119+
BPFPreserveDIType() : FunctionPass(ID) {}
120+
};
121+
} // End anonymous namespace
122+
123+
char BPFPreserveDIType::ID = 0;
124+
INITIALIZE_PASS(BPFPreserveDIType, DEBUG_TYPE, "BPF Preserve Debuginfo Type",
125+
false, false)
126+
127+
FunctionPass *llvm::createBPFPreserveDIType() {
128+
return new BPFPreserveDIType();
129+
}
130+
131+
bool BPFPreserveDIType::runOnFunction(Function &F) {
132+
return BPFPreserveDITypeImpl(F);
133+
}
134+
135+
PreservedAnalyses BPFPreserveDITypePass::run(Function &F,
136+
FunctionAnalysisManager &AM) {
137+
return BPFPreserveDITypeImpl(F) ? PreservedAnalyses::none()
138+
: PreservedAnalyses::all();
139+
}

llvm/lib/Target/BPF/BPFTargetMachine.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
1919
#include "llvm/CodeGen/TargetPassConfig.h"
2020
#include "llvm/IR/LegacyPassManager.h"
21+
#include "llvm/IR/PassManager.h"
22+
#include "llvm/Passes/PassBuilder.h"
2123
#include "llvm/Support/FormattedStream.h"
2224
#include "llvm/Support/TargetRegistry.h"
2325
#include "llvm/Target/TargetOptions.h"
2426
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
2527
#include "llvm/Transforms/Scalar.h"
28+
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
2629
#include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
2730
using namespace llvm;
2831

@@ -37,7 +40,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
3740
RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
3841

3942
PassRegistry &PR = *PassRegistry::getPassRegistry();
40-
initializeBPFAbstractMemberAccessPass(PR);
43+
initializeBPFAbstractMemberAccessLegacyPassPass(PR);
4144
initializeBPFPreserveDITypePass(PR);
4245
initializeBPFCheckAndAdjustIRPass(PR);
4346
initializeBPFMIPeepholePass(PR);
@@ -114,6 +117,20 @@ void BPFTargetMachine::adjustPassManager(PassManagerBuilder &Builder) {
114117
});
115118
}
116119

120+
void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB,
121+
bool DebugPassManager) {
122+
PB.registerPipelineStartEPCallback([=](ModulePassManager &MPM) {
123+
FunctionPassManager FPM(DebugPassManager);
124+
FPM.addPass(BPFAbstractMemberAccessPass(this));
125+
FPM.addPass(BPFPreserveDITypePass());
126+
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
127+
});
128+
PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
129+
PassBuilder::OptimizationLevel Level) {
130+
FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
131+
});
132+
}
133+
117134
void BPFPassConfig::addIRPasses() {
118135
addPass(createBPFCheckAndAdjustIR());
119136
TargetPassConfig::addIRPasses();

llvm/lib/Target/BPF/BPFTargetMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class BPFTargetMachine : public LLVMTargetMachine {
3939
}
4040

4141
void adjustPassManager(PassManagerBuilder &) override;
42+
void registerPassBuilderCallbacks(PassBuilder &PB,
43+
bool DebugPassManager) override;
4244
};
4345
}
4446

llvm/test/CodeGen/BPF/CORE/store-addr.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
; RUN: opt -O2 %s | llvm-dis > %t1
22
; RUN: llc -filetype=asm -o - %t1 | FileCheck %s
33
; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck %s
4+
; RUN: opt -passes='default<O2>' %s | llvm-dis > %t1
5+
; RUN: llc -filetype=asm -o - %t1 | FileCheck %s
6+
; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck %s
47
; Source code:
58
; struct t {
69
; int a;

0 commit comments

Comments
 (0)