MLIR  20.0.0git
ConversionUtils.h
Go to the documentation of this file.
1 //===- ConversionUtils.h - Helper functions for tosa conversion -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utility functions for TOSA lowering
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
14 #define DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
15 
19 #include "mlir/IR/PatternMatch.h"
20 #include <optional>
21 
22 namespace mlir {
23 namespace tosa {
24 
25 // Creates a SmallVector of Stringrefs for N parallel loops
26 SmallVector<utils::IteratorType>
27 getNParallelLoopsAttrs(unsigned nParallelLoops);
28 
29 // Takes a vector of values and condenses them to a vector with no gaps.
30 SmallVector<Value> condenseValues(const SmallVector<Value> &values);
31 
32 // Takes the parameters for a clamp and turns it into a series of ops for float
33 // inputs.
34 Value clampFloatHelper(Location loc, Value arg, Value min, Value max,
35  OpBuilder &rewriter);
36 
37 // Takes the parameters for a clamp and turns it into a series of ops for
38 // integer inputs.
39 Value clampIntHelper(Location loc, Value arg, Value min, Value max,
40  OpBuilder &rewriter, bool isUnsigned);
41 
42 // Determines whether the integer value falls witin the range of integer type.
43 bool validIntegerRange(IntegerType ty, int64_t value);
44 
45 // Checks for a dynamic batch dim in any of the passed parameters of an op.
46 // The batch dimention must be #0 and the rest of the dimensions must be static.
47 template <typename Op>
48 std::optional<SmallVector<Value>>
50  ArrayRef<Value> params) {
51  SmallVector<ShapedType> dynTypes;
52  SmallVector<Value> dynamicDims;
53  for (const Value &param : params) {
54  auto paramTy = cast<ShapedType>(param.getType());
55  if (!paramTy.hasStaticShape())
56  dynTypes.push_back(paramTy);
57  }
58 
59  if (dynTypes.empty())
60  return dynamicDims;
61 
62  for (const ShapedType &dynTy : dynTypes) {
63  if (llvm::any_of(dynTy.getShape().drop_front(), ShapedType::isDynamic)) {
64  (void)rewriter.notifyMatchFailure(
65  op, "input can only be dynamic for batch size");
66  return std::nullopt;
67  }
68  }
69 
70  dynamicDims.push_back(
71  rewriter.create<tensor::DimOp>(op->getLoc(), params[0], 0));
72  return dynamicDims;
73 }
74 
75 /// Common code to create the reshape op where necessary to make the rank of two
76 /// values equal. input1 and input2 will be updated when the rank has
77 /// changed. The caller is expected to use these to rewrite the original
78 /// operator with the RESHAPE now in the graph.
79 LogicalResult EqualizeRanks(PatternRewriter &rewriter, Location loc,
80  Value &input1, Value &input2);
81 
82 } // namespace tosa
83 } // namespace mlir
84 
85 #endif // DIALECT_TOSA_UTILS_COVERSION_UTILS_H_
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:468
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...
Definition: PatternMatch.h:785
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,...
Definition: PatternMatch.h:718
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
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.
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.