13#ifndef DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
14#define DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
22#include "llvm/ADT/APFloat.h"
29SmallVector<utils::IteratorType>
33SmallVector<Value>
condenseValues(
const SmallVector<Value> &values);
43 OpBuilder &rewriter,
bool isUnsigned);
58 bool negative,
bool allowNonFinites);
63std::optional<SmallVector<Value>>
68 for (
const Value ¶m : params) {
69 auto paramTy = cast<ShapedType>(param.getType());
70 if (!paramTy.hasStaticShape())
71 dynTypes.push_back(paramTy);
77 for (
const ShapedType &dynTy : dynTypes) {
78 if (llvm::any_of(dynTy.getShape().drop_front(), ShapedType::isDynamic)) {
80 op,
"input can only be dynamic for batch size");
85 dynamicDims.push_back(
86 tensor::DimOp::create(rewriter, op->
getLoc(), params[0], 0));
102template <
typename TosaOp,
typename... Args>
105 auto op = TosaOp::create(builder, resultTy, args...);
107 InferShapedTypeOpInterface shapeInterface =
108 dyn_cast<InferShapedTypeOpInterface>(op.getOperation());
114 .inferReturnTypeComponents(
115 op.getContext(), builder.
getLoc(), op->getOperands(),
116 op->getDiscardableAttrDictionary(), op->getPropertiesStorage(),
117 op->getRegions(), returnedShapes)
125 auto result = op->getResult(0);
126 const auto &predictedShape = returnedShapes[0];
131 inferredKnowledge.dtype = mlir::cast<ShapedType>(resultTy).getElementType();
132 inferredKnowledge.hasRank = predictedShape.hasRank();
133 if (predictedShape.hasRank()) {
134 for (
auto dim : predictedShape.getDims()) {
135 inferredKnowledge.sizes.push_back(dim);
145 :
Type{mlir::UnrankedTensorType::get(newKnowledge.dtype)};
154template <
typename TosaOp,
typename... Args>
157 if (TosaOp::template hasTrait<::mlir::OpTrait::SameOperandsAndResultRank>()) {
159 if constexpr (
sizeof...(Args) == 2) {
160 auto argX = std::get<0>(std::tie(args...));
161 auto argY = std::get<1>(std::tie(args...));
162 using ArgX =
decltype(argX);
163 using ArgY =
decltype(argY);
164 if constexpr (std::is_same_v<ArgX, Value> &&
165 std::is_same_v<ArgY, Value>) {
166 Value x = std::get<0>(std::tie(args...));
167 Value y = std::get<1>(std::tie(args...));
175 if constexpr (
sizeof...(Args) == 3) {
176 auto argX = std::get<0>(std::tie(args...));
177 auto argY = std::get<1>(std::tie(args...));
178 auto argZ = std::get<2>(std::tie(args...));
179 using ArgX =
decltype(argX);
180 using ArgY =
decltype(argY);
181 using ArgZ =
decltype(argZ);
182 if constexpr (std::is_same_v<ArgX, Value> &&
183 std::is_same_v<ArgY, Value> && std::is_same_v<ArgZ, bool>) {
185 Value x = std::get<0>(std::tie(args...));
186 Value y = std::get<1>(std::tie(args...));
187 bool round = std::get<2>(std::tie(args...));
194 if constexpr (std::is_same_v<ArgX, Value> &&
195 std::is_same_v<ArgY, Value> &&
196 std::is_same_v<ArgZ, Value>) {
198 Value x = std::get<0>(std::tie(args...));
199 Value y = std::get<1>(std::tie(args...));
200 Value z = std::get<2>(std::tie(args...));
221template <
typename TosaOp,
typename... Args>
223 Type resultTy, Args &&...args) {
234 size_t N = input.size();
235 permuted.resize_for_overwrite(N);
236 for (
size_t i = 0; i < N; i++)
237 permuted[i] = input[perms[i]];
264 if (
auto denseResource = dyn_cast<DenseResourceElementsAttr>(attr)) {
276 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...
TosaOp createOpAndInferShape(ImplicitLocOpBuilder &builder, Type resultTy, Args &&...args)
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)
APFloat getFloatMinMaxIdentity(const llvm::fltSemantics &semantics, bool negative, bool allowNonFinites)
SmallVector< int64_t > convertFromIntAttr(const DenseElementsAttr &attr, const int rank)
std::optional< ArrayRef< T > > tryGetDenseResourceValues(ElementsAttr attr)
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.
static ValueKnowledge join(const ValueKnowledge &lhs, const ValueKnowledge &rhs)
static ValueKnowledge getPessimisticValueState()
static ValueKnowledge getKnowledgeFromType(Type type)