Skip to content

[mlir][IR] Add support for UnknownLoc to verify-diagnostics #134421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mlir/include/mlir/IR/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ class SourceMgrDiagnosticVerifierHandler : public SourceMgrDiagnosticHandler {
/// Process a single diagnostic.
void process(Diagnostic &diag);

/// Process a FileLineColLoc diagnostic.
void process(FileLineColLoc loc, StringRef msg, DiagnosticSeverity kind);
/// Process a LocationAttr diagnostic.
void process(LocationAttr loc, StringRef msg, DiagnosticSeverity kind);

std::unique_ptr<detail::SourceMgrDiagnosticVerifierHandlerImpl> impl;
};
Expand Down
66 changes: 39 additions & 27 deletions mlir/lib/IR/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,13 @@ struct SourceMgrDiagnosticVerifierHandlerImpl {
/// A list of expected diagnostics for each buffer of the source manager.
llvm::StringMap<SmallVector<ExpectedDiag, 2>> expectedDiagsPerFile;

/// A list of expected diagnostics with unknown locations.
SmallVector<ExpectedDiag, 2> expectedUnknownLocDiags;

/// Regex to match the expected diagnostics format.
llvm::Regex expected =
llvm::Regex("expected-(error|note|remark|warning)(-re)? "
"*(@([+-][0-9]+|above|below))? *{{(.*)}}$");
"*(@([+-][0-9]+|above|below|unknown))? *{{(.*)}}$");
};
} // namespace detail
} // namespace mlir
Expand Down Expand Up @@ -774,6 +777,11 @@ SourceMgrDiagnosticVerifierHandlerImpl::computeExpectedDiags(
record.lineNo += offset;
else
record.lineNo -= offset;
} else if (offsetMatch.consume_front("unknown")) {
// This is matching unknown locations.
record.fileLoc = SMLoc();
expectedUnknownLocDiags.emplace_back(std::move(record));
continue;
} else if (offsetMatch.consume_front("above")) {
// If the designator applies 'above' we add it to the last non
// designator line.
Expand Down Expand Up @@ -828,53 +836,57 @@ SourceMgrDiagnosticVerifierHandler::~SourceMgrDiagnosticVerifierHandler() {
/// verified correctly, failure otherwise.
LogicalResult SourceMgrDiagnosticVerifierHandler::verify() {
// Verify that all expected errors were seen.
for (auto &expectedDiagsPair : impl->expectedDiagsPerFile) {
for (auto &err : expectedDiagsPair.second) {
if (err.matched)
continue;
auto checkExpectedDiags = [&](ExpectedDiag &err) {
if (!err.matched)
impl->status =
err.emitError(os, mgr,
"expected " + getDiagKindStr(err.kind) + " \"" +
err.substring + "\" was not produced");
}
}
};
for (auto &expectedDiagsPair : impl->expectedDiagsPerFile)
for (auto &err : expectedDiagsPair.second)
checkExpectedDiags(err);
for (auto &err : impl->expectedUnknownLocDiags)
checkExpectedDiags(err);
impl->expectedDiagsPerFile.clear();
return impl->status;
}

/// Process a single diagnostic.
void SourceMgrDiagnosticVerifierHandler::process(Diagnostic &diag) {
auto kind = diag.getSeverity();

// Process a FileLineColLoc.
if (auto fileLoc = diag.getLocation()->findInstanceOf<FileLineColLoc>())
return process(fileLoc, diag.str(), kind);

emitDiagnostic(diag.getLocation(),
"unexpected " + getDiagKindStr(kind) + ": " + diag.str(),
DiagnosticSeverity::Error);
impl->status = failure();
return process(diag.getLocation(), diag.str(), diag.getSeverity());
}

/// Process a FileLineColLoc diagnostic.
void SourceMgrDiagnosticVerifierHandler::process(FileLineColLoc loc,
/// Process a diagnostic at a certain location.
void SourceMgrDiagnosticVerifierHandler::process(LocationAttr loc,
StringRef msg,
DiagnosticSeverity kind) {
// Get the expected diagnostics for this file.
auto diags = impl->getExpectedDiags(loc.getFilename());
if (!diags) {
diags = impl->computeExpectedDiags(os, mgr,
getBufferForFile(loc.getFilename()));
FileLineColLoc fileLoc = loc.findInstanceOf<FileLineColLoc>();
MutableArrayRef<ExpectedDiag> diags;

if (fileLoc) {
// Get the expected diagnostics for this file.
if (auto maybeDiags = impl->getExpectedDiags(fileLoc.getFilename())) {
diags = *maybeDiags;
} else {
diags = impl->computeExpectedDiags(
os, mgr, getBufferForFile(fileLoc.getFilename()));
}
} else {
// Get all expected diagnostics at unknown locations.
diags = impl->expectedUnknownLocDiags;
}

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

// If this was an expected error, remember that we saw it and return.
unsigned line = loc.getLine();
for (auto &e : *diags) {
if (line == e.lineNo && e.match(msg)) {
for (auto &e : diags) {
// File line must match (unless it's an unknown location).
if (fileLoc && fileLoc.getLine() != e.lineNo)
continue;
if (e.match(msg)) {
if (e.kind == kind) {
e.matched = true;
return;
Expand Down
8 changes: 0 additions & 8 deletions mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir

This file was deleted.

34 changes: 29 additions & 5 deletions mlir/test/Conversion/MemRefToLLVM/invalid.mlir
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
// RUN: mlir-opt %s -finalize-memref-to-llvm 2>&1 | FileCheck %s
// Since the error is at an unknown location, we use FileCheck instead of
// -verify-diagnostics here
// RUN: mlir-opt %s -finalize-memref-to-llvm -split-input-file -verify-diagnostics | FileCheck %s

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

// CHECK: conversion of memref memory space "foo" to integer address space failed. Consider adding memory space conversions.
// -----

// expected-error@unknown{{conversion of memref memory space "foo" to integer address space failed. Consider adding memory space conversions.}}
// CHECK-LABEL: @bad_address_space
func.func @bad_address_space(%a: memref<2xindex, "foo">) {
%c0 = arith.constant 0 : index
// CHECK: memref.store
memref.store %c0, %a[%c0] : memref<2xindex, "foo">
return
}

// -----

// CHECK-LABEL: @invalid_int_conversion
func.func @invalid_int_conversion() {
// expected-error@+1 {{conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.}}
%alloc = memref.alloc() {alignment = 64 : i64} : memref<10xf32, 1 : ui64>
return
}

// -----

// expected-error@unknown{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
// CHECK-LABEL: @issue_70160
func.func @issue_70160() {
// expected-error@+1{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
%alloc = memref.alloc() : memref<1x32x33xi32, #gpu.address_space<workgroup>>
%alloc1 = memref.alloc() : memref<i32>
%c0 = arith.constant 0 : index
// CHECK: memref.load
%0 = memref.load %alloc[%c0, %c0, %c0] : memref<1x32x33xi32, #gpu.address_space<workgroup>>
memref.store %0, %alloc1[] : memref<i32>
func.return
}
15 changes: 0 additions & 15 deletions mlir/test/Conversion/MemRefToLLVM/issue-70160.mlir

This file was deleted.