1 //===- SampleProfWriter.h - 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 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/StringRef.h"
18 #include "llvm/ProfileData/ProfileCommon.h"
19 #include "llvm/ProfileData/SampleProf.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/raw_ostream.h"
26 namespace sampleprof {
28 enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
30 /// \brief Sample-based profile writer. Base class.
31 class SampleProfileWriter {
33 virtual ~SampleProfileWriter() {}
35 /// Write sample profiles in \p S.
37 /// \returns status code of the file update operation.
38 virtual std::error_code write(const FunctionSamples &S) = 0;
40 /// Write all the sample profiles in the given map of samples.
42 /// \returns status code of the file update operation.
43 std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
44 if (std::error_code EC = writeHeader(ProfileMap))
46 for (const auto &I : ProfileMap) {
47 const FunctionSamples &Profile = I.second;
48 if (std::error_code EC = write(Profile))
51 return sampleprof_error::success;
54 raw_ostream &getOutputStream() { return *OutputStream; }
56 /// Profile writer factory.
58 /// Create a new file writer based on the value of \p Format.
59 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
60 create(StringRef Filename, SampleProfileFormat Format);
62 /// Create a new stream writer based on the value of \p Format.
64 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
65 create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
68 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
69 : OutputStream(std::move(OS)) {}
71 /// \brief Write a file header for the profile file.
72 virtual std::error_code
73 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
75 /// \brief Output stream where to emit the profile to.
76 std::unique_ptr<raw_ostream> OutputStream;
78 /// \brief Profile summary.
79 std::unique_ptr<ProfileSummary> Summary;
81 /// \brief Compute summary for this profile.
82 void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
85 /// \brief Sample-based profile writer (text format).
86 class SampleProfileWriterText : public SampleProfileWriter {
88 std::error_code write(const FunctionSamples &S) override;
91 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
92 : SampleProfileWriter(OS), Indent(0) {}
95 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
96 return sampleprof_error::success;
100 /// Indent level to use when writing.
102 /// This is used when printing inlined callees.
105 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
106 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
107 SampleProfileFormat Format);
110 /// \brief Sample-based profile writer (binary format).
111 class SampleProfileWriterBinary : public SampleProfileWriter {
113 std::error_code write(const FunctionSamples &S) override;
116 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
117 : SampleProfileWriter(OS), NameTable() {}
120 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
121 std::error_code writeSummary();
122 std::error_code writeNameIdx(StringRef FName);
123 std::error_code writeBody(const FunctionSamples &S);
126 void addName(StringRef FName);
127 void addNames(const FunctionSamples &S);
129 MapVector<StringRef, uint32_t> NameTable;
131 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
132 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
133 SampleProfileFormat Format);
136 } // End namespace sampleprof
138 } // End namespace llvm
140 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H