xref: /llvm-project/flang/lib/Optimizer/Builder/Runtime/Ragged.cpp (revision fac349a169976f822fb27f03e623fa0d28aec1f3)
1de467afeSValentin Clement //===-- Ragged.cpp --------------------------------------------------------===//
2de467afeSValentin Clement //
3de467afeSValentin Clement // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4de467afeSValentin Clement // See https://llvm.org/LICENSE.txt for license information.
5de467afeSValentin Clement // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6de467afeSValentin Clement //
7de467afeSValentin Clement //===----------------------------------------------------------------------===//
8de467afeSValentin Clement 
9de467afeSValentin Clement #include "flang/Optimizer/Builder/Runtime/Ragged.h"
10de467afeSValentin Clement #include "flang/Optimizer/Builder/FIRBuilder.h"
11de467afeSValentin Clement #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12de467afeSValentin Clement #include "flang/Runtime/ragged.h"
13de467afeSValentin Clement 
14de467afeSValentin Clement using namespace Fortran::runtime;
15de467afeSValentin Clement 
genRaggedArrayAllocate(mlir::Location loc,fir::FirOpBuilder & builder,mlir::Value header,bool asHeaders,mlir::Value eleSize,mlir::ValueRange extents)16de467afeSValentin Clement void fir::runtime::genRaggedArrayAllocate(mlir::Location loc,
17de467afeSValentin Clement                                           fir::FirOpBuilder &builder,
18de467afeSValentin Clement                                           mlir::Value header, bool asHeaders,
19de467afeSValentin Clement                                           mlir::Value eleSize,
20de467afeSValentin Clement                                           mlir::ValueRange extents) {
21de467afeSValentin Clement   auto i32Ty = builder.getIntegerType(32);
22de467afeSValentin Clement   auto rank = extents.size();
23de467afeSValentin Clement   auto i64Ty = builder.getIntegerType(64);
24de467afeSValentin Clement   auto func =
25de467afeSValentin Clement       fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayAllocate)>(loc, builder);
264a3460a7SRiver Riddle   auto fTy = func.getFunctionType();
27de467afeSValentin Clement   auto i1Ty = builder.getIntegerType(1);
28de467afeSValentin Clement   fir::SequenceType::Shape shape = {
29de467afeSValentin Clement       static_cast<fir::SequenceType::Extent>(rank)};
30de467afeSValentin Clement   auto extentTy = fir::SequenceType::get(shape, i64Ty);
31de467afeSValentin Clement   auto refTy = fir::ReferenceType::get(i64Ty);
32de467afeSValentin Clement   // Position of the bufferPointer in the header struct.
33ad3bb7c7SValentin Clement   auto one = builder.createIntegerConstant(loc, i32Ty, 1);
34de467afeSValentin Clement   auto eleTy = fir::unwrapSequenceType(fir::unwrapRefType(header.getType()));
35*fac349a1SChristian Sigg   auto ptrTy =
36*fac349a1SChristian Sigg       builder.getRefType(mlir::cast<mlir::TupleType>(eleTy).getType(1));
37ad3bb7c7SValentin Clement   auto ptr = builder.create<fir::CoordinateOp>(loc, ptrTy, header, one);
38de467afeSValentin Clement   auto heap = builder.create<fir::LoadOp>(loc, ptr);
3970ade047Svdonaldson   auto cmp = builder.genIsNullAddr(loc, heap);
40de467afeSValentin Clement   builder.genIfThen(loc, cmp)
41de467afeSValentin Clement       .genThen([&]() {
42de467afeSValentin Clement         auto asHeadersVal = builder.createIntegerConstant(loc, i1Ty, asHeaders);
43de467afeSValentin Clement         auto rankVal = builder.createIntegerConstant(loc, i64Ty, rank);
44de467afeSValentin Clement         auto buff = builder.create<fir::AllocMemOp>(loc, extentTy);
45de467afeSValentin Clement         // Convert all the extents to i64 and pack them in a buffer on the heap.
46de467afeSValentin Clement         for (auto i : llvm::enumerate(extents)) {
47de467afeSValentin Clement           auto offset = builder.createIntegerConstant(loc, i32Ty, i.index());
48de467afeSValentin Clement           auto addr =
49de467afeSValentin Clement               builder.create<fir::CoordinateOp>(loc, refTy, buff, offset);
50de467afeSValentin Clement           auto castVal = builder.createConvert(loc, i64Ty, i.value());
51de467afeSValentin Clement           builder.create<fir::StoreOp>(loc, castVal, addr);
52de467afeSValentin Clement         }
53de467afeSValentin Clement         auto args = fir::runtime::createArguments(
54de467afeSValentin Clement             builder, loc, fTy, header, asHeadersVal, rankVal, eleSize, buff);
55de467afeSValentin Clement         builder.create<fir::CallOp>(loc, func, args);
56de467afeSValentin Clement       })
57de467afeSValentin Clement       .end();
58de467afeSValentin Clement }
59de467afeSValentin Clement 
genRaggedArrayDeallocate(mlir::Location loc,fir::FirOpBuilder & builder,mlir::Value header)60de467afeSValentin Clement void fir::runtime::genRaggedArrayDeallocate(mlir::Location loc,
61de467afeSValentin Clement                                             fir::FirOpBuilder &builder,
62de467afeSValentin Clement                                             mlir::Value header) {
63de467afeSValentin Clement   auto func = fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayDeallocate)>(
64de467afeSValentin Clement       loc, builder);
654a3460a7SRiver Riddle   auto fTy = func.getFunctionType();
66de467afeSValentin Clement   auto args = fir::runtime::createArguments(builder, loc, fTy, header);
67de467afeSValentin Clement   builder.create<fir::CallOp>(loc, func, args);
68de467afeSValentin Clement }
69