[llvm] b244a4c - [profi][NFC] Get rid of afdo_detail::TypeMap
Amir Ayupov via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 6 13:48:43 PDT 2023
Author: Amir Ayupov
Date: 2023-06-06T13:48:37-07:00
New Revision: b244a4c4c92150eca3ae5c75323bb91bc4f83155
URL: https://github.com/llvm/llvm-project/commit/b244a4c4c92150eca3ae5c75323bb91bc4f83155
DIFF: https://github.com/llvm/llvm-project/commit/b244a4c4c92150eca3ae5c75323bb91bc4f83155.diff
LOG: [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
Added:
Modified:
llvm/include/llvm/Transforms/Utils/SampleProfileInference.h
llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h
llvm/lib/CodeGen/MIRSampleProfile.cpp
llvm/lib/Transforms/IPO/SampleProfile.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h
index fbfd25f8d81dc..ea7ae0f57b796 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 <class BlockT> struct TypeMap {};
-template <> struct TypeMap<BasicBlock> {
- using BasicBlockT = BasicBlock;
- using FunctionT = Function;
-};
-template <> struct TypeMap<MachineBasicBlock> {
- 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 <typename BT> class SampleProfileInference {
+template <typename FT> class SampleProfileInference {
public:
- using BasicBlockT = typename afdo_detail::TypeMap<BT>::BasicBlockT;
- using FunctionT = typename afdo_detail::TypeMap<BT>::FunctionT;
+ using NodeRef = typename GraphTraits<FT *>::NodeRef;
+ using BasicBlockT = typename std::remove_pointer<NodeRef>::type;
+ using FunctionT = FT;
using Edge = std::pair<const BasicBlockT *, const BasicBlockT *>;
using BlockWeightMap = DenseMap<const BasicBlockT *, uint64_t>;
using EdgeWeightMap = DenseMap<Edge, uint64_t>;
@@ -321,40 +300,10 @@ inline void SampleProfileInference<BT>::findUnlikelyJumps(
const std::vector<const BasicBlockT *> &BasicBlocks,
BlockEdgeMap &Successors, FlowFunction &Func) {}
-template <>
-inline void SampleProfileInference<BasicBlock>::findUnlikelyJumps(
- const std::vector<const BasicBlockT *> &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<InvokeInst>(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<UnreachableInst>(SuccTI)) {
- Jump.IsUnlikely = true;
- }
- }
- }
-}
-
template <typename BT>
inline bool SampleProfileInference<BT>::isExit(const BasicBlockT *BB) {
return BB->succ_empty();
}
-template <>
-inline bool SampleProfileInference<BasicBlock>::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 6e819c6568eab..1c6ba530e3df4 100644
--- a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h
+++ b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h
@@ -132,13 +132,15 @@ class PseudoProbeManager {
extern cl::opt<bool> SampleProfileUseProfi;
-template <typename BT> class SampleProfileLoaderBaseImpl {
+template <typename FT> class SampleProfileLoaderBaseImpl {
public:
SampleProfileLoaderBaseImpl(std::string Name, std::string RemapName,
IntrusiveRefCntPtr<vfs::FileSystem> FS)
: Filename(Name), RemappingFilename(RemapName), FS(std::move(FS)) {}
void dump() { Reader->dump(); }
+ using NodeRef = typename GraphTraits<FT *>::NodeRef;
+ using BT = typename std::remove_pointer<NodeRef>::type;
using InstructionT = typename afdo_detail::IRTraits<BT>::InstructionT;
using BasicBlockT = typename afdo_detail::IRTraits<BT>::BasicBlockT;
using BlockFrequencyInfoT =
@@ -929,11 +931,11 @@ void SampleProfileLoaderBaseImpl<BT>::propagateWeights(FunctionT &F) {
}
}
-template <typename BT>
-void SampleProfileLoaderBaseImpl<BT>::applyProfi(
+template <typename FT>
+void SampleProfileLoaderBaseImpl<FT>::applyProfi(
FunctionT &F, BlockEdgeMap &Successors, BlockWeightMap &SampleBlockWeights,
BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights) {
- auto Infer = SampleProfileInference<BT>(F, Successors, SampleBlockWeights);
+ auto Infer = SampleProfileInference<FT>(F, Successors, SampleBlockWeights);
Infer.apply(BlockWeights, EdgeWeights);
}
@@ -1113,18 +1115,6 @@ unsigned SampleProfileLoaderBaseImpl<BT>::getFunctionLoc(FunctionT &F) {
return 0;
}
-template <typename BT>
-void SampleProfileLoaderBaseImpl<BT>::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 0a208b46b49ba..96f8589e682d5 100644
--- a/llvm/lib/CodeGen/MIRSampleProfile.cpp
+++ b/llvm/lib/CodeGen/MIRSampleProfile.cpp
@@ -140,7 +140,7 @@ template <> struct IRTraits<MachineBasicBlock> {
} // namespace afdo_detail
class MIRProfileLoader final
- : public SampleProfileLoaderBaseImpl<MachineBasicBlock> {
+ : public SampleProfileLoaderBaseImpl<MachineFunction> {
public:
void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT,
MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI,
@@ -195,8 +195,8 @@ class MIRProfileLoader final
};
template <>
-void SampleProfileLoaderBaseImpl<
- MachineBasicBlock>::computeDominanceAndLoopInfo(MachineFunction &F) {}
+void SampleProfileLoaderBaseImpl<MachineFunction>::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 b06b9480bbc02..44f0a7503007e 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -505,8 +505,7 @@ class SampleProfileMatcher {
/// 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<BasicBlock> {
+class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
public:
SampleProfileLoader(
StringRef Name, StringRef RemapName, ThinOrFullLTOPhase LTOPhase,
@@ -637,6 +636,50 @@ class SampleProfileLoader final
};
} // end anonymous namespace
+namespace llvm {
+template <>
+inline bool SampleProfileInference<Function>::isExit(const BasicBlock *BB) {
+ return succ_empty(BB);
+}
+
+template <>
+inline void SampleProfileInference<Function>::findUnlikelyJumps(
+ const std::vector<const BasicBlockT *> &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<InvokeInst>(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<UnreachableInst>(SuccTI)) {
+ Jump.IsUnlikely = true;
+ }
+ }
+ }
+}
+
+template <>
+void SampleProfileLoaderBaseImpl<Function>::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<uint64_t> SampleProfileLoader::getInstWeight(const Instruction &Inst) {
if (FunctionSamples::ProfileIsProbeBased)
return getProbeWeight(Inst);
More information about the llvm-commits
mailing list