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 | |
16 | #include "mlir/Bytecode/BytecodeOpInterface.h" |
17 | #include "mlir/Dialect/Traits.h" |
18 | #include "mlir/IR/OpDefinition.h" |
19 | #include "mlir/IR/OpImplementation.h" |
20 | #include "mlir/IR/TypeUtilities.h" |
21 | #include "mlir/Interfaces/InferTypeOpInterface.h" |
22 | #include "mlir/Interfaces/LoopLikeInterface.h" |
23 | #include "mlir/Interfaces/SideEffectInterfaces.h" |
24 | #include "mlir/Interfaces/VectorInterfaces.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> |
52 | class MulOperandsAndResultElementType |
53 | : public TraitBase<ConcreteType, MulOperandsAndResultElementType> { |
54 | public: |
55 | static LogicalResult verifyTrait(Operation *op) { |
56 | auto resElemType = getElementTypeOrSelf(val: op->getResult(idx: 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)) |
61 | return impl::verifySameOperandsAndResultElementType(op); |
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 | message: "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(message: "invalid data type size for operands or result" ); |
77 | |
78 | return success(); |
79 | } |
80 | |
81 | return failure(); |
82 | } |
83 | }; |
84 | |
85 | } // namespace tosa |
86 | } // namespace OpTrait |
87 | |
88 | } // namespace mlir |
89 | |
90 | #define GET_ATTRDEF_CLASSES |
91 | #include "mlir/Dialect/Tosa/IR/TosaAttributes.h.inc" |
92 | |
93 | #define GET_OP_CLASSES |
94 | #include "mlir/Dialect/Tosa/IR/TosaOps.h.inc" |
95 | |
96 | #endif // MLIR_DIALECT_TOSA_IR_TOSAOPS_H |
97 | |