InstrProfWriter.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. private:
  37. bool Sparse;
  38. StringMap<ProfilingData> FunctionData;
  39. // An enum describing the attributes of the profile.
  40. InstrProfKind ProfileKind = InstrProfKind::Unknown;
  41. // Use raw pointer here for the incomplete type object.
  42. InstrProfRecordWriterTrait *InfoObj;
  43. public:
  44. InstrProfWriter(bool Sparse = false);
  45. ~InstrProfWriter();
  46. StringMap<ProfilingData> &getProfileData() { return FunctionData; }
  47. /// Add function counts for the given function. If there are already counts
  48. /// for this function and the hash and number of counts match, each counter is
  49. /// summed. Optionally scale counts by \p Weight.
  50. void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
  51. function_ref<void(Error)> Warn);
  52. void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
  53. addRecord(std::move(I), 1, Warn);
  54. }
  55. /// Merge existing function counts from the given writer.
  56. void mergeRecordsFromWriter(InstrProfWriter &&IPW,
  57. function_ref<void(Error)> Warn);
  58. /// Write the profile to \c OS
  59. Error write(raw_fd_ostream &OS);
  60. /// Write the profile in text format to \c OS
  61. Error writeText(raw_fd_ostream &OS);
  62. Error validateRecord(const InstrProfRecord &Func);
  63. /// Write \c Record in text format to \c OS
  64. static void writeRecordInText(StringRef Name, uint64_t Hash,
  65. const InstrProfRecord &Counters,
  66. InstrProfSymtab &Symtab, raw_fd_ostream &OS);
  67. /// Write the profile, returning the raw data. For testing.
  68. std::unique_ptr<MemoryBuffer> writeBuffer();
  69. /// Update the attributes of the current profile from the attributes
  70. /// specified. An error is returned if IR and FE profiles are mixed.
  71. Error mergeProfileKind(const InstrProfKind Other) {
  72. // If the kind is unset, this is the first profile we are merging so just
  73. // set it to the given type.
  74. if (ProfileKind == InstrProfKind::Unknown) {
  75. ProfileKind = Other;
  76. return Error::success();
  77. }
  78. // Returns true if merging is should fail assuming A and B are incompatible.
  79. auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
  80. return (static_cast<bool>(ProfileKind & A) &&
  81. static_cast<bool>(Other & B)) ||
  82. (static_cast<bool>(ProfileKind & B) &&
  83. static_cast<bool>(Other & A));
  84. };
  85. // Check if the profiles are in-compatible. Clang frontend profiles can't be
  86. // merged with other profile types.
  87. if (static_cast<bool>((ProfileKind & InstrProfKind::FE) ^
  88. (Other & InstrProfKind::FE))) {
  89. return make_error<InstrProfError>(instrprof_error::unsupported_version);
  90. }
  91. if (testIncompatible(InstrProfKind::FunctionEntryOnly, InstrProfKind::BB)) {
  92. return make_error<InstrProfError>(
  93. instrprof_error::unsupported_version,
  94. "cannot merge FunctionEntryOnly profiles and BB profiles together");
  95. }
  96. // Now we update the profile type with the bits that are set.
  97. ProfileKind |= Other;
  98. return Error::success();
  99. }
  100. // Internal interface for testing purpose only.
  101. void setValueProfDataEndianness(support::endianness Endianness);
  102. void setOutputSparse(bool Sparse);
  103. // Compute the overlap b/w this object and Other. Program level result is
  104. // stored in Overlap and function level result is stored in FuncLevelOverlap.
  105. void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
  106. OverlapStats &FuncLevelOverlap,
  107. const OverlapFuncFilters &FuncFilter);
  108. private:
  109. void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
  110. uint64_t Weight, function_ref<void(Error)> Warn);
  111. bool shouldEncodeData(const ProfilingData &PD);
  112. Error writeImpl(ProfOStream &OS);
  113. };
  114. } // end namespace llvm
  115. #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
  116. #ifdef __GNUC__
  117. #pragma GCC diagnostic pop
  118. #endif