Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- InstrProfWriter.h - Instrumented profiling writer --------*- 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 // This file contains support for writing profiling data for instrumentation
0010 // based PGO and coverage.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
0015 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
0016 
0017 #include "llvm/ADT/DenseMap.h"
0018 #include "llvm/ADT/MapVector.h"
0019 #include "llvm/ADT/StringMap.h"
0020 #include "llvm/IR/GlobalValue.h"
0021 #include "llvm/Object/BuildID.h"
0022 #include "llvm/ProfileData/InstrProf.h"
0023 #include "llvm/ProfileData/MemProf.h"
0024 #include "llvm/Support/Error.h"
0025 #include <cstdint>
0026 #include <memory>
0027 #include <random>
0028 
0029 namespace llvm {
0030 
0031 /// Writer for instrumentation based profile data.
0032 class InstrProfRecordWriterTrait;
0033 class ProfOStream;
0034 class MemoryBuffer;
0035 class raw_fd_ostream;
0036 
0037 class InstrProfWriter {
0038 public:
0039   using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
0040 
0041 private:
0042   bool Sparse;
0043   StringMap<ProfilingData> FunctionData;
0044   /// The maximum length of a single temporal profile trace.
0045   uint64_t MaxTemporalProfTraceLength;
0046   /// The maximum number of stored temporal profile traces.
0047   uint64_t TemporalProfTraceReservoirSize;
0048   /// The total number of temporal profile traces seen.
0049   uint64_t TemporalProfTraceStreamSize = 0;
0050   /// The list of temporal profile traces.
0051   SmallVector<TemporalProfTraceTy> TemporalProfTraces;
0052   std::mt19937 RNG;
0053 
0054   // The MemProf data.
0055   memprof::IndexedMemProfData MemProfData;
0056 
0057   // List of binary ids.
0058   std::vector<llvm::object::BuildID> BinaryIds;
0059 
0060   // Read the vtable names from raw instr profile reader.
0061   StringSet<> VTableNames;
0062 
0063   // An enum describing the attributes of the profile.
0064   InstrProfKind ProfileKind = InstrProfKind::Unknown;
0065   // Use raw pointer here for the incomplete type object.
0066   InstrProfRecordWriterTrait *InfoObj;
0067 
0068   // Temporary support for writing the previous version of the format, to enable
0069   // some forward compatibility. Currently this suppresses the writing of the
0070   // new vtable names section and header fields.
0071   // TODO: Consider enabling this with future version changes as well, to ease
0072   // deployment of newer versions of llvm-profdata.
0073   bool WritePrevVersion = false;
0074 
0075   // The MemProf version we should write.
0076   memprof::IndexedVersion MemProfVersionRequested;
0077 
0078   // Whether to serialize the full schema.
0079   bool MemProfFullSchema;
0080 
0081   // Whether to generated random memprof hotness for testing.
0082   bool MemprofGenerateRandomHotness;
0083 
0084 public:
0085   // For memprof testing, random hotness can be assigned to the contexts if
0086   // MemprofGenerateRandomHotness is enabled. The random seed can be either
0087   // provided by MemprofGenerateRandomHotnessSeed, or if that is 0, one will be
0088   // generated in the writer using the current time.
0089   InstrProfWriter(bool Sparse = false,
0090                   uint64_t TemporalProfTraceReservoirSize = 0,
0091                   uint64_t MaxTemporalProfTraceLength = 0,
0092                   bool WritePrevVersion = false,
0093                   memprof::IndexedVersion MemProfVersionRequested =
0094                       static_cast<memprof::IndexedVersion>(
0095                           memprof::MinimumSupportedVersion),
0096                   bool MemProfFullSchema = false,
0097                   bool MemprofGenerateRandomHotness = false,
0098                   unsigned MemprofGenerateRandomHotnessSeed = 0);
0099   ~InstrProfWriter();
0100 
0101   StringMap<ProfilingData> &getProfileData() { return FunctionData; }
0102 
0103   /// Add function counts for the given function. If there are already counts
0104   /// for this function and the hash and number of counts match, each counter is
0105   /// summed. Optionally scale counts by \p Weight.
0106   void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
0107                  function_ref<void(Error)> Warn);
0108   void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
0109     addRecord(std::move(I), 1, Warn);
0110   }
0111   void addVTableName(StringRef VTableName) { VTableNames.insert(VTableName); }
0112 
0113   /// Add \p SrcTraces using reservoir sampling where \p SrcStreamSize is the
0114   /// total number of temporal profiling traces the source has seen.
0115   void addTemporalProfileTraces(SmallVectorImpl<TemporalProfTraceTy> &SrcTraces,
0116                                 uint64_t SrcStreamSize);
0117 
0118   /// Add the entire MemProfData \p Incoming to the writer context.
0119   bool addMemProfData(memprof::IndexedMemProfData Incoming,
0120                       function_ref<void(Error)> Warn);
0121 
0122   // Add a binary id to the binary ids list.
0123   void addBinaryIds(ArrayRef<llvm::object::BuildID> BIs);
0124 
0125   /// Merge existing function counts from the given writer.
0126   void mergeRecordsFromWriter(InstrProfWriter &&IPW,
0127                               function_ref<void(Error)> Warn);
0128 
0129   /// Write the profile to \c OS
0130   Error write(raw_fd_ostream &OS);
0131 
0132   /// Write the profile to a string output stream \c OS
0133   Error write(raw_string_ostream &OS);
0134 
0135   /// Write the profile in text format to \c OS
0136   Error writeText(raw_fd_ostream &OS);
0137 
0138   /// Write temporal profile trace data to the header in text format to \c OS
0139   void writeTextTemporalProfTraceData(raw_fd_ostream &OS,
0140                                       InstrProfSymtab &Symtab);
0141 
0142   Error validateRecord(const InstrProfRecord &Func);
0143 
0144   /// Write \c Record in text format to \c OS
0145   static void writeRecordInText(StringRef Name, uint64_t Hash,
0146                                 const InstrProfRecord &Counters,
0147                                 InstrProfSymtab &Symtab, raw_fd_ostream &OS);
0148 
0149   /// Write the profile, returning the raw data. For testing.
0150   std::unique_ptr<MemoryBuffer> writeBuffer();
0151 
0152   /// Update the attributes of the current profile from the attributes
0153   /// specified. An error is returned if IR and FE profiles are mixed.
0154   Error mergeProfileKind(const InstrProfKind Other) {
0155     // If the kind is unset, this is the first profile we are merging so just
0156     // set it to the given type.
0157     if (ProfileKind == InstrProfKind::Unknown) {
0158       ProfileKind = Other;
0159       return Error::success();
0160     }
0161 
0162     // Returns true if merging is should fail assuming A and B are incompatible.
0163     auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
0164       return (static_cast<bool>(ProfileKind & A) &&
0165               static_cast<bool>(Other & B)) ||
0166              (static_cast<bool>(ProfileKind & B) &&
0167               static_cast<bool>(Other & A));
0168     };
0169 
0170     // Check if the profiles are in-compatible. Clang frontend profiles can't be
0171     // merged with other profile types.
0172     if (static_cast<bool>(
0173             (ProfileKind & InstrProfKind::FrontendInstrumentation) ^
0174             (Other & InstrProfKind::FrontendInstrumentation))) {
0175       return make_error<InstrProfError>(instrprof_error::unsupported_version);
0176     }
0177     if (testIncompatible(InstrProfKind::FunctionEntryOnly,
0178                          InstrProfKind::FunctionEntryInstrumentation) ||
0179         testIncompatible(InstrProfKind::FunctionEntryOnly,
0180                          InstrProfKind::LoopEntriesInstrumentation)) {
0181       return make_error<InstrProfError>(
0182           instrprof_error::unsupported_version,
0183           "cannot merge FunctionEntryOnly profiles and BB profiles together");
0184     }
0185 
0186     // Now we update the profile type with the bits that are set.
0187     ProfileKind |= Other;
0188     return Error::success();
0189   }
0190 
0191   InstrProfKind getProfileKind() const { return ProfileKind; }
0192 
0193   bool hasSingleByteCoverage() const {
0194     return static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage);
0195   }
0196 
0197   // Internal interfaces for testing purpose only.
0198   void setValueProfDataEndianness(llvm::endianness Endianness);
0199   void setOutputSparse(bool Sparse);
0200   void setMemProfVersionRequested(memprof::IndexedVersion Version) {
0201     MemProfVersionRequested = Version;
0202   }
0203   void setMemProfFullSchema(bool Full) { MemProfFullSchema = Full; }
0204   // Compute the overlap b/w this object and Other. Program level result is
0205   // stored in Overlap and function level result is stored in FuncLevelOverlap.
0206   void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
0207                      OverlapStats &FuncLevelOverlap,
0208                      const OverlapFuncFilters &FuncFilter);
0209 
0210 private:
0211   void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
0212                  uint64_t Weight, function_ref<void(Error)> Warn);
0213   bool shouldEncodeData(const ProfilingData &PD);
0214   /// Add \p Trace using reservoir sampling.
0215   void addTemporalProfileTrace(TemporalProfTraceTy Trace);
0216 
0217   /// Add a memprof record for a function identified by its \p Id.
0218   void addMemProfRecord(const GlobalValue::GUID Id,
0219                         const memprof::IndexedMemProfRecord &Record);
0220 
0221   /// Add a memprof frame identified by the hash of the contents of the frame in
0222   /// \p FrameId.
0223   bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F,
0224                        function_ref<void(Error)> Warn);
0225 
0226   /// Add a call stack identified by the hash of the contents of the call stack
0227   /// in \p CallStack.
0228   bool addMemProfCallStack(const memprof::CallStackId CSId,
0229                            const llvm::SmallVector<memprof::FrameId> &CallStack,
0230                            function_ref<void(Error)> Warn);
0231 
0232   Error writeImpl(ProfOStream &OS);
0233 
0234   // Writes known header fields and reserves space for fields whose value are
0235   // known only after payloads are written. Returns the start byte offset for
0236   // back patching.
0237   uint64_t writeHeader(const IndexedInstrProf::Header &header,
0238                        const bool WritePrevVersion, ProfOStream &OS);
0239 
0240   // Writes binary IDs.
0241   Error writeBinaryIds(ProfOStream &OS);
0242 
0243   // Writes compressed vtable names to profiles.
0244   Error writeVTableNames(ProfOStream &OS);
0245 };
0246 
0247 } // end namespace llvm
0248 
0249 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H