MLIR  19.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:
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 = resElemType.dyn_cast<IntegerType>()) {
64  IntegerType lhsIntType =
65  getElementTypeOrSelf(op->getOperand(0)).cast<IntegerType>();
66  IntegerType rhsIntType =
67  getElementTypeOrSelf(op->getOperand(1)).cast<IntegerType>();
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  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
Helper class for implementing traits.
Definition: OpDefinition.h:371
static LogicalResult verifyTrait(Operation *op)
Definition: TosaOps.h:55
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
U cast() const
Definition: Types.h:340
LogicalResult verifySameOperandsAndResultElementType(Operation *op)
Definition: Operation.cpp:1056
ParseResult parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &attr)
Definition: TosaOps.cpp:164
void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type, Attribute attr)
Definition: TosaOps.cpp:186
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26