1//===-- Ragged.cpp --------------------------------------------------------===//
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/Ragged.h"
10#include "flang/Optimizer/Builder/FIRBuilder.h"
11#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12#include "flang/Runtime/ragged.h"
13
14using namespace Fortran::runtime;
15
16void fir::runtime::genRaggedArrayAllocate(mlir::Location loc,
17 fir::FirOpBuilder &builder,
18 mlir::Value header, bool asHeaders,
19 mlir::Value eleSize,
20 mlir::ValueRange extents) {
21 auto i32Ty = builder.getIntegerType(32);
22 auto rank = extents.size();
23 auto i64Ty = builder.getIntegerType(64);
24 auto func =
25 fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayAllocate)>(loc, builder);
26 auto fTy = func.getFunctionType();
27 auto i1Ty = builder.getIntegerType(1);
28 fir::SequenceType::Shape shape = {
29 static_cast<fir::SequenceType::Extent>(rank)};
30 auto extentTy = fir::SequenceType::get(shape, i64Ty);
31 auto refTy = fir::ReferenceType::get(i64Ty);
32 // Position of the bufferPointer in the header struct.
33 auto one = builder.createIntegerConstant(loc, i32Ty, 1);
34 auto eleTy = fir::unwrapSequenceType(fir::unwrapRefType(header.getType()));
35 auto ptrTy = builder.getRefType(eleTy.cast<mlir::TupleType>().getType(1));
36 auto ptr = builder.create<fir::CoordinateOp>(loc, ptrTy, header, one);
37 auto heap = builder.create<fir::LoadOp>(loc, ptr);
38 auto cmp = builder.genIsNullAddr(loc, heap);
39 builder.genIfThen(loc, cmp)
40 .genThen([&]() {
41 auto asHeadersVal = builder.createIntegerConstant(loc, i1Ty, asHeaders);
42 auto rankVal = builder.createIntegerConstant(loc, i64Ty, rank);
43 auto buff = builder.create<fir::AllocMemOp>(loc, extentTy);
44 // Convert all the extents to i64 and pack them in a buffer on the heap.
45 for (auto i : llvm::enumerate(extents)) {
46 auto offset = builder.createIntegerConstant(loc, i32Ty, i.index());
47 auto addr =
48 builder.create<fir::CoordinateOp>(loc, refTy, buff, offset);
49 auto castVal = builder.createConvert(loc, i64Ty, i.value());
50 builder.create<fir::StoreOp>(loc, castVal, addr);
51 }
52 auto args = fir::runtime::createArguments(
53 builder, loc, fTy, header, asHeadersVal, rankVal, eleSize, buff);
54 builder.create<fir::CallOp>(loc, func, args);
55 })
56 .end();
57}
58
59void fir::runtime::genRaggedArrayDeallocate(mlir::Location loc,
60 fir::FirOpBuilder &builder,
61 mlir::Value header) {
62 auto func = fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayDeallocate)>(
63 loc, builder);
64 auto fTy = func.getFunctionType();
65 auto args = fir::runtime::createArguments(builder, loc, fTy, header);
66 builder.create<fir::CallOp>(loc, func, args);
67}
68

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