Skip to content

Commit 431c8dd

Browse files
[mlir][IR] Add support for UnknownLoc to verify-diagnostics (#134421)
Diagnostics at unknown locations can now be verified with `-verify-diagnostics`. Example: ``` // expected-error@unknown {{something went wrong}} ``` Also clean up some MemRefToLLVM conversion tests that had to redirect all errors to stdout in order to FileCheck them. All of those tests can now be stored in a single `invalid.mlir`. That was not possible before.
1 parent 65c7ea7 commit 431c8dd

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

mlir/include/mlir/IR/Diagnostics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ class SourceMgrDiagnosticVerifierHandler : public SourceMgrDiagnosticHandler {
640640
/// Process a single diagnostic.
641641
void process(Diagnostic &diag);
642642

643-
/// Process a FileLineColLoc diagnostic.
644-
void process(FileLineColLoc loc, StringRef msg, DiagnosticSeverity kind);
643+
/// Process a LocationAttr diagnostic.
644+
void process(LocationAttr loc, StringRef msg, DiagnosticSeverity kind);
645645

646646
std::unique_ptr<detail::SourceMgrDiagnosticVerifierHandlerImpl> impl;
647647
};

mlir/lib/IR/Diagnostics.cpp

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,13 @@ struct SourceMgrDiagnosticVerifierHandlerImpl {
678678
/// A list of expected diagnostics for each buffer of the source manager.
679679
llvm::StringMap<SmallVector<ExpectedDiag, 2>> expectedDiagsPerFile;
680680

681+
/// A list of expected diagnostics with unknown locations.
682+
SmallVector<ExpectedDiag, 2> expectedUnknownLocDiags;
683+
681684
/// Regex to match the expected diagnostics format.
682685
llvm::Regex expected =
683686
llvm::Regex("expected-(error|note|remark|warning)(-re)? "
684-
"*(@([+-][0-9]+|above|below))? *{{(.*)}}$");
687+
"*(@([+-][0-9]+|above|below|unknown))? *{{(.*)}}$");
685688
};
686689
} // namespace detail
687690
} // namespace mlir
@@ -774,6 +777,11 @@ SourceMgrDiagnosticVerifierHandlerImpl::computeExpectedDiags(
774777
record.lineNo += offset;
775778
else
776779
record.lineNo -= offset;
780+
} else if (offsetMatch.consume_front("unknown")) {
781+
// This is matching unknown locations.
782+
record.fileLoc = SMLoc();
783+
expectedUnknownLocDiags.emplace_back(std::move(record));
784+
continue;
777785
} else if (offsetMatch.consume_front("above")) {
778786
// If the designator applies 'above' we add it to the last non
779787
// designator line.
@@ -828,53 +836,57 @@ SourceMgrDiagnosticVerifierHandler::~SourceMgrDiagnosticVerifierHandler() {
828836
/// verified correctly, failure otherwise.
829837
LogicalResult SourceMgrDiagnosticVerifierHandler::verify() {
830838
// Verify that all expected errors were seen.
831-
for (auto &expectedDiagsPair : impl->expectedDiagsPerFile) {
832-
for (auto &err : expectedDiagsPair.second) {
833-
if (err.matched)
834-
continue;
839+
auto checkExpectedDiags = [&](ExpectedDiag &err) {
840+
if (!err.matched)
835841
impl->status =
836842
err.emitError(os, mgr,
837843
"expected " + getDiagKindStr(err.kind) + " \"" +
838844
err.substring + "\" was not produced");
839-
}
840-
}
845+
};
846+
for (auto &expectedDiagsPair : impl->expectedDiagsPerFile)
847+
for (auto &err : expectedDiagsPair.second)
848+
checkExpectedDiags(err);
849+
for (auto &err : impl->expectedUnknownLocDiags)
850+
checkExpectedDiags(err);
841851
impl->expectedDiagsPerFile.clear();
842852
return impl->status;
843853
}
844854

845855
/// Process a single diagnostic.
846856
void SourceMgrDiagnosticVerifierHandler::process(Diagnostic &diag) {
847-
auto kind = diag.getSeverity();
848-
849-
// Process a FileLineColLoc.
850-
if (auto fileLoc = diag.getLocation()->findInstanceOf<FileLineColLoc>())
851-
return process(fileLoc, diag.str(), kind);
852-
853-
emitDiagnostic(diag.getLocation(),
854-
"unexpected " + getDiagKindStr(kind) + ": " + diag.str(),
855-
DiagnosticSeverity::Error);
856-
impl->status = failure();
857+
return process(diag.getLocation(), diag.str(), diag.getSeverity());
857858
}
858859

859-
/// Process a FileLineColLoc diagnostic.
860-
void SourceMgrDiagnosticVerifierHandler::process(FileLineColLoc loc,
860+
/// Process a diagnostic at a certain location.
861+
void SourceMgrDiagnosticVerifierHandler::process(LocationAttr loc,
861862
StringRef msg,
862863
DiagnosticSeverity kind) {
863-
// Get the expected diagnostics for this file.
864-
auto diags = impl->getExpectedDiags(loc.getFilename());
865-
if (!diags) {
866-
diags = impl->computeExpectedDiags(os, mgr,
867-
getBufferForFile(loc.getFilename()));
864+
FileLineColLoc fileLoc = loc.findInstanceOf<FileLineColLoc>();
865+
MutableArrayRef<ExpectedDiag> diags;
866+
867+
if (fileLoc) {
868+
// Get the expected diagnostics for this file.
869+
if (auto maybeDiags = impl->getExpectedDiags(fileLoc.getFilename())) {
870+
diags = *maybeDiags;
871+
} else {
872+
diags = impl->computeExpectedDiags(
873+
os, mgr, getBufferForFile(fileLoc.getFilename()));
874+
}
875+
} else {
876+
// Get all expected diagnostics at unknown locations.
877+
diags = impl->expectedUnknownLocDiags;
868878
}
869879

870880
// Search for a matching expected diagnostic.
871881
// If we find something that is close then emit a more specific error.
872882
ExpectedDiag *nearMiss = nullptr;
873883

874884
// If this was an expected error, remember that we saw it and return.
875-
unsigned line = loc.getLine();
876-
for (auto &e : *diags) {
877-
if (line == e.lineNo && e.match(msg)) {
885+
for (auto &e : diags) {
886+
// File line must match (unless it's an unknown location).
887+
if (fileLoc && fileLoc.getLine() != e.lineNo)
888+
continue;
889+
if (e.match(msg)) {
878890
if (e.kind == kind) {
879891
e.matched = true;
880892
return;

mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1-
// RUN: mlir-opt %s -finalize-memref-to-llvm 2>&1 | FileCheck %s
2-
// Since the error is at an unknown location, we use FileCheck instead of
3-
// -verify-diagnostics here
1+
// RUN: mlir-opt %s -finalize-memref-to-llvm -split-input-file -verify-diagnostics | FileCheck %s
42

5-
// CHECK: redefinition of reserved function 'malloc' of different type '!llvm.func<void (i64)>' is prohibited
3+
// expected-error@+1{{redefinition of reserved function 'malloc' of different type '!llvm.func<void (i64)>' is prohibited}}
64
llvm.func @malloc(i64)
75
func.func @redef_reserved() {
86
%alloc = memref.alloc() : memref<1024x64xf32, 1>
97
llvm.return
108
}
119

12-
// CHECK: conversion of memref memory space "foo" to integer address space failed. Consider adding memory space conversions.
10+
// -----
11+
12+
// expected-error@unknown{{conversion of memref memory space "foo" to integer address space failed. Consider adding memory space conversions.}}
1313
// CHECK-LABEL: @bad_address_space
1414
func.func @bad_address_space(%a: memref<2xindex, "foo">) {
1515
%c0 = arith.constant 0 : index
1616
// CHECK: memref.store
1717
memref.store %c0, %a[%c0] : memref<2xindex, "foo">
1818
return
1919
}
20+
21+
// -----
22+
23+
// CHECK-LABEL: @invalid_int_conversion
24+
func.func @invalid_int_conversion() {
25+
// expected-error@+1 {{conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.}}
26+
%alloc = memref.alloc() {alignment = 64 : i64} : memref<10xf32, 1 : ui64>
27+
return
28+
}
29+
30+
// -----
31+
32+
// expected-error@unknown{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
33+
// CHECK-LABEL: @issue_70160
34+
func.func @issue_70160() {
35+
// expected-error@+1{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
36+
%alloc = memref.alloc() : memref<1x32x33xi32, #gpu.address_space<workgroup>>
37+
%alloc1 = memref.alloc() : memref<i32>
38+
%c0 = arith.constant 0 : index
39+
// CHECK: memref.load
40+
%0 = memref.load %alloc[%c0, %c0, %c0] : memref<1x32x33xi32, #gpu.address_space<workgroup>>
41+
memref.store %0, %alloc1[] : memref<i32>
42+
func.return
43+
}

mlir/test/Conversion/MemRefToLLVM/issue-70160.mlir

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)