1//===- LLVMRemarkImport.cpp - Import LLVM remarks into MLIR ---------------===//
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#include "mlir/Remark/LLVMRemarkImport.h"
10
11#include "mlir/IR/Diagnostics.h"
12#include "mlir/IR/MLIRContext.h"
13#include "mlir/IR/Operation.h"
14#include "mlir/IR/SymbolTable.h"
15
16#include "llvm/ADT/Twine.h"
17#include "llvm/IR/DiagnosticInfo.h"
18#include "llvm/IR/DiagnosticPrinter.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/Remarks/Remark.h"
22#include "llvm/Remarks/RemarkParser.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace mlir;
27
28//===----------------------------------------------------------------------===//
29// Locations
30//===----------------------------------------------------------------------===//
31
32/// Returns the location of the symbol `functionName` inside `anchor`, or the
33/// location of `anchor` if there is no such symbol.
34static Location getFunctionLocation(Operation *anchor,
35 llvm::StringMap<Location> &cache,
36 StringRef functionName) {
37 if (functionName.empty() || !anchor->hasTrait<OpTrait::SymbolTable>())
38 return anchor->getLoc();
39 auto it = cache.find(Key: functionName);
40 if (it != cache.end())
41 return it->second;
42 Location loc = anchor->getLoc();
43 if (Operation *symbol = SymbolTable::lookupSymbolIn(op: anchor, symbol: functionName))
44 loc = symbol->getLoc();
45 cache.try_emplace(Key: functionName, Args&: loc);
46 return loc;
47}
48
49static Location resolveLocation(Operation *anchor,
50 llvm::StringMap<Location> &cache,
51 StringRef file, unsigned line, unsigned column,
52 StringRef functionName) {
53 if (!file.empty() && line != 0)
54 return FileLineColLoc::get(context: anchor->getContext(), fileName: file, line, column);
55 return getFunctionLocation(anchor, cache, functionName);
56}
57
58static Location
59resolveLocation(Operation *anchor, llvm::StringMap<Location> &cache,
60 const llvm::DiagnosticInfoWithLocationBase &diag) {
61 StringRef functionName = diag.getFunction().getName();
62 if (!diag.isLocationAvailable())
63 return resolveLocation(anchor, cache, file: "", line: 0, column: 0, functionName);
64 llvm::DiagnosticLocation loc = diag.getLocation();
65 return resolveLocation(anchor, cache, file: loc.getAbsolutePath(), line: loc.getLine(),
66 column: loc.getColumn(), functionName);
67}
68
69//===----------------------------------------------------------------------===//
70// Remark conversion
71//===----------------------------------------------------------------------===//
72
73namespace {
74/// The fields of an LLVM remark that are imported, shared by the live and the
75/// serialized paths.
76struct ImportedRemark {
77 remark::RemarkKind kind = remark::RemarkKind::RemarkUnknown;
78 StringRef passName;
79 StringRef remarkName;
80 StringRef functionName;
81 std::string message;
82 std::optional<uint64_t> hotness;
83 SmallVector<std::pair<StringRef, StringRef>> args;
84};
85} // namespace
86
87static remark::RemarkKind getRemarkKind(llvm::DiagnosticKind kind) {
88 switch (kind) {
89 case llvm::DK_OptimizationRemark:
90 case llvm::DK_MachineOptimizationRemark:
91 return remark::RemarkKind::RemarkPassed;
92 case llvm::DK_OptimizationRemarkMissed:
93 case llvm::DK_MachineOptimizationRemarkMissed:
94 return remark::RemarkKind::RemarkMissed;
95 case llvm::DK_OptimizationRemarkAnalysis:
96 case llvm::DK_OptimizationRemarkAnalysisFPCommute:
97 case llvm::DK_OptimizationRemarkAnalysisAliasing:
98 case llvm::DK_MachineOptimizationRemarkAnalysis:
99 return remark::RemarkKind::RemarkAnalysis;
100 case llvm::DK_OptimizationFailure:
101 return remark::RemarkKind::RemarkFailure;
102 default:
103 return remark::RemarkKind::RemarkUnknown;
104 }
105}
106
107static remark::RemarkKind getRemarkKind(llvm::remarks::Type type) {
108 switch (type) {
109 case llvm::remarks::Type::Passed:
110 return remark::RemarkKind::RemarkPassed;
111 case llvm::remarks::Type::Missed:
112 return remark::RemarkKind::RemarkMissed;
113 case llvm::remarks::Type::Analysis:
114 case llvm::remarks::Type::AnalysisFPCommute:
115 case llvm::remarks::Type::AnalysisAliasing:
116 return remark::RemarkKind::RemarkAnalysis;
117 case llvm::remarks::Type::Failure:
118 return remark::RemarkKind::RemarkFailure;
119 case llvm::remarks::Type::Unknown:
120 return remark::RemarkKind::RemarkUnknown;
121 }
122 llvm_unreachable("unknown remark type");
123}
124
125/// Returns the MLIR remark category of the LLVM pass `passName`.
126static std::string getCategory(StringRef passName) {
127 return (llvm::Twine(remark::llvmRemarkCategoryPrefix) + passName).str();
128}
129
130/// Renames the argument keys that the remark engine uses itself.
131static std::string getArgKey(StringRef key) {
132 if (key == "Remark" || key == "RemarkId" || key == "RelatedTo")
133 return ("LLVM" + key).str();
134 return key.str();
135}
136
137static void emitImportedRemark(remark::detail::RemarkEngine &engine,
138 Location loc, const ImportedRemark &imported) {
139 std::string category = getCategory(passName: imported.passName);
140 remark::RemarkOpts opts = remark::RemarkOpts::name(n: imported.remarkName)
141 .category(v: category)
142 .function(v: imported.functionName);
143 remark::detail::InFlightRemark inFlight;
144 switch (imported.kind) {
145 case remark::RemarkKind::RemarkPassed:
146 inFlight = engine.emitOptimizationRemark(loc, opts);
147 break;
148 case remark::RemarkKind::RemarkMissed:
149 inFlight = engine.emitOptimizationRemarkMiss(loc, opts);
150 break;
151 case remark::RemarkKind::RemarkFailure:
152 inFlight = engine.emitOptimizationRemarkFailure(loc, opts);
153 break;
154 case remark::RemarkKind::RemarkAnalysis:
155 inFlight = engine.emitOptimizationRemarkAnalysis(loc, opts);
156 break;
157 case remark::RemarkKind::RemarkUnknown:
158 return;
159 }
160 if (!inFlight)
161 return;
162
163 inFlight << StringRef(imported.message);
164 for (const auto &[key, value] : imported.args) {
165 // Plain strings are already part of the message.
166 if (key == "String")
167 continue;
168 inFlight << remark::detail::Remark::Arg(getArgKey(key), value);
169 }
170 if (imported.hotness)
171 inFlight << remark::detail::Remark::Arg("Hotness", *imported.hotness);
172}
173
174static void importLLVMRemark(remark::detail::RemarkEngine &engine, Location loc,
175 const llvm::DiagnosticInfoOptimizationBase &diag) {
176 ImportedRemark imported;
177 imported.kind =
178 getRemarkKind(kind: static_cast<llvm::DiagnosticKind>(diag.getKind()));
179 imported.passName = diag.getPassName();
180 imported.remarkName = diag.getRemarkName();
181 imported.functionName =
182 llvm::GlobalValue::dropLLVMManglingEscape(Name: diag.getFunction().getName());
183 imported.message = diag.getMsg();
184 imported.hotness = diag.getHotness();
185 for (const llvm::DiagnosticInfoOptimizationBase::Argument &arg :
186 diag.getArgs())
187 imported.args.emplace_back(Args: arg.Key, Args: arg.Val);
188 emitImportedRemark(engine, loc, imported);
189}
190
191static void importLLVMRemark(remark::detail::RemarkEngine &engine, Location loc,
192 const llvm::remarks::Remark &remark) {
193 ImportedRemark imported;
194 imported.kind = getRemarkKind(type: remark.RemarkType);
195 imported.passName = remark.PassName;
196 imported.remarkName = remark.RemarkName;
197 imported.functionName = remark.FunctionName;
198 imported.message = remark.getArgsAsMsg();
199 imported.hotness = remark.Hotness;
200 for (const llvm::remarks::Argument &arg : remark.Args)
201 imported.args.emplace_back(Args: arg.Key, Args: arg.Val);
202 emitImportedRemark(engine, loc, imported);
203}
204
205LogicalResult mlir::remark::importLLVMRemarks(Operation *anchor,
206 StringRef buffer,
207 llvm::remarks::Format format) {
208 remark::detail::RemarkEngine *engine =
209 anchor->getContext()->getRemarkEngine();
210 if (!engine)
211 return success();
212
213 llvm::Expected<std::unique_ptr<llvm::remarks::RemarkParser>> parser =
214 llvm::remarks::createRemarkParser(ParserFormat: format, Buf: buffer);
215 if (!parser) {
216 llvm::consumeError(Err: parser.takeError());
217 return failure();
218 }
219
220 llvm::StringMap<Location> functionLocations;
221 while (true) {
222 llvm::Expected<std::unique_ptr<llvm::remarks::Remark>> next =
223 (*parser)->next();
224 if (!next) {
225 llvm::Error error = next.takeError();
226 bool endOfFile = error.isA<llvm::remarks::EndOfFileError>();
227 llvm::consumeError(Err: std::move(error));
228 return success(IsSuccess: endOfFile);
229 }
230 const llvm::remarks::Remark &remark = **next;
231 Location loc = remark.Loc ? resolveLocation(anchor, cache&: functionLocations,
232 file: remark.Loc->SourceFilePath,
233 line: remark.Loc->SourceLine,
234 column: remark.Loc->SourceColumn,
235 functionName: remark.FunctionName)
236 : resolveLocation(anchor, cache&: functionLocations, file: "",
237 line: 0, column: 0, functionName: remark.FunctionName);
238 importLLVMRemark(engine&: *engine, loc, remark);
239 }
240}
241
242//===----------------------------------------------------------------------===//
243// LLVMToMLIRDiagnosticHandler
244//===----------------------------------------------------------------------===//
245
246remark::LLVMToMLIRDiagnosticHandler::LLVMToMLIRDiagnosticHandler(
247 Operation *anchor)
248 : anchor(anchor), engine(anchor->getContext()->getRemarkEngine()) {}
249
250bool remark::LLVMToMLIRDiagnosticHandler::isAnalysisRemarkEnabled(
251 StringRef passName) const {
252 return (engine &&
253 engine->isAnalysisOptRemarkEnabled(categoryName: getCategory(passName))) ||
254 DiagnosticHandler::isAnalysisRemarkEnabled(PassName: passName);
255}
256
257bool remark::LLVMToMLIRDiagnosticHandler::isMissedOptRemarkEnabled(
258 StringRef passName) const {
259 return (engine && engine->isMissedOptRemarkEnabled(categoryName: getCategory(passName))) ||
260 DiagnosticHandler::isMissedOptRemarkEnabled(PassName: passName);
261}
262
263bool remark::LLVMToMLIRDiagnosticHandler::isPassedOptRemarkEnabled(
264 StringRef passName) const {
265 return (engine && engine->isPassedOptRemarkEnabled(categoryName: getCategory(passName))) ||
266 DiagnosticHandler::isPassedOptRemarkEnabled(PassName: passName);
267}
268
269bool remark::LLVMToMLIRDiagnosticHandler::isAnyRemarkEnabled() const {
270 return (engine && engine->isAnyRemarkEnabled()) ||
271 DiagnosticHandler::isAnyRemarkEnabled();
272}
273
274bool remark::LLVMToMLIRDiagnosticHandler::handleDiagnostics(
275 const llvm::DiagnosticInfo &diag) {
276 if (const auto *optRemark =
277 dyn_cast<llvm::DiagnosticInfoOptimizationBase>(Val: &diag)) {
278 remark::RemarkKind kind =
279 getRemarkKind(kind: static_cast<llvm::DiagnosticKind>(optRemark->getKind()));
280 if (!engine ||
281 !engine->isRemarkEnabled(kind, categoryName: getCategory(passName: optRemark->getPassName())))
282 return false;
283 importLLVMRemark(engine&: *engine,
284 loc: resolveLocation(anchor, cache&: functionLocations, diag: *optRemark),
285 diag: *optRemark);
286 return true;
287 }
288
289 std::string message;
290 llvm::raw_string_ostream os(message);
291 llvm::DiagnosticPrinterRawOStream printer(os);
292 diag.print(DP&: printer);
293 StringRef text = StringRef(message).rtrim();
294
295 Location loc = anchor->getLoc();
296 if (const auto *withLoc = dyn_cast<llvm::DiagnosticInfoUnsupported>(Val: &diag)) {
297 loc = resolveLocation(anchor, cache&: functionLocations, diag: *withLoc);
298 // The location is carried by `loc` already.
299 text.consume_front(Prefix: withLoc->getLocationStr() + ": ");
300 }
301
302 switch (diag.getSeverity()) {
303 case llvm::DS_Error:
304 emitError(loc) << text;
305 break;
306 case llvm::DS_Warning:
307 emitWarning(loc) << text;
308 break;
309 case llvm::DS_Remark:
310 case llvm::DS_Note:
311 emitRemark(loc) << text;
312 break;
313 }
314 return true;
315}
316