Skip to content

Commit 54cdc03

Browse files
committed
[mlir:Parser] Always splice parsed operations to the end of the parsed block
The current splicing behavior dates back to when all blocks had terminators, so we would "helpfully" splice before the terminator. This doesn't make sense anymore, and leads to somewhat unexpected results when parsing multiple pieces of IR into the same block. Differential Revision: https://reviews.llvm.org/D135096
1 parent 5301826 commit 54cdc03

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

mlir/lib/AsmParser/Parser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2593,8 +2593,8 @@ ParseResult TopLevelOperationParser::parse(Block *topLevelBlock,
25932593
// top-level block.
25942594
auto &parsedOps = topLevelOp->getBody()->getOperations();
25952595
auto &destOps = topLevelBlock->getOperations();
2596-
destOps.splice(destOps.empty() ? destOps.end() : std::prev(destOps.end()),
2597-
parsedOps, parsedOps.begin(), parsedOps.end());
2596+
destOps.splice(destOps.end(), parsedOps, parsedOps.begin(),
2597+
parsedOps.end());
25982598
return success();
25992599
}
26002600

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,7 @@ LogicalResult BytecodeReader::parseIRSection(ArrayRef<uint8_t> sectionData,
14141414
// Splice the parsed operations over to the provided top-level block.
14151415
auto &parsedOps = moduleOp->getBody()->getOperations();
14161416
auto &destOps = block->getOperations();
1417-
destOps.splice(destOps.empty() ? destOps.end() : std::prev(destOps.end()),
1418-
parsedOps, parsedOps.begin(), parsedOps.end());
1417+
destOps.splice(destOps.end(), parsedOps, parsedOps.begin(), parsedOps.end());
14191418
return success();
14201419
}
14211420

mlir/unittests/Parser/ParserTest.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,31 @@ TEST(MLIRParser, ParseInvalidIR) {
2828
ASSERT_TRUE(module);
2929
ASSERT_TRUE(failed(verify(*module)));
3030
}
31+
32+
TEST(MLIRParser, ParseAtEnd) {
33+
std::string firstModuleStr = R"mlir(
34+
"test.first"() : () -> ()
35+
)mlir";
36+
std::string secondModuleStr = R"mlir(
37+
"test.second"() : () -> ()
38+
)mlir";
39+
40+
MLIRContext context;
41+
context.allowUnregisteredDialects();
42+
Block block;
43+
44+
// Parse the first module string.
45+
LogicalResult firstParse =
46+
parseSourceString(firstModuleStr, &block, &context);
47+
EXPECT_TRUE(succeeded(firstParse));
48+
49+
// Parse the second module string.
50+
LogicalResult secondParse =
51+
parseSourceString(secondModuleStr, &block, &context);
52+
EXPECT_TRUE(succeeded(secondParse));
53+
54+
// Check the we parse at the end.
55+
EXPECT_EQ(block.front().getName().getStringRef(), "test.first");
56+
EXPECT_EQ(block.back().getName().getStringRef(), "test.second");
57+
}
3158
} // namespace

0 commit comments

Comments
 (0)