22 utils::IteratorType::parallel);
28 for (
auto value : values)
30 condensedValues.push_back(value);
31 return condensedValues;
36 Value minValue = arith::MinimumFOp::create(rewriter, loc, arg,
max);
37 return arith::MaximumFOp::create(rewriter, loc, minValue,
min);
43 auto minOrArg = arith::MaxUIOp::create(rewriter, loc,
min, arg);
44 return arith::MinUIOp::create(rewriter, loc,
max, minOrArg);
46 auto minOrArg = arith::MaxSIOp::create(rewriter, loc,
min, arg);
47 return arith::MinSIOp::create(rewriter, loc,
max, minOrArg);
51 uint64_t bitwidth = ty.getIntOrFloatBitWidth();
52 if (ty.getSignedness() == IntegerType::Unsigned) {
53 uint64_t uvalue = value;
54 APInt intMin = APInt::getMinValue(bitwidth);
55 APInt intMax = APInt::getMaxValue(bitwidth);
56 return uvalue >= intMin.getZExtValue() && uvalue <= intMax.getZExtValue();
59 APInt intMin = APInt::getSignedMinValue(bitwidth);
60 APInt intMax = APInt::getSignedMaxValue(bitwidth);
61 return value >= intMin.getSExtValue() && value <= intMax.getSExtValue();
80 int64_t higherRank = higherRankShape.size();
81 int64_t lowerRank = lowerRankShape.size();
82 reshapeOutputShape.assign(higherRank, 1);
86 const int64_t rankDiff = higherRank - lowerRank;
88 for (
int64_t i = lowerRank - 1; i >= 0; i--) {
89 higherRankDim = higherRankShape[i + rankDiff];
90 lowerRankDim = lowerRankShape[i];
92 auto isStaticDimAndNotEqualToOne = [](
int64_t dim) {
93 return dim != 1 && dim != ShapedType::kDynamic;
96 if (isStaticDimAndNotEqualToOne(lowerRankDim) &&
97 isStaticDimAndNotEqualToOne(higherRankDim) &&
98 lowerRankDim != higherRankDim)
101 reshapeOutputShape[i + rankDiff] = lowerRankDim == 1 ? 1 : lowerRankDim;
115 auto input1Ty = llvm::dyn_cast<RankedTensorType>(input1.
getType());
116 auto input2Ty = llvm::dyn_cast<RankedTensorType>(input2.
getType());
118 if (!input1Ty || !input2Ty) {
122 int64_t input1Rank = input1Ty.getRank();
123 int64_t input2Rank = input2Ty.getRank();
125 if (input1Rank == input2Rank)
128 Value higherTensorValue, lowerTensorValue;
129 if (input1Rank > input2Rank) {
130 higherTensorValue = input1;
131 lowerTensorValue = input2;
133 higherTensorValue = input2;
134 lowerTensorValue = input1;
138 llvm::cast<RankedTensorType>(higherTensorValue.
getType()).getShape();
140 llvm::cast<RankedTensorType>(lowerTensorValue.
getType()).getShape();
144 if (computeReshapeOutput(higherRankShape, lowerRankShape, reshapeOutputShape)
148 auto reshapeInputType =
149 llvm::cast<RankedTensorType>(lowerTensorValue.
getType());
150 auto reshapeOutputType = RankedTensorType::get(
154 auto reshapeLower = tosa::ReshapeOp::create(
155 builder, reshapeOutputType, lowerTensorValue, reshapeOutputShapeValue);
157 if (input1Rank > input2Rank) {
158 input1 = higherTensorValue;
159 input2 = reshapeLower.getResult();
161 input1 = reshapeLower.getResult();
162 input2 = higherTensorValue;
171 auto type = mlir::tosa::shapeType::get(builder.
getContext(),
shape.size());
172 mlir::Operation *mlirOp = tosa::ConstShapeOp::create(builder, type, attr);
183 return to_vector(llvm::map_range(
shape, [](
int64_t dim) {
184 return ShapedType::isDynamic(dim) ? -1 : dim;
193 if (
auto constOp = mlir::dyn_cast<tosa::ConstShapeOp>(op)) {
194 Attribute constOpAttr = constOp->getAttr(
"values");
196 for (
int i = 0; i < elementsAttr.
size(); i++) {
198 resultShape.push_back(val);
214 if (
auto intArrayAttr = llvm::dyn_cast<DenseIntElementsAttr>(attr)) {
216 for (APInt val : intArrayAttr.getValues<APInt>()) {
217 vec.push_back(val.getSExtValue());
227 const unsigned int indicesRank = indicesShape.size();
228 const unsigned int lastDimSize = indicesShape[indicesRank - 1];
232 auto const indicesValues = indicesAttr.getValues<APInt>();
234 (indicesValues.size() % lastDimSize == 0) &&
235 "Constant indices data length should be a multiple of indicesShape[-1]");
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());
242 [](
const APInt &a,
const APInt &
b) { return a.slt(b); });
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.
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
MLIRContext * getContext() const
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...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
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...
Type getType() const
Return the type of this value.
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.