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