LLVM  3.7.0
SampleProfWriter.h
Go to the documentation of this file.
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"
20 #include "llvm/Support/ErrorOr.h"
23 
24 namespace llvm {
25 
26 namespace sampleprof {
27 
29 
30 /// \brief Sample-based profile writer. Base class.
32 public:
33  SampleProfileWriter(StringRef Filename, std::error_code &EC,
35  : OS(Filename, EC, Flags) {}
36  virtual ~SampleProfileWriter() {}
37 
38  /// \brief Write sample profiles in \p S for function \p FName.
39  ///
40  /// \returns true if the file was updated successfully. False, otherwise.
41  virtual bool write(StringRef FName, const FunctionSamples &S) = 0;
42 
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);
46  }
47 
48  /// \brief Write all the sample profiles for all the functions in \p M.
49  ///
50  /// \returns true if the file was updated successfully. False, otherwise.
52  for (const auto &F : M) {
53  StringRef Name = F.getName();
54  if (!write(Name, P[Name]))
55  return false;
56  }
57  return true;
58  }
59 
60  /// \brief Write all the sample profiles in the given map of samples.
61  ///
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))
68  return false;
69  }
70  return true;
71  }
72 
73  /// \brief Profile writer factory. Create a new writer based on the value of
74  /// \p Format.
76  create(StringRef Filename, SampleProfileFormat Format);
77 
78 protected:
79  /// \brief Output stream where to emit the profile to.
81 };
82 
83 /// \brief Sample-based profile writer (text format).
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;
91  return SampleProfileWriter::write(M, P);
92  }
93 };
94 
95 /// \brief Sample-based profile writer (binary format).
97 public:
98  SampleProfileWriterBinary(StringRef F, std::error_code &EC);
99 
100  bool write(StringRef F, const FunctionSamples &S) override;
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
raw_fd_ostream OS
Output stream where to emit the profile to.
Represents either an error or a value T.
Definition: ErrorOr.h:82
Sample-based profile writer. Base class.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
SampleProfileWriter(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
F(f)
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
Representation of the samples collected for a function.
Definition: SampleProf.h:167
bool write(StringRef FName, const FunctionSamples &S) override
Write samples to a text file.
bool write(const Module &M, StringMap< FunctionSamples > &P)
bool write(const Function &F, const FunctionSamples &S)
Write sample profiles in S for function F.
#define P(N)
Sample-based profile writer (binary format).
Module.h This file contains the declarations for the Module class.
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
bool write(const Module &M, StringMap< FunctionSamples > &P)
Write all the sample profiles for all the functions in M.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:214
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:592
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:345
virtual bool write(StringRef FName, const FunctionSamples &S)=0
Write sample profiles in S for function FName.
SampleProfileWriterText(StringRef F, std::error_code &EC)
bool write(StringRef F, const FunctionSamples &S) override
Write samples to a binary file.
#define I(x, y, z)
Definition: MD5.cpp:54
Provides ErrorOr<T> smart pointer.
bool write(StringMap< FunctionSamples > &ProfileMap)
Write all the sample profiles in the given map of samples.
bool write(const Module &M, StringMap< FunctionSamples > &P)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Sample-based profile writer (text format).
SampleProfileWriterBinary(StringRef F, std::error_code &EC)