1 //===- SampleProfReader.h - Read 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 reading sample profiles.
10 //
11 // NOTE: If you are making changes to this file format, please remember
12 //       to document them in the Clang documentation at
13 //       tools/clang/docs/UsersManual.rst.
14 //
15 // Text format
16 // -----------
17 //
18 // Sample profiles are written as ASCII text. The file is divided into
19 // sections, which correspond to each of the functions executed at runtime.
20 // Each section has the following format
21 //
22 //     function1:total_samples:total_head_samples
23 //      offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
24 //      offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
25 //      ...
26 //      offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
27 //      offsetA[.discriminator]: fnA:num_of_total_samples
28 //       offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ]
29 //       ...
30 //
31 // This is a nested tree in which the identation represents the nesting level
32 // of the inline stack. There are no blank lines in the file. And the spacing
33 // within a single line is fixed. Additional spaces will result in an error
34 // while reading the file.
35 //
36 // Any line starting with the '#' character is completely ignored.
37 //
38 // Inlined calls are represented with indentation. The Inline stack is a
39 // stack of source locations in which the top of the stack represents the
40 // leaf function, and the bottom of the stack represents the actual
41 // symbol to which the instruction belongs.
42 //
43 // Function names must be mangled in order for the profile loader to
44 // match them in the current translation unit. The two numbers in the
45 // function header specify how many total samples were accumulated in the
46 // function (first number), and the total number of samples accumulated
47 // in the prologue of the function (second number). This head sample
48 // count provides an indicator of how frequently the function is invoked.
49 //
50 // There are two types of lines in the function body.
51 //
52 // * Sampled line represents the profile information of a source location.
53 // * Callsite line represents the profile information of a callsite.
54 //
55 // Each sampled line may contain several items. Some are optional (marked
56 // below):
57 //
58 // a. Source line offset. This number represents the line number
59 //    in the function where the sample was collected. The line number is
60 //    always relative to the line where symbol of the function is
61 //    defined. So, if the function has its header at line 280, the offset
62 //    13 is at line 293 in the file.
63 //
64 //    Note that this offset should never be a negative number. This could
65 //    happen in cases like macros. The debug machinery will register the
66 //    line number at the point of macro expansion. So, if the macro was
67 //    expanded in a line before the start of the function, the profile
68 //    converter should emit a 0 as the offset (this means that the optimizers
69 //    will not be able to associate a meaningful weight to the instructions
70 //    in the macro).
71 //
72 // b. [OPTIONAL] Discriminator. This is used if the sampled program
73 //    was compiled with DWARF discriminator support
74 //    (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
75 //    DWARF discriminators are unsigned integer values that allow the
76 //    compiler to distinguish between multiple execution paths on the
77 //    same source line location.
78 //
79 //    For example, consider the line of code ``if (cond) foo(); else bar();``.
80 //    If the predicate ``cond`` is true 80% of the time, then the edge
81 //    into function ``foo`` should be considered to be taken most of the
82 //    time. But both calls to ``foo`` and ``bar`` are at the same source
83 //    line, so a sample count at that line is not sufficient. The
84 //    compiler needs to know which part of that line is taken more
85 //    frequently.
86 //
87 //    This is what discriminators provide. In this case, the calls to
88 //    ``foo`` and ``bar`` will be at the same line, but will have
89 //    different discriminator values. This allows the compiler to correctly
90 //    set edge weights into ``foo`` and ``bar``.
91 //
92 // c. Number of samples. This is an integer quantity representing the
93 //    number of samples collected by the profiler at this source
94 //    location.
95 //
96 // d. [OPTIONAL] Potential call targets and samples. If present, this
97 //    line contains a call instruction. This models both direct and
98 //    number of samples. For example,
99 //
100 //      130: 7  foo:3  bar:2  baz:7
101 //
102 //    The above means that at relative line offset 130 there is a call
103 //    instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
104 //    with ``baz()`` being the relatively more frequently called target.
105 //
106 // Each callsite line may contain several items. Some are optional.
107 //
108 // a. Source line offset. This number represents the line number of the
109 //    callsite that is inlined in the profiled binary.
110 //
111 // b. [OPTIONAL] Discriminator. Same as the discriminator for sampled line.
112 //
113 // c. Number of samples. This is an integer quantity representing the
114 //    total number of samples collected for the inlined instance at this
115 //    callsite
116 //
117 //
118 // Binary format
119 // -------------
120 //
121 // This is a more compact encoding. Numbers are encoded as ULEB128 values
122 // and all strings are encoded in a name table. The file is organized in
123 // the following sections:
124 //
125 // MAGIC (uint64_t)
126 //    File identifier computed by function SPMagic() (0x5350524f463432ff)
127 //
128 // VERSION (uint32_t)
129 //    File format version number computed by SPVersion()
130 //
131 // SUMMARY
132 //    TOTAL_COUNT (uint64_t)
133 //        Total number of samples in the profile.
134 //    MAX_COUNT (uint64_t)
135 //        Maximum value of samples on a line.
136 //    MAX_FUNCTION_COUNT (uint64_t)
137 //        Maximum number of samples at function entry (head samples).
138 //    NUM_COUNTS (uint64_t)
139 //        Number of lines with samples.
140 //    NUM_FUNCTIONS (uint64_t)
141 //        Number of functions with samples.
142 //    NUM_DETAILED_SUMMARY_ENTRIES (size_t)
143 //        Number of entries in detailed summary
144 //    DETAILED_SUMMARY
145 //        A list of detailed summary entry. Each entry consists of
146 //        CUTOFF (uint32_t)
147 //            Required percentile of total sample count expressed as a fraction
148 //            multiplied by 1000000.
149 //        MIN_COUNT (uint64_t)
150 //            The minimum number of samples required to reach the target
151 //            CUTOFF.
152 //        NUM_COUNTS (uint64_t)
153 //            Number of samples to get to the desrired percentile.
154 //
155 // NAME TABLE
156 //    SIZE (uint32_t)
157 //        Number of entries in the name table.
158 //    NAMES
159 //        A NUL-separated list of SIZE strings.
160 //
161 // FUNCTION BODY (one for each uninlined function body present in the profile)
162 //    HEAD_SAMPLES (uint64_t) [only for top-level functions]
163 //        Total number of samples collected at the head (prologue) of the
164 //        function.
165 //        NOTE: This field should only be present for top-level functions
166 //              (i.e., not inlined into any caller). Inlined function calls
167 //              have no prologue, so they don't need this.
168 //    NAME_IDX (uint32_t)
169 //        Index into the name table indicating the function name.
170 //    SAMPLES (uint64_t)
171 //        Total number of samples collected in this function.
172 //    NRECS (uint32_t)
173 //        Total number of sampling records this function's profile.
174 //    BODY RECORDS
175 //        A list of NRECS entries. Each entry contains:
176 //          OFFSET (uint32_t)
177 //            Line offset from the start of the function.
178 //          DISCRIMINATOR (uint32_t)
179 //            Discriminator value (see description of discriminators
180 //            in the text format documentation above).
181 //          SAMPLES (uint64_t)
182 //            Number of samples collected at this location.
183 //          NUM_CALLS (uint32_t)
184 //            Number of non-inlined function calls made at this location. In the
185 //            case of direct calls, this number will always be 1. For indirect
186 //            calls (virtual functions and function pointers) this will
187 //            represent all the actual functions called at runtime.
188 //          CALL_TARGETS
189 //            A list of NUM_CALLS entries for each called function:
190 //               NAME_IDX (uint32_t)
191 //                  Index into the name table with the callee name.
192 //               SAMPLES (uint64_t)
193 //                  Number of samples collected at the call site.
194 //    NUM_INLINED_FUNCTIONS (uint32_t)
195 //      Number of callees inlined into this function.
196 //    INLINED FUNCTION RECORDS
197 //      A list of NUM_INLINED_FUNCTIONS entries describing each of the inlined
198 //      callees.
199 //        OFFSET (uint32_t)
200 //          Line offset from the start of the function.
201 //        DISCRIMINATOR (uint32_t)
202 //          Discriminator value (see description of discriminators
203 //          in the text format documentation above).
204 //        FUNCTION BODY
205 //          A FUNCTION BODY entry describing the inlined function.
206 //===----------------------------------------------------------------------===//
207 
208 #ifndef LLVM_PROFILEDATA_SAMPLEPROFREADER_H
209 #define LLVM_PROFILEDATA_SAMPLEPROFREADER_H
210 
211 #include "llvm/ADT/SmallVector.h"
212 #include "llvm/ADT/StringMap.h"
213 #include "llvm/ADT/StringRef.h"
214 #include "llvm/ADT/Twine.h"
215 #include "llvm/IR/DiagnosticInfo.h"
216 #include "llvm/IR/Function.h"
217 #include "llvm/IR/LLVMContext.h"
218 #include "llvm/IR/ProfileSummary.h"
219 #include "llvm/ProfileData/GCOV.h"
220 #include "llvm/ProfileData/SampleProf.h"
221 #include "llvm/Support/Debug.h"
222 #include "llvm/Support/ErrorOr.h"
223 #include "llvm/Support/MemoryBuffer.h"
224 #include "llvm/Support/SymbolRemappingReader.h"
225 #include <algorithm>
226 #include <cstdint>
227 #include <memory>
228 #include <string>
229 #include <system_error>
230 #include <vector>
231 
232 namespace llvm {
233 
234 class raw_ostream;
235 
236 namespace sampleprof {
237 
238 class SampleProfileReader;
239 
240 /// SampleProfileReaderItaniumRemapper remaps the profile data from a
241 /// sample profile data reader, by applying a provided set of equivalences
242 /// between components of the symbol names in the profile.
243 class SampleProfileReaderItaniumRemapper {
244 public:
SampleProfileReaderItaniumRemapper(std::unique_ptr<MemoryBuffer> B,std::unique_ptr<SymbolRemappingReader> SRR,SampleProfileReader & R)245   SampleProfileReaderItaniumRemapper(std::unique_ptr<MemoryBuffer> B,
246                                      std::unique_ptr<SymbolRemappingReader> SRR,
247                                      SampleProfileReader &R)
248       : Buffer(std::move(B)), Remappings(std::move(SRR)), Reader(R) {
249     assert(Remappings && "Remappings cannot be nullptr");
250   }
251 
252   /// Create a remapper from the given remapping file. The remapper will
253   /// be used for profile read in by Reader.
254   static ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
255   create(const std::string Filename, SampleProfileReader &Reader,
256          LLVMContext &C);
257 
258   /// Create a remapper from the given Buffer. The remapper will
259   /// be used for profile read in by Reader.
260   static ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
261   create(std::unique_ptr<MemoryBuffer> &B, SampleProfileReader &Reader,
262          LLVMContext &C);
263 
264   /// Apply remappings to the profile read by Reader.
265   void applyRemapping(LLVMContext &Ctx);
266 
hasApplied()267   bool hasApplied() { return RemappingApplied; }
268 
269   /// Insert function name into remapper.
insert(StringRef FunctionName)270   void insert(StringRef FunctionName) { Remappings->insert(FunctionName); }
271 
272   /// Query whether there is equivalent in the remapper which has been
273   /// inserted.
exist(StringRef FunctionName)274   bool exist(StringRef FunctionName) {
275     return Remappings->lookup(FunctionName);
276   }
277 
278   /// Return the samples collected for function \p F if remapper knows
279   /// it is present in SampleMap.
280   FunctionSamples *getSamplesFor(StringRef FunctionName);
281 
282 private:
283   // The buffer holding the content read from remapping file.
284   std::unique_ptr<MemoryBuffer> Buffer;
285   std::unique_ptr<SymbolRemappingReader> Remappings;
286   DenseMap<SymbolRemappingReader::Key, FunctionSamples *> SampleMap;
287   // The Reader the remapper is servicing.
288   SampleProfileReader &Reader;
289   // Indicate whether remapping has been applied to the profile read
290   // by Reader -- by calling applyRemapping.
291   bool RemappingApplied = false;
292 };
293 
294 /// Sample-based profile reader.
295 ///
296 /// Each profile contains sample counts for all the functions
297 /// executed. Inside each function, statements are annotated with the
298 /// collected samples on all the instructions associated with that
299 /// statement.
300 ///
301 /// For this to produce meaningful data, the program needs to be
302 /// compiled with some debug information (at minimum, line numbers:
303 /// -gline-tables-only). Otherwise, it will be impossible to match IR
304 /// instructions to the line numbers collected by the profiler.
305 ///
306 /// From the profile file, we are interested in collecting the
307 /// following information:
308 ///
309 /// * A list of functions included in the profile (mangled names).
310 ///
311 /// * For each function F:
312 ///   1. The total number of samples collected in F.
313 ///
314 ///   2. The samples collected at each line in F. To provide some
315 ///      protection against source code shuffling, line numbers should
316 ///      be relative to the start of the function.
317 ///
318 /// The reader supports two file formats: text and binary. The text format
319 /// is useful for debugging and testing, while the binary format is more
320 /// compact and I/O efficient. They can both be used interchangeably.
321 class SampleProfileReader {
322 public:
323   SampleProfileReader(std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
324                       SampleProfileFormat Format = SPF_None)
325       : Profiles(0), Ctx(C), Buffer(std::move(B)), Format(Format) {}
326 
327   virtual ~SampleProfileReader() = default;
328 
329   /// Read and validate the file header.
330   virtual std::error_code readHeader() = 0;
331 
332   /// The interface to read sample profiles from the associated file.
read()333   std::error_code read() {
334     if (std::error_code EC = readImpl())
335       return EC;
336     if (Remapper)
337       Remapper->applyRemapping(Ctx);
338     FunctionSamples::UseMD5 = useMD5();
339     return sampleprof_error::success;
340   }
341 
342   /// The implementaion to read sample profiles from the associated file.
343   virtual std::error_code readImpl() = 0;
344 
345   /// Print the profile for \p FName on stream \p OS.
346   void dumpFunctionProfile(StringRef FName, raw_ostream &OS = dbgs());
347 
collectFuncsFrom(const Module & M)348   virtual void collectFuncsFrom(const Module &M) {}
349 
350   /// Print all the profiles on stream \p OS.
351   void dump(raw_ostream &OS = dbgs());
352 
353   /// Return the samples collected for function \p F.
getSamplesFor(const Function & F)354   FunctionSamples *getSamplesFor(const Function &F) {
355     // The function name may have been updated by adding suffix. Call
356     // a helper to (optionally) strip off suffixes so that we can
357     // match against the original function name in the profile.
358     StringRef CanonName = FunctionSamples::getCanonicalFnName(F);
359     return getSamplesFor(CanonName);
360   }
361 
362   /// Return the samples collected for function \p F, create empty
363   /// FunctionSamples if it doesn't exist.
getOrCreateSamplesFor(const Function & F)364   FunctionSamples *getOrCreateSamplesFor(const Function &F) {
365     std::string FGUID;
366     StringRef CanonName = FunctionSamples::getCanonicalFnName(F);
367     CanonName = getRepInFormat(CanonName, useMD5(), FGUID);
368     return &Profiles[CanonName];
369   }
370 
371   /// Return the samples collected for function \p F.
getSamplesFor(StringRef Fname)372   virtual FunctionSamples *getSamplesFor(StringRef Fname) {
373     if (Remapper) {
374       if (auto FS = Remapper->getSamplesFor(Fname))
375         return FS;
376     }
377     std::string FGUID;
378     Fname = getRepInFormat(Fname, useMD5(), FGUID);
379     auto It = Profiles.find(Fname);
380     if (It != Profiles.end())
381       return &It->second;
382     return nullptr;
383   }
384 
385   /// Return all the profiles.
getProfiles()386   StringMap<FunctionSamples> &getProfiles() { return Profiles; }
387 
388   /// Report a parse error message.
reportError(int64_t LineNumber,Twine Msg)389   void reportError(int64_t LineNumber, Twine Msg) const {
390     Ctx.diagnose(DiagnosticInfoSampleProfile(Buffer->getBufferIdentifier(),
391                                              LineNumber, Msg));
392   }
393 
394   /// Create a sample profile reader appropriate to the file format.
395   /// Create a remapper underlying if RemapFilename is not empty.
396   static ErrorOr<std::unique_ptr<SampleProfileReader>>
397   create(const std::string Filename, LLVMContext &C,
398          const std::string RemapFilename = "");
399 
400   /// Create a sample profile reader from the supplied memory buffer.
401   /// Create a remapper underlying if RemapFilename is not empty.
402   static ErrorOr<std::unique_ptr<SampleProfileReader>>
403   create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
404          const std::string RemapFilename = "");
405 
406   /// Return the profile summary.
getSummary()407   ProfileSummary &getSummary() const { return *(Summary.get()); }
408 
getBuffer()409   MemoryBuffer *getBuffer() const { return Buffer.get(); }
410 
411   /// \brief Return the profile format.
getFormat()412   SampleProfileFormat getFormat() const { return Format; }
413 
getProfileSymbolList()414   virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList() {
415     return nullptr;
416   };
417 
418   /// It includes all the names that have samples either in outline instance
419   /// or inline instance.
getNameTable()420   virtual std::vector<StringRef> *getNameTable() { return nullptr; }
421   virtual bool dumpSectionInfo(raw_ostream &OS = dbgs()) { return false; };
422 
423   /// Return whether names in the profile are all MD5 numbers.
useMD5()424   virtual bool useMD5() { return false; }
425 
426 protected:
427   /// Map every function to its associated profile.
428   ///
429   /// The profile of every function executed at runtime is collected
430   /// in the structure FunctionSamples. This maps function objects
431   /// to their corresponding profiles.
432   StringMap<FunctionSamples> Profiles;
433 
434   /// LLVM context used to emit diagnostics.
435   LLVMContext &Ctx;
436 
437   /// Memory buffer holding the profile file.
438   std::unique_ptr<MemoryBuffer> Buffer;
439 
440   /// Profile summary information.
441   std::unique_ptr<ProfileSummary> Summary;
442 
443   /// Take ownership of the summary of this reader.
444   static std::unique_ptr<ProfileSummary>
takeSummary(SampleProfileReader & Reader)445   takeSummary(SampleProfileReader &Reader) {
446     return std::move(Reader.Summary);
447   }
448 
449   /// Compute summary for this profile.
450   void computeSummary();
451 
452   std::unique_ptr<SampleProfileReaderItaniumRemapper> Remapper;
453 
454   /// \brief The format of sample.
455   SampleProfileFormat Format = SPF_None;
456 };
457 
458 class SampleProfileReaderText : public SampleProfileReader {
459 public:
SampleProfileReaderText(std::unique_ptr<MemoryBuffer> B,LLVMContext & C)460   SampleProfileReaderText(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
461       : SampleProfileReader(std::move(B), C, SPF_Text) {}
462 
463   /// Read and validate the file header.
readHeader()464   std::error_code readHeader() override { return sampleprof_error::success; }
465 
466   /// Read sample profiles from the associated file.
467   std::error_code readImpl() override;
468 
469   /// Return true if \p Buffer is in the format supported by this class.
470   static bool hasFormat(const MemoryBuffer &Buffer);
471 };
472 
473 class SampleProfileReaderBinary : public SampleProfileReader {
474 public:
475   SampleProfileReaderBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
476                             SampleProfileFormat Format = SPF_None)
SampleProfileReader(std::move (B),C,Format)477       : SampleProfileReader(std::move(B), C, Format) {}
478 
479   /// Read and validate the file header.
480   virtual std::error_code readHeader() override;
481 
482   /// Read sample profiles from the associated file.
483   std::error_code readImpl() override;
484 
485   /// It includes all the names that have samples either in outline instance
486   /// or inline instance.
getNameTable()487   virtual std::vector<StringRef> *getNameTable() override { return &NameTable; }
488 
489 protected:
490   /// Read a numeric value of type T from the profile.
491   ///
492   /// If an error occurs during decoding, a diagnostic message is emitted and
493   /// EC is set.
494   ///
495   /// \returns the read value.
496   template <typename T> ErrorOr<T> readNumber();
497 
498   /// Read a numeric value of type T from the profile. The value is saved
499   /// without encoded.
500   template <typename T> ErrorOr<T> readUnencodedNumber();
501 
502   /// Read a string from the profile.
503   ///
504   /// If an error occurs during decoding, a diagnostic message is emitted and
505   /// EC is set.
506   ///
507   /// \returns the read value.
508   ErrorOr<StringRef> readString();
509 
510   /// Read the string index and check whether it overflows the table.
511   template <typename T> inline ErrorOr<uint32_t> readStringIndex(T &Table);
512 
513   /// Return true if we've reached the end of file.
at_eof()514   bool at_eof() const { return Data >= End; }
515 
516   /// Read the next function profile instance.
517   std::error_code readFuncProfile(const uint8_t *Start);
518 
519   /// Read the contents of the given profile instance.
520   std::error_code readProfile(FunctionSamples &FProfile);
521 
522   /// Read the contents of Magic number and Version number.
523   std::error_code readMagicIdent();
524 
525   /// Read profile summary.
526   std::error_code readSummary();
527 
528   /// Read the whole name table.
529   virtual std::error_code readNameTable();
530 
531   /// Points to the current location in the buffer.
532   const uint8_t *Data = nullptr;
533 
534   /// Points to the end of the buffer.
535   const uint8_t *End = nullptr;
536 
537   /// Function name table.
538   std::vector<StringRef> NameTable;
539 
540   /// Read a string indirectly via the name table.
541   virtual ErrorOr<StringRef> readStringFromTable();
542 
543 private:
544   std::error_code readSummaryEntry(std::vector<ProfileSummaryEntry> &Entries);
545   virtual std::error_code verifySPMagic(uint64_t Magic) = 0;
546 };
547 
548 class SampleProfileReaderRawBinary : public SampleProfileReaderBinary {
549 private:
550   virtual std::error_code verifySPMagic(uint64_t Magic) override;
551 
552 public:
553   SampleProfileReaderRawBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
554                                SampleProfileFormat Format = SPF_Binary)
SampleProfileReaderBinary(std::move (B),C,Format)555       : SampleProfileReaderBinary(std::move(B), C, Format) {}
556 
557   /// \brief Return true if \p Buffer is in the format supported by this class.
558   static bool hasFormat(const MemoryBuffer &Buffer);
559 };
560 
561 /// SampleProfileReaderExtBinaryBase/SampleProfileWriterExtBinaryBase defines
562 /// the basic structure of the extensible binary format.
563 /// The format is organized in sections except the magic and version number
564 /// at the beginning. There is a section table before all the sections, and
565 /// each entry in the table describes the entry type, start, size and
566 /// attributes. The format in each section is defined by the section itself.
567 ///
568 /// It is easy to add a new section while maintaining the backward
569 /// compatibility of the profile. Nothing extra needs to be done. If we want
570 /// to extend an existing section, like add cache misses information in
571 /// addition to the sample count in the profile body, we can add a new section
572 /// with the extension and retire the existing section, and we could choose
573 /// to keep the parser of the old section if we want the reader to be able
574 /// to read both new and old format profile.
575 ///
576 /// SampleProfileReaderExtBinary/SampleProfileWriterExtBinary define the
577 /// commonly used sections of a profile in extensible binary format. It is
578 /// possible to define other types of profile inherited from
579 /// SampleProfileReaderExtBinaryBase/SampleProfileWriterExtBinaryBase.
580 class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
581 private:
582   std::error_code decompressSection(const uint8_t *SecStart,
583                                     const uint64_t SecSize,
584                                     const uint8_t *&DecompressBuf,
585                                     uint64_t &DecompressBufSize);
586 
587   BumpPtrAllocator Allocator;
588 
589 protected:
590   std::vector<SecHdrTableEntry> SecHdrTable;
591   std::unique_ptr<ProfileSymbolList> ProfSymList;
592   std::error_code readSecHdrTableEntry();
593   std::error_code readSecHdrTable();
594   virtual std::error_code readHeader() override;
595   virtual std::error_code verifySPMagic(uint64_t Magic) override = 0;
596   virtual std::error_code readOneSection(const uint8_t *Start, uint64_t Size,
597                                          const SecHdrTableEntry &Entry) = 0;
598 
599 public:
SampleProfileReaderExtBinaryBase(std::unique_ptr<MemoryBuffer> B,LLVMContext & C,SampleProfileFormat Format)600   SampleProfileReaderExtBinaryBase(std::unique_ptr<MemoryBuffer> B,
601                                    LLVMContext &C, SampleProfileFormat Format)
602       : SampleProfileReaderBinary(std::move(B), C, Format) {}
603 
604   /// Read sample profiles in extensible format from the associated file.
605   std::error_code readImpl() override;
606 
607   /// Get the total size of all \p Type sections.
608   uint64_t getSectionSize(SecType Type);
609   /// Get the total size of header and all sections.
610   uint64_t getFileSize();
611   virtual bool dumpSectionInfo(raw_ostream &OS = dbgs()) override;
612 };
613 
614 class SampleProfileReaderExtBinary : public SampleProfileReaderExtBinaryBase {
615 private:
616   virtual std::error_code verifySPMagic(uint64_t Magic) override;
617   virtual std::error_code
618   readOneSection(const uint8_t *Start, uint64_t Size,
619                  const SecHdrTableEntry &Entry) override;
620   std::error_code readProfileSymbolList();
621   std::error_code readFuncOffsetTable();
622   std::error_code readFuncProfiles();
623   std::error_code readMD5NameTable();
624   std::error_code readNameTableSec(bool IsMD5);
625 
626   /// The table mapping from function name to the offset of its FunctionSample
627   /// towards file start.
628   DenseMap<StringRef, uint64_t> FuncOffsetTable;
629   /// The set containing the functions to use when compiling a module.
630   DenseSet<StringRef> FuncsToUse;
631   /// Use all functions from the input profile.
632   bool UseAllFuncs = true;
633 
634   /// If MD5 is used in NameTable section, the section saves uint64_t data.
635   /// The uint64_t data has to be converted to a string and then the string
636   /// will be used to initialize StringRef in NameTable.
637   /// Note NameTable contains StringRef so it needs another buffer to own
638   /// the string data. MD5StringBuf serves as the string buffer that is
639   /// referenced by NameTable (vector of StringRef). We make sure
640   /// the lifetime of MD5StringBuf is not shorter than that of NameTable.
641   std::unique_ptr<std::vector<std::string>> MD5StringBuf;
642 
643 public:
644   SampleProfileReaderExtBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
645                                SampleProfileFormat Format = SPF_Ext_Binary)
SampleProfileReaderExtBinaryBase(std::move (B),C,Format)646       : SampleProfileReaderExtBinaryBase(std::move(B), C, Format) {}
647 
648   /// \brief Return true if \p Buffer is in the format supported by this class.
649   static bool hasFormat(const MemoryBuffer &Buffer);
650 
getProfileSymbolList()651   virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList() override {
652     return std::move(ProfSymList);
653   };
654 
655   /// Collect functions with definitions in Module \p M.
656   void collectFuncsFrom(const Module &M) override;
657 
658   /// Return whether names in the profile are all MD5 numbers.
useMD5()659   virtual bool useMD5() override {
660     assert(!NameTable.empty() && "NameTable should have been initialized");
661     return MD5StringBuf && !MD5StringBuf->empty();
662   }
663 };
664 
665 class SampleProfileReaderCompactBinary : public SampleProfileReaderBinary {
666 private:
667   /// Function name table.
668   std::vector<std::string> NameTable;
669   /// The table mapping from function name to the offset of its FunctionSample
670   /// towards file start.
671   DenseMap<StringRef, uint64_t> FuncOffsetTable;
672   /// The set containing the functions to use when compiling a module.
673   DenseSet<StringRef> FuncsToUse;
674   /// Use all functions from the input profile.
675   bool UseAllFuncs = true;
676   virtual std::error_code verifySPMagic(uint64_t Magic) override;
677   virtual std::error_code readNameTable() override;
678   /// Read a string indirectly via the name table.
679   virtual ErrorOr<StringRef> readStringFromTable() override;
680   virtual std::error_code readHeader() override;
681   std::error_code readFuncOffsetTable();
682 
683 public:
SampleProfileReaderCompactBinary(std::unique_ptr<MemoryBuffer> B,LLVMContext & C)684   SampleProfileReaderCompactBinary(std::unique_ptr<MemoryBuffer> B,
685                                    LLVMContext &C)
686       : SampleProfileReaderBinary(std::move(B), C, SPF_Compact_Binary) {}
687 
688   /// \brief Return true if \p Buffer is in the format supported by this class.
689   static bool hasFormat(const MemoryBuffer &Buffer);
690 
691   /// Read samples only for functions to use.
692   std::error_code readImpl() override;
693 
694   /// Collect functions to be used when compiling Module \p M.
695   void collectFuncsFrom(const Module &M) override;
696 
697   /// Return whether names in the profile are all MD5 numbers.
useMD5()698   virtual bool useMD5() override { return true; }
699 };
700 
701 using InlineCallStack = SmallVector<FunctionSamples *, 10>;
702 
703 // Supported histogram types in GCC.  Currently, we only need support for
704 // call target histograms.
705 enum HistType {
706   HIST_TYPE_INTERVAL,
707   HIST_TYPE_POW2,
708   HIST_TYPE_SINGLE_VALUE,
709   HIST_TYPE_CONST_DELTA,
710   HIST_TYPE_INDIR_CALL,
711   HIST_TYPE_AVERAGE,
712   HIST_TYPE_IOR,
713   HIST_TYPE_INDIR_CALL_TOPN
714 };
715 
716 class SampleProfileReaderGCC : public SampleProfileReader {
717 public:
SampleProfileReaderGCC(std::unique_ptr<MemoryBuffer> B,LLVMContext & C)718   SampleProfileReaderGCC(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
719       : SampleProfileReader(std::move(B), C, SPF_GCC),
720         GcovBuffer(Buffer.get()) {}
721 
722   /// Read and validate the file header.
723   std::error_code readHeader() override;
724 
725   /// Read sample profiles from the associated file.
726   std::error_code readImpl() override;
727 
728   /// Return true if \p Buffer is in the format supported by this class.
729   static bool hasFormat(const MemoryBuffer &Buffer);
730 
731 protected:
732   std::error_code readNameTable();
733   std::error_code readOneFunctionProfile(const InlineCallStack &InlineStack,
734                                          bool Update, uint32_t Offset);
735   std::error_code readFunctionProfiles();
736   std::error_code skipNextWord();
737   template <typename T> ErrorOr<T> readNumber();
738   ErrorOr<StringRef> readString();
739 
740   /// Read the section tag and check that it's the same as \p Expected.
741   std::error_code readSectionTag(uint32_t Expected);
742 
743   /// GCOV buffer containing the profile.
744   GCOVBuffer GcovBuffer;
745 
746   /// Function names in this profile.
747   std::vector<std::string> Names;
748 
749   /// GCOV tags used to separate sections in the profile file.
750   static const uint32_t GCOVTagAFDOFileNames = 0xaa000000;
751   static const uint32_t GCOVTagAFDOFunction = 0xac000000;
752 };
753 
754 } // end namespace sampleprof
755 
756 } // end namespace llvm
757 
758 #endif // LLVM_PROFILEDATA_SAMPLEPROFREADER_H
759