15 #include "llvm/ADT/STLExtras.h"
21 template <
typename ExprType>
27 for (int64_t r = strides.size() - 2; r >= 0; --r)
28 strides[r] = strides[r + 1] * sizes[r + 1];
32 template <
typename ExprType>
36 if (v1.empty() && v2.empty())
39 for (
auto it : llvm::zip_equal(v1, v2))
40 result.push_back(std::get<0>(it) * std::get<1>(it));
44 template <
typename ExprType>
47 assert(offsets.size() == basis.size());
48 ExprType linearIndex = zero;
49 for (
unsigned idx = 0, e = basis.size(); idx < e; ++idx)
50 linearIndex = linearIndex + offsets[idx] * basis[idx];
54 template <
typename ExprType,
typename DivOpTy>
58 int64_t rank = strides.size();
60 for (int64_t r = 0; r < rank; ++r) {
61 offsets[r] = divOp(linearIndex, strides[r]);
62 linearIndex = linearIndex % strides[r];
72 assert(llvm::all_of(sizes, [](int64_t s) {
return s >= 0; }) &&
73 "sizes must be nonnegative");
84 assert(llvm::all_of(basis, [](int64_t s) {
return s > 0; }) &&
85 "basis must be nonnegative");
88 return std::accumulate(basis.begin(), basis.end(), 1, std::plus<int64_t>());
92 assert(llvm::all_of(basis, [](int64_t s) {
return s > 0; }) &&
93 "basis must be nonnegative");
96 return std::accumulate(basis.begin(), basis.end(), 1,
97 std::multiplies<int64_t>());
101 assert(llvm::all_of(basis, [](int64_t s) {
return s > 0; }) &&
102 "basis must be nonnegative");
109 assert(llvm::all_of(strides, [](int64_t s) {
return s > 0; }) &&
110 "strides must be nonnegative");
112 [](int64_t e1, int64_t e2) {
return e1 / e2; });
115 std::optional<SmallVector<int64_t>>
117 if (shape.size() < subShape.size())
119 assert(llvm::all_of(shape, [](int64_t s) {
return s > 0; }) &&
120 "shape must be nonnegative");
121 assert(llvm::all_of(subShape, [](int64_t s) {
return s > 0; }) &&
122 "subShape must be nonnegative");
125 std::vector<int64_t> result;
126 result.reserve(shape.size());
127 for (
auto [size, subSize] :
128 llvm::zip(llvm::reverse(shape), llvm::reverse(subShape))) {
130 if (size % subSize != 0)
132 result.push_back(size / subSize);
136 int commonSize = subShape.size();
137 std::copy(shape.rbegin() + commonSize, shape.rend(),
138 std::back_inserter(result));
162 return std::accumulate(basis.begin(), basis.end(),
164 std::plus<AffineExpr>());
170 return std::accumulate(basis.begin(), basis.end(),
172 std::multiplies<AffineExpr>());
190 linearIndex, strides,
206 assert(llvm::all_of(permutation, [](int64_t s) {
return s >= 0; }) &&
207 "permutation must be non-negative");
210 inversion[pos.value()] = pos.index();
216 for (
auto i : llvm::seq<int64_t>(0, permutation.size()))
217 if (permutation[i] != i)
223 assert(llvm::all_of(interchange, [](int64_t s) {
return s >= 0; }) &&
224 "permutation must be non-negative");
225 llvm::SmallDenseSet<int64_t, 4> seenVals;
226 for (
auto val : interchange) {
227 if (seenVals.count(val))
229 seenVals.insert(val);
231 return seenVals.size() == interchange.size();
239 for (
auto [pos, desiredPos] : llvm::zip_equal(positions, desiredPositions)) {
240 res[desiredPos] = pos;
244 for (int64_t &entry : res) {
247 while (seen.contains(nextPos))
257 assert(inputPerm.size() >= dropPositions.size() &&
258 "expect inputPerm size large than position to drop");
260 unsigned permSize = inputPerm.size();
261 for (
unsigned inputIndex = 0; inputIndex < permSize; ++inputIndex) {
262 int64_t targetIndex = inputPerm[inputIndex];
263 bool shouldDrop =
false;
264 unsigned dropSize = dropPositions.size();
265 for (
unsigned dropIndex = 0; dropIndex < dropSize; dropIndex++) {
266 if (dropPositions[dropIndex] == inputPerm[inputIndex]) {
270 if (dropPositions[dropIndex] < inputPerm[inputIndex]) {
275 res.push_back(targetIndex);
284 assert(arrayAttr.size() >
dropFront + dropBack &&
"Out of bounds");
285 auto range = arrayAttr.getAsRange<IntegerAttr>();
287 res.reserve(arrayAttr.size() -
dropFront - dropBack);
288 for (
auto it = range.begin() +
dropFront, eit = range.end() - dropBack;
290 res.push_back((*it).getValue().getSExtValue());
296 assert(val &&
"Invalid value");
297 if (
auto attr = dyn_cast<Attribute>(val)) {
298 return attr.getContext();
300 return cast<Value>(val).getContext();
303 std::pair<AffineExpr, SmallVector<OpFoldResult>>
307 assert(strides.size() == indices.size());
308 auto sourceRank =
static_cast<unsigned>(strides.size());
316 values[0] = sourceOffset;
318 for (
unsigned i = 0; i < sourceRank; ++i) {
323 unsigned baseIdxForDim = 1 + 2 * i;
324 unsigned subOffsetForDim = baseIdxForDim;
325 unsigned origStrideForDim = baseIdxForDim + 1;
326 expr = expr + symbols[subOffsetForDim] * symbols[origStrideForDim];
327 values[subOffsetForDim] = indices[i];
328 values[origStrideForDim] = origStride;
331 return {expr, values};
334 std::pair<AffineExpr, SmallVector<OpFoldResult>>
348 unsigned paddedSize) {
349 assert(tileShape.size() <= paddedSize &&
350 "expected tileShape to <= paddedSize");
351 if (tileShape.size() == paddedSize)
352 return to_vector(tileShape);
354 llvm::append_range(result, tileShape);
363 sliceStrides(shape.size()) {
365 std::optional<SmallVector<int64_t>> shapeRatio =
367 assert(shapeRatio && shapeRatio->size() == shape.size() &&
368 "target shape does not evenly divide the original shape");
370 "expected loop order to be a permutation of rank equal to outer "
379 int64_t linearIndex)
const {
381 delinearize(linearIndex, sliceStrides), inverseLoopOrder);
390 delinearize(linearIndex, sliceStrides), inverseLoopOrder);
void dropFront(int64_t arr[N], int64_t *res)
static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder)
Copies the given number of bytes from src to dst pointers.
static MLIRContext * getContext(OpFoldResult val)
static SmallVector< int64_t > padTileShapeToSize(ArrayRef< int64_t > tileShape, unsigned paddedSize)
Apply left-padding by 1 to the tile shape if required.
SmallVector< ExprType > computeElementwiseMulImpl(ArrayRef< ExprType > v1, ArrayRef< ExprType > v2)
SmallVector< ExprType > computeSuffixProductImpl(ArrayRef< ExprType > sizes, ExprType unit)
ExprType linearizeImpl(ArrayRef< ExprType > offsets, ArrayRef< ExprType > basis, ExprType zero)
SmallVector< ExprType > delinearizeImpl(ExprType linearIndex, ArrayRef< ExprType > strides, DivOpTy divOp)
Base type for affine expression.
AffineExpr floorDiv(uint64_t v) const
MLIRContext * getContext() const
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a single result from folding an operation.
MLIRContext * getContext() const
This class provides an abstraction over the different types of ranges over Values.
SmallVector< int64_t > getStaticTileOffsets(int64_t linearIndex) const
TileOffsetRangeImpl(ArrayRef< int64_t > shape, ArrayRef< int64_t > tileShape, ArrayRef< int64_t > loopOrder)
SmallVector< AffineExpr > getDynamicTileOffsets(AffineExpr linearIndex) const
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Include the generated interface declarations.
OpFoldResult getAsIndexOpFoldResult(MLIRContext *ctx, int64_t val)
Convert int64_t to integer attributes of index type and return them as OpFoldResult.
SmallVector< int64_t > computeElementwiseMul(ArrayRef< int64_t > v1, ArrayRef< int64_t > v2)
Return a vector containing llvm::zip_equal(v1, v2) multiplied elementwise.
std::pair< AffineExpr, SmallVector< OpFoldResult > > computeLinearIndex(OpFoldResult sourceOffset, ArrayRef< OpFoldResult > strides, ArrayRef< OpFoldResult > indices)
Compute linear index from provided strides and indices, assuming strided layout.
SmallVector< int64_t > computeStrides(ArrayRef< int64_t > sizes)
SmallVector< T > applyPermutation(ArrayRef< T > input, ArrayRef< int64_t > permutation)
SmallVector< int64_t > delinearize(int64_t linearIndex, ArrayRef< int64_t > strides)
Given the strides together with a linear index in the dimension space, return the vector-space offset...
int64_t computeProduct(ArrayRef< int64_t > basis)
Self-explicit.
bool isIdentityPermutation(ArrayRef< int64_t > permutation)
Returns true if permutation is an identity permutation.
SmallVector< int64_t > computePermutationVector(int64_t permSize, ArrayRef< int64_t > positions, ArrayRef< int64_t > desiredPositions)
Return a permutation vector of size permSize that would result in moving positions into desiredPositi...
SmallVector< int64_t > getI64SubArray(ArrayAttr arrayAttr, unsigned dropFront=0, unsigned dropBack=0)
Helper to return a subset of arrayAttr as a vector of int64_t.
SmallVector< int64_t > computeSuffixProduct(ArrayRef< int64_t > sizes)
Given a set of sizes, return the suffix product.
int64_t computeMaxLinearIndex(ArrayRef< int64_t > basis)
Return the number of elements of basis (i.e.
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.
int64_t linearize(ArrayRef< int64_t > offsets, ArrayRef< int64_t > basis)
Return the linearized index of 'offsets' w.r.t.
SmallVector< AffineExpr > getAffineConstantExprs(ArrayRef< int64_t > constants, MLIRContext *context)
std::optional< SmallVector< int64_t > > computeShapeRatio(ArrayRef< int64_t > shape, ArrayRef< int64_t > subShape)
Return the multi-dimensional integral ratio of subShape to the trailing dimensions of shape.
void applyPermutationToVector(SmallVector< T, N > &inVec, ArrayRef< int64_t > permutation)
Apply the permutation defined by permutation to inVec.
int64_t computeSum(ArrayRef< int64_t > basis)
Self-explicit.
SmallVector< int64_t > dropDims(ArrayRef< int64_t > inputPerm, ArrayRef< int64_t > dropPositions)
Returns a permutation vector that drop the input dims in dropPositions from inputPerm.
bool isPermutationVector(ArrayRef< int64_t > interchange)
Method to check if an interchange vector is a permutation.
void bindSymbolsList(MLIRContext *ctx, MutableArrayRef< AffineExprTy > exprs)
SmallVector< int64_t > invertPermutationVector(ArrayRef< int64_t > permutation)
Helper method to apply to inverse a permutation.