MLIR 22.0.0git
MapRef.h
Go to the documentation of this file.
1//===- MapRef.h - A dim2lvl/lvl2dim map encoding ----------------*- 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// A dim2lvl/lvl2dim map encoding class, with utility methods.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_EXECUTIONENGINE_SPARSETENSOR_MAPREF_H
14#define MLIR_EXECUTIONENGINE_SPARSETENSOR_MAPREF_H
15
16#include <cinttypes>
17
18#include <cassert>
19
20namespace mlir {
21namespace sparse_tensor {
22
23/// A class for capturing the sparse tensor type map with a compact encoding.
24///
25/// Currently, the following situations are supported:
26/// (1) map is a permutation
27/// (2) map has affine ops (restricted set)
28///
29/// The pushforward/backward operations are fast for (1) but incur some obvious
30/// overhead for situation (2).
31///
32class MapRef final {
33public:
34 MapRef(uint64_t d, uint64_t l, const uint64_t *d2l, const uint64_t *l2d);
35
36 //
37 // Push forward maps from dimensions to levels.
38 //
39
40 // Map from dimRank in to lvlRank out.
41 template <typename T>
42 inline void pushforward(const T *in, T *out) const {
43 if (isPermutation) {
44 for (uint64_t l = 0; l < lvlRank; l++) {
45 out[l] = in[dim2lvl[l]];
46 }
47 } else {
48 uint64_t i, c;
49 for (uint64_t l = 0; l < lvlRank; l++)
50 if (isFloor(l, i, c)) {
51 out[l] = in[i] / c;
52 } else if (isMod(l, i, c)) {
53 out[l] = in[i] % c;
54 } else {
55 out[l] = in[dim2lvl[l]];
56 }
57 }
58 }
59
60 //
61 // Push backward maps from levels to dimensions.
62 //
63
64 // Map from lvlRank in to dimRank out.
65 template <typename T>
66 inline void pushbackward(const T *in, T *out) const {
67 if (isPermutation) {
68 for (uint64_t d = 0; d < dimRank; d++)
69 out[d] = in[lvl2dim[d]];
70 } else {
71 uint64_t i, c, ii;
72 for (uint64_t d = 0; d < dimRank; d++)
73 if (isMul(d, i, c, ii)) {
74 out[d] = in[i] + c * in[ii];
75 } else {
76 out[d] = in[lvl2dim[d]];
77 }
78 }
79 }
80
81 uint64_t getDimRank() const { return dimRank; }
82 uint64_t getLvlRank() const { return lvlRank; }
83
84private:
85 bool isPermutationMap() const;
86
87 bool isFloor(uint64_t l, uint64_t &i, uint64_t &c) const;
88 bool isMod(uint64_t l, uint64_t &i, uint64_t &c) const;
89 bool isMul(uint64_t d, uint64_t &i, uint64_t &c, uint64_t &ii) const;
90
91 const uint64_t dimRank;
92 const uint64_t lvlRank;
93 const uint64_t *const dim2lvl; // non-owning pointer
94 const uint64_t *const lvl2dim; // non-owning pointer
95 const bool isPermutation;
96};
97
98} // namespace sparse_tensor
99} // namespace mlir
100
101#endif // MLIR_EXECUTIONENGINE_SPARSETENSOR_MAPREF_H
static bool isPermutation(const std::vector< PermutationTy > &permutation)
Definition IRAffine.cpp:67
void pushforward(const T *in, T *out) const
Definition MapRef.h:42
uint64_t getLvlRank() const
Definition MapRef.h:82
MapRef(uint64_t d, uint64_t l, const uint64_t *d2l, const uint64_t *l2d)
Definition MapRef.cpp:12
void pushbackward(const T *in, T *out) const
Definition MapRef.h:66
uint64_t getDimRank() const
Definition MapRef.h:81
Include the generated interface declarations.