InstrProfWriter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains support for writing profiling data for instrumentation
  15. // based PGO and coverage.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
  19. #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/MapVector.h"
  22. #include "llvm/ADT/StringMap.h"
  23. #include "llvm/IR/GlobalValue.h"
  24. #include "llvm/Object/BuildID.h"
  25. #include "llvm/ProfileData/InstrProf.h"
  26. #include "llvm/ProfileData/MemProf.h"
  27. #include "llvm/Support/Endian.h"
  28. #include "llvm/Support/Error.h"
  29. #include <cstdint>
  30. #include <memory>
  31. namespace llvm {
  32. /// Writer for instrumentation based profile data.
  33. class InstrProfRecordWriterTrait;
  34. class ProfOStream;
  35. class MemoryBuffer;
  36. class raw_fd_ostream;
  37. class InstrProfWriter {
  38. public:
  39. using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
  40. private:
  41. bool Sparse;
  42. StringMap<ProfilingData> FunctionData;
  43. // A map to hold memprof data per function. The lower 64 bits obtained from
  44. // the md5 hash of the function name is used to index into the map.
  45. llvm::MapVector<GlobalValue::GUID, memprof::IndexedMemProfRecord>
  46. MemProfRecordData;
  47. // A map to hold frame id to frame mappings. The mappings are used to
  48. // convert IndexedMemProfRecord to MemProfRecords with frame information
  49. // inline.
  50. llvm::MapVector<memprof::FrameId, memprof::Frame> MemProfFrameData;
  51. // List of binary ids.
  52. std::vector<llvm::object::BuildID> BinaryIds;
  53. // An enum describing the attributes of the profile.
  54. InstrProfKind ProfileKind = InstrProfKind::Unknown;
  55. // Use raw pointer here for the incomplete type object.
  56. InstrProfRecordWriterTrait *InfoObj;
  57. public:
  58. InstrProfWriter(bool Sparse = false);
  59. ~InstrProfWriter();
  60. StringMap<ProfilingData> &getProfileData() { return FunctionData; }
  61. /// Add function counts for the given function. If there are already counts
  62. /// for this function and the hash and number of counts match, each counter is
  63. /// summed. Optionally scale counts by \p Weight.
  64. void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
  65. function_ref<void(Error)> Warn);
  66. void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
  67. addRecord(std::move(I), 1, Warn);
  68. }
  69. /// Add a memprof record for a function identified by its \p Id.
  70. void addMemProfRecord(const GlobalValue::GUID Id,
  71. const memprof::IndexedMemProfRecord &Record);
  72. /// Add a memprof frame identified by the hash of the contents of the frame in
  73. /// \p FrameId.
  74. bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F,
  75. function_ref<void(Error)> Warn);
  76. // Add a binary id to the binary ids list.
  77. void addBinaryIds(ArrayRef<llvm::object::BuildID> BIs);
  78. /// Merge existing function counts from the given writer.
  79. void mergeRecordsFromWriter(InstrProfWriter &&IPW,
  80. function_ref<void(Error)> Warn);
  81. /// Write the profile to \c OS
  82. Error write(raw_fd_ostream &OS);
  83. /// Write the profile in text format to \c OS
  84. Error writeText(raw_fd_ostream &OS);
  85. Error validateRecord(const InstrProfRecord &Func);
  86. /// Write \c Record in text format to \c OS
  87. static void writeRecordInText(StringRef Name, uint64_t Hash,
  88. const InstrProfRecord &Counters,
  89. InstrProfSymtab &Symtab, raw_fd_ostream &OS);
  90. /// Write the profile, returning the raw data. For testing.
  91. std::unique_ptr<MemoryBuffer> writeBuffer();
  92. /// Update the attributes of the current profile from the attributes
  93. /// specified. An error is returned if IR and FE profiles are mixed.
  94. Error mergeProfileKind(const InstrProfKind Other) {
  95. // If the kind is unset, this is the first profile we are merging so just
  96. // set it to the given type.
  97. if (ProfileKind == InstrProfKind::Unknown) {
  98. ProfileKind = Other;
  99. return Error::success();
  100. }
  101. // Returns true if merging is should fail assuming A and B are incompatible.
  102. auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
  103. return (static_cast<bool>(ProfileKind & A) &&
  104. static_cast<bool>(Other & B)) ||
  105. (static_cast<bool>(ProfileKind & B) &&
  106. static_cast<bool>(Other & A));
  107. };
  108. // Check if the profiles are in-compatible. Clang frontend profiles can't be
  109. // merged with other profile types.
  110. if (static_cast<bool>(
  111. (ProfileKind & InstrProfKind::FrontendInstrumentation) ^
  112. (Other & InstrProfKind::FrontendInstrumentation))) {
  113. return make_error<InstrProfError>(instrprof_error::unsupported_version);
  114. }
  115. if (testIncompatible(InstrProfKind::FunctionEntryOnly,
  116. InstrProfKind::FunctionEntryInstrumentation)) {
  117. return make_error<InstrProfError>(
  118. instrprof_error::unsupported_version,
  119. "cannot merge FunctionEntryOnly profiles and BB profiles together");
  120. }
  121. // Now we update the profile type with the bits that are set.
  122. ProfileKind |= Other;
  123. return Error::success();
  124. }
  125. InstrProfKind getProfileKind() const { return ProfileKind; }
  126. // Internal interface for testing purpose only.
  127. void setValueProfDataEndianness(support::endianness Endianness);
  128. void setOutputSparse(bool Sparse);
  129. // Compute the overlap b/w this object and Other. Program level result is
  130. // stored in Overlap and function level result is stored in FuncLevelOverlap.
  131. void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
  132. OverlapStats &FuncLevelOverlap,
  133. const OverlapFuncFilters &FuncFilter);
  134. private:
  135. void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
  136. uint64_t Weight, function_ref<void(Error)> Warn);
  137. bool shouldEncodeData(const ProfilingData &PD);
  138. Error writeImpl(ProfOStream &OS);
  139. };
  140. } // end namespace llvm
  141. #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
  142. #ifdef __GNUC__
  143. #pragma GCC diagnostic pop
  144. #endif