InstrProfWriter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains support for writing profiling data for instrumentation
  10. // based PGO and coverage.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
  14. #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/ProfileData/InstrProf.h"
  18. #include "llvm/Support/Endian.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include <cstdint>
  22. #include <memory>
  23. namespace llvm {
  24. /// Writer for instrumentation based profile data.
  25. class InstrProfRecordWriterTrait;
  26. class ProfOStream;
  27. class raw_fd_ostream;
  28. class InstrProfWriter {
  29. public:
  30. using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
  31. // PF_IRLevelWithCS is the profile from context sensitive IR instrumentation.
  32. enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel, PF_IRLevelWithCS };
  33. private:
  34. bool Sparse;
  35. StringMap<ProfilingData> FunctionData;
  36. ProfKind ProfileKind = PF_Unknown;
  37. bool InstrEntryBBEnabled;
  38. // Use raw pointer here for the incomplete type object.
  39. InstrProfRecordWriterTrait *InfoObj;
  40. public:
  41. InstrProfWriter(bool Sparse = false, bool InstrEntryBBEnabled = false);
  42. ~InstrProfWriter();
  43. StringMap<ProfilingData> &getProfileData() { return FunctionData; }
  44. /// Add function counts for the given function. If there are already counts
  45. /// for this function and the hash and number of counts match, each counter is
  46. /// summed. Optionally scale counts by \p Weight.
  47. void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
  48. function_ref<void(Error)> Warn);
  49. void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
  50. addRecord(std::move(I), 1, Warn);
  51. }
  52. /// Merge existing function counts from the given writer.
  53. void mergeRecordsFromWriter(InstrProfWriter &&IPW,
  54. function_ref<void(Error)> Warn);
  55. /// Write the profile to \c OS
  56. Error write(raw_fd_ostream &OS);
  57. /// Write the profile in text format to \c OS
  58. Error writeText(raw_fd_ostream &OS);
  59. Error validateRecord(const InstrProfRecord &Func);
  60. /// Write \c Record in text format to \c OS
  61. static void writeRecordInText(StringRef Name, uint64_t Hash,
  62. const InstrProfRecord &Counters,
  63. InstrProfSymtab &Symtab, raw_fd_ostream &OS);
  64. /// Write the profile, returning the raw data. For testing.
  65. std::unique_ptr<MemoryBuffer> writeBuffer();
  66. /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
  67. /// \c WithCS indicates if this is for contenxt sensitive instrumentation.
  68. Error setIsIRLevelProfile(bool IsIRLevel, bool WithCS) {
  69. if (ProfileKind == PF_Unknown) {
  70. if (IsIRLevel)
  71. ProfileKind = WithCS ? PF_IRLevelWithCS : PF_IRLevel;
  72. else
  73. ProfileKind = PF_FE;
  74. return Error::success();
  75. }
  76. if (((ProfileKind != PF_FE) && !IsIRLevel) ||
  77. ((ProfileKind == PF_FE) && IsIRLevel))
  78. return make_error<InstrProfError>(instrprof_error::unsupported_version);
  79. // When merging a context-sensitive profile (WithCS == true) with an IRLevel
  80. // profile, set the kind to PF_IRLevelWithCS.
  81. if (ProfileKind == PF_IRLevel && WithCS)
  82. ProfileKind = PF_IRLevelWithCS;
  83. return Error::success();
  84. }
  85. void setInstrEntryBBEnabled(bool Enabled) { InstrEntryBBEnabled = Enabled; }
  86. // Internal interface for testing purpose only.
  87. void setValueProfDataEndianness(support::endianness Endianness);
  88. void setOutputSparse(bool Sparse);
  89. // Compute the overlap b/w this object and Other. Program level result is
  90. // stored in Overlap and function level result is stored in FuncLevelOverlap.
  91. void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
  92. OverlapStats &FuncLevelOverlap,
  93. const OverlapFuncFilters &FuncFilter);
  94. private:
  95. void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
  96. uint64_t Weight, function_ref<void(Error)> Warn);
  97. bool shouldEncodeData(const ProfilingData &PD);
  98. Error writeImpl(ProfOStream &OS);
  99. };
  100. } // end namespace llvm
  101. #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H