MLIR  15.0.0git
Classes | Macros | Functions
TosaOps.cpp File Reference
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/Dialect/Quant/QuantOps.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Tosa/Utils/QuantUtils.h"
#include "mlir/Dialect/Tosa/Utils/ShapeUtils.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/FoldUtils.h"
#include "mlir/Transforms/InliningUtils.h"
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/TypeSwitch.h"
#include "mlir/Dialect/Tosa/IR/TosaOpsDialect.cpp.inc"
#include "mlir/Dialect/Tosa/IR/TosaInterfaces.cpp.inc"
#include "mlir/Dialect/Tosa/IR/TosaOps.cpp.inc"
#include "mlir/Dialect/Tosa/IR/TosaAttributes.cpp.inc"
+ Include dependency graph for TosaOps.cpp:

Go to the source code of this file.

Classes

struct  ConcatOptimization
 
struct  ReshapeReshapeOptimization
 
struct  ReshapeConstOptimization
 
struct  NoOpOptimization
 
struct  AddZeroOptimization
 
struct  MulOneOptimization
 
struct  MaterializePadValue
 
struct  MaxPool2dIsNoOp
 
struct  ClampIsNoOp
 
struct  ClampClampOptimization
 

Macros

#define GET_OP_LIST
 
#define GET_ATTRDEF_LIST
 
#define GET_OP_LIST
 
#define REDUCE_FOLDER(OP)
 
#define REDUCE_SHAPE_INFER(OP)
 
#define NARY_SHAPE_INFER(OP)
 
#define GET_ATTRDEF_CLASSES
 
#define GET_OP_CLASSES
 

Functions

template<typename... Args>
void addOpsCanonicalizations (MLIRContext *ctx, RewritePatternSet &patterns)
 
template<typename T >
static LogicalResult verifyConvOp (T op)
 
static void buildConvOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input, Value weight, Value bias, ArrayAttr pad, ArrayAttr stride, ArrayAttr dilation)
 This builder is called on all convolution operators except TransposeConv, which has specialized output shape semantics. More...
 
static void buildTransConvOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input, Value weight, Value bias, ArrayAttr outpad, ArrayAttr stride, ArrayAttr outputShape)
 Handles tosa.transpose_conv2d which has outpad and output shape attributes. More...
 
static void buildFCOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input, Value weight, Value bias)
 The tosa.fully_connected op has its own builder as it does not have strides/dilation/padding. More...
 
static void buildMatMulOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value a, Value b)
 The tosa.matmul op is also intended to be generated where a fully_connected op must be constructed where the weight is not a constant. More...
 
static void buildAvgPool2dOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input, ArrayAttr kernel, ArrayAttr stride, ArrayAttr pad)
 Both the tosa.avg_pool2d and unary ops use the same UnaruOpQuantizationAttr but avg_pool operator has its own builder as it has additional parameters not part of the unary ops. More...
 
static void buildUnaryOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input)
 This builder is called on single-parameter unary operators that have scale relationship between their input and output, expressed by the UnaryOpQuantizationAttr. More...
 
static void buildPadOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input, Value paddings)
 This builder is called on TOSA pad operator that needs to create its own OptionalAttr quantization_attr parameter to scale the padding values correctly. More...
 
static void buildExplicitValuePadOpWithQuantInfo (OpBuilder &builder, OperationState &result, Type outputType, Value input, Value paddings, Value padConst)
 This builder is called on TOSA pad operator when an explicit pad_const value is passed in. More...
 
static void getI64Values (ArrayAttr arrayAttr, SmallVector< int64_t > &values)
 
static void getF64Values (ArrayAttr arrayAttr, SmallVector< double > &values)
 
static LogicalResult ReduceInferReturnTypes (ShapeAdaptor operandShape, IntegerAttr axis, SmallVectorImpl< ShapedTypeComponents > &inferredReturnShapes)
 
static LogicalResult resolveBroadcastShape (const ValueShapeRange &operands, SmallVector< int64_t > &outShape)
 
static LogicalResult NAryInferReturnTypes (const ValueShapeRange &operands, SmallVectorImpl< ShapedTypeComponents > &inferredReturnShapes)
 
static LogicalResult poolingInferReturnTypes (const ValueShapeRange &operands, DictionaryAttr attributes, SmallVectorImpl< ShapedTypeComponents > &inferredReturnShapes)
 

Macro Definition Documentation

◆ GET_ATTRDEF_CLASSES

#define GET_ATTRDEF_CLASSES

Definition at line 1935 of file TosaOps.cpp.

◆ GET_ATTRDEF_LIST

#define GET_ATTRDEF_LIST

◆ GET_OP_CLASSES

#define GET_OP_CLASSES

Definition at line 1942 of file TosaOps.cpp.

◆ GET_OP_LIST [1/2]

#define GET_OP_LIST

◆ GET_OP_LIST [2/2]

#define GET_OP_LIST

◆ NARY_SHAPE_INFER

#define NARY_SHAPE_INFER (   OP)
Value:
LogicalResult OP::inferReturnTypeComponents( \
MLIRContext *context, ::llvm::Optional<Location> location, \
ValueShapeRange operands, DictionaryAttr attributes, \
RegionRange regions, \
SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) { \
return NAryInferReturnTypes(operands, inferredReturnShapes); \
}
Range of values and shapes (corresponding effectively to Shapes dialect&#39;s ValueShape type concept)...
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
static LogicalResult NAryInferReturnTypes(const ValueShapeRange &operands, SmallVectorImpl< ShapedTypeComponents > &inferredReturnShapes)
Definition: TosaOps.cpp:1436
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
This class provides an abstraction over the different types of ranges over Regions.
Definition: Region.h:328

Definition at line 1448 of file TosaOps.cpp.

◆ REDUCE_FOLDER

#define REDUCE_FOLDER (   OP)
Value:
OpFoldResult OP::fold(ArrayRef<Attribute> operands) { \
ShapedType inputTy = input().getType().cast<ShapedType>(); \
if (!inputTy.hasRank()) \
return {}; \
if (inputTy.getDimSize(axis()) == 1) \
return input(); \
return {}; \
}
This class represents a single result from folding an operation.
Definition: OpDefinition.h:229

Definition at line 530 of file TosaOps.cpp.

◆ REDUCE_SHAPE_INFER

#define REDUCE_SHAPE_INFER (   OP)
Value:
LogicalResult OP::inferReturnTypeComponents( \
MLIRContext *context, ::llvm::Optional<Location> location, \
ValueShapeRange operands, DictionaryAttr attributes, \
RegionRange regions, \
SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) { \
return ReduceInferReturnTypes(operands.getShape(0), \
attributes.get("axis").cast<IntegerAttr>(), \
inferredReturnShapes); \
}
Range of values and shapes (corresponding effectively to Shapes dialect&#39;s ValueShape type concept)...
static LogicalResult ReduceInferReturnTypes(ShapeAdaptor operandShape, IntegerAttr axis, SmallVectorImpl< ShapedTypeComponents > &inferredReturnShapes)
Definition: TosaOps.cpp:1365
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
ShapeAdaptor getShape(int index) const
Returns the shape of index&#39;th operand.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
This class provides an abstraction over the different types of ranges over Regions.
Definition: Region.h:328

Definition at line 1381 of file TosaOps.cpp.

Function Documentation

◆ addOpsCanonicalizations()

template<typename... Args>
void addOpsCanonicalizations ( MLIRContext ctx,
RewritePatternSet patterns 
)

Definition at line 104 of file TosaOps.cpp.

Referenced by mlir::tosa::populateTosaOpsCanonicalizationPatterns().

◆ buildAvgPool2dOpWithQuantInfo()

static void buildAvgPool2dOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input,
ArrayAttr  kernel,
ArrayAttr  stride,
ArrayAttr  pad 
)
static

Both the tosa.avg_pool2d and unary ops use the same UnaruOpQuantizationAttr but avg_pool operator has its own builder as it has additional parameters not part of the unary ops.

Definition at line 798 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::tosa::buildUnaryOpQuantizationAttr(), and mlir::OperationState::types.

◆ buildConvOpWithQuantInfo()

static void buildConvOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input,
Value  weight,
Value  bias,
ArrayAttr  pad,
ArrayAttr  stride,
ArrayAttr  dilation 
)
static

This builder is called on all convolution operators except TransposeConv, which has specialized output shape semantics.

The builder also defines the bitwidth of the output given the bit width of the input & weight content.

Definition at line 699 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::OperationState::addTypes(), mlir::tosa::buildConvOpQuantizationAttr(), and mlir::tosa::buildConvOpResultTypeInfo().

◆ buildExplicitValuePadOpWithQuantInfo()

static void buildExplicitValuePadOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input,
Value  paddings,
Value  padConst 
)
static

This builder is called on TOSA pad operator when an explicit pad_const value is passed in.

It also optionally constructs quantization_attr.

Definition at line 841 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::tosa::buildPadOpQuantizationAttr(), and mlir::OperationState::types.

◆ buildFCOpWithQuantInfo()

static void buildFCOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input,
Value  weight,
Value  bias 
)
static

The tosa.fully_connected op has its own builder as it does not have strides/dilation/padding.

Definition at line 743 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::OperationState::addTypes(), mlir::tosa::buildConvOpQuantizationAttr(), and mlir::tosa::buildConvOpResultTypeInfo().

◆ buildMatMulOpWithQuantInfo()

static void buildMatMulOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  a,
Value  b 
)
static

The tosa.matmul op is also intended to be generated where a fully_connected op must be constructed where the weight is not a constant.

In this case, the fully_connected op must be expressed using matmul. TODO: Add link to the leglization document explaining this.

Definition at line 762 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::OperationState::addTypes(), mlir::tosa::buildMatMulOpQuantizationAttr(), mlir::Type::dyn_cast(), mlir::Builder::getI32Type(), mlir::Builder::getIntegerType(), and mlir::Value::getType().

◆ buildPadOpWithQuantInfo()

static void buildPadOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input,
Value  paddings 
)
static

This builder is called on TOSA pad operator that needs to create its own OptionalAttr quantization_attr parameter to scale the padding values correctly.

No pad_const is interpreted as zero-padding.

Definition at line 829 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::tosa::buildPadOpQuantizationAttr(), and mlir::OperationState::types.

◆ buildTransConvOpWithQuantInfo()

static void buildTransConvOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input,
Value  weight,
Value  bias,
ArrayAttr  outpad,
ArrayAttr  stride,
ArrayAttr  outputShape 
)
static

Handles tosa.transpose_conv2d which has outpad and output shape attributes.

Definition at line 720 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::OperationState::addTypes(), mlir::tosa::buildConvOpQuantizationAttr(), and mlir::tosa::buildConvOpResultTypeInfo().

◆ buildUnaryOpWithQuantInfo()

static void buildUnaryOpWithQuantInfo ( OpBuilder builder,
OperationState result,
Type  outputType,
Value  input 
)
static

This builder is called on single-parameter unary operators that have scale relationship between their input and output, expressed by the UnaryOpQuantizationAttr.

Definition at line 816 of file TosaOps.cpp.

References mlir::OperationState::addAttribute(), mlir::OperationState::addOperands(), mlir::tosa::buildUnaryOpQuantizationAttr(), and mlir::OperationState::types.

◆ getF64Values()

static void getF64Values ( ArrayAttr  arrayAttr,
SmallVector< double > &  values 
)
static

◆ getI64Values()

static void getI64Values ( ArrayAttr  arrayAttr,
SmallVector< int64_t > &  values 
)
static

Definition at line 857 of file TosaOps.cpp.

Referenced by getF64Values(), and poolingInferReturnTypes().

◆ NAryInferReturnTypes()

static LogicalResult NAryInferReturnTypes ( const ValueShapeRange operands,
SmallVectorImpl< ShapedTypeComponents > &  inferredReturnShapes 
)
static

Definition at line 1436 of file TosaOps.cpp.

References mlir::failed(), resolveBroadcastShape(), and mlir::success().

◆ poolingInferReturnTypes()

static LogicalResult poolingInferReturnTypes ( const ValueShapeRange operands,
DictionaryAttr  attributes,
SmallVectorImpl< ShapedTypeComponents > &  inferredReturnShapes 
)
static

◆ ReduceInferReturnTypes()

static LogicalResult ReduceInferReturnTypes ( ShapeAdaptor  operandShape,
IntegerAttr  axis,
SmallVectorImpl< ShapedTypeComponents > &  inferredReturnShapes 
)
static

◆ resolveBroadcastShape()

static LogicalResult resolveBroadcastShape ( const ValueShapeRange operands,
SmallVector< int64_t > &  outShape 
)
static

Definition at line 1400 of file TosaOps.cpp.

References mlir::failure(), mlir::ValueShapeRange::getShape(), and mlir::success().

Referenced by NAryInferReturnTypes().

◆ verifyConvOp()

template<typename T >
static LogicalResult verifyConvOp ( op)
static

Definition at line 631 of file TosaOps.cpp.

References mlir::failure(), getElementType(), mlir::success(), and mlir::verify().

Referenced by getF64Values(), and poolingInferReturnTypes().