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 auto &Data = FunctionData[FunctionName];
27 Data.Hash = FunctionHash;
28 Data.Counts = Counters;
29 return instrprof_error::success;;
32 auto &Data = Where->getValue();
33 // We can only add to existing functions if they match, so we check the hash
34 // and number of counters.
35 if (Data.Hash != FunctionHash)
36 return instrprof_error::hash_mismatch;
37 if (Data.Counts.size() != Counters.size())
38 return instrprof_error::count_mismatch;
39 // These match, add up the counters.
40 for (size_t I = 0, E = Counters.size(); I < E; ++I) {
41 if (Data.Counts[I] + Counters[I] < Data.Counts[I])
42 return instrprof_error::counter_overflow;
43 Data.Counts[I] += Counters[I];
45 return instrprof_error::success;
48 void InstrProfWriter::write(raw_ostream &OS) {
49 // Write out the counts for each function.
50 for (const auto &I : FunctionData) {
51 StringRef Name = I.getKey();
52 uint64_t Hash = I.getValue().Hash;
53 const std::vector<uint64_t> &Counts = I.getValue().Counts;
55 OS << Name << "\n" << Hash << "\n" << Counts.size() << "\n";
56 for (uint64_t Count : Counts)