Skip to content

Commit a9a1f84

Browse files
authored
[flang][hlfir] Fixed assignment/finalization order for user-defined assignments. (#66736)
This patch places the finalization code for the RHS of a user-defined assignment after the assignment code. The change only affects standalone RegionAssignOp operations.
1 parent f1097e8 commit a9a1f84

File tree

2 files changed

+299
-1
lines changed

2 files changed

+299
-1
lines changed

flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIROrderedAssignments.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,22 @@ void OrderedAssignmentRewriter::generateSaveEntity(
11121112
assert(inserted.second && "entity must have been emplaced");
11131113
(void)inserted;
11141114
} else {
1115-
generateCleanupIfAny(oldYield);
1115+
if (constructStack.empty() &&
1116+
mlir::isa<hlfir::RegionAssignOp>(region.getParentOp())) {
1117+
// Here the clean-up code is inserted after the original
1118+
// RegionAssignOp, so that the assignment code happens
1119+
// before the cleanup. We do this only for standalone
1120+
// operations, because the clean-up is handled specially
1121+
// during lowering of the parent constructs if any
1122+
// (e.g. see generateNoneElementalCleanupIfAny for
1123+
// WhereOp).
1124+
auto insertionPoint = builder.saveInsertionPoint();
1125+
builder.setInsertionPointAfter(region.getParentOp());
1126+
generateCleanupIfAny(oldYield);
1127+
builder.restoreInsertionPoint(insertionPoint);
1128+
} else {
1129+
generateCleanupIfAny(oldYield);
1130+
}
11161131
}
11171132
}
11181133

0 commit comments

Comments
 (0)