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/SampleProf.h"
19 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/raw_ostream.h"
25 namespace sampleprof {
27 enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
29 /// \brief Sample-based profile writer. Base class.
30 class SampleProfileWriter {
32 SampleProfileWriter(StringRef Filename, std::error_code &EC,
33 sys::fs::OpenFlags Flags)
34 : OS(Filename, EC, Flags) {}
35 virtual ~SampleProfileWriter() {}
37 /// Write sample profiles in \p S for function \p FName.
39 /// \returns status code of the file update operation.
40 virtual std::error_code write(StringRef FName, 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) {
46 if (std::error_code EC = writeHeader(ProfileMap))
49 for (const auto &I : ProfileMap) {
50 StringRef FName = I.first();
51 const FunctionSamples &Profile = I.second;
52 if (std::error_code EC = write(FName, Profile))
55 return sampleprof_error::success;
58 /// Profile writer factory.
60 /// Create a new writer based on the value of \p Format.
61 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
62 create(StringRef Filename, SampleProfileFormat Format);
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.
73 /// \brief Sample-based profile writer (text format).
74 class SampleProfileWriterText : public SampleProfileWriter {
76 SampleProfileWriterText(StringRef F, std::error_code &EC)
77 : SampleProfileWriter(F, EC, sys::fs::F_Text), Indent(0) {}
79 std::error_code write(StringRef FName, const FunctionSamples &S) override;
83 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
84 return sampleprof_error::success;
88 /// Indent level to use when writing.
90 /// This is used when printing inlined callees.
94 /// \brief Sample-based profile writer (binary format).
95 class SampleProfileWriterBinary : public SampleProfileWriter {
97 SampleProfileWriterBinary(StringRef F, std::error_code &EC)
98 : SampleProfileWriter(F, EC, sys::fs::F_None), NameTable() {}
100 std::error_code write(StringRef F, const FunctionSamples &S) override;
104 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
105 std::error_code writeNameIdx(StringRef FName);
106 std::error_code writeBody(StringRef FName, const FunctionSamples &S);
109 void addName(StringRef FName);
110 void addNames(const FunctionSamples &S);
112 MapVector<StringRef, uint32_t> NameTable;
115 } // End namespace sampleprof
117 } // End namespace llvm
119 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H