InstrProfWriter.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/StringMap.h"
  22. #include "llvm/ProfileData/InstrProf.h"
  23. #include "llvm/Support/Endian.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include <cstdint>
  27. #include <memory>
  28. namespace llvm {
  29. /// Writer for instrumentation based profile data.
  30. class InstrProfRecordWriterTrait;
  31. class ProfOStream;
  32. class raw_fd_ostream;
  33. class InstrProfWriter {
  34. public:
  35. using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
  36. // PF_IRLevelWithCS is the profile from context sensitive IR instrumentation.
  37. enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel, PF_IRLevelWithCS };
  38. private:
  39. bool Sparse;
  40. StringMap<ProfilingData> FunctionData;
  41. ProfKind ProfileKind = PF_Unknown;
  42. bool InstrEntryBBEnabled;
  43. // Use raw pointer here for the incomplete type object.
  44. InstrProfRecordWriterTrait *InfoObj;
  45. public:
  46. InstrProfWriter(bool Sparse = false, bool InstrEntryBBEnabled = false);
  47. ~InstrProfWriter();
  48. StringMap<ProfilingData> &getProfileData() { return FunctionData; }
  49. /// Add function counts for the given function. If there are already counts
  50. /// for this function and the hash and number of counts match, each counter is
  51. /// summed. Optionally scale counts by \p Weight.
  52. void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
  53. function_ref<void(Error)> Warn);
  54. void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
  55. addRecord(std::move(I), 1, Warn);
  56. }
  57. /// Merge existing function counts from the given writer.
  58. void mergeRecordsFromWriter(InstrProfWriter &&IPW,
  59. function_ref<void(Error)> Warn);
  60. /// Write the profile to \c OS
  61. void write(raw_fd_ostream &OS);
  62. /// Write the profile in text format to \c OS
  63. Error writeText(raw_fd_ostream &OS);
  64. /// Write \c Record in text format to \c OS
  65. static void writeRecordInText(StringRef Name, uint64_t Hash,
  66. const InstrProfRecord &Counters,
  67. InstrProfSymtab &Symtab, raw_fd_ostream &OS);
  68. /// Write the profile, returning the raw data. For testing.
  69. std::unique_ptr<MemoryBuffer> writeBuffer();
  70. /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
  71. /// \c WithCS indicates if this is for contenxt sensitive instrumentation.
  72. Error setIsIRLevelProfile(bool IsIRLevel, bool WithCS) {
  73. if (ProfileKind == PF_Unknown) {
  74. if (IsIRLevel)
  75. ProfileKind = WithCS ? PF_IRLevelWithCS : PF_IRLevel;
  76. else
  77. ProfileKind = PF_FE;
  78. return Error::success();
  79. }
  80. if (((ProfileKind != PF_FE) && !IsIRLevel) ||
  81. ((ProfileKind == PF_FE) && IsIRLevel))
  82. return make_error<InstrProfError>(instrprof_error::unsupported_version);
  83. // When merging a context-sensitive profile (WithCS == true) with an IRLevel
  84. // profile, set the kind to PF_IRLevelWithCS.
  85. if (ProfileKind == PF_IRLevel && WithCS)
  86. ProfileKind = PF_IRLevelWithCS;
  87. return Error::success();
  88. }
  89. void setInstrEntryBBEnabled(bool Enabled) { InstrEntryBBEnabled = Enabled; }
  90. // Internal interface for testing purpose only.
  91. void setValueProfDataEndianness(support::endianness Endianness);
  92. void setOutputSparse(bool Sparse);
  93. // Compute the overlap b/w this object and Other. Program level result is
  94. // stored in Overlap and function level result is stored in FuncLevelOverlap.
  95. void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
  96. OverlapStats &FuncLevelOverlap,
  97. const OverlapFuncFilters &FuncFilter);
  98. private:
  99. void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
  100. uint64_t Weight, function_ref<void(Error)> Warn);
  101. bool shouldEncodeData(const ProfilingData &PD);
  102. void writeImpl(ProfOStream &OS);
  103. };
  104. } // end namespace llvm
  105. #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif