[Mlir-commits] [mlir] [mlir][Parser] Fix crash after block parsing failure (PR #128011)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Feb 20 06:48:56 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: Matthias Springer (matthias-springer)

<details>
<summary>Changes</summary>

Fix a crash when parsing malformed block that defines values that preceding operations refer to (which would be a dominance error).

Previously: Producer multiple error messages and then crashes as follows:
```
LLVM ERROR: operation destroyed but still has uses
```

Now: Report an error that the block is malformed. No crash.


---
Full diff: https://github.com/llvm/llvm-project/pull/128011.diff


2 Files Affected:

- (modified) mlir/lib/AsmParser/Parser.cpp (+4) 
- (modified) mlir/test/IR/invalid.mlir (+14) 


``````````diff
diff --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp
index b5f1d2e27c9ba..8c18f3a751394 100644
--- a/mlir/lib/AsmParser/Parser.cpp
+++ b/mlir/lib/AsmParser/Parser.cpp
@@ -2228,6 +2228,10 @@ ParseResult OperationParser::parseRegionBody(Region &region, SMLoc startLoc,
 
   // Parse the first block directly to allow for it to be unnamed.
   auto owningBlock = std::make_unique<Block>();
+  auto cleanup = llvm::make_scope_exit([&] {
+    if (owningBlock)
+      owningBlock->dropAllDefinedValueUses();
+  });
   Block *block = owningBlock.get();
 
   // If this block is not defined in the source file, add a definition for it
diff --git a/mlir/test/IR/invalid.mlir b/mlir/test/IR/invalid.mlir
index 861f4ef6c020d..d5b23fcf27950 100644
--- a/mlir/test/IR/invalid.mlir
+++ b/mlir/test/IR/invalid.mlir
@@ -675,3 +675,17 @@ func.func @error_at_end_of_line() {
 // -----
 
 @foo   // expected-error {{expected operation name in quotes}}
+
+// -----
+
+func.func @drop_references_on_block_parse_error(){
+  "test.user"(%i, %1) : (index, index) -> ()
+  "test.op_with_region"() ({
+  ^bb0(%i : index):
+    // expected-error @below{{expected operation name in quotes}}
+    %1 = "test.foo"() : () -> (index)
+    // Syntax error to abort parsing this block.
+    123
+  }) : () -> ()
+  return
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/128011


More information about the Mlir-commits mailing list