1 //===- llvm/IR/LLVMRemarkStreamer.cpp - 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 contains the implementation of the conversion between IR
10 // Diagnostics and serializable remarks::Remark objects.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/LLVMRemarkStreamer.h"
15 #include "llvm/IR/DiagnosticInfo.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/Remarks/RemarkStreamer.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/ToolOutputFile.h"
25 /// DiagnosticKind -> remarks::Type
26 static remarks::Type
toRemarkType(enum DiagnosticKind Kind
) {
29 return remarks::Type::Unknown
;
30 case DK_OptimizationRemark
:
31 case DK_MachineOptimizationRemark
:
32 return remarks::Type::Passed
;
33 case DK_OptimizationRemarkMissed
:
34 case DK_MachineOptimizationRemarkMissed
:
35 return remarks::Type::Missed
;
36 case DK_OptimizationRemarkAnalysis
:
37 case DK_MachineOptimizationRemarkAnalysis
:
38 return remarks::Type::Analysis
;
39 case DK_OptimizationRemarkAnalysisFPCommute
:
40 return remarks::Type::AnalysisFPCommute
;
41 case DK_OptimizationRemarkAnalysisAliasing
:
42 return remarks::Type::AnalysisAliasing
;
43 case DK_OptimizationFailure
:
44 return remarks::Type::Failure
;
48 /// DiagnosticLocation -> remarks::RemarkLocation.
49 static std::optional
<remarks::RemarkLocation
>
50 toRemarkLocation(const DiagnosticLocation
&DL
) {
53 StringRef File
= DL
.getRelativePath();
54 unsigned Line
= DL
.getLine();
55 unsigned Col
= DL
.getColumn();
56 return remarks::RemarkLocation
{File
, Line
, Col
};
59 /// LLVM Diagnostic -> Remark
61 LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase
&Diag
) const {
62 remarks::Remark R
; // The result.
63 R
.RemarkType
= toRemarkType(static_cast<DiagnosticKind
>(Diag
.getKind()));
64 R
.PassName
= Diag
.getPassName();
65 R
.RemarkName
= Diag
.getRemarkName();
67 GlobalValue::dropLLVMManglingEscape(Diag
.getFunction().getName());
68 R
.Loc
= toRemarkLocation(Diag
.getLocation());
69 R
.Hotness
= Diag
.getHotness();
71 for (const DiagnosticInfoOptimizationBase::Argument
&Arg
: Diag
.getArgs()) {
72 R
.Args
.emplace_back();
73 R
.Args
.back().Key
= Arg
.Key
;
74 R
.Args
.back().Val
= Arg
.Val
;
75 R
.Args
.back().Loc
= toRemarkLocation(Arg
.Loc
);
81 void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase
&Diag
) {
82 if (!RS
.matchesFilter(Diag
.getPassName()))
85 // First, convert the diagnostic to a remark.
86 remarks::Remark R
= toRemark(Diag
);
87 // Then, emit the remark through the serializer.
88 RS
.getSerializer().emit(R
);
91 char LLVMRemarkSetupFileError::ID
= 0;
92 char LLVMRemarkSetupPatternError::ID
= 0;
93 char LLVMRemarkSetupFormatError::ID
= 0;
95 Expected
<std::unique_ptr
<ToolOutputFile
>> llvm::setupLLVMOptimizationRemarks(
96 LLVMContext
&Context
, StringRef RemarksFilename
, StringRef RemarksPasses
,
97 StringRef RemarksFormat
, bool RemarksWithHotness
,
98 std::optional
<uint64_t> RemarksHotnessThreshold
) {
99 if (RemarksWithHotness
|| RemarksHotnessThreshold
.value_or(1))
100 Context
.setDiagnosticsHotnessRequested(true);
102 Context
.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold
);
104 if (RemarksFilename
.empty())
107 Expected
<remarks::Format
> Format
= remarks::parseFormat(RemarksFormat
);
108 if (Error E
= Format
.takeError())
109 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
112 auto Flags
= *Format
== remarks::Format::YAML
? sys::fs::OF_TextWithCRLF
115 std::make_unique
<ToolOutputFile
>(RemarksFilename
, EC
, Flags
);
116 // We don't use llvm::FileError here because some diagnostics want the file
119 return make_error
<LLVMRemarkSetupFileError
>(errorCodeToError(EC
));
121 Expected
<std::unique_ptr
<remarks::RemarkSerializer
>> RemarkSerializer
=
122 remarks::createRemarkSerializer(
123 *Format
, remarks::SerializerMode::Separate
, RemarksFile
->os());
124 if (Error E
= RemarkSerializer
.takeError())
125 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
127 // Create the main remark streamer.
128 Context
.setMainRemarkStreamer(std::make_unique
<remarks::RemarkStreamer
>(
129 std::move(*RemarkSerializer
), RemarksFilename
));
131 // Create LLVM's optimization remarks streamer.
132 Context
.setLLVMRemarkStreamer(
133 std::make_unique
<LLVMRemarkStreamer
>(*Context
.getMainRemarkStreamer()));
135 if (!RemarksPasses
.empty())
136 if (Error E
= Context
.getMainRemarkStreamer()->setFilter(RemarksPasses
))
137 return make_error
<LLVMRemarkSetupPatternError
>(std::move(E
));
139 return std::move(RemarksFile
);
142 Error
llvm::setupLLVMOptimizationRemarks(
143 LLVMContext
&Context
, raw_ostream
&OS
, StringRef RemarksPasses
,
144 StringRef RemarksFormat
, bool RemarksWithHotness
,
145 std::optional
<uint64_t> RemarksHotnessThreshold
) {
146 if (RemarksWithHotness
|| RemarksHotnessThreshold
.value_or(1))
147 Context
.setDiagnosticsHotnessRequested(true);
149 Context
.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold
);
151 Expected
<remarks::Format
> Format
= remarks::parseFormat(RemarksFormat
);
152 if (Error E
= Format
.takeError())
153 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
155 Expected
<std::unique_ptr
<remarks::RemarkSerializer
>> RemarkSerializer
=
156 remarks::createRemarkSerializer(*Format
,
157 remarks::SerializerMode::Separate
, OS
);
158 if (Error E
= RemarkSerializer
.takeError())
159 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
161 // Create the main remark streamer.
162 Context
.setMainRemarkStreamer(
163 std::make_unique
<remarks::RemarkStreamer
>(std::move(*RemarkSerializer
)));
165 // Create LLVM's optimization remarks streamer.
166 Context
.setLLVMRemarkStreamer(
167 std::make_unique
<LLVMRemarkStreamer
>(*Context
.getMainRemarkStreamer()));
169 if (!RemarksPasses
.empty())
170 if (Error E
= Context
.getMainRemarkStreamer()->setFilter(RemarksPasses
))
171 return make_error
<LLVMRemarkSetupPatternError
>(std::move(E
));
173 return Error::success();