Skip to content

Commit e5cb6da

Browse files
authored
[flang] caller side BIND(C) INTENT(OUT) deallocation in HLFIR (#67119)
- Just use genDeallocateIfAllocate. - Port/share the related FIR test.
1 parent 8545093 commit e5cb6da

File tree

2 files changed

+98
-58
lines changed

2 files changed

+98
-58
lines changed

flang/lib/Lower/ConvertCall.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "flang/Lower/ConvertCall.h"
14+
#include "flang/Lower/Allocatable.h"
1415
#include "flang/Lower/ConvertExprToHLFIR.h"
1516
#include "flang/Lower/ConvertVariable.h"
1617
#include "flang/Lower/CustomIntrinsicCall.h"
@@ -1199,12 +1200,21 @@ genUserCall(Fortran::lower::PreparedActualArguments &loweredActuals,
11991200
}
12001201
// Passing a POINTER to a POINTER, or an ALLOCATABLE to an ALLOCATABLE.
12011202
assert(actual.isMutableBox() && "actual must be a mutable box");
1202-
caller.placeInput(arg, actual);
12031203
if (fir::isAllocatableType(argTy) && arg.isIntentOut() &&
12041204
Fortran::semantics::IsBindCProcedure(
12051205
*callContext.procRef.proc().GetSymbol())) {
1206-
TODO(loc, "BIND(C) INTENT(OUT) allocatable deallocation in HLFIR");
1206+
// INTENT(OUT) allocatables are deallocated on the callee side,
1207+
// but BIND(C) procedures may be implemented in C, so deallocation is
1208+
// also done on the caller side (if the procedure is implemented in
1209+
// Fortran, the deallocation attempt in the callee will be a no-op).
1210+
auto [exv, cleanup] =
1211+
hlfir::translateToExtendedValue(loc, builder, actual);
1212+
const auto *mutableBox = exv.getBoxOf<fir::MutableBoxValue>();
1213+
assert(mutableBox && !cleanup && "expect allocatable");
1214+
Fortran::lower::genDeallocateIfAllocated(callContext.converter,
1215+
*mutableBox, loc);
12071216
}
1217+
caller.placeInput(arg, actual);
12081218
} break;
12091219
}
12101220
}

0 commit comments

Comments
 (0)