|
| 1 | +//===- TensorOps.td - Tensor op definitions ----------------*- 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 | +#ifndef TENSOR_OPS |
| 10 | +#define TENSOR_OPS |
| 11 | + |
| 12 | +include "mlir/Dialect/Tensor/IR/TensorBase.td" |
| 13 | +include "mlir/Interfaces/SideEffectInterfaces.td" |
| 14 | + |
| 15 | +class Tensor_Op<string mnemonic, list<OpTrait> traits = []> |
| 16 | + : Op<Tensor_Dialect, mnemonic, traits> { |
| 17 | + let printer = [{ return ::print(p, *this); }]; |
| 18 | + let verifier = [{ return ::verify(*this); }]; |
| 19 | + let parser = [{ return ::parse$cppClass(parser, result); }]; |
| 20 | +} |
| 21 | + |
| 22 | +//===----------------------------------------------------------------------===// |
| 23 | +// ExtractOp |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | + |
| 26 | +def Tensor_ExtractOp : Tensor_Op<"extract", |
| 27 | + [NoSideEffect, |
| 28 | + TypesMatchWith<"result type matches element type of tensor", |
| 29 | + "tensor", "result", |
| 30 | + "$_self.cast<ShapedType>().getElementType()">]> { |
| 31 | + let summary = "element extraction operation"; |
| 32 | + let description = [{ |
| 33 | + The `tensor.extract` op reads a tensor and returns one |
| 34 | + element from it specified by an index list. The output of the op is a |
| 35 | + new value with the same type as the elements of the tensor. The |
| 36 | + arity of indices must match the rank of the accessed value (i.e., if a |
| 37 | + tensor is of rank 3, then 3 indices are required for the extract. The |
| 38 | + indices should all be of `index` type. |
| 39 | + |
| 40 | + Example: |
| 41 | + |
| 42 | + ```mlir |
| 43 | + %4 = tensor.extract %t[%1, %2] : tensor<4x4xi32> |
| 44 | + %5 = tensor.extract %rt[%1, %2] : tensor<?x?xi32> |
| 45 | + %6 = tensor.extract %ut[%1, %2] : tensor<*xi32> |
| 46 | + ``` |
| 47 | + }]; |
| 48 | + |
| 49 | + let arguments = (ins AnyTensor:$tensor, Variadic<Index>:$indices); |
| 50 | + let results = (outs AnyType:$result); |
| 51 | + let assemblyFormat = "$tensor `[` $indices `]` attr-dict `:` type($tensor)"; |
| 52 | + |
| 53 | + let builders = [ |
| 54 | + OpBuilderDAG<(ins "Value":$tensor, CArg<"ValueRange", "{}">:$indices), [{ |
| 55 | + auto resType = tensor.getType().cast<ShapedType>().getElementType(); |
| 56 | + build($_builder, $_state, resType, tensor, indices); |
| 57 | + }]>]; |
| 58 | + |
| 59 | + let hasFolder = 1; |
| 60 | +} |
| 61 | + |
| 62 | +#endif // TENSOR_OPS |
0 commit comments