Author: Arthur Eubanks Date: 2020-11-26T10:11:26-08:00 New Revision: 92a67e131f3d0440ef87c6038cf239e641a3167c
URL: https://github.com/llvm/llvm-project/commit/92a67e131f3d0440ef87c6038cf239e641a3167c DIFF: https://github.com/llvm/llvm-project/commit/92a67e131f3d0440ef87c6038cf239e641a3167c.diff LOG: [BPF][NewPM] Port bpf-adjust-opt to NPM and add it to pipeline Reviewed By: yonghong-song Differential Revision: https://reviews.llvm.org/D91990 Added: Modified: llvm/lib/Target/BPF/BPF.h llvm/lib/Target/BPF/BPFAdjustOpt.cpp llvm/lib/Target/BPF/BPFTargetMachine.cpp llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll Removed: ################################################################################ diff --git a/llvm/lib/Target/BPF/BPF.h b/llvm/lib/Target/BPF/BPF.h index 8629c1503b4e..a98a3e08d5de 100644 --- a/llvm/lib/Target/BPF/BPF.h +++ b/llvm/lib/Target/BPF/BPF.h @@ -56,6 +56,11 @@ class BPFPreserveDITypePass : public PassInfoMixin<BPFPreserveDITypePass> { static bool isRequired() { return true; } }; + +class BPFAdjustOptPass : public PassInfoMixin<BPFAdjustOptPass> { +public: + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; } // namespace llvm #endif diff --git a/llvm/lib/Target/BPF/BPFAdjustOpt.cpp b/llvm/lib/Target/BPF/BPFAdjustOpt.cpp index 8efaa9d72b57..6afd2d77485d 100644 --- a/llvm/lib/Target/BPF/BPFAdjustOpt.cpp +++ b/llvm/lib/Target/BPF/BPFAdjustOpt.cpp @@ -39,6 +39,14 @@ static cl::opt<bool> DisableBPFavoidSpeculation( namespace { class BPFAdjustOpt final : public ModulePass { +public: + static char ID; + + BPFAdjustOpt() : ModulePass(ID) {} + bool runOnModule(Module &M) override; +}; + +class BPFAdjustOptImpl { struct PassThroughInfo { Instruction *Input; Instruction *UsedInst; @@ -48,13 +56,12 @@ class BPFAdjustOpt final : public ModulePass { }; public: - static char ID; - Module *Mod; + BPFAdjustOptImpl(Module *M) : M(M) {} - BPFAdjustOpt() : ModulePass(ID) {} - bool runOnModule(Module &M) override; + bool run(); private: + Module *M; SmallVector<PassThroughInfo, 16> PassThroughs; void adjustBasicBlock(BasicBlock &BB); @@ -73,9 +80,10 @@ INITIALIZE_PASS(BPFAdjustOpt, "bpf-adjust-opt", "BPF Adjust Optimization", ModulePass *llvm::createBPFAdjustOpt() { return new BPFAdjustOpt(); } -bool BPFAdjustOpt::runOnModule(Module &M) { - Mod = &M; - for (Function &F : M) +bool BPFAdjustOpt::runOnModule(Module &M) { return BPFAdjustOptImpl(&M).run(); } + +bool BPFAdjustOptImpl::run() { + for (Function &F : *M) for (auto &BB : F) { adjustBasicBlock(BB); for (auto &I : BB) @@ -85,10 +93,10 @@ bool BPFAdjustOpt::runOnModule(Module &M) { return insertPassThrough(); } -bool BPFAdjustOpt::insertPassThrough() { +bool BPFAdjustOptImpl::insertPassThrough() { for (auto &Info : PassThroughs) { auto *CI = BPFCoreSharedInfo::insertPassThrough( - Mod, Info.UsedInst->getParent(), Info.Input, Info.UsedInst); + M, Info.UsedInst->getParent(), Info.Input, Info.UsedInst); Info.UsedInst->setOperand(Info.OpIdx, CI); } @@ -97,7 +105,7 @@ bool BPFAdjustOpt::insertPassThrough() { // To avoid combining conditionals in the same basic block by // instrcombine optimization. -bool BPFAdjustOpt::serializeICMPInBB(Instruction &I) { +bool BPFAdjustOptImpl::serializeICMPInBB(Instruction &I) { // For: // comp1 = icmp <opcode> ...; // comp2 = icmp <opcode> ...; @@ -130,7 +138,7 @@ bool BPFAdjustOpt::serializeICMPInBB(Instruction &I) { // To avoid combining conditionals in the same basic block by // instrcombine optimization. -bool BPFAdjustOpt::serializeICMPCrossBB(BasicBlock &BB) { +bool BPFAdjustOptImpl::serializeICMPCrossBB(BasicBlock &BB) { // For: // B1: // comp1 = icmp <opcode> ...; @@ -204,7 +212,7 @@ bool BPFAdjustOpt::serializeICMPCrossBB(BasicBlock &BB) { // To avoid speculative hoisting certain computations out of // a basic block. -bool BPFAdjustOpt::avoidSpeculation(Instruction &I) { +bool BPFAdjustOptImpl::avoidSpeculation(Instruction &I) { if (auto *LdInst = dyn_cast<LoadInst>(&I)) { if (auto *GV = dyn_cast<GlobalVariable>(LdInst->getOperand(0))) { if (GV->hasAttribute(BPFCoreSharedInfo::AmaAttr) || @@ -297,14 +305,19 @@ bool BPFAdjustOpt::avoidSpeculation(Instruction &I) { return true; } -void BPFAdjustOpt::adjustBasicBlock(BasicBlock &BB) { +void BPFAdjustOptImpl::adjustBasicBlock(BasicBlock &BB) { if (!DisableBPFserializeICMP && serializeICMPCrossBB(BB)) return; } -void BPFAdjustOpt::adjustInst(Instruction &I) { +void BPFAdjustOptImpl::adjustInst(Instruction &I) { if (!DisableBPFserializeICMP && serializeICMPInBB(I)) return; if (!DisableBPFavoidSpeculation && avoidSpeculation(I)) return; } + +PreservedAnalyses BPFAdjustOptPass::run(Module &M, ModuleAnalysisManager &AM) { + return BPFAdjustOptImpl(&M).run() ? PreservedAnalyses::none() + : PreservedAnalyses::all(); +} diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp index 25fccea256f1..c35a3192282f 100644 --- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp +++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp @@ -136,6 +136,10 @@ void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB, PassBuilder::OptimizationLevel Level) { FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true))); }); + PB.registerPipelineEarlySimplificationEPCallback( + [=](ModulePassManager &MPM, PassBuilder::OptimizationLevel) { + MPM.addPass(BPFAdjustOptPass()); + }); } void BPFPassConfig::addIRPasses() { diff --git a/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll b/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll index bb651f4ea57a..6eea4d2f4618 100644 --- a/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll +++ b/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll @@ -1,7 +1,11 @@ ; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1 ; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK %s +; RUN: opt -passes='default<O2>' -mtriple=bpf-pc-linux %s | llvm-dis > %t1 +; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK %s ; RUN: opt -O2 -mtriple=bpf-pc-linux -bpf-disable-serialize-icmp %s | llvm-dis > %t1 ; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK-DISABLE %s +; RUN: opt -passes='default<O2>' -mtriple=bpf-pc-linux -bpf-disable-serialize-icmp %s | llvm-dis > %t1 +; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK-DISABLE %s ; ; Source: ; int foo(); _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits