1 //===- SampleProfWriter.h - Write LLVM sample profile data ------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains definitions needed for writing sample profiles. 10 // 11 //===----------------------------------------------------------------------===// 12 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 13 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 14 15 #include "llvm/ADT/MapVector.h" 16 #include "llvm/ADT/StringMap.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/IR/ProfileSummary.h" 19 #include "llvm/ProfileData/SampleProf.h" 20 #include "llvm/Support/ErrorOr.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include <algorithm> 23 #include <cstdint> 24 #include <memory> 25 #include <set> 26 #include <system_error> 27 28 namespace llvm { 29 namespace sampleprof { 30 31 /// Sample-based profile writer. Base class. 32 class SampleProfileWriter { 33 public: 34 virtual ~SampleProfileWriter() = default; 35 36 /// Write sample profiles in \p S. 37 /// 38 /// \returns status code of the file update operation. 39 virtual std::error_code writeSample(const FunctionSamples &S) = 0; 40 41 /// Write all the sample profiles in the given map of samples. 42 /// 43 /// \returns status code of the file update operation. 44 virtual std::error_code write(const StringMap<FunctionSamples> &ProfileMap); 45 46 raw_ostream &getOutputStream() { return *OutputStream; } 47 48 /// Profile writer factory. 49 /// 50 /// Create a new file writer based on the value of \p Format. 51 static ErrorOr<std::unique_ptr<SampleProfileWriter>> 52 create(StringRef Filename, SampleProfileFormat Format); 53 54 /// Create a new stream writer based on the value of \p Format. 55 /// For testing. 56 static ErrorOr<std::unique_ptr<SampleProfileWriter>> 57 create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format); 58 59 virtual void setProfileSymbolList(ProfileSymbolList *PSL) {} 60 virtual void setToCompressAllSections() {} 61 virtual void setUseMD5() {} 62 virtual void setPartialProfile() {} 63 64 protected: 65 SampleProfileWriter(std::unique_ptr<raw_ostream> &OS) 66 : OutputStream(std::move(OS)) {} 67 68 /// Write a file header for the profile file. 69 virtual std::error_code 70 writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0; 71 72 // Write function profiles to the profile file. 73 virtual std::error_code 74 writeFuncProfiles(const StringMap<FunctionSamples> &ProfileMap); 75 76 /// Output stream where to emit the profile to. 77 std::unique_ptr<raw_ostream> OutputStream; 78 79 /// Profile summary. 80 std::unique_ptr<ProfileSummary> Summary; 81 82 /// Compute summary for this profile. 83 void computeSummary(const StringMap<FunctionSamples> &ProfileMap); 84 85 /// Profile format. 86 SampleProfileFormat Format = SPF_None; 87 }; 88 89 /// Sample-based profile writer (text format). 90 class SampleProfileWriterText : public SampleProfileWriter { 91 public: 92 std::error_code writeSample(const FunctionSamples &S) override; 93 94 protected: 95 SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS) 96 : SampleProfileWriter(OS), Indent(0) {} 97 98 std::error_code 99 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override { 100 return sampleprof_error::success; 101 } 102 103 private: 104 /// Indent level to use when writing. 105 /// 106 /// This is used when printing inlined callees. 107 unsigned Indent; 108 109 friend ErrorOr<std::unique_ptr<SampleProfileWriter>> 110 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, 111 SampleProfileFormat Format); 112 }; 113 114 /// Sample-based profile writer (binary format). 115 class SampleProfileWriterBinary : public SampleProfileWriter { 116 public: 117 SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS) 118 : SampleProfileWriter(OS) {} 119 120 virtual std::error_code writeSample(const FunctionSamples &S) override; 121 122 protected: 123 virtual std::error_code writeMagicIdent(SampleProfileFormat Format); 124 virtual std::error_code writeNameTable(); 125 virtual std::error_code 126 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override; 127 std::error_code writeSummary(); 128 std::error_code writeNameIdx(StringRef FName); 129 std::error_code writeBody(const FunctionSamples &S); 130 inline void stablizeNameTable(std::set<StringRef> &V); 131 132 MapVector<StringRef, uint32_t> NameTable; 133 134 void addName(StringRef FName); 135 void addNames(const FunctionSamples &S); 136 137 private: 138 friend ErrorOr<std::unique_ptr<SampleProfileWriter>> 139 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, 140 SampleProfileFormat Format); 141 }; 142 143 class SampleProfileWriterRawBinary : public SampleProfileWriterBinary { 144 using SampleProfileWriterBinary::SampleProfileWriterBinary; 145 }; 146 147 class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary { 148 using SampleProfileWriterBinary::SampleProfileWriterBinary; 149 public: 150 virtual std::error_code 151 write(const StringMap<FunctionSamples> &ProfileMap) override; 152 153 virtual void setToCompressAllSections() override; 154 void setToCompressSection(SecType Type); 155 virtual std::error_code writeSample(const FunctionSamples &S) override; 156 157 // Set to use MD5 to represent string in NameTable. 158 virtual void setUseMD5() override { 159 UseMD5 = true; 160 addSectionFlag(SecNameTable, SecNameTableFlags::SecFlagMD5Name); 161 } 162 163 // Set the profile to be partial. It means the profile is for 164 // common/shared code. The common profile is usually merged from 165 // profiles collected from running other targets. 166 virtual void setPartialProfile() override { 167 addSectionFlag(SecProfSummary, SecProfSummaryFlags::SecFlagPartial); 168 } 169 170 virtual void setProfileSymbolList(ProfileSymbolList *PSL) override { 171 ProfSymList = PSL; 172 }; 173 174 protected: 175 uint64_t markSectionStart(SecType Type); 176 std::error_code addNewSection(SecType Sec, uint64_t SectionStart); 177 template <class SecFlagType> 178 void addSectionFlag(SecType Type, SecFlagType Flag) { 179 for (auto &Entry : SectionHdrLayout) { 180 if (Entry.Type == Type) 181 addSecFlag(Entry, Flag); 182 } 183 } 184 185 // placeholder for subclasses to dispatch their own section writers. 186 virtual std::error_code writeCustomSection(SecType Type) = 0; 187 188 virtual void initSectionHdrLayout() = 0; 189 // specify the order to write sections. 190 virtual std::error_code 191 writeSections(const StringMap<FunctionSamples> &ProfileMap) = 0; 192 193 // Dispatch section writer for each section. 194 virtual std::error_code 195 writeOneSection(SecType Type, const StringMap<FunctionSamples> &ProfileMap); 196 197 // Helper function to write name table. 198 virtual std::error_code writeNameTable() override; 199 200 // Functions to write various kinds of sections. 201 std::error_code 202 writeNameTableSection(const StringMap<FunctionSamples> &ProfileMap); 203 std::error_code writeFuncOffsetTable(); 204 std::error_code writeProfileSymbolListSection(); 205 206 // Specifiy the order of sections in section header table. Note 207 // the order of sections in the profile may be different that the 208 // order in SectionHdrLayout. sample Reader will follow the order 209 // in SectionHdrLayout to read each section. 210 SmallVector<SecHdrTableEntry, 8> SectionHdrLayout; 211 212 // Save the start of SecLBRProfile so we can compute the offset to the 213 // start of SecLBRProfile for each Function's Profile and will keep it 214 // in FuncOffsetTable. 215 uint64_t SecLBRProfileStart = 0; 216 217 private: 218 void allocSecHdrTable(); 219 std::error_code writeSecHdrTable(); 220 virtual std::error_code 221 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override; 222 SecHdrTableEntry &getEntryInLayout(SecType Type); 223 std::error_code compressAndOutput(); 224 225 // We will swap the raw_ostream held by LocalBufStream and that 226 // held by OutputStream if we try to add a section which needs 227 // compression. After the swap, all the data written to output 228 // will be temporarily buffered into the underlying raw_string_ostream 229 // originally held by LocalBufStream. After the data writing for the 230 // section is completed, compress the data in the local buffer, 231 // swap the raw_ostream back and write the compressed data to the 232 // real output. 233 std::unique_ptr<raw_ostream> LocalBufStream; 234 // The location where the output stream starts. 235 uint64_t FileStart; 236 // The location in the output stream where the SecHdrTable should be 237 // written to. 238 uint64_t SecHdrTableOffset; 239 // Initial Section Flags setting. 240 std::vector<SecHdrTableEntry> SecHdrTable; 241 242 // FuncOffsetTable maps function name to its profile offset in SecLBRProfile 243 // section. It is used to load function profile on demand. 244 MapVector<StringRef, uint64_t> FuncOffsetTable; 245 // Whether to use MD5 to represent string. 246 bool UseMD5 = false; 247 248 ProfileSymbolList *ProfSymList = nullptr; 249 }; 250 251 class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase { 252 public: 253 SampleProfileWriterExtBinary(std::unique_ptr<raw_ostream> &OS) 254 : SampleProfileWriterExtBinaryBase(OS) { 255 initSectionHdrLayout(); 256 } 257 258 private: 259 virtual void initSectionHdrLayout() override { 260 // Note that SecFuncOffsetTable section is written after SecLBRProfile 261 // in the profile, but is put before SecLBRProfile in SectionHdrLayout. 262 // 263 // This is because sample reader follows the order of SectionHdrLayout to 264 // read each section, to read function profiles on demand sample reader 265 // need to get the offset of each function profile first. 266 // 267 // SecFuncOffsetTable section is written after SecLBRProfile in the 268 // profile because FuncOffsetTable needs to be populated while section 269 // SecLBRProfile is written. 270 SectionHdrLayout = {{SecProfSummary, 0, 0, 0}, 271 {SecNameTable, 0, 0, 0}, 272 {SecFuncOffsetTable, 0, 0, 0}, 273 {SecLBRProfile, 0, 0, 0}, 274 {SecProfileSymbolList, 0, 0, 0}}; 275 }; 276 virtual std::error_code 277 writeSections(const StringMap<FunctionSamples> &ProfileMap) override; 278 279 virtual std::error_code writeCustomSection(SecType Type) override { 280 return sampleprof_error::success; 281 }; 282 }; 283 284 // CompactBinary is a compact format of binary profile which both reduces 285 // the profile size and the load time needed when compiling. It has two 286 // major difference with Binary format. 287 // 1. It represents all the strings in name table using md5 hash. 288 // 2. It saves a function offset table which maps function name index to 289 // the offset of its function profile to the start of the binary profile, 290 // so by using the function offset table, for those function profiles which 291 // will not be needed when compiling a module, the profile reader does't 292 // have to read them and it saves compile time if the profile size is huge. 293 // The layout of the compact format is shown as follows: 294 // 295 // Part1: Profile header, the same as binary format, containing magic 296 // number, version, summary, name table... 297 // Part2: Function Offset Table Offset, which saves the position of 298 // Part4. 299 // Part3: Function profile collection 300 // function1 profile start 301 // .... 302 // function2 profile start 303 // .... 304 // function3 profile start 305 // .... 306 // ...... 307 // Part4: Function Offset Table 308 // function1 name index --> function1 profile start 309 // function2 name index --> function2 profile start 310 // function3 name index --> function3 profile start 311 // 312 // We need Part2 because profile reader can use it to find out and read 313 // function offset table without reading Part3 first. 314 class SampleProfileWriterCompactBinary : public SampleProfileWriterBinary { 315 using SampleProfileWriterBinary::SampleProfileWriterBinary; 316 317 public: 318 virtual std::error_code writeSample(const FunctionSamples &S) override; 319 virtual std::error_code 320 write(const StringMap<FunctionSamples> &ProfileMap) override; 321 322 protected: 323 /// The table mapping from function name to the offset of its FunctionSample 324 /// towards profile start. 325 MapVector<StringRef, uint64_t> FuncOffsetTable; 326 /// The offset of the slot to be filled with the offset of FuncOffsetTable 327 /// towards profile start. 328 uint64_t TableOffset; 329 virtual std::error_code writeNameTable() override; 330 virtual std::error_code 331 writeHeader(const StringMap<FunctionSamples> &ProfileMap) override; 332 std::error_code writeFuncOffsetTable(); 333 }; 334 335 } // end namespace sampleprof 336 } // end namespace llvm 337 338 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 339