1 //===- InstrProf.h - Instrumented profiling format support ------*- 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 // Instrumentation-based profiling data is generated by instrumented
10 // binaries through library functions in compiler-rt, and read by the clang
11 // frontend to feed PGO.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_PROFILEDATA_INSTRPROF_H
16 #define LLVM_PROFILEDATA_INSTRPROF_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitmaskEnum.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSet.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/IR/ProfileSummary.h"
25 #include "llvm/ProfileData/InstrProfData.inc"
26 #include "llvm/Support/BalancedPartitioning.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/Error.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MD5.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/TargetParser/Host.h"
35 #include "llvm/TargetParser/Triple.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <cstddef>
39 #include <cstdint>
40 #include <cstring>
41 #include <list>
42 #include <memory>
43 #include <string>
44 #include <system_error>
45 #include <utility>
46 #include <vector>
47
48 namespace llvm {
49
50 class Function;
51 class GlobalVariable;
52 struct InstrProfRecord;
53 class InstrProfSymtab;
54 class Instruction;
55 class MDNode;
56 class Module;
57
58 enum InstrProfSectKind {
59 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Kind,
60 #include "llvm/ProfileData/InstrProfData.inc"
61 };
62
63 /// Return the max count value. We reserver a few large values for special use.
getInstrMaxCountValue()64 inline uint64_t getInstrMaxCountValue() {
65 return std::numeric_limits<uint64_t>::max() - 2;
66 }
67
68 /// Return the name of the profile section corresponding to \p IPSK.
69 ///
70 /// The name of the section depends on the object format type \p OF. If
71 /// \p AddSegmentInfo is true, a segment prefix and additional linker hints may
72 /// be added to the section name (this is the default).
73 std::string getInstrProfSectionName(InstrProfSectKind IPSK,
74 Triple::ObjectFormatType OF,
75 bool AddSegmentInfo = true);
76
77 /// Return the name profile runtime entry point to do value profiling
78 /// for a given site.
getInstrProfValueProfFuncName()79 inline StringRef getInstrProfValueProfFuncName() {
80 return INSTR_PROF_VALUE_PROF_FUNC_STR;
81 }
82
83 /// Return the name profile runtime entry point to do memop size value
84 /// profiling.
getInstrProfValueProfMemOpFuncName()85 inline StringRef getInstrProfValueProfMemOpFuncName() {
86 return INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR;
87 }
88
89 /// Return the name prefix of variables containing instrumented function names.
getInstrProfNameVarPrefix()90 inline StringRef getInstrProfNameVarPrefix() { return "__profn_"; }
91
92 /// Return the name prefix of variables containing per-function control data.
getInstrProfDataVarPrefix()93 inline StringRef getInstrProfDataVarPrefix() { return "__profd_"; }
94
95 /// Return the name prefix of profile counter variables.
getInstrProfCountersVarPrefix()96 inline StringRef getInstrProfCountersVarPrefix() { return "__profc_"; }
97
98 /// Return the name prefix of profile bitmap variables.
getInstrProfBitmapVarPrefix()99 inline StringRef getInstrProfBitmapVarPrefix() { return "__profbm_"; }
100
101 /// Return the name prefix of value profile variables.
getInstrProfValuesVarPrefix()102 inline StringRef getInstrProfValuesVarPrefix() { return "__profvp_"; }
103
104 /// Return the name of value profile node array variables:
getInstrProfVNodesVarName()105 inline StringRef getInstrProfVNodesVarName() { return "__llvm_prf_vnodes"; }
106
107 /// Return the name of the variable holding the strings (possibly compressed)
108 /// of all function's PGO names.
getInstrProfNamesVarName()109 inline StringRef getInstrProfNamesVarName() {
110 return "__llvm_prf_nm";
111 }
112
113 /// Return the name of a covarage mapping variable (internal linkage)
114 /// for each instrumented source module. Such variables are allocated
115 /// in the __llvm_covmap section.
getCoverageMappingVarName()116 inline StringRef getCoverageMappingVarName() {
117 return "__llvm_coverage_mapping";
118 }
119
120 /// Return the name of the internal variable recording the array
121 /// of PGO name vars referenced by the coverage mapping. The owning
122 /// functions of those names are not emitted by FE (e.g, unused inline
123 /// functions.)
getCoverageUnusedNamesVarName()124 inline StringRef getCoverageUnusedNamesVarName() {
125 return "__llvm_coverage_names";
126 }
127
128 /// Return the name of function that registers all the per-function control
129 /// data at program startup time by calling __llvm_register_function. This
130 /// function has internal linkage and is called by __llvm_profile_init
131 /// runtime method. This function is not generated for these platforms:
132 /// Darwin, Linux, and FreeBSD.
getInstrProfRegFuncsName()133 inline StringRef getInstrProfRegFuncsName() {
134 return "__llvm_profile_register_functions";
135 }
136
137 /// Return the name of the runtime interface that registers per-function control
138 /// data for one instrumented function.
getInstrProfRegFuncName()139 inline StringRef getInstrProfRegFuncName() {
140 return "__llvm_profile_register_function";
141 }
142
143 /// Return the name of the runtime interface that registers the PGO name strings.
getInstrProfNamesRegFuncName()144 inline StringRef getInstrProfNamesRegFuncName() {
145 return "__llvm_profile_register_names_function";
146 }
147
148 /// Return the name of the runtime initialization method that is generated by
149 /// the compiler. The function calls __llvm_profile_register_functions and
150 /// __llvm_profile_override_default_filename functions if needed. This function
151 /// has internal linkage and invoked at startup time via init_array.
getInstrProfInitFuncName()152 inline StringRef getInstrProfInitFuncName() { return "__llvm_profile_init"; }
153
154 /// Return the name of the hook variable defined in profile runtime library.
155 /// A reference to the variable causes the linker to link in the runtime
156 /// initialization module (which defines the hook variable).
getInstrProfRuntimeHookVarName()157 inline StringRef getInstrProfRuntimeHookVarName() {
158 return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_RUNTIME_VAR);
159 }
160
161 /// Return the name of the compiler generated function that references the
162 /// runtime hook variable. The function is a weak global.
getInstrProfRuntimeHookVarUseFuncName()163 inline StringRef getInstrProfRuntimeHookVarUseFuncName() {
164 return "__llvm_profile_runtime_user";
165 }
166
getInstrProfCounterBiasVarName()167 inline StringRef getInstrProfCounterBiasVarName() {
168 return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR);
169 }
170
171 /// Return the marker used to separate PGO names during serialization.
getInstrProfNameSeparator()172 inline StringRef getInstrProfNameSeparator() { return "\01"; }
173
174 /// Please use getIRPGOFuncName for LLVM IR instrumentation. This function is
175 /// for front-end (Clang, etc) instrumentation.
176 /// Return the modified name for function \c F suitable to be
177 /// used the key for profile lookup. Variable \c InLTO indicates if this
178 /// is called in LTO optimization passes.
179 std::string getPGOFuncName(const Function &F, bool InLTO = false,
180 uint64_t Version = INSTR_PROF_INDEX_VERSION);
181
182 /// Return the modified name for a function suitable to be
183 /// used the key for profile lookup. The function's original
184 /// name is \c RawFuncName and has linkage of type \c Linkage.
185 /// The function is defined in module \c FileName.
186 std::string getPGOFuncName(StringRef RawFuncName,
187 GlobalValue::LinkageTypes Linkage,
188 StringRef FileName,
189 uint64_t Version = INSTR_PROF_INDEX_VERSION);
190
191 /// \return the modified name for function \c F suitable to be
192 /// used as the key for IRPGO profile lookup. \c InLTO indicates if this is
193 /// called from LTO optimization passes.
194 std::string getIRPGOFuncName(const Function &F, bool InLTO = false);
195
196 /// \return the filename and the function name parsed from the output of
197 /// \c getIRPGOFuncName()
198 std::pair<StringRef, StringRef> getParsedIRPGOName(StringRef IRPGOName);
199
200 /// Return the name of the global variable used to store a function
201 /// name in PGO instrumentation. \c FuncName is the IRPGO function name
202 /// (returned by \c getIRPGOFuncName) for LLVM IR instrumentation and PGO
203 /// function name (returned by \c getPGOFuncName) for front-end instrumentation.
204 std::string getPGOFuncNameVarName(StringRef FuncName,
205 GlobalValue::LinkageTypes Linkage);
206
207 /// Create and return the global variable for function name used in PGO
208 /// instrumentation. \c FuncName is the IRPGO function name (returned by
209 /// \c getIRPGOFuncName) for LLVM IR instrumentation and PGO function name
210 /// (returned by \c getPGOFuncName) for front-end instrumentation.
211 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName);
212
213 /// Create and return the global variable for function name used in PGO
214 /// instrumentation. \c FuncName is the IRPGO function name (returned by
215 /// \c getIRPGOFuncName) for LLVM IR instrumentation and PGO function name
216 /// (returned by \c getPGOFuncName) for front-end instrumentation.
217 GlobalVariable *createPGOFuncNameVar(Module &M,
218 GlobalValue::LinkageTypes Linkage,
219 StringRef PGOFuncName);
220
221 /// Return the initializer in string of the PGO name var \c NameVar.
222 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar);
223
224 /// Given a PGO function name, remove the filename prefix and return
225 /// the original (static) function name.
226 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
227 StringRef FileName = "<unknown>");
228
229 /// Given a vector of strings (names of global objects like functions or,
230 /// virtual tables) \c NameStrs, the method generates a combined string \c
231 /// Result that is ready to be serialized. The \c Result string is comprised of
232 /// three fields: The first field is the length of the uncompressed strings, and
233 /// the the second field is the length of the zlib-compressed string. Both
234 /// fields are encoded in ULEB128. If \c doCompress is false, the
235 /// third field is the uncompressed strings; otherwise it is the
236 /// compressed string. When the string compression is off, the
237 /// second field will have value zero.
238 Error collectGlobalObjectNameStrings(ArrayRef<std::string> NameStrs,
239 bool doCompression, std::string &Result);
240
241 /// Produce \c Result string with the same format described above. The input
242 /// is vector of PGO function name variables that are referenced.
243 /// The global variable element in 'NameVars' is a string containing the pgo
244 /// name of a function. See `createPGOFuncNameVar` that creates these global
245 /// variables.
246 Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
247 std::string &Result, bool doCompression = true);
248
249 /// Check if INSTR_PROF_RAW_VERSION_VAR is defined. This global is only being
250 /// set in IR PGO compilation.
251 bool isIRPGOFlagSet(const Module *M);
252
253 /// Check if we can safely rename this Comdat function. Instances of the same
254 /// comdat function may have different control flows thus can not share the
255 /// same counter variable.
256 bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken = false);
257
258 enum InstrProfValueKind : uint32_t {
259 #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
260 #include "llvm/ProfileData/InstrProfData.inc"
261 };
262
263 /// Get the value profile data for value site \p SiteIdx from \p InstrProfR
264 /// and annotate the instruction \p Inst with the value profile meta data.
265 /// Annotate up to \p MaxMDCount (default 3) number of records per value site.
266 void annotateValueSite(Module &M, Instruction &Inst,
267 const InstrProfRecord &InstrProfR,
268 InstrProfValueKind ValueKind, uint32_t SiteIndx,
269 uint32_t MaxMDCount = 3);
270
271 /// Same as the above interface but using an ArrayRef, as well as \p Sum.
272 void annotateValueSite(Module &M, Instruction &Inst,
273 ArrayRef<InstrProfValueData> VDs, uint64_t Sum,
274 InstrProfValueKind ValueKind, uint32_t MaxMDCount);
275
276 /// Extract the value profile data from \p Inst which is annotated with
277 /// value profile meta data. Return false if there is no value data annotated,
278 /// otherwise return true.
279 bool getValueProfDataFromInst(const Instruction &Inst,
280 InstrProfValueKind ValueKind,
281 uint32_t MaxNumValueData,
282 InstrProfValueData ValueData[],
283 uint32_t &ActualNumValueData, uint64_t &TotalC,
284 bool GetNoICPValue = false);
285
getPGOFuncNameMetadataName()286 inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }
287
288 /// Return the PGOFuncName meta data associated with a function.
289 MDNode *getPGOFuncNameMetadata(const Function &F);
290
291 /// Create the PGOFuncName meta data if PGOFuncName is different from
292 /// function's raw name. This should only apply to internal linkage functions
293 /// declared by users only.
294 void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName);
295
296 /// Check if we can use Comdat for profile variables. This will eliminate
297 /// the duplicated profile variables for Comdat functions.
298 bool needsComdatForCounter(const Function &F, const Module &M);
299
300 /// An enum describing the attributes of an instrumented profile.
301 enum class InstrProfKind {
302 Unknown = 0x0,
303 // A frontend clang profile, incompatible with other attrs.
304 FrontendInstrumentation = 0x1,
305 // An IR-level profile (default when -fprofile-generate is used).
306 IRInstrumentation = 0x2,
307 // A profile with entry basic block instrumentation.
308 FunctionEntryInstrumentation = 0x4,
309 // A context sensitive IR-level profile.
310 ContextSensitive = 0x8,
311 // Use single byte probes for coverage.
312 SingleByteCoverage = 0x10,
313 // Only instrument the function entry basic block.
314 FunctionEntryOnly = 0x20,
315 // A memory profile collected using -fprofile=memory.
316 MemProf = 0x40,
317 // A temporal profile.
318 TemporalProfile = 0x80,
319 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/TemporalProfile)
320 };
321
322 const std::error_category &instrprof_category();
323
324 enum class instrprof_error {
325 success = 0,
326 eof,
327 unrecognized_format,
328 bad_magic,
329 bad_header,
330 unsupported_version,
331 unsupported_hash_type,
332 too_large,
333 truncated,
334 malformed,
335 missing_correlation_info,
336 unexpected_correlation_info,
337 unable_to_correlate_profile,
338 unknown_function,
339 invalid_prof,
340 hash_mismatch,
341 count_mismatch,
342 bitmap_mismatch,
343 counter_overflow,
344 value_site_count_mismatch,
345 compress_failed,
346 uncompress_failed,
347 empty_raw_profile,
348 zlib_unavailable,
349 raw_profile_version_mismatch,
350 counter_value_too_large,
351 };
352
353 /// An ordered list of functions identified by their NameRef found in
354 /// INSTR_PROF_DATA
355 struct TemporalProfTraceTy {
356 std::vector<uint64_t> FunctionNameRefs;
357 uint64_t Weight;
358 TemporalProfTraceTy(std::initializer_list<uint64_t> Trace = {},
359 uint64_t Weight = 1)
FunctionNameRefsTemporalProfTraceTy360 : FunctionNameRefs(Trace), Weight(Weight) {}
361
362 /// Use a set of temporal profile traces to create a list of balanced
363 /// partitioning function nodes used by BalancedPartitioning to generate a
364 /// function order that reduces page faults during startup
365 static std::vector<BPFunctionNode>
366 createBPFunctionNodes(ArrayRef<TemporalProfTraceTy> Traces);
367 };
368
make_error_code(instrprof_error E)369 inline std::error_code make_error_code(instrprof_error E) {
370 return std::error_code(static_cast<int>(E), instrprof_category());
371 }
372
373 class InstrProfError : public ErrorInfo<InstrProfError> {
374 public:
375 InstrProfError(instrprof_error Err, const Twine &ErrStr = Twine())
Err(Err)376 : Err(Err), Msg(ErrStr.str()) {
377 assert(Err != instrprof_error::success && "Not an error");
378 }
379
380 std::string message() const override;
381
log(raw_ostream & OS)382 void log(raw_ostream &OS) const override { OS << message(); }
383
convertToErrorCode()384 std::error_code convertToErrorCode() const override {
385 return make_error_code(Err);
386 }
387
get()388 instrprof_error get() const { return Err; }
getMessage()389 const std::string &getMessage() const { return Msg; }
390
391 /// Consume an Error and return the raw enum value contained within it, and
392 /// the optional error message. The Error must either be a success value, or
393 /// contain a single InstrProfError.
take(Error E)394 static std::pair<instrprof_error, std::string> take(Error E) {
395 auto Err = instrprof_error::success;
396 std::string Msg = "";
397 handleAllErrors(std::move(E), [&Err, &Msg](const InstrProfError &IPE) {
398 assert(Err == instrprof_error::success && "Multiple errors encountered");
399 Err = IPE.get();
400 Msg = IPE.getMessage();
401 });
402 return {Err, Msg};
403 }
404
405 static char ID;
406
407 private:
408 instrprof_error Err;
409 std::string Msg;
410 };
411
412 namespace object {
413
414 class SectionRef;
415
416 } // end namespace object
417
418 namespace IndexedInstrProf {
419
420 uint64_t ComputeHash(StringRef K);
421
422 } // end namespace IndexedInstrProf
423
424 /// A symbol table used for function [IR]PGO name look-up with keys
425 /// (such as pointers, md5hash values) to the function. A function's
426 /// [IR]PGO name or name's md5hash are used in retrieving the profile
427 /// data of the function. See \c getIRPGOFuncName() and \c getPGOFuncName
428 /// methods for details how [IR]PGO name is formed.
429 class InstrProfSymtab {
430 public:
431 using AddrHashMap = std::vector<std::pair<uint64_t, uint64_t>>;
432
433 private:
434 StringRef Data;
435 uint64_t Address = 0;
436 // Unique name strings.
437 StringSet<> NameTab;
438 // A map from MD5 keys to function name strings.
439 std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
440 // A map from MD5 keys to function define. We only populate this map
441 // when build the Symtab from a Module.
442 std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;
443 // A map from function runtime address to function name MD5 hash.
444 // This map is only populated and used by raw instr profile reader.
445 AddrHashMap AddrToMD5Map;
446 bool Sorted = false;
447
getExternalSymbol()448 static StringRef getExternalSymbol() {
449 return "** External Symbol **";
450 }
451
452 // Returns the canonial name of the given PGOName. In a canonical name, all
453 // suffixes that begins with "." except ".__uniq." are stripped.
454 // FIXME: Unify this with `FunctionSamples::getCanonicalFnName`.
455 static StringRef getCanonicalName(StringRef PGOName);
456
457 // Add the function into the symbol table, by creating the following
458 // map entries:
459 // name-set = {PGOFuncName} + {getCanonicalName(PGOFuncName)} if the canonical
460 // name is different from pgo name
461 // - In MD5NameMap: <MD5Hash(name), name> for name in name-set
462 // - In MD5FuncMap: <MD5Hash(name), &F> for name in name-set
463 Error addFuncWithName(Function &F, StringRef PGOFuncName);
464
465 // If the symtab is created by a series of calls to \c addFuncName, \c
466 // finalizeSymtab needs to be called before looking up function names.
467 // This is required because the underlying map is a vector (for space
468 // efficiency) which needs to be sorted.
469 inline void finalizeSymtab();
470
471 public:
472 InstrProfSymtab() = default;
473
474 /// Create InstrProfSymtab from an object file section which
475 /// contains function PGO names. When section may contain raw
476 /// string data or string data in compressed form. This method
477 /// only initialize the symtab with reference to the data and
478 /// the section base address. The decompression will be delayed
479 /// until before it is used. See also \c create(StringRef) method.
480 Error create(object::SectionRef &Section);
481
482 /// \c NameStrings is a string composed of one of more sub-strings
483 /// encoded in the format described in \c collectPGOFuncNameStrings.
484 /// This method is a wrapper to \c readPGOFuncNameStrings method.
485 Error create(StringRef NameStrings);
486
487 /// This interface is used by reader of CoverageMapping test
488 /// format.
489 inline Error create(StringRef D, uint64_t BaseAddr);
490
491 /// A wrapper interface to populate the PGO symtab with functions
492 /// decls from module \c M. This interface is used by transformation
493 /// passes such as indirect function call promotion. Variable \c InLTO
494 /// indicates if this is called from LTO optimization passes.
495 Error create(Module &M, bool InLTO = false);
496
497 /// Create InstrProfSymtab from a set of names iteratable from
498 /// \p IterRange. This interface is used by IndexedProfReader.
499 template <typename NameIterRange> Error create(const NameIterRange &IterRange);
500
501 /// Update the symtab by adding \p FuncName to the table. This interface
502 /// is used by the raw and text profile readers.
addFuncName(StringRef FuncName)503 Error addFuncName(StringRef FuncName) {
504 if (FuncName.empty())
505 return make_error<InstrProfError>(instrprof_error::malformed,
506 "function name is empty");
507 auto Ins = NameTab.insert(FuncName);
508 if (Ins.second) {
509 MD5NameMap.push_back(std::make_pair(
510 IndexedInstrProf::ComputeHash(FuncName), Ins.first->getKey()));
511 Sorted = false;
512 }
513 return Error::success();
514 }
515
516 /// Map a function address to its name's MD5 hash. This interface
517 /// is only used by the raw profiler reader.
mapAddress(uint64_t Addr,uint64_t MD5Val)518 void mapAddress(uint64_t Addr, uint64_t MD5Val) {
519 AddrToMD5Map.push_back(std::make_pair(Addr, MD5Val));
520 }
521
522 /// Return a function's hash, or 0, if the function isn't in this SymTab.
523 uint64_t getFunctionHashFromAddress(uint64_t Address);
524
525 /// Return function's PGO name from the function name's symbol
526 /// address in the object file. If an error occurs, return
527 /// an empty string.
528 StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
529
530 /// Return name of functions or global variables from the name's md5 hash
531 /// value. If not found, return an empty string.
532 inline StringRef getFuncOrVarName(uint64_t ValMD5Hash);
533
534 /// Just like getFuncOrVarName, except that it will return literal string
535 /// 'External Symbol' if the function or global variable is external to
536 /// this symbol table.
537 inline StringRef getFuncOrVarNameIfDefined(uint64_t ValMD5Hash);
538
539 /// True if Symbol is the value used to represent external symbols.
isExternalSymbol(const StringRef & Symbol)540 static bool isExternalSymbol(const StringRef &Symbol) {
541 return Symbol == InstrProfSymtab::getExternalSymbol();
542 }
543
544 /// Return function from the name's md5 hash. Return nullptr if not found.
545 inline Function *getFunction(uint64_t FuncMD5Hash);
546
547 /// Return the name section data.
getNameData()548 inline StringRef getNameData() const { return Data; }
549
550 /// Dump the symbols in this table.
551 void dumpNames(raw_ostream &OS) const;
552 };
553
create(StringRef D,uint64_t BaseAddr)554 Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
555 Data = D;
556 Address = BaseAddr;
557 return Error::success();
558 }
559
560 template <typename NameIterRange>
create(const NameIterRange & IterRange)561 Error InstrProfSymtab::create(const NameIterRange &IterRange) {
562 for (auto Name : IterRange)
563 if (Error E = addFuncName(Name))
564 return E;
565
566 finalizeSymtab();
567 return Error::success();
568 }
569
finalizeSymtab()570 void InstrProfSymtab::finalizeSymtab() {
571 if (Sorted)
572 return;
573 llvm::sort(MD5NameMap, less_first());
574 llvm::sort(MD5FuncMap, less_first());
575 llvm::sort(AddrToMD5Map, less_first());
576 AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()),
577 AddrToMD5Map.end());
578 Sorted = true;
579 }
580
getFuncOrVarNameIfDefined(uint64_t MD5Hash)581 StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) {
582 StringRef ret = getFuncOrVarName(MD5Hash);
583 if (ret.empty())
584 return InstrProfSymtab::getExternalSymbol();
585 return ret;
586 }
587
getFuncOrVarName(uint64_t MD5Hash)588 StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
589 finalizeSymtab();
590 auto Result = llvm::lower_bound(MD5NameMap, MD5Hash,
591 [](const std::pair<uint64_t, StringRef> &LHS,
592 uint64_t RHS) { return LHS.first < RHS; });
593 if (Result != MD5NameMap.end() && Result->first == MD5Hash)
594 return Result->second;
595 return StringRef();
596 }
597
getFunction(uint64_t FuncMD5Hash)598 Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
599 finalizeSymtab();
600 auto Result = llvm::lower_bound(MD5FuncMap, FuncMD5Hash,
601 [](const std::pair<uint64_t, Function *> &LHS,
602 uint64_t RHS) { return LHS.first < RHS; });
603 if (Result != MD5FuncMap.end() && Result->first == FuncMD5Hash)
604 return Result->second;
605 return nullptr;
606 }
607
608 // To store the sums of profile count values, or the percentage of
609 // the sums of the total count values.
610 struct CountSumOrPercent {
611 uint64_t NumEntries;
612 double CountSum;
613 double ValueCounts[IPVK_Last - IPVK_First + 1];
CountSumOrPercentCountSumOrPercent614 CountSumOrPercent() : NumEntries(0), CountSum(0.0f), ValueCounts() {}
resetCountSumOrPercent615 void reset() {
616 NumEntries = 0;
617 CountSum = 0.0f;
618 for (double &VC : ValueCounts)
619 VC = 0.0f;
620 }
621 };
622
623 // Function level or program level overlap information.
624 struct OverlapStats {
625 enum OverlapStatsLevel { ProgramLevel, FunctionLevel };
626 // Sum of the total count values for the base profile.
627 CountSumOrPercent Base;
628 // Sum of the total count values for the test profile.
629 CountSumOrPercent Test;
630 // Overlap lap score. Should be in range of [0.0f to 1.0f].
631 CountSumOrPercent Overlap;
632 CountSumOrPercent Mismatch;
633 CountSumOrPercent Unique;
634 OverlapStatsLevel Level;
635 const std::string *BaseFilename;
636 const std::string *TestFilename;
637 StringRef FuncName;
638 uint64_t FuncHash;
639 bool Valid;
640
641 OverlapStats(OverlapStatsLevel L = ProgramLevel)
LevelOverlapStats642 : Level(L), BaseFilename(nullptr), TestFilename(nullptr), FuncHash(0),
643 Valid(false) {}
644
645 void dump(raw_fd_ostream &OS) const;
646
setFuncInfoOverlapStats647 void setFuncInfo(StringRef Name, uint64_t Hash) {
648 FuncName = Name;
649 FuncHash = Hash;
650 }
651
652 Error accumulateCounts(const std::string &BaseFilename,
653 const std::string &TestFilename, bool IsCS);
654 void addOneMismatch(const CountSumOrPercent &MismatchFunc);
655 void addOneUnique(const CountSumOrPercent &UniqueFunc);
656
scoreOverlapStats657 static inline double score(uint64_t Val1, uint64_t Val2, double Sum1,
658 double Sum2) {
659 if (Sum1 < 1.0f || Sum2 < 1.0f)
660 return 0.0f;
661 return std::min(Val1 / Sum1, Val2 / Sum2);
662 }
663 };
664
665 // This is used to filter the functions whose overlap information
666 // to be output.
667 struct OverlapFuncFilters {
668 uint64_t ValueCutoff;
669 const std::string NameFilter;
670 };
671
672 struct InstrProfValueSiteRecord {
673 /// Value profiling data pairs at a given value site.
674 std::list<InstrProfValueData> ValueData;
675
InstrProfValueSiteRecordInstrProfValueSiteRecord676 InstrProfValueSiteRecord() { ValueData.clear(); }
677 template <class InputIterator>
InstrProfValueSiteRecordInstrProfValueSiteRecord678 InstrProfValueSiteRecord(InputIterator F, InputIterator L)
679 : ValueData(F, L) {}
680
681 /// Sort ValueData ascending by Value
sortByTargetValuesInstrProfValueSiteRecord682 void sortByTargetValues() {
683 ValueData.sort(
684 [](const InstrProfValueData &left, const InstrProfValueData &right) {
685 return left.Value < right.Value;
686 });
687 }
688 /// Sort ValueData Descending by Count
689 inline void sortByCount();
690
691 /// Merge data from another InstrProfValueSiteRecord
692 /// Optionally scale merged counts by \p Weight.
693 void merge(InstrProfValueSiteRecord &Input, uint64_t Weight,
694 function_ref<void(instrprof_error)> Warn);
695 /// Scale up value profile data counts by N (Numerator) / D (Denominator).
696 void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
697
698 /// Compute the overlap b/w this record and Input record.
699 void overlap(InstrProfValueSiteRecord &Input, uint32_t ValueKind,
700 OverlapStats &Overlap, OverlapStats &FuncLevelOverlap);
701 };
702
703 /// Profiling information for a single function.
704 struct InstrProfRecord {
705 std::vector<uint64_t> Counts;
706 std::vector<uint8_t> BitmapBytes;
707
708 InstrProfRecord() = default;
InstrProfRecordInstrProfRecord709 InstrProfRecord(std::vector<uint64_t> Counts) : Counts(std::move(Counts)) {}
InstrProfRecordInstrProfRecord710 InstrProfRecord(std::vector<uint64_t> Counts,
711 std::vector<uint8_t> BitmapBytes)
712 : Counts(std::move(Counts)), BitmapBytes(std::move(BitmapBytes)) {}
713 InstrProfRecord(InstrProfRecord &&) = default;
InstrProfRecordInstrProfRecord714 InstrProfRecord(const InstrProfRecord &RHS)
715 : Counts(RHS.Counts), BitmapBytes(RHS.BitmapBytes),
716 ValueData(RHS.ValueData
717 ? std::make_unique<ValueProfData>(*RHS.ValueData)
718 : nullptr) {}
719 InstrProfRecord &operator=(InstrProfRecord &&) = default;
720 InstrProfRecord &operator=(const InstrProfRecord &RHS) {
721 Counts = RHS.Counts;
722 BitmapBytes = RHS.BitmapBytes;
723 if (!RHS.ValueData) {
724 ValueData = nullptr;
725 return *this;
726 }
727 if (!ValueData)
728 ValueData = std::make_unique<ValueProfData>(*RHS.ValueData);
729 else
730 *ValueData = *RHS.ValueData;
731 return *this;
732 }
733
734 /// Return the number of value profile kinds with non-zero number
735 /// of profile sites.
736 inline uint32_t getNumValueKinds() const;
737 /// Return the number of instrumented sites for ValueKind.
738 inline uint32_t getNumValueSites(uint32_t ValueKind) const;
739
740 /// Return the total number of ValueData for ValueKind.
741 inline uint32_t getNumValueData(uint32_t ValueKind) const;
742
743 /// Return the number of value data collected for ValueKind at profiling
744 /// site: Site.
745 inline uint32_t getNumValueDataForSite(uint32_t ValueKind,
746 uint32_t Site) const;
747
748 /// Return the array of profiled values at \p Site. If \p TotalC
749 /// is not null, the total count of all target values at this site
750 /// will be stored in \c *TotalC.
751 inline std::unique_ptr<InstrProfValueData[]>
752 getValueForSite(uint32_t ValueKind, uint32_t Site,
753 uint64_t *TotalC = nullptr) const;
754
755 /// Get the target value/counts of kind \p ValueKind collected at site
756 /// \p Site and store the result in array \p Dest. Return the total
757 /// counts of all target values at this site.
758 inline uint64_t getValueForSite(InstrProfValueData Dest[], uint32_t ValueKind,
759 uint32_t Site) const;
760
761 /// Reserve space for NumValueSites sites.
762 inline void reserveSites(uint32_t ValueKind, uint32_t NumValueSites);
763
764 /// Add ValueData for ValueKind at value Site.
765 void addValueData(uint32_t ValueKind, uint32_t Site,
766 InstrProfValueData *VData, uint32_t N,
767 InstrProfSymtab *SymTab);
768
769 /// Merge the counts in \p Other into this one.
770 /// Optionally scale merged counts by \p Weight.
771 void merge(InstrProfRecord &Other, uint64_t Weight,
772 function_ref<void(instrprof_error)> Warn);
773
774 /// Scale up profile counts (including value profile data) by
775 /// a factor of (N / D).
776 void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
777
778 /// Sort value profile data (per site) by count.
sortValueDataInstrProfRecord779 void sortValueData() {
780 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
781 for (auto &SR : getValueSitesForKind(Kind))
782 SR.sortByCount();
783 }
784
785 /// Clear value data entries and edge counters.
ClearInstrProfRecord786 void Clear() {
787 Counts.clear();
788 clearValueData();
789 }
790
791 /// Clear value data entries
clearValueDataInstrProfRecord792 void clearValueData() { ValueData = nullptr; }
793
794 /// Compute the sums of all counts and store in Sum.
795 void accumulateCounts(CountSumOrPercent &Sum) const;
796
797 /// Compute the overlap b/w this IntrprofRecord and Other.
798 void overlap(InstrProfRecord &Other, OverlapStats &Overlap,
799 OverlapStats &FuncLevelOverlap, uint64_t ValueCutoff);
800
801 /// Compute the overlap of value profile counts.
802 void overlapValueProfData(uint32_t ValueKind, InstrProfRecord &Src,
803 OverlapStats &Overlap,
804 OverlapStats &FuncLevelOverlap);
805
806 enum CountPseudoKind {
807 NotPseudo = 0,
808 PseudoHot,
809 PseudoWarm,
810 };
811 enum PseudoCountVal {
812 HotFunctionVal = -1,
813 WarmFunctionVal = -2,
814 };
getCountPseudoKindInstrProfRecord815 CountPseudoKind getCountPseudoKind() const {
816 uint64_t FirstCount = Counts[0];
817 if (FirstCount == (uint64_t)HotFunctionVal)
818 return PseudoHot;
819 if (FirstCount == (uint64_t)WarmFunctionVal)
820 return PseudoWarm;
821 return NotPseudo;
822 }
setPseudoCountInstrProfRecord823 void setPseudoCount(CountPseudoKind Kind) {
824 if (Kind == PseudoHot)
825 Counts[0] = (uint64_t)HotFunctionVal;
826 else if (Kind == PseudoWarm)
827 Counts[0] = (uint64_t)WarmFunctionVal;
828 }
829
830 private:
831 struct ValueProfData {
832 std::vector<InstrProfValueSiteRecord> IndirectCallSites;
833 std::vector<InstrProfValueSiteRecord> MemOPSizes;
834 std::vector<InstrProfValueSiteRecord> VTableTargets;
835 };
836 std::unique_ptr<ValueProfData> ValueData;
837
838 MutableArrayRef<InstrProfValueSiteRecord>
getValueSitesForKindInstrProfRecord839 getValueSitesForKind(uint32_t ValueKind) {
840 // Cast to /add/ const (should be an implicit_cast, ideally, if that's ever
841 // implemented in LLVM) to call the const overload of this function, then
842 // cast away the constness from the result.
843 auto AR = const_cast<const InstrProfRecord *>(this)->getValueSitesForKind(
844 ValueKind);
845 return MutableArrayRef(
846 const_cast<InstrProfValueSiteRecord *>(AR.data()), AR.size());
847 }
848 ArrayRef<InstrProfValueSiteRecord>
getValueSitesForKindInstrProfRecord849 getValueSitesForKind(uint32_t ValueKind) const {
850 if (!ValueData)
851 return std::nullopt;
852 switch (ValueKind) {
853 case IPVK_IndirectCallTarget:
854 return ValueData->IndirectCallSites;
855 case IPVK_MemOPSize:
856 return ValueData->MemOPSizes;
857 case IPVK_VTableTarget:
858 return ValueData->VTableTargets;
859 default:
860 llvm_unreachable("Unknown value kind!");
861 }
862 }
863
864 std::vector<InstrProfValueSiteRecord> &
getOrCreateValueSitesForKindInstrProfRecord865 getOrCreateValueSitesForKind(uint32_t ValueKind) {
866 if (!ValueData)
867 ValueData = std::make_unique<ValueProfData>();
868 switch (ValueKind) {
869 case IPVK_IndirectCallTarget:
870 return ValueData->IndirectCallSites;
871 case IPVK_MemOPSize:
872 return ValueData->MemOPSizes;
873 default:
874 llvm_unreachable("Unknown value kind!");
875 }
876 }
877
878 // Map indirect call target name hash to name string.
879 uint64_t remapValue(uint64_t Value, uint32_t ValueKind,
880 InstrProfSymtab *SymTab);
881
882 // Merge Value Profile data from Src record to this record for ValueKind.
883 // Scale merged value counts by \p Weight.
884 void mergeValueProfData(uint32_t ValkeKind, InstrProfRecord &Src,
885 uint64_t Weight,
886 function_ref<void(instrprof_error)> Warn);
887
888 // Scale up value profile data count by N (Numerator) / D (Denominator).
889 void scaleValueProfData(uint32_t ValueKind, uint64_t N, uint64_t D,
890 function_ref<void(instrprof_error)> Warn);
891 };
892
893 struct NamedInstrProfRecord : InstrProfRecord {
894 StringRef Name;
895 uint64_t Hash;
896
897 // We reserve this bit as the flag for context sensitive profile record.
898 static const int CS_FLAG_IN_FUNC_HASH = 60;
899
900 NamedInstrProfRecord() = default;
NamedInstrProfRecordNamedInstrProfRecord901 NamedInstrProfRecord(StringRef Name, uint64_t Hash,
902 std::vector<uint64_t> Counts)
903 : InstrProfRecord(std::move(Counts)), Name(Name), Hash(Hash) {}
NamedInstrProfRecordNamedInstrProfRecord904 NamedInstrProfRecord(StringRef Name, uint64_t Hash,
905 std::vector<uint64_t> Counts,
906 std::vector<uint8_t> BitmapBytes)
907 : InstrProfRecord(std::move(Counts), std::move(BitmapBytes)), Name(Name),
908 Hash(Hash) {}
909
hasCSFlagInHashNamedInstrProfRecord910 static bool hasCSFlagInHash(uint64_t FuncHash) {
911 return ((FuncHash >> CS_FLAG_IN_FUNC_HASH) & 1);
912 }
setCSFlagInHashNamedInstrProfRecord913 static void setCSFlagInHash(uint64_t &FuncHash) {
914 FuncHash |= ((uint64_t)1 << CS_FLAG_IN_FUNC_HASH);
915 }
916 };
917
getNumValueKinds()918 uint32_t InstrProfRecord::getNumValueKinds() const {
919 uint32_t NumValueKinds = 0;
920 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
921 NumValueKinds += !(getValueSitesForKind(Kind).empty());
922 return NumValueKinds;
923 }
924
getNumValueData(uint32_t ValueKind)925 uint32_t InstrProfRecord::getNumValueData(uint32_t ValueKind) const {
926 uint32_t N = 0;
927 for (const auto &SR : getValueSitesForKind(ValueKind))
928 N += SR.ValueData.size();
929 return N;
930 }
931
getNumValueSites(uint32_t ValueKind)932 uint32_t InstrProfRecord::getNumValueSites(uint32_t ValueKind) const {
933 return getValueSitesForKind(ValueKind).size();
934 }
935
getNumValueDataForSite(uint32_t ValueKind,uint32_t Site)936 uint32_t InstrProfRecord::getNumValueDataForSite(uint32_t ValueKind,
937 uint32_t Site) const {
938 return getValueSitesForKind(ValueKind)[Site].ValueData.size();
939 }
940
941 std::unique_ptr<InstrProfValueData[]>
getValueForSite(uint32_t ValueKind,uint32_t Site,uint64_t * TotalC)942 InstrProfRecord::getValueForSite(uint32_t ValueKind, uint32_t Site,
943 uint64_t *TotalC) const {
944 uint64_t Dummy = 0;
945 uint64_t &TotalCount = (TotalC == nullptr ? Dummy : *TotalC);
946 uint32_t N = getNumValueDataForSite(ValueKind, Site);
947 if (N == 0) {
948 TotalCount = 0;
949 return std::unique_ptr<InstrProfValueData[]>(nullptr);
950 }
951
952 auto VD = std::make_unique<InstrProfValueData[]>(N);
953 TotalCount = getValueForSite(VD.get(), ValueKind, Site);
954
955 return VD;
956 }
957
getValueForSite(InstrProfValueData Dest[],uint32_t ValueKind,uint32_t Site)958 uint64_t InstrProfRecord::getValueForSite(InstrProfValueData Dest[],
959 uint32_t ValueKind,
960 uint32_t Site) const {
961 uint32_t I = 0;
962 uint64_t TotalCount = 0;
963 for (auto V : getValueSitesForKind(ValueKind)[Site].ValueData) {
964 Dest[I].Value = V.Value;
965 Dest[I].Count = V.Count;
966 TotalCount = SaturatingAdd(TotalCount, V.Count);
967 I++;
968 }
969 return TotalCount;
970 }
971
reserveSites(uint32_t ValueKind,uint32_t NumValueSites)972 void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {
973 if (!NumValueSites)
974 return;
975 getOrCreateValueSitesForKind(ValueKind).reserve(NumValueSites);
976 }
977
978 // Include definitions for value profile data
979 #define INSTR_PROF_VALUE_PROF_DATA
980 #include "llvm/ProfileData/InstrProfData.inc"
981
sortByCount()982 void InstrProfValueSiteRecord::sortByCount() {
983 ValueData.sort(
984 [](const InstrProfValueData &left, const InstrProfValueData &right) {
985 return left.Count > right.Count;
986 });
987 // Now truncate
988 size_t max_s = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
989 if (ValueData.size() > max_s)
990 ValueData.resize(max_s);
991 }
992
993 namespace IndexedInstrProf {
994
995 enum class HashT : uint32_t {
996 MD5,
997 Last = MD5
998 };
999
ComputeHash(HashT Type,StringRef K)1000 inline uint64_t ComputeHash(HashT Type, StringRef K) {
1001 switch (Type) {
1002 case HashT::MD5:
1003 return MD5Hash(K);
1004 }
1005 llvm_unreachable("Unhandled hash type");
1006 }
1007
1008 const uint64_t Magic = 0x8169666f72706cff; // "\xfflprofi\x81"
1009
1010 enum ProfVersion {
1011 // Version 1 is the first version. In this version, the value of
1012 // a key/value pair can only include profile data of a single function.
1013 // Due to this restriction, the number of block counters for a given
1014 // function is not recorded but derived from the length of the value.
1015 Version1 = 1,
1016 // The version 2 format supports recording profile data of multiple
1017 // functions which share the same key in one value field. To support this,
1018 // the number block counters is recorded as an uint64_t field right after the
1019 // function structural hash.
1020 Version2 = 2,
1021 // Version 3 supports value profile data. The value profile data is expected
1022 // to follow the block counter profile data.
1023 Version3 = 3,
1024 // In this version, profile summary data \c IndexedInstrProf::Summary is
1025 // stored after the profile header.
1026 Version4 = 4,
1027 // In this version, the frontend PGO stable hash algorithm defaults to V2.
1028 Version5 = 5,
1029 // In this version, the frontend PGO stable hash algorithm got fixed and
1030 // may produce hashes different from Version5.
1031 Version6 = 6,
1032 // An additional counter is added around logical operators.
1033 Version7 = 7,
1034 // An additional (optional) memory profile type is added.
1035 Version8 = 8,
1036 // Binary ids are added.
1037 Version9 = 9,
1038 // An additional (optional) temporal profile traces section is added.
1039 Version10 = 10,
1040 // An additional field is used for bitmap bytes.
1041 Version11 = 11,
1042 // VTable profiling,
1043 Version12 = 12,
1044 // The current version is 12.
1045 CurrentVersion = INSTR_PROF_INDEX_VERSION
1046 };
1047 const uint64_t Version = ProfVersion::CurrentVersion;
1048
1049 const HashT HashType = HashT::MD5;
1050
ComputeHash(StringRef K)1051 inline uint64_t ComputeHash(StringRef K) { return ComputeHash(HashType, K); }
1052
1053 // This structure defines the file header of the LLVM profile
1054 // data file in indexed-format. Please update llvm/docs/InstrProfileFormat.rst
1055 // as appropriate when updating the indexed profile format.
1056 struct Header {
1057 uint64_t Magic;
1058 uint64_t Version;
1059 uint64_t Unused; // Becomes unused since version 4
1060 uint64_t HashType;
1061 uint64_t HashOffset;
1062 uint64_t MemProfOffset;
1063 uint64_t BinaryIdOffset;
1064 uint64_t TemporalProfTracesOffset;
1065 uint64_t VTableNamesOffset;
1066 // New fields should only be added at the end to ensure that the size
1067 // computation is correct. The methods below need to be updated to ensure that
1068 // the new field is read correctly.
1069
1070 // Reads a header struct from the buffer.
1071 static Expected<Header> readFromBuffer(const unsigned char *Buffer);
1072
1073 // Returns the size of the header in bytes for all valid fields based on the
1074 // version. I.e a older version header will return a smaller size.
1075 size_t size() const;
1076
1077 // Returns the format version in little endian. The header retains the version
1078 // in native endian of the compiler runtime.
1079 uint64_t formatVersion() const;
1080 };
1081
1082 // Profile summary data recorded in the profile data file in indexed
1083 // format. It is introduced in version 4. The summary data follows
1084 // right after the profile file header.
1085 struct Summary {
1086 struct Entry {
1087 uint64_t Cutoff; ///< The required percentile of total execution count.
1088 uint64_t
1089 MinBlockCount; ///< The minimum execution count for this percentile.
1090 uint64_t NumBlocks; ///< Number of blocks >= the minumum execution count.
1091 };
1092 // The field kind enumerator to assigned value mapping should remain
1093 // unchanged when a new kind is added or an old kind gets deleted in
1094 // the future.
1095 enum SummaryFieldKind {
1096 /// The total number of functions instrumented.
1097 TotalNumFunctions = 0,
1098 /// Total number of instrumented blocks/edges.
1099 TotalNumBlocks = 1,
1100 /// The maximal execution count among all functions.
1101 /// This field does not exist for profile data from IR based
1102 /// instrumentation.
1103 MaxFunctionCount = 2,
1104 /// Max block count of the program.
1105 MaxBlockCount = 3,
1106 /// Max internal block count of the program (excluding entry blocks).
1107 MaxInternalBlockCount = 4,
1108 /// The sum of all instrumented block counts.
1109 TotalBlockCount = 5,
1110 NumKinds = TotalBlockCount + 1
1111 };
1112
1113 // The number of summmary fields following the summary header.
1114 uint64_t NumSummaryFields;
1115 // The number of Cutoff Entries (Summary::Entry) following summary fields.
1116 uint64_t NumCutoffEntries;
1117
1118 Summary() = delete;
SummarySummary1119 Summary(uint32_t Size) { memset(this, 0, Size); }
1120
deleteSummary1121 void operator delete(void *ptr) { ::operator delete(ptr); }
1122
getSizeSummary1123 static uint32_t getSize(uint32_t NumSumFields, uint32_t NumCutoffEntries) {
1124 return sizeof(Summary) + NumCutoffEntries * sizeof(Entry) +
1125 NumSumFields * sizeof(uint64_t);
1126 }
1127
getSummaryDataBaseSummary1128 const uint64_t *getSummaryDataBase() const {
1129 return reinterpret_cast<const uint64_t *>(this + 1);
1130 }
1131
getSummaryDataBaseSummary1132 uint64_t *getSummaryDataBase() {
1133 return reinterpret_cast<uint64_t *>(this + 1);
1134 }
1135
getCutoffEntryBaseSummary1136 const Entry *getCutoffEntryBase() const {
1137 return reinterpret_cast<const Entry *>(
1138 &getSummaryDataBase()[NumSummaryFields]);
1139 }
1140
getCutoffEntryBaseSummary1141 Entry *getCutoffEntryBase() {
1142 return reinterpret_cast<Entry *>(&getSummaryDataBase()[NumSummaryFields]);
1143 }
1144
getSummary1145 uint64_t get(SummaryFieldKind K) const {
1146 return getSummaryDataBase()[K];
1147 }
1148
setSummary1149 void set(SummaryFieldKind K, uint64_t V) {
1150 getSummaryDataBase()[K] = V;
1151 }
1152
getEntrySummary1153 const Entry &getEntry(uint32_t I) const { return getCutoffEntryBase()[I]; }
1154
setEntrySummary1155 void setEntry(uint32_t I, const ProfileSummaryEntry &E) {
1156 Entry &ER = getCutoffEntryBase()[I];
1157 ER.Cutoff = E.Cutoff;
1158 ER.MinBlockCount = E.MinCount;
1159 ER.NumBlocks = E.NumCounts;
1160 }
1161 };
1162
allocSummary(uint32_t TotalSize)1163 inline std::unique_ptr<Summary> allocSummary(uint32_t TotalSize) {
1164 return std::unique_ptr<Summary>(new (::operator new(TotalSize))
1165 Summary(TotalSize));
1166 }
1167
1168 } // end namespace IndexedInstrProf
1169
1170 namespace RawInstrProf {
1171
1172 // Version 1: First version
1173 // Version 2: Added value profile data section. Per-function control data
1174 // struct has more fields to describe value profile information.
1175 // Version 3: Compressed name section support. Function PGO name reference
1176 // from control data struct is changed from raw pointer to Name's MD5 value.
1177 // Version 4: ValueDataBegin and ValueDataSizes fields are removed from the
1178 // raw header.
1179 // Version 5: Bit 60 of FuncHash is reserved for the flag for the context
1180 // sensitive records.
1181 // Version 6: Added binary id.
1182 // Version 7: Reorder binary id and include version in signature.
1183 // Version 8: Use relative counter pointer.
1184 // Version 9: Added relative bitmap bytes pointer and count used by MC/DC.
1185 const uint64_t Version = INSTR_PROF_RAW_VERSION;
1186
1187 template <class IntPtrT> inline uint64_t getMagic();
1188 template <> inline uint64_t getMagic<uint64_t>() {
1189 return INSTR_PROF_RAW_MAGIC_64;
1190 }
1191
1192 template <> inline uint64_t getMagic<uint32_t>() {
1193 return INSTR_PROF_RAW_MAGIC_32;
1194 }
1195
1196 // Per-function profile data header/control structure.
1197 // The definition should match the structure defined in
1198 // compiler-rt/lib/profile/InstrProfiling.h.
1199 // It should also match the synthesized type in
1200 // Transforms/Instrumentation/InstrProfiling.cpp:getOrCreateRegionCounters.
1201 template <class IntPtrT> struct alignas(8) ProfileData {
1202 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Type Name;
1203 #include "llvm/ProfileData/InstrProfData.inc"
1204 };
1205
1206 template <class IntPtrT> struct alignas(8) VTableProfileData {
1207 #define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) Type Name;
1208 #include "llvm/ProfileData/InstrProfData.inc"
1209 };
1210
1211 // File header structure of the LLVM profile data in raw format.
1212 // The definition should match the header referenced in
1213 // compiler-rt/lib/profile/InstrProfilingFile.c and
1214 // InstrProfilingBuffer.c.
1215 struct Header {
1216 #define INSTR_PROF_RAW_HEADER(Type, Name, Init) const Type Name;
1217 #include "llvm/ProfileData/InstrProfData.inc"
1218 };
1219
1220 } // end namespace RawInstrProf
1221
1222 // Create the variable for the profile file name.
1223 void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput);
1224
1225 // Whether to compress function names in profile records, and filenames in
1226 // code coverage mappings. Used by the Instrumentation library and unit tests.
1227 extern cl::opt<bool> DoInstrProfNameCompression;
1228
1229 } // end namespace llvm
1230 #endif // LLVM_PROFILEDATA_INSTRPROF_H
1231