[flang-commits] [flang] [llvm] [flang][cuda] Fix device-variable leaks and reset-safe cleanup (PR #209865)
via flang-commits
flang-commits at lists.llvm.org
Fri Jul 17 01:54:12 PDT 2026
================
@@ -1296,22 +1306,25 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter,
}
if (std::optional<VariableCleanUp> cleanup =
- needDeallocationOrFinalization(var)) {
+ needDeallocationOrFinalization(var, deallocateMainProgramVariable)) {
auto *builder = &converter.getFirOpBuilder();
mlir::Location loc = converter.getCurrentLocation();
fir::ExtendedValue exv =
converter.getSymbolExtendedValue(var.getSymbol(), &symMap);
+ Fortran::lower::StatementContext &cleanupCtx =
+ needsHostCudaCleanup ? converter.getCudaCleanupCtx()
+ : converter.getFctCtx();
switch (*cleanup) {
case VariableCleanUp::Finalize:
- converter.getFctCtx().attachCleanup([builder, loc, exv]() {
+ cleanupCtx.attachCleanup([builder, loc, exv]() {
mlir::Value box = builder->createBox(loc, exv);
fir::runtime::genDerivedTypeDestroy(*builder, loc, box);
});
----------------
jeanPerier wrote:
I am not sure what are the constraints in CUDA Fortran with regards to finalization, but I find it a bit weird that the Fortran language required finalization could be made conditional on `CUFDeviceIsActive` (which as I understand this patch is what happens when using the CudaCleanupCtx).
@clementval, if you have a derived type device variable with a final methods that reaches its end of scope on the host, what is supposed to happen? Should the final method be called on the host? Will it be called on the device? Should there be a special final routine with a DEVICE input? Or should finalization be skipped all together.
https://github.com/llvm/llvm-project/pull/209865
More information about the flang-commits
mailing list