94 lines · cpp
1//===-- Allocatable.cpp -- generate allocatable runtime API calls----------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "flang/Optimizer/Builder/Runtime/Allocatable.h"10#include "flang/Optimizer/Builder/FIRBuilder.h"11#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"12#include "flang/Runtime/allocatable.h"13 14using namespace Fortran::runtime;15 16mlir::Value fir::runtime::genMoveAlloc(fir::FirOpBuilder &builder,17 mlir::Location loc, mlir::Value to,18 mlir::Value from, mlir::Value hasStat,19 mlir::Value errMsg) {20 mlir::func::FuncOp func{21 fir::runtime::getRuntimeFunc<mkRTKey(MoveAlloc)>(loc, builder)};22 mlir::FunctionType fTy{func.getFunctionType()};23 mlir::Value sourceFile{fir::factory::locationToFilename(builder, loc)};24 mlir::Value sourceLine{25 fir::factory::locationToLineNo(builder, loc, fTy.getInput(6))};26 mlir::Value declaredTypeDesc;27 if (fir::isPolymorphicType(from.getType()) &&28 !fir::isUnlimitedPolymorphicType(from.getType())) {29 fir::ClassType clTy =30 mlir::dyn_cast<fir::ClassType>(fir::dyn_cast_ptrEleTy(from.getType()));31 mlir::Type derivedType = fir::unwrapInnerType(clTy.getEleTy());32 declaredTypeDesc =33 fir::TypeDescOp::create(builder, loc, mlir::TypeAttr::get(derivedType));34 } else {35 declaredTypeDesc = builder.createNullConstant(loc);36 }37 llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(38 builder, loc, fTy, to, from, declaredTypeDesc, hasStat, errMsg,39 sourceFile, sourceLine)};40 41 return fir::CallOp::create(builder, loc, func, args).getResult(0);42}43 44void fir::runtime::genAllocatableApplyMold(fir::FirOpBuilder &builder,45 mlir::Location loc, mlir::Value desc,46 mlir::Value mold, int rank) {47 mlir::func::FuncOp func{48 fir::runtime::getRuntimeFunc<mkRTKey(AllocatableApplyMold)>(loc,49 builder)};50 mlir::FunctionType fTy = func.getFunctionType();51 mlir::Value rankVal =52 builder.createIntegerConstant(loc, fTy.getInput(2), rank);53 llvm::SmallVector<mlir::Value> args{54 fir::runtime::createArguments(builder, loc, fTy, desc, mold, rankVal)};55 fir::CallOp::create(builder, loc, func, args);56}57 58void fir::runtime::genAllocatableSetBounds(fir::FirOpBuilder &builder,59 mlir::Location loc, mlir::Value desc,60 mlir::Value dimIndex,61 mlir::Value lowerBound,62 mlir::Value upperBound) {63 mlir::func::FuncOp func{64 fir::runtime::getRuntimeFunc<mkRTKey(AllocatableSetBounds)>(loc,65 builder)};66 mlir::FunctionType fTy{func.getFunctionType()};67 llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(68 builder, loc, fTy, desc, dimIndex, lowerBound, upperBound)};69 fir::CallOp::create(builder, loc, func, args);70}71 72void fir::runtime::genAllocatableAllocate(fir::FirOpBuilder &builder,73 mlir::Location loc, mlir::Value desc,74 mlir::Value hasStat,75 mlir::Value errMsg) {76 mlir::func::FuncOp func{77 fir::runtime::getRuntimeFunc<mkRTKey(AllocatableAllocate)>(loc, builder)};78 mlir::FunctionType fTy{func.getFunctionType()};79 mlir::Value asyncObject = builder.createNullConstant(loc);80 mlir::Value sourceFile{fir::factory::locationToFilename(builder, loc)};81 mlir::Value sourceLine{82 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5))};83 if (!hasStat)84 hasStat = builder.createBool(loc, false);85 if (!errMsg) {86 mlir::Type boxNoneTy = fir::BoxType::get(builder.getNoneType());87 errMsg = fir::AbsentOp::create(builder, loc, boxNoneTy).getResult();88 }89 llvm::SmallVector<mlir::Value> args{90 fir::runtime::createArguments(builder, loc, fTy, desc, asyncObject,91 hasStat, errMsg, sourceFile, sourceLine)};92 fir::CallOp::create(builder, loc, func, args);93}94