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
);
58 template <typename ThisError
>
59 struct RemarkSetupErrorInfo
: public ErrorInfo
<ThisError
> {
63 RemarkSetupErrorInfo(Error E
) {
64 handleAllErrors(std::move(E
), [&](const ErrorInfoBase
&EIB
) {
66 EC
= EIB
.convertToErrorCode();
70 void log(raw_ostream
&OS
) const override
{ OS
<< Msg
; }
71 std::error_code
convertToErrorCode() const override
{ return EC
; }
74 struct RemarkSetupFileError
: RemarkSetupErrorInfo
<RemarkSetupFileError
> {
76 using RemarkSetupErrorInfo
<RemarkSetupFileError
>::RemarkSetupErrorInfo
;
79 struct RemarkSetupPatternError
: RemarkSetupErrorInfo
<RemarkSetupPatternError
> {
81 using RemarkSetupErrorInfo
<RemarkSetupPatternError
>::RemarkSetupErrorInfo
;
84 struct RemarkSetupFormatError
: RemarkSetupErrorInfo
<RemarkSetupFormatError
> {
86 using RemarkSetupErrorInfo
<RemarkSetupFormatError
>::RemarkSetupErrorInfo
;
89 /// Setup optimization remarks that output to a file.
90 Expected
<std::unique_ptr
<ToolOutputFile
>>
91 setupOptimizationRemarks(LLVMContext
&Context
, StringRef RemarksFilename
,
92 StringRef RemarksPasses
, StringRef RemarksFormat
,
93 bool RemarksWithHotness
,
94 unsigned RemarksHotnessThreshold
= 0);
96 /// Setup optimization remarks that output directly to a raw_ostream.
97 /// \p OS is managed by the caller and should be open for writing as long as \p
98 /// Context is streaming remarks to it.
99 Error
setupOptimizationRemarks(LLVMContext
&Context
, raw_ostream
&OS
,
100 StringRef RemarksPasses
, StringRef RemarksFormat
,
101 bool RemarksWithHotness
,
102 unsigned RemarksHotnessThreshold
= 0);
104 } // end namespace llvm
106 #endif // LLVM_IR_REMARKSTREAMER_H