1 //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===//
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 instrumentation
11 // based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
16 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ProfileData/InstrProf.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/MemoryBuffer.h"
29 /// Writer for instrumentation based profile data.
30 class InstrProfRecordWriterTrait;
34 class InstrProfWriter {
36 using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
37 enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel };
41 StringMap<ProfilingData> FunctionData;
42 ProfKind ProfileKind = PF_Unknown;
43 // Use raw pointer here for the incomplete type object.
44 InstrProfRecordWriterTrait *InfoObj;
47 InstrProfWriter(bool Sparse = false);
50 /// Add function counts for the given function. If there are already counts
51 /// for this function and the hash and number of counts match, each counter is
52 /// summed. Optionally scale counts by \p Weight.
53 void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
54 function_ref<void(Error)> Warn);
55 void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
56 addRecord(std::move(I), 1, Warn);
59 /// Merge existing function counts from the given writer.
60 void mergeRecordsFromWriter(InstrProfWriter &&IPW,
61 function_ref<void(Error)> Warn);
63 /// Write the profile to \c OS
64 void write(raw_fd_ostream &OS);
66 /// Write the profile in text format to \c OS
67 Error writeText(raw_fd_ostream &OS);
69 /// Write \c Record in text format to \c OS
70 static void writeRecordInText(StringRef Name, uint64_t Hash,
71 const InstrProfRecord &Counters,
72 InstrProfSymtab &Symtab, raw_fd_ostream &OS);
74 /// Write the profile, returning the raw data. For testing.
75 std::unique_ptr<MemoryBuffer> writeBuffer();
77 /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
78 Error setIsIRLevelProfile(bool IsIRLevel) {
79 if (ProfileKind == PF_Unknown) {
80 ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE;
81 return Error::success();
83 return (IsIRLevel == (ProfileKind == PF_IRLevel))
85 : make_error<InstrProfError>(
86 instrprof_error::unsupported_version);
89 // Internal interface for testing purpose only.
90 void setValueProfDataEndianness(support::endianness Endianness);
91 void setOutputSparse(bool Sparse);
94 void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
95 uint64_t Weight, function_ref<void(Error)> Warn);
96 bool shouldEncodeData(const ProfilingData &PD);
97 void writeImpl(ProfOStream &OS);
100 } // end namespace llvm
102 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H