19#include <mlir/Dialect/Arith/IR/Arith.h>
20#include <mlir/Dialect/Utils/IndexingUtils.h>
21#include <mlir/IR/Matchers.h>
23#include <llvm/ADT/APInt.h>
24#include <llvm/ADT/STLExtras.h>
25#include <llvm/ADT/STLFunctionalExtras.h>
32 : shape(t.getShape()), linearSize(t.getNumElements()), strides(
mlir::computeStrides(shape)) {}
35 assert(t.hasStaticShape());
36 return ArrayIndexGen(t);
41inline bool isInRange(int64_t idx, int64_t dimSize) {
return 0 <= idx && idx < dimSize; }
44template <
typename TypeOfIndex>
inline std::optional<int64_t> toI64(TypeOfIndex index) {
46 if (!mlir::matchPattern(index, mlir::m_ConstantInt(&idxAP))) {
52template <
typename OutType>
struct CheckAndConvert {
53 template <
typename InType>
54 static std::optional<OutType>
from(InType , int64_t ) {
55 static_assert(
sizeof(OutType) == 0,
"CheckAndConvert not implemented for requested type.");
61template <>
struct CheckAndConvert<int64_t> {
62 template <
typename InType>
static std::optional<int64_t>
from(InType index, int64_t dimSize) {
63 if (
auto idxVal = toI64<InType>(index)) {
64 if (isInRange(*idxVal, dimSize)) {
73template <>
struct CheckAndConvert<Attribute> {
74 template <
typename InType>
static std::optional<Attribute>
from(InType index, int64_t dimSize) {
75 if (
auto c = CheckAndConvert<int64_t>::from(index, dimSize)) {
76 return IntegerAttr::get(IndexType::get(index.getContext()), *c);
82template <
typename OutType,
typename InListType>
83inline std::optional<SmallVector<OutType>>
84checkAndConvertMulti(InListType multiDimIndex, ArrayRef<int64_t> shape,
bool mustBeEqual) {
87 llvm::all_equal({llvm::range_size(multiDimIndex), llvm::range_size(shape)}) &&
88 "Iteratees do not have equal length"
91 SmallVector<OutType> ret;
92 for (
auto [idx, dimSize] : llvm::zip_first(multiDimIndex, shape)) {
93 std::optional<OutType> next = CheckAndConvert<OutType>::from(idx, dimSize);
94 if (!next.has_value()) {
97 ret.push_back(next.value());
102inline std::optional<int64_t> linearizeImpl(
103 ArrayRef<int64_t> multiDimIndex,
const ArrayRef<int64_t> &shape,
104 const SmallVector<int64_t> &strides
107 for (
auto [idx, dimSize] : llvm::zip_equal(multiDimIndex, shape)) {
108 if (!isInRange(idx, dimSize)) {
112 return mlir::linearize(multiDimIndex, strides);
115template <
typename TypeOfIndex>
116inline std::optional<int64_t> linearizeImpl(
117 ArrayRef<TypeOfIndex> multiDimIndex,
const ArrayRef<int64_t> &shape,
118 const SmallVector<int64_t> &strides
120 std::optional<SmallVector<int64_t>> conv =
121 checkAndConvertMulti<int64_t>(multiDimIndex, shape,
true );
122 if (!conv.has_value()) {
125 return mlir::linearize(conv.value(), strides);
128template <
typename ResultElemType>
129inline std::optional<SmallVector<ResultElemType>> delinearizeImpl(
130 int64_t linearIndex, int64_t linearSize,
const SmallVector<int64_t> &strides, MLIRContext *ctx,
131 llvm::function_ref<ResultElemType(IntegerAttr)> convert
133 if (!isInRange(linearIndex, linearSize)) {
136 SmallVector<ResultElemType> ret;
137 for (int64_t idx : mlir::delinearize(linearIndex, strides)) {
138 ret.push_back(convert(IntegerAttr::get(IndexType::get(ctx), idx)));
145std::optional<SmallVector<Value>>
147 return delinearizeImpl<Value>(
148 linearIndex, linearSize, strides, bldr.getContext(),
149 [&](IntegerAttr a) { return bldr.create<arith::ConstantOp>(loc, a); }
153std::optional<SmallVector<Attribute>>
155 return delinearizeImpl<Attribute>(linearIndex, linearSize, strides, ctx, [](IntegerAttr a) {
161 static_assert(
sizeof(InListType) == 0,
"linearize() not implemented for requested type.");
162 llvm_unreachable(
"must have concrete instantiation");
167 return linearizeImpl(multiDimIndex, shape, strides);
172 return linearizeImpl(multiDimIndex, shape, strides);
177 return linearizeImpl(multiDimIndex, shape, strides);
181 return linearizeImpl(multiDimIndex, shape, strides);
184template <
typename InListType>
186 static_assert(
sizeof(InListType) == 0,
"checkAndConvert() not implemented for requested type.");
187 llvm_unreachable(
"must have concrete instantiation");
193 return checkAndConvertMulti<Attribute>(multiDimIndex, shape,
false);
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
std::optional< llvm::SmallVector< mlir::Attribute > > checkAndConvert(InListType multiDimIndex)
static ArrayIndexGen from(ArrayType)
Construct new ArrayIndexGen. Will assert if hasStaticShape() is false.
std::optional< int64_t > linearize(InListType multiDimIndex) const
std::optional< llvm::SmallVector< mlir::Value > > delinearize(int64_t, mlir::Location, mlir::OpBuilder &) const
int64_t fromAPInt(const llvm::APInt &i)