LLVMRemarkStreamer.h 3.5 KB

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