1 //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=// 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 support for writing profiling data for clang's 11 // instrumentation based PGO and coverage. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ProfileData/InstrProfWriter.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/Support/EndianStream.h" 18 #include "llvm/Support/OnDiskHashTable.h" 19 20 #include "InstrProfIndexed.h" 21 22 using namespace llvm; 23 24 namespace { 25 class InstrProfRecordTrait { 26 public: 27 typedef StringRef key_type; 28 typedef StringRef key_type_ref; 29 30 typedef const InstrProfWriter::CounterData *const data_type; 31 typedef const InstrProfWriter::CounterData *const data_type_ref; 32 33 typedef uint64_t hash_value_type; 34 typedef uint64_t offset_type; 35 36 static hash_value_type ComputeHash(key_type_ref K) { 37 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K); 38 } 39 40 static std::pair<offset_type, offset_type> 41 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) { 42 using namespace llvm::support; 43 endian::Writer<little> LE(Out); 44 45 offset_type N = K.size(); 46 LE.write<offset_type>(N); 47 48 offset_type M = 0; 49 for (const auto &Counts : *V) 50 M += (2 + Counts.second.size()) * sizeof(uint64_t); 51 LE.write<offset_type>(M); 52 53 return std::make_pair(N, M); 54 } 55 56 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){ 57 Out.write(K.data(), N); 58 } 59 60 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, 61 offset_type) { 62 using namespace llvm::support; 63 endian::Writer<little> LE(Out); 64 65 for (const auto &Counts : *V) { 66 LE.write<uint64_t>(Counts.first); 67 LE.write<uint64_t>(Counts.second.size()); 68 for (uint64_t I : Counts.second) 69 LE.write<uint64_t>(I); 70 } 71 } 72 }; 73 } 74 75 std::error_code 76 InstrProfWriter::addFunctionCounts(StringRef FunctionName, 77 uint64_t FunctionHash, 78 ArrayRef<uint64_t> Counters) { 79 auto &CounterData = FunctionData[FunctionName]; 80 81 auto Where = CounterData.find(FunctionHash); 82 if (Where == CounterData.end()) { 83 // We've never seen a function with this name and hash, add it. 84 CounterData[FunctionHash] = Counters; 85 // We keep track of the max function count as we go for simplicity. 86 if (Counters[0] > MaxFunctionCount) 87 MaxFunctionCount = Counters[0]; 88 return instrprof_error::success; 89 } 90 91 // We're updating a function we've seen before. 92 auto &FoundCounters = Where->second; 93 // If the number of counters doesn't match we either have bad data or a hash 94 // collision. 95 if (FoundCounters.size() != Counters.size()) 96 return instrprof_error::count_mismatch; 97 98 for (size_t I = 0, E = Counters.size(); I < E; ++I) { 99 if (FoundCounters[I] + Counters[I] < FoundCounters[I]) 100 return instrprof_error::counter_overflow; 101 FoundCounters[I] += Counters[I]; 102 } 103 // We keep track of the max function count as we go for simplicity. 104 if (FoundCounters[0] > MaxFunctionCount) 105 MaxFunctionCount = FoundCounters[0]; 106 107 return instrprof_error::success; 108 } 109 110 void InstrProfWriter::write(raw_fd_ostream &OS) { 111 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator; 112 113 // Populate the hash table generator. 114 for (const auto &I : FunctionData) 115 Generator.insert(I.getKey(), &I.getValue()); 116 117 using namespace llvm::support; 118 endian::Writer<little> LE(OS); 119 120 // Write the header. 121 LE.write<uint64_t>(IndexedInstrProf::Magic); 122 LE.write<uint64_t>(IndexedInstrProf::Version); 123 LE.write<uint64_t>(MaxFunctionCount); 124 LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType)); 125 126 // Save a space to write the hash table start location. 127 uint64_t HashTableStartLoc = OS.tell(); 128 LE.write<uint64_t>(0); 129 // Write the hash table. 130 uint64_t HashTableStart = Generator.Emit(OS); 131 132 // Go back and fill in the hash table start. 133 OS.seek(HashTableStartLoc); 134 LE.write<uint64_t>(HashTableStart); 135 } 136