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 
getOutputStream()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 
setProfileSymbolList(ProfileSymbolList * PSL)59   virtual void setProfileSymbolList(ProfileSymbolList *PSL) {}
60 
61 protected:
SampleProfileWriter(std::unique_ptr<raw_ostream> & OS)62   SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
63       : OutputStream(std::move(OS)) {}
64 
65   /// Write a file header for the profile file.
66   virtual std::error_code
67   writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
68 
69   // Write function profiles to the profile file.
70   virtual std::error_code
71   writeFuncProfiles(const StringMap<FunctionSamples> &ProfileMap);
72 
73   /// Output stream where to emit the profile to.
74   std::unique_ptr<raw_ostream> OutputStream;
75 
76   /// Profile summary.
77   std::unique_ptr<ProfileSummary> Summary;
78 
79   /// Compute summary for this profile.
80   void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
81 
82   /// Profile format.
83   SampleProfileFormat Format = SPF_None;
84 };
85 
86 /// Sample-based profile writer (text format).
87 class SampleProfileWriterText : public SampleProfileWriter {
88 public:
89   std::error_code writeSample(const FunctionSamples &S) override;
90 
91 protected:
SampleProfileWriterText(std::unique_ptr<raw_ostream> & OS)92   SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
93       : SampleProfileWriter(OS), Indent(0) {}
94 
95   std::error_code
writeHeader(const StringMap<FunctionSamples> & ProfileMap)96   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
97     return sampleprof_error::success;
98   }
99 
100 private:
101   /// Indent level to use when writing.
102   ///
103   /// This is used when printing inlined callees.
104   unsigned Indent;
105 
106   friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
107   SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
108                               SampleProfileFormat Format);
109 };
110 
111 /// Sample-based profile writer (binary format).
112 class SampleProfileWriterBinary : public SampleProfileWriter {
113 public:
SampleProfileWriterBinary(std::unique_ptr<raw_ostream> & OS)114   SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
115       : SampleProfileWriter(OS) {}
116 
117   virtual std::error_code writeSample(const FunctionSamples &S) override;
118 
119 protected:
120   virtual std::error_code writeMagicIdent(SampleProfileFormat Format);
121   virtual std::error_code writeNameTable();
122   virtual std::error_code
123   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
124   std::error_code writeSummary();
125   std::error_code writeNameIdx(StringRef FName);
126   std::error_code writeBody(const FunctionSamples &S);
127   inline void stablizeNameTable(std::set<StringRef> &V);
128 
129   MapVector<StringRef, uint32_t> NameTable;
130 
131   void addName(StringRef FName);
132   void addNames(const FunctionSamples &S);
133 
134 private:
135   friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
136   SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
137                               SampleProfileFormat Format);
138 };
139 
140 class SampleProfileWriterRawBinary : public SampleProfileWriterBinary {
141   using SampleProfileWriterBinary::SampleProfileWriterBinary;
142 };
143 
144 class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
145   using SampleProfileWriterBinary::SampleProfileWriterBinary;
146 public:
147   virtual std::error_code
148   write(const StringMap<FunctionSamples> &ProfileMap) override;
149 
150   void setToCompressAllSections();
151   void setToCompressSection(SecType Type);
152 
153 protected:
154   uint64_t markSectionStart(SecType Type);
155   std::error_code addNewSection(SecType Sec, uint64_t SectionStart);
156   virtual void initSectionHdrLayout() = 0;
157   virtual std::error_code
158   writeSections(const StringMap<FunctionSamples> &ProfileMap) = 0;
159 
160   // Specifiy the order of sections in section header table. Note
161   // the order of sections in the profile may be different that the
162   // order in SectionHdrLayout. sample Reader will follow the order
163   // in SectionHdrLayout to read each section.
164   SmallVector<SecHdrTableEntry, 8> SectionHdrLayout;
165 
166 private:
167   void allocSecHdrTable();
168   std::error_code writeSecHdrTable();
169   virtual std::error_code
170   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
171   void addSectionFlags(SecType Type, SecFlags Flags);
172   SecHdrTableEntry &getEntryInLayout(SecType Type);
173   std::error_code compressAndOutput();
174 
175   // We will swap the raw_ostream held by LocalBufStream and that
176   // held by OutputStream if we try to add a section which needs
177   // compression. After the swap, all the data written to output
178   // will be temporarily buffered into the underlying raw_string_ostream
179   // originally held by LocalBufStream. After the data writing for the
180   // section is completed, compress the data in the local buffer,
181   // swap the raw_ostream back and write the compressed data to the
182   // real output.
183   std::unique_ptr<raw_ostream> LocalBufStream;
184   // The location where the output stream starts.
185   uint64_t FileStart;
186   // The location in the output stream where the SecHdrTable should be
187   // written to.
188   uint64_t SecHdrTableOffset;
189   // Initial Section Flags setting.
190   std::vector<SecHdrTableEntry> SecHdrTable;
191 };
192 
193 class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase {
194 public:
SampleProfileWriterExtBinary(std::unique_ptr<raw_ostream> & OS)195   SampleProfileWriterExtBinary(std::unique_ptr<raw_ostream> &OS)
196       : SampleProfileWriterExtBinaryBase(OS) {
197     initSectionHdrLayout();
198   }
199 
200   virtual std::error_code writeSample(const FunctionSamples &S) override;
setProfileSymbolList(ProfileSymbolList * PSL)201   virtual void setProfileSymbolList(ProfileSymbolList *PSL) override {
202     ProfSymList = PSL;
203   };
204 
205 private:
initSectionHdrLayout()206   virtual void initSectionHdrLayout() override {
207     // Note that SecFuncOffsetTable section is written after SecLBRProfile
208     // in the profile, but is put before SecLBRProfile in SectionHdrLayout.
209     //
210     // This is because sample reader follows the order of SectionHdrLayout to
211     // read each section, to read function profiles on demand sample reader
212     // need to get the offset of each function profile first.
213     //
214     // SecFuncOffsetTable section is written after SecLBRProfile in the
215     // profile because FuncOffsetTable needs to be populated while section
216     // SecLBRProfile is written.
217     SectionHdrLayout = {{SecProfSummary, 0, 0, 0},
218                         {SecNameTable, 0, 0, 0},
219                         {SecFuncOffsetTable, 0, 0, 0},
220                         {SecLBRProfile, 0, 0, 0},
221                         {SecProfileSymbolList, 0, 0, 0}};
222   };
223   virtual std::error_code
224   writeSections(const StringMap<FunctionSamples> &ProfileMap) override;
225   ProfileSymbolList *ProfSymList = nullptr;
226 
227   // Save the start of SecLBRProfile so we can compute the offset to the
228   // start of SecLBRProfile for each Function's Profile and will keep it
229   // in FuncOffsetTable.
230   uint64_t SecLBRProfileStart = 0;
231   // FuncOffsetTable maps function name to its profile offset in SecLBRProfile
232   // section. It is used to load function profile on demand.
233   MapVector<StringRef, uint64_t> FuncOffsetTable;
234   std::error_code writeFuncOffsetTable();
235 };
236 
237 // CompactBinary is a compact format of binary profile which both reduces
238 // the profile size and the load time needed when compiling. It has two
239 // major difference with Binary format.
240 // 1. It represents all the strings in name table using md5 hash.
241 // 2. It saves a function offset table which maps function name index to
242 // the offset of its function profile to the start of the binary profile,
243 // so by using the function offset table, for those function profiles which
244 // will not be needed when compiling a module, the profile reader does't
245 // have to read them and it saves compile time if the profile size is huge.
246 // The layout of the compact format is shown as follows:
247 //
248 //    Part1: Profile header, the same as binary format, containing magic
249 //           number, version, summary, name table...
250 //    Part2: Function Offset Table Offset, which saves the position of
251 //           Part4.
252 //    Part3: Function profile collection
253 //             function1 profile start
254 //                 ....
255 //             function2 profile start
256 //                 ....
257 //             function3 profile start
258 //                 ....
259 //                ......
260 //    Part4: Function Offset Table
261 //             function1 name index --> function1 profile start
262 //             function2 name index --> function2 profile start
263 //             function3 name index --> function3 profile start
264 //
265 // We need Part2 because profile reader can use it to find out and read
266 // function offset table without reading Part3 first.
267 class SampleProfileWriterCompactBinary : public SampleProfileWriterBinary {
268   using SampleProfileWriterBinary::SampleProfileWriterBinary;
269 
270 public:
271   virtual std::error_code writeSample(const FunctionSamples &S) override;
272   virtual std::error_code
273   write(const StringMap<FunctionSamples> &ProfileMap) override;
274 
275 protected:
276   /// The table mapping from function name to the offset of its FunctionSample
277   /// towards profile start.
278   MapVector<StringRef, uint64_t> FuncOffsetTable;
279   /// The offset of the slot to be filled with the offset of FuncOffsetTable
280   /// towards profile start.
281   uint64_t TableOffset;
282   virtual std::error_code writeNameTable() override;
283   virtual std::error_code
284   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
285   std::error_code writeFuncOffsetTable();
286 };
287 
288 } // end namespace sampleprof
289 } // end namespace llvm
290 
291 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
292