LLVMRemarkStreamer.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the conversion between IR Diagnostics and
  10. // serializable remarks::Remark objects.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_LLVMREMARKSTREAMER_H
  14. #define LLVM_IR_LLVMREMARKSTREAMER_H
  15. #include "llvm/IR/DiagnosticInfo.h"
  16. #include "llvm/Remarks/RemarkStreamer.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/ToolOutputFile.h"
  19. #include <memory>
  20. #include <string>
  21. namespace llvm {
  22. /// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo
  23. /// objects.
  24. class LLVMRemarkStreamer {
  25. remarks::RemarkStreamer &RS;
  26. /// Convert diagnostics into remark objects.
  27. /// The lifetime of the members of the result is bound to the lifetime of
  28. /// the LLVM diagnostics.
  29. remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag) const;
  30. public:
  31. LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {}
  32. /// Emit a diagnostic through the streamer.
  33. void emit(const DiagnosticInfoOptimizationBase &Diag);
  34. };
  35. template <typename ThisError>
  36. struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
  37. std::string Msg;
  38. std::error_code EC;
  39. LLVMRemarkSetupErrorInfo(Error E) {
  40. handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
  41. Msg = EIB.message();
  42. EC = EIB.convertToErrorCode();
  43. });
  44. }
  45. void log(raw_ostream &OS) const override { OS << Msg; }
  46. std::error_code convertToErrorCode() const override { return EC; }
  47. };
  48. struct LLVMRemarkSetupFileError
  49. : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> {
  50. static char ID;
  51. using LLVMRemarkSetupErrorInfo<
  52. LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo;
  53. };
  54. struct LLVMRemarkSetupPatternError
  55. : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> {
  56. static char ID;
  57. using LLVMRemarkSetupErrorInfo<
  58. LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo;
  59. };
  60. struct LLVMRemarkSetupFormatError
  61. : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> {
  62. static char ID;
  63. using LLVMRemarkSetupErrorInfo<
  64. LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo;
  65. };
  66. /// Setup optimization remarks that output to a file.
  67. Expected<std::unique_ptr<ToolOutputFile>>
  68. setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
  69. StringRef RemarksPasses, StringRef RemarksFormat,
  70. bool RemarksWithHotness,
  71. Optional<uint64_t> RemarksHotnessThreshold = 0);
  72. /// Setup optimization remarks that output directly to a raw_ostream.
  73. /// \p OS is managed by the caller and should be open for writing as long as \p
  74. /// Context is streaming remarks to it.
  75. Error setupLLVMOptimizationRemarks(
  76. LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
  77. StringRef RemarksFormat, bool RemarksWithHotness,
  78. Optional<uint64_t> RemarksHotnessThreshold = 0);
  79. } // end namespace llvm
  80. #endif // LLVM_IR_LLVMREMARKSTREAMER_H