Skip to content

Commit 4e44bd0

Browse files
[mlir][Parser] Fix crash after block parsing failure (llvm#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.
1 parent e23ab73 commit 4e44bd0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

mlir/lib/AsmParser/Parser.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,14 @@ ParseResult OperationParser::parseRegionBody(Region &region, SMLoc startLoc,
22342234

22352235
// Parse the first block directly to allow for it to be unnamed.
22362236
auto owningBlock = std::make_unique<Block>();
2237+
auto failureCleanup = llvm::make_scope_exit([&] {
2238+
if (owningBlock) {
2239+
// If parsing failed, as indicated by the fact that `owningBlock` still
2240+
// owns the block, drop all forward references from preceding operations
2241+
// to definitions within the parsed block.
2242+
owningBlock->dropAllDefinedValueUses();
2243+
}
2244+
});
22372245
Block *block = owningBlock.get();
22382246

22392247
// If this block is not defined in the source file, add a definition for it

mlir/test/IR/invalid.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,17 @@ func.func @error_at_end_of_line() {
675675
// -----
676676

677677
@foo // expected-error {{expected operation name in quotes}}
678+
679+
// -----
680+
681+
func.func @drop_references_on_block_parse_error(){
682+
"test.user"(%i, %1) : (index, index) -> ()
683+
"test.op_with_region"() ({
684+
^bb0(%i : index):
685+
// expected-error @below{{expected operation name in quotes}}
686+
%1 = "test.foo"() : () -> (index)
687+
// Syntax error to abort parsing this block.
688+
123
689+
}) : () -> ()
690+
return
691+
}

0 commit comments

Comments
 (0)