1 | //===- llvm/IR/LLVMRemarkStreamer.cpp - Remark Streamer -*- C++ ---------*-===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file contains the implementation of the conversion between IR |
10 | // Diagnostics and serializable remarks::Remark objects. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
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" |
21 | #include <optional> |
22 | |
23 | using namespace llvm; |
24 | |
25 | /// DiagnosticKind -> remarks::Type |
26 | static remarks::Type (enum DiagnosticKind Kind) { |
27 | switch (Kind) { |
28 | default: |
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; |
45 | } |
46 | } |
47 | |
48 | /// DiagnosticLocation -> remarks::RemarkLocation. |
49 | static std::optional<remarks::RemarkLocation> |
50 | (const DiagnosticLocation &DL) { |
51 | if (!DL.isValid()) |
52 | return std::nullopt; |
53 | StringRef File = DL.getRelativePath(); |
54 | unsigned Line = DL.getLine(); |
55 | unsigned Col = DL.getColumn(); |
56 | return remarks::RemarkLocation{.SourceFilePath: File, .SourceLine: Line, .SourceColumn: Col}; |
57 | } |
58 | |
59 | /// LLVM Diagnostic -> Remark |
60 | remarks::Remark |
61 | LLVMRemarkStreamer::(const DiagnosticInfoOptimizationBase &Diag) const { |
62 | remarks::Remark R; // The result. |
63 | R.RemarkType = toRemarkType(Kind: static_cast<DiagnosticKind>(Diag.getKind())); |
64 | R.PassName = Diag.getPassName(); |
65 | R.RemarkName = Diag.getRemarkName(); |
66 | R.FunctionName = |
67 | GlobalValue::dropLLVMManglingEscape(Name: Diag.getFunction().getName()); |
68 | R.Loc = toRemarkLocation(DL: Diag.getLocation()); |
69 | R.Hotness = Diag.getHotness(); |
70 | |
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(DL: Arg.Loc); |
76 | } |
77 | |
78 | return R; |
79 | } |
80 | |
81 | void LLVMRemarkStreamer::(const DiagnosticInfoOptimizationBase &Diag) { |
82 | if (!RS.matchesFilter(Str: Diag.getPassName())) |
83 | return; |
84 | |
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(Remark: R); |
89 | } |
90 | |
91 | char LLVMRemarkSetupFileError:: = 0; |
92 | char LLVMRemarkSetupPatternError:: = 0; |
93 | char LLVMRemarkSetupFormatError:: = 0; |
94 | |
95 | Expected<std::unique_ptr<ToolOutputFile>> llvm::( |
96 | LLVMContext &Context, StringRef , StringRef , |
97 | StringRef , bool , |
98 | std::optional<uint64_t> ) { |
99 | if (RemarksWithHotness || RemarksHotnessThreshold.value_or(u: 1)) |
100 | Context.setDiagnosticsHotnessRequested(true); |
101 | |
102 | Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold); |
103 | |
104 | if (RemarksFilename.empty()) |
105 | return nullptr; |
106 | |
107 | Expected<remarks::Format> Format = remarks::parseFormat(FormatStr: RemarksFormat); |
108 | if (Error E = Format.takeError()) |
109 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
110 | |
111 | std::error_code EC; |
112 | auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_TextWithCRLF |
113 | : sys::fs::OF_None; |
114 | auto = |
115 | std::make_unique<ToolOutputFile>(args&: RemarksFilename, args&: EC, args&: Flags); |
116 | // We don't use llvm::FileError here because some diagnostics want the file |
117 | // name separately. |
118 | if (EC) |
119 | return make_error<LLVMRemarkSetupFileError>(Args: errorCodeToError(EC)); |
120 | |
121 | Expected<std::unique_ptr<remarks::RemarkSerializer>> = |
122 | remarks::createRemarkSerializer( |
123 | RemarksFormat: *Format, Mode: remarks::SerializerMode::Separate, OS&: RemarksFile->os()); |
124 | if (Error E = RemarkSerializer.takeError()) |
125 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
126 | |
127 | // Create the main remark streamer. |
128 | Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>( |
129 | args: std::move(*RemarkSerializer), args&: RemarksFilename)); |
130 | |
131 | // Create LLVM's optimization remarks streamer. |
132 | Context.setLLVMRemarkStreamer( |
133 | std::make_unique<LLVMRemarkStreamer>(args&: *Context.getMainRemarkStreamer())); |
134 | |
135 | if (!RemarksPasses.empty()) |
136 | if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses)) |
137 | return make_error<LLVMRemarkSetupPatternError>(Args: std::move(E)); |
138 | |
139 | return std::move(RemarksFile); |
140 | } |
141 | |
142 | Error llvm::( |
143 | LLVMContext &Context, raw_ostream &OS, StringRef , |
144 | StringRef , bool , |
145 | std::optional<uint64_t> ) { |
146 | if (RemarksWithHotness || RemarksHotnessThreshold.value_or(u: 1)) |
147 | Context.setDiagnosticsHotnessRequested(true); |
148 | |
149 | Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold); |
150 | |
151 | Expected<remarks::Format> Format = remarks::parseFormat(FormatStr: RemarksFormat); |
152 | if (Error E = Format.takeError()) |
153 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
154 | |
155 | Expected<std::unique_ptr<remarks::RemarkSerializer>> = |
156 | remarks::createRemarkSerializer(RemarksFormat: *Format, |
157 | Mode: remarks::SerializerMode::Separate, OS); |
158 | if (Error E = RemarkSerializer.takeError()) |
159 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
160 | |
161 | // Create the main remark streamer. |
162 | Context.setMainRemarkStreamer( |
163 | std::make_unique<remarks::RemarkStreamer>(args: std::move(*RemarkSerializer))); |
164 | |
165 | // Create LLVM's optimization remarks streamer. |
166 | Context.setLLVMRemarkStreamer( |
167 | std::make_unique<LLVMRemarkStreamer>(args&: *Context.getMainRemarkStreamer())); |
168 | |
169 | if (!RemarksPasses.empty()) |
170 | if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses)) |
171 | return make_error<LLVMRemarkSetupPatternError>(Args: std::move(E)); |
172 | |
173 | return Error::success(); |
174 | } |
175 | |