13#ifndef DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
14#define DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
29SmallVector<utils::IteratorType>
33SmallVector<Value>
condenseValues(
const SmallVector<Value> &values);
43 OpBuilder &rewriter,
bool isUnsigned);
51std::optional<SmallVector<Value>>
56 for (
const Value ¶m : params) {
57 auto paramTy = cast<ShapedType>(param.getType());
58 if (!paramTy.hasStaticShape())
59 dynTypes.push_back(paramTy);
65 for (
const ShapedType &dynTy : dynTypes) {
66 if (llvm::any_of(dynTy.getShape().drop_front(), ShapedType::isDynamic)) {
68 op,
"input can only be dynamic for batch size");
73 dynamicDims.push_back(
74 tensor::DimOp::create(rewriter, op->
getLoc(), params[0], 0));
92template <
typename TosaOp,
typename... Args>
95 auto op = TosaOp::create(builder, resultTy, args...);
97 InferShapedTypeOpInterface shapeInterface =
98 dyn_cast<InferShapedTypeOpInterface>(op.getOperation());
104 .inferReturnTypeComponents(op.getContext(), builder.
getLoc(),
105 op->getOperands(), op->getAttrDictionary(),
106 op->getPropertiesStorage(),
107 op->getRegions(), returnedShapes)
115 auto result = op->getResult(0);
116 auto predictedShape = returnedShapes[0];
121 inferredKnowledge.dtype = mlir::cast<ShapedType>(resultTy).getElementType();
122 inferredKnowledge.hasRank = predictedShape.hasRank();
123 if (predictedShape.hasRank()) {
124 for (
auto dim : predictedShape.getDims()) {
125 inferredKnowledge.sizes.push_back(dim);
133 ? Type{mlir::RankedTensorType::get(llvm::ArrayRef(newKnowledge.sizes),
135 : Type{mlir::UnrankedTensorType::get(newKnowledge.dtype)};
146template <
typename TosaOp,
typename... Args>
149 if (TosaOp::template hasTrait<::mlir::OpTrait::SameOperandsAndResultRank>()) {
151 if constexpr (
sizeof...(Args) == 2) {
152 auto argX = std::get<0>(std::tie(args...));
153 auto argY = std::get<1>(std::tie(args...));
154 using ArgX =
decltype(argX);
155 using ArgY =
decltype(argY);
156 if constexpr (std::is_same_v<ArgX, Value> &&
157 std::is_same_v<ArgY, Value>) {
158 Value x = std::get<0>(std::tie(args...));
159 Value y = std::get<1>(std::tie(args...));
164 return createOpAndInferShape<TosaOp>(builder, resultTy, x, y);
167 if constexpr (
sizeof...(Args) == 3) {
168 auto argX = std::get<0>(std::tie(args...));
169 auto argY = std::get<1>(std::tie(args...));
170 auto argZ = std::get<2>(std::tie(args...));
171 using ArgX =
decltype(argX);
172 using ArgY =
decltype(argY);
173 using ArgZ =
decltype(argZ);
174 if constexpr (std::is_same_v<ArgX, Value> &&
175 std::is_same_v<ArgY, Value> && std::is_same_v<ArgZ, bool>) {
177 Value x = std::get<0>(std::tie(args...));
178 Value y = std::get<1>(std::tie(args...));
179 bool round = std::get<2>(std::tie(args...));
184 return createOpAndInferShape<TosaOp>(builder, resultTy, x, y, round);
186 if constexpr (std::is_same_v<ArgX, Value> &&
187 std::is_same_v<ArgY, Value> &&
188 std::is_same_v<ArgZ, Value>) {
190 Value x = std::get<0>(std::tie(args...));
191 Value y = std::get<1>(std::tie(args...));
192 Value z = std::get<2>(std::tie(args...));
201 return createOpAndInferShape<TosaOp>(builder, resultTy, x, y, z);
206 return createOpAndInferShape<TosaOp>(builder, resultTy, args...);
213template <
typename TosaOp,
typename... Args>
215 Type resultTy, Args &&...args) {
226 size_t N = input.size();
227 permuted.resize_for_overwrite(N);
228 for (
size_t i = 0; i < N; i++)
229 permuted[i] = input[perms[i]];
256 if (
auto denseResource = dyn_cast<DenseResourceElementsAttr>(attr)) {
268 return blob->template getDataAs<T>();
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
This class represents a processed binary blob of data.
ArrayRef< char > getData() const
Return the raw underlying data of this blob.
An attribute that represents a reference to a dense vector or tensor object.
static bool isValidRawBuffer(ShapedType type, ArrayRef< char > rawBuffer)
Returns true if the given buffer is a valid raw buffer for the given type.
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...
Location getLoc() const
Accessors for the implied location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Location getLoc()
The source location the operation was defined or derived from.
This provides public APIs that all operations should have.
Operation is the basic unit of execution within MLIR.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Value clampFloatHelper(Location loc, Value arg, Value min, Value max, OpBuilder &rewriter)
SmallVector< T > applyTOSAPermutation(ArrayRef< T > input, ArrayRef< int32_t > perms)
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.
std::optional< SmallVector< Value > > checkHasDynamicBatchDims(PatternRewriter &rewriter, Op op, ArrayRef< Value > params)
TosaOp CreateOpAndInferShape(ImplicitLocOpBuilder &builder, Type resultTy, Args &&...args)
SmallVector< int64_t > convertFromIntAttr(const DenseElementsAttr &attr, const int rank)
std::optional< ArrayRef< T > > tryGetDenseResourceValues(ElementsAttr attr)
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.
static ValueKnowledge join(const ValueKnowledge &lhs, const ValueKnowledge &rhs)
static ValueKnowledge getPessimisticValueState()
static ValueKnowledge getKnowledgeFromType(Type type)