LLVM  9.0.0svn
RemarkStreamer.h
Go to the documentation of this file.
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"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/Regex.h"
22 #include <string>
23 #include <vector>
24 
25 namespace llvm {
26 /// Streamer for remarks.
28  /// The filename that the remark diagnostics are emitted to.
29  const std::string Filename;
30  /// The regex used to filter remarks based on the passes that emit them.
31  Optional<Regex> PassFilter;
32  /// The object used to serialize the remarks to a specific format.
33  std::unique_ptr<remarks::Serializer> Serializer;
34 
35  /// Temporary buffer for converting diagnostics into remark objects. This is
36  /// used for the remark arguments that are converted from a vector of
37  /// diagnostic arguments to a vector of remark arguments.
39  /// Convert diagnostics into remark objects. The result uses \p TmpArgs as a
40  /// temporary buffer for the remark arguments, and relies on all the strings
41  /// to be kept in memory until the next call to `toRemark`.
42  /// The lifetime of the members of the result is bound to the lifetime of both
43  /// the remark streamer and the LLVM diagnostics.
44  remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag);
45 
46 public:
47  RemarkStreamer(StringRef Filename,
48  std::unique_ptr<remarks::Serializer> Serializer);
49  /// Return the filename that the remark diagnostics are emitted to.
50  StringRef getFilename() const { return Filename; }
51  /// Return stream that the remark diagnostics are emitted to.
52  raw_ostream &getStream() { return Serializer->OS; }
53  /// Return the serializer used for this stream.
54  remarks::Serializer &getSerializer() { return *Serializer; }
55  /// Set a pass filter based on a regex \p Filter.
56  /// Returns an error if the regex is invalid.
58  /// Emit a diagnostic through the streamer.
59  void emit(const DiagnosticInfoOptimizationBase &Diag);
60 };
61 
62 template <typename ThisError>
63 struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
64  std::string Msg;
65  std::error_code EC;
66 
68  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
69  Msg = EIB.message();
70  EC = EIB.convertToErrorCode();
71  });
72  }
73 
74  void log(raw_ostream &OS) const override { OS << Msg; }
75  std::error_code convertToErrorCode() const override { return EC; }
76 };
77 
78 struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
79  static char ID;
81 };
82 
83 struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
84  static char ID;
86 };
87 
88 struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
89  static char ID;
91 };
92 
94 
96 
97 /// Setup optimization remarks.
101  bool RemarksWithHotness,
102  unsigned RemarksHotnessThreshold = 0);
103 
104 } // end namespace llvm
105 
106 #endif // LLVM_IR_REMARKSTREAMER_H
Error setFilter(StringRef Filter)
Set a pass filter based on a regex Filter.
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:56
StringRef getFilename() const
Return the filename that the remark diagnostics are emitted to.
Base class for error info classes.
Definition: Error.h:48
RemarksSerializerFormat
remarks::Serializer & getSerializer()
Return the serializer used for this stream.
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
A remark type used for both emission and parsing.
Definition: Remark.h:66
RemarkStreamer(StringRef Filename, std::unique_ptr< remarks::Serializer > Serializer)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
Expected< RemarksSerializerFormat > parseSerializerFormat(StringRef Format)
void log(raw_ostream &OS) const override
Print an error message to an output stream.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
raw_ostream & getStream()
Return stream that the remark diagnostics are emitted to.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is the base class for a remark serializer.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:904
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
Base class for user error types.
Definition: Error.h:344
Streamer for remarks.
Expected< std::unique_ptr< ToolOutputFile > > setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, unsigned RemarksHotnessThreshold=0)
Setup optimization remarks.
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.