LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
ArrayTypeHelper.cpp
Go to the documentation of this file.
1//===-- ArrayTypeHelper.cpp -------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
13//===----------------------------------------------------------------------===//
14
17
18#include <mlir/Dialect/Arith/IR/Arith.h>
19#include <mlir/Dialect/Utils/IndexingUtils.h>
20#include <mlir/IR/Matchers.h>
21
22#include <llvm/ADT/APInt.h>
23#include <llvm/ADT/STLExtras.h>
24#include <llvm/ADT/STLFunctionalExtras.h>
25
26using namespace mlir;
27using namespace llzk;
28using namespace llzk::array;
29
30ArrayIndexGen::ArrayIndexGen(ArrayType t)
31 : shape(t.getShape()), linearSize(t.getNumElements()), strides(mlir::computeStrides(shape)) {}
32
34 assert(t.hasStaticShape());
35 return ArrayIndexGen(t);
36}
37
38namespace {
39
40inline bool isInRange(int64_t idx, int64_t dimSize) { return 0 <= idx && idx < dimSize; }
41
42// This can support Value, Attribute, and Operation* per matchPattern() implementations.
43template <typename TypeOfIndex> inline std::optional<int64_t> toI64(TypeOfIndex index) {
44 llvm::APInt idxAP;
45 if (!mlir::matchPattern(index, mlir::m_ConstantInt(&idxAP))) {
46 return std::nullopt;
47 }
48 return llzk::fromAPInt(idxAP);
49}
50
51template <typename OutType> struct CheckAndConvert {
52 template <typename InType> static std::optional<OutType> from(InType index, int64_t dimSize) {
53 static_assert(sizeof(OutType) == 0, "CheckAndConvert not implemented for requested type.");
54 assert(false);
55 }
56};
57
58// Specialization to produce `int64_t`
59template <> struct CheckAndConvert<int64_t> {
60 template <typename InType> static std::optional<int64_t> from(InType index, int64_t dimSize) {
61 if (auto idxVal = toI64<InType>(index)) {
62 if (isInRange(*idxVal, dimSize)) {
63 return *idxVal;
64 }
65 }
66 return std::nullopt;
67 }
68};
69
70// Specialization to produce `Attribute`
71template <> struct CheckAndConvert<Attribute> {
72 template <typename InType> static std::optional<Attribute> from(InType index, int64_t dimSize) {
73 if (auto c = CheckAndConvert<int64_t>::from(index, dimSize)) {
74 return IntegerAttr::get(IndexType::get(index.getContext()), *c);
75 }
76 return std::nullopt;
77 }
78};
79
80template <typename OutType, typename InListType>
81inline std::optional<SmallVector<OutType>>
82checkAndConvertMulti(InListType multiDimIndex, ArrayRef<int64_t> shape, bool mustBeEqual) {
83 if (mustBeEqual) {
84 assert(
85 llvm::all_equal({llvm::range_size(multiDimIndex), llvm::range_size(shape)}) &&
86 "Iteratees do not have equal length"
87 );
88 }
89 SmallVector<OutType> ret;
90 for (auto [idx, dimSize] : llvm::zip_first(multiDimIndex, shape)) {
91 std::optional<OutType> next = CheckAndConvert<OutType>::from(idx, dimSize);
92 if (!next.has_value()) {
93 return std::nullopt;
94 }
95 ret.push_back(next.value());
96 }
97 return ret;
98}
99
100inline std::optional<int64_t> linearizeImpl(
101 ArrayRef<int64_t> multiDimIndex, const ArrayRef<int64_t> &shape,
102 const SmallVector<int64_t> &strides
103) {
104 // Ensure the index for each dimension is in range. Then the linearized index will be as well.
105 for (auto [idx, dimSize] : llvm::zip_equal(multiDimIndex, shape)) {
106 if (!isInRange(idx, dimSize)) {
107 return std::nullopt;
108 }
109 }
110 return mlir::linearize(multiDimIndex, strides);
111}
112
113template <typename TypeOfIndex>
114inline std::optional<int64_t> linearizeImpl(
115 ArrayRef<TypeOfIndex> multiDimIndex, const ArrayRef<int64_t> &shape,
116 const SmallVector<int64_t> &strides
117) {
118 std::optional<SmallVector<int64_t>> conv =
119 checkAndConvertMulti<int64_t>(multiDimIndex, shape, true /*TODO: I think*/);
120 if (!conv.has_value()) {
121 return std::nullopt;
122 }
123 return mlir::linearize(conv.value(), strides);
124}
125
126template <typename ResultElemType>
127inline std::optional<SmallVector<ResultElemType>> delinearizeImpl(
128 int64_t linearIndex, int64_t linearSize, const SmallVector<int64_t> &strides, MLIRContext *ctx,
129 llvm::function_ref<ResultElemType(IntegerAttr)> convert
130) {
131 if (!isInRange(linearIndex, linearSize)) {
132 return std::nullopt;
133 }
134 SmallVector<ResultElemType> ret;
135 for (int64_t idx : mlir::delinearize(linearIndex, strides)) {
136 ret.push_back(convert(IntegerAttr::get(IndexType::get(ctx), idx)));
137 }
138 return ret;
139}
140
141} // namespace
142
143std::optional<SmallVector<Value>>
144ArrayIndexGen::delinearize(int64_t linearIndex, Location loc, OpBuilder &bldr) const {
145 return delinearizeImpl<Value>(
146 linearIndex, linearSize, strides, bldr.getContext(),
147 [&](IntegerAttr a) { return bldr.create<arith::ConstantOp>(loc, a); }
148 );
149}
150
151std::optional<SmallVector<Attribute>>
152ArrayIndexGen::delinearize(int64_t linearIndex, MLIRContext *ctx) const {
153 return delinearizeImpl<Attribute>(linearIndex, linearSize, strides, ctx, [](IntegerAttr a) {
154 return a;
155 });
156}
157
158template <typename InListType> std::optional<int64_t> ArrayIndexGen::linearize(InListType) const {
159 static_assert(sizeof(InListType) == 0, "linearize() not implemented for requested type.");
160 assert(false);
161}
162
163template <> std::optional<int64_t> ArrayIndexGen::linearize(ArrayRef<int64_t> multiDimIndex) const {
164 return linearizeImpl(multiDimIndex, shape, strides);
165}
166
167template <>
168std::optional<int64_t> ArrayIndexGen::linearize(ArrayRef<Attribute> multiDimIndex) const {
169 return linearizeImpl(multiDimIndex, shape, strides);
170}
171
172template <>
173std::optional<int64_t> ArrayIndexGen::linearize(ArrayRef<Operation *> multiDimIndex) const {
174 return linearizeImpl(multiDimIndex, shape, strides);
175}
176
177template <> std::optional<int64_t> ArrayIndexGen::linearize(ArrayRef<Value> multiDimIndex) const {
178 return linearizeImpl(multiDimIndex, shape, strides);
179}
180
181template <typename InListType>
182std::optional<SmallVector<Attribute>> ArrayIndexGen::checkAndConvert(InListType) {
183 static_assert(sizeof(InListType) == 0, "checkAndConvert() not implemented for requested type.");
184 assert(false);
185}
186
187template <>
188std::optional<SmallVector<Attribute>> ArrayIndexGen::checkAndConvert(OperandRange multiDimIndex) {
189 return checkAndConvertMulti<Attribute>(multiDimIndex, shape, false);
190}
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
Definition LICENSE.txt:45
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(llvm::APInt i)