xref: /llvm-project/bolt/lib/Profile/YAMLProfileWriter.cpp (revision 97025bd9d5b32f984f07d6ae20a3ce6ddb3fbe2a)
1 //===- bolt/Profile/YAMLProfileWriter.cpp - YAML profile serializer -------===//
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 #include "bolt/Profile/YAMLProfileWriter.h"
10 #include "bolt/Core/BinaryBasicBlock.h"
11 #include "bolt/Core/BinaryFunction.h"
12 #include "bolt/Profile/BoltAddressTranslation.h"
13 #include "bolt/Profile/DataAggregator.h"
14 #include "bolt/Profile/ProfileReaderBase.h"
15 #include "bolt/Rewrite/RewriteInstance.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 #undef  DEBUG_TYPE
21 #define DEBUG_TYPE "bolt-prof"
22 
23 namespace opts {
24 extern llvm::cl::opt<bool> ProfileUseDFS;
25 } // namespace opts
26 
27 namespace llvm {
28 namespace bolt {
29 
30 const BinaryFunction *YAMLProfileWriter::setCSIDestination(
31     const BinaryContext &BC, yaml::bolt::CallSiteInfo &CSI,
32     const MCSymbol *Symbol, const BoltAddressTranslation *BAT,
33     uint32_t Offset) {
34   CSI.DestId = 0; // designated for unknown functions
35   CSI.EntryDiscriminator = 0;
36 
37   if (Symbol) {
38     uint64_t EntryID = 0;
39     if (const BinaryFunction *Callee =
40             BC.getFunctionForSymbol(Symbol, &EntryID)) {
41       if (BAT && BAT->isBATFunction(Callee->getAddress()))
42         std::tie(Callee, EntryID) = BAT->translateSymbol(BC, *Symbol, Offset);
43       else if (const BinaryBasicBlock *BB =
44                    Callee->getBasicBlockContainingOffset(Offset))
45         BC.getFunctionForSymbol(Callee->getSecondaryEntryPointSymbol(*BB),
46                                 &EntryID);
47       CSI.DestId = Callee->getFunctionNumber();
48       CSI.EntryDiscriminator = EntryID;
49       return Callee;
50     }
51   }
52   return nullptr;
53 }
54 
55 yaml::bolt::BinaryFunctionProfile
56 YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
57                            const BoltAddressTranslation *BAT) {
58   yaml::bolt::BinaryFunctionProfile YamlBF;
59   const BinaryContext &BC = BF.getBinaryContext();
60 
61   const uint16_t LBRProfile = BF.getProfileFlags() & BinaryFunction::PF_LBR;
62 
63   // Prepare function and block hashes
64   BF.computeHash(UseDFS);
65   BF.computeBlockHashes();
66 
67   YamlBF.Name = DataAggregator::getLocationName(BF, BAT);
68   YamlBF.Id = BF.getFunctionNumber();
69   YamlBF.Hash = BF.getHash();
70   YamlBF.NumBasicBlocks = BF.size();
71   YamlBF.ExecCount = BF.getKnownExecutionCount();
72 
73   BinaryFunction::BasicBlockOrderType Order;
74   llvm::copy(UseDFS ? BF.dfs() : BF.getLayout().blocks(),
75              std::back_inserter(Order));
76 
77   for (const BinaryBasicBlock *BB : Order) {
78     yaml::bolt::BinaryBasicBlockProfile YamlBB;
79     YamlBB.Index = BB->getLayoutIndex();
80     YamlBB.NumInstructions = BB->getNumNonPseudos();
81     YamlBB.Hash = BB->getHash();
82 
83     if (!LBRProfile) {
84       YamlBB.EventCount = BB->getKnownExecutionCount();
85       if (YamlBB.EventCount)
86         YamlBF.Blocks.emplace_back(YamlBB);
87       continue;
88     }
89 
90     YamlBB.ExecCount = BB->getKnownExecutionCount();
91 
92     for (const MCInst &Instr : *BB) {
93       if (!BC.MIB->isCall(Instr) && !BC.MIB->isIndirectBranch(Instr))
94         continue;
95 
96       SmallVector<std::pair<StringRef, yaml::bolt::CallSiteInfo>> CSTargets;
97       yaml::bolt::CallSiteInfo CSI;
98       std::optional<uint32_t> Offset = BC.MIB->getOffset(Instr);
99       if (!Offset || *Offset < BB->getInputOffset())
100         continue;
101       CSI.Offset = *Offset - BB->getInputOffset();
102 
103       if (BC.MIB->isIndirectCall(Instr) || BC.MIB->isIndirectBranch(Instr)) {
104         const auto ICSP = BC.MIB->tryGetAnnotationAs<IndirectCallSiteProfile>(
105             Instr, "CallProfile");
106         if (!ICSP)
107           continue;
108         for (const IndirectCallProfile &CSP : ICSP.get()) {
109           StringRef TargetName = "";
110           const BinaryFunction *Callee =
111               setCSIDestination(BC, CSI, CSP.Symbol, BAT);
112           if (Callee)
113             TargetName = Callee->getOneName();
114           CSI.Count = CSP.Count;
115           CSI.Mispreds = CSP.Mispreds;
116           CSTargets.emplace_back(TargetName, CSI);
117         }
118       } else { // direct call or a tail call
119         StringRef TargetName = "";
120         const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(Instr);
121         const BinaryFunction *const Callee =
122             setCSIDestination(BC, CSI, CalleeSymbol, BAT);
123         if (Callee)
124           TargetName = Callee->getOneName();
125 
126         auto getAnnotationWithDefault = [&](const MCInst &Inst, StringRef Ann) {
127           return BC.MIB->getAnnotationWithDefault(Instr, Ann, 0ull);
128         };
129         if (BC.MIB->getConditionalTailCall(Instr)) {
130           CSI.Count = getAnnotationWithDefault(Instr, "CTCTakenCount");
131           CSI.Mispreds = getAnnotationWithDefault(Instr, "CTCMispredCount");
132         } else {
133           CSI.Count = getAnnotationWithDefault(Instr, "Count");
134         }
135 
136         if (CSI.Count)
137           CSTargets.emplace_back(TargetName, CSI);
138       }
139       // Sort targets in a similar way to getBranchData, see Location::operator<
140       llvm::sort(CSTargets, [](const auto &RHS, const auto &LHS) {
141         if (RHS.first != LHS.first)
142           return RHS.first < LHS.first;
143         return RHS.second.Offset < LHS.second.Offset;
144       });
145       for (auto &KV : CSTargets)
146         YamlBB.CallSites.push_back(KV.second);
147     }
148 
149     // Skip printing if there's no profile data for non-entry basic block.
150     // Include landing pads with non-zero execution count.
151     if (YamlBB.CallSites.empty() && !BB->isEntryPoint() &&
152         !(BB->isLandingPad() && BB->getKnownExecutionCount() != 0)) {
153       // Include blocks having successors or predecessors with positive counts.
154       uint64_t SuccessorExecCount = 0;
155       for (const BinaryBasicBlock::BinaryBranchInfo &BranchInfo :
156            BB->branch_info())
157         SuccessorExecCount += BranchInfo.Count;
158       uint64_t PredecessorExecCount = 0;
159       for (auto Pred : BB->predecessors())
160         PredecessorExecCount += Pred->getBranchInfo(*BB).Count;
161       if (!SuccessorExecCount && !PredecessorExecCount)
162         continue;
163     }
164 
165     auto BranchInfo = BB->branch_info_begin();
166     for (const BinaryBasicBlock *Successor : BB->successors()) {
167       yaml::bolt::SuccessorInfo YamlSI;
168       YamlSI.Index = Successor->getLayoutIndex();
169       YamlSI.Count = BranchInfo->Count;
170       YamlSI.Mispreds = BranchInfo->MispredictedCount;
171 
172       YamlBB.Successors.emplace_back(YamlSI);
173 
174       ++BranchInfo;
175     }
176 
177     YamlBF.Blocks.emplace_back(YamlBB);
178   }
179   return YamlBF;
180 }
181 
182 std::error_code YAMLProfileWriter::writeProfile(const RewriteInstance &RI) {
183   const BinaryContext &BC = RI.getBinaryContext();
184   const auto &Functions = BC.getBinaryFunctions();
185 
186   std::error_code EC;
187   OS = std::make_unique<raw_fd_ostream>(Filename, EC, sys::fs::OF_None);
188   if (EC) {
189     errs() << "BOLT-WARNING: " << EC.message() << " : unable to open "
190            << Filename << " for output.\n";
191     return EC;
192   }
193 
194   yaml::bolt::BinaryProfile BP;
195 
196   // Fill out the header info.
197   BP.Header.Version = 1;
198   BP.Header.FileName = std::string(BC.getFilename());
199   std::optional<StringRef> BuildID = BC.getFileBuildID();
200   BP.Header.Id = BuildID ? std::string(*BuildID) : "<unknown>";
201   BP.Header.Origin = std::string(RI.getProfileReader()->getReaderName());
202   BP.Header.IsDFSOrder = opts::ProfileUseDFS;
203   BP.Header.HashFunction = HashFunction::Default;
204 
205   StringSet<> EventNames = RI.getProfileReader()->getEventNames();
206   if (!EventNames.empty()) {
207     std::string Sep;
208     for (const StringMapEntry<std::nullopt_t> &EventEntry : EventNames) {
209       BP.Header.EventNames += Sep + EventEntry.first().str();
210       Sep = ",";
211     }
212   }
213 
214   // Make sure the profile is consistent across all functions.
215   uint16_t ProfileFlags = BinaryFunction::PF_NONE;
216   for (const auto &BFI : Functions) {
217     const BinaryFunction &BF = BFI.second;
218     if (BF.hasProfile() && !BF.empty()) {
219       assert(BF.getProfileFlags() != BinaryFunction::PF_NONE);
220       if (ProfileFlags == BinaryFunction::PF_NONE)
221         ProfileFlags = BF.getProfileFlags();
222 
223       assert(BF.getProfileFlags() == ProfileFlags &&
224              "expected consistent profile flags across all functions");
225     }
226   }
227   BP.Header.Flags = ProfileFlags;
228 
229   // Add all function objects.
230   for (const auto &BFI : Functions) {
231     const BinaryFunction &BF = BFI.second;
232     if (BF.hasProfile()) {
233       if (!BF.hasValidProfile() && !RI.getProfileReader()->isTrustedSource())
234         continue;
235 
236       BP.Functions.emplace_back(convert(BF, opts::ProfileUseDFS));
237     }
238   }
239 
240   // Write the profile.
241   yaml::Output Out(*OS, nullptr, 0);
242   Out << BP;
243 
244   return std::error_code();
245 }
246 
247 } // namespace bolt
248 } // namespace llvm
249