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::dump(). 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 OS << FName << ":" << S.getTotalSamples();
44 OS << ":" << S.getHeadSamples();
47 for (const auto &I : S.getBodySamples()) {
48 LineLocation Loc = I.first;
49 const SampleRecord &Sample = I.second;
50 OS.indent(Indent + 1);
51 if (Loc.Discriminator == 0)
52 OS << Loc.LineOffset << ": ";
54 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
56 OS << Sample.getSamples();
58 for (const auto &J : Sample.getCallTargets())
59 OS << " " << J.first() << ":" << J.second;
64 for (const auto &I : S.getCallsiteSamples()) {
65 CallsiteLocation Loc = I.first;
66 const FunctionSamples &CalleeSamples = I.second;
68 if (Loc.Discriminator == 0)
69 OS << Loc.LineOffset << ": ";
71 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
72 if (std::error_code EC = write(Loc.CalleeName, CalleeSamples))
77 return sampleprof_error::success;
80 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
81 const auto &ret = NameTable.find(FName);
82 if (ret == NameTable.end())
83 return sampleprof_error::truncated_name_table;
84 encodeULEB128(ret->second, OS);
85 return sampleprof_error::success;
88 void SampleProfileWriterBinary::addName(StringRef FName) {
89 auto NextIdx = NameTable.size();
90 NameTable.insert(std::make_pair(FName, NextIdx));
93 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
94 // Add all the names in indirect call targets.
95 for (const auto &I : S.getBodySamples()) {
96 const SampleRecord &Sample = I.second;
97 for (const auto &J : Sample.getCallTargets())
101 // Recursively add all the names for inlined callsites.
102 for (const auto &J : S.getCallsiteSamples()) {
103 CallsiteLocation Loc = J.first;
104 const FunctionSamples &CalleeSamples = J.second;
105 addName(Loc.CalleeName);
106 addNames(CalleeSamples);
110 std::error_code SampleProfileWriterBinary::writeHeader(
111 const StringMap<FunctionSamples> &ProfileMap) {
112 // Write file magic identifier.
113 encodeULEB128(SPMagic(), OS);
114 encodeULEB128(SPVersion(), OS);
116 // Generate the name table for all the functions referenced in the profile.
117 for (const auto &I : ProfileMap) {
122 // Write out the name table.
123 encodeULEB128(NameTable.size(), OS);
124 for (auto N : NameTable) {
126 encodeULEB128(0, OS);
129 return sampleprof_error::success;
132 std::error_code SampleProfileWriterBinary::writeBody(StringRef FName,
133 const FunctionSamples &S) {
134 if (std::error_code EC = writeNameIdx(FName))
137 encodeULEB128(S.getTotalSamples(), OS);
139 // Emit all the body samples.
140 encodeULEB128(S.getBodySamples().size(), OS);
141 for (const auto &I : S.getBodySamples()) {
142 LineLocation Loc = I.first;
143 const SampleRecord &Sample = I.second;
144 encodeULEB128(Loc.LineOffset, OS);
145 encodeULEB128(Loc.Discriminator, OS);
146 encodeULEB128(Sample.getSamples(), OS);
147 encodeULEB128(Sample.getCallTargets().size(), OS);
148 for (const auto &J : Sample.getCallTargets()) {
149 StringRef Callee = J.first();
150 uint64_t CalleeSamples = J.second;
151 if (std::error_code EC = writeNameIdx(Callee))
153 encodeULEB128(CalleeSamples, OS);
157 // Recursively emit all the callsite samples.
158 encodeULEB128(S.getCallsiteSamples().size(), OS);
159 for (const auto &J : S.getCallsiteSamples()) {
160 CallsiteLocation Loc = J.first;
161 const FunctionSamples &CalleeSamples = J.second;
162 encodeULEB128(Loc.LineOffset, OS);
163 encodeULEB128(Loc.Discriminator, OS);
164 if (std::error_code EC = writeBody(Loc.CalleeName, CalleeSamples))
168 return sampleprof_error::success;
171 /// \brief Write samples of a top-level function to a binary file.
173 /// \returns true if the samples were written successfully, false otherwise.
174 std::error_code SampleProfileWriterBinary::write(StringRef FName,
175 const FunctionSamples &S) {
176 encodeULEB128(S.getHeadSamples(), OS);
177 return writeBody(FName, S);
180 /// \brief Create a sample profile writer based on the specified format.
182 /// \param Filename The file to create.
184 /// \param Writer The writer to instantiate according to the specified format.
186 /// \param Format Encoding format for the profile file.
188 /// \returns an error code indicating the status of the created writer.
189 ErrorOr<std::unique_ptr<SampleProfileWriter>>
190 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
192 std::unique_ptr<SampleProfileWriter> Writer;
194 if (Format == SPF_Binary)
195 Writer.reset(new SampleProfileWriterBinary(Filename, EC));
196 else if (Format == SPF_Text)
197 Writer.reset(new SampleProfileWriterText(Filename, EC));
198 else if (Format == SPF_GCC)
199 EC = sampleprof_error::unsupported_writing_format;
201 EC = sampleprof_error::unrecognized_format;
206 return std::move(Writer);