MLIR  21.0.0git
TosaOps.h
Go to the documentation of this file.
1 //===-- TosaOps.h - TOSA dialect operation definitions ----------*- 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 // This file declares the TOSA Dialect in MLIR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_TOSA_IR_TOSAOPS_H
14 #define MLIR_DIALECT_TOSA_IR_TOSAOPS_H
15 
18 #include "mlir/Dialect/Traits.h"
19 #include "mlir/IR/Matchers.h"
20 #include "mlir/IR/OpDefinition.h"
22 #include "mlir/IR/TypeUtilities.h"
27 
28 //===----------------------------------------------------------------------===//
29 // TOSA dialect and structs includes.
30 //===----------------------------------------------------------------------===//
31 
32 #include "mlir/Dialect/Tosa/IR/TosaEnums.h.inc"
33 #include "mlir/Dialect/Tosa/IR/TosaOpsDialect.h.inc"
35 
36 //===----------------------------------------------------------------------===//
37 // TOSA operation validation includes.
38 //===----------------------------------------------------------------------===//
39 
40 #include "mlir/Dialect/Tosa/IR/TosaAvailability.h.inc"
41 
42 namespace mlir {
43 class PatternRewriter;
44 
45 namespace tosa {
46 
47 ParseResult parseVariableOpTypeOrInitialValue(OpAsmParser &parser,
48  DenseElementsAttr &varShapeAttr,
49  TypeAttr &typeAttr,
50  Attribute &initialValueAttr);
51 void printVariableOpTypeOrInitialValue(OpAsmPrinter &p, Operation *op,
52  DenseElementsAttr varShapeAttr,
53  TypeAttr typeAttr,
54  Attribute initialValueAttr);
55 
56 #include "mlir/Dialect/Tosa/IR/TosaInterfaces.h.inc"
57 
58 } // namespace tosa
59 
60 namespace OpTrait {
61 namespace tosa {
62 
63 // This trait verifies if the element type amoung operands and result
64 // of multiplication match tosa specification.
65 template <typename ConcreteType>
67  : public TraitBase<ConcreteType, MulOperandsAndResultElementType> {
68 public:
69  static LogicalResult verifyTrait(Operation *op) {
70  // Check we have a single result.
71  if (failed(impl::verifyOneResult(op)))
72  return failure();
73  Type resElemType = getElementTypeOrSelf(op->getResult(0));
74 
75  // Check we have lhs and rhs.
76  if (failed(impl::verifyAtLeastNOperands(op, 2)))
77  return failure();
78 
79  Type lhsElemType = getElementTypeOrSelf(op->getOperand(0));
80  Type rhsElemType = getElementTypeOrSelf(op->getOperand(1));
81 
82  // Check that for i32 a shift has been explicitly provided.
83  if (lhsElemType.isInteger(32) && failed(impl::verifyNOperands(op, 3)))
84  return failure();
85 
86  // Verify operands type match (ignoring the shift parameter which will
87  // always be i8).
88  if (lhsElemType != rhsElemType)
89  return op->emitOpError("requires the same element type for all operands");
90 
91  // Though the spec requires the element type of result to be i32, a more
92  // relaxed way is provided at dialect level for easier cooperating with
93  // other dialects.
94  if (auto resIntType = dyn_cast<IntegerType>(resElemType)) {
95  auto lhsIntType = cast<IntegerType>(lhsElemType);
96  if (lhsIntType.getWidth() > resIntType.getWidth())
97  return op->emitOpError("invalid data type size for operands or result");
98  } else {
99  // In cases of floating point type or quant types, op requires the same
100  // element type for all operands and result (excluding shift).
101  if (resElemType != lhsElemType)
102  return op->emitOpError(
103  "requires the same element type for all operands and results");
104  }
105 
106  return llvm::success();
107  }
108 };
109 
110 /// This class indicates that an op is tosa-elementwise (permits broadcasting,
111 /// unlike Elementwise trait).
112 template <typename ConcreteType>
114  : public TraitBase<ConcreteType, TosaElementwiseOperator> {};
115 
117 /// This class verifies that tosa shape operands are compile time resolvable
118 template <typename ConcreteType>
120  : public TraitBase<ConcreteType, TosaResolvableShapeOperands> {
121 public:
122  static LogicalResult verifyTrait(Operation *op) {
124  }
125 };
126 
127 LogicalResult verifyTosaShapeOperator(Operation *op);
128 /// This class indicates that op operates on tosa shape types
129 template <typename ConcreteType>
130 class TosaShapeOperator : public TraitBase<ConcreteType, TosaShapeOperator> {
131 public:
132  static LogicalResult verifyTrait(Operation *op) {
133  return verifyTosaShapeOperator(op);
134  }
135 };
136 
138 /// This class indicates that op operates on tosa shape types
139 template <typename ConcreteType>
141  : public TraitBase<ConcreteType, TosaShapeOperatorWithSameRanks> {
142 public:
143  static LogicalResult verifyTrait(Operation *op) {
145  }
146 };
147 
148 } // namespace tosa
149 } // namespace OpTrait
150 
151 namespace tosa {
152 
154 
155 } // namespace tosa
156 
157 } // namespace mlir
158 
159 #define GET_ATTRDEF_CLASSES
160 #include "mlir/Dialect/Tosa/IR/TosaAttributes.h.inc"
161 
162 #define GET_TYPEDEF_CLASSES
163 #include "mlir/Dialect/Tosa/IR/TosaOpsTypesBase.h.inc"
164 
165 #define GET_OP_CLASSES
166 #include "mlir/Dialect/Tosa/IR/TosaOps.h.inc"
167 
168 namespace mlir {
169 namespace tosa {
170 
171 // Create a rank-1 const tensor for zero point of the source tensor.
172 std::optional<Value> createZeroPointTensor(OpBuilder &builder, Location loc,
173  Type srcElemType, int64_t zp = 0);
174 
175 // Create a pad-const const tensor with value of `val` of required data-type
176 Value createPadConstTensor(OpBuilder &builder, Location loc, Value src,
177  int32_t val = 0);
178 
179 // returns type of variable op
180 RankedTensorType getVariableType(VariableOp variableOp);
181 
182 } // namespace tosa
183 } // namespace mlir
184 
185 #endif // MLIR_DIALECT_TOSA_IR_TOSAOPS_H
Helper class for implementing traits.
Definition: OpDefinition.h:377
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:69
This class indicates that an op is tosa-elementwise (permits broadcasting, unlike Elementwise trait).
Definition: TosaOps.h:114
This class verifies that tosa shape operands are compile time resolvable.
Definition: TosaOps.h:120
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:122
This class indicates that op operates on tosa shape types.
Definition: TosaOps.h:141
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:143
This class indicates that op operates on tosa shape types.
Definition: TosaOps.h:130
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:132
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Value getOperand(unsigned idx)
Definition: Operation.h:350
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:407
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Definition: Operation.cpp:673
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition: Types.cpp:56
LogicalResult verifyNOperands(Operation *op, unsigned numOperands)
Definition: Operation.cpp:908
LogicalResult verifyAtLeastNOperands(Operation *op, unsigned numOperands)
Definition: Operation.cpp:917
LogicalResult verifyOneResult(Operation *op)
Definition: Operation.cpp:1018
LogicalResult verifyTosaShapeOperator(Operation *op)
Definition: TosaOps.cpp:3990
LogicalResult verifyTosaShapeOperatorWithSameRanks(Operation *op)
Definition: TosaOps.cpp:4005
LogicalResult verifyTosaResolvableShapeOperands(Operation *op)
Definition: TosaOps.cpp:3978
RankedTensorType getVariableType(VariableOp variableOp)
ParseResult parseVariableOpTypeOrInitialValue(OpAsmParser &parser, DenseElementsAttr &varShapeAttr, TypeAttr &typeAttr, Attribute &initialValueAttr)
Definition: TosaOps.cpp:227
void printVariableOpTypeOrInitialValue(OpAsmPrinter &p, Operation *op, DenseElementsAttr varShapeAttr, TypeAttr typeAttr, Attribute initialValueAttr)
Definition: TosaOps.cpp:252
std::optional< Value > createZeroPointTensor(OpBuilder &builder, Location loc, Type srcElemType, int64_t zp=0)
Definition: TosaOps.cpp:3942
bool isa_tosa_shape_type(mlir::Type t)
Definition: TosaOps.cpp:3966
Value createPadConstTensor(OpBuilder &builder, Location loc, Value src, int32_t val=0)
Definition: TosaOps.cpp:316
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.