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