[Mlir-commits] [mlir] [mlir-c] Add mlirEmitWarning and mlirEmitRemark (PR #206542)

Maksim Levental llvmlistbot at llvm.org
Tue Sep 22 15:51:29 PDT 2026


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/206542

>From 151819abb9c812cc05421c532f42842330f994ea Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Mon, 29 Jun 2026 10:27:50 -0700
Subject: [PATCH 1/2] [mlir-c] Add mlirEmitWarning and mlirEmitRemark

---
 mlir/include/mlir-c/Diagnostics.h | 10 ++++++++++
 mlir/lib/CAPI/IR/Diagnostics.cpp  |  8 ++++++++
 mlir/test/CAPI/ir.c               |  8 ++++++++
 3 files changed, 26 insertions(+)

diff --git a/mlir/include/mlir-c/Diagnostics.h b/mlir/include/mlir-c/Diagnostics.h
index 884c67942ef659..87c4fa59f615a4 100644
--- a/mlir/include/mlir-c/Diagnostics.h
+++ b/mlir/include/mlir-c/Diagnostics.h
@@ -94,6 +94,16 @@ mlirContextDetachDiagnosticHandler(MlirContext context,
 MLIR_CAPI_EXPORTED void mlirEmitError(MlirLocation location,
                                       const char *message);
 
+/// Emits a warning at the given location through the diagnostics engine. Used
+/// for testing purposes.
+MLIR_CAPI_EXPORTED void mlirEmitWarning(MlirLocation location,
+                                        const char *message);
+
+/// Emits a remark at the given location through the diagnostics engine. Used
+/// for testing purposes.
+MLIR_CAPI_EXPORTED void mlirEmitRemark(MlirLocation location,
+                                       const char *message);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mlir/lib/CAPI/IR/Diagnostics.cpp b/mlir/lib/CAPI/IR/Diagnostics.cpp
index 4a13ae57611d8e..eee4427b488d55 100644
--- a/mlir/lib/CAPI/IR/Diagnostics.cpp
+++ b/mlir/lib/CAPI/IR/Diagnostics.cpp
@@ -78,3 +78,11 @@ void mlirContextDetachDiagnosticHandler(MlirContext context,
 void mlirEmitError(MlirLocation location, const char *message) {
   emitError(unwrap(location)) << message;
 }
+
+void mlirEmitWarning(MlirLocation location, const char *message) {
+  emitWarning(unwrap(location)) << message;
+}
+
+void mlirEmitRemark(MlirLocation location, const char *message) {
+  emitRemark(unwrap(location)) << message;
+}
diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index 57ae8b9a2819b0..4a98685b1fa37d 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -2425,6 +2425,8 @@ void testDiagnostics(void) {
   MlirAttribute nullAttr = {0};
   MlirLocation fusedLoc = mlirLocationFusedGet(ctx, 2, locs, nullAttr);
   mlirEmitError(fusedLoc, "test diagnostics");
+  mlirEmitWarning(unknownLoc, "test warning");
+  mlirEmitRemark(unknownLoc, "test remark");
   mlirContextDetachDiagnosticHandler(ctx, id);
   mlirEmitError(unknownLoc, "more test diagnostics");
   // CHECK-LABEL: @test_diagnostics
@@ -2454,6 +2456,12 @@ void testDiagnostics(void) {
   // CHECK: processing diagnostic (userData: 42) <<
   // CHECK:   test diagnostics
   // CHECK:   loc(fused["named", callsite("other-file.c":2:3 at "file.c":1:2)])
+  // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   test warning
+  // CHECK:   loc(unknown)
+  // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   test remark
+  // CHECK:   loc(unknown)
   // CHECK: deleting user data (userData: 42)
   // CHECK-NOT: processing diagnostic
   // CHECK:     more test diagnostics

>From c6ed357f4a16a93f506e2faa08034248589f54b0 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim at Maksims-MacBook-Pro.local>
Date: Tue, 22 Sep 2026 15:51:11 -0700
Subject: [PATCH 2/2] [mlir-c] Assert diagnostic severity in CAPI test

Print the diagnostic severity in the test handler and add CHECK lines
asserting Error/Warning/Remark, so the warning/remark tests no longer
pass vacuously. Also add the missing end-of-diagnostic CHECKs.
---
 mlir/test/CAPI/ir.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index 4a98685b1fa37d..b4855ae2e30e69 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -2051,6 +2051,20 @@ int testClone(void) {
 MlirLogicalResult errorHandler(MlirDiagnostic diagnostic, void *userData) {
   fprintf(stderr, "processing diagnostic (userData: %" PRIdPTR ") <<\n",
           (intptr_t)userData);
+  switch (mlirDiagnosticGetSeverity(diagnostic)) {
+  case MlirDiagnosticError:
+    fprintf(stderr, "severity: Error\n");
+    break;
+  case MlirDiagnosticWarning:
+    fprintf(stderr, "severity: Warning\n");
+    break;
+  case MlirDiagnosticNote:
+    fprintf(stderr, "severity: Note\n");
+    break;
+  case MlirDiagnosticRemark:
+    fprintf(stderr, "severity: Remark\n");
+    break;
+  }
   mlirDiagnosticPrint(diagnostic, printToStderr, NULL);
   fprintf(stderr, "\n");
   MlirLocation loc = mlirDiagnosticGetLocation(diagnostic);
@@ -2431,37 +2445,49 @@ void testDiagnostics(void) {
   mlirEmitError(unknownLoc, "more test diagnostics");
   // CHECK-LABEL: @test_diagnostics
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test diagnostics
   // CHECK:   loc(unknown)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test clone
   // CHECK:   loc(unknown)
   // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test diagnostics
   // CHECK:   loc("file.c":1:2)
   // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test diagnostics
   // CHECK:   loc("other-file.c":1:2 to 3:4)
   // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test diagnostics
   // CHECK:   loc(callsite("other-file.c":2:3 at "file.c":1:2))
   // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test diagnostics
   // CHECK:   loc("named")
   // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Error
   // CHECK:   test diagnostics
   // CHECK:   loc(fused["named", callsite("other-file.c":2:3 at "file.c":1:2)])
+  // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Warning
   // CHECK:   test warning
   // CHECK:   loc(unknown)
+  // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   severity: Remark
   // CHECK:   test remark
   // CHECK:   loc(unknown)
+  // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: deleting user data (userData: 42)
   // CHECK-NOT: processing diagnostic
   // CHECK:     more test diagnostics



More information about the Mlir-commits mailing list