LLVMRemarkStreamer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- 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 implements the conversion between IR Diagnostics and
  15. // serializable remarks::Remark objects.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_LLVMREMARKSTREAMER_H
  19. #define LLVM_IR_LLVMREMARKSTREAMER_H
  20. #include "llvm/Remarks/Remark.h"
  21. #include "llvm/Support/Error.h"
  22. #include <memory>
  23. #include <optional>
  24. #include <string>
  25. namespace llvm {
  26. class DiagnosticInfoOptimizationBase;
  27. class LLVMContext;
  28. class ToolOutputFile;
  29. namespace remarks {
  30. class RemarkStreamer;
  31. }
  32. /// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo
  33. /// objects.
  34. class LLVMRemarkStreamer {
  35. remarks::RemarkStreamer &RS;
  36. /// Convert diagnostics into remark objects.
  37. /// The lifetime of the members of the result is bound to the lifetime of
  38. /// the LLVM diagnostics.
  39. remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag) const;
  40. public:
  41. LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {}
  42. /// Emit a diagnostic through the streamer.
  43. void emit(const DiagnosticInfoOptimizationBase &Diag);
  44. };
  45. template <typename ThisError>
  46. struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
  47. std::string Msg;
  48. std::error_code EC;
  49. LLVMRemarkSetupErrorInfo(Error E) {
  50. handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
  51. Msg = EIB.message();
  52. EC = EIB.convertToErrorCode();
  53. });
  54. }
  55. void log(raw_ostream &OS) const override { OS << Msg; }
  56. std::error_code convertToErrorCode() const override { return EC; }
  57. };
  58. struct LLVMRemarkSetupFileError
  59. : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> {
  60. static char ID;
  61. using LLVMRemarkSetupErrorInfo<
  62. LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo;
  63. };
  64. struct LLVMRemarkSetupPatternError
  65. : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> {
  66. static char ID;
  67. using LLVMRemarkSetupErrorInfo<
  68. LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo;
  69. };
  70. struct LLVMRemarkSetupFormatError
  71. : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> {
  72. static char ID;
  73. using LLVMRemarkSetupErrorInfo<
  74. LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo;
  75. };
  76. /// Setup optimization remarks that output to a file.
  77. Expected<std::unique_ptr<ToolOutputFile>>
  78. setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
  79. StringRef RemarksPasses, StringRef RemarksFormat,
  80. bool RemarksWithHotness,
  81. std::optional<uint64_t> RemarksHotnessThreshold = 0);
  82. /// Setup optimization remarks that output directly to a raw_ostream.
  83. /// \p OS is managed by the caller and should be open for writing as long as \p
  84. /// Context is streaming remarks to it.
  85. Error setupLLVMOptimizationRemarks(
  86. LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
  87. StringRef RemarksFormat, bool RemarksWithHotness,
  88. std::optional<uint64_t> RemarksHotnessThreshold = 0);
  89. } // end namespace llvm
  90. #endif // LLVM_IR_LLVMREMARKSTREAMER_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif