1 //===- SampleProfWriter.h - Write LLVM sample profile data ------*- C++ -*-===//
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 contains definitions needed for writing sample profiles.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
13 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
14 
15 #include "llvm/ADT/MapVector.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/ProfileSummary.h"
19 #include "llvm/ProfileData/SampleProf.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <algorithm>
23 #include <cstdint>
24 #include <memory>
25 #include <set>
26 #include <system_error>
27 
28 namespace llvm {
29 namespace sampleprof {
30 
31 /// Sample-based profile writer. Base class.
32 class SampleProfileWriter {
33 public:
34   virtual ~SampleProfileWriter() = default;
35 
36   /// Write sample profiles in \p S.
37   ///
38   /// \returns status code of the file update operation.
39   virtual std::error_code writeSample(const FunctionSamples &S) = 0;
40 
41   /// Write all the sample profiles in the given map of samples.
42   ///
43   /// \returns status code of the file update operation.
44   virtual std::error_code write(const StringMap<FunctionSamples> &ProfileMap);
45 
46   raw_ostream &getOutputStream() { return *OutputStream; }
47 
48   /// Profile writer factory.
49   ///
50   /// Create a new file writer based on the value of \p Format.
51   static ErrorOr<std::unique_ptr<SampleProfileWriter>>
52   create(StringRef Filename, SampleProfileFormat Format);
53 
54   /// Create a new stream writer based on the value of \p Format.
55   /// For testing.
56   static ErrorOr<std::unique_ptr<SampleProfileWriter>>
57   create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
58 
59   virtual void setProfileSymbolList(ProfileSymbolList *PSL) {}
60   virtual void setToCompressAllSections() {}
61   virtual void setUseMD5() {}
62   virtual void setPartialProfile() {}
63 
64 protected:
65   SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
66       : OutputStream(std::move(OS)) {}
67 
68   /// Write a file header for the profile file.
69   virtual std::error_code
70   writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
71 
72   // Write function profiles to the profile file.
73   virtual std::error_code
74   writeFuncProfiles(const StringMap<FunctionSamples> &ProfileMap);
75 
76   /// Output stream where to emit the profile to.
77   std::unique_ptr<raw_ostream> OutputStream;
78 
79   /// Profile summary.
80   std::unique_ptr<ProfileSummary> Summary;
81 
82   /// Compute summary for this profile.
83   void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
84 
85   /// Profile format.
86   SampleProfileFormat Format = SPF_None;
87 };
88 
89 /// Sample-based profile writer (text format).
90 class SampleProfileWriterText : public SampleProfileWriter {
91 public:
92   std::error_code writeSample(const FunctionSamples &S) override;
93 
94 protected:
95   SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
96       : SampleProfileWriter(OS), Indent(0) {}
97 
98   std::error_code
99   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
100     return sampleprof_error::success;
101   }
102 
103 private:
104   /// Indent level to use when writing.
105   ///
106   /// This is used when printing inlined callees.
107   unsigned Indent;
108 
109   friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
110   SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
111                               SampleProfileFormat Format);
112 };
113 
114 /// Sample-based profile writer (binary format).
115 class SampleProfileWriterBinary : public SampleProfileWriter {
116 public:
117   SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
118       : SampleProfileWriter(OS) {}
119 
120   virtual std::error_code writeSample(const FunctionSamples &S) override;
121 
122 protected:
123   virtual std::error_code writeMagicIdent(SampleProfileFormat Format);
124   virtual std::error_code writeNameTable();
125   virtual std::error_code
126   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
127   std::error_code writeSummary();
128   std::error_code writeNameIdx(StringRef FName);
129   std::error_code writeBody(const FunctionSamples &S);
130   inline void stablizeNameTable(std::set<StringRef> &V);
131 
132   MapVector<StringRef, uint32_t> NameTable;
133 
134   void addName(StringRef FName);
135   void addNames(const FunctionSamples &S);
136 
137 private:
138   friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
139   SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
140                               SampleProfileFormat Format);
141 };
142 
143 class SampleProfileWriterRawBinary : public SampleProfileWriterBinary {
144   using SampleProfileWriterBinary::SampleProfileWriterBinary;
145 };
146 
147 class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
148   using SampleProfileWriterBinary::SampleProfileWriterBinary;
149 public:
150   virtual std::error_code
151   write(const StringMap<FunctionSamples> &ProfileMap) override;
152 
153   virtual void setToCompressAllSections() override;
154   void setToCompressSection(SecType Type);
155 
156 protected:
157   uint64_t markSectionStart(SecType Type);
158   std::error_code addNewSection(SecType Sec, uint64_t SectionStart);
159   template <class SecFlagType>
160   void addSectionFlag(SecType Type, SecFlagType Flag) {
161     for (auto &Entry : SectionHdrLayout) {
162       if (Entry.Type == Type)
163         addSecFlag(Entry, Flag);
164     }
165   }
166 
167   virtual void initSectionHdrLayout() = 0;
168   virtual std::error_code
169   writeSections(const StringMap<FunctionSamples> &ProfileMap) = 0;
170 
171   // Specifiy the order of sections in section header table. Note
172   // the order of sections in the profile may be different that the
173   // order in SectionHdrLayout. sample Reader will follow the order
174   // in SectionHdrLayout to read each section.
175   SmallVector<SecHdrTableEntry, 8> SectionHdrLayout;
176 
177 private:
178   void allocSecHdrTable();
179   std::error_code writeSecHdrTable();
180   virtual std::error_code
181   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
182   SecHdrTableEntry &getEntryInLayout(SecType Type);
183   std::error_code compressAndOutput();
184 
185   // We will swap the raw_ostream held by LocalBufStream and that
186   // held by OutputStream if we try to add a section which needs
187   // compression. After the swap, all the data written to output
188   // will be temporarily buffered into the underlying raw_string_ostream
189   // originally held by LocalBufStream. After the data writing for the
190   // section is completed, compress the data in the local buffer,
191   // swap the raw_ostream back and write the compressed data to the
192   // real output.
193   std::unique_ptr<raw_ostream> LocalBufStream;
194   // The location where the output stream starts.
195   uint64_t FileStart;
196   // The location in the output stream where the SecHdrTable should be
197   // written to.
198   uint64_t SecHdrTableOffset;
199   // Initial Section Flags setting.
200   std::vector<SecHdrTableEntry> SecHdrTable;
201 };
202 
203 class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase {
204 public:
205   SampleProfileWriterExtBinary(std::unique_ptr<raw_ostream> &OS)
206       : SampleProfileWriterExtBinaryBase(OS) {
207     initSectionHdrLayout();
208   }
209 
210   virtual std::error_code writeSample(const FunctionSamples &S) override;
211   virtual void setProfileSymbolList(ProfileSymbolList *PSL) override {
212     ProfSymList = PSL;
213   };
214 
215   // Set to use MD5 to represent string in NameTable.
216   virtual void setUseMD5() override {
217     UseMD5 = true;
218     addSectionFlag(SecNameTable, SecNameTableFlags::SecFlagMD5Name);
219   }
220 
221   // Set the profile to be partial. It means the profile is for
222   // common/shared code. The common profile is usually merged from
223   // profiles collected from running other targets.
224   virtual void setPartialProfile() override {
225     addSectionFlag(SecProfSummary, SecProfSummaryFlags::SecFlagPartial);
226   }
227 
228 private:
229   virtual void initSectionHdrLayout() override {
230     // Note that SecFuncOffsetTable section is written after SecLBRProfile
231     // in the profile, but is put before SecLBRProfile in SectionHdrLayout.
232     //
233     // This is because sample reader follows the order of SectionHdrLayout to
234     // read each section, to read function profiles on demand sample reader
235     // need to get the offset of each function profile first.
236     //
237     // SecFuncOffsetTable section is written after SecLBRProfile in the
238     // profile because FuncOffsetTable needs to be populated while section
239     // SecLBRProfile is written.
240     SectionHdrLayout = {{SecProfSummary, 0, 0, 0},
241                         {SecNameTable, 0, 0, 0},
242                         {SecFuncOffsetTable, 0, 0, 0},
243                         {SecLBRProfile, 0, 0, 0},
244                         {SecProfileSymbolList, 0, 0, 0}};
245   };
246   virtual std::error_code
247   writeSections(const StringMap<FunctionSamples> &ProfileMap) override;
248 
249   std::error_code writeFuncOffsetTable();
250   virtual std::error_code writeNameTable() override;
251 
252   ProfileSymbolList *ProfSymList = nullptr;
253 
254   // Save the start of SecLBRProfile so we can compute the offset to the
255   // start of SecLBRProfile for each Function's Profile and will keep it
256   // in FuncOffsetTable.
257   uint64_t SecLBRProfileStart = 0;
258   // FuncOffsetTable maps function name to its profile offset in SecLBRProfile
259   // section. It is used to load function profile on demand.
260   MapVector<StringRef, uint64_t> FuncOffsetTable;
261   // Whether to use MD5 to represent string.
262   bool UseMD5 = false;
263 };
264 
265 // CompactBinary is a compact format of binary profile which both reduces
266 // the profile size and the load time needed when compiling. It has two
267 // major difference with Binary format.
268 // 1. It represents all the strings in name table using md5 hash.
269 // 2. It saves a function offset table which maps function name index to
270 // the offset of its function profile to the start of the binary profile,
271 // so by using the function offset table, for those function profiles which
272 // will not be needed when compiling a module, the profile reader does't
273 // have to read them and it saves compile time if the profile size is huge.
274 // The layout of the compact format is shown as follows:
275 //
276 //    Part1: Profile header, the same as binary format, containing magic
277 //           number, version, summary, name table...
278 //    Part2: Function Offset Table Offset, which saves the position of
279 //           Part4.
280 //    Part3: Function profile collection
281 //             function1 profile start
282 //                 ....
283 //             function2 profile start
284 //                 ....
285 //             function3 profile start
286 //                 ....
287 //                ......
288 //    Part4: Function Offset Table
289 //             function1 name index --> function1 profile start
290 //             function2 name index --> function2 profile start
291 //             function3 name index --> function3 profile start
292 //
293 // We need Part2 because profile reader can use it to find out and read
294 // function offset table without reading Part3 first.
295 class SampleProfileWriterCompactBinary : public SampleProfileWriterBinary {
296   using SampleProfileWriterBinary::SampleProfileWriterBinary;
297 
298 public:
299   virtual std::error_code writeSample(const FunctionSamples &S) override;
300   virtual std::error_code
301   write(const StringMap<FunctionSamples> &ProfileMap) override;
302 
303 protected:
304   /// The table mapping from function name to the offset of its FunctionSample
305   /// towards profile start.
306   MapVector<StringRef, uint64_t> FuncOffsetTable;
307   /// The offset of the slot to be filled with the offset of FuncOffsetTable
308   /// towards profile start.
309   uint64_t TableOffset;
310   virtual std::error_code writeNameTable() override;
311   virtual std::error_code
312   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
313   std::error_code writeFuncOffsetTable();
314 };
315 
316 } // end namespace sampleprof
317 } // end namespace llvm
318 
319 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
320