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);
48 raw_ostream &getOutputStream() { return *OutputStream; }
50 /// Profile writer factory.
52 /// Create a new file writer based on the value of \p Format.
53 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
54 create(StringRef Filename, SampleProfileFormat Format);
56 /// Create a new stream writer based on the value of \p Format.
58 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
59 create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
62 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
63 : OutputStream(std::move(OS)) {}
65 /// \brief Write a file header for the profile file.
66 virtual std::error_code
67 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
69 /// \brief Output stream where to emit the profile to.
70 std::unique_ptr<raw_ostream> OutputStream;
72 /// \brief Profile summary.
73 std::unique_ptr<ProfileSummary> Summary;
75 /// \brief Compute summary for this profile.
76 void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
79 /// \brief Sample-based profile writer (text format).
80 class SampleProfileWriterText : public SampleProfileWriter {
82 std::error_code write(const FunctionSamples &S) override;
85 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
86 : SampleProfileWriter(OS), Indent(0) {}
89 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
90 return sampleprof_error::success;
94 /// Indent level to use when writing.
96 /// This is used when printing inlined callees.
99 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
100 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
101 SampleProfileFormat Format);
104 /// \brief Sample-based profile writer (binary format).
105 class SampleProfileWriterBinary : public SampleProfileWriter {
107 std::error_code write(const FunctionSamples &S) override;
110 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
111 : SampleProfileWriter(OS) {}
114 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
115 std::error_code writeSummary();
116 std::error_code writeNameIdx(StringRef FName);
117 std::error_code writeBody(const FunctionSamples &S);
120 void addName(StringRef FName);
121 void addNames(const FunctionSamples &S);
123 MapVector<StringRef, uint32_t> NameTable;
125 friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
126 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
127 SampleProfileFormat Format);
130 } // end namespace sampleprof
131 } // end namespace llvm
133 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H