[Mlir-commits] [mlir] 65aedd3 - [mlir][python] Fix issue in diagnostic note initialization

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Jul 22 13:56:19 PDT 2022


Author: rkayaith
Date: 2022-07-22T16:56:14-04:00
New Revision: 65aedd338c1edd1ebeee709268d507df7c592e6a

URL: https://github.com/llvm/llvm-project/commit/65aedd338c1edd1ebeee709268d507df7c592e6a
DIFF: https://github.com/llvm/llvm-project/commit/65aedd338c1edd1ebeee709268d507df7c592e6a.diff

LOG: [mlir][python] Fix issue in diagnostic note initialization

Previously the elements of the notes tuple would be invalid objects when
accessed from a diagnostic handler, resulting in a segfault when used.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D129943

Added: 
    

Modified: 
    mlir/lib/Bindings/Python/IRCore.cpp
    mlir/test/python/ir/diagnostic_handler.py

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index fea26ec661a60..beb0c6cb9f72b 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -785,8 +785,7 @@ py::tuple PyDiagnostic::getNotes() {
   materializedNotes = py::tuple(numNotes);
   for (intptr_t i = 0; i < numNotes; ++i) {
     MlirDiagnostic noteDiag = mlirDiagnosticGetNote(diagnostic, i);
-    py::object pyNoteDiag = py::cast(PyDiagnostic(noteDiag));
-    PyTuple_SET_ITEM(materializedNotes->ptr(), i, pyNoteDiag.ptr());
+    materializedNotes.value()[i] = PyDiagnostic(noteDiag);
   }
   return *materializedNotes;
 }

diff  --git a/mlir/test/python/ir/diagnostic_handler.py b/mlir/test/python/ir/diagnostic_handler.py
index f38187a6f3be2..d973db24c366b 100644
--- a/mlir/test/python/ir/diagnostic_handler.py
+++ b/mlir/test/python/ir/diagnostic_handler.py
@@ -85,6 +85,23 @@ def callback(d):
   assert not handler.had_error
 
 
+# CHECK-LABEL: TEST: testDiagnosticNonEmptyNotes
+ at run
+def testDiagnosticNonEmptyNotes():
+  ctx = Context()
+  def callback(d):
+    # CHECK: DIAGNOSTIC:
+    # CHECK:   message='arith.addi' op requires one result
+    # CHECK:   notes=['see current operation: "arith.addi"() : () -> ()']
+    print(f"DIAGNOSTIC:")
+    print(f"  message={d.message}")
+    print(f"  notes={list(map(str, d.notes))}")
+    return True
+  handler = ctx.attach_diagnostic_handler(callback)
+  loc = Location.unknown(ctx)
+  Operation.create('arith.addi', loc=loc).verify()
+  assert not handler.had_error
+
 # CHECK-LABEL: TEST: testDiagnosticCallbackException
 @run
 def testDiagnosticCallbackException():


        


More information about the Mlir-commits mailing list