LLVM 23.0.0git
MIRSampleProfile.cpp
Go to the documentation of this file.
1//===-------- MIRSampleProfile.cpp: MIRSampleFDO (For FSAFDO) -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides the implementation of the MIRSampleProfile loader, mainly
10// for flow sensitive SampleFDO.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/PseudoProbe.h"
31#include "llvm/Support/Debug.h"
36#include <optional>
37
38using namespace llvm;
39using namespace sampleprof;
40using namespace llvm::sampleprofutil;
41
42#define DEBUG_TYPE "fs-profile-loader"
43
45 "show-fs-branchprob", cl::Hidden, cl::init(false),
46 cl::desc("Print setting flow sensitive branch probabilities"));
48 "fs-profile-debug-prob-diff-threshold", cl::init(10),
50 "Only show debug message if the branch probability is greater than "
51 "this value (in percentage)."));
52
54 "fs-profile-debug-bw-threshold", cl::init(10000),
55 cl::desc("Only show debug message if the source branch weight is greater "
56 " than this value."));
57
58static cl::opt<bool> ViewBFIBefore("fs-viewbfi-before", cl::Hidden,
59 cl::init(false),
60 cl::desc("View BFI before MIR loader"));
61static cl::opt<bool> ViewBFIAfter("fs-viewbfi-after", cl::Hidden,
62 cl::init(false),
63 cl::desc("View BFI after MIR loader"));
64
66
68 "Load MIR Sample Profile",
69 /* cfg = */ false, /* is_analysis = */ false)
76 /* cfg = */ false, /* is_analysis = */ false)
77
79
81llvm::createMIRProfileLoaderPass(std::string File, std::string RemappingFile,
83 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
84 return new MIRProfileLoaderPass(File, RemappingFile, P, std::move(FS));
85}
86
87namespace llvm {
88
89// Internal option used to control BFI display only after MBP pass.
90// Defined in CodeGen/MachineBlockFrequencyInfo.cpp:
91// -view-block-layout-with-bfi={none | fraction | integer | count}
93
94// Command line option to specify the name of the function for CFG dump
95// Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
97
98std::optional<PseudoProbe> extractProbe(const MachineInstr &MI) {
99 if (MI.isPseudoProbe()) {
100 PseudoProbe Probe;
101 Probe.Id = MI.getOperand(1).getImm();
102 Probe.Type = MI.getOperand(2).getImm();
103 Probe.Attr = MI.getOperand(3).getImm();
104 Probe.Factor = 1;
105 DILocation *DebugLoc = MI.getDebugLoc();
106 Probe.Discriminator = DebugLoc ? DebugLoc->getDiscriminator() : 0;
107 return Probe;
108 }
109
110 // Ignore callsite probes since they do not have FS discriminators.
111 return std::nullopt;
112}
113
114namespace afdo_detail {
142} // namespace afdo_detail
143
145 : public SampleProfileLoaderBaseImpl<MachineFunction> {
146public:
150 DT = MDT;
151 PDT = MPDT;
152 LI = MLI;
153 BFI = MBFI;
154 ORE = MORE;
155 }
157 P = Pass;
160 assert(LowBit < HighBit && "HighBit needs to be greater than Lowbit");
161 }
162
165 : SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName),
166 std::move(FS)) {}
167
170 bool doInitialization(Module &M);
171 bool isValid() const { return ProfileIsValid; }
172
173protected:
175
176 /// Hold the information of the basic block frequency.
178
179 /// PassNum is the sequence number this pass is called, start from 1.
181
182 // LowBit in the FS discriminator used by this instance. Note the number is
183 // 0-based. Base discrimnator use bit 0 to bit 11.
184 unsigned LowBit;
185 // HighwBit in the FS discriminator used by this instance. Note the number
186 // is 0-based.
187 unsigned HighBit;
188
189 bool ProfileIsValid = true;
192 return getProbeWeight(MI);
193 if (ImprovedFSDiscriminator && MI.isMetaInstruction())
194 return std::error_code();
195 return getInstWeightImpl(MI);
196 }
197};
198
199template <>
202
204 LLVM_DEBUG(dbgs() << "\nPropagation complete. Setting branch probs\n");
205 for (auto &BI : F) {
206 MachineBasicBlock *BB = &BI;
207 if (BB->succ_size() < 2)
208 continue;
209 const MachineBasicBlock *EC = EquivalenceClass[BB];
210 uint64_t BBWeight = BlockWeights[EC];
211 uint64_t SumEdgeWeight = 0;
212 for (MachineBasicBlock *Succ : BB->successors()) {
213 Edge E = std::make_pair(BB, Succ);
214 SumEdgeWeight += EdgeWeights[E];
215 }
216
217 if (BBWeight != SumEdgeWeight) {
218 LLVM_DEBUG(dbgs() << "BBweight is not equal to SumEdgeWeight: BBWWeight="
219 << BBWeight << " SumEdgeWeight= " << SumEdgeWeight
220 << "\n");
221 BBWeight = SumEdgeWeight;
222 }
223 if (BBWeight == 0) {
224 LLVM_DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n");
225 continue;
226 }
227
228#ifndef NDEBUG
229 uint64_t BBWeightOrig = BBWeight;
230#endif
231 uint32_t MaxWeight = std::numeric_limits<uint32_t>::max();
232 uint32_t Factor = 1;
233 if (BBWeight > MaxWeight) {
234 Factor = BBWeight / MaxWeight + 1;
235 BBWeight /= Factor;
236 LLVM_DEBUG(dbgs() << "Scaling weights by " << Factor << "\n");
237 }
238
240 SE = BB->succ_end();
241 SI != SE; ++SI) {
242 MachineBasicBlock *Succ = *SI;
243 Edge E = std::make_pair(BB, Succ);
244 uint64_t EdgeWeight = EdgeWeights[E];
245 EdgeWeight /= Factor;
246
247 assert(BBWeight >= EdgeWeight &&
248 "BBweight is larger than EdgeWeight -- should not happen.\n");
249
251 BranchProbability NewProb(EdgeWeight, BBWeight);
252 if (OldProb == NewProb)
253 continue;
254 BB->setSuccProbability(SI, NewProb);
255#ifndef NDEBUG
256 if (!ShowFSBranchProb)
257 continue;
258 bool Show = false;
260 if (OldProb > NewProb)
261 Diff = OldProb - NewProb;
262 else
263 Diff = NewProb - OldProb;
265 Show &= (BBWeightOrig >= FSProfileDebugBWThreshold);
266
267 auto DIL = BB->findBranchDebugLoc();
268 auto SuccDIL = Succ->findBranchDebugLoc();
269 if (Show) {
270 dbgs() << "Set branch fs prob: MBB (" << BB->getNumber() << " -> "
271 << Succ->getNumber() << "): ";
272 if (DIL)
273 dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
274 << DIL->getColumn();
275 if (SuccDIL)
276 dbgs() << "-->" << SuccDIL->getFilename() << ":" << SuccDIL->getLine()
277 << ":" << SuccDIL->getColumn();
278 dbgs() << " W=" << BBWeightOrig << " " << OldProb << " --> " << NewProb
279 << "\n";
280 }
281#endif
282 }
283 }
284}
285
287 auto &Ctx = M.getContext();
288
290 Filename, Ctx, *FS, P, RemappingFilename);
291 if (std::error_code EC = ReaderOrErr.getError()) {
292 std::string Msg = "Could not open profile: " + EC.message();
293 Ctx.diagnose(DiagnosticInfoSampleProfile(Filename, Msg));
294 return false;
295 }
296
297 Reader = std::move(ReaderOrErr.get());
298 Reader->setModule(&M);
300
301 // Load pseudo probe descriptors for probe-based function samples.
302 if (Reader->profileIsProbeBased()) {
303 ProbeManager = std::make_unique<PseudoProbeManager>(M);
304 if (!ProbeManager->moduleIsProbed(M)) {
305 return false;
306 }
307 }
308
309 return true;
310}
311
313 // Do not load non-FS profiles. A line or probe can get a zero-valued
314 // discriminator at certain pass which could result in accidentally loading
315 // the corresponding base counter in the non-FS profile, while a non-zero
316 // discriminator would end up getting zero samples. This could in turn undo
317 // the sample distribution effort done by previous BFI maintenance and the
318 // probe distribution factor work for pseudo probes.
319 if (!Reader->profileIsFS())
320 return false;
321
322 Function &Func = MF.getFunction();
323 clearFunctionData(false);
324 Samples = Reader->getSamplesFor(Func);
325 if (!Samples || Samples->empty())
326 return false;
327
329 if (!ProbeManager->profileIsValid(MF.getFunction(), *Samples))
330 return false;
331 } else {
332 if (getFunctionLoc(MF) == 0)
333 return false;
334 }
335
336 DenseSet<GlobalValue::GUID> InlinedGUIDs;
337 bool Changed = computeAndPropagateWeights(MF, InlinedGUIDs);
338
339 // Set the new BPI, BFI.
340 setBranchProbs(MF);
341
342 return Changed;
343}
344
345} // namespace llvm
346
348 std::string FileName, std::string RemappingFileName, FSDiscriminatorPass P,
350 : MachineFunctionPass(ID), ProfileFileName(FileName), P(P) {
351 LowBit = getFSPassBitBegin(P);
352 HighBit = getFSPassBitEnd(P);
353
354 auto VFS = FS ? std::move(FS) : vfs::getRealFileSystem();
355 MIRSampleLoader = std::make_unique<MIRProfileLoader>(
356 FileName, RemappingFileName, std::move(VFS));
357 assert(LowBit < HighBit && "HighBit needs to be greater than Lowbit");
358}
359
360bool MIRProfileLoaderPass::runOnMachineFunction(MachineFunction &MF) {
361 if (!MIRSampleLoader->isValid())
362 return false;
363
364 LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Func: "
365 << MF.getFunction().getName() << "\n");
367 auto *MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
368 auto *MPDT =
370
371 MF.RenumberBlocks();
372
373 MIRSampleLoader->setInitVals(
374 MDT, MPDT, &getAnalysis<MachineLoopInfoWrapperPass>().getLI(), MBFI,
376
378 (ViewBlockFreqFuncName.empty() ||
380 MBFI->view("MIR_Prof_loader_b." + MF.getName(), false);
381 }
382
383 bool Changed = MIRSampleLoader->runOnFunction(MF);
384 if (Changed)
385 MBFI->calculate(MF, *MBFI->getMBPI(),
387
389 (ViewBlockFreqFuncName.empty() ||
391 MBFI->view("MIR_prof_loader_a." + MF.getName(), false);
392 }
393
394 return Changed;
395}
396
397bool MIRProfileLoaderPass::doInitialization(Module &M) {
398 LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Module " << M.getName()
399 << "\n");
400
401 MIRSampleLoader->setFSPass(P);
402 return MIRSampleLoader->doInitialization(M);
403}
404
405void MIRProfileLoaderPass::getAnalysisUsage(AnalysisUsage &AU) const {
406 AU.setPreservesAll();
407 AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
408 AU.addRequired<MachineDominatorTreeWrapperPass>();
409 AU.addRequired<MachinePostDominatorTreeWrapperPass>();
410 AU.addRequiredTransitive<MachineLoopInfoWrapperPass>();
411 AU.addRequired<MachineOptimizationRemarkEmitterPass>();
413}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
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"))
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
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.
#define LLVM_DEBUG(...)
Definition Debug.h:119
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()
A debug info location.
Definition DebugLoc.h:126
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
Diagnostic information for the sample profiler.
Represents either an error or a value T.
Definition ErrorOr.h:56
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
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.
MIRProfileLoader(StringRef Name, StringRef RemapName, IntrusiveRefCntPtr< vfs::FileSystem > FS)
void setBranchProbs(MachineFunction &F)
ErrorOr< uint64_t > getInstWeight(const MachineInstr &MI) override
bool runOnFunction(MachineFunction &F)
MachineBlockFrequencyInfo * BFI
Hold the information of the basic block frequency.
FSDiscriminatorPass P
PassNum is the sequence number this pass is called, start from 1.
bool doInitialization(Module &M)
void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT, MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI, MachineOptimizationRemarkEmitter *MORE)
void setFSPass(FSDiscriminatorPass Pass)
LLVM_ABI BranchProbability getSuccProbability(const_succ_iterator Succ) const
Return probability of the edge from this block to MBB.
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.
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...
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.
Diagnostic information for optimization analysis remarks.
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.
Definition Module.h:67
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - 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)
ErrorOr< uint64_t > getInstWeightImpl(const InstructionT &Inst)
SampleProfileLoaderBaseImpl(std::string Name, std::string RemapName, IntrusiveRefCntPtr< vfs::FileSystem > FS)
virtual ErrorOr< uint64_t > getProbeWeight(const InstructionT &Inst)
std::pair< const BasicBlockT *, const BasicBlockT * > Edge
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
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.
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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)
LLVM_ABI cl::opt< bool > ImprovedFSDiscriminator
LLVM_ABI char & MIRProfileLoaderPassID
This pass reads flow sensitive profile.
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.
Definition Debug.cpp:209
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.")))
LLVM_ABI FunctionPass * createMIRProfileLoaderPass(std::string File, std::string RemappingFile, sampleprof::FSDiscriminatorPass P, IntrusiveRefCntPtr< vfs::FileSystem > FS)
Read Flow Sensitive Profile.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
#define MORE()
Definition regcomp.c:246
uint32_t Discriminator
MachineOptimizationRemarkEmitter OptRemarkEmitterT
static PredRangeT getPredecessors(MachineBasicBlock *BB)
MachineOptimizationRemarkAnalysis OptRemarkAnalysisT
iterator_range< SmallVectorImpl< MachineBasicBlock * >::iterator > SuccRangeT
static SuccRangeT getSuccessors(MachineBasicBlock *BB)
static const MachineBasicBlock * getEntryBB(const MachineFunction *F)
iterator_range< SmallVectorImpl< MachineBasicBlock * >::iterator > PredRangeT
static Function & getFunction(MachineFunction &F)