1//===- ArrayConstructor.cpp - array constructor 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/ArrayConstructor.h"
10#include "flang/Optimizer/Builder/FIRBuilder.h"
11#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12#include "flang/Runtime/array-constructor.h"
13
14using namespace Fortran::runtime;
15
16namespace fir::runtime {
17template <>
18constexpr TypeBuilderFunc
19getModel<Fortran::runtime::ArrayConstructorVector &>() {
20 return getModel<void *>();
21}
22} // namespace fir::runtime
23
24mlir::Value fir::runtime::genInitArrayConstructorVector(
25 mlir::Location loc, fir::FirOpBuilder &builder, mlir::Value toBox,
26 mlir::Value useValueLengthParameters) {
27 // Allocate storage for the runtime cookie for the array constructor vector.
28 // Use the "host" size and alignment, but double them to be safe regardless of
29 // the target. The "cookieSize" argument is used to validate this wild
30 // assumption until runtime interfaces are improved.
31 std::size_t arrayVectorStructBitSize =
32 2 * sizeof(Fortran::runtime::ArrayConstructorVector) * 8;
33 std::size_t alignLike = alignof(Fortran::runtime::ArrayConstructorVector) * 8;
34 fir::SequenceType::Extent numElem =
35 (arrayVectorStructBitSize + alignLike - 1) / alignLike;
36 mlir::Type intType = builder.getIntegerType(alignLike);
37 mlir::Type seqType = fir::SequenceType::get({numElem}, intType);
38 mlir::Value cookie =
39 builder.createTemporary(loc, seqType, ".rt.arrayctor.vector");
40
41 mlir::func::FuncOp func =
42 fir::runtime::getRuntimeFunc<mkRTKey(InitArrayConstructorVector)>(
43 loc, builder);
44 mlir::FunctionType funcType = func.getFunctionType();
45 cookie = builder.createConvert(loc, funcType.getInput(0), cookie);
46 mlir::Value cookieSize = builder.createIntegerConstant(
47 loc, funcType.getInput(3), numElem * alignLike / 8);
48 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
49 mlir::Value sourceLine =
50 fir::factory::locationToLineNo(builder, loc, funcType.getInput(5));
51 auto args = fir::runtime::createArguments(builder, loc, funcType, cookie,
52 toBox, useValueLengthParameters,
53 cookieSize, sourceFile, sourceLine);
54 builder.create<fir::CallOp>(loc, func, args);
55 return cookie;
56}
57
58void fir::runtime::genPushArrayConstructorValue(
59 mlir::Location loc, fir::FirOpBuilder &builder,
60 mlir::Value arrayConstructorVector, mlir::Value fromBox) {
61 mlir::func::FuncOp func =
62 fir::runtime::getRuntimeFunc<mkRTKey(PushArrayConstructorValue)>(loc,
63 builder);
64 mlir::FunctionType funcType = func.getFunctionType();
65 auto args = fir::runtime::createArguments(builder, loc, funcType,
66 arrayConstructorVector, fromBox);
67 builder.create<fir::CallOp>(loc, func, args);
68}
69
70void fir::runtime::genPushArrayConstructorSimpleScalar(
71 mlir::Location loc, fir::FirOpBuilder &builder,
72 mlir::Value arrayConstructorVector, mlir::Value fromAddress) {
73 mlir::func::FuncOp func =
74 fir::runtime::getRuntimeFunc<mkRTKey(PushArrayConstructorSimpleScalar)>(
75 loc, builder);
76 mlir::FunctionType funcType = func.getFunctionType();
77 auto args = fir::runtime::createArguments(
78 builder, loc, funcType, arrayConstructorVector, fromAddress);
79 builder.create<fir::CallOp>(loc, func, args);
80}
81

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