File: Ragged.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (67 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//===-- Ragged.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "flang/Optimizer/Builder/Runtime/Ragged.h"
#include "flang/Optimizer/Builder/FIRBuilder.h"
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "flang/Runtime/ragged.h"

using namespace Fortran::runtime;

void fir::runtime::genRaggedArrayAllocate(mlir::Location loc,
                                          fir::FirOpBuilder &builder,
                                          mlir::Value header, bool asHeaders,
                                          mlir::Value eleSize,
                                          mlir::ValueRange extents) {
  auto i32Ty = builder.getIntegerType(32);
  auto rank = extents.size();
  auto i64Ty = builder.getIntegerType(64);
  auto func =
      fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayAllocate)>(loc, builder);
  auto fTy = func.getFunctionType();
  auto i1Ty = builder.getIntegerType(1);
  fir::SequenceType::Shape shape = {
      static_cast<fir::SequenceType::Extent>(rank)};
  auto extentTy = fir::SequenceType::get(shape, i64Ty);
  auto refTy = fir::ReferenceType::get(i64Ty);
  // Position of the bufferPointer in the header struct.
  auto one = builder.createIntegerConstant(loc, i32Ty, 1);
  auto eleTy = fir::unwrapSequenceType(fir::unwrapRefType(header.getType()));
  auto ptrTy = builder.getRefType(eleTy.cast<mlir::TupleType>().getType(1));
  auto ptr = builder.create<fir::CoordinateOp>(loc, ptrTy, header, one);
  auto heap = builder.create<fir::LoadOp>(loc, ptr);
  auto cmp = builder.genIsNullAddr(loc, heap);
  builder.genIfThen(loc, cmp)
      .genThen([&]() {
        auto asHeadersVal = builder.createIntegerConstant(loc, i1Ty, asHeaders);
        auto rankVal = builder.createIntegerConstant(loc, i64Ty, rank);
        auto buff = builder.create<fir::AllocMemOp>(loc, extentTy);
        // Convert all the extents to i64 and pack them in a buffer on the heap.
        for (auto i : llvm::enumerate(extents)) {
          auto offset = builder.createIntegerConstant(loc, i32Ty, i.index());
          auto addr =
              builder.create<fir::CoordinateOp>(loc, refTy, buff, offset);
          auto castVal = builder.createConvert(loc, i64Ty, i.value());
          builder.create<fir::StoreOp>(loc, castVal, addr);
        }
        auto args = fir::runtime::createArguments(
            builder, loc, fTy, header, asHeadersVal, rankVal, eleSize, buff);
        builder.create<fir::CallOp>(loc, func, args);
      })
      .end();
}

void fir::runtime::genRaggedArrayDeallocate(mlir::Location loc,
                                            fir::FirOpBuilder &builder,
                                            mlir::Value header) {
  auto func = fir::runtime::getRuntimeFunc<mkRTKey(RaggedArrayDeallocate)>(
      loc, builder);
  auto fTy = func.getFunctionType();
  auto args = fir::runtime::createArguments(builder, loc, fTy, header);
  builder.create<fir::CallOp>(loc, func, args);
}