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 parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr,
48  Attribute &attr);
49 void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type,
50  Attribute attr);
51 
52 #include "mlir/Dialect/Tosa/IR/TosaInterfaces.h.inc"
53 
54 } // namespace tosa
55 
56 namespace OpTrait {
57 namespace tosa {
58 
59 // This trait verifies if the element type amoung operands and result
60 // of multiplication match tosa specification.
61 template <typename ConcreteType>
63  : public TraitBase<ConcreteType, MulOperandsAndResultElementType> {
64 public:
65  static LogicalResult verifyTrait(Operation *op) {
66  // Check we have a single result.
67  if (failed(impl::verifyOneResult(op)))
68  return failure();
69  Type resElemType = getElementTypeOrSelf(op->getResult(0));
70 
71  // Check we have lhs and rhs.
72  if (failed(impl::verifyAtLeastNOperands(op, 2)))
73  return failure();
74 
75  Type lhsElemType = getElementTypeOrSelf(op->getOperand(0));
76  Type rhsElemType = getElementTypeOrSelf(op->getOperand(1));
77 
78  // Check that for i32 a shift has been explicitly provided.
79  if (lhsElemType.isInteger(32) && failed(impl::verifyNOperands(op, 3)))
80  return failure();
81 
82  // Verify operands type match (ignoring the shift parameter which will
83  // always be i8).
84  if (lhsElemType != rhsElemType)
85  return op->emitOpError("requires the same element type for all operands");
86 
87  // Though the spec requires the element type of result to be i32, a more
88  // relaxed way is provided at dialect level for easier cooperating with
89  // other dialects.
90  if (auto resIntType = dyn_cast<IntegerType>(resElemType)) {
91  auto lhsIntType = cast<IntegerType>(lhsElemType);
92  if (lhsIntType.getWidth() > resIntType.getWidth())
93  return op->emitOpError("invalid data type size for operands or result");
94  } else {
95  // In cases of floating point type or quant types, op requires the same
96  // element type for all operands and result (excluding shift).
97  if (resElemType != lhsElemType)
98  return op->emitOpError(
99  "requires the same element type for all operands and results");
100  }
101 
102  return llvm::success();
103  }
104 };
105 
106 /// This class indicates that an op is tosa-elementwise (permits broadcasting,
107 /// unlike Elementwise trait).
108 template <typename ConcreteType>
110  : public TraitBase<ConcreteType, TosaElementwiseOperator> {};
111 
113 /// This class verifies that tosa shape operands are compile time resolvable
114 template <typename ConcreteType>
116  : public TraitBase<ConcreteType, TosaResolvableShapeOperands> {
117 public:
118  static LogicalResult verifyTrait(Operation *op) {
120  }
121 };
122 
123 LogicalResult verifyTosaShapeOperator(Operation *op);
124 /// This class indicates that op operates on tosa shape types
125 template <typename ConcreteType>
126 class TosaShapeOperator : public TraitBase<ConcreteType, TosaShapeOperator> {
127 public:
128  static LogicalResult verifyTrait(Operation *op) {
129  return verifyTosaShapeOperator(op);
130  }
131 };
132 
134 /// This class indicates that op operates on tosa shape types
135 template <typename ConcreteType>
137  : public TraitBase<ConcreteType, TosaShapeOperatorWithSameRanks> {
138 public:
139  static LogicalResult verifyTrait(Operation *op) {
141  }
142 };
143 
144 } // namespace tosa
145 } // namespace OpTrait
146 
147 namespace tosa {
148 
150 
151 } // namespace tosa
152 
153 } // namespace mlir
154 
155 #define GET_ATTRDEF_CLASSES
156 #include "mlir/Dialect/Tosa/IR/TosaAttributes.h.inc"
157 
158 #define GET_TYPEDEF_CLASSES
159 #include "mlir/Dialect/Tosa/IR/TosaOpsTypesBase.h.inc"
160 
161 #define GET_OP_CLASSES
162 #include "mlir/Dialect/Tosa/IR/TosaOps.h.inc"
163 
164 namespace mlir {
165 namespace tosa {
166 
167 // Create a rank-1 const tensor for zero point of the source tensor.
168 std::optional<Value> createZeroPointTensor(OpBuilder &builder, Location loc,
169  Type srcElemType, int64_t zp = 0);
170 
171 // Create a pad-const const tensor with value of `val` of required data-type
172 Value createPadConstTensor(OpBuilder &builder, Location loc, Value src,
173  int32_t val = 0);
174 
175 } // namespace tosa
176 } // namespace mlir
177 
178 #endif // MLIR_DIALECT_TOSA_IR_TOSAOPS_H
Helper class for implementing traits.
Definition: OpDefinition.h:377
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:65
This class indicates that an op is tosa-elementwise (permits broadcasting, unlike Elementwise trait).
Definition: TosaOps.h:110
This class verifies that tosa shape operands are compile time resolvable.
Definition: TosaOps.h:116
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:118
This class indicates that op operates on tosa shape types.
Definition: TosaOps.h:137
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:139
This class indicates that op operates on tosa shape types.
Definition: TosaOps.h:126
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:128
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:671
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:906
LogicalResult verifyAtLeastNOperands(Operation *op, unsigned numOperands)
Definition: Operation.cpp:915
LogicalResult verifyOneResult(Operation *op)
Definition: Operation.cpp:1016
LogicalResult verifyTosaShapeOperator(Operation *op)
Definition: TosaOps.cpp:3494
LogicalResult verifyTosaShapeOperatorWithSameRanks(Operation *op)
Definition: TosaOps.cpp:3509
LogicalResult verifyTosaResolvableShapeOperands(Operation *op)
Definition: TosaOps.cpp:3482
ParseResult parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &attr)
Definition: TosaOps.cpp:180
std::optional< Value > createZeroPointTensor(OpBuilder &builder, Location loc, Type srcElemType, int64_t zp=0)
Definition: TosaOps.cpp:3446
bool isa_tosa_shape_type(mlir::Type t)
Definition: TosaOps.cpp:3470
void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type, Attribute attr)
Definition: TosaOps.cpp:202
Value createPadConstTensor(OpBuilder &builder, Location loc, Value src, int32_t val=0)
Definition: TosaOps.cpp:260
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.