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;
33 class InstrProfWriter {
35 typedef SmallDenseMap<uint64_t, InstrProfRecord, 1> ProfilingData;
36 enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel };
40 StringMap<ProfilingData> FunctionData;
41 ProfKind ProfileKind = PF_Unknown;
42 // Use raw pointer here for the incomplete type object.
43 InstrProfRecordWriterTrait *InfoObj;
46 InstrProfWriter(bool Sparse = false);
49 /// Add function counts for the given function. If there are already counts
50 /// for this function and the hash and number of counts match, each counter is
51 /// summed. Optionally scale counts by \p Weight.
52 Error addRecord(InstrProfRecord &&I, uint64_t Weight = 1);
54 /// Merge existing function counts from the given writer.
55 Error mergeRecordsFromWriter(InstrProfWriter &&IPW);
57 /// Write the profile to \c OS
58 void write(raw_fd_ostream &OS);
60 /// Write the profile in text format to \c OS
61 void writeText(raw_fd_ostream &OS);
63 /// Write \c Record in text format to \c OS
64 static void writeRecordInText(const InstrProfRecord &Record,
65 InstrProfSymtab &Symtab, raw_fd_ostream &OS);
67 /// Write the profile, returning the raw data. For testing.
68 std::unique_ptr<MemoryBuffer> writeBuffer();
70 /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
71 Error setIsIRLevelProfile(bool IsIRLevel) {
72 if (ProfileKind == PF_Unknown) {
73 ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE;
74 return Error::success();
76 return (IsIRLevel == (ProfileKind == PF_IRLevel))
78 : make_error<InstrProfError>(
79 instrprof_error::unsupported_version);
82 // Internal interface for testing purpose only.
83 void setValueProfDataEndianness(support::endianness Endianness);
84 void setOutputSparse(bool Sparse);
87 bool shouldEncodeData(const ProfilingData &PD);
88 void writeImpl(ProfOStream &OS);
91 } // end namespace llvm
93 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H