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