13 #ifndef DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
14 #define DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
28 SmallVector<utils::IteratorType>
32 SmallVector<Value>
condenseValues(
const SmallVector<Value> &values);
42 OpBuilder &rewriter,
bool isUnsigned);
49 template <
typename Op>
50 std::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 rewriter.
create<tensor::DimOp>(op->
getLoc(), params[0], 0));
91 template <
typename TosaOp,
typename... Args>
94 auto op = builder.
create<TosaOp>(resultTy, args...);
96 InferShapedTypeOpInterface shapeInterface =
97 dyn_cast<InferShapedTypeOpInterface>(op.getOperation());
103 .inferReturnTypeComponents(op.getContext(), builder.getLoc(),
104 op->getOperands(), op->getAttrDictionary(),
105 op->getPropertiesStorage(),
106 op->getRegions(), returnedShapes)
114 auto result = op->getResult(0);
115 auto predictedShape = returnedShapes[0];
120 inferredKnowledge.dtype = mlir::cast<ShapedType>(resultTy).getElementType();
121 inferredKnowledge.hasRank = predictedShape.hasRank();
122 if (predictedShape.hasRank()) {
123 for (
auto dim : predictedShape.getDims()) {
124 inferredKnowledge.sizes.push_back(dim);
135 result.setType(newTy);
145 template <
typename TosaOp,
typename... Args>
148 if (TosaOp::template hasTrait<OpTrait::SameOperandsAndResultRank>()) {
150 if constexpr (
sizeof...(Args) == 2) {
151 auto argX = std::get<0>(std::tie(args...));
152 auto argY = std::get<1>(std::tie(args...));
153 using ArgX = decltype(argX);
154 using ArgY = decltype(argY);
155 if constexpr (std::is_same_v<ArgX, Value> &&
156 std::is_same_v<ArgY, Value>) {
157 Value x = std::get<0>(std::tie(args...));
158 Value y = std::get<1>(std::tie(args...));
163 return createOpAndInferShape<TosaOp>(builder, resultTy, x, y);
166 if constexpr (
sizeof...(Args) == 3) {
167 auto argX = std::get<0>(std::tie(args...));
168 auto argY = std::get<1>(std::tie(args...));
169 auto argZ = std::get<2>(std::tie(args...));
170 using ArgX = decltype(argX);
171 using ArgY = decltype(argY);
172 using ArgZ = decltype(argZ);
173 if constexpr (std::is_same_v<ArgX, Value> &&
174 std::is_same_v<ArgY, Value> && std::is_same_v<ArgZ, bool>) {
176 Value x = std::get<0>(std::tie(args...));
177 Value y = std::get<1>(std::tie(args...));
178 bool round = std::get<2>(std::tie(args...));
183 return createOpAndInferShape<TosaOp>(builder, resultTy, x, y,
round);
185 if constexpr (std::is_same_v<ArgX, Value> &&
186 std::is_same_v<ArgY, Value> &&
187 std::is_same_v<ArgZ, Value>) {
189 Value x = std::get<0>(std::tie(args...));
190 Value y = std::get<1>(std::tie(args...));
191 Value z = std::get<2>(std::tie(args...));
200 return createOpAndInferShape<TosaOp>(builder, resultTy, x, y, z);
205 return createOpAndInferShape<TosaOp>(builder, resultTy, args...);
212 template <
typename TosaOp,
typename... Args>
214 Type resultTy, Args &&...args) {
216 return CreateOpAndInferShape<TosaOp>(builder, resultTy, args...);
221 template <
typename T>
225 size_t N = input.size();
226 permuted.resize_for_overwrite(N);
227 for (
size_t i = 0; i < N; i++)
228 permuted[i] = input[perms[i]];
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
OpTy create(Args &&...args)
Create an operation of specific op type at the current insertion point and location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Location getLoc()
The source location the operation was defined or derived from.
This provides public APIs that all operations should have.
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...
@ Type
An inlay hint that for a type annotation.
DynamicAPInt round(const Fraction &f)
Value clampFloatHelper(Location loc, Value arg, Value min, Value max, OpBuilder &rewriter)
std::optional< SmallVector< Value > > checkHasDynamicBatchDims(PatternRewriter &rewriter, Op op, ArrayRef< Value > params)
SmallVector< utils::IteratorType > getNParallelLoopsAttrs(unsigned nParallelLoops)
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< T > applyTOSAPermutation(ArrayRef< T > input, ArrayRef< int32_t > perms)
TosaOp CreateOpAndInferShape(ImplicitLocOpBuilder &builder, Type resultTy, Args &&...args)
bool validIntegerRange(IntegerType ty, int64_t value)
Value clampIntHelper(Location loc, Value arg, Value min, Value max, OpBuilder &rewriter, bool isUnsigned)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
static ValueKnowledge join(const ValueKnowledge &lhs, const ValueKnowledge &rhs)
static ValueKnowledge getPessimisticValueState()
static ValueKnowledge getKnowledgeFromType(Type type)