xref: /llvm-project/flang/lib/Optimizer/Builder/Runtime/Allocatable.cpp (revision fac349a169976f822fb27f03e623fa0d28aec1f3)
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-exception
6 //
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 
14 using namespace Fortran::runtime;
15 
genMoveAlloc(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value to,mlir::Value from,mlir::Value hasStat,mlir::Value errMsg)16 mlir::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         builder.create<fir::TypeDescOp>(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 builder.create<fir::CallOp>(loc, func, args).getResult(0);
42 }
43 
genAllocatableApplyMold(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value desc,mlir::Value mold,int rank)44 void 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   builder.create<fir::CallOp>(loc, func, args);
56 }
57 
genAllocatableSetBounds(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value desc,mlir::Value dimIndex,mlir::Value lowerBound,mlir::Value upperBound)58 void 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   builder.create<fir::CallOp>(loc, func, args);
70 }
71 
genAllocatableAllocate(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value desc,mlir::Value hasStat,mlir::Value errMsg)72 void 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 sourceFile{fir::factory::locationToFilename(builder, loc)};
80   mlir::Value sourceLine{
81       fir::factory::locationToLineNo(builder, loc, fTy.getInput(4))};
82   if (!hasStat)
83     hasStat = builder.createBool(loc, false);
84   if (!errMsg) {
85     mlir::Type boxNoneTy = fir::BoxType::get(builder.getNoneType());
86     errMsg = builder.create<fir::AbsentOp>(loc, boxNoneTy).getResult();
87   }
88   llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(
89       builder, loc, fTy, desc, hasStat, errMsg, sourceFile, sourceLine)};
90   builder.create<fir::CallOp>(loc, func, args);
91 }
92