Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:24

0001 //===- InstrProf.h - Instrumented profiling format support ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Instrumentation-based profiling data is generated by instrumented
0010 // binaries through library functions in compiler-rt, and read by the clang
0011 // frontend to feed PGO.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_PROFILEDATA_INSTRPROF_H
0016 #define LLVM_PROFILEDATA_INSTRPROF_H
0017 
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/BitmaskEnum.h"
0020 #include "llvm/ADT/DenseMap.h"
0021 #include "llvm/ADT/IntervalMap.h"
0022 #include "llvm/ADT/STLExtras.h"
0023 #include "llvm/ADT/StringRef.h"
0024 #include "llvm/ADT/StringSet.h"
0025 #include "llvm/IR/GlobalValue.h"
0026 #include "llvm/IR/ProfileSummary.h"
0027 #include "llvm/ProfileData/InstrProfData.inc"
0028 #include "llvm/Support/BalancedPartitioning.h"
0029 #include "llvm/Support/CommandLine.h"
0030 #include "llvm/Support/Compiler.h"
0031 #include "llvm/Support/Error.h"
0032 #include "llvm/Support/ErrorHandling.h"
0033 #include "llvm/Support/MD5.h"
0034 #include "llvm/Support/MathExtras.h"
0035 #include "llvm/Support/raw_ostream.h"
0036 #include "llvm/TargetParser/Host.h"
0037 #include "llvm/TargetParser/Triple.h"
0038 #include <algorithm>
0039 #include <cassert>
0040 #include <cstddef>
0041 #include <cstdint>
0042 #include <cstring>
0043 #include <list>
0044 #include <memory>
0045 #include <string>
0046 #include <system_error>
0047 #include <utility>
0048 #include <vector>
0049 
0050 namespace llvm {
0051 
0052 class Function;
0053 class GlobalVariable;
0054 struct InstrProfRecord;
0055 class InstrProfSymtab;
0056 class Instruction;
0057 class MDNode;
0058 class Module;
0059 
0060 enum InstrProfSectKind {
0061 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Kind,
0062 #include "llvm/ProfileData/InstrProfData.inc"
0063 };
0064 
0065 /// Return the max count value. We reserver a few large values for special use.
0066 inline uint64_t getInstrMaxCountValue() {
0067   return std::numeric_limits<uint64_t>::max() - 2;
0068 }
0069 
0070 /// Return the name of the profile section corresponding to \p IPSK.
0071 ///
0072 /// The name of the section depends on the object format type \p OF. If
0073 /// \p AddSegmentInfo is true, a segment prefix and additional linker hints may
0074 /// be added to the section name (this is the default).
0075 std::string getInstrProfSectionName(InstrProfSectKind IPSK,
0076                                     Triple::ObjectFormatType OF,
0077                                     bool AddSegmentInfo = true);
0078 
0079 /// Return the name profile runtime entry point to do value profiling
0080 /// for a given site.
0081 inline StringRef getInstrProfValueProfFuncName() {
0082   return INSTR_PROF_VALUE_PROF_FUNC_STR;
0083 }
0084 
0085 /// Return the name profile runtime entry point to do memop size value
0086 /// profiling.
0087 inline StringRef getInstrProfValueProfMemOpFuncName() {
0088   return INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR;
0089 }
0090 
0091 /// Return the name prefix of variables containing instrumented function names.
0092 inline StringRef getInstrProfNameVarPrefix() { return "__profn_"; }
0093 
0094 /// Return the name prefix of variables containing virtual table profile data.
0095 inline StringRef getInstrProfVTableVarPrefix() { return "__profvt_"; }
0096 
0097 /// Return the name prefix of variables containing per-function control data.
0098 inline StringRef getInstrProfDataVarPrefix() { return "__profd_"; }
0099 
0100 /// Return the name prefix of profile counter variables.
0101 inline StringRef getInstrProfCountersVarPrefix() { return "__profc_"; }
0102 
0103 /// Return the name prefix of profile bitmap variables.
0104 inline StringRef getInstrProfBitmapVarPrefix() { return "__profbm_"; }
0105 
0106 /// Return the name prefix of value profile variables.
0107 inline StringRef getInstrProfValuesVarPrefix() { return "__profvp_"; }
0108 
0109 /// Return the name of value profile node array variables:
0110 inline StringRef getInstrProfVNodesVarName() { return "__llvm_prf_vnodes"; }
0111 
0112 /// Return the name of the variable holding the strings (possibly compressed)
0113 /// of all function's PGO names.
0114 inline StringRef getInstrProfNamesVarName() { return "__llvm_prf_nm"; }
0115 
0116 inline StringRef getInstrProfVTableNamesVarName() { return "__llvm_prf_vnm"; }
0117 
0118 /// Return the name of a covarage mapping variable (internal linkage)
0119 /// for each instrumented source module. Such variables are allocated
0120 /// in the __llvm_covmap section.
0121 inline StringRef getCoverageMappingVarName() {
0122   return "__llvm_coverage_mapping";
0123 }
0124 
0125 /// Return the name of the internal variable recording the array
0126 /// of PGO name vars referenced by the coverage mapping. The owning
0127 /// functions of those names are not emitted by FE (e.g, unused inline
0128 /// functions.)
0129 inline StringRef getCoverageUnusedNamesVarName() {
0130   return "__llvm_coverage_names";
0131 }
0132 
0133 /// Return the name of function that registers all the per-function control
0134 /// data at program startup time by calling __llvm_register_function. This
0135 /// function has internal linkage and is called by  __llvm_profile_init
0136 /// runtime method. This function is not generated for these platforms:
0137 /// Darwin, Linux, and FreeBSD.
0138 inline StringRef getInstrProfRegFuncsName() {
0139   return "__llvm_profile_register_functions";
0140 }
0141 
0142 /// Return the name of the runtime interface that registers per-function control
0143 /// data for one instrumented function.
0144 inline StringRef getInstrProfRegFuncName() {
0145   return "__llvm_profile_register_function";
0146 }
0147 
0148 /// Return the name of the runtime interface that registers the PGO name
0149 /// strings.
0150 inline StringRef getInstrProfNamesRegFuncName() {
0151   return "__llvm_profile_register_names_function";
0152 }
0153 
0154 /// Return the name of the runtime initialization method that is generated by
0155 /// the compiler. The function calls __llvm_profile_register_functions and
0156 /// __llvm_profile_override_default_filename functions if needed. This function
0157 /// has internal linkage and invoked at startup time via init_array.
0158 inline StringRef getInstrProfInitFuncName() { return "__llvm_profile_init"; }
0159 
0160 /// Return the name of the hook variable defined in profile runtime library.
0161 /// A reference to the variable causes the linker to link in the runtime
0162 /// initialization module (which defines the hook variable).
0163 inline StringRef getInstrProfRuntimeHookVarName() {
0164   return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_RUNTIME_VAR);
0165 }
0166 
0167 /// Return the name of the compiler generated function that references the
0168 /// runtime hook variable. The function is a weak global.
0169 inline StringRef getInstrProfRuntimeHookVarUseFuncName() {
0170   return "__llvm_profile_runtime_user";
0171 }
0172 
0173 inline StringRef getInstrProfCounterBiasVarName() {
0174   return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR);
0175 }
0176 
0177 inline StringRef getInstrProfBitmapBiasVarName() {
0178   return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_BITMAP_BIAS_VAR);
0179 }
0180 
0181 /// Return the marker used to separate PGO names during serialization.
0182 inline StringRef getInstrProfNameSeparator() { return "\01"; }
0183 
0184 /// Determines whether module targets a GPU eligable for PGO
0185 /// instrumentation
0186 bool isGPUProfTarget(const Module &M);
0187 
0188 /// Please use getIRPGOFuncName for LLVM IR instrumentation. This function is
0189 /// for front-end (Clang, etc) instrumentation.
0190 /// Return the modified name for function \c F suitable to be
0191 /// used the key for profile lookup. Variable \c InLTO indicates if this
0192 /// is called in LTO optimization passes.
0193 std::string getPGOFuncName(const Function &F, bool InLTO = false,
0194                            uint64_t Version = INSTR_PROF_INDEX_VERSION);
0195 
0196 /// Return the modified name for a function suitable to be
0197 /// used the key for profile lookup. The function's original
0198 /// name is \c RawFuncName and has linkage of type \c Linkage.
0199 /// The function is defined in module \c FileName.
0200 std::string getPGOFuncName(StringRef RawFuncName,
0201                            GlobalValue::LinkageTypes Linkage,
0202                            StringRef FileName,
0203                            uint64_t Version = INSTR_PROF_INDEX_VERSION);
0204 
0205 /// \return the modified name for function \c F suitable to be
0206 /// used as the key for IRPGO profile lookup. \c InLTO indicates if this is
0207 /// called from LTO optimization passes.
0208 std::string getIRPGOFuncName(const Function &F, bool InLTO = false);
0209 
0210 /// \return the filename and the function name parsed from the output of
0211 /// \c getIRPGOFuncName()
0212 std::pair<StringRef, StringRef> getParsedIRPGOName(StringRef IRPGOName);
0213 
0214 /// Return the name of the global variable used to store a function
0215 /// name in PGO instrumentation. \c FuncName is the IRPGO function name
0216 /// (returned by \c getIRPGOFuncName) for LLVM IR instrumentation and PGO
0217 /// function name (returned by \c getPGOFuncName) for front-end instrumentation.
0218 std::string getPGOFuncNameVarName(StringRef FuncName,
0219                                   GlobalValue::LinkageTypes Linkage);
0220 
0221 /// Create and return the global variable for function name used in PGO
0222 /// instrumentation. \c FuncName is the IRPGO function name (returned by
0223 /// \c getIRPGOFuncName) for LLVM IR instrumentation and PGO function name
0224 /// (returned by \c getPGOFuncName) for front-end instrumentation.
0225 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName);
0226 
0227 /// Create and return the global variable for function name used in PGO
0228 /// instrumentation. \c FuncName is the IRPGO function name (returned by
0229 /// \c getIRPGOFuncName) for LLVM IR instrumentation and PGO function name
0230 /// (returned by \c getPGOFuncName) for front-end instrumentation.
0231 GlobalVariable *createPGOFuncNameVar(Module &M,
0232                                      GlobalValue::LinkageTypes Linkage,
0233                                      StringRef PGOFuncName);
0234 
0235 /// Return the initializer in string of the PGO name var \c NameVar.
0236 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar);
0237 
0238 /// Given a PGO function name, remove the filename prefix and return
0239 /// the original (static) function name.
0240 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
0241                                    StringRef FileName = "<unknown>");
0242 
0243 /// Given a vector of strings (names of global objects like functions or,
0244 /// virtual tables) \c NameStrs, the method generates a combined string \c
0245 /// Result that is ready to be serialized.  The \c Result string is comprised of
0246 /// three fields: The first field is the length of the uncompressed strings, and
0247 /// the the second field is the length of the zlib-compressed string. Both
0248 /// fields are encoded in ULEB128.  If \c doCompress is false, the
0249 ///  third field is the uncompressed strings; otherwise it is the
0250 /// compressed string. When the string compression is off, the
0251 /// second field will have value zero.
0252 Error collectGlobalObjectNameStrings(ArrayRef<std::string> NameStrs,
0253                                      bool doCompression, std::string &Result);
0254 
0255 /// Produce \c Result string with the same format described above. The input
0256 /// is vector of PGO function name variables that are referenced.
0257 /// The global variable element in 'NameVars' is a string containing the pgo
0258 /// name of a function. See `createPGOFuncNameVar` that creates these global
0259 /// variables.
0260 Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
0261                                 std::string &Result, bool doCompression = true);
0262 
0263 Error collectVTableStrings(ArrayRef<GlobalVariable *> VTables,
0264                            std::string &Result, bool doCompression);
0265 
0266 /// Check if INSTR_PROF_RAW_VERSION_VAR is defined. This global is only being
0267 /// set in IR PGO compilation.
0268 bool isIRPGOFlagSet(const Module *M);
0269 
0270 /// Check if we can safely rename this Comdat function. Instances of the same
0271 /// comdat function may have different control flows thus can not share the
0272 /// same counter variable.
0273 bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken = false);
0274 
0275 enum InstrProfValueKind : uint32_t {
0276 #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
0277 #include "llvm/ProfileData/InstrProfData.inc"
0278 };
0279 
0280 /// Get the value profile data for value site \p SiteIdx from \p InstrProfR
0281 /// and annotate the instruction \p Inst with the value profile meta data.
0282 /// Annotate up to \p MaxMDCount (default 3) number of records per value site.
0283 void annotateValueSite(Module &M, Instruction &Inst,
0284                        const InstrProfRecord &InstrProfR,
0285                        InstrProfValueKind ValueKind, uint32_t SiteIndx,
0286                        uint32_t MaxMDCount = 3);
0287 
0288 /// Same as the above interface but using an ArrayRef, as well as \p Sum.
0289 /// This function will not annotate !prof metadata on the instruction if the
0290 /// referenced array is empty.
0291 void annotateValueSite(Module &M, Instruction &Inst,
0292                        ArrayRef<InstrProfValueData> VDs, uint64_t Sum,
0293                        InstrProfValueKind ValueKind, uint32_t MaxMDCount);
0294 
0295 // TODO: Unify metadata name 'PGOFuncName' and 'PGOName', by supporting read
0296 // of this metadata for backward compatibility and generating 'PGOName' only.
0297 /// Extract the value profile data from \p Inst and returns them if \p Inst is
0298 /// annotated with value profile data. Returns an empty vector otherwise.
0299 SmallVector<InstrProfValueData, 4>
0300 getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
0301                          uint32_t MaxNumValueData, uint64_t &TotalC,
0302                          bool GetNoICPValue = false);
0303 
0304 inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }
0305 
0306 inline StringRef getPGONameMetadataName() { return "PGOName"; }
0307 
0308 /// Return the PGOFuncName meta data associated with a function.
0309 MDNode *getPGOFuncNameMetadata(const Function &F);
0310 
0311 std::string getPGOName(const GlobalVariable &V, bool InLTO = false);
0312 
0313 /// Create the PGOFuncName meta data if PGOFuncName is different from
0314 /// function's raw name. This should only apply to internal linkage functions
0315 /// declared by users only.
0316 /// TODO: Update all callers to 'createPGONameMetadata' and deprecate this
0317 /// function.
0318 void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName);
0319 
0320 /// Create the PGOName metadata if a global object's PGO name is different from
0321 /// its mangled name. This should apply to local-linkage global objects only.
0322 void createPGONameMetadata(GlobalObject &GO, StringRef PGOName);
0323 
0324 /// Check if we can use Comdat for profile variables. This will eliminate
0325 /// the duplicated profile variables for Comdat functions.
0326 bool needsComdatForCounter(const GlobalObject &GV, const Module &M);
0327 
0328 /// An enum describing the attributes of an instrumented profile.
0329 enum class InstrProfKind {
0330   Unknown = 0x0,
0331   // A frontend clang profile, incompatible with other attrs.
0332   FrontendInstrumentation = 0x1,
0333   // An IR-level profile (default when -fprofile-generate is used).
0334   IRInstrumentation = 0x2,
0335   // A profile with entry basic block instrumentation.
0336   FunctionEntryInstrumentation = 0x4,
0337   // A context sensitive IR-level profile.
0338   ContextSensitive = 0x8,
0339   // Use single byte probes for coverage.
0340   SingleByteCoverage = 0x10,
0341   // Only instrument the function entry basic block.
0342   FunctionEntryOnly = 0x20,
0343   // A memory profile collected using -fprofile=memory.
0344   MemProf = 0x40,
0345   // A temporal profile.
0346   TemporalProfile = 0x80,
0347   // A profile with loop entry basic blocks instrumentation.
0348   LoopEntriesInstrumentation = 0x100,
0349   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/LoopEntriesInstrumentation)
0350 };
0351 
0352 const std::error_category &instrprof_category();
0353 
0354 enum class instrprof_error {
0355   success = 0,
0356   eof,
0357   unrecognized_format,
0358   bad_magic,
0359   bad_header,
0360   unsupported_version,
0361   unsupported_hash_type,
0362   too_large,
0363   truncated,
0364   malformed,
0365   missing_correlation_info,
0366   unexpected_correlation_info,
0367   unable_to_correlate_profile,
0368   unknown_function,
0369   invalid_prof,
0370   hash_mismatch,
0371   count_mismatch,
0372   bitmap_mismatch,
0373   counter_overflow,
0374   value_site_count_mismatch,
0375   compress_failed,
0376   uncompress_failed,
0377   empty_raw_profile,
0378   zlib_unavailable,
0379   raw_profile_version_mismatch,
0380   counter_value_too_large,
0381 };
0382 
0383 /// An ordered list of functions identified by their NameRef found in
0384 /// INSTR_PROF_DATA
0385 struct TemporalProfTraceTy {
0386   std::vector<uint64_t> FunctionNameRefs;
0387   uint64_t Weight;
0388   TemporalProfTraceTy(std::initializer_list<uint64_t> Trace = {},
0389                       uint64_t Weight = 1)
0390       : FunctionNameRefs(Trace), Weight(Weight) {}
0391 
0392   /// Use a set of temporal profile traces to create a list of balanced
0393   /// partitioning function nodes used by BalancedPartitioning to generate a
0394   /// function order that reduces page faults during startup
0395   static void createBPFunctionNodes(ArrayRef<TemporalProfTraceTy> Traces,
0396                                     std::vector<BPFunctionNode> &Nodes,
0397                                     bool RemoveOutlierUNs = true);
0398 };
0399 
0400 inline std::error_code make_error_code(instrprof_error E) {
0401   return std::error_code(static_cast<int>(E), instrprof_category());
0402 }
0403 
0404 class InstrProfError : public ErrorInfo<InstrProfError> {
0405 public:
0406   InstrProfError(instrprof_error Err, const Twine &ErrStr = Twine())
0407       : Err(Err), Msg(ErrStr.str()) {
0408     assert(Err != instrprof_error::success && "Not an error");
0409   }
0410 
0411   std::string message() const override;
0412 
0413   void log(raw_ostream &OS) const override { OS << message(); }
0414 
0415   std::error_code convertToErrorCode() const override {
0416     return make_error_code(Err);
0417   }
0418 
0419   instrprof_error get() const { return Err; }
0420   const std::string &getMessage() const { return Msg; }
0421 
0422   /// Consume an Error and return the raw enum value contained within it, and
0423   /// the optional error message. The Error must either be a success value, or
0424   /// contain a single InstrProfError.
0425   static std::pair<instrprof_error, std::string> take(Error E) {
0426     auto Err = instrprof_error::success;
0427     std::string Msg = "";
0428     handleAllErrors(std::move(E), [&Err, &Msg](const InstrProfError &IPE) {
0429       assert(Err == instrprof_error::success && "Multiple errors encountered");
0430       Err = IPE.get();
0431       Msg = IPE.getMessage();
0432     });
0433     return {Err, Msg};
0434   }
0435 
0436   static char ID;
0437 
0438 private:
0439   instrprof_error Err;
0440   std::string Msg;
0441 };
0442 
0443 namespace object {
0444 
0445 class SectionRef;
0446 
0447 } // end namespace object
0448 
0449 namespace IndexedInstrProf {
0450 
0451 uint64_t ComputeHash(StringRef K);
0452 
0453 } // end namespace IndexedInstrProf
0454 
0455 /// A symbol table used for function [IR]PGO name look-up with keys
0456 /// (such as pointers, md5hash values) to the function. A function's
0457 /// [IR]PGO name or name's md5hash are used in retrieving the profile
0458 /// data of the function. See \c getIRPGOFuncName() and \c getPGOFuncName
0459 /// methods for details how [IR]PGO name is formed.
0460 class InstrProfSymtab {
0461 public:
0462   using AddrHashMap = std::vector<std::pair<uint64_t, uint64_t>>;
0463 
0464 private:
0465   using AddrIntervalMap =
0466       IntervalMap<uint64_t, uint64_t, 4, IntervalMapHalfOpenInfo<uint64_t>>;
0467   StringRef Data;
0468   uint64_t Address = 0;
0469   // Unique name strings. Used to ensure entries in MD5NameMap (a vector that's
0470   // going to be sorted) has unique MD5 keys in the first place.
0471   StringSet<> NameTab;
0472   // Records the unique virtual table names. This is used by InstrProfWriter to
0473   // write out an on-disk chained hash table of virtual table names.
0474   // InstrProfWriter stores per function profile data (keyed by function names)
0475   // so it doesn't use a StringSet for function names.
0476   StringSet<> VTableNames;
0477   // A map from MD5 keys to function name strings.
0478   std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
0479   // A map from MD5 keys to function define. We only populate this map
0480   // when build the Symtab from a Module.
0481   std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;
0482   // A map from MD5 to the global variable. This map is only populated when
0483   // building the symtab from a module. Use separate container instances for
0484   // `MD5FuncMap` and `MD5VTableMap`.
0485   // TODO: Unify the container type and the lambda function 'mapName' inside
0486   // add{Func,VTable}WithName.
0487   DenseMap<uint64_t, GlobalVariable *> MD5VTableMap;
0488   // A map from function runtime address to function name MD5 hash.
0489   // This map is only populated and used by raw instr profile reader.
0490   AddrHashMap AddrToMD5Map;
0491 
0492   AddrIntervalMap::Allocator VTableAddrMapAllocator;
0493   // This map is only populated and used by raw instr profile reader.
0494   AddrIntervalMap VTableAddrMap;
0495   bool Sorted = false;
0496 
0497   static StringRef getExternalSymbol() { return "** External Symbol **"; }
0498 
0499   // Returns the canonial name of the given PGOName. In a canonical name, all
0500   // suffixes that begins with "." except ".__uniq." are stripped.
0501   // FIXME: Unify this with `FunctionSamples::getCanonicalFnName`.
0502   static StringRef getCanonicalName(StringRef PGOName);
0503 
0504   // Add the function into the symbol table, by creating the following
0505   // map entries:
0506   // name-set = {PGOFuncName} union {getCanonicalName(PGOFuncName)}
0507   // - In MD5NameMap: <MD5Hash(name), name> for name in name-set
0508   // - In MD5FuncMap: <MD5Hash(name), &F> for name in name-set
0509   // The canonical name is only added if \c AddCanonical is true.
0510   Error addFuncWithName(Function &F, StringRef PGOFuncName, bool AddCanonical);
0511 
0512   // Add the vtable into the symbol table, by creating the following
0513   // map entries:
0514   // name-set = {PGOName} union {getCanonicalName(PGOName)}
0515   // - In MD5NameMap:  <MD5Hash(name), name> for name in name-set
0516   // - In MD5VTableMap: <MD5Hash(name), name> for name in name-set
0517   Error addVTableWithName(GlobalVariable &V, StringRef PGOVTableName);
0518 
0519   // If the symtab is created by a series of calls to \c addFuncName, \c
0520   // finalizeSymtab needs to be called before looking up function names.
0521   // This is required because the underlying map is a vector (for space
0522   // efficiency) which needs to be sorted.
0523   inline void finalizeSymtab();
0524 
0525 public:
0526   InstrProfSymtab() : VTableAddrMap(VTableAddrMapAllocator) {}
0527 
0528   // Not copyable or movable.
0529   // Consider std::unique_ptr for move.
0530   InstrProfSymtab(const InstrProfSymtab &) = delete;
0531   InstrProfSymtab &operator=(const InstrProfSymtab &) = delete;
0532   InstrProfSymtab(InstrProfSymtab &&) = delete;
0533   InstrProfSymtab &operator=(InstrProfSymtab &&) = delete;
0534 
0535   /// Create InstrProfSymtab from an object file section which
0536   /// contains function PGO names. When section may contain raw
0537   /// string data or string data in compressed form. This method
0538   /// only initialize the symtab with reference to the data and
0539   /// the section base address. The decompression will be delayed
0540   /// until before it is used. See also \c create(StringRef) method.
0541   Error create(object::SectionRef &Section);
0542 
0543   /// \c NameStrings is a string composed of one of more sub-strings
0544   ///  encoded in the format described in \c collectPGOFuncNameStrings.
0545   /// This method is a wrapper to \c readAndDecodeStrings method.
0546   Error create(StringRef NameStrings);
0547 
0548   /// Initialize symtab states with function names and vtable names. \c
0549   /// FuncNameStrings is a string composed of one or more encoded function name
0550   /// strings, and \c VTableNameStrings composes of one or more encoded vtable
0551   /// names. This interface is solely used by raw profile reader.
0552   Error create(StringRef FuncNameStrings, StringRef VTableNameStrings);
0553 
0554   /// Initialize 'this' with the set of vtable names encoded in
0555   /// \c CompressedVTableNames.
0556   Error initVTableNamesFromCompressedStrings(StringRef CompressedVTableNames);
0557 
0558   /// This interface is used by reader of CoverageMapping test
0559   /// format.
0560   inline Error create(StringRef D, uint64_t BaseAddr);
0561 
0562   /// A wrapper interface to populate the PGO symtab with functions
0563   /// decls from module \c M. This interface is used by transformation
0564   /// passes such as indirect function call promotion. Variable \c InLTO
0565   /// indicates if this is called from LTO optimization passes.
0566   /// A canonical name, removing non-__uniq suffixes, is added if
0567   /// \c AddCanonical is true.
0568   Error create(Module &M, bool InLTO = false, bool AddCanonical = true);
0569 
0570   /// Create InstrProfSymtab from a set of names iteratable from
0571   /// \p IterRange. This interface is used by IndexedProfReader.
0572   template <typename NameIterRange>
0573   Error create(const NameIterRange &IterRange);
0574 
0575   /// Create InstrProfSymtab from a set of function names and vtable
0576   /// names iteratable from \p IterRange. This interface is used by
0577   /// IndexedProfReader.
0578   template <typename FuncNameIterRange, typename VTableNameIterRange>
0579   Error create(const FuncNameIterRange &FuncIterRange,
0580                const VTableNameIterRange &VTableIterRange);
0581 
0582   // Map the MD5 of the symbol name to the name.
0583   Error addSymbolName(StringRef SymbolName) {
0584     if (SymbolName.empty())
0585       return make_error<InstrProfError>(instrprof_error::malformed,
0586                                         "symbol name is empty");
0587 
0588     // Insert into NameTab so that MD5NameMap (a vector that will be sorted)
0589     // won't have duplicated entries in the first place.
0590     auto Ins = NameTab.insert(SymbolName);
0591     if (Ins.second) {
0592       MD5NameMap.push_back(std::make_pair(
0593           IndexedInstrProf::ComputeHash(SymbolName), Ins.first->getKey()));
0594       Sorted = false;
0595     }
0596     return Error::success();
0597   }
0598 
0599   /// The method name is kept since there are many callers.
0600   /// It just forwards to 'addSymbolName'.
0601   Error addFuncName(StringRef FuncName) { return addSymbolName(FuncName); }
0602 
0603   /// Adds VTableName as a known symbol, and inserts it to a map that
0604   /// tracks all vtable names.
0605   Error addVTableName(StringRef VTableName) {
0606     if (Error E = addSymbolName(VTableName))
0607       return E;
0608 
0609     // Record VTableName. InstrProfWriter uses this set. The comment around
0610     // class member explains why.
0611     VTableNames.insert(VTableName);
0612     return Error::success();
0613   }
0614 
0615   const StringSet<> &getVTableNames() const { return VTableNames; }
0616 
0617   /// Map a function address to its name's MD5 hash. This interface
0618   /// is only used by the raw profiler reader.
0619   void mapAddress(uint64_t Addr, uint64_t MD5Val) {
0620     AddrToMD5Map.push_back(std::make_pair(Addr, MD5Val));
0621   }
0622 
0623   /// Map the address range (i.e., [start_address, end_address)) of a variable
0624   /// to  its names' MD5 hash. This interface is only used by the raw profile
0625   /// reader.
0626   void mapVTableAddress(uint64_t StartAddr, uint64_t EndAddr, uint64_t MD5Val) {
0627     VTableAddrMap.insert(StartAddr, EndAddr, MD5Val);
0628   }
0629 
0630   /// Return a function's hash, or 0, if the function isn't in this SymTab.
0631   uint64_t getFunctionHashFromAddress(uint64_t Address);
0632 
0633   /// Return a vtable's hash, or 0 if the vtable doesn't exist in this SymTab.
0634   uint64_t getVTableHashFromAddress(uint64_t Address);
0635 
0636   /// Return function's PGO name from the function name's symbol
0637   /// address in the object file. If an error occurs, return
0638   /// an empty string.
0639   StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
0640 
0641   /// Return name of functions or global variables from the name's md5 hash
0642   /// value. If not found, return an empty string.
0643   inline StringRef getFuncOrVarName(uint64_t ValMD5Hash);
0644 
0645   /// Just like getFuncOrVarName, except that it will return literal string
0646   /// 'External Symbol' if the function or global variable is external to
0647   /// this symbol table.
0648   inline StringRef getFuncOrVarNameIfDefined(uint64_t ValMD5Hash);
0649 
0650   /// True if Symbol is the value used to represent external symbols.
0651   static bool isExternalSymbol(const StringRef &Symbol) {
0652     return Symbol == InstrProfSymtab::getExternalSymbol();
0653   }
0654 
0655   /// Return function from the name's md5 hash. Return nullptr if not found.
0656   inline Function *getFunction(uint64_t FuncMD5Hash);
0657 
0658   /// Return the global variable corresponding to md5 hash. Return nullptr if
0659   /// not found.
0660   inline GlobalVariable *getGlobalVariable(uint64_t MD5Hash);
0661 
0662   /// Return the name section data.
0663   inline StringRef getNameData() const { return Data; }
0664 
0665   /// Dump the symbols in this table.
0666   void dumpNames(raw_ostream &OS) const;
0667 };
0668 
0669 Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
0670   Data = D;
0671   Address = BaseAddr;
0672   return Error::success();
0673 }
0674 
0675 template <typename NameIterRange>
0676 Error InstrProfSymtab::create(const NameIterRange &IterRange) {
0677   for (auto Name : IterRange)
0678     if (Error E = addFuncName(Name))
0679       return E;
0680 
0681   finalizeSymtab();
0682   return Error::success();
0683 }
0684 
0685 template <typename FuncNameIterRange, typename VTableNameIterRange>
0686 Error InstrProfSymtab::create(const FuncNameIterRange &FuncIterRange,
0687                               const VTableNameIterRange &VTableIterRange) {
0688   // Iterate elements by StringRef rather than by const reference.
0689   // StringRef is small enough, so the loop is efficient whether
0690   // element in the range is std::string or StringRef.
0691   for (StringRef Name : FuncIterRange)
0692     if (Error E = addFuncName(Name))
0693       return E;
0694 
0695   for (StringRef VTableName : VTableIterRange)
0696     if (Error E = addVTableName(VTableName))
0697       return E;
0698 
0699   finalizeSymtab();
0700   return Error::success();
0701 }
0702 
0703 void InstrProfSymtab::finalizeSymtab() {
0704   if (Sorted)
0705     return;
0706   llvm::sort(MD5NameMap, less_first());
0707   llvm::sort(MD5FuncMap, less_first());
0708   llvm::sort(AddrToMD5Map, less_first());
0709   AddrToMD5Map.erase(llvm::unique(AddrToMD5Map), AddrToMD5Map.end());
0710   Sorted = true;
0711 }
0712 
0713 StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) {
0714   StringRef ret = getFuncOrVarName(MD5Hash);
0715   if (ret.empty())
0716     return InstrProfSymtab::getExternalSymbol();
0717   return ret;
0718 }
0719 
0720 StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
0721   finalizeSymtab();
0722   auto Result = llvm::lower_bound(MD5NameMap, MD5Hash,
0723                                   [](const std::pair<uint64_t, StringRef> &LHS,
0724                                      uint64_t RHS) { return LHS.first < RHS; });
0725   if (Result != MD5NameMap.end() && Result->first == MD5Hash)
0726     return Result->second;
0727   return StringRef();
0728 }
0729 
0730 Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
0731   finalizeSymtab();
0732   auto Result = llvm::lower_bound(MD5FuncMap, FuncMD5Hash,
0733                                   [](const std::pair<uint64_t, Function *> &LHS,
0734                                      uint64_t RHS) { return LHS.first < RHS; });
0735   if (Result != MD5FuncMap.end() && Result->first == FuncMD5Hash)
0736     return Result->second;
0737   return nullptr;
0738 }
0739 
0740 GlobalVariable *InstrProfSymtab::getGlobalVariable(uint64_t MD5Hash) {
0741   return MD5VTableMap.lookup(MD5Hash);
0742 }
0743 
0744 // To store the sums of profile count values, or the percentage of
0745 // the sums of the total count values.
0746 struct CountSumOrPercent {
0747   uint64_t NumEntries = 0;
0748   double CountSum = 0.0f;
0749   std::array<double, IPVK_Last - IPVK_First + 1> ValueCounts = {};
0750   CountSumOrPercent() = default;
0751   void reset() {
0752     NumEntries = 0;
0753     CountSum = 0.0f;
0754     ValueCounts.fill(0.0f);
0755   }
0756 };
0757 
0758 // Function level or program level overlap information.
0759 struct OverlapStats {
0760   enum OverlapStatsLevel { ProgramLevel, FunctionLevel };
0761   // Sum of the total count values for the base profile.
0762   CountSumOrPercent Base;
0763   // Sum of the total count values for the test profile.
0764   CountSumOrPercent Test;
0765   // Overlap lap score. Should be in range of [0.0f to 1.0f].
0766   CountSumOrPercent Overlap;
0767   CountSumOrPercent Mismatch;
0768   CountSumOrPercent Unique;
0769   OverlapStatsLevel Level;
0770   const std::string *BaseFilename = nullptr;
0771   const std::string *TestFilename = nullptr;
0772   StringRef FuncName;
0773   uint64_t FuncHash = 0;
0774   bool Valid = false;
0775 
0776   OverlapStats(OverlapStatsLevel L = ProgramLevel) : Level(L) {}
0777 
0778   void dump(raw_fd_ostream &OS) const;
0779 
0780   void setFuncInfo(StringRef Name, uint64_t Hash) {
0781     FuncName = Name;
0782     FuncHash = Hash;
0783   }
0784 
0785   Error accumulateCounts(const std::string &BaseFilename,
0786                          const std::string &TestFilename, bool IsCS);
0787   void addOneMismatch(const CountSumOrPercent &MismatchFunc);
0788   void addOneUnique(const CountSumOrPercent &UniqueFunc);
0789 
0790   static inline double score(uint64_t Val1, uint64_t Val2, double Sum1,
0791                              double Sum2) {
0792     if (Sum1 < 1.0f || Sum2 < 1.0f)
0793       return 0.0f;
0794     return std::min(Val1 / Sum1, Val2 / Sum2);
0795   }
0796 };
0797 
0798 // This is used to filter the functions whose overlap information
0799 // to be output.
0800 struct OverlapFuncFilters {
0801   uint64_t ValueCutoff;
0802   const std::string NameFilter;
0803 };
0804 
0805 struct InstrProfValueSiteRecord {
0806   /// Value profiling data pairs at a given value site.
0807   std::vector<InstrProfValueData> ValueData;
0808 
0809   InstrProfValueSiteRecord() = default;
0810   InstrProfValueSiteRecord(std::vector<InstrProfValueData> &&VD)
0811       : ValueData(VD) {}
0812 
0813   /// Sort ValueData ascending by Value
0814   void sortByTargetValues() {
0815     llvm::sort(ValueData,
0816                [](const InstrProfValueData &L, const InstrProfValueData &R) {
0817                  return L.Value < R.Value;
0818                });
0819   }
0820   /// Sort ValueData Descending by Count
0821   inline void sortByCount();
0822 
0823   /// Merge data from another InstrProfValueSiteRecord
0824   /// Optionally scale merged counts by \p Weight.
0825   void merge(InstrProfValueSiteRecord &Input, uint64_t Weight,
0826              function_ref<void(instrprof_error)> Warn);
0827   /// Scale up value profile data counts by N (Numerator) / D (Denominator).
0828   void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
0829 
0830   /// Compute the overlap b/w this record and Input record.
0831   void overlap(InstrProfValueSiteRecord &Input, uint32_t ValueKind,
0832                OverlapStats &Overlap, OverlapStats &FuncLevelOverlap);
0833 };
0834 
0835 /// Profiling information for a single function.
0836 struct InstrProfRecord {
0837   std::vector<uint64_t> Counts;
0838   std::vector<uint8_t> BitmapBytes;
0839 
0840   InstrProfRecord() = default;
0841   InstrProfRecord(std::vector<uint64_t> Counts) : Counts(std::move(Counts)) {}
0842   InstrProfRecord(std::vector<uint64_t> Counts,
0843                   std::vector<uint8_t> BitmapBytes)
0844       : Counts(std::move(Counts)), BitmapBytes(std::move(BitmapBytes)) {}
0845   InstrProfRecord(InstrProfRecord &&) = default;
0846   InstrProfRecord(const InstrProfRecord &RHS)
0847       : Counts(RHS.Counts), BitmapBytes(RHS.BitmapBytes),
0848         ValueData(RHS.ValueData
0849                       ? std::make_unique<ValueProfData>(*RHS.ValueData)
0850                       : nullptr) {}
0851   InstrProfRecord &operator=(InstrProfRecord &&) = default;
0852   InstrProfRecord &operator=(const InstrProfRecord &RHS) {
0853     Counts = RHS.Counts;
0854     BitmapBytes = RHS.BitmapBytes;
0855     if (!RHS.ValueData) {
0856       ValueData = nullptr;
0857       return *this;
0858     }
0859     if (!ValueData)
0860       ValueData = std::make_unique<ValueProfData>(*RHS.ValueData);
0861     else
0862       *ValueData = *RHS.ValueData;
0863     return *this;
0864   }
0865 
0866   /// Return the number of value profile kinds with non-zero number
0867   /// of profile sites.
0868   inline uint32_t getNumValueKinds() const;
0869   /// Return the number of instrumented sites for ValueKind.
0870   inline uint32_t getNumValueSites(uint32_t ValueKind) const;
0871 
0872   /// Return the total number of ValueData for ValueKind.
0873   inline uint32_t getNumValueData(uint32_t ValueKind) const;
0874 
0875   /// Return the array of profiled values at \p Site.
0876   inline ArrayRef<InstrProfValueData> getValueArrayForSite(uint32_t ValueKind,
0877                                                            uint32_t Site) const;
0878 
0879   /// Reserve space for NumValueSites sites.
0880   inline void reserveSites(uint32_t ValueKind, uint32_t NumValueSites);
0881 
0882   /// Add ValueData for ValueKind at value Site.  We do not support adding sites
0883   /// out of order.  Site must go up from 0 one by one.
0884   void addValueData(uint32_t ValueKind, uint32_t Site,
0885                     ArrayRef<InstrProfValueData> VData,
0886                     InstrProfSymtab *SymTab);
0887 
0888   /// Merge the counts in \p Other into this one.
0889   /// Optionally scale merged counts by \p Weight.
0890   void merge(InstrProfRecord &Other, uint64_t Weight,
0891              function_ref<void(instrprof_error)> Warn);
0892 
0893   /// Scale up profile counts (including value profile data) by
0894   /// a factor of (N / D).
0895   void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
0896 
0897   /// Sort value profile data (per site) by count.
0898   void sortValueData() {
0899     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
0900       for (auto &SR : getValueSitesForKind(Kind))
0901         SR.sortByCount();
0902   }
0903 
0904   /// Clear value data entries and edge counters.
0905   void Clear() {
0906     Counts.clear();
0907     clearValueData();
0908   }
0909 
0910   /// Clear value data entries
0911   void clearValueData() { ValueData = nullptr; }
0912 
0913   /// Compute the sums of all counts and store in Sum.
0914   void accumulateCounts(CountSumOrPercent &Sum) const;
0915 
0916   /// Compute the overlap b/w this IntrprofRecord and Other.
0917   void overlap(InstrProfRecord &Other, OverlapStats &Overlap,
0918                OverlapStats &FuncLevelOverlap, uint64_t ValueCutoff);
0919 
0920   /// Compute the overlap of value profile counts.
0921   void overlapValueProfData(uint32_t ValueKind, InstrProfRecord &Src,
0922                             OverlapStats &Overlap,
0923                             OverlapStats &FuncLevelOverlap);
0924 
0925   enum CountPseudoKind {
0926     NotPseudo = 0,
0927     PseudoHot,
0928     PseudoWarm,
0929   };
0930   enum PseudoCountVal {
0931     HotFunctionVal = -1,
0932     WarmFunctionVal = -2,
0933   };
0934   CountPseudoKind getCountPseudoKind() const {
0935     uint64_t FirstCount = Counts[0];
0936     if (FirstCount == (uint64_t)HotFunctionVal)
0937       return PseudoHot;
0938     if (FirstCount == (uint64_t)WarmFunctionVal)
0939       return PseudoWarm;
0940     return NotPseudo;
0941   }
0942   void setPseudoCount(CountPseudoKind Kind) {
0943     if (Kind == PseudoHot)
0944       Counts[0] = (uint64_t)HotFunctionVal;
0945     else if (Kind == PseudoWarm)
0946       Counts[0] = (uint64_t)WarmFunctionVal;
0947   }
0948 
0949 private:
0950   using ValueProfData = std::array<std::vector<InstrProfValueSiteRecord>,
0951                                    IPVK_Last - IPVK_First + 1>;
0952   std::unique_ptr<ValueProfData> ValueData;
0953 
0954   MutableArrayRef<InstrProfValueSiteRecord>
0955   getValueSitesForKind(uint32_t ValueKind) {
0956     // Cast to /add/ const (should be an implicit_cast, ideally, if that's ever
0957     // implemented in LLVM) to call the const overload of this function, then
0958     // cast away the constness from the result.
0959     auto AR = const_cast<const InstrProfRecord *>(this)->getValueSitesForKind(
0960         ValueKind);
0961     return MutableArrayRef(
0962         const_cast<InstrProfValueSiteRecord *>(AR.data()), AR.size());
0963   }
0964   ArrayRef<InstrProfValueSiteRecord>
0965   getValueSitesForKind(uint32_t ValueKind) const {
0966     if (!ValueData)
0967       return {};
0968     assert(IPVK_First <= ValueKind && ValueKind <= IPVK_Last &&
0969            "Unknown value kind!");
0970     return (*ValueData)[ValueKind - IPVK_First];
0971   }
0972 
0973   std::vector<InstrProfValueSiteRecord> &
0974   getOrCreateValueSitesForKind(uint32_t ValueKind) {
0975     if (!ValueData)
0976       ValueData = std::make_unique<ValueProfData>();
0977     assert(IPVK_First <= ValueKind && ValueKind <= IPVK_Last &&
0978            "Unknown value kind!");
0979     return (*ValueData)[ValueKind - IPVK_First];
0980   }
0981 
0982   // Map indirect call target name hash to name string.
0983   uint64_t remapValue(uint64_t Value, uint32_t ValueKind,
0984                       InstrProfSymtab *SymTab);
0985 
0986   // Merge Value Profile data from Src record to this record for ValueKind.
0987   // Scale merged value counts by \p Weight.
0988   void mergeValueProfData(uint32_t ValkeKind, InstrProfRecord &Src,
0989                           uint64_t Weight,
0990                           function_ref<void(instrprof_error)> Warn);
0991 
0992   // Scale up value profile data count by N (Numerator) / D (Denominator).
0993   void scaleValueProfData(uint32_t ValueKind, uint64_t N, uint64_t D,
0994                           function_ref<void(instrprof_error)> Warn);
0995 };
0996 
0997 struct NamedInstrProfRecord : InstrProfRecord {
0998   StringRef Name;
0999   uint64_t Hash;
1000 
1001   // We reserve this bit as the flag for context sensitive profile record.
1002   static const int CS_FLAG_IN_FUNC_HASH = 60;
1003 
1004   NamedInstrProfRecord() = default;
1005   NamedInstrProfRecord(StringRef Name, uint64_t Hash,
1006                        std::vector<uint64_t> Counts)
1007       : InstrProfRecord(std::move(Counts)), Name(Name), Hash(Hash) {}
1008   NamedInstrProfRecord(StringRef Name, uint64_t Hash,
1009                        std::vector<uint64_t> Counts,
1010                        std::vector<uint8_t> BitmapBytes)
1011       : InstrProfRecord(std::move(Counts), std::move(BitmapBytes)), Name(Name),
1012         Hash(Hash) {}
1013 
1014   static bool hasCSFlagInHash(uint64_t FuncHash) {
1015     return ((FuncHash >> CS_FLAG_IN_FUNC_HASH) & 1);
1016   }
1017   static void setCSFlagInHash(uint64_t &FuncHash) {
1018     FuncHash |= ((uint64_t)1 << CS_FLAG_IN_FUNC_HASH);
1019   }
1020 };
1021 
1022 uint32_t InstrProfRecord::getNumValueKinds() const {
1023   uint32_t NumValueKinds = 0;
1024   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1025     NumValueKinds += !(getValueSitesForKind(Kind).empty());
1026   return NumValueKinds;
1027 }
1028 
1029 uint32_t InstrProfRecord::getNumValueData(uint32_t ValueKind) const {
1030   uint32_t N = 0;
1031   for (const auto &SR : getValueSitesForKind(ValueKind))
1032     N += SR.ValueData.size();
1033   return N;
1034 }
1035 
1036 uint32_t InstrProfRecord::getNumValueSites(uint32_t ValueKind) const {
1037   return getValueSitesForKind(ValueKind).size();
1038 }
1039 
1040 ArrayRef<InstrProfValueData>
1041 InstrProfRecord::getValueArrayForSite(uint32_t ValueKind, uint32_t Site) const {
1042   return getValueSitesForKind(ValueKind)[Site].ValueData;
1043 }
1044 
1045 void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {
1046   if (!NumValueSites)
1047     return;
1048   getOrCreateValueSitesForKind(ValueKind).reserve(NumValueSites);
1049 }
1050 
1051 // Include definitions for value profile data
1052 #define INSTR_PROF_VALUE_PROF_DATA
1053 #include "llvm/ProfileData/InstrProfData.inc"
1054 
1055 void InstrProfValueSiteRecord::sortByCount() {
1056   llvm::stable_sort(
1057       ValueData, [](const InstrProfValueData &L, const InstrProfValueData &R) {
1058         return L.Count > R.Count;
1059       });
1060   // Now truncate
1061   size_t max_s = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
1062   if (ValueData.size() > max_s)
1063     ValueData.resize(max_s);
1064 }
1065 
1066 namespace IndexedInstrProf {
1067 
1068 enum class HashT : uint32_t {
1069   MD5,
1070   Last = MD5
1071 };
1072 
1073 inline uint64_t ComputeHash(HashT Type, StringRef K) {
1074   switch (Type) {
1075   case HashT::MD5:
1076     return MD5Hash(K);
1077   }
1078   llvm_unreachable("Unhandled hash type");
1079 }
1080 
1081 const uint64_t Magic = 0x8169666f72706cff; // "\xfflprofi\x81"
1082 
1083 enum ProfVersion {
1084   // Version 1 is the first version. In this version, the value of
1085   // a key/value pair can only include profile data of a single function.
1086   // Due to this restriction, the number of block counters for a given
1087   // function is not recorded but derived from the length of the value.
1088   Version1 = 1,
1089   // The version 2 format supports recording profile data of multiple
1090   // functions which share the same key in one value field. To support this,
1091   // the number block counters is recorded as an uint64_t field right after the
1092   // function structural hash.
1093   Version2 = 2,
1094   // Version 3 supports value profile data. The value profile data is expected
1095   // to follow the block counter profile data.
1096   Version3 = 3,
1097   // In this version, profile summary data \c IndexedInstrProf::Summary is
1098   // stored after the profile header.
1099   Version4 = 4,
1100   // In this version, the frontend PGO stable hash algorithm defaults to V2.
1101   Version5 = 5,
1102   // In this version, the frontend PGO stable hash algorithm got fixed and
1103   // may produce hashes different from Version5.
1104   Version6 = 6,
1105   // An additional counter is added around logical operators.
1106   Version7 = 7,
1107   // An additional (optional) memory profile type is added.
1108   Version8 = 8,
1109   // Binary ids are added.
1110   Version9 = 9,
1111   // An additional (optional) temporal profile traces section is added.
1112   Version10 = 10,
1113   // An additional field is used for bitmap bytes.
1114   Version11 = 11,
1115   // VTable profiling, decision record and bitmap are modified for mcdc.
1116   Version12 = 12,
1117   // The current version is 12.
1118   CurrentVersion = INSTR_PROF_INDEX_VERSION
1119 };
1120 const uint64_t Version = ProfVersion::CurrentVersion;
1121 
1122 const HashT HashType = HashT::MD5;
1123 
1124 inline uint64_t ComputeHash(StringRef K) { return ComputeHash(HashType, K); }
1125 
1126 // This structure defines the file header of the LLVM profile
1127 // data file in indexed-format. Please update llvm/docs/InstrProfileFormat.rst
1128 // as appropriate when updating the indexed profile format.
1129 struct Header {
1130   uint64_t Magic = IndexedInstrProf::Magic;
1131   // The lower 32 bits specify the version of the indexed profile.
1132   // The most significant 32 bits are reserved to specify the variant types of
1133   // the profile.
1134   uint64_t Version = 0;
1135   uint64_t Unused = 0; // Becomes unused since version 4
1136   uint64_t HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
1137   // This field records the offset of this hash table's metadata (i.e., the
1138   // number of buckets and entries), which follows right after the payload of
1139   // the entire hash table.
1140   uint64_t HashOffset = 0;
1141   uint64_t MemProfOffset = 0;
1142   uint64_t BinaryIdOffset = 0;
1143   uint64_t TemporalProfTracesOffset = 0;
1144   uint64_t VTableNamesOffset = 0;
1145   // New fields should only be added at the end to ensure that the size
1146   // computation is correct. The methods below need to be updated to ensure that
1147   // the new field is read correctly.
1148 
1149   // Reads a header struct from the buffer. Header fields are in machine native
1150   // endianness.
1151   static Expected<Header> readFromBuffer(const unsigned char *Buffer);
1152 
1153   // Returns the size of the header in bytes for all valid fields based on the
1154   // version. I.e a older version header will return a smaller size.
1155   size_t size() const;
1156 
1157   // Return the indexed profile version, i.e., the least significant 32 bits
1158   // in Header.Version.
1159   uint64_t getIndexedProfileVersion() const;
1160 };
1161 
1162 // Profile summary data recorded in the profile data file in indexed
1163 // format. It is introduced in version 4. The summary data follows
1164 // right after the profile file header.
1165 struct Summary {
1166   struct Entry {
1167     uint64_t Cutoff; ///< The required percentile of total execution count.
1168     uint64_t
1169         MinBlockCount;  ///< The minimum execution count for this percentile.
1170     uint64_t NumBlocks; ///< Number of blocks >= the minumum execution count.
1171   };
1172   // The field kind enumerator to assigned value mapping should remain
1173   // unchanged  when a new kind is added or an old kind gets deleted in
1174   // the future.
1175   enum SummaryFieldKind {
1176     /// The total number of functions instrumented.
1177     TotalNumFunctions = 0,
1178     /// Total number of instrumented blocks/edges.
1179     TotalNumBlocks = 1,
1180     /// The maximal execution count among all functions.
1181     /// This field does not exist for profile data from IR based
1182     /// instrumentation.
1183     MaxFunctionCount = 2,
1184     /// Max block count of the program.
1185     MaxBlockCount = 3,
1186     /// Max internal block count of the program (excluding entry blocks).
1187     MaxInternalBlockCount = 4,
1188     /// The sum of all instrumented block counts.
1189     TotalBlockCount = 5,
1190     NumKinds = TotalBlockCount + 1
1191   };
1192 
1193   // The number of summmary fields following the summary header.
1194   uint64_t NumSummaryFields;
1195   // The number of Cutoff Entries (Summary::Entry) following summary fields.
1196   uint64_t NumCutoffEntries;
1197 
1198   Summary() = delete;
1199   Summary(uint32_t Size) { memset(this, 0, Size); }
1200 
1201   void operator delete(void *ptr) { ::operator delete(ptr); }
1202 
1203   static uint32_t getSize(uint32_t NumSumFields, uint32_t NumCutoffEntries) {
1204     return sizeof(Summary) + NumCutoffEntries * sizeof(Entry) +
1205            NumSumFields * sizeof(uint64_t);
1206   }
1207 
1208   const uint64_t *getSummaryDataBase() const {
1209     return reinterpret_cast<const uint64_t *>(this + 1);
1210   }
1211 
1212   uint64_t *getSummaryDataBase() {
1213     return reinterpret_cast<uint64_t *>(this + 1);
1214   }
1215 
1216   const Entry *getCutoffEntryBase() const {
1217     return reinterpret_cast<const Entry *>(
1218         &getSummaryDataBase()[NumSummaryFields]);
1219   }
1220 
1221   Entry *getCutoffEntryBase() {
1222     return reinterpret_cast<Entry *>(&getSummaryDataBase()[NumSummaryFields]);
1223   }
1224 
1225   uint64_t get(SummaryFieldKind K) const {
1226     return getSummaryDataBase()[K];
1227   }
1228 
1229   void set(SummaryFieldKind K, uint64_t V) {
1230     getSummaryDataBase()[K] = V;
1231   }
1232 
1233   const Entry &getEntry(uint32_t I) const { return getCutoffEntryBase()[I]; }
1234 
1235   void setEntry(uint32_t I, const ProfileSummaryEntry &E) {
1236     Entry &ER = getCutoffEntryBase()[I];
1237     ER.Cutoff = E.Cutoff;
1238     ER.MinBlockCount = E.MinCount;
1239     ER.NumBlocks = E.NumCounts;
1240   }
1241 };
1242 
1243 inline std::unique_ptr<Summary> allocSummary(uint32_t TotalSize) {
1244   return std::unique_ptr<Summary>(new (::operator new(TotalSize))
1245                                       Summary(TotalSize));
1246 }
1247 
1248 } // end namespace IndexedInstrProf
1249 
1250 namespace RawInstrProf {
1251 
1252 // Version 1: First version
1253 // Version 2: Added value profile data section. Per-function control data
1254 // struct has more fields to describe value profile information.
1255 // Version 3: Compressed name section support. Function PGO name reference
1256 // from control data struct is changed from raw pointer to Name's MD5 value.
1257 // Version 4: ValueDataBegin and ValueDataSizes fields are removed from the
1258 // raw header.
1259 // Version 5: Bit 60 of FuncHash is reserved for the flag for the context
1260 // sensitive records.
1261 // Version 6: Added binary id.
1262 // Version 7: Reorder binary id and include version in signature.
1263 // Version 8: Use relative counter pointer.
1264 // Version 9: Added relative bitmap bytes pointer and count used by MC/DC.
1265 // Version 10: Added vtable, a new type of value profile data.
1266 const uint64_t Version = INSTR_PROF_RAW_VERSION;
1267 
1268 template <class IntPtrT> inline uint64_t getMagic();
1269 template <> inline uint64_t getMagic<uint64_t>() {
1270   return INSTR_PROF_RAW_MAGIC_64;
1271 }
1272 
1273 template <> inline uint64_t getMagic<uint32_t>() {
1274   return INSTR_PROF_RAW_MAGIC_32;
1275 }
1276 
1277 // Per-function profile data header/control structure.
1278 // The definition should match the structure defined in
1279 // compiler-rt/lib/profile/InstrProfiling.h.
1280 // It should also match the synthesized type in
1281 // Transforms/Instrumentation/InstrProfiling.cpp:getOrCreateRegionCounters.
1282 template <class IntPtrT> struct alignas(8) ProfileData {
1283 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Type Name;
1284 #include "llvm/ProfileData/InstrProfData.inc"
1285 };
1286 
1287 template <class IntPtrT> struct alignas(8) VTableProfileData {
1288 #define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) Type Name;
1289 #include "llvm/ProfileData/InstrProfData.inc"
1290 };
1291 
1292 // File header structure of the LLVM profile data in raw format.
1293 // The definition should match the header referenced in
1294 // compiler-rt/lib/profile/InstrProfilingFile.c  and
1295 // InstrProfilingBuffer.c.
1296 struct Header {
1297 #define INSTR_PROF_RAW_HEADER(Type, Name, Init) const Type Name;
1298 #include "llvm/ProfileData/InstrProfData.inc"
1299 };
1300 
1301 } // end namespace RawInstrProf
1302 
1303 // Create the variable for the profile file name.
1304 void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput);
1305 
1306 // Whether to compress function names in profile records, and filenames in
1307 // code coverage mappings. Used by the Instrumentation library and unit tests.
1308 extern cl::opt<bool> DoInstrProfNameCompression;
1309 
1310 } // end namespace llvm
1311 #endif // LLVM_PROFILEDATA_INSTRPROF_H