Add show and merge tools for sample PGO profiles.
[oota-llvm.git] / include / llvm / ProfileData / SampleProfWriter.h
1 //===- SampleProfWriter.h - Write LLVM sample profile data ----------------===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains definitions needed for writing sample profiles.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
14 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
15
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/FileSystem.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 namespace llvm {
24
25 namespace sampleprof {
26
27 enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
28
29 /// \brief Sample-based profile writer. Base class.
30 class SampleProfileWriter {
31 public:
32   SampleProfileWriter(StringRef Filename, std::error_code &EC,
33                       sys::fs::OpenFlags Flags)
34       : OS(Filename, EC, Flags) {}
35   virtual ~SampleProfileWriter() {}
36
37   /// \brief Write sample profiles in \p S for function \p FName.
38   ///
39   /// \returns true if the file was updated successfully. False, otherwise.
40   virtual bool write(StringRef FName, const FunctionSamples &S) = 0;
41
42   /// \brief Write sample profiles in \p S for function \p F.
43   bool write(const Function &F, const FunctionSamples &S) {
44     return write(F.getName(), S);
45   }
46
47   /// \brief Write all the sample profiles for all the functions in \p M.
48   ///
49   /// \returns true if the file was updated successfully. False, otherwise.
50   bool write(const Module &M, StringMap<FunctionSamples> &P) {
51     for (const auto &F : M) {
52       StringRef Name = F.getName();
53       if (!write(Name, P[Name]))
54         return false;
55     }
56     return true;
57   }
58
59   /// \brief Write all the sample profiles in the given map of samples.
60   ///
61   /// \returns true if the file was updated successfully. False, otherwise.
62   bool write(StringMap<FunctionSamples> &ProfileMap) {
63     for (auto &I : ProfileMap) {
64       StringRef FName = I.first();
65       FunctionSamples &Profile = I.second;
66       if (!write(FName, Profile))
67         return false;
68     }
69     return true;
70   }
71
72   /// \brief Profile writer factory. Create a new writer based on the value of
73   /// \p Format.
74   static std::error_code create(StringRef Filename,
75                                 std::unique_ptr<SampleProfileWriter> &Result,
76                                 SampleProfileFormat Format);
77
78 protected:
79   /// \brief Output stream where to emit the profile to.
80   raw_fd_ostream OS;
81 };
82
83 /// \brief Sample-based profile writer (text format).
84 class SampleProfileWriterText : public SampleProfileWriter {
85 public:
86   SampleProfileWriterText(StringRef F, std::error_code &EC)
87       : SampleProfileWriter(F, EC, sys::fs::F_Text) {}
88
89   bool write(StringRef FName, const FunctionSamples &S) override;
90   bool write(const Module &M, StringMap<FunctionSamples> &P) {
91     return SampleProfileWriter::write(M, P);
92   }
93 };
94
95 /// \brief Sample-based profile writer (binary format).
96 class SampleProfileWriterBinary : public SampleProfileWriter {
97 public:
98   SampleProfileWriterBinary(StringRef F, std::error_code &EC);
99
100   bool write(StringRef F, const FunctionSamples &S) override;
101   bool write(const Module &M, StringMap<FunctionSamples> &P) {
102     return SampleProfileWriter::write(M, P);
103   }
104 };
105
106 } // End namespace sampleprof
107
108 } // End namespace llvm
109
110 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H