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/Support/Endian.h"
20 error_code InstrProfWriter::addFunctionCounts(StringRef FunctionName,
21 uint64_t FunctionHash,
22 ArrayRef<uint64_t> Counters) {
23 auto Where = FunctionData.find(FunctionName);
24 if (Where == FunctionData.end()) {
25 // If this is the first time we've seen this function, just add it.
26 FunctionData[FunctionName] = {FunctionHash, Counters};
27 return instrprof_error::success;;
30 auto &Data = Where->getValue();
31 // We can only add to existing functions if they match, so we check the hash
32 // and number of counters.
33 if (Data.Hash != FunctionHash)
34 return instrprof_error::hash_mismatch;
35 if (Data.Counts.size() != Counters.size())
36 return instrprof_error::count_mismatch;
37 // These match, add up the counters.
38 for (size_t I = 0, E = Counters.size(); I < E; ++I) {
39 if (Data.Counts[I] + Counters[I] < Data.Counts[I])
40 return instrprof_error::counter_overflow;
41 Data.Counts[I] += Counters[I];
43 return instrprof_error::success;
46 void InstrProfWriter::write(raw_ostream &OS) {
47 // Write out the counts for each function.
48 for (const auto &I : FunctionData) {
49 StringRef Name = I.getKey();
50 uint64_t Hash = I.getValue().Hash;
51 const std::vector<uint64_t> &Counts = I.getValue().Counts;
53 OS << Name << "\n" << Hash << "\n" << Counts.size() << "\n";
54 for (uint64_t Count : Counts)