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/MemoryBuffer.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/LineIterator.h"
27 #include "llvm/Support/Regex.h"
29 using namespace llvm::sampleprof;
32 /// \brief Write samples to a text file.
33 bool SampleProfileWriterText::write(StringRef FName, const FunctionSamples &S) {
37 OS << FName << ":" << S.getTotalSamples() << ":" << S.getHeadSamples()
40 for (const auto &I : S.getBodySamples()) {
41 LineLocation Loc = I.first;
42 const SampleRecord &Sample = I.second;
43 if (Loc.Discriminator == 0)
44 OS << Loc.LineOffset << ": ";
46 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
48 OS << Sample.getSamples();
50 for (const auto &J : Sample.getCallTargets())
51 OS << " " << J.first() << ":" << J.second;
58 SampleProfileWriterBinary::SampleProfileWriterBinary(StringRef F,
60 : SampleProfileWriter(F, EC, sys::fs::F_None) {
64 // Write the file header.
65 encodeULEB128(SPMagic(), OS);
66 encodeULEB128(SPVersion(), OS);
69 /// \brief Write samples to a binary file.
71 /// \returns true if the samples were written successfully, false otherwise.
72 bool SampleProfileWriterBinary::write(StringRef FName,
73 const FunctionSamples &S) {
79 encodeULEB128(S.getTotalSamples(), OS);
80 encodeULEB128(S.getHeadSamples(), OS);
81 encodeULEB128(S.getBodySamples().size(), OS);
82 for (const auto &I : S.getBodySamples()) {
83 LineLocation Loc = I.first;
84 const SampleRecord &Sample = I.second;
85 encodeULEB128(Loc.LineOffset, OS);
86 encodeULEB128(Loc.Discriminator, OS);
87 encodeULEB128(Sample.getSamples(), OS);
88 encodeULEB128(Sample.getCallTargets().size(), OS);
89 for (const auto &J : Sample.getCallTargets()) {
90 std::string Callee = J.first();
91 unsigned CalleeSamples = J.second;
94 encodeULEB128(CalleeSamples, OS);
101 /// \brief Create a sample profile writer based on the specified format.
103 /// \param Filename The file to create.
105 /// \param Writer The writer to instantiate according to the specified format.
107 /// \param Format Encoding format for the profile file.
109 /// \returns an error code indicating the status of the created writer.
110 ErrorOr<std::unique_ptr<SampleProfileWriter>>
111 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
113 std::unique_ptr<SampleProfileWriter> Writer;
115 if (Format == SPF_Binary)
116 Writer.reset(new SampleProfileWriterBinary(Filename, EC));
117 else if (Format == SPF_Text)
118 Writer.reset(new SampleProfileWriterText(Filename, EC));
120 EC = sampleprof_error::unrecognized_format;
125 return std::move(Writer);