[Mlir-commits] [mlir] [mlir] Add metadata to Diagnostic. (PR #99398)
weiwei chen
llvmlistbot at llvm.org
Wed Jul 24 10:30:35 PDT 2024
https://github.com/weiweichen updated https://github.com/llvm/llvm-project/pull/99398
>From 662546872cd5f539bd932838c5f1edcda08d4091 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 17 Jul 2024 18:00:05 -0400
Subject: [PATCH 1/7] Add metadat to Diagnostic.
---
mlir/include/mlir/IR/Diagnostics.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index bb2e1bb183e9b..1ef902ae5439c 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -271,6 +271,9 @@ class Diagnostic {
return failure();
}
+ /// Returns the current list of diagnostic metadata.
+ SmallVector<DiagnosticArgument, 1>& getMetaData() { return metadata; }
+
private:
Diagnostic(const Diagnostic &rhs) = delete;
Diagnostic &operator=(const Diagnostic &rhs) = delete;
@@ -290,6 +293,9 @@ class Diagnostic {
/// A list of attached notes.
NoteVector notes;
+
+ /// A list of metadata attached to this Diagnostic.
+ SmallVector<DiagnosticArgument, 1> metadata;
};
inline raw_ostream &operator<<(raw_ostream &os, const Diagnostic &diag) {
>From 90d8968d99cf6a854cae4c55fa57160a99291f7a Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 19 Jul 2024 19:43:25 -0400
Subject: [PATCH 2/7] Apply review comment and format.
---
mlir/include/mlir/IR/Diagnostics.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index 1ef902ae5439c..f87e5b2170e68 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -272,7 +272,7 @@ class Diagnostic {
}
/// Returns the current list of diagnostic metadata.
- SmallVector<DiagnosticArgument, 1>& getMetaData() { return metadata; }
+ SmallVector<DiagnosticArgument, 0> &getMetaData() { return metadata; }
private:
Diagnostic(const Diagnostic &rhs) = delete;
@@ -295,7 +295,7 @@ class Diagnostic {
NoteVector notes;
/// A list of metadata attached to this Diagnostic.
- SmallVector<DiagnosticArgument, 1> metadata;
+ SmallVector<DiagnosticArgument, 0> metadata;
};
inline raw_ostream &operator<<(raw_ostream &os, const Diagnostic &diag) {
>From 0b33c7b50d26aa6e3d3827e4502e394e20f07355 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 19 Jul 2024 23:41:32 -0400
Subject: [PATCH 3/7] Add test for Diagnostic metadata.
---
mlir/include/mlir/IR/Diagnostics.h | 2 +-
mlir/test/lib/IR/CMakeLists.txt | 1 +
mlir/tools/mlir-opt/mlir-opt.cpp | 2 ++
3 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index f87e5b2170e68..cb30bb3f59688 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -272,7 +272,7 @@ class Diagnostic {
}
/// Returns the current list of diagnostic metadata.
- SmallVector<DiagnosticArgument, 0> &getMetaData() { return metadata; }
+ SmallVectorImpl<DiagnosticArgument> &getMetadata() { return metadata; }
private:
Diagnostic(const Diagnostic &rhs) = delete;
diff --git a/mlir/test/lib/IR/CMakeLists.txt b/mlir/test/lib/IR/CMakeLists.txt
index faaa3bb8db24c..01297ad0a1148 100644
--- a/mlir/test/lib/IR/CMakeLists.txt
+++ b/mlir/test/lib/IR/CMakeLists.txt
@@ -6,6 +6,7 @@ add_mlir_library(MLIRTestIR
TestBuiltinDistinctAttributes.cpp
TestClone.cpp
TestDiagnostics.cpp
+ TestDiagnosticsMetadata.cpp
TestDominance.cpp
TestFunc.cpp
TestInterfaces.cpp
diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp
index 149f9d59961b8..1695ad887470b 100644
--- a/mlir/tools/mlir-opt/mlir-opt.cpp
+++ b/mlir/tools/mlir-opt/mlir-opt.cpp
@@ -92,6 +92,7 @@ void registerTestDataLayoutQuery();
void registerTestDeadCodeAnalysisPass();
void registerTestDecomposeCallGraphTypes();
void registerTestDiagnosticsPass();
+void registerTestDiagnosticsMetadataPass();
void registerTestDominancePass();
void registerTestDynamicPipelinePass();
void registerTestEmulateNarrowTypePass();
@@ -225,6 +226,7 @@ void registerTestPasses() {
mlir::test::registerTestDeadCodeAnalysisPass();
mlir::test::registerTestDecomposeCallGraphTypes();
mlir::test::registerTestDiagnosticsPass();
+ mlir::test::registerTestDiagnosticsMetadataPass();
mlir::test::registerTestDominancePass();
mlir::test::registerTestDynamicPipelinePass();
mlir::test::registerTestEmulateNarrowTypePass();
>From b75c1dd016530308e135d48c00d7b9b9b675650d Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 19 Jul 2024 23:49:26 -0400
Subject: [PATCH 4/7] Actually add the tests.
---
mlir/test/IR/diagnostic-handler-metadata.mlir | 19 ++++++
mlir/test/lib/IR/TestDiagnosticsMetadata.cpp | 65 +++++++++++++++++++
2 files changed, 84 insertions(+)
create mode 100644 mlir/test/IR/diagnostic-handler-metadata.mlir
create mode 100644 mlir/test/lib/IR/TestDiagnosticsMetadata.cpp
diff --git a/mlir/test/IR/diagnostic-handler-metadata.mlir b/mlir/test/IR/diagnostic-handler-metadata.mlir
new file mode 100644
index 0000000000000..28267de1f760b
--- /dev/null
+++ b/mlir/test/IR/diagnostic-handler-metadata.mlir
@@ -0,0 +1,19 @@
+// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(test-diagnostic-metadata))" -o - 2>&1 | FileCheck %s
+// This test verifies that diagnostic handler can filter the diagnostic to emit the errors.
+
+// CHECK-LABEL: Test 'test'
+// CHECK-NEXT: 8:3: error: test diagnostic metadata
+// CHECK-NOT: 13:3: error: test diagnostic metadata
+func.func @test() {
+ "test.test_attr0"() {
+ // CHECK: attr = "emit_error"
+ attr = "emit_error"
+ } : () -> ()
+
+ "test.test_attr1"() {
+ // CHECK: attr = "do_not_emit_error"
+ attr = "do_not_emit_error"
+ } : () -> ()
+
+ return
+}
diff --git a/mlir/test/lib/IR/TestDiagnosticsMetadata.cpp b/mlir/test/lib/IR/TestDiagnosticsMetadata.cpp
new file mode 100644
index 0000000000000..5cb0193baa171
--- /dev/null
+++ b/mlir/test/lib/IR/TestDiagnosticsMetadata.cpp
@@ -0,0 +1,65 @@
+//===- TestDiagnosticsMetadata.cpp - Test Diagnostic Metatdata ------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains test passes for constructing and resolving dominance
+// information.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/IR/SymbolTable.h"
+#include "mlir/Pass/Pass.h"
+#include "llvm/Support/SourceMgr.h"
+
+using namespace mlir;
+
+namespace {
+struct TestDiagnosticMetadataPass
+ : public PassWrapper<TestDiagnosticMetadataPass,
+ InterfacePass<SymbolOpInterface>> {
+ MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDiagnosticMetadataPass)
+
+ StringRef getArgument() const final { return "test-diagnostic-metadata"; }
+ StringRef getDescription() const final { return "Test diagnostic metadata."; }
+ TestDiagnosticMetadataPass() = default;
+ TestDiagnosticMetadataPass(const TestDiagnosticMetadataPass &) {}
+
+ void runOnOperation() override {
+ llvm::errs() << "Test '" << getOperation().getName() << "'\n";
+
+ // Build a diagnostic handler that has filtering capabilities.
+ ScopedDiagnosticHandler handler(&getContext(), [](mlir::Diagnostic &diag) {
+ return mlir::success(
+ llvm::none_of(diag.getMetadata(), [](mlir::DiagnosticArgument &arg) {
+ return arg.getKind() == mlir::DiagnosticArgument::
+ DiagnosticArgumentKind::String &&
+ arg.getAsString().contains("hello");
+ }));
+ });
+
+ // Emit a diagnostic for every operation with a valid loc.
+ getOperation()->walk([&](Operation *op) {
+ if (StringAttr strAttr = op->getAttrOfType<StringAttr>("attr")) {
+ if (strAttr.getValue() == "emit_error")
+ emitError(op->getLoc(), "test diagnostic metadata")
+ .getUnderlyingDiagnostic()
+ ->getMetadata()
+ .push_back(DiagnosticArgument("hello"));
+ }
+ });
+ }
+};
+
+} // namespace
+
+namespace mlir {
+namespace test {
+void registerTestDiagnosticsMetadataPass() {
+ PassRegistration<TestDiagnosticMetadataPass>{};
+}
+} // namespace test
+} // namespace mlir
>From 5ddfd3a4caebdde97e719ec42272e62b0c2d4f3b Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 19 Jul 2024 23:50:32 -0400
Subject: [PATCH 5/7] Update comment.
---
mlir/test/IR/diagnostic-handler-metadata.mlir | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mlir/test/IR/diagnostic-handler-metadata.mlir b/mlir/test/IR/diagnostic-handler-metadata.mlir
index 28267de1f760b..5663aa4561593 100644
--- a/mlir/test/IR/diagnostic-handler-metadata.mlir
+++ b/mlir/test/IR/diagnostic-handler-metadata.mlir
@@ -1,5 +1,5 @@
// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(test-diagnostic-metadata))" -o - 2>&1 | FileCheck %s
-// This test verifies that diagnostic handler can filter the diagnostic to emit the errors.
+// This test verifies that diagnostic handler can filter the diagnostic whether to emit the errors.
// CHECK-LABEL: Test 'test'
// CHECK-NEXT: 8:3: error: test diagnostic metadata
>From f7dcc91347481070dd4aea36e25e60c0232f6b25 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 24 Jul 2024 12:54:40 -0400
Subject: [PATCH 6/7] Update test to use -verify-diagnostic.
---
mlir/test/IR/diagnostic-handler-metadata.mlir | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/mlir/test/IR/diagnostic-handler-metadata.mlir b/mlir/test/IR/diagnostic-handler-metadata.mlir
index 5663aa4561593..c089185cc3570 100644
--- a/mlir/test/IR/diagnostic-handler-metadata.mlir
+++ b/mlir/test/IR/diagnostic-handler-metadata.mlir
@@ -1,16 +1,16 @@
-// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(test-diagnostic-metadata))" -o - 2>&1 | FileCheck %s
-// This test verifies that diagnostic handler can filter the diagnostic whether to emit the errors.
+// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(test-diagnostic-metadata))" -verify-diagnostics -o - 2>&1 | FileCheck %s
+// COM: This test verifies that diagnostic handler can filter the diagnostic based on its metadata
+// COM: whether to emit the errors.
// CHECK-LABEL: Test 'test'
-// CHECK-NEXT: 8:3: error: test diagnostic metadata
-// CHECK-NOT: 13:3: error: test diagnostic metadata
func.func @test() {
- "test.test_attr0"() {
+ // expected-error @+1 {{test diagnostic metadata}}
+ "test.emit_error"() {
// CHECK: attr = "emit_error"
attr = "emit_error"
} : () -> ()
- "test.test_attr1"() {
+ "test.do_not_emit_error"() {
// CHECK: attr = "do_not_emit_error"
attr = "do_not_emit_error"
} : () -> ()
>From efc2836b97dc889f0b580f4b13d62e894e3970d7 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 24 Jul 2024 13:30:01 -0400
Subject: [PATCH 7/7] Add some documentation about diagnostic metadata.
---
mlir/docs/Diagnostics.md | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/mlir/docs/Diagnostics.md b/mlir/docs/Diagnostics.md
index 82bc61dd8c3ad..a6d59bdecbdd8 100644
--- a/mlir/docs/Diagnostics.md
+++ b/mlir/docs/Diagnostics.md
@@ -77,7 +77,7 @@ InFlightDiagnostic Operation::emitOpError();
## Diagnostic
A `Diagnostic` in MLIR contains all of the necessary information for reporting a
-message to the user. A `Diagnostic` essentially boils down to three main
+message to the user. A `Diagnostic` essentially boils down to four main
components:
* [Source Location](#source-locations)
@@ -85,6 +85,11 @@ components:
- Error, Note, Remark, Warning
* Diagnostic Arguments
- The diagnostic arguments are used when constructing the output message.
+* Metadata
+ - Some additional information attached that can be used to identify
+ this diagnostic other than source location and severity level
+ (e.g. for diagnostic handlers to do some filtering).
+ Metadata is not part of the output message.
### Appending arguments
@@ -143,6 +148,11 @@ op->emitError("...").attachNote(noteLoc) << "...";
op->emitError("...").attachNote() << "...";
```
+### Managing Metadata
+Metadata is a mutable vector of DiagnosticArguments.
+It can be accessed and modified as a vector.
+
+
## InFlight Diagnostic
Now that [Diagnostics](#diagnostic) have been explained, we introduce the
More information about the Mlir-commits
mailing list