1 //===- SampleProfWriter.h - Write LLVM sample profile data ------*- C++ -*-===//
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 contains definitions needed for writing sample profiles.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
14 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/ProfileSummary.h"
20 #include "llvm/ProfileData/SampleProf.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include "llvm/Support/raw_ostream.h"
27 #include <system_error>
30 namespace sampleprof {
32 /// Sample-based profile writer. Base class.
33 class SampleProfileWriter {
35 virtual ~SampleProfileWriter() = default;
37 /// Write sample profiles in \p S.
39 /// \returns status code of the file update operation.
40 virtual std::error_code write(const FunctionSamples &S) = 0;
42 /// Write all the sample profiles in the given map of samples.
44 /// \returns status code of the file update operation.
45 std::error_code write(const StringMap<FunctionSamples> &ProfileMap);
47 raw_ostream &getOutputStream() { return *OutputStream; }
49 /// Profile writer factory.
51 /// Create a new file writer based on the value of \p Format.
52 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
53 create(StringRef Filename, SampleProfileFormat Format);
55 /// Create a new stream writer based on the value of \p Format.
57 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
58 create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
61 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
62 : OutputStream(std::move(OS)) {}
64 /// Write a file header for the profile file.
65 virtual std::error_code
66 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
68 /// Output stream where to emit the profile to.
69 std::unique_ptr<raw_ostream> OutputStream;
72 std::unique_ptr<ProfileSummary> Summary;
74 /// Compute summary for this profile.
75 void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
78 /// Sample-based profile writer (text format).
79 class SampleProfileWriterText : public SampleProfileWriter {
81 std::error_code write(const FunctionSamples &S) override;
84 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
85 : SampleProfileWriter(OS), Indent(0) {}
88 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
89 return sampleprof_error::success;
93 /// Indent level to use when writing.
95 /// This is used when printing inlined callees.
98 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
99 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
100 SampleProfileFormat Format);
103 /// Sample-based profile writer (binary format).
104 class SampleProfileWriterBinary : public SampleProfileWriter {
106 std::error_code write(const FunctionSamples &S) override;
107 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
108 : SampleProfileWriter(OS) {}
111 virtual std::error_code writeNameTable() = 0;
112 virtual std::error_code writeMagicIdent() = 0;
113 std::error_code writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
114 std::error_code writeSummary();
115 std::error_code writeNameIdx(StringRef FName);
116 std::error_code writeBody(const FunctionSamples &S);
117 inline void stablizeNameTable(std::set<StringRef> &V);
119 MapVector<StringRef, uint32_t> NameTable;
122 void addName(StringRef FName);
123 void addNames(const FunctionSamples &S);
125 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
126 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
127 SampleProfileFormat Format);
130 class SampleProfileWriterRawBinary : public SampleProfileWriterBinary {
131 using SampleProfileWriterBinary::SampleProfileWriterBinary;
134 virtual std::error_code writeNameTable() override;
135 virtual std::error_code writeMagicIdent() override;
138 class SampleProfileWriterCompactBinary : public SampleProfileWriterBinary {
139 using SampleProfileWriterBinary::SampleProfileWriterBinary;
142 virtual std::error_code writeNameTable() override;
143 virtual std::error_code writeMagicIdent() override;
146 } // end namespace sampleprof
147 } // end namespace llvm
149 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H