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);
48 offset_type M = (1 + V->Counts.size()) * sizeof(uint64_t);
49 LE.write<offset_type>(M);
51 return std::make_pair(N, M);
54 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
55 Out.write(K.data(), N);
58 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
60 using namespace llvm::support;
61 endian::Writer<little> LE(Out);
62 LE.write<uint64_t>(V->Hash);
63 for (uint64_t I : V->Counts)
64 LE.write<uint64_t>(I);
70 InstrProfWriter::addFunctionCounts(StringRef FunctionName,
71 uint64_t FunctionHash,
72 ArrayRef<uint64_t> Counters) {
73 auto Where = FunctionData.find(FunctionName);
74 if (Where == FunctionData.end()) {
75 // If this is the first time we've seen this function, just add it.
76 auto &Data = FunctionData[FunctionName];
77 Data.Hash = FunctionHash;
78 Data.Counts = Counters;
79 return instrprof_error::success;
82 auto &Data = Where->getValue();
83 // We can only add to existing functions if they match, so we check the hash
84 // and number of counters.
85 if (Data.Hash != FunctionHash)
86 return instrprof_error::hash_mismatch;
87 if (Data.Counts.size() != Counters.size())
88 return instrprof_error::count_mismatch;
89 // These match, add up the counters.
90 for (size_t I = 0, E = Counters.size(); I < E; ++I) {
91 if (Data.Counts[I] + Counters[I] < Data.Counts[I])
92 return instrprof_error::counter_overflow;
93 Data.Counts[I] += Counters[I];
95 return instrprof_error::success;
98 void InstrProfWriter::write(raw_fd_ostream &OS) {
99 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
100 uint64_t MaxFunctionCount = 0;
102 // Populate the hash table generator.
103 for (const auto &I : FunctionData) {
104 Generator.insert(I.getKey(), &I.getValue());
105 if (I.getValue().Counts[0] > MaxFunctionCount)
106 MaxFunctionCount = I.getValue().Counts[0];
109 using namespace llvm::support;
110 endian::Writer<little> LE(OS);
113 LE.write<uint64_t>(IndexedInstrProf::Magic);
114 LE.write<uint64_t>(IndexedInstrProf::Version);
115 LE.write<uint64_t>(MaxFunctionCount);
116 LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType));
118 // Save a space to write the hash table start location.
119 uint64_t HashTableStartLoc = OS.tell();
120 LE.write<uint64_t>(0);
121 // Write the hash table.
122 uint64_t HashTableStart = Generator.Emit(OS);
124 // Go back and fill in the hash table start.
125 OS.seek(HashTableStartLoc);
126 LE.write<uint64_t>(HashTableStart);