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 
93 } // namespace tosa
94 } // namespace OpTrait
95 
96 } // namespace mlir
97 
98 #define GET_ATTRDEF_CLASSES
99 #include "mlir/Dialect/Tosa/IR/TosaAttributes.h.inc"
100 
101 #define GET_OP_CLASSES
102 #include "mlir/Dialect/Tosa/IR/TosaOps.h.inc"
103 
104 #endif // MLIR_DIALECT_TOSA_IR_TOSAOPS_H
Helper class for implementing traits.
Definition: OpDefinition.h:373
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
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Value getOperand(unsigned idx)
Definition: Operation.h:345
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:402
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
LogicalResult verifySameOperandsAndResultElementType(Operation *op)
Definition: Operation.cpp:1076
ParseResult parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &attr)
Definition: TosaOps.cpp:166
void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type, Attribute attr)
Definition: TosaOps.cpp:188
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.