15#include "llvm/ADT/SmallVectorExtras.h"
23 utils::IteratorType::parallel);
29 for (
auto value : values)
31 condensedValues.push_back(value);
32 return condensedValues;
37 Value minValue = arith::MinimumFOp::create(rewriter, loc, arg,
max);
38 return arith::MaximumFOp::create(rewriter, loc, minValue,
min);
44 auto minOrArg = arith::MaxUIOp::create(rewriter, loc,
min, arg);
45 return arith::MinUIOp::create(rewriter, loc,
max, minOrArg);
47 auto minOrArg = arith::MaxSIOp::create(rewriter, loc,
min, arg);
48 return arith::MinSIOp::create(rewriter, loc,
max, minOrArg);
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();
60 APInt intMin = APInt::getSignedMinValue(bitwidth);
61 APInt intMax = APInt::getSignedMaxValue(bitwidth);
62 return value >= intMin.getSExtValue() && value <= intMax.getSExtValue();
67 bool allowNonFinites) {
68 if (allowNonFinites && APFloat::semanticsHasInf(semantics))
69 return APFloat::getInf(semantics, negative);
70 return APFloat::getLargest(semantics, negative);
89 int64_t higherRank = higherRankShape.size();
90 int64_t lowerRank = lowerRankShape.size();
91 reshapeOutputShape.assign(higherRank, 1);
95 const int64_t rankDiff = higherRank - lowerRank;
97 for (
int64_t i = lowerRank - 1; i >= 0; i--) {
98 higherRankDim = higherRankShape[i + rankDiff];
99 lowerRankDim = lowerRankShape[i];
101 auto isStaticDimAndNotEqualToOne = [](
int64_t dim) {
102 return dim != 1 && dim != ShapedType::kDynamic;
105 if (isStaticDimAndNotEqualToOne(lowerRankDim) &&
106 isStaticDimAndNotEqualToOne(higherRankDim) &&
107 lowerRankDim != higherRankDim)
110 reshapeOutputShape[i + rankDiff] = lowerRankDim == 1 ? 1 : lowerRankDim;
124 auto input1Ty = llvm::dyn_cast<RankedTensorType>(input1.
getType());
125 auto input2Ty = llvm::dyn_cast<RankedTensorType>(input2.
getType());
127 if (!input1Ty || !input2Ty) {
131 int64_t input1Rank = input1Ty.getRank();
132 int64_t input2Rank = input2Ty.getRank();
134 if (input1Rank == input2Rank)
137 Value higherTensorValue, lowerTensorValue;
138 if (input1Rank > input2Rank) {
139 higherTensorValue = input1;
140 lowerTensorValue = input2;
142 higherTensorValue = input2;
143 lowerTensorValue = input1;
147 llvm::cast<RankedTensorType>(higherTensorValue.
getType()).getShape();
149 llvm::cast<RankedTensorType>(lowerTensorValue.
getType()).getShape();
153 if (computeReshapeOutput(higherRankShape, lowerRankShape, reshapeOutputShape)
157 auto reshapeInputType =
158 llvm::cast<RankedTensorType>(lowerTensorValue.
getType());
159 auto reshapeOutputType = RankedTensorType::get(
163 auto reshapeLower = tosa::ReshapeOp::create(
164 builder, reshapeOutputType, lowerTensorValue, reshapeOutputShapeValue);
166 if (input1Rank > input2Rank) {
167 input1 = higherTensorValue;
168 input2 = reshapeLower.getResult();
170 input1 = reshapeLower.getResult();
171 input2 = higherTensorValue;
180 auto type = mlir::tosa::shapeType::get(builder.
getContext(),
shape.size());
181 mlir::Operation *mlirOp = tosa::ConstShapeOp::create(builder, type, attr);
192 return map_to_vector(
193 shape, [](
int64_t dim) {
return ShapedType::isDynamic(dim) ? -1 : dim; });
201 if (
auto constOp = mlir::dyn_cast<tosa::ConstShapeOp>(op)) {
203 for (
int i = 0; i < elementsAttr.
size(); i++) {
205 resultShape.push_back(val);
221 if (
auto intArrayAttr = llvm::dyn_cast<DenseIntElementsAttr>(attr)) {
223 for (APInt val : intArrayAttr.getValues<APInt>()) {
224 vec.push_back(val.getSExtValue());
234 const unsigned int indicesRank = indicesShape.size();
235 const unsigned int lastDimSize = indicesShape[indicesRank - 1];
239 auto const indicesValues = indicesAttr.getValues<APInt>();
241 (indicesValues.size() % lastDimSize == 0) &&
242 "Constant indices data length should be a multiple of indicesShape[-1]");
244 std::vector<APInt>
indices(lastDimSize);
245 for (
auto beg = indicesValues.begin(); beg < indicesValues.end();
246 beg += lastDimSize) {
247 std::copy(beg, beg + lastDimSize,
indices.begin());
249 [](
const APInt &a,
const APInt &
b) { return a.slt(b); });
265 if (!llvm::isa<IntegerType>(attr.getElementType()) ||
266 attr.getNumElements() != 1)
269 return attr.getValues<T>()[0];
272template FailureOr<int32_t>
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
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.
APFloat getFloatMinMaxIdentity(const llvm::fltSemantics &semantics, bool negative, bool allowNonFinites)
SmallVector< int64_t > convertFromIntAttr(const DenseElementsAttr &attr, const int rank)
FailureOr< T > getConstantScalarIntValue(Value val)
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.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.