13#ifndef DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
14#define DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
28SmallVector<utils::IteratorType>
32SmallVector<Value>
condenseValues(
const SmallVector<Value> &values);
42 OpBuilder &rewriter,
bool isUnsigned);
50std::optional<SmallVector<Value>>
55 for (
const Value ¶m : params) {
56 auto paramTy = cast<ShapedType>(param.getType());
57 if (!paramTy.hasStaticShape())
58 dynTypes.push_back(paramTy);
64 for (
const ShapedType &dynTy : dynTypes) {
65 if (llvm::any_of(dynTy.getShape().drop_front(), ShapedType::isDynamic)) {
67 op,
"input can only be dynamic for batch size");
72 dynamicDims.push_back(
73 tensor::DimOp::create(rewriter, op->
getLoc(), params[0], 0));
89template <
typename TosaOp,
typename... Args>
92 auto op = TosaOp::create(builder, resultTy, args...);
94 InferShapedTypeOpInterface shapeInterface =
95 dyn_cast<InferShapedTypeOpInterface>(op.getOperation());
101 .inferReturnTypeComponents(
102 op.getContext(), builder.
getLoc(), op->getOperands(),
103 op->getDiscardableAttrDictionary(), op->getPropertiesStorage(),
104 op->getRegions(), returnedShapes)
112 auto result = op->getResult(0);
113 const auto &predictedShape = returnedShapes[0];
118 inferredKnowledge.dtype = mlir::cast<ShapedType>(resultTy).getElementType();
119 inferredKnowledge.hasRank = predictedShape.hasRank();
120 if (predictedShape.hasRank()) {
121 for (
auto dim : predictedShape.getDims()) {
122 inferredKnowledge.sizes.push_back(dim);
132 :
Type{mlir::UnrankedTensorType::get(newKnowledge.dtype)};
141template <
typename TosaOp,
typename... Args>
144 if (TosaOp::template hasTrait<::mlir::OpTrait::SameOperandsAndResultRank>()) {
146 if constexpr (
sizeof...(Args) == 2) {
147 auto argX = std::get<0>(std::tie(args...));
148 auto argY = std::get<1>(std::tie(args...));
149 using ArgX =
decltype(argX);
150 using ArgY =
decltype(argY);
151 if constexpr (std::is_same_v<ArgX, Value> &&
152 std::is_same_v<ArgY, Value>) {
153 Value x = std::get<0>(std::tie(args...));
154 Value y = std::get<1>(std::tie(args...));
162 if constexpr (
sizeof...(Args) == 3) {
163 auto argX = std::get<0>(std::tie(args...));
164 auto argY = std::get<1>(std::tie(args...));
165 auto argZ = std::get<2>(std::tie(args...));
166 using ArgX =
decltype(argX);
167 using ArgY =
decltype(argY);
168 using ArgZ =
decltype(argZ);
169 if constexpr (std::is_same_v<ArgX, Value> &&
170 std::is_same_v<ArgY, Value> && std::is_same_v<ArgZ, bool>) {
172 Value x = std::get<0>(std::tie(args...));
173 Value y = std::get<1>(std::tie(args...));
174 bool round = std::get<2>(std::tie(args...));
181 if constexpr (std::is_same_v<ArgX, Value> &&
182 std::is_same_v<ArgY, Value> &&
183 std::is_same_v<ArgZ, Value>) {
185 Value x = std::get<0>(std::tie(args...));
186 Value y = std::get<1>(std::tie(args...));
187 Value z = std::get<2>(std::tie(args...));
208template <
typename TosaOp,
typename... Args>
210 Type resultTy, Args &&...args) {
221 size_t N = input.size();
222 permuted.resize_for_overwrite(N);
223 for (
size_t i = 0; i < N; i++)
224 permuted[i] = input[perms[i]];
251 if (
auto denseResource = dyn_cast<DenseResourceElementsAttr>(attr)) {
263 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)
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)