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