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/MapVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Object/BuildID.h"
22 #include "llvm/ProfileData/InstrProf.h"
23 #include "llvm/ProfileData/MemProf.h"
24 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/Error.h"
26 #include <cstdint>
27 #include <memory>
28 #include <random>
29 
30 namespace llvm {
31 
32 /// Writer for instrumentation based profile data.
33 class InstrProfRecordWriterTrait;
34 class ProfOStream;
35 class MemoryBuffer;
36 class raw_fd_ostream;
37 
38 class InstrProfWriter {
39 public:
40   using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
41 
42 private:
43   bool Sparse;
44   StringMap<ProfilingData> FunctionData;
45   /// The maximum length of a single temporal profile trace.
46   uint64_t MaxTemporalProfTraceLength;
47   /// The maximum number of stored temporal profile traces.
48   uint64_t TemporalProfTraceReservoirSize;
49   /// The total number of temporal profile traces seen.
50   uint64_t TemporalProfTraceStreamSize = 0;
51   /// The list of temporal profile traces.
52   SmallVector<TemporalProfTraceTy> TemporalProfTraces;
53   std::mt19937 RNG;
54 
55   // A map to hold memprof data per function. The lower 64 bits obtained from
56   // the md5 hash of the function name is used to index into the map.
57   llvm::MapVector<GlobalValue::GUID, memprof::IndexedMemProfRecord>
58       MemProfRecordData;
59   // A map to hold frame id to frame mappings. The mappings are used to
60   // convert IndexedMemProfRecord to MemProfRecords with frame information
61   // inline.
62   llvm::MapVector<memprof::FrameId, memprof::Frame> MemProfFrameData;
63 
64   // List of binary ids.
65   std::vector<llvm::object::BuildID> BinaryIds;
66 
67   // An enum describing the attributes of the profile.
68   InstrProfKind ProfileKind = InstrProfKind::Unknown;
69   // Use raw pointer here for the incomplete type object.
70   InstrProfRecordWriterTrait *InfoObj;
71 
72 public:
73   InstrProfWriter(bool Sparse = false,
74                   uint64_t TemporalProfTraceReservoirSize = 0,
75                   uint64_t MaxTemporalProfTraceLength = 0);
76   ~InstrProfWriter();
77 
78   StringMap<ProfilingData> &getProfileData() { return FunctionData; }
79 
80   /// Add function counts for the given function. If there are already counts
81   /// for this function and the hash and number of counts match, each counter is
82   /// summed. Optionally scale counts by \p Weight.
83   void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
84                  function_ref<void(Error)> Warn);
85   void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
86     addRecord(std::move(I), 1, Warn);
87   }
88 
89   /// Add \p SrcTraces using reservoir sampling where \p SrcStreamSize is the
90   /// total number of temporal profiling traces the source has seen.
91   void addTemporalProfileTraces(SmallVectorImpl<TemporalProfTraceTy> &SrcTraces,
92                                 uint64_t SrcStreamSize);
93 
94   /// Add a memprof record for a function identified by its \p Id.
95   void addMemProfRecord(const GlobalValue::GUID Id,
96                         const memprof::IndexedMemProfRecord &Record);
97 
98   /// Add a memprof frame identified by the hash of the contents of the frame in
99   /// \p FrameId.
100   bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F,
101                        function_ref<void(Error)> Warn);
102 
103   // Add a binary id to the binary ids list.
104   void addBinaryIds(ArrayRef<llvm::object::BuildID> BIs);
105 
106   /// Merge existing function counts from the given writer.
107   void mergeRecordsFromWriter(InstrProfWriter &&IPW,
108                               function_ref<void(Error)> Warn);
109 
110   /// Write the profile to \c OS
111   Error write(raw_fd_ostream &OS);
112 
113   /// Write the profile to a string output stream \c OS
114   Error write(raw_string_ostream &OS);
115 
116   /// Write the profile in text format to \c OS
117   Error writeText(raw_fd_ostream &OS);
118 
119   /// Write temporal profile trace data to the header in text format to \c OS
120   void writeTextTemporalProfTraceData(raw_fd_ostream &OS,
121                                       InstrProfSymtab &Symtab);
122 
123   Error validateRecord(const InstrProfRecord &Func);
124 
125   /// Write \c Record in text format to \c OS
126   static void writeRecordInText(StringRef Name, uint64_t Hash,
127                                 const InstrProfRecord &Counters,
128                                 InstrProfSymtab &Symtab, raw_fd_ostream &OS);
129 
130   /// Write the profile, returning the raw data. For testing.
131   std::unique_ptr<MemoryBuffer> writeBuffer();
132 
133   /// Update the attributes of the current profile from the attributes
134   /// specified. An error is returned if IR and FE profiles are mixed.
135   Error mergeProfileKind(const InstrProfKind Other) {
136     // If the kind is unset, this is the first profile we are merging so just
137     // set it to the given type.
138     if (ProfileKind == InstrProfKind::Unknown) {
139       ProfileKind = Other;
140       return Error::success();
141     }
142 
143     // Returns true if merging is should fail assuming A and B are incompatible.
144     auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
145       return (static_cast<bool>(ProfileKind & A) &&
146               static_cast<bool>(Other & B)) ||
147              (static_cast<bool>(ProfileKind & B) &&
148               static_cast<bool>(Other & A));
149     };
150 
151     // Check if the profiles are in-compatible. Clang frontend profiles can't be
152     // merged with other profile types.
153     if (static_cast<bool>(
154             (ProfileKind & InstrProfKind::FrontendInstrumentation) ^
155             (Other & InstrProfKind::FrontendInstrumentation))) {
156       return make_error<InstrProfError>(instrprof_error::unsupported_version);
157     }
158     if (testIncompatible(InstrProfKind::FunctionEntryOnly,
159                          InstrProfKind::FunctionEntryInstrumentation)) {
160       return make_error<InstrProfError>(
161           instrprof_error::unsupported_version,
162           "cannot merge FunctionEntryOnly profiles and BB profiles together");
163     }
164 
165     // Now we update the profile type with the bits that are set.
166     ProfileKind |= Other;
167     return Error::success();
168   }
169 
170   InstrProfKind getProfileKind() const { return ProfileKind; }
171 
172   // Internal interface for testing purpose only.
173   void setValueProfDataEndianness(support::endianness Endianness);
174   void setOutputSparse(bool Sparse);
175   // Compute the overlap b/w this object and Other. Program level result is
176   // stored in Overlap and function level result is stored in FuncLevelOverlap.
177   void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
178                      OverlapStats &FuncLevelOverlap,
179                      const OverlapFuncFilters &FuncFilter);
180 
181 private:
182   void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
183                  uint64_t Weight, function_ref<void(Error)> Warn);
184   bool shouldEncodeData(const ProfilingData &PD);
185   /// Add \p Trace using reservoir sampling.
186   void addTemporalProfileTrace(TemporalProfTraceTy Trace);
187 
188   Error writeImpl(ProfOStream &OS);
189 };
190 
191 } // end namespace llvm
192 
193 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
194