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/ProfileData/InstrProf.h" 20 #include "llvm/Support/DataTypes.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 namespace llvm { 25 26 /// Writer for instrumentation based profile data. 27 class ProfOStream; 28 class InstrProfRecordWriterTrait; 29 30 class InstrProfWriter { 31 public: 32 typedef SmallDenseMap<uint64_t, InstrProfRecord, 1> ProfilingData; 33 enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel }; 34 35 private: 36 bool Sparse; 37 StringMap<ProfilingData> FunctionData; 38 ProfKind ProfileKind; 39 // Use raw pointer here for the incomplete type object. 40 InstrProfRecordWriterTrait *InfoObj; 41 42 public: 43 InstrProfWriter(bool Sparse = false); 44 ~InstrProfWriter(); 45 46 /// Add function counts for the given function. If there are already counts 47 /// for this function and the hash and number of counts match, each counter is 48 /// summed. Optionally scale counts by \p Weight. 49 Error addRecord(InstrProfRecord &&I, uint64_t Weight = 1); 50 /// Write the profile to \c OS 51 void write(raw_fd_ostream &OS); 52 /// Write the profile in text format to \c OS 53 void writeText(raw_fd_ostream &OS); 54 /// Write \c Record in text format to \c OS 55 static void writeRecordInText(const InstrProfRecord &Record, 56 InstrProfSymtab &Symtab, raw_fd_ostream &OS); 57 /// Write the profile, returning the raw data. For testing. 58 std::unique_ptr<MemoryBuffer> writeBuffer(); 59 60 /// Set the ProfileKind. Report error if mixing FE and IR level profiles. setIsIRLevelProfile(bool IsIRLevel)61 Error setIsIRLevelProfile(bool IsIRLevel) { 62 if (ProfileKind == PF_Unknown) { 63 ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE; 64 return Error::success(); 65 } 66 return (IsIRLevel == (ProfileKind == PF_IRLevel)) 67 ? Error::success() 68 : make_error<InstrProfError>( 69 instrprof_error::unsupported_version); 70 } 71 72 // Internal interface for testing purpose only. 73 void setValueProfDataEndianness(support::endianness Endianness); 74 void setOutputSparse(bool Sparse); 75 76 private: 77 bool shouldEncodeData(const ProfilingData &PD); 78 void writeImpl(ProfOStream &OS); 79 }; 80 81 } // end namespace llvm 82 83 #endif 84