LLVM  10.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::RemarkSerializer> RemarkSerializer;
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(StringRef Filename,
42  std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer);
43  /// Return the filename that the remark diagnostics are emitted to.
44  StringRef getFilename() const { return Filename; }
45  /// Return stream that the remark diagnostics are emitted to.
46  raw_ostream &getStream() { return RemarkSerializer->OS; }
47  /// Return the serializer used for this stream.
48  remarks::RemarkSerializer &getSerializer() { return *RemarkSerializer; }
49  /// Set a pass filter based on a regex \p Filter.
50  /// Returns an error if the regex is invalid.
52  /// Emit a diagnostic through the streamer.
53  void emit(const DiagnosticInfoOptimizationBase &Diag);
54 };
55 
56 template <typename ThisError>
57 struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
58  std::string Msg;
59  std::error_code EC;
60 
62  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
63  Msg = EIB.message();
64  EC = EIB.convertToErrorCode();
65  });
66  }
67 
68  void log(raw_ostream &OS) const override { OS << Msg; }
69  std::error_code convertToErrorCode() const override { return EC; }
70 };
71 
72 struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
73  static char ID;
75 };
76 
77 struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
78  static char ID;
80 };
81 
82 struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
83  static char ID;
85 };
86 
87 /// Setup optimization remarks.
91  bool RemarksWithHotness,
92  unsigned RemarksHotnessThreshold = 0);
93 
94 } // end namespace llvm
95 
96 #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
remarks::RemarkSerializer & 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:67
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
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.
RemarkStreamer(StringRef Filename, std::unique_ptr< remarks::RemarkSerializer > RemarkSerializer)
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")
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 the base class for a remark serializer.
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.