LLVMRemarkStreamer.h 3.4 KB

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