23
23
namespace mlir {
24
24
class ArrayAttr ;
25
25
26
- // / Computes and returns the linearized index of 'offsets' w.r.t. 'basis'.
27
- int64_t linearize (ArrayRef<int64_t > offsets, ArrayRef<int64_t > basis);
28
-
29
- // / Given the strides together with a linear index in the dimension
30
- // / space, returns the vector-space offsets in each dimension for a
31
- // / de-linearized index.
32
- SmallVector<int64_t > delinearize (ArrayRef<int64_t > strides,
33
- int64_t linearIndex);
26
+ // ===----------------------------------------------------------------------===//
27
+ // Utils that operate on static integer values.
28
+ // ===----------------------------------------------------------------------===//
34
29
35
- // / Given a set of sizes, compute and return the strides (i.e. the number of
36
- // / linear incides to skip along the (k-1) most minor dimensions to get the next
37
- // / k-slice). This is also the basis that one can use to linearize an n-D offset
38
- // / confined to `[0 .. sizes]`.
39
- SmallVector<int64_t > computeStrides (ArrayRef<int64_t > sizes);
30
+ // / Given a set of sizes, return the suffix product.
31
+ // /
32
+ // / When applied to slicing, this is the calculation needed to derive the
33
+ // / strides (i.e. the number of linear indices to skip along the (k-1) most
34
+ // / minor dimensions to get the next k-slice).
35
+ // /
36
+ // / This is the basis to linearize an n-D offset confined to `[0 ... sizes]`.
37
+ // /
38
+ // / Assuming `sizes` is `[s0, .. sn]`, return the vector<int64_t>
39
+ // / `[s1 * ... * sn, s2 * ... * sn, ..., sn, 1]`.
40
+ // /
41
+ // / `sizes` elements are asserted to be non-negative.
42
+ // /
43
+ // / Return an empty vector if `sizes` is empty.
44
+ SmallVector<int64_t > computeSuffixProduct (ArrayRef<int64_t > sizes);
45
+ inline SmallVector<int64_t > computeStrides (ArrayRef<int64_t > sizes) {
46
+ return computeSuffixProduct (sizes);
47
+ }
40
48
41
- // / Return a vector containing llvm::zip of v1 and v2 multiplied elementwise.
49
+ // / Return a vector containing llvm::zip_equal(v1, v2) multiplied elementwise.
50
+ // /
51
+ // / Return an empty vector if `v1` and `v2` are empty.
42
52
SmallVector<int64_t > computeElementwiseMul (ArrayRef<int64_t > v1,
43
53
ArrayRef<int64_t > v2);
44
54
45
- // / Compute and return the multi-dimensional integral ratio of `subShape` to
46
- // / the trailing dimensions of `shape`. This represents how many times
47
- // / `subShape` fits within `shape`.
48
- // / If integral division is not possible, return std::nullopt.
55
+ // / Return the number of elements of basis (i.e. the max linear index).
56
+ // / Return `0` if `basis` is empty.
57
+ // /
58
+ // / `basis` elements are asserted to be non-negative.
59
+ // /
60
+ // / Return `0` if `basis` is empty.
61
+ int64_t computeMaxLinearIndex (ArrayRef<int64_t > basis);
62
+
63
+ // / Return the linearized index of 'offsets' w.r.t. 'basis'.
64
+ // /
65
+ // / `basis` elements are asserted to be non-negative.
66
+ int64_t linearize (ArrayRef<int64_t > offsets, ArrayRef<int64_t > basis);
67
+
68
+ // / Given the strides together with a linear index in the dimension space,
69
+ // / return the vector-space offsets in each dimension for a de-linearized index.
70
+ // / `strides` elements are asserted to be non-negative.
71
+ // /
72
+ // / Let `li = linearIndex`, assuming `strides` are `[s0, .. sn]`, return the
73
+ // / vector of int64_t
74
+ // / `[li % s0, (li / s0) % s1, ..., (li / s0 / .. / sn-1) % sn]`
75
+ SmallVector<int64_t > delinearize (int64_t linearIndex,
76
+ ArrayRef<int64_t > strides);
77
+
78
+ // / Return the multi-dimensional integral ratio of `subShape` to the trailing
79
+ // / dimensions of `shape`. This represents how many times `subShape` fits
80
+ // / within `shape`. If integral division is not possible, return std::nullopt.
49
81
// / The trailing `subShape.size()` entries of both shapes are assumed (and
50
- // / enforced) to only contain noonnegative values.
82
+ // / enforced) to only contain non-negative values.
51
83
// /
52
84
// / Examples:
53
85
// / - shapeRatio({3, 5, 8}, {2, 5, 2}) returns {3, 2, 1}.
54
- // / - shapeRatio({3, 8}, {2, 5, 2}) returns std::nullopt (subshape has higher
86
+ // / - shapeRatio({3, 8}, {2, 5, 2}) returns std::nullopt (subshape has
87
+ // / higher
55
88
// / rank).
56
89
// / - shapeRatio({42, 2, 10, 32}, {2, 5, 2}) returns {42, 1, 2, 16} which is
57
90
// / derived as {42(leading shape dim), 2/2, 10/5, 32/2}.
@@ -60,14 +93,96 @@ SmallVector<int64_t> computeElementwiseMul(ArrayRef<int64_t> v1,
60
93
std::optional<SmallVector<int64_t >>
61
94
computeShapeRatio (ArrayRef<int64_t > shape, ArrayRef<int64_t > subShape);
62
95
96
+ // ===----------------------------------------------------------------------===//
97
+ // Utils that operate on AffineExpr.
98
+ // ===----------------------------------------------------------------------===//
99
+
100
+ // / Given a set of sizes, return the suffix product.
101
+ // /
102
+ // / When applied to slicing, this is the calculation needed to derive the
103
+ // / strides (i.e. the number of linear indices to skip along the (k-1) most
104
+ // / minor dimensions to get the next k-slice).
105
+ // /
106
+ // / This is the basis to linearize an n-D offset confined to `[0 ... sizes]`.
107
+ // /
108
+ // / Assuming `sizes` is `[s0, .. sn]`, return the vector<AffineExpr>
109
+ // / `[s1 * ... * sn, s2 * ... * sn, ..., sn, 1]`.
110
+ // /
111
+ // / It is the caller's responsibility to pass proper AffineExpr kind that
112
+ // / result in valid AffineExpr (i.e. cannot multiply 2 AffineDimExpr or divide
113
+ // / by an AffineDimExpr).
114
+ // /
115
+ // / `sizes` elements are expected to bind to non-negative values.
116
+ // /
117
+ // / Return an empty vector if `sizes` is empty.
118
+ SmallVector<AffineExpr> computeSuffixProduct (ArrayRef<AffineExpr> sizes);
119
+ inline SmallVector<AffineExpr> computeStrides (ArrayRef<AffineExpr> sizes) {
120
+ return computeSuffixProduct (sizes);
121
+ }
122
+
123
+ // / Return a vector containing llvm::zip_equal(v1, v2) multiplied elementwise.
124
+ // /
125
+ // / It is the caller's responsibility to pass proper AffineExpr kind that
126
+ // / result in valid AffineExpr (i.e. cannot multiply 2 AffineDimExpr or divide
127
+ // / by an AffineDimExpr).
128
+ // /
129
+ // / Return an empty vector if `v1` and `v2` are empty.
130
+ SmallVector<AffineExpr> computeElementwiseMul (ArrayRef<AffineExpr> v1,
131
+ ArrayRef<AffineExpr> v2);
132
+
63
133
// / Return the number of elements of basis (i.e. the max linear index).
64
134
// / Return `0` if `basis` is empty.
65
- int64_t computeMaxLinearIndex (ArrayRef<int64_t > basis);
135
+ // /
136
+ // / It is the caller's responsibility to pass proper AffineExpr kind that
137
+ // / result in valid AffineExpr (i.e. cannot multiply 2 AffineDimExpr or divide
138
+ // / by an AffineDimExpr).
139
+ // /
140
+ // / `basis` elements are expected to bind to non-negative values.
141
+ // /
142
+ // / Return the `0` AffineConstantExpr if `basis` is empty.
143
+ AffineExpr computeMaxLinearIndex (MLIRContext *ctx, ArrayRef<AffineExpr> basis);
144
+
145
+ // / Return the linearized index of 'offsets' w.r.t. 'basis'.
146
+ // /
147
+ // / Assuming `offsets` is `[o0, .. on]` and `basis` is `[b0, .. bn]`, return the
148
+ // / AffineExpr `o0 * b0 + .. + on * bn`.
149
+ // /
150
+ // / It is the caller's responsibility to pass proper AffineExpr kind that result
151
+ // / in valid AffineExpr (i.e. cannot multiply 2 AffineDimExpr or divide by an
152
+ // / AffineDimExpr).
153
+ // /
154
+ // / `basis` elements are expected to bind to non-negative values.
155
+ AffineExpr linearize (MLIRContext *ctx, ArrayRef<AffineExpr> offsets,
156
+ ArrayRef<AffineExpr> basis);
157
+ AffineExpr linearize (MLIRContext *ctx, ArrayRef<AffineExpr> offsets,
158
+ ArrayRef<int64_t > basis);
159
+
160
+ // / Given the strides together with a linear index in the dimension space,
161
+ // / return the vector-space offsets in each dimension for a de-linearized index.
162
+ // /
163
+ // / Let `li = linearIndex`, assuming `strides` are `[s0, .. sn]`, return the
164
+ // / vector of AffineExpr
165
+ // / `[li % s0, (li / s0) % s1, ..., (li / s0 / .. / sn-1) % sn]`
166
+ // /
167
+ // / It is the caller's responsibility to pass proper AffineExpr kind that result
168
+ // / in valid AffineExpr (i.e. cannot multiply 2 AffineDimExpr or divide by an
169
+ // / AffineDimExpr).
170
+ // /
171
+ // / `strides` elements are expected to bind to non-negative values.
172
+ SmallVector<AffineExpr> delinearize (AffineExpr linearIndex,
173
+ ArrayRef<AffineExpr> strides);
174
+ SmallVector<AffineExpr> delinearize (AffineExpr linearIndex,
175
+ ArrayRef<int64_t > strides);
176
+
177
+ // ===----------------------------------------------------------------------===//
178
+ // Permutation utils.
179
+ // ===----------------------------------------------------------------------===//
66
180
67
181
// / Apply the permutation defined by `permutation` to `inVec`.
68
182
// / Element `i` in `inVec` is mapped to location `j = permutation[i]`.
69
- // / E.g.: for an input vector `inVec = ['a', 'b', 'c']` and a permutation vector
70
- // / `permutation = [2, 0, 1]`, this function leaves `inVec = ['c', 'a', 'b']`.
183
+ // / E.g.: for an input vector `inVec = ['a', 'b', 'c']` and a permutation
184
+ // / vector `permutation = [2, 0, 1]`, this function leaves `inVec = ['c', 'a',
185
+ // / 'b']`.
71
186
template <typename T, unsigned N>
72
187
void applyPermutationToVector (SmallVector<T, N> &inVec,
73
188
ArrayRef<int64_t > permutation) {
@@ -83,18 +198,11 @@ SmallVector<int64_t> invertPermutationVector(ArrayRef<int64_t> permutation);
83
198
// / Method to check if an interchange vector is a permutation.
84
199
bool isPermutationVector (ArrayRef<int64_t > interchange);
85
200
86
- // / Helper that returns a subset of `arrayAttr` as a vector of int64_t.
201
+ // / Helper to return a subset of `arrayAttr` as a vector of int64_t.
202
+ // TODO: Port everything relevant to DenseArrayAttr and drop this util.
87
203
SmallVector<int64_t > getI64SubArray (ArrayAttr arrayAttr, unsigned dropFront = 0 ,
88
204
unsigned dropBack = 0 );
89
205
90
- // / Computes and returns linearized affine expression w.r.t. `basis`.
91
- mlir::AffineExpr getLinearAffineExpr (ArrayRef<int64_t > basis, mlir::Builder &b);
92
-
93
- // / Given the strides in the dimension space, returns the affine expressions for
94
- // / vector-space offsets in each dimension for a de-linearized index.
95
- SmallVector<mlir::AffineExpr>
96
- getDelinearizedAffineExpr (ArrayRef<int64_t > strides, mlir::Builder &b);
97
-
98
206
} // namespace mlir
99
207
100
208
#endif // MLIR_DIALECT_UTILS_INDEXINGUTILS_H
0 commit comments