LLVM  7.0.0
SampleProfWriter.h
Go to the documentation of this file.
1 //===- SampleProfWriter.h - Write LLVM sample profile data ------*- C++ -*-===//
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/MapVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/ProfileSummary.h"
21 #include "llvm/Support/ErrorOr.h"
23 #include <algorithm>
24 #include <cstdint>
25 #include <memory>
26 #include <set>
27 #include <system_error>
28 
29 namespace llvm {
30 namespace sampleprof {
31 
32 /// Sample-based profile writer. Base class.
34 public:
35  virtual ~SampleProfileWriter() = default;
36 
37  /// Write sample profiles in \p S.
38  ///
39  /// \returns status code of the file update operation.
40  virtual std::error_code write(const FunctionSamples &S) = 0;
41 
42  /// Write all the sample profiles in the given map of samples.
43  ///
44  /// \returns status code of the file update operation.
45  std::error_code write(const StringMap<FunctionSamples> &ProfileMap);
46 
48 
49  /// Profile writer factory.
50  ///
51  /// Create a new file writer based on the value of \p Format.
54 
55  /// Create a new stream writer based on the value of \p Format.
56  /// For testing.
58  create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
59 
60 protected:
61  SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
62  : OutputStream(std::move(OS)) {}
63 
64  /// Write a file header for the profile file.
65  virtual std::error_code
66  writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
67 
68  /// Output stream where to emit the profile to.
69  std::unique_ptr<raw_ostream> OutputStream;
70 
71  /// Profile summary.
72  std::unique_ptr<ProfileSummary> Summary;
73 
74  /// Compute summary for this profile.
75  void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
76 };
77 
78 /// Sample-based profile writer (text format).
80 public:
81  std::error_code write(const FunctionSamples &S) override;
82 
83 protected:
84  SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
85  : SampleProfileWriter(OS), Indent(0) {}
86 
87  std::error_code
88  writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
90  }
91 
92 private:
93  /// Indent level to use when writing.
94  ///
95  /// This is used when printing inlined callees.
96  unsigned Indent;
97 
99  SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
101 };
102 
103 /// Sample-based profile writer (binary format).
105 public:
106  std::error_code write(const FunctionSamples &S) override;
107  SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
108  : SampleProfileWriter(OS) {}
109 
110 protected:
111  virtual std::error_code writeNameTable() = 0;
112  virtual std::error_code writeMagicIdent() = 0;
113  std::error_code writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
114  std::error_code writeSummary();
115  std::error_code writeNameIdx(StringRef FName);
116  std::error_code writeBody(const FunctionSamples &S);
117  inline void stablizeNameTable(std::set<StringRef> &V);
118 
120 
121 private:
122  void addName(StringRef FName);
123  void addNames(const FunctionSamples &S);
124 
126  SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
127  SampleProfileFormat Format);
128 };
129 
132 
133 protected:
134  virtual std::error_code writeNameTable() override;
135  virtual std::error_code writeMagicIdent() override;
136 };
137 
140 
141 protected:
142  virtual std::error_code writeNameTable() override;
143  virtual std::error_code writeMagicIdent() override;
144 };
145 
146 } // end namespace sampleprof
147 } // end namespace llvm
148 
149 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
Represents either an error or a value T.
Definition: ErrorOr.h:57
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
MapVector< StringRef, uint32_t > NameTable
Sample-based profile writer. Base class.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
SampleProfileWriterBinary(std::unique_ptr< raw_ostream > &OS)
Representation of the samples collected for a function.
Definition: SampleProf.h:216
Definition: BitVector.h:921
std::unique_ptr< ProfileSummary > Summary
Profile summary.
Sample-based profile writer (binary format).
virtual std::error_code write(const FunctionSamples &S)=0
Write sample profiles in S.
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
SampleProfileWriterText(std::unique_ptr< raw_ostream > &OS)
Provides ErrorOr<T> smart pointer.
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap)=0
Write a file header for the profile file.
Definition: JSON.cpp:592
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
SampleProfileWriter(std::unique_ptr< raw_ostream > &OS)
void computeSummary(const StringMap< FunctionSamples > &ProfileMap)
Compute summary for this profile.
Sample-based profile writer (text format).