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
18#include "mlir/Dialect/Traits.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"
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 {
48 DenseElementsAttr &varShapeAttr,
49 TypeAttr &typeAttr,
50 Attribute &initialValueAttr);
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
127LogicalResult verifyTosaShapeOperator(Operation *op);
128/// This class indicates that op operates on tosa shape types
129template <typename ConcreteType>
130class TosaShapeOperator : public TraitBase<ConcreteType, TosaShapeOperator> {
131public:
132 static LogicalResult verifyTrait(Operation *op) {
133 return verifyTosaShapeOperator(op);
134 }
135};
136
138/// This class indicates that op operates on tosa shape types
139template <typename ConcreteType>
141 : public TraitBase<ConcreteType, TosaShapeOperatorWithSameRanks> {
142public:
143 static LogicalResult verifyTrait(Operation *op) {
145 }
146};
147
148} // namespace tosa
149} // namespace OpTrait
150
151namespace tosa {
152
154
155} // namespace tosa
156
157} // namespace mlir
158
159#define GET_ATTRDEF_CLASSES
160#include "mlir/Dialect/Tosa/IR/TosaAttributes.h.inc"
161
162#define GET_TYPEDEF_CLASSES
163#include "mlir/Dialect/Tosa/IR/TosaOpsTypesBase.h.inc"
164
165#define GET_OP_CLASSES
166#include "mlir/Dialect/Tosa/IR/TosaOps.h.inc"
167
168namespace mlir {
169namespace tosa {
170
171// Create a rank-1 const tensor for zero point of the source tensor.
172std::optional<Value> createZeroPointTensor(OpBuilder &builder, Location loc,
173 Type srcElemType, int64_t zp = 0);
174
175// Create a pad-const const tensor with value of `val` of required data-type
176Value createPadConstTensor(OpBuilder &builder, Location loc, Value src,
177 int32_t val = 0);
178
179// returns type of variable op
180RankedTensorType getVariableType(VariableOp variableOp);
181
182// Returns the bitwidth of a TOSA tensor element type
183unsigned getBitWidth(Type type);
184
185} // namespace tosa
186} // namespace mlir
187
188#endif // MLIR_DIALECT_TOSA_IR_TOSAOPS_H
Attributes are known-constant values of operations.
Definition Attributes.h:25
An attribute that represents a reference to a dense vector or tensor object.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
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:141
static LogicalResult verifyTrait(Operation *op)
Definition TosaOps.h:143
This class indicates that op operates on tosa shape types.
Definition TosaOps.h:130
static LogicalResult verifyTrait(Operation *op)
Definition TosaOps.h:132
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 verifyTosaShapeOperator(Operation *op)
Definition TosaOps.cpp:4606
LogicalResult verifyTosaShapeOperatorWithSameRanks(Operation *op)
Definition TosaOps.cpp:4621
LogicalResult verifyTosaResolvableShapeOperands(Operation *op)
Definition TosaOps.cpp:4594
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:4558
bool isa_tosa_shape_type(mlir::Type t)
Definition TosaOps.cpp:4582
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.