1 //===- SampleProfWriter.h - Write LLVM sample profile data ------*- 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 definitions needed for writing sample profiles. 11 // 12 //===----------------------------------------------------------------------===// 13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 14 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 15 16 #include "llvm/ADT/MapVector.h" 17 #include "llvm/ADT/StringMap.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/IR/ProfileSummary.h" 20 #include "llvm/ProfileData/SampleProf.h" 21 #include "llvm/Support/ErrorOr.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <algorithm> 24 #include <cstdint> 25 #include <memory> 26 #include <system_error> 27 28 namespace llvm { 29 namespace sampleprof { 30 31 enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC }; 32 33 /// \brief Sample-based profile writer. Base class. 34 class SampleProfileWriter { 35 public: 36 virtual ~SampleProfileWriter() = default; 37 38 /// Write sample profiles in \p S. 39 /// 40 /// \returns status code of the file update operation. 41 virtual std::error_code write(const FunctionSamples &S) = 0; 42 43 /// Write all the sample profiles in the given map of samples. 44 /// 45 /// \returns status code of the file update operation. 46 std::error_code write(const StringMap<FunctionSamples> &ProfileMap) { 47 if (std::error_code EC = writeHeader(ProfileMap)) 48 return EC; 49 for (const auto &I : ProfileMap) { 50 const FunctionSamples &Profile = I.second; 51 if (std::error_code EC = write(Profile)) 52 return EC; 53 } 54 return sampleprof_error::success; 55 } 56 57 raw_ostream &getOutputStream() { return *OutputStream; } 58 59 /// Profile writer factory. 60 /// 61 /// Create a new file writer based on the value of \p Format. 62 static ErrorOr<std::unique_ptr<SampleProfileWriter>> 63 create(StringRef Filename, SampleProfileFormat Format); 64 65 /// Create a new stream writer based on the value of \p Format. 66 /// For testing. 67 static ErrorOr<std::unique_ptr<SampleProfileWriter>> 68 create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format); 69 70 protected: 71 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS) 72 : OutputStream(std::move(OS)) {} 73 74 /// \brief Write a file header for the profile file. 75 virtual std::error_code 76 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0; 77 78 /// \brief Output stream where to emit the profile to. 79 std::unique_ptr<raw_ostream> OutputStream; 80 81 /// \brief Profile summary. 82 std::unique_ptr<ProfileSummary> Summary; 83 84 /// \brief Compute summary for this profile. 85 void computeSummary(const StringMap<FunctionSamples> &ProfileMap); 86 }; 87 88 /// \brief Sample-based profile writer (text format). 89 class SampleProfileWriterText : public SampleProfileWriter { 90 public: 91 std::error_code write(const FunctionSamples &S) override; 92 93 protected: 94 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS) 95 : SampleProfileWriter(OS), Indent(0) {} 96 97 std::error_code 98 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override { 99 return sampleprof_error::success; 100 } 101 102 private: 103 /// Indent level to use when writing. 104 /// 105 /// This is used when printing inlined callees. 106 unsigned Indent; 107 108 friend ErrorOr<std::unique_ptr<SampleProfileWriter>> 109 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, 110 SampleProfileFormat Format); 111 }; 112 113 /// \brief Sample-based profile writer (binary format). 114 class SampleProfileWriterBinary : public SampleProfileWriter { 115 public: 116 std::error_code write(const FunctionSamples &S) override; 117 118 protected: 119 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS) 120 : SampleProfileWriter(OS) {} 121 122 std::error_code 123 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override; 124 std::error_code writeSummary(); 125 std::error_code writeNameIdx(StringRef FName); 126 std::error_code writeBody(const FunctionSamples &S); 127 128 private: 129 void addName(StringRef FName); 130 void addNames(const FunctionSamples &S); 131 132 MapVector<StringRef, uint32_t> NameTable; 133 134 friend ErrorOr<std::unique_ptr<SampleProfileWriter>> 135 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, 136 SampleProfileFormat Format); 137 }; 138 139 } // end namespace sampleprof 140 } // end namespace llvm 141 142 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 143