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 OS << FName << ":" << S.getTotalSamples();
44 OS << ":" << S.getHeadSamples();
47 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
48 for (const auto &I : SortedSamples.get()) {
49 LineLocation Loc = I->first;
50 const SampleRecord &Sample = I->second;
51 OS.indent(Indent + 1);
52 if (Loc.Discriminator == 0)
53 OS << Loc.LineOffset << ": ";
55 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
57 OS << Sample.getSamples();
59 for (const auto &J : Sample.getCallTargets())
60 OS << " " << J.first() << ":" << J.second;
64 SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples(
65 S.getCallsiteSamples());
67 for (const auto &I : SortedCallsiteSamples.get()) {
68 CallsiteLocation Loc = I->first;
69 const FunctionSamples &CalleeSamples = I->second;
71 if (Loc.Discriminator == 0)
72 OS << Loc.LineOffset << ": ";
74 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
75 if (std::error_code EC = write(Loc.CalleeName, CalleeSamples))
80 return sampleprof_error::success;
83 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
84 const auto &ret = NameTable.find(FName);
85 if (ret == NameTable.end())
86 return sampleprof_error::truncated_name_table;
87 encodeULEB128(ret->second, OS);
88 return sampleprof_error::success;
91 void SampleProfileWriterBinary::addName(StringRef FName) {
92 auto NextIdx = NameTable.size();
93 NameTable.insert(std::make_pair(FName, NextIdx));
96 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
97 // Add all the names in indirect call targets.
98 for (const auto &I : S.getBodySamples()) {
99 const SampleRecord &Sample = I.second;
100 for (const auto &J : Sample.getCallTargets())
104 // Recursively add all the names for inlined callsites.
105 for (const auto &J : S.getCallsiteSamples()) {
106 CallsiteLocation Loc = J.first;
107 const FunctionSamples &CalleeSamples = J.second;
108 addName(Loc.CalleeName);
109 addNames(CalleeSamples);
113 std::error_code SampleProfileWriterBinary::writeHeader(
114 const StringMap<FunctionSamples> &ProfileMap) {
115 // Write file magic identifier.
116 encodeULEB128(SPMagic(), OS);
117 encodeULEB128(SPVersion(), OS);
119 // Generate the name table for all the functions referenced in the profile.
120 for (const auto &I : ProfileMap) {
125 // Write out the name table.
126 encodeULEB128(NameTable.size(), OS);
127 for (auto N : NameTable) {
129 encodeULEB128(0, OS);
132 return sampleprof_error::success;
135 std::error_code SampleProfileWriterBinary::writeBody(StringRef FName,
136 const FunctionSamples &S) {
137 if (std::error_code EC = writeNameIdx(FName))
140 encodeULEB128(S.getTotalSamples(), OS);
142 // Emit all the body samples.
143 encodeULEB128(S.getBodySamples().size(), OS);
144 for (const auto &I : S.getBodySamples()) {
145 LineLocation Loc = I.first;
146 const SampleRecord &Sample = I.second;
147 encodeULEB128(Loc.LineOffset, OS);
148 encodeULEB128(Loc.Discriminator, OS);
149 encodeULEB128(Sample.getSamples(), OS);
150 encodeULEB128(Sample.getCallTargets().size(), OS);
151 for (const auto &J : Sample.getCallTargets()) {
152 StringRef Callee = J.first();
153 uint64_t CalleeSamples = J.second;
154 if (std::error_code EC = writeNameIdx(Callee))
156 encodeULEB128(CalleeSamples, OS);
160 // Recursively emit all the callsite samples.
161 encodeULEB128(S.getCallsiteSamples().size(), OS);
162 for (const auto &J : S.getCallsiteSamples()) {
163 CallsiteLocation Loc = J.first;
164 const FunctionSamples &CalleeSamples = J.second;
165 encodeULEB128(Loc.LineOffset, OS);
166 encodeULEB128(Loc.Discriminator, OS);
167 if (std::error_code EC = writeBody(Loc.CalleeName, CalleeSamples))
171 return sampleprof_error::success;
174 /// \brief Write samples of a top-level function to a binary file.
176 /// \returns true if the samples were written successfully, false otherwise.
177 std::error_code SampleProfileWriterBinary::write(StringRef FName,
178 const FunctionSamples &S) {
179 encodeULEB128(S.getHeadSamples(), OS);
180 return writeBody(FName, S);
183 /// \brief Create a sample profile writer based on the specified format.
185 /// \param Filename The file to create.
187 /// \param Writer The writer to instantiate according to the specified format.
189 /// \param Format Encoding format for the profile file.
191 /// \returns an error code indicating the status of the created writer.
192 ErrorOr<std::unique_ptr<SampleProfileWriter>>
193 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
195 std::unique_ptr<SampleProfileWriter> Writer;
197 if (Format == SPF_Binary)
198 Writer.reset(new SampleProfileWriterBinary(Filename, EC));
199 else if (Format == SPF_Text)
200 Writer.reset(new SampleProfileWriterText(Filename, EC));
201 else if (Format == SPF_GCC)
202 EC = sampleprof_error::unsupported_writing_format;
204 EC = sampleprof_error::unrecognized_format;
209 return std::move(Writer);