Skip to content

Commit 37397b3

Browse files
committed
[CIR][CIRGen][NFC] Split cir.scope creation on buildReturnStmt
1 parent b0c1490 commit 37397b3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,22 @@ mlir::LogicalResult CIRGenFunction::buildReturnStmt(const ReturnStmt &S) {
524524
else {
525525
mlir::Location scopeLoc =
526526
getLoc(RV ? RV->getSourceRange() : S.getSourceRange());
527+
// First create cir.scope and later emit it's body. Otherwise all CIRGen
528+
// dispatched by `handleReturnVal()` might needs to manipulate blocks and
529+
// look into parents, which are all unlinked.
530+
mlir::OpBuilder::InsertPoint scopeBody;
527531
builder.create<mlir::cir::ScopeOp>(
528532
scopeLoc, /*scopeBuilder=*/
529533
[&](mlir::OpBuilder &b, mlir::Location loc) {
530-
CIRGenFunction::LexicalScope lexScope{*this, loc,
531-
builder.getInsertionBlock()};
532-
handleReturnVal();
534+
scopeBody = b.saveInsertionPoint();
533535
});
536+
{
537+
mlir::OpBuilder::InsertionGuard guard(builder);
538+
builder.restoreInsertionPoint(scopeBody);
539+
CIRGenFunction::LexicalScope lexScope{*this, scopeLoc,
540+
builder.getInsertionBlock()};
541+
handleReturnVal();
542+
}
534543
}
535544

536545
// Create a new return block (if not existent) and add a branch to

0 commit comments

Comments
 (0)