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/Support/FileSystem.h"
22 /// DiagnosticKind -> remarks::Type
23 static remarks::Type
toRemarkType(enum DiagnosticKind Kind
) {
26 return remarks::Type::Unknown
;
27 case DK_OptimizationRemark
:
28 case DK_MachineOptimizationRemark
:
29 return remarks::Type::Passed
;
30 case DK_OptimizationRemarkMissed
:
31 case DK_MachineOptimizationRemarkMissed
:
32 return remarks::Type::Missed
;
33 case DK_OptimizationRemarkAnalysis
:
34 case DK_MachineOptimizationRemarkAnalysis
:
35 return remarks::Type::Analysis
;
36 case DK_OptimizationRemarkAnalysisFPCommute
:
37 return remarks::Type::AnalysisFPCommute
;
38 case DK_OptimizationRemarkAnalysisAliasing
:
39 return remarks::Type::AnalysisAliasing
;
40 case DK_OptimizationFailure
:
41 return remarks::Type::Failure
;
45 /// DiagnosticLocation -> remarks::RemarkLocation.
46 static Optional
<remarks::RemarkLocation
>
47 toRemarkLocation(const DiagnosticLocation
&DL
) {
50 StringRef File
= DL
.getRelativePath();
51 unsigned Line
= DL
.getLine();
52 unsigned Col
= DL
.getColumn();
53 return remarks::RemarkLocation
{File
, Line
, Col
};
56 /// LLVM Diagnostic -> Remark
58 LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase
&Diag
) const {
59 remarks::Remark R
; // The result.
60 R
.RemarkType
= toRemarkType(static_cast<DiagnosticKind
>(Diag
.getKind()));
61 R
.PassName
= Diag
.getPassName();
62 R
.RemarkName
= Diag
.getRemarkName();
64 GlobalValue::dropLLVMManglingEscape(Diag
.getFunction().getName());
65 R
.Loc
= toRemarkLocation(Diag
.getLocation());
66 R
.Hotness
= Diag
.getHotness();
68 for (const DiagnosticInfoOptimizationBase::Argument
&Arg
: Diag
.getArgs()) {
69 R
.Args
.emplace_back();
70 R
.Args
.back().Key
= Arg
.Key
;
71 R
.Args
.back().Val
= Arg
.Val
;
72 R
.Args
.back().Loc
= toRemarkLocation(Arg
.Loc
);
78 void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase
&Diag
) {
79 if (!RS
.matchesFilter(Diag
.getPassName()))
82 // First, convert the diagnostic to a remark.
83 remarks::Remark R
= toRemark(Diag
);
84 // Then, emit the remark through the serializer.
85 RS
.getSerializer().emit(R
);
88 char LLVMRemarkSetupFileError::ID
= 0;
89 char LLVMRemarkSetupPatternError::ID
= 0;
90 char LLVMRemarkSetupFormatError::ID
= 0;
92 Expected
<std::unique_ptr
<ToolOutputFile
>> llvm::setupLLVMOptimizationRemarks(
93 LLVMContext
&Context
, StringRef RemarksFilename
, StringRef RemarksPasses
,
94 StringRef RemarksFormat
, bool RemarksWithHotness
,
95 Optional
<uint64_t> RemarksHotnessThreshold
) {
96 if (RemarksWithHotness
)
97 Context
.setDiagnosticsHotnessRequested(true);
99 Context
.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold
);
101 if (RemarksFilename
.empty())
104 Expected
<remarks::Format
> Format
= remarks::parseFormat(RemarksFormat
);
105 if (Error E
= Format
.takeError())
106 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
109 auto Flags
= *Format
== remarks::Format::YAML
? sys::fs::OF_TextWithCRLF
112 std::make_unique
<ToolOutputFile
>(RemarksFilename
, EC
, Flags
);
113 // We don't use llvm::FileError here because some diagnostics want the file
116 return make_error
<LLVMRemarkSetupFileError
>(errorCodeToError(EC
));
118 Expected
<std::unique_ptr
<remarks::RemarkSerializer
>> RemarkSerializer
=
119 remarks::createRemarkSerializer(
120 *Format
, remarks::SerializerMode::Separate
, RemarksFile
->os());
121 if (Error E
= RemarkSerializer
.takeError())
122 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
124 // Create the main remark streamer.
125 Context
.setMainRemarkStreamer(std::make_unique
<remarks::RemarkStreamer
>(
126 std::move(*RemarkSerializer
), RemarksFilename
));
128 // Create LLVM's optimization remarks streamer.
129 Context
.setLLVMRemarkStreamer(
130 std::make_unique
<LLVMRemarkStreamer
>(*Context
.getMainRemarkStreamer()));
132 if (!RemarksPasses
.empty())
133 if (Error E
= Context
.getMainRemarkStreamer()->setFilter(RemarksPasses
))
134 return make_error
<LLVMRemarkSetupPatternError
>(std::move(E
));
136 return std::move(RemarksFile
);
139 Error
llvm::setupLLVMOptimizationRemarks(
140 LLVMContext
&Context
, raw_ostream
&OS
, StringRef RemarksPasses
,
141 StringRef RemarksFormat
, bool RemarksWithHotness
,
142 Optional
<uint64_t> RemarksHotnessThreshold
) {
143 if (RemarksWithHotness
)
144 Context
.setDiagnosticsHotnessRequested(true);
146 Context
.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold
);
148 Expected
<remarks::Format
> Format
= remarks::parseFormat(RemarksFormat
);
149 if (Error E
= Format
.takeError())
150 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
152 Expected
<std::unique_ptr
<remarks::RemarkSerializer
>> RemarkSerializer
=
153 remarks::createRemarkSerializer(*Format
,
154 remarks::SerializerMode::Separate
, OS
);
155 if (Error E
= RemarkSerializer
.takeError())
156 return make_error
<LLVMRemarkSetupFormatError
>(std::move(E
));
158 // Create the main remark streamer.
159 Context
.setMainRemarkStreamer(
160 std::make_unique
<remarks::RemarkStreamer
>(std::move(*RemarkSerializer
)));
162 // Create LLVM's optimization remarks streamer.
163 Context
.setLLVMRemarkStreamer(
164 std::make_unique
<LLVMRemarkStreamer
>(*Context
.getMainRemarkStreamer()));
166 if (!RemarksPasses
.empty())
167 if (Error E
= Context
.getMainRemarkStreamer()->setFilter(RemarksPasses
))
168 return make_error
<LLVMRemarkSetupPatternError
>(std::move(E
));
170 return Error::success();