1 //===- SampleProfWriter.h - Write LLVM sample profile data ------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
26 #include <system_error>
29 namespace sampleprof
{
31 /// Sample-based profile writer. Base class.
32 class SampleProfileWriter
{
34 virtual ~SampleProfileWriter() = default;
36 /// Write sample profiles in \p S.
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.
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.
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.
56 static ErrorOr
<std::unique_ptr
<SampleProfileWriter
>>
57 create(std::unique_ptr
<raw_ostream
> &OS
, SampleProfileFormat Format
);
59 virtual void setProfileSymbolList(ProfileSymbolList
*PSL
) {}
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
;
77 std::unique_ptr
<ProfileSummary
> Summary
;
79 /// Compute summary for this profile.
80 void computeSummary(const StringMap
<FunctionSamples
> &ProfileMap
);
83 SampleProfileFormat Format
;
86 /// Sample-based profile writer (text format).
87 class SampleProfileWriterText
: public SampleProfileWriter
{
89 std::error_code
writeSample(const FunctionSamples
&S
) override
;
92 SampleProfileWriterText(std::unique_ptr
<raw_ostream
> &OS
)
93 : SampleProfileWriter(OS
), Indent(0) {}
96 writeHeader(const StringMap
<FunctionSamples
> &ProfileMap
) override
{
97 return sampleprof_error::success
;
101 /// Indent level to use when writing.
103 /// This is used when printing inlined callees.
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
{
114 SampleProfileWriterBinary(std::unique_ptr
<raw_ostream
> &OS
)
115 : SampleProfileWriter(OS
) {}
117 virtual std::error_code
writeSample(const FunctionSamples
&S
) override
;
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
);
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 virtual std::error_code
148 write(const StringMap
<FunctionSamples
> &ProfileMap
) override
;
150 void setToCompressAllSections();
151 void setToCompressSection(SecType Type
);
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;
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
;
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();
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
183 std::unique_ptr
<raw_ostream
> LocalBufStream
;
184 // The location where the output stream starts.
186 // The location in the output stream where the SecHdrTable should be
188 uint64_t SecHdrTableOffset
;
189 // Initial Section Flags setting.
190 std::vector
<SecHdrTableEntry
> SecHdrTable
;
193 class SampleProfileWriterExtBinary
: public SampleProfileWriterExtBinaryBase
{
195 SampleProfileWriterExtBinary(std::unique_ptr
<raw_ostream
> &OS
)
196 : SampleProfileWriterExtBinaryBase(OS
) {
197 initSectionHdrLayout();
200 virtual std::error_code
writeSample(const FunctionSamples
&S
) override
;
201 virtual void setProfileSymbolList(ProfileSymbolList
*PSL
) override
{
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.
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.
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}};
223 virtual std::error_code
224 writeSections(const StringMap
<FunctionSamples
> &ProfileMap
) override
;
225 ProfileSymbolList
*ProfSymList
= nullptr;
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();
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:
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
252 // Part3: Function profile collection
253 // function1 profile start
255 // function2 profile start
257 // function3 profile start
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
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
;
271 virtual std::error_code
writeSample(const FunctionSamples
&S
) override
;
272 virtual std::error_code
273 write(const StringMap
<FunctionSamples
> &ProfileMap
) override
;
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();
288 } // end namespace sampleprof
289 } // end namespace llvm
291 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H