Files
clang-p2996/flang/lib/Optimizer/Builder/Runtime/Derived.cpp
Leandro Lupori 1fcb6a9754 [flang][OpenMP] Initialize allocatable members of derived types (#120295)
Allocatable members of privatized derived types must be allocated,
with the same bounds as the original object, whenever that member
is also allocated in it, but Flang was not performing such
initialization.

The `Initialize` runtime function can't perform this task unless
its signature is changed to receive an additional parameter, the
original object, that is needed to find out which allocatable
members, with their bounds, must also be allocated in the clone.
As `Initialize` is used not only for privatization, sometimes this
other object won't even exist, so this new parameter would need
to be optional.
Because of this, it seemed better to add a new runtime function:
`InitializeClone`.
To avoid unnecessary calls, lowering inserts a call to it only for
privatized items that are derived types with allocatable members.

Fixes https://github.com/llvm/llvm-project/issues/114888
Fixes https://github.com/llvm/llvm-project/issues/114889
2024-12-19 17:26:50 -03:00

115 lines
5.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::genDerivedTypeInitializeClone(fir::FirOpBuilder &builder,
mlir::Location loc,
mlir::Value newBox,
mlir::Value box) {
auto func =
fir::runtime::getRuntimeFunc<mkRTKey(InitializeClone)>(loc, builder);
auto fTy = func.getFunctionType();
auto sourceFile = fir::factory::locationToFilename(builder, loc);
auto sourceLine =
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
auto args = fir::runtime::createArguments(builder, loc, fTy, newBox, 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);
}