|
| 1 | +//===- PermutationRef.h - Permutation reference wrapper ---------*- 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 | +// This header is not part of the public API. It is placed in the |
| 10 | +// includes directory only because that's required by the implementations |
| 11 | +// of template-classes. |
| 12 | +// |
| 13 | +// This file is part of the lightweight runtime support library for sparse |
| 14 | +// tensor manipulations. The functionality of the support library is meant |
| 15 | +// to simplify benchmarking, testing, and debugging MLIR code operating on |
| 16 | +// sparse tensors. However, the provided functionality is **not** part of |
| 17 | +// core MLIR itself. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#ifndef MLIR_EXECUTIONENGINE_SPARSETENSOR_PERMUTATIONREF_H |
| 22 | +#define MLIR_EXECUTIONENGINE_SPARSETENSOR_PERMUTATIONREF_H |
| 23 | + |
| 24 | +#include "mlir/ExecutionEngine/SparseTensor/Attributes.h" |
| 25 | +#include "mlir/ExecutionEngine/SparseTensor/ErrorHandling.h" |
| 26 | + |
| 27 | +#include <cassert> |
| 28 | +#include <cinttypes> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +namespace mlir { |
| 32 | +namespace sparse_tensor { |
| 33 | +namespace detail { |
| 34 | + |
| 35 | +/// Checks whether the `perm` array is a permutation of `[0 .. size)`. |
| 36 | +MLIR_SPARSETENSOR_PURE bool isPermutation(uint64_t size, const uint64_t *perm); |
| 37 | + |
| 38 | +/// Wrapper around `isPermutation` to ensure consistent error messages. |
| 39 | +inline void assertIsPermutation(uint64_t size, const uint64_t *perm) { |
| 40 | +#ifndef NDEBUG |
| 41 | + if (!isPermutation(size, perm)) |
| 42 | + MLIR_SPARSETENSOR_FATAL("Not a permutation of [0..%" PRIu64 ")\n", size); |
| 43 | +#endif |
| 44 | +} |
| 45 | + |
| 46 | +// TODO: To implement things like `inverse` and `compose` while preserving |
| 47 | +// the knowledge that `isPermutation` is true, we'll need to also have |
| 48 | +// an owning version of `PermutationRef`. (Though ideally we'll really |
| 49 | +// want to defunctionalize those methods so that we can avoid intermediate |
| 50 | +// arrays/copies and only materialize the data on request.) |
| 51 | + |
| 52 | +/// A non-owning class for capturing the knowledge that `isPermutation` |
| 53 | +/// is true, to avoid needing to assert it repeatedly. |
| 54 | +class MLIR_SPARSETENSOR_GSL_POINTER [[nodiscard]] PermutationRef final { |
| 55 | +public: |
| 56 | + /// Asserts `isPermutation` and returns the witness to that being true. |
| 57 | + // |
| 58 | + // TODO: For now the assertive ctor is sufficient, but in principle |
| 59 | + // we'll want a factory that can optionally construct the object |
| 60 | + // (so callers can handle errors themselves). |
| 61 | + explicit PermutationRef(uint64_t size, const uint64_t *perm) |
| 62 | + : permSize(size), perm(perm) { |
| 63 | + assertIsPermutation(size, perm); |
| 64 | + } |
| 65 | + |
| 66 | + uint64_t size() const { return permSize; } |
| 67 | + |
| 68 | + const uint64_t *data() const { return perm; } |
| 69 | + |
| 70 | + const uint64_t &operator[](uint64_t i) const { |
| 71 | + assert(i < permSize && "index is out of bounds"); |
| 72 | + return perm[i]; |
| 73 | + } |
| 74 | + |
| 75 | + /// Constructs a pushforward array of values. This method is the inverse |
| 76 | + /// of `permute` in the sense that for all `p` and `xs` we have: |
| 77 | + /// * `p.permute(p.pushforward(xs)) == xs` |
| 78 | + /// * `p.pushforward(p.permute(xs)) == xs` |
| 79 | + template <typename T> |
| 80 | + inline std::vector<T> pushforward(const std::vector<T> &values) const { |
| 81 | + return pushforward(values.size(), values.data()); |
| 82 | + } |
| 83 | + |
| 84 | + template <typename T> |
| 85 | + inline std::vector<T> pushforward(uint64_t size, const T *values) const { |
| 86 | + std::vector<T> out(permSize); |
| 87 | + pushforward(size, values, out.data()); |
| 88 | + return out; |
| 89 | + } |
| 90 | + |
| 91 | + // NOTE: This form of the method is required by `toMLIRSparseTensor`, |
| 92 | + // so it can reuse the `out` buffer for each iteration of a loop. |
| 93 | + template <typename T> |
| 94 | + inline void pushforward(uint64_t size, const T *values, T *out) const { |
| 95 | + assert(size == permSize && "size mismatch"); |
| 96 | + for (uint64_t i = 0; i < permSize; ++i) |
| 97 | + out[perm[i]] = values[i]; |
| 98 | + } |
| 99 | + |
| 100 | + // NOTE: this is only needed by `toMLIRSparseTensor`, which in |
| 101 | + // turn only needs it as a vector to hand off to `newSparseTensor`. |
| 102 | + // Otherwise we would want the result to be an owning-permutation, |
| 103 | + // to retain the knowledge that `isPermutation` is true. |
| 104 | + // |
| 105 | + /// Constructs the inverse permutation. This is equivalent to calling |
| 106 | + /// `pushforward` with `std::iota` for the values. |
| 107 | + std::vector<uint64_t> inverse() const; |
| 108 | + |
| 109 | + /// Constructs a permuted array of values. This method is the inverse |
| 110 | + /// of `pushforward` in the sense that for all `p` and `xs` we have: |
| 111 | + /// * `p.permute(p.pushforward(xs)) == xs` |
| 112 | + /// * `p.pushforward(p.permute(xs)) == xs` |
| 113 | + template <typename T> |
| 114 | + inline std::vector<T> permute(const std::vector<T> &values) const { |
| 115 | + return permute(values.size(), values.data()); |
| 116 | + } |
| 117 | + |
| 118 | + template <typename T> |
| 119 | + inline std::vector<T> permute(uint64_t size, const T *values) const { |
| 120 | + std::vector<T> out(permSize); |
| 121 | + permute(size, values, out.data()); |
| 122 | + return out; |
| 123 | + } |
| 124 | + |
| 125 | + template <typename T> |
| 126 | + inline void permute(uint64_t size, const T *values, T *out) const { |
| 127 | + assert(size == permSize && "size mismatch"); |
| 128 | + for (uint64_t i = 0; i < permSize; ++i) |
| 129 | + out[i] = values[perm[i]]; |
| 130 | + } |
| 131 | + |
| 132 | +private: |
| 133 | + const uint64_t permSize; |
| 134 | + const uint64_t *const perm; // non-owning pointer. |
| 135 | +}; |
| 136 | + |
| 137 | +} // namespace detail |
| 138 | +} // namespace sparse_tensor |
| 139 | +} // namespace mlir |
| 140 | + |
| 141 | +#endif // MLIR_EXECUTIONENGINE_SPARSETENSOR_PERMUTATIONREF_H |
0 commit comments