Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 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" |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/EndianStream.h" |
| 18 | #include "llvm/Support/OnDiskHashTable.h" |
| 19 | |
| 20 | #include "InstrProfIndexed.h" |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class InstrProfRecordTrait { |
| 26 | public: |
| 27 | typedef StringRef key_type; |
| 28 | typedef StringRef key_type_ref; |
| 29 | |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame^] | 30 | typedef const InstrProfWriter::CounterData *const data_type; |
| 31 | typedef const InstrProfWriter::CounterData *const data_type_ref; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 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 | |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 45 | offset_type N = K.size(); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 46 | LE.write<offset_type>(N); |
| 47 | |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame^] | 48 | offset_type M = (1 + V->Counts.size()) * sizeof(uint64_t); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 49 | LE.write<offset_type>(M); |
| 50 | |
| 51 | return std::make_pair(N, M); |
| 52 | } |
| 53 | |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 54 | static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){ |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 55 | Out.write(K.data(), N); |
| 56 | } |
| 57 | |
| 58 | static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 59 | offset_type) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 60 | using namespace llvm::support; |
| 61 | endian::Writer<little> LE(Out); |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame^] | 62 | LE.write<uint64_t>(V->Hash); |
| 63 | for (uint64_t I : V->Counts) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 64 | LE.write<uint64_t>(I); |
| 65 | } |
| 66 | }; |
| 67 | } |
| 68 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 69 | error_code InstrProfWriter::addFunctionCounts(StringRef FunctionName, |
| 70 | uint64_t FunctionHash, |
| 71 | ArrayRef<uint64_t> Counters) { |
| 72 | auto Where = FunctionData.find(FunctionName); |
| 73 | if (Where == FunctionData.end()) { |
| 74 | // If this is the first time we've seen this function, just add it. |
Justin Bogner | 47b8768 | 2014-03-21 18:22:16 +0000 | [diff] [blame] | 75 | auto &Data = FunctionData[FunctionName]; |
| 76 | Data.Hash = FunctionHash; |
| 77 | Data.Counts = Counters; |
Justin Bogner | a2bfd66 | 2014-04-19 23:42:50 +0000 | [diff] [blame] | 78 | return instrprof_error::success; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | auto &Data = Where->getValue(); |
| 82 | // We can only add to existing functions if they match, so we check the hash |
| 83 | // and number of counters. |
| 84 | if (Data.Hash != FunctionHash) |
| 85 | return instrprof_error::hash_mismatch; |
| 86 | if (Data.Counts.size() != Counters.size()) |
| 87 | return instrprof_error::count_mismatch; |
| 88 | // These match, add up the counters. |
| 89 | for (size_t I = 0, E = Counters.size(); I < E; ++I) { |
| 90 | if (Data.Counts[I] + Counters[I] < Data.Counts[I]) |
| 91 | return instrprof_error::counter_overflow; |
| 92 | Data.Counts[I] += Counters[I]; |
| 93 | } |
| 94 | return instrprof_error::success; |
| 95 | } |
| 96 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 97 | void InstrProfWriter::write(raw_fd_ostream &OS) { |
| 98 | OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator; |
| 99 | uint64_t MaxFunctionCount = 0; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 100 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 101 | // Populate the hash table generator. |
| 102 | for (const auto &I : FunctionData) { |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame^] | 103 | Generator.insert(I.getKey(), &I.getValue()); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 104 | if (I.getValue().Counts[0] > MaxFunctionCount) |
| 105 | MaxFunctionCount = I.getValue().Counts[0]; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 106 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 107 | |
| 108 | using namespace llvm::support; |
| 109 | endian::Writer<little> LE(OS); |
| 110 | |
| 111 | // Write the header. |
| 112 | LE.write<uint64_t>(IndexedInstrProf::Magic); |
| 113 | LE.write<uint64_t>(IndexedInstrProf::Version); |
| 114 | LE.write<uint64_t>(MaxFunctionCount); |
| 115 | LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType)); |
| 116 | |
| 117 | // Save a space to write the hash table start location. |
| 118 | uint64_t HashTableStartLoc = OS.tell(); |
| 119 | LE.write<uint64_t>(0); |
| 120 | // Write the hash table. |
| 121 | uint64_t HashTableStart = Generator.Emit(OS); |
| 122 | |
| 123 | // Go back and fill in the hash table start. |
| 124 | OS.seek(HashTableStartLoc); |
| 125 | LE.write<uint64_t>(HashTableStart); |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 126 | } |