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/Remarks/Remark.h"
18 #include "llvm/Support/Error.h"
19 #include <memory>
20 #include <string>
21 
22 namespace llvm {
23 
24 class DiagnosticInfoOptimizationBase;
25 class LLVMContext;
26 class ToolOutputFile;
27 namespace remarks {
28 class RemarkStreamer;
29 }
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 
40 public:
41   LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {}
42   /// Emit a diagnostic through the streamer.
43   void emit(const DiagnosticInfoOptimizationBase &Diag);
44 };
45 
46 template <typename ThisError>
47 struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
48   std::string Msg;
49   std::error_code EC;
50 
51   LLVMRemarkSetupErrorInfo(Error E) {
52     handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
53       Msg = EIB.message();
54       EC = EIB.convertToErrorCode();
55     });
56   }
57 
58   void log(raw_ostream &OS) const override { OS << Msg; }
59   std::error_code convertToErrorCode() const override { return EC; }
60 };
61 
62 struct LLVMRemarkSetupFileError
63     : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> {
64   static char ID;
65   using LLVMRemarkSetupErrorInfo<
66       LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo;
67 };
68 
69 struct LLVMRemarkSetupPatternError
70     : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> {
71   static char ID;
72   using LLVMRemarkSetupErrorInfo<
73       LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo;
74 };
75 
76 struct LLVMRemarkSetupFormatError
77     : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> {
78   static char ID;
79   using LLVMRemarkSetupErrorInfo<
80       LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo;
81 };
82 
83 /// Setup optimization remarks that output to a file.
84 Expected<std::unique_ptr<ToolOutputFile>>
85 setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
86                              StringRef RemarksPasses, StringRef RemarksFormat,
87                              bool RemarksWithHotness,
88                              Optional<uint64_t> RemarksHotnessThreshold = 0);
89 
90 /// Setup optimization remarks that output directly to a raw_ostream.
91 /// \p OS is managed by the caller and should be open for writing as long as \p
92 /// Context is streaming remarks to it.
93 Error setupLLVMOptimizationRemarks(
94     LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
95     StringRef RemarksFormat, bool RemarksWithHotness,
96     Optional<uint64_t> RemarksHotnessThreshold = 0);
97 
98 } // end namespace llvm
99 
100 #endif // LLVM_IR_LLVMREMARKSTREAMER_H
101