Debian Codegraph @

suite/synctools/tablegen/include/llvm/ProfileData/InstrProfWriter.h

raw symbols SCIP
1 //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing profiling data for instrumentation
10 // based PGO and coverage.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
15 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ProfileData/InstrProf.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include <cstdint>
24 #include <memory>
25
26 namespace llvm {
27
28 /// Writer for instrumentation based profile data.
29 class InstrProfRecordWriterTrait;
30 class ProfOStream;
31 class raw_fd_ostream;
32
33 class InstrProfWriter {
34 public:
35 using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
36
37 private:
38 bool Sparse;
39 StringMap<ProfilingData> FunctionData;
40 // An enum describing the attributes of the profile.
41 InstrProfKind ProfileKind = InstrProfKind::Unknown;
42 // Use raw pointer here for the incomplete type object.
43 InstrProfRecordWriterTrait *InfoObj;
44
45 public:
46 InstrProfWriter(bool Sparse = false);
47 ~InstrProfWriter();
48
49 StringMap<ProfilingData> &getProfileData() { return FunctionData; }
50
51 /// Add function counts for the given function. If there are already counts
52 /// for this function and the hash and number of counts match, each counter is
53 /// summed. Optionally scale counts by \p Weight.
54 void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
55 function_ref<void(Error)> Warn);
56 void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
57 addRecord(std::move(I), 1, Warn);
58 }
59
60 /// Merge existing function counts from the given writer.
61 void mergeRecordsFromWriter(InstrProfWriter &&IPW,
62 function_ref<void(Error)> Warn);
63
64 /// Write the profile to \c OS
65 Error write(raw_fd_ostream &OS);
66
67 /// Write the profile in text format to \c OS
68 Error writeText(raw_fd_ostream &OS);
69
70 Error validateRecord(const InstrProfRecord &Func);
71
72 /// Write \c Record in text format to \c OS
73 static void writeRecordInText(StringRef Name, uint64_t Hash,
74 const InstrProfRecord &Counters,
75 InstrProfSymtab &Symtab, raw_fd_ostream &OS);
76
77 /// Write the profile, returning the raw data. For testing.
78 std::unique_ptr<MemoryBuffer> writeBuffer();
79
80 /// Update the attributes of the current profile from the attributes
81 /// specified. An error is returned if IR and FE profiles are mixed.
82 Error mergeProfileKind(const InstrProfKind Other) {
83 // If the kind is unset, this is the first profile we are merging so just
84 // set it to the given type.
85 if (ProfileKind == InstrProfKind::Unknown) {
86 ProfileKind = Other;
87 return Error::success();
88 }
89
90 // Returns true if merging is should fail assuming A and B are incompatible.
91 auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
92 return (static_cast<bool>(ProfileKind & A) &&
93 static_cast<bool>(Other & B)) ||
94 (static_cast<bool>(ProfileKind & B) &&
95 static_cast<bool>(Other & A));
96 };
97
98 // Check if the profiles are in-compatible. Clang frontend profiles can't be
99 // merged with other profile types.
100 if (static_cast<bool>((ProfileKind & InstrProfKind::FE) ^
101 (Other & InstrProfKind::FE))) {
102 return make_error<InstrProfError>(instrprof_error::unsupported_version);
103 }
104 if (testIncompatible(InstrProfKind::FunctionEntryOnly, InstrProfKind::BB)) {
105 return make_error<InstrProfError>(
106 instrprof_error::unsupported_version,
107 "cannot merge FunctionEntryOnly profiles and BB profiles together");
108 }
109
110 // Now we update the profile type with the bits that are set.
111 ProfileKind |= Other;
112 return Error::success();
113 }
114
115 // Internal interface for testing purpose only.
116 void setValueProfDataEndianness(support::endianness Endianness);
117 void setOutputSparse(bool Sparse);
118 // Compute the overlap b/w this object and Other. Program level result is
119 // stored in Overlap and function level result is stored in FuncLevelOverlap.
120 void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
121 OverlapStats &FuncLevelOverlap,
122 const OverlapFuncFilters &FuncFilter);
123
124 private:
125 void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
126 uint64_t Weight, function_ref<void(Error)> Warn);
127 bool shouldEncodeData(const ProfilingData &PD);
128
129 Error writeImpl(ProfOStream &OS);
130 };
131
132 } // end namespace llvm
133
134 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H