From: Amir Ayupov Date: Tue, 6 Jun 2023 20:41:43 +0000 (-0700) Subject: [profi][NFC] Get rid of afdo_detail::TypeMap X-Git-Tag: upstream/17.0.6~5929 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b244a4c4c92150eca3ae5c75323bb91bc4f83155;p=platform%2Fupstream%2Fllvm.git [profi][NFC] Get rid of afdo_detail::TypeMap Parametrize SampleProfileInference and SampleProfileLoaderBaseImpl by function type (Function/MachineFunction) instead of block type (BasicBlock/MachineBasicBlock). Move out specializations to appropriate locations. This change makes it possible to use GraphTraits instead of a custom TypeMap and make SampleProfileInference not dependent on LLVM types, paving the way for generalizing SampleProfileInference interfaces to BOLT IR types (BinaryFunction/BinaryBasicBlock) in stale profile matching (D144500). Reviewed By: hoy Differential Revision: https://reviews.llvm.org/D152187 --- diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h index fbfd25f..ea7ae0f 100644 --- a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h @@ -18,30 +18,8 @@ #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/IR/BasicBlock.h" -#include "llvm/IR/Instruction.h" -#include "llvm/IR/Instructions.h" - namespace llvm { -class Function; -class MachineBasicBlock; -class MachineFunction; - -namespace afdo_detail { - -template struct TypeMap {}; -template <> struct TypeMap { - using BasicBlockT = BasicBlock; - using FunctionT = Function; -}; -template <> struct TypeMap { - using BasicBlockT = MachineBasicBlock; - using FunctionT = MachineFunction; -}; - -} // end namespace afdo_detail - struct FlowJump; /// A wrapper of a binary basic block. @@ -138,10 +116,11 @@ void applyFlowInference(const ProfiParams &Params, FlowFunction &Func); void applyFlowInference(FlowFunction &Func); /// Sample profile inference pass. -template class SampleProfileInference { +template class SampleProfileInference { public: - using BasicBlockT = typename afdo_detail::TypeMap::BasicBlockT; - using FunctionT = typename afdo_detail::TypeMap::FunctionT; + using NodeRef = typename GraphTraits::NodeRef; + using BasicBlockT = typename std::remove_pointer::type; + using FunctionT = FT; using Edge = std::pair; using BlockWeightMap = DenseMap; using EdgeWeightMap = DenseMap; @@ -321,40 +300,10 @@ inline void SampleProfileInference::findUnlikelyJumps( const std::vector &BasicBlocks, BlockEdgeMap &Successors, FlowFunction &Func) {} -template <> -inline void SampleProfileInference::findUnlikelyJumps( - const std::vector &BasicBlocks, - BlockEdgeMap &Successors, FlowFunction &Func) { - for (auto &Jump : Func.Jumps) { - const auto *BB = BasicBlocks[Jump.Source]; - const auto *Succ = BasicBlocks[Jump.Target]; - const Instruction *TI = BB->getTerminator(); - // Check if a block ends with InvokeInst and mark non-taken branch unlikely. - // In that case block Succ should be a landing pad - if (Successors[BB].size() == 2 && Successors[BB].back() == Succ) { - if (isa(TI)) { - Jump.IsUnlikely = true; - } - } - const Instruction *SuccTI = Succ->getTerminator(); - // Check if the target block contains UnreachableInst and mark it unlikely - if (SuccTI->getNumSuccessors() == 0) { - if (isa(SuccTI)) { - Jump.IsUnlikely = true; - } - } - } -} - template inline bool SampleProfileInference::isExit(const BasicBlockT *BB) { return BB->succ_empty(); } -template <> -inline bool SampleProfileInference::isExit(const BasicBlock *BB) { - return succ_empty(BB); -} - } // end namespace llvm #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCE_H diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h index 6e819c6..1c6ba53 100644 --- a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h @@ -132,13 +132,15 @@ public: extern cl::opt SampleProfileUseProfi; -template class SampleProfileLoaderBaseImpl { +template class SampleProfileLoaderBaseImpl { public: SampleProfileLoaderBaseImpl(std::string Name, std::string RemapName, IntrusiveRefCntPtr FS) : Filename(Name), RemappingFilename(RemapName), FS(std::move(FS)) {} void dump() { Reader->dump(); } + using NodeRef = typename GraphTraits::NodeRef; + using BT = typename std::remove_pointer::type; using InstructionT = typename afdo_detail::IRTraits::InstructionT; using BasicBlockT = typename afdo_detail::IRTraits::BasicBlockT; using BlockFrequencyInfoT = @@ -929,11 +931,11 @@ void SampleProfileLoaderBaseImpl::propagateWeights(FunctionT &F) { } } -template -void SampleProfileLoaderBaseImpl::applyProfi( +template +void SampleProfileLoaderBaseImpl::applyProfi( FunctionT &F, BlockEdgeMap &Successors, BlockWeightMap &SampleBlockWeights, BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights) { - auto Infer = SampleProfileInference(F, Successors, SampleBlockWeights); + auto Infer = SampleProfileInference(F, Successors, SampleBlockWeights); Infer.apply(BlockWeights, EdgeWeights); } @@ -1113,18 +1115,6 @@ unsigned SampleProfileLoaderBaseImpl::getFunctionLoc(FunctionT &F) { return 0; } -template -void SampleProfileLoaderBaseImpl::computeDominanceAndLoopInfo( - FunctionT &F) { - DT.reset(new DominatorTree); - DT->recalculate(F); - - PDT.reset(new PostDominatorTree(F)); - - LI.reset(new LoopInfo); - LI->analyze(*DT); -} - #undef DEBUG_TYPE } // namespace llvm diff --git a/llvm/lib/CodeGen/MIRSampleProfile.cpp b/llvm/lib/CodeGen/MIRSampleProfile.cpp index 0a208b4..96f8589e 100644 --- a/llvm/lib/CodeGen/MIRSampleProfile.cpp +++ b/llvm/lib/CodeGen/MIRSampleProfile.cpp @@ -140,7 +140,7 @@ template <> struct IRTraits { } // namespace afdo_detail class MIRProfileLoader final - : public SampleProfileLoaderBaseImpl { + : public SampleProfileLoaderBaseImpl { public: void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT, MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI, @@ -195,8 +195,8 @@ protected: }; template <> -void SampleProfileLoaderBaseImpl< - MachineBasicBlock>::computeDominanceAndLoopInfo(MachineFunction &F) {} +void SampleProfileLoaderBaseImpl::computeDominanceAndLoopInfo( + MachineFunction &F) {} void MIRProfileLoader::setBranchProbs(MachineFunction &F) { LLVM_DEBUG(dbgs() << "\nPropagation complete. Setting branch probs\n"); diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index b06b948..44f0a75 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -505,8 +505,7 @@ private: /// This pass reads profile data from the file specified by /// -sample-profile-file and annotates every affected function with the /// profile information found in that file. -class SampleProfileLoader final - : public SampleProfileLoaderBaseImpl { +class SampleProfileLoader final : public SampleProfileLoaderBaseImpl { public: SampleProfileLoader( StringRef Name, StringRef RemapName, ThinOrFullLTOPhase LTOPhase, @@ -637,6 +636,50 @@ private: }; } // end anonymous namespace +namespace llvm { +template <> +inline bool SampleProfileInference::isExit(const BasicBlock *BB) { + return succ_empty(BB); +} + +template <> +inline void SampleProfileInference::findUnlikelyJumps( + const std::vector &BasicBlocks, + BlockEdgeMap &Successors, FlowFunction &Func) { + for (auto &Jump : Func.Jumps) { + const auto *BB = BasicBlocks[Jump.Source]; + const auto *Succ = BasicBlocks[Jump.Target]; + const Instruction *TI = BB->getTerminator(); + // Check if a block ends with InvokeInst and mark non-taken branch unlikely. + // In that case block Succ should be a landing pad + if (Successors[BB].size() == 2 && Successors[BB].back() == Succ) { + if (isa(TI)) { + Jump.IsUnlikely = true; + } + } + const Instruction *SuccTI = Succ->getTerminator(); + // Check if the target block contains UnreachableInst and mark it unlikely + if (SuccTI->getNumSuccessors() == 0) { + if (isa(SuccTI)) { + Jump.IsUnlikely = true; + } + } + } +} + +template <> +void SampleProfileLoaderBaseImpl::computeDominanceAndLoopInfo( + Function &F) { + DT.reset(new DominatorTree); + DT->recalculate(F); + + PDT.reset(new PostDominatorTree(F)); + + LI.reset(new LoopInfo); + LI->analyze(*DT); +} +} // namespace llvm + ErrorOr SampleProfileLoader::getInstWeight(const Instruction &Inst) { if (FunctionSamples::ProfileIsProbeBased) return getProbeWeight(Inst);