1//===- Diagnostics.cpp - MLIR Diagnostics ---------------------------------===//
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/IR/Diagnostics.h"
10#include "mlir/IR/Attributes.h"
11#include "mlir/IR/Location.h"
12#include "mlir/IR/MLIRContext.h"
13#include "mlir/IR/Operation.h"
14#include "mlir/IR/Types.h"
15#include "llvm/ADT/MapVector.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/TypeSwitch.h"
19#include "llvm/Support/Mutex.h"
20#include "llvm/Support/PrettyStackTrace.h"
21#include "llvm/Support/Regex.h"
22#include "llvm/Support/Signals.h"
23#include "llvm/Support/SourceMgr.h"
24#include "llvm/Support/SourceMgrDiagnosticVerifier.h"
25#include "llvm/Support/raw_ostream.h"
26#include <atomic>
27#include <optional>
28
29using namespace mlir;
30using namespace mlir::detail;
31
32//===----------------------------------------------------------------------===//
33// DiagnosticArgument
34//===----------------------------------------------------------------------===//
35
36/// Construct from an Attribute.
37DiagnosticArgument::DiagnosticArgument(Attribute attr)
38 : kind(DiagnosticArgumentKind::Attribute),
39 opaqueVal(const_cast<void *>(attr.getAsOpaquePointer())) {}
40
41/// Construct from a Type.
42DiagnosticArgument::DiagnosticArgument(Type val)
43 : kind(DiagnosticArgumentKind::Type),
44 opaqueVal(const_cast<void *>(val.getAsOpaquePointer())) {}
45
46/// Returns this argument as an Attribute.
47Attribute DiagnosticArgument::getAsAttribute() const {
48 assert(getKind() == DiagnosticArgumentKind::Attribute);
49 return Attribute::getFromOpaquePointer(ptr: opaqueVal);
50}
51
52/// Returns this argument as a Type.
53Type DiagnosticArgument::getAsType() const {
54 assert(getKind() == DiagnosticArgumentKind::Type);
55 return Type::getFromOpaquePointer(pointer: opaqueVal);
56}
57
58/// Outputs this argument to a stream.
59void DiagnosticArgument::print(raw_ostream &os) const {
60 switch (kind) {
61 case DiagnosticArgumentKind::Attribute:
62 os << getAsAttribute();
63 break;
64 case DiagnosticArgumentKind::Double:
65 os << getAsDouble();
66 break;
67 case DiagnosticArgumentKind::Integer:
68 os << getAsInteger();
69 break;
70 case DiagnosticArgumentKind::String:
71 os << getAsString();
72 break;
73 case DiagnosticArgumentKind::Type:
74 os << '\'' << getAsType() << '\'';
75 break;
76 case DiagnosticArgumentKind::Unsigned:
77 os << getAsUnsigned();
78 break;
79 }
80}
81
82//===----------------------------------------------------------------------===//
83// Diagnostic
84//===----------------------------------------------------------------------===//
85
86/// Convert a Twine to a StringRef. Memory used for generating the StringRef is
87/// stored in 'strings'.
88static StringRef
89twineToStrRef(const Twine &val,
90 SmallVectorImpl<std::unique_ptr<char[]>> &strings) {
91 // Allocate memory to hold this string.
92 SmallString<64> data;
93 auto strRef = val.toStringRef(Out&: data);
94 if (strRef.empty())
95 return strRef;
96
97 strings.push_back(Elt: std::unique_ptr<char[]>(new char[strRef.size()]));
98 memcpy(dest: &strings.back()[0], src: strRef.data(), n: strRef.size());
99 // Return a reference to the new string.
100 return StringRef(&strings.back()[0], strRef.size());
101}
102
103DiagnosticArgument Diagnostic::getStringArgument(const Twine &val) {
104 return DiagnosticArgument(twineToStrRef(val, strings));
105}
106
107/// Stream in a Twine argument.
108Diagnostic &Diagnostic::operator<<(char val) { return *this << Twine(val); }
109Diagnostic &Diagnostic::operator<<(const Twine &val) {
110 arguments.push_back(Elt: getStringArgument(val));
111 return *this;
112}
113Diagnostic &Diagnostic::operator<<(Twine &&val) {
114 arguments.push_back(Elt: getStringArgument(val));
115 return *this;
116}
117
118Diagnostic &Diagnostic::operator<<(StringAttr val) {
119 arguments.push_back(Elt: DiagnosticArgument(val));
120 return *this;
121}
122
123/// Stream in an OperationName.
124Diagnostic &Diagnostic::operator<<(OperationName val) {
125 // An OperationName is stored in the context, so we don't need to worry about
126 // the lifetime of its data.
127 arguments.push_back(Elt: DiagnosticArgument(val.getStringRef()));
128 return *this;
129}
130
131/// Adjusts operation printing flags used in diagnostics for the given severity
132/// level.
133static OpPrintingFlags adjustPrintingFlags(OpPrintingFlags flags,
134 DiagnosticSeverity severity) {
135 flags.useLocalScope();
136 flags.elideLargeElementsAttrs();
137 if (severity == DiagnosticSeverity::Error)
138 flags.printGenericOpForm();
139 return flags;
140}
141
142/// Stream in an Operation.
143Diagnostic &Diagnostic::operator<<(Operation &op) {
144 return appendOp(op, flags: OpPrintingFlags());
145}
146
147Diagnostic &Diagnostic::operator<<(OpWithFlags op) {
148 return appendOp(op&: *op.getOperation(), flags: op.flags());
149}
150
151Diagnostic &Diagnostic::appendOp(Operation &op, const OpPrintingFlags &flags) {
152 std::string str;
153 llvm::raw_string_ostream os(str);
154 op.print(os, flags: adjustPrintingFlags(flags, severity));
155 // Print on a new line for better readability if the op will be printed on
156 // multiple lines.
157 if (str.find(c: '\n') != std::string::npos)
158 *this << '\n';
159 return *this << str;
160}
161
162/// Stream in a Value.
163Diagnostic &Diagnostic::operator<<(Value val) {
164 std::string str;
165 llvm::raw_string_ostream os(str);
166 val.print(os, flags: adjustPrintingFlags(flags: OpPrintingFlags(), severity));
167 return *this << str;
168}
169
170/// Outputs this diagnostic to a stream.
171void Diagnostic::print(raw_ostream &os,
172 std::optional<int64_t> messagePartIndex) const {
173 if (!messagePartIndex.has_value()) {
174 for (auto &arg : getArguments())
175 arg.print(os);
176 return;
177 }
178
179 assert(0 <= *messagePartIndex &&
180 *messagePartIndex <= static_cast<int64_t>(messagePartEnds.size()));
181 size_t argumentStart =
182 *messagePartIndex == 0 ? 0 : messagePartEnds[*messagePartIndex - 1];
183 size_t argumentEnd =
184 *messagePartIndex == static_cast<int64_t>(messagePartEnds.size())
185 ? arguments.size()
186 : messagePartEnds[*messagePartIndex];
187 for (auto &arg :
188 getArguments().slice(N: argumentStart, M: argumentEnd - argumentStart))
189 arg.print(os);
190}
191
192/// Convert the diagnostic to a string.
193std::string Diagnostic::str() const {
194 std::string str;
195 llvm::raw_string_ostream os(str);
196 print(os);
197 return str;
198}
199
200/// Converts each message part to a separate string.
201SmallVector<std::string> Diagnostic::strs() const {
202 SmallVector<std::string> strs;
203 size_t numMessageParts = messagePartEnds.size();
204
205 // Include the current message part if there are no completed parts or if it
206 // contains arguments after the last completed part.
207 if (messagePartEnds.empty() || messagePartEnds.back() != arguments.size())
208 ++numMessageParts;
209 for (size_t i = 0; i < numMessageParts; ++i) {
210 std::string str;
211 llvm::raw_string_ostream os(str);
212 print(os, messagePartIndex: i);
213 strs.push_back(Elt: str);
214 }
215 return strs;
216}
217
218/// Attaches a note to this diagnostic. A new location may be optionally
219/// provided, if not, then the location defaults to the one specified for this
220/// diagnostic. Notes may not be attached to other notes.
221Diagnostic &Diagnostic::attachNote(std::optional<Location> noteLoc) {
222 // We don't allow attaching notes to notes.
223 assert(severity != DiagnosticSeverity::Note &&
224 "cannot attach a note to a note");
225
226 // If a location wasn't provided then reuse our location.
227 if (!noteLoc)
228 noteLoc = loc;
229
230 /// Append and return a new note.
231 notes.push_back(
232 Elt: std::make_unique<Diagnostic>(args&: *noteLoc, args: DiagnosticSeverity::Note));
233 return *notes.back();
234}
235
236/// Allow a diagnostic to be converted to 'failure'.
237Diagnostic::operator LogicalResult() const { return failure(); }
238
239/// Starts a new message part.
240void Diagnostic::startNewMessagePart() {
241 if (arguments.empty())
242 return;
243 if (!messagePartEnds.empty() && messagePartEnds.back() == arguments.size())
244 return;
245 messagePartEnds.push_back(Elt: arguments.size());
246}
247
248//===----------------------------------------------------------------------===//
249// InFlightDiagnostic
250//===----------------------------------------------------------------------===//
251
252/// Allow an inflight diagnostic to be converted to 'failure', otherwise
253/// 'success' if this is an empty diagnostic.
254InFlightDiagnostic::operator LogicalResult() const {
255 return failure(IsFailure: isActive());
256}
257
258/// Reports the diagnostic to the engine.
259void InFlightDiagnostic::report() {
260 // If this diagnostic is still inflight and it hasn't been abandoned, then
261 // report it.
262 if (isInFlight()) {
263 owner->emit(diag: std::move(*impl));
264 owner = nullptr;
265 }
266 impl.reset();
267}
268
269/// Abandons this diagnostic.
270void InFlightDiagnostic::abandon() { owner = nullptr; }
271
272//===----------------------------------------------------------------------===//
273// DiagnosticEngineImpl
274//===----------------------------------------------------------------------===//
275
276namespace mlir {
277namespace detail {
278struct DiagnosticEngineImpl {
279 /// Emit a diagnostic using the registered issue handle if present, or with
280 /// the default behavior if not.
281 void emit(Diagnostic &&diag);
282
283 /// A mutex to ensure that diagnostics emission is thread-safe.
284 llvm::sys::SmartMutex<true> mutex;
285
286 /// These are the handlers used to report diagnostics.
287 llvm::SmallMapVector<DiagnosticEngine::HandlerID, DiagnosticEngine::HandlerTy,
288 2>
289 handlers;
290
291 /// This is a unique identifier counter for diagnostic handlers in the
292 /// context. This id starts at 1 to allow for 0 to be used as a sentinel.
293 DiagnosticEngine::HandlerID uniqueHandlerId = 1;
294};
295} // namespace detail
296} // namespace mlir
297
298/// Emit a diagnostic using the registered issue handle if present, or with
299/// the default behavior if not.
300void DiagnosticEngineImpl::emit(Diagnostic &&diag) {
301 llvm::sys::SmartScopedLock<true> lock(mutex);
302
303 // Try to process the given diagnostic on one of the registered handlers.
304 // Handlers are walked in reverse order, so that the most recent handler is
305 // processed first.
306 for (auto &handlerIt : llvm::reverse(C&: handlers))
307 if (succeeded(Result: handlerIt.second(diag)))
308 return;
309
310 // Otherwise, if this is an error we emit it to stderr.
311 if (diag.getSeverity() != DiagnosticSeverity::Error)
312 return;
313
314 auto &os = llvm::errs();
315 if (!llvm::isa<UnknownLoc>(Val: diag.getLocation()))
316 os << diag.getLocation() << ": ";
317 os << "error: ";
318
319 // The default behavior for errors is to emit them to stderr.
320 os << diag << '\n';
321 os.flush();
322}
323
324//===----------------------------------------------------------------------===//
325// DiagnosticEngine
326//===----------------------------------------------------------------------===//
327
328DiagnosticEngine::DiagnosticEngine() : impl(new DiagnosticEngineImpl()) {}
329DiagnosticEngine::~DiagnosticEngine() = default;
330
331/// Register a new handler for diagnostics to the engine. This function returns
332/// a unique identifier for the registered handler, which can be used to
333/// unregister this handler at a later time.
334auto DiagnosticEngine::registerHandler(HandlerTy handler) -> HandlerID {
335 llvm::sys::SmartScopedLock<true> lock(impl->mutex);
336 auto uniqueID = impl->uniqueHandlerId++;
337 impl->handlers.insert(KV: {uniqueID, std::move(handler)});
338 return uniqueID;
339}
340
341/// Erase the registered diagnostic handler with the given identifier.
342void DiagnosticEngine::eraseHandler(HandlerID handlerID) {
343 llvm::sys::SmartScopedLock<true> lock(impl->mutex);
344 impl->handlers.erase(Key: handlerID);
345}
346
347/// Emit a diagnostic using the registered issue handler if present, or with
348/// the default behavior if not.
349void DiagnosticEngine::emit(Diagnostic &&diag) {
350 assert(diag.getSeverity() != DiagnosticSeverity::Note &&
351 "notes should not be emitted directly");
352 impl->emit(diag: std::move(diag));
353}
354
355/// Helper function used to emit a diagnostic with an optionally empty twine
356/// message. If the message is empty, then it is not inserted into the
357/// diagnostic.
358static InFlightDiagnostic
359emitDiag(Location location, DiagnosticSeverity severity, const Twine &message) {
360 MLIRContext *ctx = location->getContext();
361 auto &diagEngine = ctx->getDiagEngine();
362 auto diag = diagEngine.emit(loc: location, severity);
363 if (!message.isTriviallyEmpty())
364 diag << message;
365
366 // Add the stack trace as a note if necessary.
367 if (ctx->shouldPrintStackTraceOnDiagnostic()) {
368 std::string bt;
369 {
370 llvm::raw_string_ostream stream(bt);
371 llvm::sys::PrintStackTrace(OS&: stream);
372 }
373 if (!bt.empty())
374 diag.attachNote() << "diagnostic emitted with trace:\n" << bt;
375 }
376
377 return diag;
378}
379
380/// Emit an error message using this location.
381InFlightDiagnostic mlir::emitError(Location loc) { return emitError(loc, message: {}); }
382InFlightDiagnostic mlir::emitError(Location loc, const Twine &message) {
383 return emitDiag(location: loc, severity: DiagnosticSeverity::Error, message);
384}
385
386/// Emit a warning message using this location.
387InFlightDiagnostic mlir::emitWarning(Location loc) {
388 return emitWarning(loc, message: {});
389}
390InFlightDiagnostic mlir::emitWarning(Location loc, const Twine &message) {
391 return emitDiag(location: loc, severity: DiagnosticSeverity::Warning, message);
392}
393
394/// Emit a remark message using this location.
395InFlightDiagnostic mlir::emitRemark(Location loc) {
396 return emitRemark(loc, message: {});
397}
398InFlightDiagnostic mlir::emitRemark(Location loc, const Twine &message) {
399 return emitDiag(location: loc, severity: DiagnosticSeverity::Remark, message);
400}
401
402//===----------------------------------------------------------------------===//
403// ScopedDiagnosticHandler
404//===----------------------------------------------------------------------===//
405
406ScopedDiagnosticHandler::~ScopedDiagnosticHandler() {
407 if (handlerID)
408 ctx->getDiagEngine().eraseHandler(handlerID);
409}
410
411//===----------------------------------------------------------------------===//
412// SourceMgrDiagnosticHandler
413//===----------------------------------------------------------------------===//
414namespace mlir {
415namespace detail {
416struct SourceMgrDiagnosticHandlerImpl {
417 /// Return the SrcManager buffer id for the specified file, or zero if none
418 /// can be found.
419 unsigned getSourceMgrBufferIDForFile(llvm::SourceMgr &mgr,
420 StringRef filename) {
421 // Check for an existing mapping to the buffer id for this file.
422 auto bufferIt = filenameToBufId.find(Key: filename);
423 if (bufferIt != filenameToBufId.end())
424 return bufferIt->second;
425
426 // Look for a buffer in the manager that has this filename.
427 for (unsigned i = 1, e = mgr.getNumBuffers() + 1; i != e; ++i) {
428 auto *buf = mgr.getMemoryBuffer(i);
429 if (buf->getBufferIdentifier() == filename)
430 return filenameToBufId[filename] = i;
431 }
432
433 // Otherwise, try to load the source file.
434 std::string ignored;
435 unsigned id = mgr.AddIncludeFile(Filename: std::string(filename), IncludeLoc: SMLoc(), IncludedFile&: ignored);
436 filenameToBufId[filename] = id;
437 return id;
438 }
439
440 /// Mapping between file name and buffer ID's.
441 llvm::StringMap<unsigned> filenameToBufId;
442};
443} // namespace detail
444} // namespace mlir
445
446/// Return a processable CallSiteLoc from the given location.
447static std::optional<CallSiteLoc> getCallSiteLoc(Location loc) {
448 if (isa<NameLoc>(Val: loc))
449 return getCallSiteLoc(loc: cast<NameLoc>(Val&: loc).getChildLoc());
450 if (auto callLoc = dyn_cast<CallSiteLoc>(Val&: loc))
451 return callLoc;
452 if (isa<FusedLoc>(Val: loc)) {
453 for (auto subLoc : cast<FusedLoc>(Val&: loc).getLocations()) {
454 if (auto callLoc = getCallSiteLoc(loc: subLoc)) {
455 return callLoc;
456 }
457 }
458 return std::nullopt;
459 }
460 return std::nullopt;
461}
462
463/// Given a diagnostic kind, returns the LLVM DiagKind.
464static llvm::SourceMgr::DiagKind getDiagKind(DiagnosticSeverity kind) {
465 switch (kind) {
466 case DiagnosticSeverity::Note:
467 return llvm::SourceMgr::DK_Note;
468 case DiagnosticSeverity::Warning:
469 return llvm::SourceMgr::DK_Warning;
470 case DiagnosticSeverity::Error:
471 return llvm::SourceMgr::DK_Error;
472 case DiagnosticSeverity::Remark:
473 return llvm::SourceMgr::DK_Remark;
474 }
475 llvm_unreachable("Unknown DiagnosticSeverity");
476}
477
478SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(
479 llvm::SourceMgr &mgr, MLIRContext *ctx, raw_ostream &os,
480 ShouldShowLocFn &&shouldShowLocFn)
481 : ScopedDiagnosticHandler(ctx), mgr(mgr), os(os),
482 shouldShowLocFn(std::move(shouldShowLocFn)),
483 impl(new SourceMgrDiagnosticHandlerImpl()) {
484 setHandler([this](Diagnostic &diag) { emitDiagnostic(diag); });
485}
486
487SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(
488 llvm::SourceMgr &mgr, MLIRContext *ctx, ShouldShowLocFn &&shouldShowLocFn)
489 : SourceMgrDiagnosticHandler(mgr, ctx, llvm::errs(),
490 std::move(shouldShowLocFn)) {}
491
492SourceMgrDiagnosticHandler::~SourceMgrDiagnosticHandler() = default;
493
494void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
495 DiagnosticSeverity kind,
496 bool displaySourceLine) {
497 // Extract a file location from this loc.
498 auto fileLoc = loc->findInstanceOf<FileLineColLoc>();
499
500 // If one doesn't exist, then print the raw message without a source location.
501 if (!fileLoc) {
502 std::string str;
503 llvm::raw_string_ostream strOS(str);
504 if (!llvm::isa<UnknownLoc>(Val: loc))
505 strOS << loc << ": ";
506 strOS << message;
507 return mgr.PrintMessage(OS&: os, Loc: SMLoc(), Kind: getDiagKind(kind), Msg: str);
508 }
509
510 // Otherwise if we are displaying the source line, try to convert the file
511 // location to an SMLoc.
512 if (displaySourceLine) {
513 auto smloc = convertLocToSMLoc(loc: fileLoc);
514 if (smloc.isValid())
515 return mgr.PrintMessage(OS&: os, Loc: smloc, Kind: getDiagKind(kind), Msg: message);
516 }
517
518 // If the conversion was unsuccessful, create a diagnostic with the file
519 // information. We manually combine the line and column to avoid asserts in
520 // the constructor of SMDiagnostic that takes a location.
521 std::string locStr;
522 llvm::raw_string_ostream locOS(locStr);
523 locOS << fileLoc.getFilename().getValue() << ":" << fileLoc.getLine() << ":"
524 << fileLoc.getColumn();
525 llvm::SMDiagnostic diag(locStr, getDiagKind(kind), message.str());
526 diag.print(ProgName: nullptr, S&: os);
527}
528
529/// Emit the given diagnostic with the held source manager.
530void SourceMgrDiagnosticHandler::emitDiagnostic(Diagnostic &diag) {
531 SmallVector<std::pair<Location, StringRef>> locationStack;
532 auto addLocToStack = [&](Location loc, StringRef locContext) {
533 if (std::optional<Location> showableLoc = findLocToShow(loc))
534 locationStack.emplace_back(Args&: *showableLoc, Args&: locContext);
535 };
536
537 // Add locations to display for this diagnostic.
538 Location loc = diag.getLocation();
539 addLocToStack(loc, /*locContext=*/{});
540
541 // If the diagnostic location was a call site location, add the call stack as
542 // well.
543 if (auto callLoc = getCallSiteLoc(loc)) {
544 // Print the call stack while valid, or until the limit is reached.
545 loc = callLoc->getCaller();
546 for (unsigned curDepth = 0; curDepth < callStackLimit; ++curDepth) {
547 addLocToStack(loc, "called from");
548 if ((callLoc = getCallSiteLoc(loc)))
549 loc = callLoc->getCaller();
550 else
551 break;
552 }
553 }
554
555 // If the location stack is empty, use the initial location.
556 if (locationStack.empty()) {
557 for (const std::string &str : diag.strs())
558 emitDiagnostic(loc: diag.getLocation(), message: str, kind: diag.getSeverity());
559
560 // Otherwise, use the location stack.
561 } else {
562 for (const std::string &str : diag.strs())
563 emitDiagnostic(loc: locationStack.front().first, message: str, kind: diag.getSeverity());
564 for (auto &it : llvm::drop_begin(RangeOrContainer&: locationStack))
565 emitDiagnostic(loc: it.first, message: it.second, kind: DiagnosticSeverity::Note);
566 }
567
568 // Emit each of the notes. Only display the source code if the location is
569 // different from the previous location.
570 for (auto &note : diag.getNotes()) {
571 emitDiagnostic(loc: note.getLocation(), message: note.str(), kind: note.getSeverity(),
572 /*displaySourceLine=*/loc != note.getLocation());
573 loc = note.getLocation();
574 }
575}
576
577void SourceMgrDiagnosticHandler::setCallStackLimit(unsigned limit) {
578 callStackLimit = limit;
579}
580
581/// Get a memory buffer for the given file, or nullptr if one is not found.
582const llvm::MemoryBuffer *
583SourceMgrDiagnosticHandler::getBufferForFile(StringRef filename) {
584 if (unsigned id = impl->getSourceMgrBufferIDForFile(mgr, filename))
585 return mgr.getMemoryBuffer(i: id);
586 return nullptr;
587}
588
589std::optional<Location>
590SourceMgrDiagnosticHandler::findLocToShow(Location loc) {
591 if (!shouldShowLocFn)
592 return loc;
593 if (!shouldShowLocFn(loc))
594 return std::nullopt;
595
596 // Recurse into the child locations of some of location types.
597 return TypeSwitch<LocationAttr, std::optional<Location>>(loc)
598 .Case(caseFn: [&](CallSiteLoc callLoc) -> std::optional<Location> {
599 // We recurse into the callee of a call site, as the caller will be
600 // emitted in a different note on the main diagnostic.
601 return findLocToShow(loc: callLoc.getCallee());
602 })
603 .Case(caseFn: [&](FileLineColLoc) -> std::optional<Location> { return loc; })
604 .Case(caseFn: [&](FusedLoc fusedLoc) -> std::optional<Location> {
605 // Fused location is unique in that we try to find a sub-location to
606 // show, rather than the top-level location itself.
607 for (Location childLoc : fusedLoc.getLocations())
608 if (std::optional<Location> showableLoc = findLocToShow(loc: childLoc))
609 return showableLoc;
610 return std::nullopt;
611 })
612 .Case(caseFn: [&](NameLoc nameLoc) -> std::optional<Location> {
613 return findLocToShow(loc: nameLoc.getChildLoc());
614 })
615 .Case(caseFn: [&](OpaqueLoc opaqueLoc) -> std::optional<Location> {
616 // OpaqueLoc always falls back to a different source location.
617 return findLocToShow(loc: opaqueLoc.getFallbackLocation());
618 })
619 .Case(caseFn: [](UnknownLoc) -> std::optional<Location> {
620 // Prefer not to show unknown locations.
621 return std::nullopt;
622 });
623}
624
625/// Get a memory buffer for the given file, or the main file of the source
626/// manager if one doesn't exist. This always returns non-null.
627SMLoc SourceMgrDiagnosticHandler::convertLocToSMLoc(FileLineColLoc loc) {
628 // The column and line may be zero to represent unknown column and/or unknown
629 /// line/column information.
630 if (loc.getLine() == 0 || loc.getColumn() == 0)
631 return SMLoc();
632
633 unsigned bufferId = impl->getSourceMgrBufferIDForFile(mgr, filename: loc.getFilename());
634 if (!bufferId)
635 return SMLoc();
636 return mgr.FindLocForLineAndColumn(BufferID: bufferId, LineNo: loc.getLine(), ColNo: loc.getColumn());
637}
638
639//===----------------------------------------------------------------------===//
640// SourceMgrDiagnosticVerifierHandler
641//===----------------------------------------------------------------------===//
642
643namespace mlir {
644namespace detail {
645struct SourceMgrDiagnosticVerifierHandlerImpl {
646 SourceMgrDiagnosticVerifierHandlerImpl(
647 SourceMgrDiagnosticVerifierHandler::Level level)
648 : level(level) {}
649
650 /// The generic SourceMgr-based verifier core that this handler delegates
651 /// scanning and matching of 'expected-*' diagnostics to.
652 llvm::SourceMgrDiagnosticVerifier verifier;
653
654 /// Verification level.
655 SourceMgrDiagnosticVerifierHandler::Level level;
656};
657} // namespace detail
658} // namespace mlir
659
660/// Given a diagnostic kind, return a human readable string for it.
661static StringRef getDiagKindStr(DiagnosticSeverity kind) {
662 switch (kind) {
663 case DiagnosticSeverity::Note:
664 return "note";
665 case DiagnosticSeverity::Warning:
666 return "warning";
667 case DiagnosticSeverity::Error:
668 return "error";
669 case DiagnosticSeverity::Remark:
670 return "remark";
671 }
672 llvm_unreachable("Unknown DiagnosticSeverity");
673}
674
675SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
676 llvm::SourceMgr &srcMgr, MLIRContext *ctx, raw_ostream &out, Level level)
677 : SourceMgrDiagnosticHandler(srcMgr, ctx, out),
678 impl(new SourceMgrDiagnosticVerifierHandlerImpl(level)) {
679 // Compute the expected diagnostics for each of the current files in the
680 // source manager.
681 for (unsigned i = 0, e = mgr.getNumBuffers(); i != e; ++i)
682 (void)impl->verifier.computeExpectedDiags(os&: out, mgr,
683 buf: mgr.getMemoryBuffer(i: i + 1));
684
685 // The base class registered a handler that prints every diagnostic. The
686 // verifier takes its place: it consumes diagnostics and reports unexpected
687 // ones itself.
688 setHandler([this](Diagnostic &diag) { process(diag); });
689}
690
691SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
692 llvm::SourceMgr &srcMgr, MLIRContext *ctx, Level level)
693 : SourceMgrDiagnosticVerifierHandler(srcMgr, ctx, llvm::errs(), level) {}
694
695SourceMgrDiagnosticVerifierHandler::~SourceMgrDiagnosticVerifierHandler() {
696 // Ensure that all expected diagnostics were handled.
697 (void)verify();
698}
699
700/// Returns the status of the verifier and verifies that all expected
701/// diagnostics were emitted. This return success if all diagnostics were
702/// verified correctly, failure otherwise.
703LogicalResult SourceMgrDiagnosticVerifierHandler::verify() {
704 return success(IsSuccess: impl->verifier.verify(os, mgr));
705}
706
707std::unique_ptr<ScopedDiagnosticHandler>
708SourceMgrDiagnosticVerifierHandler::registerInContext(MLIRContext *ctx) {
709 return std::make_unique<ScopedDiagnosticHandler>(
710 args&: ctx, args: [this](Diagnostic &diag) { process(diag); });
711}
712
713/// Process a diagnostic and its notes.
714void SourceMgrDiagnosticVerifierHandler::process(Diagnostic &diag) {
715 for (const std::string &str : diag.strs())
716 process(loc: diag.getLocation(), msg: str, kind: diag.getSeverity());
717 for (auto &note : diag.getNotes())
718 process(diag&: note);
719}
720
721/// Process a diagnostic at a certain location.
722void SourceMgrDiagnosticVerifierHandler::process(LocationAttr loc,
723 StringRef msg,
724 DiagnosticSeverity kind) {
725 FileLineColLoc fileLoc = loc.findInstanceOf<FileLineColLoc>();
726 const llvm::MemoryBuffer *buf =
727 fileLoc ? getBufferForFile(filename: fileLoc.getFilename()) : nullptr;
728 bool reportUnexpected = impl->level != Level::OnlyExpected;
729
730 using MatchResult = llvm::SourceMgrDiagnosticVerifier::MatchResult;
731 MatchResult result = impl->verifier.process(
732 os, mgr, kind: getDiagKind(kind), /*hasLoc=*/static_cast<bool>(fileLoc), buf,
733 lineNo: fileLoc ? fileLoc.getLine() : 0, colNo: fileLoc ? fileLoc.getColumn() : 0, message: msg,
734 reportUnexpected);
735 if (result == MatchResult::Unexpected)
736 emitDiagnostic(loc, message: "unexpected " + getDiagKindStr(kind) + ": " + msg,
737 kind: DiagnosticSeverity::Error);
738}
739
740//===----------------------------------------------------------------------===//
741// ParallelDiagnosticHandler
742//===----------------------------------------------------------------------===//
743
744namespace mlir {
745namespace detail {
746/// Unique ids for parallel diagnostic handlers, so the thread-local slot
747/// below can name a handler without holding a raw pointer to it: a handler
748/// may be destroyed while a thread that skipped its erase still names it.
749static std::atomic<uint64_t> nextHandlerUniqueID{1};
750
751/// One entry of the thread-local nesting stack: the handler an element
752/// belongs to and its order id. Plain data so the thread-locals below stay
753/// trivially destructible.
754struct ThreadOrderEntry {
755 uint64_t handlerID;
756 size_t orderID;
757};
758
759/// How many nested parallel loops one thread tracks without a lock. Past
760/// this the handler's map is the overflow.
761constexpr unsigned kMaxThreadOrderEntries = 4;
762
763/// The order ids for the current thread, one entry per enclosing parallel
764/// loop, innermost last. A parallel loop sets and erases a thread's order id
765/// once per element, and doing that under the handler's mutex made the mutex
766/// the most contended lock of a loop over many small elements. A thread
767/// processes one element at a time for each handler, so a small stack covers
768/// every realistic nesting depth. Erases are LIFO, matching loop nesting.
769/// Reentrant use of the same handler on one thread remains unsupported, as
770/// before.
771static LLVM_THREAD_LOCAL ThreadOrderEntry
772 threadOrderStack[kMaxThreadOrderEntries];
773static LLVM_THREAD_LOCAL unsigned threadOrderDepth = 0;
774
775struct ParallelDiagnosticHandlerImpl : public llvm::PrettyStackTraceEntry {
776 struct ThreadDiagnostic {
777 ThreadDiagnostic(size_t id, Diagnostic diag)
778 : id(id), diag(std::move(diag)) {}
779 bool operator<(const ThreadDiagnostic &rhs) const { return id < rhs.id; }
780
781 /// The id for this diagnostic, this is used for ordering.
782 /// Note: This id corresponds to the ordered position of the current element
783 /// being processed by a given thread.
784 size_t id;
785
786 /// The diagnostic.
787 Diagnostic diag;
788 };
789
790 ParallelDiagnosticHandlerImpl(MLIRContext *ctx) : context(ctx) {
791 handlerID = ctx->getDiagEngine().registerHandler(handler: [this](Diagnostic &diag) {
792 // Fast path: scan the thread-local stack for this handler, no lock.
793 // Appending below still locks.
794 for (unsigned i = threadOrderDepth; i > 0; --i) {
795 if (threadOrderStack[i - 1].handlerID == handlerUniqueID) {
796 llvm::sys::SmartScopedLock<true> lock(mutex);
797 diagnostics.emplace_back(args&: threadOrderStack[i - 1].orderID,
798 args: std::move(diag));
799 return success();
800 }
801 }
802 // Slow path: nested past the stack, consult the map. If this
803 // thread is not tracked, return failure to let another handler
804 // process this diagnostic.
805 uint64_t tid = llvm::get_threadid();
806 llvm::sys::SmartScopedLock<true> lock(mutex);
807 auto it = threadToOrderID.find(Val: tid);
808 if (it == threadToOrderID.end())
809 return failure();
810 diagnostics.emplace_back(args&: it->second, args: std::move(diag));
811 return success();
812 });
813 }
814
815 ~ParallelDiagnosticHandlerImpl() override {
816 // Erase this handler from the context.
817 context->getDiagEngine().eraseHandler(handlerID);
818
819 // Early exit if there are no diagnostics, this is the common case.
820 if (diagnostics.empty())
821 return;
822
823 // Emit the diagnostics back to the context.
824 emitDiagnostics(emitFn: [&](Diagnostic &diag) {
825 return context->getDiagEngine().emit(diag: std::move(diag));
826 });
827 }
828
829 /// Utility method to emit any held diagnostics.
830 void emitDiagnostics(llvm::function_ref<void(Diagnostic &)> emitFn) const {
831 // Stable sort all of the diagnostics that were emitted. This creates a
832 // deterministic ordering for the diagnostics based upon which order id they
833 // were emitted for.
834 llvm::stable_sort(Range&: diagnostics);
835
836 // Emit each diagnostic to the context again.
837 for (ThreadDiagnostic &diag : diagnostics)
838 emitFn(diag.diag);
839 }
840
841 /// Set the order id for the current thread.
842 void setOrderIDForThread(size_t orderID) {
843 if (threadOrderDepth > 0 &&
844 threadOrderStack[threadOrderDepth - 1].handlerID == handlerUniqueID) {
845 threadOrderStack[threadOrderDepth - 1].orderID = orderID;
846 return;
847 }
848 if (threadOrderDepth < kMaxThreadOrderEntries) {
849 threadOrderStack[threadOrderDepth++] = {.handlerID: handlerUniqueID, .orderID: orderID};
850 return;
851 }
852 uint64_t tid = llvm::get_threadid();
853 llvm::sys::SmartScopedLock<true> lock(mutex);
854 threadToOrderID[tid] = orderID;
855 }
856
857 /// Remove the order id for the current thread.
858 void eraseOrderIDForThread() {
859 if (threadOrderDepth > 0 &&
860 threadOrderStack[threadOrderDepth - 1].handlerID == handlerUniqueID) {
861 --threadOrderDepth;
862 return;
863 }
864 uint64_t tid = llvm::get_threadid();
865 llvm::sys::SmartScopedLock<true> lock(mutex);
866 threadToOrderID.erase(Val: tid);
867 }
868
869 /// Dump the current diagnostics that were inflight.
870 void print(raw_ostream &os) const override {
871 // Early exit if there are no diagnostics, this is the common case.
872 if (diagnostics.empty())
873 return;
874
875 os << "In-Flight Diagnostics:\n";
876 emitDiagnostics(emitFn: [&](const Diagnostic &diag) {
877 os.indent(NumSpaces: 4);
878
879 // Print each diagnostic with the format:
880 // "<location>: <kind>: <msg>"
881 if (!llvm::isa<UnknownLoc>(Val: diag.getLocation()))
882 os << diag.getLocation() << ": ";
883 switch (diag.getSeverity()) {
884 case DiagnosticSeverity::Error:
885 os << "error: ";
886 break;
887 case DiagnosticSeverity::Warning:
888 os << "warning: ";
889 break;
890 case DiagnosticSeverity::Note:
891 os << "note: ";
892 break;
893 case DiagnosticSeverity::Remark:
894 os << "remark: ";
895 break;
896 }
897 os << diag << '\n';
898 });
899 }
900
901 /// A smart mutex to lock access to the internal state.
902 llvm::sys::SmartMutex<true> mutex;
903
904 /// Order ids, keyed by thread id, for threads nested past what the
905 /// thread-local stack holds: more enclosing parallel loops on one thread
906 /// than entries. Guarded by `mutex`. A thread in neither the stack nor
907 /// this map is not tracked, and its diagnostics pass on to the next
908 /// handler.
909 DenseMap<uint64_t, size_t> threadToOrderID;
910
911 /// An unordered list of diagnostics that were emitted.
912 mutable std::vector<ThreadDiagnostic> diagnostics;
913
914 /// The unique id for the parallel handler.
915 DiagnosticEngine::HandlerID handlerID = 0;
916
917 /// Unique id naming this handler in the thread-local order-id stack.
918 const uint64_t handlerUniqueID =
919 nextHandlerUniqueID.fetch_add(i: 1, m: std::memory_order_relaxed);
920
921 /// The context to emit the diagnostics to.
922 MLIRContext *context;
923};
924} // namespace detail
925} // namespace mlir
926
927ParallelDiagnosticHandler::ParallelDiagnosticHandler(MLIRContext *ctx)
928 : impl(new ParallelDiagnosticHandlerImpl(ctx)) {}
929ParallelDiagnosticHandler::~ParallelDiagnosticHandler() = default;
930
931/// Set the order id for the current thread.
932void ParallelDiagnosticHandler::setOrderIDForThread(size_t orderID) {
933 impl->setOrderIDForThread(orderID);
934}
935
936/// Remove the order id for the current thread. This removes the thread from
937/// diagnostics tracking.
938void ParallelDiagnosticHandler::eraseOrderIDForThread() {
939 impl->eraseOrderIDForThread();
940}
941