1//===-- TosaUtilOps.td - TOSA dialect utility operations ---*- tablegen -*-===// 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 defines codegen utility operators for the TOSA dialect. 10// These operators are not part of the formal TOSA specification and 11// are intended to aid code generation from TOSA. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef TOSA_UTIL_OPS 16#define TOSA_UTIL_OPS 17 18include "mlir/IR/OpBase.td" 19 20include "mlir/Interfaces/SideEffectInterfaces.td" 21include "mlir/Interfaces/LoopLikeInterface.td" 22include "mlir/Interfaces/VectorInterfaces.td" 23include "mlir/Dialect/Tosa/IR/TosaInterfaces.td" 24 25include "mlir/Dialect/Tosa/IR/TosaTypesBase.td" 26include "mlir/Dialect/Tosa/IR/TosaOpBase.td" 27 28def Tosa_ApplyScaleOp : 29 Tosa_Op<"apply_scale", 30 [Pure, DeclareOpInterfaceMethods<VectorUnrollOpInterface>] # 31 ElementwiseMappable.traits> { 32 let summary = "Rescale scalar operator for Tosa tensor operators"; 33 34 let description = [{ 35 Applies rescaling for fixed point values. This behavior is replicated in 36 multiple quantized operations (mul, convolution, rescale, matmul, pooling). 37 38 The commonplace implementation is to use i64 operations to avoid integer 39 overflow with target specific implementations can use native operations to 40 avoid wider than necessary types. 41 }]; 42 43 let arguments = (ins 44 Tosa_IntLike:$value, 45 Tosa_IntLike:$multiplier, 46 Tosa_Int8Like:$shift, 47 BoolAttr:$double_round 48 ); 49 50 let results = (outs 51 Tosa_IntLike:$output 52 ); 53 54 let extraClassDeclaration = [{ 55 std::optional<SmallVector<int64_t, 4>> getShapeForUnroll(); 56 }]; 57 58 let assemblyFormat = "operands attr-dict `:` functional-type(operands, results)"; 59} 60 61//===----------------------------------------------------------------------===// 62// Operator: yield 63//===----------------------------------------------------------------------===// 64def Tosa_YieldOp : Tosa_Op<"yield", [ 65 Terminator, 66 Pure]> { 67 let summary = "yield operator"; 68 69 let description = [{ 70 return operation within the conditional and body of 71 structured control flow. Operation takes variadic operands 72 but produces no results of its own. 73 }]; 74 75 let arguments = (ins 76 Variadic<Tosa_Tensor>:$inputs 77 ); 78 79 let assemblyFormat = "$inputs attr-dict `:` type($inputs)"; 80} 81 82//===----------------------------------------------------------------------===// 83// Operator: variable 84//===----------------------------------------------------------------------===// 85def Tosa_VariableOp : Tosa_Op<"variable", []> { 86 let summary = "Defines a variable"; 87 88 let description = [{ 89 Defines a new TOSA variable. This is a mutable value. 90 Modifications are expressed using read/write semantics. 91 }]; 92 93 let arguments = (ins 94 SymbolNameAttr:$name, 95 TypeAttr:$type, 96 OptionalAttr<AnyAttr>:$initial_value 97 ); 98 99 let assemblyFormat = [{ 100 $name 101 attr-dict 102 custom<TypeOrAttr>($type, $initial_value) 103 }]; 104} 105 106//===----------------------------------------------------------------------===// 107// Operator: variable.write 108//===----------------------------------------------------------------------===// 109def Tosa_VariableWriteOp : Tosa_Op<"variable.write", []> { 110 let summary = "write_buffer operator"; 111 112 let description = [{ 113 Assigns a value to pseudo-buffer resource holding a mutable tensor. 114 }]; 115 116 let arguments = (ins 117 SymbolNameAttr:$name, 118 AnyType:$value 119 ); 120 121 let assemblyFormat = [{ 122 $name attr-dict `,` $value `:` type($value) 123 }]; 124} 125 126//===----------------------------------------------------------------------===// 127// Operator: variable.read 128//===----------------------------------------------------------------------===// 129def Tosa_VariableReadOp : Tosa_Op<"variable.read", []> { 130 let summary = "read_buffer operator"; 131 132 let description = [{ 133 Reads the value from a pseudo-buffer resource holding a mutable tensor. 134 }]; 135 136 let arguments = (ins 137 SymbolNameAttr:$name 138 ); 139 140 let results = (outs 141 AnyType:$value 142 ); 143 144 let assemblyFormat = [{ 145 $name attr-dict `:` type($value) 146 }]; 147} 148 149#endif // TOSA_UTIL_OPS 150