[flang-commits] [flang] [flang][hlfir] Fixed some finalization/deallocation issues. (PR #67047)

via flang-commits flang-commits at lists.llvm.org
Fri Sep 22 01:33:28 PDT 2023


================
@@ -439,8 +440,20 @@ static bool allOtherUsesAreSafeForAssociate(mlir::Value value,
        value.getParentRegion() != endAssociate->getParentRegion()))
     return false;
 
-  for (mlir::Operation *useOp : value.getUsers())
-    if (!mlir::isa<hlfir::DestroyOp>(useOp) && useOp != currentUse) {
+  for (mlir::Operation *useOp : value.getUsers()) {
+    // Ignore DestroyOp's that do not imply finalization.
+    // If finalization is implied, then we must delegate
+    // the finalization to the correspoding EndAssociateOp,
+    // but we currently do not; so we disable the buffer
+    // reuse in this case.
+    if (auto destroy = mlir::dyn_cast<hlfir::DestroyOp>(useOp)) {
+      if (destroy.mustFinalizeExpr())
+        return false;
----------------
jeanPerier wrote:

While semantically correct, it is a bit annoying to introduce a copy instead of running the finalizers at the hlfir.end_associate.

Moving the finalization at the end associate needs a bit of thinking though since it is not the same if the associated buffer was modified (I think correct Fortran cannot modified a variable that we create with hlfir.associate, except maybe for VALUE dummies...), so I am OK with doing the copy right now.

https://github.com/llvm/llvm-project/pull/67047


More information about the flang-commits mailing list