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::MemProfRecord> MemProfData; 47 48 // An enum describing the attributes of the profile. 49 InstrProfKind ProfileKind = InstrProfKind::Unknown; 50 // Use raw pointer here for the incomplete type object. 51 InstrProfRecordWriterTrait *InfoObj; 52 53 public: 54 InstrProfWriter(bool Sparse = false); 55 ~InstrProfWriter(); 56 57 StringMap<ProfilingData> &getProfileData() { return FunctionData; } 58 59 /// Add function counts for the given function. If there are already counts 60 /// for this function and the hash and number of counts match, each counter is 61 /// summed. Optionally scale counts by \p Weight. 62 void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, 63 function_ref<void(Error)> Warn); 64 void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) { 65 addRecord(std::move(I), 1, Warn); 66 } 67 68 void addRecord(const GlobalValue::GUID Id, 69 const memprof::MemProfRecord &Record, 70 function_ref<void(Error)> Warn); 71 72 /// Merge existing function counts from the given writer. 73 void mergeRecordsFromWriter(InstrProfWriter &&IPW, 74 function_ref<void(Error)> Warn); 75 76 /// Write the profile to \c OS 77 Error write(raw_fd_ostream &OS); 78 79 /// Write the profile in text format to \c OS 80 Error writeText(raw_fd_ostream &OS); 81 82 Error validateRecord(const InstrProfRecord &Func); 83 84 /// Write \c Record in text format to \c OS 85 static void writeRecordInText(StringRef Name, uint64_t Hash, 86 const InstrProfRecord &Counters, 87 InstrProfSymtab &Symtab, raw_fd_ostream &OS); 88 89 /// Write the profile, returning the raw data. For testing. 90 std::unique_ptr<MemoryBuffer> writeBuffer(); 91 92 /// Update the attributes of the current profile from the attributes 93 /// specified. An error is returned if IR and FE profiles are mixed. 94 Error mergeProfileKind(const InstrProfKind Other) { 95 // If the kind is unset, this is the first profile we are merging so just 96 // set it to the given type. 97 if (ProfileKind == InstrProfKind::Unknown) { 98 ProfileKind = Other; 99 return Error::success(); 100 } 101 102 // Returns true if merging is should fail assuming A and B are incompatible. 103 auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) { 104 return (static_cast<bool>(ProfileKind & A) && 105 static_cast<bool>(Other & B)) || 106 (static_cast<bool>(ProfileKind & B) && 107 static_cast<bool>(Other & A)); 108 }; 109 110 // Check if the profiles are in-compatible. Clang frontend profiles can't be 111 // merged with other profile types. 112 if (static_cast<bool>( 113 (ProfileKind & InstrProfKind::FrontendInstrumentation) ^ 114 (Other & InstrProfKind::FrontendInstrumentation))) { 115 return make_error<InstrProfError>(instrprof_error::unsupported_version); 116 } 117 if (testIncompatible(InstrProfKind::FunctionEntryOnly, 118 InstrProfKind::FunctionEntryInstrumentation)) { 119 return make_error<InstrProfError>( 120 instrprof_error::unsupported_version, 121 "cannot merge FunctionEntryOnly profiles and BB profiles together"); 122 } 123 124 // Now we update the profile type with the bits that are set. 125 ProfileKind |= Other; 126 return Error::success(); 127 } 128 129 InstrProfKind getProfileKind() const { return ProfileKind; } 130 131 // Internal interface for testing purpose only. 132 void setValueProfDataEndianness(support::endianness Endianness); 133 void setOutputSparse(bool Sparse); 134 // Compute the overlap b/w this object and Other. Program level result is 135 // stored in Overlap and function level result is stored in FuncLevelOverlap. 136 void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, 137 OverlapStats &FuncLevelOverlap, 138 const OverlapFuncFilters &FuncFilter); 139 140 private: 141 void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I, 142 uint64_t Weight, function_ref<void(Error)> Warn); 143 bool shouldEncodeData(const ProfilingData &PD); 144 145 Error writeImpl(ProfOStream &OS); 146 }; 147 148 } // end namespace llvm 149 150 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H 151