1//===- VectorInterfaces.td - Vector interfaces -------------*- 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// Defines the interface for operations on vectors. 10// 11//===----------------------------------------------------------------------===// 12 13#ifndef MLIR_INTERFACES_VECTORINTERFACES 14#define MLIR_INTERFACES_VECTORINTERFACES 15 16include "mlir/IR/OpBase.td" 17 18def VectorUnrollOpInterface : OpInterface<"VectorUnrollOpInterface"> { 19 let description = [{ 20 Encodes properties of an operation on vectors that can be unrolled. 21 }]; 22 let cppNamespace = "::mlir"; 23 24 let methods = [ 25 InterfaceMethod< 26 /*desc=*/[{ 27 Return the shape ratio of unrolling to the target vector shape 28 `targetShape`. Return `None` if the op cannot be unrolled to the target 29 vector shape. 30 }], 31 /*retTy=*/"Optional<SmallVector<int64_t, 4>>", 32 /*methodName=*/"getShapeForUnroll", 33 /*args=*/(ins), 34 /*methodBody=*/"", 35 /*defaultImplementation=*/[{ 36 assert($_op->getNumResults() == 1); 37 auto vt = $_op.getResult().getType(). 38 template dyn_cast<VectorType>(); 39 if (!vt) 40 return None; 41 SmallVector<int64_t, 4> res(vt.getShape().begin(), vt.getShape().end()); 42 return res; 43 }] 44 >, 45 ]; 46} 47 48def VectorTransferOpInterface : OpInterface<"VectorTransferOpInterface"> { 49 let description = [{ 50 Encodes properties of an operation on vectors that can be unrolled. 51 }]; 52 let cppNamespace = "::mlir"; 53 54 let methods = [ 55 StaticInterfaceMethod< 56 /*desc=*/"Return the `masked` attribute name.", 57 /*retTy=*/"StringRef", 58 /*methodName=*/"getMaskedAttrName", 59 /*args=*/(ins), 60 /*methodBody=*/"", 61 /*defaultImplementation=*/ [{ return "masked"; }] 62 >, 63 StaticInterfaceMethod< 64 /*desc=*/"Return the `permutation_map` attribute name.", 65 /*retTy=*/"StringRef", 66 /*methodName=*/"getPermutationMapAttrName", 67 /*args=*/(ins), 68 /*methodBody=*/"", 69 /*defaultImplementation=*/ [{ return "permutation_map"; }] 70 >, 71 InterfaceMethod< 72 /*desc=*/[{ 73 Return `false` when the `masked` attribute at dimension 74 `dim` is set to `false`. Return `true` otherwise.}], 75 /*retTy=*/"bool", 76 /*methodName=*/"isMaskedDim", 77 /*args=*/(ins "unsigned":$dim), 78 /*methodBody=*/"", 79 /*defaultImplementation=*/[{ 80 return !$_op.masked() || 81 $_op.masked()->template cast<ArrayAttr>()[dim] 82 .template cast<BoolAttr>().getValue(); 83 }] 84 >, 85 InterfaceMethod< 86 /*desc=*/"Return the memref operand.", 87 /*retTy=*/"Value", 88 /*methodName=*/"memref", 89 /*args=*/(ins), 90 /*methodBody=*/"return $_op.memref();" 91 /*defaultImplementation=*/ 92 >, 93 InterfaceMethod< 94 /*desc=*/"Return the vector operand or result.", 95 /*retTy=*/"Value", 96 /*methodName=*/"vector", 97 /*args=*/(ins), 98 /*methodBody=*/"return $_op.vector();" 99 /*defaultImplementation=*/ 100 >, 101 InterfaceMethod< 102 /*desc=*/"Return the indices operands.", 103 /*retTy=*/"ValueRange", 104 /*methodName=*/"indices", 105 /*args=*/(ins), 106 /*methodBody=*/"return $_op.indices();" 107 /*defaultImplementation=*/ 108 >, 109 InterfaceMethod< 110 /*desc=*/"Return the permutation map.", 111 /*retTy=*/"AffineMap", 112 /*methodName=*/"permutation_map", 113 /*args=*/(ins), 114 /*methodBody=*/"return $_op.permutation_map();" 115 /*defaultImplementation=*/ 116 >, 117 InterfaceMethod< 118 /*desc=*/"Return the `masked` boolean ArrayAttr.", 119 /*retTy=*/"Optional<ArrayAttr>", 120 /*methodName=*/"masked", 121 /*args=*/(ins), 122 /*methodBody=*/"return $_op.masked();" 123 /*defaultImplementation=*/ 124 >, 125 InterfaceMethod< 126 /*desc=*/"Return the MemRefType.", 127 /*retTy=*/"MemRefType", 128 /*methodName=*/"getMemRefType", 129 /*args=*/(ins), 130 /*methodBody=*/"", 131 /*defaultImplementation=*/ 132 "return $_op.memref().getType().template cast<MemRefType>();" 133 >, 134 InterfaceMethod< 135 /*desc=*/"Return the VectorType.", 136 /*retTy=*/"VectorType", 137 /*methodName=*/"getVectorType", 138 /*args=*/(ins), 139 /*methodBody=*/"", 140 /*defaultImplementation=*/[{ 141 return $_op.vector().getType().template dyn_cast<VectorType>(); 142 }] 143 >, 144 InterfaceMethod< 145 /*desc=*/[{ Return the number of dimensions that participate in the 146 permutation map.}], 147 /*retTy=*/"unsigned", 148 /*methodName=*/"getTransferRank", 149 /*args=*/(ins), 150 /*methodBody=*/"", 151 /*defaultImplementation=*/ 152 "return $_op.permutation_map().getNumResults();" 153 >, 154 InterfaceMethod< 155 /*desc=*/[{ Return the number of leading memref dimensions that do not 156 participate in the permutation map.}], 157 /*retTy=*/"unsigned", 158 /*methodName=*/"getLeadingMemRefRank", 159 /*args=*/(ins), 160 /*methodBody=*/"", 161 /*defaultImplementation=*/ 162 "return $_op.getMemRefType().getRank() - $_op.getTransferRank();" 163 >, 164 InterfaceMethod< 165 /*desc=*/[{ Returns true if at least one of the dimensions is masked.}], 166 /*retTy=*/"bool", 167 /*methodName=*/"hasMaskedDim", 168 /*args=*/(ins), 169 /*methodBody=*/"", 170 /*defaultImplementation=*/[{ 171 for (unsigned idx = 0, e = $_op.getTransferRank(); idx < e; ++idx) 172 if ($_op.isMaskedDim(idx)) 173 return true; 174 return false; 175 }] 176 >, 177 InterfaceMethod< 178 /*desc=*/[{ 179 Helper function to account for the fact that `permutationMap` results and 180 `op.indices` sizes may not match and may not be aligned. The first 181 `getLeadingMemRefRank()` indices may just be indexed and not transferred 182 from/into the vector. 183 For example: 184 ``` 185 vector.transfer %0[%i, %j, %k, %c0] : 186 memref<?x?x?x?xf32>, vector<2x4xf32> 187 ``` 188 with `permutation_map = (d0, d1, d2, d3) -> (d2, d3)`. 189 Provide a zip function to coiterate on 2 running indices: `resultIdx` and 190 `indicesIdx` which accounts for this misalignment. 191 }], 192 /*retTy=*/"void", 193 /*methodName=*/"zipResultAndIndexing", 194 /*args=*/(ins "llvm::function_ref<void(int64_t, int64_t)>":$fun), 195 /*methodBody=*/"", 196 /*defaultImplementation=*/[{ 197 for (int64_t resultIdx = 0, 198 indicesIdx = $_op.getLeadingMemRefRank(), 199 eResult = $_op.getTransferRank(); 200 resultIdx < eResult; 201 ++resultIdx, ++indicesIdx) 202 fun(resultIdx, indicesIdx); 203 }] 204 >, 205 ]; 206} 207 208#endif // MLIR_INTERFACES_VECTORINTERFACES 209