MLIR  16.0.0git
IndexingUtils.cpp
Go to the documentation of this file.
1 //===- IndexingUtils.cpp - Helpers related to index computations ----------===//
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 
10 
12 
14  assert(offsets.size() == basis.size());
15  int64_t linearIndex = 0;
16  for (unsigned idx = 0, e = basis.size(); idx < e; ++idx)
17  linearIndex += offsets[idx] * basis[idx];
18  return linearIndex;
19 }
20 
22  int64_t index) {
23  int64_t rank = sliceStrides.size();
24  SmallVector<int64_t, 4> vectorOffsets(rank);
25  for (int64_t r = 0; r < rank; ++r) {
26  assert(sliceStrides[r] > 0);
27  vectorOffsets[r] = index / sliceStrides[r];
28  index %= sliceStrides[r];
29  }
30  return vectorOffsets;
31 }
32 
34  unsigned dropFront,
35  unsigned dropBack) {
36  assert(arrayAttr.size() > dropFront + dropBack && "Out of bounds");
37  auto range = arrayAttr.getAsRange<IntegerAttr>();
39  res.reserve(arrayAttr.size() - dropFront - dropBack);
40  for (auto it = range.begin() + dropFront, eit = range.end() - dropBack;
41  it != eit; ++it)
42  res.push_back((*it).getValue().getSExtValue());
43  return res;
44 }
SmallVector< int64_t, 4 > getI64SubArray(ArrayAttr arrayAttr, unsigned dropFront=0, unsigned dropBack=0)
Helper that returns a subset of arrayAttr as a vector of int64_t.
SmallVector< int64_t, 4 > delinearize(ArrayRef< int64_t > strides, int64_t linearIndex)
Given the strides together with a linear index in the dimension space, returns the vector-space offse...
void dropFront(int64_t arr[N], int64_t *res)
Definition: CRunnerUtils.h:117
int64_t linearize(ArrayRef< int64_t > offsets, ArrayRef< int64_t > basis)
Computes and returns the linearized index of &#39;offsets&#39; w.r.t. &#39;basis&#39;.