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