1 //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains support for writing profiling data for instrumentation 11 // based PGO and coverage. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H 16 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H 17 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" 24 #include <cstdint> 25 #include <memory> 26 27 namespace llvm { 28 29 /// Writer for instrumentation based profile data. 30 class InstrProfRecordWriterTrait; 31 class ProfOStream; 32 33 class InstrProfWriter { 34 public: 35 typedef SmallDenseMap<uint64_t, InstrProfRecord, 1> ProfilingData; 36 enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel }; 37 38 private: 39 bool Sparse; 40 StringMap<ProfilingData> FunctionData; 41 ProfKind ProfileKind = PF_Unknown; 42 // Use raw pointer here for the incomplete type object. 43 InstrProfRecordWriterTrait *InfoObj; 44 45 public: 46 InstrProfWriter(bool Sparse = false); 47 ~InstrProfWriter(); 48 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); 53 54 /// Merge existing function counts from the given writer. 55 Error mergeRecordsFromWriter(InstrProfWriter &&IPW); 56 57 /// Write the profile to \c OS 58 void write(raw_fd_ostream &OS); 59 60 /// Write the profile in text format to \c OS 61 void writeText(raw_fd_ostream &OS); 62 63 /// Write \c Record in text format to \c OS 64 static void writeRecordInText(const InstrProfRecord &Record, 65 InstrProfSymtab &Symtab, raw_fd_ostream &OS); 66 67 /// Write the profile, returning the raw data. For testing. 68 std::unique_ptr<MemoryBuffer> writeBuffer(); 69 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(); 75 } 76 return (IsIRLevel == (ProfileKind == PF_IRLevel)) 77 ? Error::success() 78 : make_error<InstrProfError>( 79 instrprof_error::unsupported_version); 80 } 81 82 // Internal interface for testing purpose only. 83 void setValueProfDataEndianness(support::endianness Endianness); 84 void setOutputSparse(bool Sparse); 85 86 private: 87 bool shouldEncodeData(const ProfilingData &PD); 88 void writeImpl(ProfOStream &OS); 89 }; 90 91 } // end namespace llvm 92 93 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H 94