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/StringRef.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Module.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 SampleProfileWriter(StringRef Filename, std::error_code &EC,
34 sys::fs::OpenFlags Flags)
35 : OS(Filename, EC, Flags) {}
36 virtual ~SampleProfileWriter() {}
38 /// \brief Write sample profiles in \p S for function \p FName.
40 /// \returns true if the file was updated successfully. False, otherwise.
41 virtual bool write(StringRef FName, const FunctionSamples &S) = 0;
43 /// \brief Write sample profiles in \p S for function \p F.
44 bool write(const Function &F, const FunctionSamples &S) {
45 return write(F.getName(), S);
48 /// \brief Write all the sample profiles for all the functions in \p M.
50 /// \returns true if the file was updated successfully. False, otherwise.
51 bool write(const Module &M, StringMap<FunctionSamples> &P) {
52 for (const auto &F : M) {
53 StringRef Name = F.getName();
54 if (!write(Name, P[Name]))
60 /// \brief Write all the sample profiles in the given map of samples.
62 /// \returns true if the file was updated successfully. False, otherwise.
63 bool write(StringMap<FunctionSamples> &ProfileMap) {
64 for (auto &I : ProfileMap) {
65 StringRef FName = I.first();
66 FunctionSamples &Profile = I.second;
67 if (!write(FName, Profile))
73 /// \brief Profile writer factory. Create a new writer based on the value of
75 static ErrorOr<std::unique_ptr<SampleProfileWriter>>
76 create(StringRef Filename, SampleProfileFormat Format);
79 /// \brief Output stream where to emit the profile to.
83 /// \brief Sample-based profile writer (text format).
84 class SampleProfileWriterText : public SampleProfileWriter {
86 SampleProfileWriterText(StringRef F, std::error_code &EC)
87 : SampleProfileWriter(F, EC, sys::fs::F_Text) {}
89 bool write(StringRef FName, const FunctionSamples &S) override;
90 bool write(const Module &M, StringMap<FunctionSamples> &P) {
91 return SampleProfileWriter::write(M, P);
95 /// \brief Sample-based profile writer (binary format).
96 class SampleProfileWriterBinary : public SampleProfileWriter {
98 SampleProfileWriterBinary(StringRef F, std::error_code &EC);
100 bool write(StringRef F, const FunctionSamples &S) override;
101 bool write(const Module &M, StringMap<FunctionSamples> &P) {
102 return SampleProfileWriter::write(M, P);
106 } // End namespace sampleprof
108 } // End namespace llvm
110 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H