This set of commits resolves some of the issues with elemental calls producing
results that may require finalization, and also some memory leak issues due to
the missing deallocation of allocatable components of the temporary buffers
created by the bufferization pass.
- [flang][runtime] Expose Finalize API for derived types.
- [flang][hlfir] Add 'finalize' attribute for DestroyOp.
- [flang][hlfir] Postpone result finalization for elemental calls.
The results of elemental calls generated inside hlfir.elemental must not
be finalized/destructed before they are copied into the resulting
array. The finalization must be done on the array as a whole
(e.g. there might be different scalar and array finalization routines).
The finalization work is left to the hlfir.destroy corresponding
to this hlfir.elemental.
- [flang][hlfir] Tighten requirements on hlfir.end_associate operand.
If component deallocation might be required for the operand of
hlfir.end_associate, we have to be able to get the variable
shape/params to create a descriptor for calling the runtime.
This commit adds verification that we can do so.
- [flang][hlfir] Lower argument clean-ups using valid hlfir.end_associate.
The operand must be a Fortran entity, when allocatable component
deallocation may be required.
- [flang][hlfir] Properly clean-up temporary buffers in bufferization pass.
This commit combines changes for proper finalization and component
deallocation of the temporary buffers. The finalization part
relates to hlfir.destroy operations with 'finalize' attribute.
The component deallocation might be invoked for both hlfir.destroy
and hlfir.end_associate, if the operand is of a derived type
with allocatable component(s).
The changes are mostly in one function, so I decided not to split them.
- [flang][hlfir] Disable optimizations for hlfir.elemental requiring finalization.
If hlfir.elemental is coupled with hlfir.destroy with 'finalize' attribute,
the temporary array result of hlfir.elemental needs to be created
for the purpose of finalization. We cannot do certain optimizations
on such hlfir.elemental operations.
I was not able to come up with a test for the OptimizedBufferization pass,
but I put the check there as well.
100 lines
4.8 KiB
C++
100 lines
4.8 KiB
C++
//===-- Derived.cpp -- derived type runtime API ---------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Optimizer/Builder/Runtime/Derived.h"
|
|
#include "flang/Optimizer/Builder/FIRBuilder.h"
|
|
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
|
|
#include "flang/Optimizer/Support/FatalError.h"
|
|
#include "flang/Optimizer/Support/InternalNames.h"
|
|
#include "flang/Runtime/derived-api.h"
|
|
#include "flang/Runtime/pointer.h"
|
|
|
|
using namespace Fortran::runtime;
|
|
|
|
void fir::runtime::genDerivedTypeInitialize(fir::FirOpBuilder &builder,
|
|
mlir::Location loc,
|
|
mlir::Value box) {
|
|
auto func = fir::runtime::getRuntimeFunc<mkRTKey(Initialize)>(loc, builder);
|
|
auto fTy = func.getFunctionType();
|
|
auto sourceFile = fir::factory::locationToFilename(builder, loc);
|
|
auto sourceLine =
|
|
fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
|
|
auto args = fir::runtime::createArguments(builder, loc, fTy, box, sourceFile,
|
|
sourceLine);
|
|
builder.create<fir::CallOp>(loc, func, args);
|
|
}
|
|
|
|
void fir::runtime::genDerivedTypeDestroy(fir::FirOpBuilder &builder,
|
|
mlir::Location loc, mlir::Value box) {
|
|
auto func = fir::runtime::getRuntimeFunc<mkRTKey(Destroy)>(loc, builder);
|
|
auto fTy = func.getFunctionType();
|
|
auto args = fir::runtime::createArguments(builder, loc, fTy, box);
|
|
builder.create<fir::CallOp>(loc, func, args);
|
|
}
|
|
|
|
void fir::runtime::genDerivedTypeFinalize(fir::FirOpBuilder &builder,
|
|
mlir::Location loc, mlir::Value box) {
|
|
auto func = fir::runtime::getRuntimeFunc<mkRTKey(Finalize)>(loc, builder);
|
|
auto fTy = func.getFunctionType();
|
|
auto sourceFile = fir::factory::locationToFilename(builder, loc);
|
|
auto sourceLine =
|
|
fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
|
|
auto args = fir::runtime::createArguments(builder, loc, fTy, box, sourceFile,
|
|
sourceLine);
|
|
builder.create<fir::CallOp>(loc, func, args);
|
|
}
|
|
|
|
void fir::runtime::genDerivedTypeDestroyWithoutFinalization(
|
|
fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value box) {
|
|
auto func = fir::runtime::getRuntimeFunc<mkRTKey(DestroyWithoutFinalization)>(
|
|
loc, builder);
|
|
auto fTy = func.getFunctionType();
|
|
auto args = fir::runtime::createArguments(builder, loc, fTy, box);
|
|
builder.create<fir::CallOp>(loc, func, args);
|
|
}
|
|
|
|
void fir::runtime::genNullifyDerivedType(fir::FirOpBuilder &builder,
|
|
mlir::Location loc, mlir::Value box,
|
|
fir::RecordType derivedType,
|
|
unsigned rank) {
|
|
mlir::Value typeDesc =
|
|
builder.create<fir::TypeDescOp>(loc, mlir::TypeAttr::get(derivedType));
|
|
mlir::func::FuncOp callee =
|
|
fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyDerived)>(loc,
|
|
builder);
|
|
llvm::ArrayRef<mlir::Type> inputTypes = callee.getFunctionType().getInputs();
|
|
llvm::SmallVector<mlir::Value> args;
|
|
args.push_back(builder.createConvert(loc, inputTypes[0], box));
|
|
args.push_back(builder.createConvert(loc, inputTypes[1], typeDesc));
|
|
mlir::Value rankCst = builder.createIntegerConstant(loc, inputTypes[2], rank);
|
|
mlir::Value c0 = builder.createIntegerConstant(loc, inputTypes[3], 0);
|
|
args.push_back(rankCst);
|
|
args.push_back(c0);
|
|
builder.create<fir::CallOp>(loc, callee, args);
|
|
}
|
|
|
|
mlir::Value fir::runtime::genSameTypeAs(fir::FirOpBuilder &builder,
|
|
mlir::Location loc, mlir::Value a,
|
|
mlir::Value b) {
|
|
mlir::func::FuncOp sameTypeAsFunc =
|
|
fir::runtime::getRuntimeFunc<mkRTKey(SameTypeAs)>(loc, builder);
|
|
auto fTy = sameTypeAsFunc.getFunctionType();
|
|
auto args = fir::runtime::createArguments(builder, loc, fTy, a, b);
|
|
return builder.create<fir::CallOp>(loc, sameTypeAsFunc, args).getResult(0);
|
|
}
|
|
|
|
mlir::Value fir::runtime::genExtendsTypeOf(fir::FirOpBuilder &builder,
|
|
mlir::Location loc, mlir::Value a,
|
|
mlir::Value mold) {
|
|
mlir::func::FuncOp extendsTypeOfFunc =
|
|
fir::runtime::getRuntimeFunc<mkRTKey(ExtendsTypeOf)>(loc, builder);
|
|
auto fTy = extendsTypeOfFunc.getFunctionType();
|
|
auto args = fir::runtime::createArguments(builder, loc, fTy, a, mold);
|
|
return builder.create<fir::CallOp>(loc, extendsTypeOfFunc, args).getResult(0);
|
|
}
|