• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/IR/RemarkStreamer.h - Remark Streamer ---------------*- 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 declares the main interface for outputting remarks.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_REMARKSTREAMER_H
14 #define LLVM_IR_REMARKSTREAMER_H
15 
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/Remarks/RemarkSerializer.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <string>
23 #include <vector>
24 
25 namespace llvm {
26 /// Streamer for remarks.
27 class RemarkStreamer {
28   /// The regex used to filter remarks based on the passes that emit them.
29   Optional<Regex> PassFilter;
30   /// The object used to serialize the remarks to a specific format.
31   std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer;
32   /// The filename that the remark diagnostics are emitted to.
33   const Optional<std::string> Filename;
34 
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);
39 
40 public:
41   RemarkStreamer(std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
42                  Optional<StringRef> Filename = None);
43   /// Return the filename that the remark diagnostics are emitted to.
getFilename()44   Optional<StringRef> getFilename() const {
45     return Filename ? Optional<StringRef>(*Filename) : None;
46   }
47   /// Return stream that the remark diagnostics are emitted to.
getStream()48   raw_ostream &getStream() { return RemarkSerializer->OS; }
49   /// Return the serializer used for this stream.
getSerializer()50   remarks::RemarkSerializer &getSerializer() { return *RemarkSerializer; }
51   /// Set a pass filter based on a regex \p Filter.
52   /// Returns an error if the regex is invalid.
53   Error setFilter(StringRef Filter);
54   /// Emit a diagnostic through the streamer.
55   void emit(const DiagnosticInfoOptimizationBase &Diag);
56   /// Check if the remarks also need to have associated metadata in a section.
57   bool needsSection() const;
58 };
59 
60 template <typename ThisError>
61 struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
62   std::string Msg;
63   std::error_code EC;
64 
RemarkSetupErrorInfoRemarkSetupErrorInfo65   RemarkSetupErrorInfo(Error E) {
66     handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
67       Msg = EIB.message();
68       EC = EIB.convertToErrorCode();
69     });
70   }
71 
logRemarkSetupErrorInfo72   void log(raw_ostream &OS) const override { OS << Msg; }
convertToErrorCodeRemarkSetupErrorInfo73   std::error_code convertToErrorCode() const override { return EC; }
74 };
75 
76 struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
77   static char ID;
78   using RemarkSetupErrorInfo<RemarkSetupFileError>::RemarkSetupErrorInfo;
79 };
80 
81 struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
82   static char ID;
83   using RemarkSetupErrorInfo<RemarkSetupPatternError>::RemarkSetupErrorInfo;
84 };
85 
86 struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
87   static char ID;
88   using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
89 };
90 
91 /// Setup optimization remarks that output to a file.
92 Expected<std::unique_ptr<ToolOutputFile>>
93 setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
94                          StringRef RemarksPasses, StringRef RemarksFormat,
95                          bool RemarksWithHotness,
96                          unsigned RemarksHotnessThreshold = 0);
97 
98 /// Setup optimization remarks that output directly to a raw_ostream.
99 /// \p OS is managed by the caller and should be open for writing as long as \p
100 /// Context is streaming remarks to it.
101 Error setupOptimizationRemarks(LLVMContext &Context, raw_ostream &OS,
102                                StringRef RemarksPasses, StringRef RemarksFormat,
103                                bool RemarksWithHotness,
104                                unsigned RemarksHotnessThreshold = 0);
105 
106 } // end namespace llvm
107 
108 #endif // LLVM_IR_REMARKSTREAMER_H
109