From 40251fee00840b98d927c2c138e45e812bc3468b Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Mon, 5 Oct 2020 15:17:12 -0700 Subject: [PATCH] [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 --- llvm/lib/Target/BPF/BPF.h | 19 +++++++- llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp | 50 ++++++++++++++------- llvm/lib/Target/BPF/BPFPreserveDIType.cpp | 58 +++++++++++++------------ llvm/lib/Target/BPF/BPFTargetMachine.cpp | 19 +++++++- llvm/lib/Target/BPF/BPFTargetMachine.h | 2 + llvm/test/CodeGen/BPF/CORE/store-addr.ll | 3 ++ 6 files changed, 105 insertions(+), 46 deletions(-) diff --git a/llvm/lib/Target/BPF/BPF.h b/llvm/lib/Target/BPF/BPF.h index 79466fa..966a3b0c 100644 --- a/llvm/lib/Target/BPF/BPF.h +++ b/llvm/lib/Target/BPF/BPF.h @@ -10,6 +10,7 @@ #define LLVM_LIB_TARGET_BPF_BPF_H #include "MCTargetDesc/BPFMCTargetDesc.h" +#include "llvm/IR/PassManager.h" #include "llvm/Target/TargetMachine.h" namespace llvm { @@ -28,13 +29,27 @@ FunctionPass *createBPFMIPreEmitCheckingPass(); void initializeBPFCheckAndAdjustIRPass(PassRegistry&); -void initializeBPFAbstractMemberAccessPass(PassRegistry&); +void initializeBPFAbstractMemberAccessLegacyPassPass(PassRegistry &); void initializeBPFPreserveDITypePass(PassRegistry&); void initializeBPFMISimplifyPatchablePass(PassRegistry&); void initializeBPFMIPeepholePass(PassRegistry&); void initializeBPFMIPeepholeTruncElimPass(PassRegistry&); void initializeBPFMIPreEmitPeepholePass(PassRegistry&); void initializeBPFMIPreEmitCheckingPass(PassRegistry&); -} + +class BPFAbstractMemberAccessPass + : public PassInfoMixin { + BPFTargetMachine *TM; + +public: + BPFAbstractMemberAccessPass(BPFTargetMachine *TM) : TM(TM) {} + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; + +class BPFPreserveDITypePass : public PassInfoMixin { +public: + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; +} // namespace llvm #endif diff --git a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp index 848fc36..d04b064 100644 --- a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp +++ b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp @@ -83,6 +83,7 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicsBPF.h" #include "llvm/IR/Module.h" +#include "llvm/IR/PassManager.h" #include "llvm/IR/Type.h" #include "llvm/IR/User.h" #include "llvm/IR/Value.h" @@ -113,18 +114,11 @@ Instruction *BPFCoreSharedInfo::insertPassThrough(Module *M, BasicBlock *BB, using namespace llvm; namespace { - -class BPFAbstractMemberAccess final : public FunctionPass { - bool runOnFunction(Function &F) override; - +class BPFAbstractMemberAccess final { public: - static char ID; - TargetMachine *TM; - // Add optional BPFTargetMachine parameter so that BPF backend can add the phase - // with target machine to find out the endianness. The default constructor (without - // parameters) is used by the pass manager for managing purposes. - BPFAbstractMemberAccess(BPFTargetMachine *TM = nullptr) - : FunctionPass(ID), TM(TM) {} + BPFAbstractMemberAccess(BPFTargetMachine *TM) : TM(TM) {} + + bool run(Function &F); struct CallInfo { uint32_t Kind; @@ -143,6 +137,7 @@ private: BPFPreserveFieldInfoAI = 4, }; + TargetMachine *TM; const DataLayout *DL = nullptr; Module *M = nullptr; @@ -183,17 +178,36 @@ private: uint64_t getConstant(const Value *IndexValue); bool transformGEPChain(CallInst *Call, CallInfo &CInfo); }; + +class BPFAbstractMemberAccessLegacyPass final : public FunctionPass { + BPFTargetMachine *TM; + + bool runOnFunction(Function &F) override { + return BPFAbstractMemberAccess(TM).run(F); + } + +public: + static char ID; + + // Add optional BPFTargetMachine parameter so that BPF backend can add the + // phase with target machine to find out the endianness. The default + // constructor (without parameters) is used by the pass manager for managing + // purposes. + BPFAbstractMemberAccessLegacyPass(BPFTargetMachine *TM = nullptr) + : FunctionPass(ID), TM(TM) {} +}; + } // End anonymous namespace -char BPFAbstractMemberAccess::ID = 0; -INITIALIZE_PASS(BPFAbstractMemberAccess, DEBUG_TYPE, +char BPFAbstractMemberAccessLegacyPass::ID = 0; +INITIALIZE_PASS(BPFAbstractMemberAccessLegacyPass, DEBUG_TYPE, "BPF Abstract Member Access", false, false) FunctionPass *llvm::createBPFAbstractMemberAccess(BPFTargetMachine *TM) { - return new BPFAbstractMemberAccess(TM); + return new BPFAbstractMemberAccessLegacyPass(TM); } -bool BPFAbstractMemberAccess::runOnFunction(Function &F) { +bool BPFAbstractMemberAccess::run(Function &F) { LLVM_DEBUG(dbgs() << "********** Abstract Member Accesses **********\n"); M = F.getParent(); @@ -1096,3 +1110,9 @@ bool BPFAbstractMemberAccess::doTransformation(Function &F) { return removePreserveAccessIndexIntrinsic(F) || Transformed; } + +PreservedAnalyses +BPFAbstractMemberAccessPass::run(Function &F, FunctionAnalysisManager &AM) { + return BPFAbstractMemberAccess(TM).run(F) ? PreservedAnalyses::none() + : PreservedAnalyses::all(); +} diff --git a/llvm/lib/Target/BPF/BPFPreserveDIType.cpp b/llvm/lib/Target/BPF/BPFPreserveDIType.cpp index 282f0fb..d2e9697 100644 --- a/llvm/lib/Target/BPF/BPFPreserveDIType.cpp +++ b/llvm/lib/Target/BPF/BPFPreserveDIType.cpp @@ -17,6 +17,7 @@ #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Module.h" +#include "llvm/IR/PassManager.h" #include "llvm/IR/Type.h" #include "llvm/IR/User.h" #include "llvm/IR/Value.h" @@ -33,41 +34,15 @@ using namespace llvm; namespace { -class BPFPreserveDIType final : public FunctionPass { - bool runOnFunction(Function &F) override; - -public: - static char ID; - BPFPreserveDIType() : FunctionPass(ID) {} - -private: - Module *M = nullptr; - - bool doTransformation(Function &F); -}; -} // End anonymous namespace - -char BPFPreserveDIType::ID = 0; -INITIALIZE_PASS(BPFPreserveDIType, DEBUG_TYPE, "BPF Preserve Debuginfo Type", - false, false) - -FunctionPass *llvm::createBPFPreserveDIType() { return new BPFPreserveDIType(); } - -bool BPFPreserveDIType::runOnFunction(Function &F) { +static bool BPFPreserveDITypeImpl(Function &F) { LLVM_DEBUG(dbgs() << "********** preserve debuginfo type **********\n"); - M = F.getParent(); - if (!M) - return false; + Module *M = F.getParent(); // Bail out if no debug info. if (M->debug_compile_units().empty()) return false; - return doTransformation(F); -} - -bool BPFPreserveDIType::doTransformation(Function &F) { std::vector PreserveDITypeCalls; for (auto &BB : F) { @@ -135,3 +110,30 @@ bool BPFPreserveDIType::doTransformation(Function &F) { return true; } + +class BPFPreserveDIType final : public FunctionPass { + bool runOnFunction(Function &F) override; + +public: + static char ID; + BPFPreserveDIType() : FunctionPass(ID) {} +}; +} // End anonymous namespace + +char BPFPreserveDIType::ID = 0; +INITIALIZE_PASS(BPFPreserveDIType, DEBUG_TYPE, "BPF Preserve Debuginfo Type", + false, false) + +FunctionPass *llvm::createBPFPreserveDIType() { + return new BPFPreserveDIType(); +} + +bool BPFPreserveDIType::runOnFunction(Function &F) { + return BPFPreserveDITypeImpl(F); +} + +PreservedAnalyses BPFPreserveDITypePass::run(Function &F, + FunctionAnalysisManager &AM) { + return BPFPreserveDITypeImpl(F) ? PreservedAnalyses::none() + : PreservedAnalyses::all(); +} diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp index 7511dde..e5fc5ba 100644 --- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp +++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp @@ -18,11 +18,14 @@ #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Passes/PassBuilder.h" #include "llvm/Support/FormattedStream.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" #include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/SimplifyCFG.h" #include "llvm/Transforms/Utils/SimplifyCFGOptions.h" using namespace llvm; @@ -37,7 +40,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() { RegisterTargetMachine Z(getTheBPFTarget()); PassRegistry &PR = *PassRegistry::getPassRegistry(); - initializeBPFAbstractMemberAccessPass(PR); + initializeBPFAbstractMemberAccessLegacyPassPass(PR); initializeBPFPreserveDITypePass(PR); initializeBPFCheckAndAdjustIRPass(PR); initializeBPFMIPeepholePass(PR); @@ -114,6 +117,20 @@ void BPFTargetMachine::adjustPassManager(PassManagerBuilder &Builder) { }); } +void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB, + bool DebugPassManager) { + PB.registerPipelineStartEPCallback([=](ModulePassManager &MPM) { + FunctionPassManager FPM(DebugPassManager); + FPM.addPass(BPFAbstractMemberAccessPass(this)); + FPM.addPass(BPFPreserveDITypePass()); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + }); + PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM, + PassBuilder::OptimizationLevel Level) { + FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true))); + }); +} + void BPFPassConfig::addIRPasses() { addPass(createBPFCheckAndAdjustIR()); TargetPassConfig::addIRPasses(); diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.h b/llvm/lib/Target/BPF/BPFTargetMachine.h index b252d21..5243a15 100644 --- a/llvm/lib/Target/BPF/BPFTargetMachine.h +++ b/llvm/lib/Target/BPF/BPFTargetMachine.h @@ -39,6 +39,8 @@ public: } void adjustPassManager(PassManagerBuilder &) override; + void registerPassBuilderCallbacks(PassBuilder &PB, + bool DebugPassManager) override; }; } diff --git a/llvm/test/CodeGen/BPF/CORE/store-addr.ll b/llvm/test/CodeGen/BPF/CORE/store-addr.ll index 609460f..47d8fd2 100644 --- a/llvm/test/CodeGen/BPF/CORE/store-addr.ll +++ b/llvm/test/CodeGen/BPF/CORE/store-addr.ll @@ -1,6 +1,9 @@ ; RUN: opt -O2 %s | llvm-dis > %t1 ; RUN: llc -filetype=asm -o - %t1 | FileCheck %s ; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck %s +; RUN: opt -passes='default' %s | llvm-dis > %t1 +; RUN: llc -filetype=asm -o - %t1 | FileCheck %s +; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck %s ; Source code: ; struct t { ; int a; -- 2.7.4