LLVM  10.0.0svn
SampleProfWriter.h
Go to the documentation of this file.
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"
20 #include "llvm/Support/ErrorOr.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.
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 
47 
48  /// Profile writer factory.
49  ///
50  /// Create a new file writer based on the value of \p Format.
53 
54  /// Create a new stream writer based on the value of \p Format.
55  /// For testing.
57  create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
58 
60 
61 protected:
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
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.
84 };
85 
86 /// Sample-based profile writer (text format).
88 public:
89  std::error_code writeSample(const FunctionSamples &S) override;
90 
91 protected:
92  SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
93  : SampleProfileWriter(OS), Indent(0) {}
94 
95  std::error_code
96  writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
98  }
99 
100 private:
101  /// Indent level to use when writing.
102  ///
103  /// This is used when printing inlined callees.
104  unsigned Indent;
105 
107  SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
109 };
110 
111 /// Sample-based profile writer (binary format).
113 public:
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 
130 
131  void addName(StringRef FName);
132  void addNames(const FunctionSamples &S);
133 
134 private:
136  SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
137  SampleProfileFormat Format);
138 };
139 
142 };
143 
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.
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 
194 public:
195  SampleProfileWriterExtBinary(std::unique_ptr<raw_ostream> &OS)
197  initSectionHdrLayout();
198  }
199 
200  virtual std::error_code writeSample(const FunctionSamples &S) override;
201  virtual void setProfileSymbolList(ProfileSymbolList *PSL) override {
202  ProfSymList = PSL;
203  };
204 
205 private:
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;
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.
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.
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
Represents either an error or a value T.
Definition: ErrorOr.h:56
uint64_t TableOffset
The offset of the slot to be filled with the offset of FuncOffsetTable towards profile start...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MapVector< StringRef, uint32_t > NameTable
Sample-based profile writer. Base class.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:37
std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
SampleProfileWriterBinary(std::unique_ptr< raw_ostream > &OS)
Representation of the samples collected for a function.
Definition: SampleProf.h:300
Definition: BitVector.h:937
virtual std::error_code writeFuncProfiles(const StringMap< FunctionSamples > &ProfileMap)
MapVector< StringRef, uint64_t > FuncOffsetTable
The table mapping from function name to the offset of its FunctionSample towards profile start...
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap)
Write all the sample profiles in the given map of samples.
std::unique_ptr< ProfileSummary > Summary
Profile summary.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Sample-based profile writer (binary format).
SmallVector< SecHdrTableEntry, 8 > SectionHdrLayout
virtual std::error_code writeSample(const FunctionSamples &S)=0
Write sample profiles in S.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
SampleProfileFormat Format
Profile format.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:242
virtual void setProfileSymbolList(ProfileSymbolList *PSL)
virtual void setProfileSymbolList(ProfileSymbolList *PSL) override
SampleProfileWriterText(std::unique_ptr< raw_ostream > &OS)
ProfileSymbolList records the list of function symbols shown up in the binary used to generate the pr...
Definition: SampleProf.h:641
Provides ErrorOr<T> smart pointer.
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap)=0
Write a file header for the profile file.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
SampleProfileWriter(std::unique_ptr< raw_ostream > &OS)
void computeSummary(const StringMap< FunctionSamples > &ProfileMap)
Compute summary for this profile.
Sample-based profile writer (text format).
SampleProfileWriterExtBinary(std::unique_ptr< raw_ostream > &OS)