Files
clang-p2996/flang/lib/Optimizer/Builder/Runtime/Derived.cpp
Valentin Clement 90e9fcbb68 [flang] Set declared type when NULLIFY a polymorphic pointer
Fortran standard 7.3.2.3 point 7 mentions that a diassociated
pointer dynamic type is its declared type.
in 9.7.2 note 1, when a NULLIFY statement is applied to a polymorphic pointer,
its dynamic type becomes the same as its declared type.
This patch enforce these standard points by calling the runtime function
`PointerNullifyDerived` with the declared type descriptor.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D136948
2022-10-31 11:03:13 +01:00

65 lines
3.1 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::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);
}