|
| 1 | +//===-- Runtime/ragged.h ----------------------------------------*- C++ -*-===// |
| 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 | +#ifndef FORTRAN_RUNTIME_RAGGED_H_ |
| 10 | +#define FORTRAN_RUNTIME_RAGGED_H_ |
| 11 | + |
| 12 | +#include "flang/Runtime/entry-names.h" |
| 13 | +#include <cstdint> |
| 14 | + |
| 15 | +namespace Fortran::runtime { |
| 16 | + |
| 17 | +// A ragged array header block. |
| 18 | +// The header block is used to create the "array of arrays" ragged data |
| 19 | +// structure. It contains a pair in `flags` to indicate if the header points to |
| 20 | +// an array of headers (isIndirection) or data elements and the rank of the |
| 21 | +// pointed-to array. The rank is the length of the extents vector accessed |
| 22 | +// through `extentPointer`. The `bufferPointer` is overloaded and is null, |
| 23 | +// points to an array of headers (isIndirection), or data. |
| 24 | +// By default, a header is set to zero, which is its unused state. |
| 25 | +// The layout of a ragged buffer header is mirrored in the compiler. |
| 26 | +struct RaggedArrayHeader { |
| 27 | + bool indirection{false}; |
| 28 | + std::uint8_t rank; |
| 29 | + void *bufferPointer{nullptr}; |
| 30 | + std::int64_t *extentPointer{nullptr}; |
| 31 | + |
| 32 | + bool isIndirection() { return indirection; } |
| 33 | + std::uint8_t getRank() { return rank; } |
| 34 | +}; |
| 35 | + |
| 36 | +RaggedArrayHeader *RaggedArrayAllocate( |
| 37 | + RaggedArrayHeader *, bool, std::int64_t, std::int64_t, std::int64_t *); |
| 38 | + |
| 39 | +void RaggedArrayDeallocate(RaggedArrayHeader *); |
| 40 | + |
| 41 | +extern "C" { |
| 42 | + |
| 43 | +// For more on ragged arrays see https://en.wikipedia.org/wiki/Jagged_array. The |
| 44 | +// Flang compiler allocates ragged arrays as a generalization for |
| 45 | +// non-rectangular array temporaries. Ragged arrays can be allocated recursively |
| 46 | +// and on demand. Structurally, each leaf is an optional rectangular array of |
| 47 | +// elements. The shape of each leaf is independent and may be computed on |
| 48 | +// demand. Each branch node is an optional, possibly sparse rectangular array of |
| 49 | +// headers. The shape of each branch is independent and may be computed on |
| 50 | +// demand. Ragged arrays preserve a correspondence between a multidimensional |
| 51 | +// iteration space and array access vectors, which is helpful for dependence |
| 52 | +// analysis. |
| 53 | + |
| 54 | +// Runtime helper for allocation of ragged array buffers. |
| 55 | +// A pointer to the header block to be allocated is given as header. The flag |
| 56 | +// isHeader indicates if a block of headers or data is to be allocated. A |
| 57 | +// non-negative rank indicates the length of the extentVector, which is a list |
| 58 | +// of non-negative extents. elementSize is the size of a data element in the |
| 59 | +// rectangular space defined by the extentVector. |
| 60 | +void *RTNAME(RaggedArrayAllocate)(void *header, bool isHeader, |
| 61 | + std::int64_t rank, std::int64_t elementSize, std::int64_t *extentVector); |
| 62 | + |
| 63 | +// Runtime helper for deallocation of ragged array buffers. The root header of |
| 64 | +// the ragged array structure is passed to deallocate the entire ragged array. |
| 65 | +void RTNAME(RaggedArrayDeallocate)(void *raggedArrayHeader); |
| 66 | + |
| 67 | +} // extern "C" |
| 68 | +} // namespace Fortran::runtime |
| 69 | +#endif // FORTRAN_RUNTIME_RAGGED_H_ |
0 commit comments