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 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
;
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(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.
51 Error
setFilter(StringRef Filter
);
52 /// Emit a diagnostic through the streamer.
53 void emit(const DiagnosticInfoOptimizationBase
&Diag
);
56 template <typename ThisError
>
57 struct RemarkSetupErrorInfo
: public ErrorInfo
<ThisError
> {
61 RemarkSetupErrorInfo(Error E
) {
62 handleAllErrors(std::move(E
), [&](const ErrorInfoBase
&EIB
) {
64 EC
= EIB
.convertToErrorCode();
68 void log(raw_ostream
&OS
) const override
{ OS
<< Msg
; }
69 std::error_code
convertToErrorCode() const override
{ return EC
; }
72 struct RemarkSetupFileError
: RemarkSetupErrorInfo
<RemarkSetupFileError
> {
74 using RemarkSetupErrorInfo
<RemarkSetupFileError
>::RemarkSetupErrorInfo
;
77 struct RemarkSetupPatternError
: RemarkSetupErrorInfo
<RemarkSetupPatternError
> {
79 using RemarkSetupErrorInfo
<RemarkSetupPatternError
>::RemarkSetupErrorInfo
;
82 struct RemarkSetupFormatError
: RemarkSetupErrorInfo
<RemarkSetupFormatError
> {
84 using RemarkSetupErrorInfo
<RemarkSetupFormatError
>::RemarkSetupErrorInfo
;
87 /// Setup optimization remarks.
88 Expected
<std::unique_ptr
<ToolOutputFile
>>
89 setupOptimizationRemarks(LLVMContext
&Context
, StringRef RemarksFilename
,
90 StringRef RemarksPasses
, StringRef RemarksFormat
,
91 bool RemarksWithHotness
,
92 unsigned RemarksHotnessThreshold
= 0);
94 } // end namespace llvm
96 #endif // LLVM_IR_REMARKSTREAMER_H