Debian Codegraph @

llvm-project/flang/lib/Optimizer/Builder/Runtime/Derived.cpp

raw
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::genNullifyDerivedType(fir::FirOpBuilder &builder,
41 mlir::Location loc, mlir::Value box,
42 fir::RecordType derivedType,
43 unsigned rank) {
44 std::string typeDescName =
45 fir::NameUniquer::getTypeDescriptorName(derivedType.getName());
46 fir::GlobalOp typeDescGlobal = builder.getNamedGlobal(typeDescName);
47 if (!typeDescGlobal)
48 fir::emitFatalError(loc, "no type descriptor found for NULLIFY");
49 auto typeDescAddr = builder.create<fir::AddrOfOp>(
50 loc, fir::ReferenceType::get(typeDescGlobal.getType()),
51 typeDescGlobal.getSymbol());
52 mlir::func::FuncOp callee =
53 fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyDerived)>(loc,
54 builder);
55 llvm::ArrayRef<mlir::Type> inputTypes = callee.getFunctionType().getInputs();
56 llvm::SmallVector<mlir::Value> args;
57 args.push_back(builder.createConvert(loc, inputTypes[0], box));
58 args.push_back(builder.createConvert(loc, inputTypes[1], typeDescAddr));
59 mlir::Value rankCst = builder.createIntegerConstant(loc, inputTypes[2], rank);
60 mlir::Value c0 = builder.createIntegerConstant(loc, inputTypes[3], 0);
61 args.push_back(rankCst);
62 args.push_back(c0);
63 builder.create<fir::CallOp>(loc, callee, args);
64 }
65
66 mlir::Value fir::runtime::genSameTypeAs(fir::FirOpBuilder &builder,
67 mlir::Location loc, mlir::Value a,
68 mlir::Value b) {
69 mlir::func::FuncOp sameTypeAsFunc =
70 fir::runtime::getRuntimeFunc<mkRTKey(SameTypeAs)>(loc, builder);
71 auto fTy = sameTypeAsFunc.getFunctionType();
72 auto args = fir::runtime::createArguments(builder, loc, fTy, a, b);
73 return builder.create<fir::CallOp>(loc, sameTypeAsFunc, args).getResult(0);
74 }
75
76 mlir::Value fir::runtime::genExtendsTypeOf(fir::FirOpBuilder &builder,
77 mlir::Location loc, mlir::Value a,
78 mlir::Value mold) {
79 mlir::func::FuncOp extendsTypeOfFunc =
80 fir::runtime::getRuntimeFunc<mkRTKey(ExtendsTypeOf)>(loc, builder);
81 auto fTy = extendsTypeOfFunc.getFunctionType();
82 auto args = fir::runtime::createArguments(builder, loc, fTy, a, mold);
83 return builder.create<fir::CallOp>(loc, extendsTypeOfFunc, args).getResult(0);
84 }