1 //===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that writes LLVM sample profiles. It
11 // supports two file formats: text and binary. The textual representation
12 // is useful for debugging and testing purposes. The binary representation
13 // is more compact, resulting in smaller file sizes. However, they can
14 // both be used interchangeably.
16 // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ProfileData/SampleProfWriter.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/LineIterator.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Regex.h"
29 using namespace llvm::sampleprof;
32 /// \brief Write samples to a text file.
34 /// Note: it may be tempting to implement this in terms of
35 /// FunctionSamples::print(). Please don't. The dump functionality is intended
36 /// for debugging and has no specified form.
38 /// The format used here is more structured and deliberate because
39 /// it needs to be parsed by the SampleProfileReaderText class.
40 std::error_code SampleProfileWriterText::write(StringRef FName,
41 const FunctionSamples &S) {
42 auto &OS = *OutputStream;
44 OS << FName << ":" << S.getTotalSamples();
46 OS << ":" << S.getHeadSamples();
49 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
50 for (const auto &I : SortedSamples.get()) {
51 LineLocation Loc = I->first;
52 const SampleRecord &Sample = I->second;
53 OS.indent(Indent + 1);
54 if (Loc.Discriminator == 0)
55 OS << Loc.LineOffset << ": ";
57 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
59 OS << Sample.getSamples();
61 for (const auto &J : Sample.getCallTargets())
62 OS << " " << J.first() << ":" << J.second;
66 SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples(
67 S.getCallsiteSamples());
69 for (const auto &I : SortedCallsiteSamples.get()) {
70 CallsiteLocation Loc = I->first;
71 const FunctionSamples &CalleeSamples = I->second;
73 if (Loc.Discriminator == 0)
74 OS << Loc.LineOffset << ": ";
76 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
77 if (std::error_code EC = write(Loc.CalleeName, CalleeSamples))
82 return sampleprof_error::success;
85 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
86 const auto &ret = NameTable.find(FName);
87 if (ret == NameTable.end())
88 return sampleprof_error::truncated_name_table;
89 encodeULEB128(ret->second, *OutputStream);
90 return sampleprof_error::success;
93 void SampleProfileWriterBinary::addName(StringRef FName) {
94 auto NextIdx = NameTable.size();
95 NameTable.insert(std::make_pair(FName, NextIdx));
98 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
99 // Add all the names in indirect call targets.
100 for (const auto &I : S.getBodySamples()) {
101 const SampleRecord &Sample = I.second;
102 for (const auto &J : Sample.getCallTargets())
106 // Recursively add all the names for inlined callsites.
107 for (const auto &J : S.getCallsiteSamples()) {
108 CallsiteLocation Loc = J.first;
109 const FunctionSamples &CalleeSamples = J.second;
110 addName(Loc.CalleeName);
111 addNames(CalleeSamples);
115 std::error_code SampleProfileWriterBinary::writeHeader(
116 const StringMap<FunctionSamples> &ProfileMap) {
117 auto &OS = *OutputStream;
119 // Write file magic identifier.
120 encodeULEB128(SPMagic(), OS);
121 encodeULEB128(SPVersion(), OS);
123 // Generate the name table for all the functions referenced in the profile.
124 for (const auto &I : ProfileMap) {
129 // Write out the name table.
130 encodeULEB128(NameTable.size(), OS);
131 for (auto N : NameTable) {
133 encodeULEB128(0, OS);
136 return sampleprof_error::success;
139 std::error_code SampleProfileWriterBinary::writeBody(StringRef FName,
140 const FunctionSamples &S) {
141 auto &OS = *OutputStream;
143 if (std::error_code EC = writeNameIdx(FName))
146 encodeULEB128(S.getTotalSamples(), OS);
148 // Emit all the body samples.
149 encodeULEB128(S.getBodySamples().size(), OS);
150 for (const auto &I : S.getBodySamples()) {
151 LineLocation Loc = I.first;
152 const SampleRecord &Sample = I.second;
153 encodeULEB128(Loc.LineOffset, OS);
154 encodeULEB128(Loc.Discriminator, OS);
155 encodeULEB128(Sample.getSamples(), OS);
156 encodeULEB128(Sample.getCallTargets().size(), OS);
157 for (const auto &J : Sample.getCallTargets()) {
158 StringRef Callee = J.first();
159 uint64_t CalleeSamples = J.second;
160 if (std::error_code EC = writeNameIdx(Callee))
162 encodeULEB128(CalleeSamples, OS);
166 // Recursively emit all the callsite samples.
167 encodeULEB128(S.getCallsiteSamples().size(), OS);
168 for (const auto &J : S.getCallsiteSamples()) {
169 CallsiteLocation Loc = J.first;
170 const FunctionSamples &CalleeSamples = J.second;
171 encodeULEB128(Loc.LineOffset, OS);
172 encodeULEB128(Loc.Discriminator, OS);
173 if (std::error_code EC = writeBody(Loc.CalleeName, CalleeSamples))
177 return sampleprof_error::success;
180 /// \brief Write samples of a top-level function to a binary file.
182 /// \returns true if the samples were written successfully, false otherwise.
183 std::error_code SampleProfileWriterBinary::write(StringRef FName,
184 const FunctionSamples &S) {
185 encodeULEB128(S.getHeadSamples(), *OutputStream);
186 return writeBody(FName, S);
189 /// \brief Create a sample profile file writer based on the specified format.
191 /// \param Filename The file to create.
193 /// \param Writer The writer to instantiate according to the specified format.
195 /// \param Format Encoding format for the profile file.
197 /// \returns an error code indicating the status of the created writer.
198 ErrorOr<std::unique_ptr<SampleProfileWriter>>
199 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
201 std::unique_ptr<raw_ostream> OS;
202 if (Format == SPF_Binary)
203 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
205 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
209 return create(OS, Format);
212 /// \brief Create a sample profile stream writer based on the specified format.
214 /// \param OS The output stream to store the profile data to.
216 /// \param Writer The writer to instantiate according to the specified format.
218 /// \param Format Encoding format for the profile file.
220 /// \returns an error code indicating the status of the created writer.
221 ErrorOr<std::unique_ptr<SampleProfileWriter>>
222 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
223 SampleProfileFormat Format) {
225 std::unique_ptr<SampleProfileWriter> Writer;
227 if (Format == SPF_Binary)
228 Writer.reset(new SampleProfileWriterBinary(OS));
229 else if (Format == SPF_Text)
230 Writer.reset(new SampleProfileWriterText(OS));
231 else if (Format == SPF_GCC)
232 EC = sampleprof_error::unsupported_writing_format;
234 EC = sampleprof_error::unrecognized_format;
239 return std::move(Writer);