MLIR  20.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 
17 #include "mlir/Dialect/Traits.h"
18 #include "mlir/IR/OpDefinition.h"
20 #include "mlir/IR/TypeUtilities.h"
25 
26 //===----------------------------------------------------------------------===//
27 // TOSA dialect and structs includes.
28 //===----------------------------------------------------------------------===//
29 
30 #include "mlir/Dialect/Tosa/IR/TosaOpsDialect.h.inc"
31 
32 namespace mlir {
33 class PatternRewriter;
34 
35 namespace tosa {
36 
37 ParseResult parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr,
38  Attribute &attr);
39 void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type,
40  Attribute attr);
41 
42 #include "mlir/Dialect/Tosa/IR/TosaInterfaces.h.inc"
43 
44 } // namespace tosa
45 
46 namespace OpTrait {
47 namespace tosa {
48 
49 // This trait verifies if the element type amoung operands and result
50 // of multiplication match tosa specification.
51 template <typename ConcreteType>
53  : public TraitBase<ConcreteType, MulOperandsAndResultElementType> {
54 public:
55  static LogicalResult verifyTrait(Operation *op) {
56  auto resElemType = getElementTypeOrSelf(op->getResult(0));
57 
58  // In cases of floating point type, op requires the same element
59  // type for all operands and result.
60  if (llvm::isa<FloatType>(resElemType))
62 
63  if (auto resIntType = dyn_cast<IntegerType>(resElemType)) {
64  IntegerType lhsIntType =
65  cast<IntegerType>(getElementTypeOrSelf(op->getOperand(0)));
66  IntegerType rhsIntType =
67  cast<IntegerType>(getElementTypeOrSelf(op->getOperand(1)));
68  if (lhsIntType != rhsIntType)
69  return op->emitOpError(
70  "requires the same element type for all operands");
71 
72  // Though the spec requires the element type of result to be i32, a more
73  // relaxed way is provided at dialect level for easier cooperating with
74  // other dialects.
75  if (lhsIntType.getWidth() > resIntType.getWidth())
76  return op->emitOpError("invalid data type size for operands or result");
77 
78  return success();
79  }
80 
81  // In cases of all other types, op requires the same element
82  // type for all operands and result.
84  }
85 };
86 
87 /// This class indicates that an op is tosa-elementwise (permits broadcasting,
88 /// unlike Elementwise trait).
89 template <typename ConcreteType>
91  : public TraitBase<ConcreteType, TosaElementwiseOperator> {};
92 
94 /// This class verifies that tosa shape operands are compile time resolvable
95 template <typename ConcreteType>
97  : public TraitBase<ConcreteType, TosaResolvableShapeOperands> {
98 public:
99  static LogicalResult verifyTrait(Operation *op) {
101  }
102 };
103 
104 LogicalResult verifyTosaShapeOperator(Operation *op);
105 /// This class indicates that op operates on tosa shape types
106 template <typename ConcreteType>
107 class TosaShapeOperator : public TraitBase<ConcreteType, TosaShapeOperator> {
108 public:
109  static LogicalResult verifyTrait(Operation *op) {
110  return verifyTosaShapeOperator(op);
111  }
112 };
113 
115 /// This class indicates that op operates on tosa shape types
116 template <typename ConcreteType>
118  : public TraitBase<ConcreteType, TosaShapeOperatorWithSameRanks> {
119 public:
120  static LogicalResult verifyTrait(Operation *op) {
122  }
123 };
124 
125 } // namespace tosa
126 } // namespace OpTrait
127 
128 namespace tosa {
129 
131 
132 } // namespace tosa
133 
134 } // namespace mlir
135 
136 #define GET_ATTRDEF_CLASSES
137 #include "mlir/Dialect/Tosa/IR/TosaAttributes.h.inc"
138 
139 #define GET_TYPEDEF_CLASSES
140 #include "mlir/Dialect/Tosa/IR/TosaOpsTypesBase.h.inc"
141 
142 #define GET_OP_CLASSES
143 #include "mlir/Dialect/Tosa/IR/TosaOps.h.inc"
144 
145 #endif // MLIR_DIALECT_TOSA_IR_TOSAOPS_H
Helper class for implementing traits.
Definition: OpDefinition.h:374
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:55
This class indicates that an op is tosa-elementwise (permits broadcasting, unlike Elementwise trait).
Definition: TosaOps.h:91
This class verifies that tosa shape operands are compile time resolvable.
Definition: TosaOps.h:97
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:99
This class indicates that op operates on tosa shape types.
Definition: TosaOps.h:118
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:120
This class indicates that op operates on tosa shape types.
Definition: TosaOps.h:107
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:109
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
LogicalResult verifySameOperandsAndResultElementType(Operation *op)
Definition: Operation.cpp:1076
LogicalResult verifyTosaShapeOperator(Operation *op)
Definition: TosaOps.cpp:2209
LogicalResult verifyTosaShapeOperatorWithSameRanks(Operation *op)
Definition: TosaOps.cpp:2224
LogicalResult verifyTosaResolvableShapeOperands(Operation *op)
Definition: TosaOps.cpp:2197
ParseResult parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &attr)
Definition: TosaOps.cpp:174
bool isa_tosa_shape_type(mlir::Type t)
Definition: TosaOps.cpp:2185
void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type, Attribute attr)
Definition: TosaOps.cpp:196
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.