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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Feb 22 03:57:43 PST 2025


Author: Matthias Springer
Date: 2025-02-22T12:57:40+01:00
New Revision: 4e44bd027bf4e490380be770172994561616bd2d

URL: https://github.com/llvm/llvm-project/commit/4e44bd027bf4e490380be770172994561616bd2d
DIFF: https://github.com/llvm/llvm-project/commit/4e44bd027bf4e490380be770172994561616bd2d.diff

LOG: [mlir][Parser] Fix crash after block parsing failure (#128011)

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.

Added: 
    

Modified: 
    mlir/lib/AsmParser/Parser.cpp
    mlir/test/IR/invalid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp
index 2982757a6c5ce..c316f34d977e9 100644
--- a/mlir/lib/AsmParser/Parser.cpp
+++ b/mlir/lib/AsmParser/Parser.cpp
@@ -2234,6 +2234,14 @@ 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 failureCleanup = llvm::make_scope_exit([&] {
+    if (owningBlock) {
+      // If parsing failed, as indicated by the fact that `owningBlock` still
+      // owns the block, drop all forward references from preceding operations
+      // to definitions within the parsed block.
+      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
+}


        


More information about the Mlir-commits mailing list