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 | |
17 | using namespace Fortran::runtime; |
18 | |
19 | void 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 | |
32 | void fir::runtime::genDerivedTypeDestroy(fir::FirOpBuilder &builder, |
33 | mlir::Location loc, mlir::Value box) { |
34 | auto func = fir::runtime::getRuntimeFunc<mkRTKey(Destroy)>(loc, builder); |
35 | auto fTy = func.getFunctionType(); |
36 | auto args = fir::runtime::createArguments(builder, loc, fTy, box); |
37 | builder.create<fir::CallOp>(loc, func, args); |
38 | } |
39 | |
40 | void fir::runtime::genDerivedTypeFinalize(fir::FirOpBuilder &builder, |
41 | mlir::Location loc, mlir::Value box) { |
42 | auto func = fir::runtime::getRuntimeFunc<mkRTKey(Finalize)>(loc, builder); |
43 | auto fTy = func.getFunctionType(); |
44 | auto sourceFile = fir::factory::locationToFilename(builder, loc); |
45 | auto sourceLine = |
46 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(2)); |
47 | auto args = fir::runtime::createArguments(builder, loc, fTy, box, sourceFile, |
48 | sourceLine); |
49 | builder.create<fir::CallOp>(loc, func, args); |
50 | } |
51 | |
52 | void fir::runtime::genDerivedTypeDestroyWithoutFinalization( |
53 | fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value box) { |
54 | auto func = fir::runtime::getRuntimeFunc<mkRTKey(DestroyWithoutFinalization)>( |
55 | loc, builder); |
56 | auto fTy = func.getFunctionType(); |
57 | auto args = fir::runtime::createArguments(builder, loc, fTy, box); |
58 | builder.create<fir::CallOp>(loc, func, args); |
59 | } |
60 | |
61 | void fir::runtime::genNullifyDerivedType(fir::FirOpBuilder &builder, |
62 | mlir::Location loc, mlir::Value box, |
63 | fir::RecordType derivedType, |
64 | unsigned rank) { |
65 | mlir::Value typeDesc = |
66 | builder.create<fir::TypeDescOp>(loc, mlir::TypeAttr::get(derivedType)); |
67 | mlir::func::FuncOp callee = |
68 | fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyDerived)>(loc, |
69 | builder); |
70 | llvm::ArrayRef<mlir::Type> inputTypes = callee.getFunctionType().getInputs(); |
71 | llvm::SmallVector<mlir::Value> args; |
72 | args.push_back(builder.createConvert(loc, inputTypes[0], box)); |
73 | args.push_back(builder.createConvert(loc, inputTypes[1], typeDesc)); |
74 | mlir::Value rankCst = builder.createIntegerConstant(loc, inputTypes[2], rank); |
75 | mlir::Value c0 = builder.createIntegerConstant(loc, inputTypes[3], 0); |
76 | args.push_back(rankCst); |
77 | args.push_back(c0); |
78 | builder.create<fir::CallOp>(loc, callee, args); |
79 | } |
80 | |
81 | mlir::Value fir::runtime::genSameTypeAs(fir::FirOpBuilder &builder, |
82 | mlir::Location loc, mlir::Value a, |
83 | mlir::Value b) { |
84 | mlir::func::FuncOp sameTypeAsFunc = |
85 | fir::runtime::getRuntimeFunc<mkRTKey(SameTypeAs)>(loc, builder); |
86 | auto fTy = sameTypeAsFunc.getFunctionType(); |
87 | auto args = fir::runtime::createArguments(builder, loc, fTy, a, b); |
88 | return builder.create<fir::CallOp>(loc, sameTypeAsFunc, args).getResult(0); |
89 | } |
90 | |
91 | mlir::Value fir::runtime::genExtendsTypeOf(fir::FirOpBuilder &builder, |
92 | mlir::Location loc, mlir::Value a, |
93 | mlir::Value mold) { |
94 | mlir::func::FuncOp extendsTypeOfFunc = |
95 | fir::runtime::getRuntimeFunc<mkRTKey(ExtendsTypeOf)>(loc, builder); |
96 | auto fTy = extendsTypeOfFunc.getFunctionType(); |
97 | auto args = fir::runtime::createArguments(builder, loc, fTy, a, mold); |
98 | return builder.create<fir::CallOp>(loc, extendsTypeOfFunc, args).getResult(0); |
99 | } |
100 | |