LLVM: lib/CodeGen/MIRSampleProfile.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
36#include
37
38using namespace llvm;
42
43#define DEBUG_TYPE "fs-profile-loader"
44
47 cl::desc("Print setting flow sensitive branch probabilities"));
49 "fs-profile-debug-prob-diff-threshold", cl::init(10),
51 "Only show debug message if the branch probability is greater than "
52 "this value (in percentage)."));
53
55 "fs-profile-debug-bw-threshold", cl::init(10000),
56 cl::desc("Only show debug message if the source branch weight is greater "
57 " than this value."));
58
61 cl::desc("View BFI before MIR loader"));
64 cl::desc("View BFI after MIR loader"));
65
67
69 "Load MIR Sample Profile",
70 false, false)
78
80
87
88namespace llvm {
89
90
91
92
94
95
96
98
100 if (MI.isPseudoProbe()) {
102 Probe.Id = MI.getOperand(1).getImm();
103 Probe.Type = MI.getOperand(2).getImm();
104 Probe.Attr = MI.getOperand(3).getImm();
108 return Probe;
109 }
110
111
112 return std::nullopt;
113}
114
143}
144
147public:
151 DT = MDT;
152 PDT = MPDT;
153 LI = MLI;
154 BFI = MBFI;
156 }
163
168
173
174protected:
176
177
179
180
182
183
184
186
187
189
195 return std::error_code();
197 }
198};
199
200template <>
203
205 LLVM_DEBUG(dbgs() << "\nPropagation complete. Setting branch probs\n");
206 for (auto &BI : F) {
209 continue;
214 Edge E = std::make_pair(BB, Succ);
216 }
217
218 if (BBWeight != SumEdgeWeight) {
219 LLVM_DEBUG(dbgs() << "BBweight is not equal to SumEdgeWeight: BBWWeight="
220 << BBWeight << " SumEdgeWeight= " << SumEdgeWeight
221 << "\n");
222 BBWeight = SumEdgeWeight;
223 }
224 if (BBWeight == 0) {
225 LLVM_DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n");
226 continue;
227 }
228
229#ifndef NDEBUG
230 uint64_t BBWeightOrig = BBWeight;
231#endif
232 uint32_t MaxWeight = std::numeric_limits<uint32_t>::max();
234 if (BBWeight > MaxWeight) {
235 Factor = BBWeight / MaxWeight + 1;
236 BBWeight /= Factor;
237 LLVM_DEBUG(dbgs() << "Scaling weights by " << Factor << "\n");
238 }
239
244 Edge E = std::make_pair(BB, Succ);
246 EdgeWeight /= Factor;
247
248 assert(BBWeight >= EdgeWeight &&
249 "BBweight is larger than EdgeWeight -- should not happen.\n");
250
253 if (OldProb == NewProb)
254 continue;
256#ifndef NDEBUG
258 continue;
259 bool Show = false;
261 if (OldProb > NewProb)
262 Diff = OldProb - NewProb;
263 else
264 Diff = NewProb - OldProb;
267
270 if (Show) {
271 dbgs() << "Set branch fs prob: MBB (" << BB->getNumber() << " -> "
273 if (DIL)
274 dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
275 << DIL->getColumn();
276 if (SuccDIL)
277 dbgs() << "-->" << SuccDIL->getFilename() << ":" << SuccDIL->getLine()
278 << ":" << SuccDIL->getColumn();
279 dbgs() << " W=" << BBWeightOrig << " " << OldProb << " --> " << NewProb
280 << "\n";
281 }
282#endif
283 }
284 }
285}
286
288 auto &Ctx = M.getContext();
289
292 if (std::error_code EC = ReaderOrErr.getError()) {
293 std::string Msg = "Could not open profile: " + EC.message();
295 return false;
296 }
297
298 Reader = std::move(ReaderOrErr.get());
299 Reader->setModule(&M);
301
302
303 if (Reader->profileIsProbeBased()) {
304 ProbeManager = std::make_unique(M);
306 return false;
307 }
308 }
309
310 return true;
311}
312
314
315
316
317
318
319
320 if (->profileIsFS())
321 return false;
322
327 return false;
328
331 return false;
332 } else {
334 return false;
335 }
336
339
340
342
344}
345
346}
347
349 std::string FileName, std::string RemappingFileName, FSDiscriminatorPass P,
354
356 MIRSampleLoader = std::make_unique(
357 FileName, RemappingFileName, std::move(VFS));
358 assert(LowBit < HighBit && "HighBit needs to be greater than Lowbit");
359}
360
361bool MIRProfileLoaderPass::runOnMachineFunction(MachineFunction &MF) {
362 if (!MIRSampleLoader->isValid())
363 return false;
364
365 LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Func: "
369 auto *MPDT =
371
373 MDT->updateBlockNumbers();
374 MPDT->updateBlockNumbers();
375
376 MIRSampleLoader->setInitVals(
379
383 MBFI->view("MIR_Prof_loader_b." + MF.getName(), false);
384 }
385
386 bool Changed = MIRSampleLoader->runOnFunction(MF);
390
394 MBFI->view("MIR_prof_loader_a." + MF.getName(), false);
395 }
396
398}
399
400bool MIRProfileLoaderPass::doInitialization(Module &M) {
401 LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Module " << M.getName()
402 << "\n");
403
404 MIRSampleLoader->setFSPass(P);
405 return MIRSampleLoader->doInitialization(M);
406}
407
408void MIRProfileLoaderPass::getAnalysisUsage(AnalysisUsage &AU) const {
410 AU.addRequired();
411 AU.addRequired();
412 AU.addRequired();
414 AU.addRequired();
416}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
static cl::opt< bool > ShowFSBranchProb("show-fs-branchprob", cl::Hidden, cl::init(false), cl::desc("Print setting flow sensitive branch probabilities"))
static cl::opt< bool > ViewBFIAfter("fs-viewbfi-after", cl::Hidden, cl::init(false), cl::desc("View BFI after MIR loader"))
static cl::opt< unsigned > FSProfileDebugBWThreshold("fs-profile-debug-bw-threshold", cl::init(10000), cl::desc("Only show debug message if the source branch weight is greater " " than this value."))
static cl::opt< unsigned > FSProfileDebugProbDiffThreshold("fs-profile-debug-prob-diff-threshold", cl::init(10), cl::desc("Only show debug message if the branch probability is greater than " "this value (in percentage)."))
static cl::opt< bool > ViewBFIBefore("fs-viewbfi-before", cl::Hidden, cl::init(false), cl::desc("View BFI before MIR loader"))
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file provides the interface for the sampled PGO profile loader base implementation.
This file provides the utility functions for the sampled PGO loader base implementation.
Defines the virtual file system interface vfs::FileSystem.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
Implements a dense probed hash-table based set.
Diagnostic information for the sample profiler.
Represents either an error or a value T.
FunctionPass class - This class is used to implement most global optimizations.
Class to represent profile counts.
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
MIRProfileLoaderPass(std::string FileName="", std::string RemappingFileName="", FSDiscriminatorPass P=FSDiscriminatorPass::Pass1, IntrusiveRefCntPtr< vfs::FileSystem > FS=nullptr)
FS bits will only use the '1' bits in the Mask.
Definition MIRSampleProfile.cpp:348
MIRProfileLoader(StringRef Name, StringRef RemapName, IntrusiveRefCntPtr< vfs::FileSystem > FS)
Definition MIRSampleProfile.cpp:164
bool isValid() const
Definition MIRSampleProfile.cpp:172
void setBranchProbs(MachineFunction &F)
Definition MIRSampleProfile.cpp:204
ErrorOr< uint64_t > getInstWeight(const MachineInstr &MI) override
Definition MIRSampleProfile.cpp:191
bool runOnFunction(MachineFunction &F)
Definition MIRSampleProfile.cpp:313
unsigned HighBit
Definition MIRSampleProfile.cpp:188
MachineBlockFrequencyInfo * BFI
Hold the information of the basic block frequency.
Definition MIRSampleProfile.cpp:178
FSDiscriminatorPass P
PassNum is the sequence number this pass is called, start from 1.
Definition MIRSampleProfile.cpp:181
bool doInitialization(Module &M)
Definition MIRSampleProfile.cpp:287
unsigned LowBit
Definition MIRSampleProfile.cpp:185
friend class SampleCoverageTracker
Definition MIRSampleProfile.cpp:175
void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT, MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI, MachineOptimizationRemarkEmitter *MORE)
Definition MIRSampleProfile.cpp:148
bool ProfileIsValid
Definition MIRSampleProfile.cpp:190
void setFSPass(FSDiscriminatorPass Pass)
Definition MIRSampleProfile.cpp:157
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
succ_iterator succ_begin()
unsigned succ_size() const
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
LLVM_ABI DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
LLVM_ABI void view(const Twine &Name, bool isSimple=true) const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
LLVM_ABI const MachineBranchProbabilityInfo * getMBPI() const
LLVM_ABI void calculate(const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, const MachineLoopInfo &MLI)
calculate - compute block frequency info for the given function.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass(char &ID)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
Representation of each machine instruction.
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
A Module instance is used to store all the information related to an LLVM module.
Pass interface - Implemented by all 'passes'.
AnalysisType & getAnalysis() const
getAnalysis() - This function is used by subclasses to get to the analysis information ...
bool computeAndPropagateWeights(FunctionT &F, const DenseSet< GlobalValue::GUID > &InlinedGUIDs)
void computeDominanceAndLoopInfo(FunctionT &F)
IntrusiveRefCntPtr< vfs::FileSystem > FS
EdgeWeightMap EdgeWeights
unsigned getFunctionLoc(FunctionT &Func)
ErrorOr< uint64_t > getInstWeightImpl(const InstructionT &Inst)
EquivalenceClassMap EquivalenceClass
std::unique_ptr< SampleProfileReader > Reader
SampleProfileLoaderBaseImpl(std::string Name, std::string RemapName, IntrusiveRefCntPtr< vfs::FileSystem > FS)
std::unique_ptr< PseudoProbeManager > ProbeManager
PostDominatorTreePtrT PDT
virtual ErrorOr< uint64_t > getProbeWeight(const InstructionT &Inst)
std::string RemappingFilename
FunctionSamples * Samples
std::pair< const BasicBlockT *, const BasicBlockT * > Edge
void clearFunctionData(bool ResetDT=true)
BlockWeightMap BlockWeights
StringRef - Represent a constant reference to a string, i.e.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
A range adaptor for a pair of iterators.
static LLVM_ABI bool ProfileIsProbeBased
static LLVM_ABI ErrorOr< std::unique_ptr< SampleProfileReader > > create(StringRef Filename, LLVMContext &C, vfs::FileSystem &FS, FSDiscriminatorPass P=FSDiscriminatorPass::Base, StringRef RemapFilename="")
Create a sample profile reader appropriate to the file format.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
LLVM_ABI IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
This is an optimization pass for GlobalISel generic memory operations.
static unsigned getFSPassBitBegin(sampleprof::FSDiscriminatorPass P)
cl::opt< bool > ImprovedFSDiscriminator
LLVM_ABI char & MIRProfileLoaderPassID
This pass reads flow sensitive profile.
Definition MIRSampleProfile.cpp:79
static unsigned getFSPassBitEnd(sampleprof::FSDiscriminatorPass P)
cl::opt< std::string > ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden, cl::desc("The option to specify " "the name of the function " "whose CFG will be displayed."))
LLVM_ABI std::optional< PseudoProbe > extractProbe(const Instruction &Inst)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
cl::opt< GVDAGType > ViewBlockLayoutWithBFI("view-block-layout-with-bfi", cl::Hidden, cl::desc("Pop up a window to show a dag displaying MBP layout and associated " "block frequencies of the CFG."), cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " "fractional block frequency representation."), clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " "integer fractional block frequency representation."), clEnumValN(GVDT_Count, "count", "display a graph using the real " "profile count if available.")))
Function::ProfileCount ProfileCount
LLVM_ABI FunctionPass * createMIRProfileLoaderPass(std::string File, std::string RemappingFile, sampleprof::FSDiscriminatorPass P, IntrusiveRefCntPtr< vfs::FileSystem > FS)
Read Flow Sensitive Profile.
Definition MIRSampleProfile.cpp:82
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
MachineLoop LoopT
Definition MIRSampleProfile.cpp:121
MachinePostDominatorTree * PostDominatorTreePtrT
Definition MIRSampleProfile.cpp:124
MachineOptimizationRemarkEmitter OptRemarkEmitterT
Definition MIRSampleProfile.cpp:126
static PredRangeT getPredecessors(MachineBasicBlock *BB)
Definition MIRSampleProfile.cpp:136
MachineFunction FunctionT
Definition MIRSampleProfile.cpp:119
MachineOptimizationRemarkAnalysis OptRemarkAnalysisT
Definition MIRSampleProfile.cpp:127
MachineInstr InstructionT
Definition MIRSampleProfile.cpp:117
MachineDominatorTree * DominatorTreePtrT
Definition MIRSampleProfile.cpp:123
iterator_range< SmallVectorImpl< MachineBasicBlock * >::iterator > SuccRangeT
Definition MIRSampleProfile.cpp:130
static SuccRangeT getSuccessors(MachineBasicBlock *BB)
Definition MIRSampleProfile.cpp:139
MachineBlockFrequencyInfo BlockFrequencyInfoT
Definition MIRSampleProfile.cpp:120
static const MachineBasicBlock * getEntryBB(const MachineFunction *F)
Definition MIRSampleProfile.cpp:133
MachineLoopInfo * LoopInfoPtrT
Definition MIRSampleProfile.cpp:122
iterator_range< SmallVectorImpl< MachineBasicBlock * >::iterator > PredRangeT
Definition MIRSampleProfile.cpp:128
MachineBasicBlock BasicBlockT
Definition MIRSampleProfile.cpp:118
MachinePostDominatorTree PostDominatorTreeT
Definition MIRSampleProfile.cpp:125
static Function & getFunction(MachineFunction &F)
Definition MIRSampleProfile.cpp:132