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
14 using namespace Fortran::runtime;
15
genRaggedArrayAllocate(mlir::Location loc,fir::FirOpBuilder & builder,mlir::Value header,bool asHeaders,mlir::Value eleSize,mlir::ValueRange extents)16 void 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 =
36 builder.getRefType(mlir::cast<mlir::TupleType>(eleTy).getType(1));
37 auto ptr = builder.create<fir::CoordinateOp>(loc, ptrTy, header, one);
38 auto heap = builder.create<fir::LoadOp>(loc, ptr);
39 auto cmp = builder.genIsNullAddr(loc, heap);
40 builder.genIfThen(loc, cmp)
41 .genThen([&]() {
42 auto asHeadersVal = builder.createIntegerConstant(loc, i1Ty, asHeaders);
43 auto rankVal = builder.createIntegerConstant(loc, i64Ty, rank);
44 auto buff = builder.create<fir::AllocMemOp>(loc, extentTy);
45 // Convert all the extents to i64 and pack them in a buffer on the heap.
46 for (auto i : llvm::enumerate(extents)) {
47 auto offset = builder.createIntegerConstant(loc, i32Ty, i.index());
48 auto addr =
49 builder.create<fir::CoordinateOp>(loc, refTy, buff, offset);
50 auto castVal = builder.createConvert(loc, i64Ty, i.value());
51 builder.create<fir::StoreOp>(loc, castVal, addr);
52 }
53 auto args = fir::runtime::createArguments(
54 builder, loc, fTy, header, asHeadersVal, rankVal, eleSize, buff);
55 builder.create<fir::CallOp>(loc, func, args);
56 })
57 .end();
58 }
59
genRaggedArrayDeallocate(mlir::Location loc,fir::FirOpBuilder & builder,mlir::Value header)60 void fir::runtime::genRaggedArrayDeallocate(mlir::Location loc,
61 fir::FirOpBuilder &builder,
62 mlir::Value header) {
63 auto func = fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayDeallocate)>(
64 loc, builder);
65 auto fTy = func.getFunctionType();
66 auto args = fir::runtime::createArguments(builder, loc, fTy, header);
67 builder.create<fir::CallOp>(loc, func, args);
68 }
69