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"
26 #include <system_error>
29 namespace sampleprof {
31 enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
33 /// \brief Sample-based profile writer. Base class.
34 class SampleProfileWriter {
36 virtual ~SampleProfileWriter() = default;
38 /// Write sample profiles in \p S.
40 /// \returns status code of the file update operation.
41 virtual std::error_code write(const FunctionSamples &S) = 0;
43 /// Write all the sample profiles in the given map of samples.
45 /// \returns status code of the file update operation.
46 std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
47 if (std::error_code EC = writeHeader(ProfileMap))
49 for (const auto &I : ProfileMap) {
50 const FunctionSamples &Profile = I.second;
51 if (std::error_code EC = write(Profile))
54 return sampleprof_error::success;
57 raw_ostream &getOutputStream() { return *OutputStream; }
59 /// Profile writer factory.
61 /// Create a new file writer based on the value of \p Format.
62 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
63 create(StringRef Filename, SampleProfileFormat Format);
65 /// Create a new stream writer based on the value of \p Format.
67 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
68 create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
71 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
72 : OutputStream(std::move(OS)) {}
74 /// \brief Write a file header for the profile file.
75 virtual std::error_code
76 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
78 /// \brief Output stream where to emit the profile to.
79 std::unique_ptr<raw_ostream> OutputStream;
81 /// \brief Profile summary.
82 std::unique_ptr<ProfileSummary> Summary;
84 /// \brief Compute summary for this profile.
85 void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
88 /// \brief Sample-based profile writer (text format).
89 class SampleProfileWriterText : public SampleProfileWriter {
91 std::error_code write(const FunctionSamples &S) override;
94 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
95 : SampleProfileWriter(OS), Indent(0) {}
98 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
99 return sampleprof_error::success;
103 /// Indent level to use when writing.
105 /// This is used when printing inlined callees.
108 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
109 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
110 SampleProfileFormat Format);
113 /// \brief Sample-based profile writer (binary format).
114 class SampleProfileWriterBinary : public SampleProfileWriter {
116 std::error_code write(const FunctionSamples &S) override;
119 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
120 : SampleProfileWriter(OS) {}
123 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
124 std::error_code writeSummary();
125 std::error_code writeNameIdx(StringRef FName);
126 std::error_code writeBody(const FunctionSamples &S);
129 void addName(StringRef FName);
130 void addNames(const FunctionSamples &S);
132 MapVector<StringRef, uint32_t> NameTable;
134 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
135 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
136 SampleProfileFormat Format);
139 } // end namespace sampleprof
140 } // end namespace llvm
142 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H