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 
14 #include "llvm/CodeGen/MIRSampleProfile.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h"
23 #include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
24 
25 using namespace llvm;
26 using namespace sampleprof;
27 using namespace llvm::sampleprofutil;
28 using ProfileCount = Function::ProfileCount;
29 
30 #define DEBUG_TYPE "fs-profile-loader"
31 
32 static cl::opt<bool> ShowFSBranchProb(
33     "show-fs-branchprob", cl::Hidden, cl::init(false),
34     cl::desc("Print setting flow sensitive branch probabilities"));
35 static cl::opt<unsigned> FSProfileDebugProbDiffThreshold(
36     "fs-profile-debug-prob-diff-threshold", cl::init(10),
37     cl::desc("Only show debug message if the branch probility is greater than "
38              "this value (in percentage)."));
39 
40 static cl::opt<unsigned> FSProfileDebugBWThreshold(
41     "fs-profile-debug-bw-threshold", cl::init(10000),
42     cl::desc("Only show debug message if the source branch weight is greater "
43              " than this value."));
44 
45 static cl::opt<bool> ViewBFIBefore("fs-viewbfi-before", cl::Hidden,
46                                    cl::init(false),
47                                    cl::desc("View BFI before MIR loader"));
48 static cl::opt<bool> ViewBFIAfter("fs-viewbfi-after", cl::Hidden,
49                                   cl::init(false),
50                                   cl::desc("View BFI after MIR loader"));
51 
52 char MIRProfileLoaderPass::ID = 0;
53 
54 INITIALIZE_PASS_BEGIN(MIRProfileLoaderPass, DEBUG_TYPE,
55                       "Load MIR Sample Profile",
56                       /* cfg = */ false, /* is_analysis = */ false)
57 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
58 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
59 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
60 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
61 INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
62 INITIALIZE_PASS_END(MIRProfileLoaderPass, DEBUG_TYPE, "Load MIR Sample Profile",
63                     /* cfg = */ false, /* is_analysis = */ false)
64 
65 char &llvm::MIRProfileLoaderPassID = MIRProfileLoaderPass::ID;
66 
67 FunctionPass *llvm::createMIRProfileLoaderPass(std::string File,
68                                                std::string RemappingFile,
69                                                FSDiscriminatorPass P) {
70   return new MIRProfileLoaderPass(File, RemappingFile, P);
71 }
72 
73 namespace llvm {
74 
75 // Internal option used to control BFI display only after MBP pass.
76 // Defined in CodeGen/MachineBlockFrequencyInfo.cpp:
77 // -view-block-layout-with-bfi={none | fraction | integer | count}
78 extern cl::opt<GVDAGType> ViewBlockLayoutWithBFI;
79 
80 // Command line option to specify the name of the function for CFG dump
81 // Defined in Analysis/BlockFrequencyInfo.cpp:  -view-bfi-func-name=
82 extern cl::opt<std::string> ViewBlockFreqFuncName;
83 
84 namespace afdo_detail {
85 template <> struct IRTraits<MachineBasicBlock> {
86   using InstructionT = MachineInstr;
87   using BasicBlockT = MachineBasicBlock;
88   using FunctionT = MachineFunction;
89   using BlockFrequencyInfoT = MachineBlockFrequencyInfo;
90   using LoopT = MachineLoop;
91   using LoopInfoPtrT = MachineLoopInfo *;
92   using DominatorTreePtrT = MachineDominatorTree *;
93   using PostDominatorTreePtrT = MachinePostDominatorTree *;
94   using PostDominatorTreeT = MachinePostDominatorTree;
95   using OptRemarkEmitterT = MachineOptimizationRemarkEmitter;
96   using OptRemarkAnalysisT = MachineOptimizationRemarkAnalysis;
97   using PredRangeT = iterator_range<std::vector<MachineBasicBlock *>::iterator>;
98   using SuccRangeT = iterator_range<std::vector<MachineBasicBlock *>::iterator>;
99   static Function &getFunction(MachineFunction &F) { return F.getFunction(); }
100   static const MachineBasicBlock *getEntryBB(const MachineFunction *F) {
101     return GraphTraits<const MachineFunction *>::getEntryNode(F);
102   }
103   static PredRangeT getPredecessors(MachineBasicBlock *BB) {
104     return BB->predecessors();
105   }
106   static SuccRangeT getSuccessors(MachineBasicBlock *BB) {
107     return BB->successors();
108   }
109 };
110 } // namespace afdo_detail
111 
112 class MIRProfileLoader final
113     : public SampleProfileLoaderBaseImpl<MachineBasicBlock> {
114 public:
115   void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT,
116                    MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI,
117                    MachineOptimizationRemarkEmitter *MORE) {
118     DT = MDT;
119     PDT = MPDT;
120     LI = MLI;
121     BFI = MBFI;
122     ORE = MORE;
123   }
124   void setFSPass(FSDiscriminatorPass Pass) {
125     P = Pass;
126     LowBit = getFSPassBitBegin(P);
127     HighBit = getFSPassBitEnd(P);
128     assert(LowBit < HighBit && "HighBit needs to be greater than Lowbit");
129   }
130 
131   MIRProfileLoader(StringRef Name, StringRef RemapName)
132       : SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName)) {
133   }
134 
135   void setBranchProbs(MachineFunction &F);
136   bool runOnFunction(MachineFunction &F);
137   bool doInitialization(Module &M);
138   bool isValid() const { return ProfileIsValid; }
139 
140 protected:
141   friend class SampleCoverageTracker;
142 
143   /// Hold the information of the basic block frequency.
144   MachineBlockFrequencyInfo *BFI;
145 
146   /// PassNum is the sequence number this pass is called, start from 1.
147   FSDiscriminatorPass P;
148 
149   // LowBit in the FS discriminator used by this instance. Note the number is
150   // 0-based. Base discrimnator use bit 0 to bit 11.
151   unsigned LowBit;
152   // HighwBit in the FS discriminator used by this instance. Note the number
153   // is 0-based.
154   unsigned HighBit;
155 
156   bool ProfileIsValid = true;
157 };
158 
159 template <>
160 void SampleProfileLoaderBaseImpl<
161     MachineBasicBlock>::computeDominanceAndLoopInfo(MachineFunction &F) {}
162 
163 void MIRProfileLoader::setBranchProbs(MachineFunction &F) {
164   LLVM_DEBUG(dbgs() << "\nPropagation complete. Setting branch probs\n");
165   for (auto &BI : F) {
166     MachineBasicBlock *BB = &BI;
167     if (BB->succ_size() < 2)
168       continue;
169     const MachineBasicBlock *EC = EquivalenceClass[BB];
170     uint64_t BBWeight = BlockWeights[EC];
171     uint64_t SumEdgeWeight = 0;
172     for (MachineBasicBlock *Succ : BB->successors()) {
173       Edge E = std::make_pair(BB, Succ);
174       SumEdgeWeight += EdgeWeights[E];
175     }
176 
177     if (BBWeight != SumEdgeWeight) {
178       LLVM_DEBUG(dbgs() << "BBweight is not equal to SumEdgeWeight: BBWWeight="
179                         << BBWeight << " SumEdgeWeight= " << SumEdgeWeight
180                         << "\n");
181       BBWeight = SumEdgeWeight;
182     }
183     if (BBWeight == 0) {
184       LLVM_DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n");
185       continue;
186     }
187 
188 #ifndef NDEBUG
189     uint64_t BBWeightOrig = BBWeight;
190 #endif
191     uint32_t MaxWeight = std::numeric_limits<uint32_t>::max();
192     uint32_t Factor = 1;
193     if (BBWeight > MaxWeight) {
194       Factor = BBWeight / MaxWeight + 1;
195       BBWeight /= Factor;
196       LLVM_DEBUG(dbgs() << "Scaling weights by " << Factor << "\n");
197     }
198 
199     for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
200                                           SE = BB->succ_end();
201          SI != SE; ++SI) {
202       MachineBasicBlock *Succ = *SI;
203       Edge E = std::make_pair(BB, Succ);
204       uint64_t EdgeWeight = EdgeWeights[E];
205       EdgeWeight /= Factor;
206 
207       assert(BBWeight >= EdgeWeight &&
208              "BBweight is larger than EdgeWeight -- should not happen.\n");
209 
210       BranchProbability OldProb = BFI->getMBPI()->getEdgeProbability(BB, SI);
211       BranchProbability NewProb(EdgeWeight, BBWeight);
212       if (OldProb == NewProb)
213         continue;
214       BB->setSuccProbability(SI, NewProb);
215 #ifndef NDEBUG
216       if (!ShowFSBranchProb)
217         continue;
218       bool Show = false;
219       BranchProbability Diff;
220       if (OldProb > NewProb)
221         Diff = OldProb - NewProb;
222       else
223         Diff = NewProb - OldProb;
224       Show = (Diff >= BranchProbability(FSProfileDebugProbDiffThreshold, 100));
225       Show &= (BBWeightOrig >= FSProfileDebugBWThreshold);
226 
227       auto DIL = BB->findBranchDebugLoc();
228       auto SuccDIL = Succ->findBranchDebugLoc();
229       if (Show) {
230         dbgs() << "Set branch fs prob: MBB (" << BB->getNumber() << " -> "
231                << Succ->getNumber() << "): ";
232         if (DIL)
233           dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
234                  << DIL->getColumn();
235         if (SuccDIL)
236           dbgs() << "-->" << SuccDIL->getFilename() << ":" << SuccDIL->getLine()
237                  << ":" << SuccDIL->getColumn();
238         dbgs() << " W=" << BBWeightOrig << "  " << OldProb << " --> " << NewProb
239                << "\n";
240       }
241 #endif
242     }
243   }
244 }
245 
246 bool MIRProfileLoader::doInitialization(Module &M) {
247   auto &Ctx = M.getContext();
248 
249   auto ReaderOrErr = sampleprof::SampleProfileReader::create(Filename, Ctx, P,
250                                                              RemappingFilename);
251   if (std::error_code EC = ReaderOrErr.getError()) {
252     std::string Msg = "Could not open profile: " + EC.message();
253     Ctx.diagnose(DiagnosticInfoSampleProfile(Filename, Msg));
254     return false;
255   }
256 
257   Reader = std::move(ReaderOrErr.get());
258   Reader->setModule(&M);
259   ProfileIsValid = (Reader->read() == sampleprof_error::success);
260   Reader->getSummary();
261 
262   return true;
263 }
264 
265 bool MIRProfileLoader::runOnFunction(MachineFunction &MF) {
266   Function &Func = MF.getFunction();
267   clearFunctionData(false);
268   Samples = Reader->getSamplesFor(Func);
269   if (!Samples || Samples->empty())
270     return false;
271 
272   if (getFunctionLoc(MF) == 0)
273     return false;
274 
275   DenseSet<GlobalValue::GUID> InlinedGUIDs;
276   bool Changed = computeAndPropagateWeights(MF, InlinedGUIDs);
277 
278   // Set the new BPI, BFI.
279   setBranchProbs(MF);
280 
281   return Changed;
282 }
283 
284 } // namespace llvm
285 
286 MIRProfileLoaderPass::MIRProfileLoaderPass(std::string FileName,
287                                            std::string RemappingFileName,
288                                            FSDiscriminatorPass P)
289     : MachineFunctionPass(ID), ProfileFileName(FileName), P(P),
290       MIRSampleLoader(
291           std::make_unique<MIRProfileLoader>(FileName, RemappingFileName)) {
292   LowBit = getFSPassBitBegin(P);
293   HighBit = getFSPassBitEnd(P);
294   assert(LowBit < HighBit && "HighBit needs to be greater than Lowbit");
295 }
296 
297 bool MIRProfileLoaderPass::runOnMachineFunction(MachineFunction &MF) {
298   if (!MIRSampleLoader->isValid())
299     return false;
300 
301   LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Func: "
302                     << MF.getFunction().getName() << "\n");
303   MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
304   MIRSampleLoader->setInitVals(
305       &getAnalysis<MachineDominatorTree>(),
306       &getAnalysis<MachinePostDominatorTree>(), &getAnalysis<MachineLoopInfo>(),
307       MBFI, &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE());
308 
309   MF.RenumberBlocks();
310   if (ViewBFIBefore && ViewBlockLayoutWithBFI != GVDT_None &&
311       (ViewBlockFreqFuncName.empty() ||
312        MF.getFunction().getName().equals(ViewBlockFreqFuncName))) {
313     MBFI->view("MIR_Prof_loader_b." + MF.getName(), false);
314   }
315 
316   bool Changed = MIRSampleLoader->runOnFunction(MF);
317   if (Changed)
318     MBFI->calculate(MF, *MBFI->getMBPI(), *&getAnalysis<MachineLoopInfo>());
319 
320   if (ViewBFIAfter && ViewBlockLayoutWithBFI != GVDT_None &&
321       (ViewBlockFreqFuncName.empty() ||
322        MF.getFunction().getName().equals(ViewBlockFreqFuncName))) {
323     MBFI->view("MIR_prof_loader_a." + MF.getName(), false);
324   }
325 
326   return Changed;
327 }
328 
329 bool MIRProfileLoaderPass::doInitialization(Module &M) {
330   LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Module " << M.getName()
331                     << "\n");
332 
333   MIRSampleLoader->setFSPass(P);
334   return MIRSampleLoader->doInitialization(M);
335 }
336 
337 void MIRProfileLoaderPass::getAnalysisUsage(AnalysisUsage &AU) const {
338   AU.setPreservesAll();
339   AU.addRequired<MachineBlockFrequencyInfo>();
340   AU.addRequired<MachineDominatorTree>();
341   AU.addRequired<MachinePostDominatorTree>();
342   AU.addRequiredTransitive<MachineLoopInfo>();
343   AU.addRequired<MachineOptimizationRemarkEmitterPass>();
344   MachineFunctionPass::getAnalysisUsage(AU);
345 }
346