diff --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp --- a/mlir/lib/AsmParser/Parser.cpp +++ b/mlir/lib/AsmParser/Parser.cpp @@ -2593,8 +2593,8 @@ // top-level block. auto &parsedOps = topLevelOp->getBody()->getOperations(); auto &destOps = topLevelBlock->getOperations(); - destOps.splice(destOps.empty() ? destOps.end() : std::prev(destOps.end()), - parsedOps, parsedOps.begin(), parsedOps.end()); + destOps.splice(destOps.end(), parsedOps, parsedOps.begin(), + parsedOps.end()); return success(); } diff --git a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp --- a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp +++ b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp @@ -1414,8 +1414,7 @@ // Splice the parsed operations over to the provided top-level block. auto &parsedOps = moduleOp->getBody()->getOperations(); auto &destOps = block->getOperations(); - destOps.splice(destOps.empty() ? destOps.end() : std::prev(destOps.end()), - parsedOps, parsedOps.begin(), parsedOps.end()); + destOps.splice(destOps.end(), parsedOps, parsedOps.begin(), parsedOps.end()); return success(); } diff --git a/mlir/unittests/Parser/ParserTest.cpp b/mlir/unittests/Parser/ParserTest.cpp --- a/mlir/unittests/Parser/ParserTest.cpp +++ b/mlir/unittests/Parser/ParserTest.cpp @@ -28,4 +28,31 @@ ASSERT_TRUE(module); ASSERT_TRUE(failed(verify(*module))); } + +TEST(MLIRParser, ParseAtEnd) { + std::string firstModuleStr = R"mlir( + "test.first"() : () -> () + )mlir"; + std::string secondModuleStr = R"mlir( + "test.second"() : () -> () + )mlir"; + + MLIRContext context; + context.allowUnregisteredDialects(); + Block block; + + // Parse the first module string. + LogicalResult firstParse = + parseSourceString(firstModuleStr, &block, &context); + EXPECT_TRUE(succeeded(firstParse)); + + // Parse the second module string. + LogicalResult secondParse = + parseSourceString(secondModuleStr, &block, &context); + EXPECT_TRUE(succeeded(secondParse)); + + // Check the we parse at the end. + EXPECT_EQ(block.front().getName().getStringRef(), "test.first"); + EXPECT_EQ(block.back().getName().getStringRef(), "test.second"); +} } // namespace