MLIR 23.0.0git
ConversionUtils.cpp
Go to the documentation of this file.
1//===- ConversionUtils.cpp ------------------------------------------------===//
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//
9// Utility functions for TOSA lowering
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/ADT/SmallVectorExtras.h"
16
17using namespace mlir;
18using namespace mlir::tosa;
19
21mlir::tosa::getNParallelLoopsAttrs(unsigned nParallelLoops) {
22 return SmallVector<utils::IteratorType>(nParallelLoops,
23 utils::IteratorType::parallel);
24}
25
28 SmallVector<Value> condensedValues;
29 for (auto value : values)
30 if (value)
31 condensedValues.push_back(value);
32 return condensedValues;
33}
34
36 Value max, OpBuilder &rewriter) {
37 Value minValue = arith::MinimumFOp::create(rewriter, loc, arg, max);
38 return arith::MaximumFOp::create(rewriter, loc, minValue, min);
39}
40
42 OpBuilder &rewriter, bool isUnsigned) {
43 if (isUnsigned) {
44 auto minOrArg = arith::MaxUIOp::create(rewriter, loc, min, arg);
45 return arith::MinUIOp::create(rewriter, loc, max, minOrArg);
46 }
47 auto minOrArg = arith::MaxSIOp::create(rewriter, loc, min, arg);
48 return arith::MinSIOp::create(rewriter, loc, max, minOrArg);
49}
50
51bool mlir::tosa::validIntegerRange(IntegerType ty, int64_t value) {
52 uint64_t bitwidth = ty.getIntOrFloatBitWidth();
53 if (ty.getSignedness() == IntegerType::Unsigned) {
54 uint64_t uvalue = value;
55 APInt intMin = APInt::getMinValue(bitwidth);
56 APInt intMax = APInt::getMaxValue(bitwidth);
57 return uvalue >= intMin.getZExtValue() && uvalue <= intMax.getZExtValue();
58 }
59
60 APInt intMin = APInt::getSignedMinValue(bitwidth);
61 APInt intMax = APInt::getSignedMaxValue(bitwidth);
62 return value >= intMin.getSExtValue() && value <= intMax.getSExtValue();
63}
64
65namespace {
66// Given two tensors of high and low ranks, derive the output shape
67// to reshape the lower rank to.
68// Examples:
69// If lower=[c], higher=[a, b, c], [c] reshaped into [1, 1, c].
70// If lower=[b, c], higher=[a, b, c], [b, c] reshaped into [1, b, c].
71// If lower=[a], higher=[a, a], [a] reshaped into [1, a].
72// If lower=[a], target=[a, b, a], [a] reshaped into [1, 1, a].
73// If lower=[], target=[a, b, c], [] reshaped into [1, 1, 1].
74// If lower=[c], higher=[?, ?, c], [c] reshaped into [1, 1, c].
75// If lower=[?], higher=[?, ?, ?], [?] reshaped into [1, 1, ?].
76LogicalResult
77computeReshapeOutput(ArrayRef<int64_t> higherRankShape,
78 ArrayRef<int64_t> lowerRankShape,
79 SmallVectorImpl<int64_t> &reshapeOutputShape) {
80 // Initialize new shapes with [1] * higherRank.
81 int64_t higherRank = higherRankShape.size();
82 int64_t lowerRank = lowerRankShape.size();
83 reshapeOutputShape.assign(higherRank, 1);
84
85 int64_t higherRankDim;
86 int64_t lowerRankDim;
87 const int64_t rankDiff = higherRank - lowerRank;
88
89 for (int64_t i = lowerRank - 1; i >= 0; i--) {
90 higherRankDim = higherRankShape[i + rankDiff];
91 lowerRankDim = lowerRankShape[i];
92
93 auto isStaticDimAndNotEqualToOne = [](int64_t dim) {
94 return dim != 1 && dim != ShapedType::kDynamic;
95 };
96
97 if (isStaticDimAndNotEqualToOne(lowerRankDim) &&
98 isStaticDimAndNotEqualToOne(higherRankDim) &&
99 lowerRankDim != higherRankDim)
100 return failure();
101
102 reshapeOutputShape[i + rankDiff] = lowerRankDim == 1 ? 1 : lowerRankDim;
103 }
104 return success();
105}
106} // namespace
107
109 Value &input1, Value &input2) {
110 ImplicitLocOpBuilder builder(loc, rewriter);
111 return EqualizeRanks(builder, input1, input2);
112}
113
115 Value &input1, Value &input2) {
116 auto input1Ty = llvm::dyn_cast<RankedTensorType>(input1.getType());
117 auto input2Ty = llvm::dyn_cast<RankedTensorType>(input2.getType());
118
119 if (!input1Ty || !input2Ty) {
120 return failure();
121 }
122
123 int64_t input1Rank = input1Ty.getRank();
124 int64_t input2Rank = input2Ty.getRank();
125
126 if (input1Rank == input2Rank)
127 return success();
128
129 Value higherTensorValue, lowerTensorValue;
130 if (input1Rank > input2Rank) {
131 higherTensorValue = input1;
132 lowerTensorValue = input2;
133 } else {
134 higherTensorValue = input2;
135 lowerTensorValue = input1;
136 }
137
138 ArrayRef<int64_t> higherRankShape =
139 llvm::cast<RankedTensorType>(higherTensorValue.getType()).getShape();
140 ArrayRef<int64_t> lowerRankShape =
141 llvm::cast<RankedTensorType>(lowerTensorValue.getType()).getShape();
142
143 SmallVector<int64_t, 4> reshapeOutputShape;
144
145 if (computeReshapeOutput(higherRankShape, lowerRankShape, reshapeOutputShape)
146 .failed())
147 return failure();
148
149 auto reshapeInputType =
150 llvm::cast<RankedTensorType>(lowerTensorValue.getType());
151 auto reshapeOutputType = RankedTensorType::get(
152 ArrayRef<int64_t>(reshapeOutputShape), reshapeInputType.getElementType());
153 auto reshapeOutputShapeValue = getTosaConstShape(builder, reshapeOutputShape);
154
155 auto reshapeLower = tosa::ReshapeOp::create(
156 builder, reshapeOutputType, lowerTensorValue, reshapeOutputShapeValue);
157
158 if (input1Rank > input2Rank) {
159 input1 = higherTensorValue;
160 input2 = reshapeLower.getResult();
161 } else {
162 input1 = reshapeLower.getResult();
163 input2 = higherTensorValue;
164 }
165
166 return success();
167}
168
171 auto attr = builder.getIndexTensorAttr(convertFromMlirShape(shape));
172 auto type = mlir::tosa::shapeType::get(builder.getContext(), shape.size());
173 mlir::Operation *mlirOp = tosa::ConstShapeOp::create(builder, type, attr);
174 return mlirOp->getResult(0);
175}
176
182
184 return map_to_vector(
185 shape, [](int64_t dim) { return ShapedType::isDynamic(dim) ? -1 : dim; });
186}
187
189 llvm::SmallVector<int64_t> &resultShape) {
190 if (!op) {
191 return false;
192 }
193 if (auto constOp = mlir::dyn_cast<tosa::ConstShapeOp>(op)) {
194 Attribute constOpAttr = constOp->getAttr("values");
195 DenseElementsAttr elementsAttr = cast<DenseElementsAttr>(constOpAttr);
196 for (int i = 0; i < elementsAttr.size(); i++) {
197 int64_t val = elementsAttr.getValues<int64_t>()[i];
198 resultShape.push_back(val);
199 }
200 return true;
201 }
202 // for undefined op, return false.
203 return false;
204}
205
206// returns a small vector of int64_t values that attr contains
209 if (attr.isSplat()) {
210 int64_t v = attr.getSplatValue<APInt>().getSExtValue();
211 return SmallVector<int64_t>(rank, v);
212 }
213
214 if (auto intArrayAttr = llvm::dyn_cast<DenseIntElementsAttr>(attr)) {
216 for (APInt val : intArrayAttr.getValues<APInt>()) {
217 vec.push_back(val.getSExtValue());
218 }
219 return vec;
220 }
221 return {};
222}
223
225 ShapedType indicesType, DenseIntElementsAttr indicesAttr) {
226 const llvm::ArrayRef<int64_t> indicesShape = indicesType.getShape();
227 const unsigned int indicesRank = indicesShape.size();
228 const unsigned int lastDimSize = indicesShape[indicesRank - 1];
229
230 // check each batch of indices from the flat indicesAttr values
231 // for duplicates
232 auto const indicesValues = indicesAttr.getValues<APInt>();
233 assert(
234 (indicesValues.size() % lastDimSize == 0) &&
235 "Constant indices data length should be a multiple of indicesShape[-1]");
236
237 std::vector<APInt> indices(lastDimSize);
238 for (auto beg = indicesValues.begin(); beg < indicesValues.end();
239 beg += lastDimSize) {
240 std::copy(beg, beg + lastDimSize, indices.begin());
241 std::sort(indices.begin(), indices.end(),
242 [](const APInt &a, const APInt &b) { return a.slt(b); });
243 if (std::adjacent_find(indices.begin(), indices.end()) != indices.end()) {
244 // found duplicate values in indices in batch
245 return false;
246 }
247 }
248
249 return true;
250}
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
Attributes are known-constant values of operations.
Definition Attributes.h:25
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:193
MLIRContext * getContext() const
Definition Builders.h:56
An attribute that represents a reference to a dense vector or tensor object.
auto getValues() const
Return the held element values as a range of the given type.
std::enable_if_t<!std::is_base_of< Attribute, T >::value||std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for this attribute.
int64_t size() const
Returns the number of elements held by this attribute.
bool isSplat() const
Returns true if this attribute corresponds to a splat, i.e.
An attribute that represents a reference to a dense integer vector or tensor object.
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
Definition Builders.h:630
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:407
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
Value clampFloatHelper(Location loc, Value arg, Value min, Value max, OpBuilder &rewriter)
SmallVector< utils::IteratorType > getNParallelLoopsAttrs(unsigned nParallelLoops)
bool hasUniqueConstantScatterIndices(ShapedType indicesType, DenseIntElementsAttr indicesAttr)
SmallVector< Value > condenseValues(const SmallVector< Value > &values)
LogicalResult EqualizeRanks(PatternRewriter &rewriter, Location loc, Value &input1, Value &input2)
Common code to create the reshape op where necessary to make the rank of two values equal.
SmallVector< int64_t > convertFromIntAttr(const DenseElementsAttr &attr, const int rank)
bool validIntegerRange(IntegerType ty, int64_t value)
Value getTosaConstShape(ImplicitLocOpBuilder &builder, llvm::ArrayRef< int64_t > shape)
SmallVector< int64_t > convertFromMlirShape(ArrayRef< int64_t > shape)
Value clampIntHelper(Location loc, Value arg, Value min, Value max, OpBuilder &rewriter, bool isUnsigned)
bool getConstShapeValues(Operation *op, llvm::SmallVector< int64_t > &result_shape)
Include the generated interface declarations.