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

Matthias Springer llvmlistbot at llvm.org
Thu Feb 20 06:50:46 PST 2025


https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/128011

>From 6120243b27335481579ce438b3e8a985d5283502 Mon Sep 17 00:00:00 2001
From: Matthias Springer <mspringer at nvidia.com>
Date: Thu, 20 Feb 2025 15:43:36 +0100
Subject: [PATCH] [mlir][Parser] Fix crash after block parsing failure

---
 mlir/lib/AsmParser/Parser.cpp |  8 ++++++++
 mlir/test/IR/invalid.mlir     | 14 ++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp
index b5f1d2e27c9ba..7d0033b897dd7 100644
--- a/mlir/lib/AsmParser/Parser.cpp
+++ b/mlir/lib/AsmParser/Parser.cpp
@@ -2228,6 +2228,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