1 //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing profiling data for clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfWriter.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/EndianStream.h"
18 #include "llvm/Support/OnDiskHashTable.h"
20 #include "InstrProfIndexed.h"
25 class InstrProfRecordTrait {
27 typedef StringRef key_type;
28 typedef StringRef key_type_ref;
30 typedef const InstrProfWriter::CounterData *const data_type;
31 typedef const InstrProfWriter::CounterData *const data_type_ref;
33 typedef uint64_t hash_value_type;
34 typedef uint64_t offset_type;
36 static hash_value_type ComputeHash(key_type_ref K) {
37 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
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);
45 offset_type N = K.size();
46 LE.write<offset_type>(N);
49 for (const auto &Counts : *V)
50 M += (2 + Counts.second.size()) * sizeof(uint64_t);
51 LE.write<offset_type>(M);
53 return std::make_pair(N, M);
56 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
57 Out.write(K.data(), N);
60 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
62 using namespace llvm::support;
63 endian::Writer<little> LE(Out);
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);
76 InstrProfWriter::addFunctionCounts(StringRef FunctionName,
77 uint64_t FunctionHash,
78 ArrayRef<uint64_t> Counters) {
79 auto &CounterData = FunctionData[FunctionName];
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;
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
95 if (FoundCounters.size() != Counters.size())
96 return instrprof_error::count_mismatch;
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];
103 // We keep track of the max function count as we go for simplicity.
104 if (FoundCounters[0] > MaxFunctionCount)
105 MaxFunctionCount = FoundCounters[0];
107 return instrprof_error::success;
110 void InstrProfWriter::write(raw_fd_ostream &OS) {
111 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
113 // Populate the hash table generator.
114 std::vector<uint64_t> CounterBuffer;
115 for (const auto &I : FunctionData)
116 Generator.insert(I.getKey(), &I.getValue());
118 using namespace llvm::support;
119 endian::Writer<little> LE(OS);
122 LE.write<uint64_t>(IndexedInstrProf::Magic);
123 LE.write<uint64_t>(IndexedInstrProf::Version);
124 LE.write<uint64_t>(MaxFunctionCount);
125 LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType));
127 // Save a space to write the hash table start location.
128 uint64_t HashTableStartLoc = OS.tell();
129 LE.write<uint64_t>(0);
130 // Write the hash table.
131 uint64_t HashTableStart = Generator.Emit(OS);
133 // Go back and fill in the hash table start.
134 OS.seek(HashTableStartLoc);
135 LE.write<uint64_t>(HashTableStart);