1 //===- llvm/IR/RemarkStreamer.h - Remark Streamer ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares the main interface for outputting remarks.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_REMARKSTREAMER_H
14 #define LLVM_IR_REMARKSTREAMER_H
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"
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;
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);
41 RemarkStreamer(std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
42 Optional<StringRef> Filename = None);
43 /// Return the filename that the remark diagnostics are emitted to.
44 Optional<StringRef> getFilename() const {
45 return Filename ? Optional<StringRef>(*Filename) : None;
47 /// Return stream that the remark diagnostics are emitted to.
48 raw_ostream &getStream() { return RemarkSerializer->OS; }
49 /// Return the serializer used for this stream.
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;
60 template <typename ThisError>
61 struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
65 RemarkSetupErrorInfo(Error E) {
66 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
68 EC = EIB.convertToErrorCode();
72 void log(raw_ostream &OS) const override { OS << Msg; }
73 std::error_code convertToErrorCode() const override { return EC; }
76 struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
78 using RemarkSetupErrorInfo<RemarkSetupFileError>::RemarkSetupErrorInfo;
81 struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
83 using RemarkSetupErrorInfo<RemarkSetupPatternError>::RemarkSetupErrorInfo;
86 struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
88 using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
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);
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);
106 } // end namespace llvm
108 #endif // LLVM_IR_REMARKSTREAMER_H