1//===-- Derived.cpp -- derived type runtime API ---------------------------===//
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/Derived.h"
10#include "flang/Optimizer/Builder/FIRBuilder.h"
11#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12#include "flang/Optimizer/Support/FatalError.h"
13#include "flang/Optimizer/Support/InternalNames.h"
14#include "flang/Runtime/derived-api.h"
15#include "flang/Runtime/pointer.h"
16
17using namespace Fortran::runtime;
18
19void fir::runtime::genDerivedTypeInitialize(fir::FirOpBuilder &builder,
20 mlir::Location loc,
21 mlir::Value box) {
22 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Initialize)>(loc, builder);
23 auto fTy = func.getFunctionType();
24 auto sourceFile = fir::factory::locationToFilename(builder, loc);
25 auto sourceLine =
26 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
27 auto args = fir::runtime::createArguments(builder, loc, fTy, box, sourceFile,
28 sourceLine);
29 builder.create<fir::CallOp>(loc, func, args);
30}
31
32void fir::runtime::genDerivedTypeInitializeClone(fir::FirOpBuilder &builder,
33 mlir::Location loc,
34 mlir::Value newBox,
35 mlir::Value box) {
36 auto func =
37 fir::runtime::getRuntimeFunc<mkRTKey(InitializeClone)>(loc, builder);
38 auto fTy = func.getFunctionType();
39 auto sourceFile = fir::factory::locationToFilename(builder, loc);
40 auto sourceLine =
41 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
42 auto args = fir::runtime::createArguments(builder, loc, fTy, newBox, box,
43 sourceFile, sourceLine);
44 builder.create<fir::CallOp>(loc, func, args);
45}
46
47void fir::runtime::genDerivedTypeDestroy(fir::FirOpBuilder &builder,
48 mlir::Location loc, mlir::Value box) {
49 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Destroy)>(loc, builder);
50 auto fTy = func.getFunctionType();
51 auto args = fir::runtime::createArguments(builder, loc, fTy, box);
52 builder.create<fir::CallOp>(loc, func, args);
53}
54
55void fir::runtime::genDerivedTypeFinalize(fir::FirOpBuilder &builder,
56 mlir::Location loc, mlir::Value box) {
57 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Finalize)>(loc, builder);
58 auto fTy = func.getFunctionType();
59 auto sourceFile = fir::factory::locationToFilename(builder, loc);
60 auto sourceLine =
61 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
62 auto args = fir::runtime::createArguments(builder, loc, fTy, box, sourceFile,
63 sourceLine);
64 builder.create<fir::CallOp>(loc, func, args);
65}
66
67void fir::runtime::genDerivedTypeDestroyWithoutFinalization(
68 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value box) {
69 auto func = fir::runtime::getRuntimeFunc<mkRTKey(DestroyWithoutFinalization)>(
70 loc, builder);
71 auto fTy = func.getFunctionType();
72 auto args = fir::runtime::createArguments(builder, loc, fTy, box);
73 builder.create<fir::CallOp>(loc, func, args);
74}
75
76void fir::runtime::genNullifyDerivedType(fir::FirOpBuilder &builder,
77 mlir::Location loc, mlir::Value box,
78 fir::RecordType derivedType,
79 unsigned rank) {
80 mlir::Value typeDesc =
81 builder.create<fir::TypeDescOp>(loc, mlir::TypeAttr::get(derivedType));
82 mlir::func::FuncOp callee =
83 fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyDerived)>(loc,
84 builder);
85 llvm::ArrayRef<mlir::Type> inputTypes = callee.getFunctionType().getInputs();
86 llvm::SmallVector<mlir::Value> args;
87 args.push_back(builder.createConvert(loc, inputTypes[0], box));
88 args.push_back(builder.createConvert(loc, inputTypes[1], typeDesc));
89 mlir::Value rankCst = builder.createIntegerConstant(loc, inputTypes[2], rank);
90 mlir::Value c0 = builder.createIntegerConstant(loc, inputTypes[3], 0);
91 args.push_back(rankCst);
92 args.push_back(c0);
93 builder.create<fir::CallOp>(loc, callee, args);
94}
95
96mlir::Value fir::runtime::genSameTypeAs(fir::FirOpBuilder &builder,
97 mlir::Location loc, mlir::Value a,
98 mlir::Value b) {
99 mlir::func::FuncOp sameTypeAsFunc =
100 fir::runtime::getRuntimeFunc<mkRTKey(SameTypeAs)>(loc, builder);
101 auto fTy = sameTypeAsFunc.getFunctionType();
102 auto args = fir::runtime::createArguments(builder, loc, fTy, a, b);
103 return builder.create<fir::CallOp>(loc, sameTypeAsFunc, args).getResult(0);
104}
105
106mlir::Value fir::runtime::genExtendsTypeOf(fir::FirOpBuilder &builder,
107 mlir::Location loc, mlir::Value a,
108 mlir::Value mold) {
109 mlir::func::FuncOp extendsTypeOfFunc =
110 fir::runtime::getRuntimeFunc<mkRTKey(ExtendsTypeOf)>(loc, builder);
111 auto fTy = extendsTypeOfFunc.getFunctionType();
112 auto args = fir::runtime::createArguments(builder, loc, fTy, a, mold);
113 return builder.create<fir::CallOp>(loc, extendsTypeOfFunc, args).getResult(0);
114}
115

source code of flang/lib/Optimizer/Builder/Runtime/Derived.cpp