[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / include / llvm / ProfileData / SampleProfWriter.h
blob35218e3879c4a3fff94b089b99a977311bca2460
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.
11 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
13 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
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>
28 namespace llvm {
29 namespace sampleprof {
31 /// Sample-based profile writer. Base class.
32 class SampleProfileWriter {
33 public:
34 virtual ~SampleProfileWriter() = default;
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;
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);
46 raw_ostream &getOutputStream() { return *OutputStream; }
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);
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);
59 virtual void setProfileSymbolList(ProfileSymbolList *PSL) {}
61 protected:
62 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
63 : OutputStream(std::move(OS)) {}
65 /// Write a file header for the profile file.
66 virtual std::error_code
67 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
69 // Write function profiles to the profile file.
70 virtual std::error_code
71 writeFuncProfiles(const StringMap<FunctionSamples> &ProfileMap);
73 /// Output stream where to emit the profile to.
74 std::unique_ptr<raw_ostream> OutputStream;
76 /// Profile summary.
77 std::unique_ptr<ProfileSummary> Summary;
79 /// Compute summary for this profile.
80 void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
82 /// Profile format.
83 SampleProfileFormat Format;
86 /// Sample-based profile writer (text format).
87 class SampleProfileWriterText : public SampleProfileWriter {
88 public:
89 std::error_code writeSample(const FunctionSamples &S) override;
91 protected:
92 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
93 : SampleProfileWriter(OS), Indent(0) {}
95 std::error_code
96 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
97 return sampleprof_error::success;
100 private:
101 /// Indent level to use when writing.
103 /// This is used when printing inlined callees.
104 unsigned Indent;
106 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
107 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
108 SampleProfileFormat Format);
111 /// Sample-based profile writer (binary format).
112 class SampleProfileWriterBinary : public SampleProfileWriter {
113 public:
114 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
115 : SampleProfileWriter(OS) {}
117 virtual std::error_code writeSample(const FunctionSamples &S) override;
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);
129 MapVector<StringRef, uint32_t> NameTable;
131 void addName(StringRef FName);
132 void addNames(const FunctionSamples &S);
134 private:
135 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
136 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
137 SampleProfileFormat Format);
140 class SampleProfileWriterRawBinary : public SampleProfileWriterBinary {
141 using SampleProfileWriterBinary::SampleProfileWriterBinary;
144 class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
145 using SampleProfileWriterBinary::SampleProfileWriterBinary;
147 public:
148 virtual std::error_code
149 write(const StringMap<FunctionSamples> &ProfileMap) override;
151 protected:
152 uint64_t markSectionStart();
153 uint64_t addNewSection(SecType Sec, uint64_t SectionStart);
154 virtual void initSectionLayout() = 0;
155 virtual std::error_code
156 writeSections(const StringMap<FunctionSamples> &ProfileMap) = 0;
158 // Specifiy the section layout in the profile. Note that the order in
159 // SecHdrTable (order to collect sections) may be different from the
160 // order in SectionLayout (order to write out sections into profile).
161 SmallVector<SecType, 8> SectionLayout;
163 private:
164 void allocSecHdrTable();
165 std::error_code writeSecHdrTable();
166 virtual std::error_code
167 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
169 // The location where the output stream starts.
170 uint64_t FileStart;
171 // The location in the output stream where the SecHdrTable should be
172 // written to.
173 uint64_t SecHdrTableOffset;
174 std::vector<SecHdrTableEntry> SecHdrTable;
177 class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase {
178 using SampleProfileWriterExtBinaryBase::SampleProfileWriterExtBinaryBase;
180 public:
181 virtual void setProfileSymbolList(ProfileSymbolList *PSL) override {
182 ProfSymList = PSL;
185 private:
186 virtual void initSectionLayout() override {
187 SectionLayout = {SecProfSummary, SecNameTable, SecLBRProfile,
188 SecProfileSymbolList};
190 virtual std::error_code
191 writeSections(const StringMap<FunctionSamples> &ProfileMap) override;
192 ProfileSymbolList *ProfSymList = nullptr;
195 // CompactBinary is a compact format of binary profile which both reduces
196 // the profile size and the load time needed when compiling. It has two
197 // major difference with Binary format.
198 // 1. It represents all the strings in name table using md5 hash.
199 // 2. It saves a function offset table which maps function name index to
200 // the offset of its function profile to the start of the binary profile,
201 // so by using the function offset table, for those function profiles which
202 // will not be needed when compiling a module, the profile reader does't
203 // have to read them and it saves compile time if the profile size is huge.
204 // The layout of the compact format is shown as follows:
206 // Part1: Profile header, the same as binary format, containing magic
207 // number, version, summary, name table...
208 // Part2: Function Offset Table Offset, which saves the position of
209 // Part4.
210 // Part3: Function profile collection
211 // function1 profile start
212 // ....
213 // function2 profile start
214 // ....
215 // function3 profile start
216 // ....
217 // ......
218 // Part4: Function Offset Table
219 // function1 name index --> function1 profile start
220 // function2 name index --> function2 profile start
221 // function3 name index --> function3 profile start
223 // We need Part2 because profile reader can use it to find out and read
224 // function offset table without reading Part3 first.
225 class SampleProfileWriterCompactBinary : public SampleProfileWriterBinary {
226 using SampleProfileWriterBinary::SampleProfileWriterBinary;
228 public:
229 virtual std::error_code writeSample(const FunctionSamples &S) override;
230 virtual std::error_code
231 write(const StringMap<FunctionSamples> &ProfileMap) override;
233 protected:
234 /// The table mapping from function name to the offset of its FunctionSample
235 /// towards profile start.
236 MapVector<StringRef, uint64_t> FuncOffsetTable;
237 /// The offset of the slot to be filled with the offset of FuncOffsetTable
238 /// towards profile start.
239 uint64_t TableOffset;
240 virtual std::error_code writeNameTable() override;
241 virtual std::error_code
242 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
243 std::error_code writeFuncOffsetTable();
246 } // end namespace sampleprof
247 } // end namespace llvm
249 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H