Slava Zakharin da60b9e7dc [flang] Fixed managing copy-in/copy-out temps.
There are several observations regarding the copy-in/copy-out:
  * Actual argument associated with INTENT(OUT) dummy argument that
    requires finalization (7.5.6.3 p. 7) may be read by the finalization
    function, so a copy-in is required.
  * A temporary created for the copy-in/copy-out must be destroyed
    without finalization after the call (or after the corresponding copy-out),
    otherwise, memory leaks may occur.
  * The copy-out assignment must not perform finalization for the LHS.
  * The copy-out assignment from the temporary to the actual argument
    may or may not need to initialize the LHS.

This change-set introduces new runtime methods: CopyOutAssign and
DestroyWithoutFinalization. They are called by the compiler generated
code to match the behavior described above.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D151135
2023-05-23 09:35:17 -07:00

94 lines
4.5 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::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) {
std::string typeDescName =
fir::NameUniquer::getTypeDescriptorName(derivedType.getName());
fir::GlobalOp typeDescGlobal = builder.getNamedGlobal(typeDescName);
if (!typeDescGlobal)
fir::emitFatalError(loc, "no type descriptor found for NULLIFY");
auto typeDescAddr = builder.create<fir::AddrOfOp>(
loc, fir::ReferenceType::get(typeDescGlobal.getType()),
typeDescGlobal.getSymbol());
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], typeDescAddr));
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);
}