Skip to content

Commit 6120243

Browse files
[mlir][Parser] Fix crash after block parsing failure
1 parent 01d0793 commit 6120243

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
@@ -2228,6 +2228,14 @@ ParseResult OperationParser::parseRegionBody(Region &region, SMLoc startLoc,
22282228

22292229
// Parse the first block directly to allow for it to be unnamed.
22302230
auto owningBlock = std::make_unique<Block>();
2231+
auto failureCleanup = llvm::make_scope_exit([&] {
2232+
if (owningBlock) {
2233+
// If parsing failed, as indicated by the fact that `owningBlock` still
2234+
// owns the block, drop all forward references from preceding operations
2235+
// to definitions within the parsed block.
2236+
owningBlock->dropAllDefinedValueUses();
2237+
}
2238+
});
22312239
Block *block = owningBlock.get();
22322240

22332241
// 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)