| 1 | //===- Utils.cpp - Utilities to support the Linalg dialect ----------------===// |
| 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 implements utilities for the Linalg dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Dialect/Linalg/Utils/Utils.h" |
| 14 | |
| 15 | #include "mlir/Analysis/SliceAnalysis.h" |
| 16 | #include "mlir/Dialect/Affine/Analysis/AffineStructures.h" |
| 17 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 18 | #include "mlir/Dialect/Affine/LoopUtils.h" |
| 19 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 20 | #include "mlir/Dialect/Arith/Utils/Utils.h" |
| 21 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 22 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 23 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 24 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 25 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 26 | #include "mlir/Dialect/Tensor/Utils/Utils.h" |
| 27 | #include "mlir/Dialect/Utils/IndexingUtils.h" |
| 28 | #include "mlir/Dialect/Utils/StaticValueUtils.h" |
| 29 | #include "mlir/IR/AffineExpr.h" |
| 30 | #include "mlir/IR/AffineExprVisitor.h" |
| 31 | #include "mlir/IR/AffineMap.h" |
| 32 | #include "mlir/IR/Matchers.h" |
| 33 | #include "llvm/ADT/SmallVectorExtras.h" |
| 34 | #include "llvm/ADT/TypeSwitch.h" |
| 35 | #include "llvm/Support/Debug.h" |
| 36 | |
| 37 | #include <optional> |
| 38 | |
| 39 | #define DEBUG_TYPE "linalg-utils" |
| 40 | |
| 41 | using namespace mlir; |
| 42 | using namespace presburger; |
| 43 | using namespace mlir::affine; |
| 44 | using namespace mlir::linalg; |
| 45 | using namespace mlir::scf; |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | // Helper visitor to determine whether an AffineExpr is tiled. |
| 50 | // This is achieved by traversing every AffineDimExpr with position `pos` and |
| 51 | // checking whether the corresponding `tileSizes[pos]` is non-zero. |
| 52 | // This also enforces only positive coefficients occur in multiplications. |
| 53 | // |
| 54 | // Example: |
| 55 | // `d0 + 2 * d1 + d3` is tiled by [0, 0, 0, 2] but not by [0, 0, 2, 0] |
| 56 | // |
| 57 | struct TileCheck : public AffineExprVisitor<TileCheck> { |
| 58 | TileCheck(ArrayRef<OpFoldResult> tileSizes) : tileSizes(tileSizes) {} |
| 59 | |
| 60 | void visitDimExpr(AffineDimExpr expr) { |
| 61 | isTiled |= !isZeroInteger(v: tileSizes[expr.getPosition()]); |
| 62 | } |
| 63 | void visitAffineBinaryOpExpr(AffineBinaryOpExpr expr) { |
| 64 | visit(expr: expr.getLHS()); |
| 65 | visit(expr: expr.getRHS()); |
| 66 | if (expr.getKind() == mlir::AffineExprKind::Mul) |
| 67 | assert(cast<AffineConstantExpr>(expr.getRHS()).getValue() > 0 && |
| 68 | "nonpositive multiplying coefficient" ); |
| 69 | } |
| 70 | bool isTiled = false; |
| 71 | ArrayRef<OpFoldResult> tileSizes; |
| 72 | }; |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | static bool isTiled(AffineExpr expr, ArrayRef<OpFoldResult> tileSizes) { |
| 77 | if (!expr) |
| 78 | return false; |
| 79 | TileCheck t(tileSizes); |
| 80 | t.visit(expr); |
| 81 | return t.isTiled; |
| 82 | } |
| 83 | |
| 84 | // Checks whether the `map varies with respect to a non-zero `tileSize`. |
| 85 | static bool isTiled(AffineMap map, ArrayRef<OpFoldResult> tileSizes) { |
| 86 | if (!map) |
| 87 | return false; |
| 88 | for (unsigned r = 0; r < map.getNumResults(); ++r) |
| 89 | if (isTiled(expr: map.getResult(idx: r), tileSizes)) |
| 90 | return true; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | std::optional<RegionMatcher::BinaryOpKind> |
| 95 | RegionMatcher::matchAsScalarBinaryOp(GenericOp op) { |
| 96 | auto ®ion = op.getRegion(); |
| 97 | if (!region.hasOneBlock()) |
| 98 | return std::nullopt; |
| 99 | |
| 100 | Block &block = region.front(); |
| 101 | if (block.getNumArguments() != 2 || |
| 102 | !block.getArgument(i: 0).getType().isSignlessIntOrFloat() || |
| 103 | !block.getArgument(i: 1).getType().isSignlessIntOrFloat()) |
| 104 | return std::nullopt; |
| 105 | |
| 106 | auto &ops = block.getOperations(); |
| 107 | if (!llvm::hasSingleElement(C: block.without_terminator())) |
| 108 | return std::nullopt; |
| 109 | |
| 110 | using mlir::matchers::m_Val; |
| 111 | auto a = m_Val(v: block.getArgument(i: 0)); |
| 112 | auto b = m_Val(v: block.getArgument(i: 1)); |
| 113 | |
| 114 | auto addPattern = m_Op<linalg::YieldOp>(matchers: m_Op<arith::AddIOp>(matchers: a, matchers: b)); |
| 115 | if (addPattern.match(op: &ops.back())) |
| 116 | return BinaryOpKind::IAdd; |
| 117 | |
| 118 | return std::nullopt; |
| 119 | } |
| 120 | |
| 121 | /// Explicit instantiation of loop nest generator for different loop types. |
| 122 | template struct mlir::linalg::GenerateLoopNest<scf::ForOp>; |
| 123 | template struct mlir::linalg::GenerateLoopNest<scf::ParallelOp>; |
| 124 | template struct mlir::linalg::GenerateLoopNest<AffineForOp>; |
| 125 | |
| 126 | /// Given a list of subview ranges, extract individual values for lower, upper |
| 127 | /// bounds and steps and put them into the corresponding vectors. |
| 128 | static void unpackRanges(OpBuilder &builder, Location loc, |
| 129 | ArrayRef<Range> ranges, SmallVectorImpl<Value> &lbs, |
| 130 | SmallVectorImpl<Value> &ubs, |
| 131 | SmallVectorImpl<Value> &steps) { |
| 132 | for (Range range : ranges) { |
| 133 | lbs.emplace_back( |
| 134 | Args: getValueOrCreateConstantIndexOp(b&: builder, loc, ofr: range.offset)); |
| 135 | ubs.emplace_back(Args: getValueOrCreateConstantIndexOp(b&: builder, loc, ofr: range.size)); |
| 136 | steps.emplace_back( |
| 137 | Args: getValueOrCreateConstantIndexOp(b&: builder, loc, ofr: range.stride)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | // General utilities |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | // |
| 145 | /// The permutation can be obtained from two permutations: |
| 146 | /// a) Compute the permutation vector to move the last `numPackedDims` into |
| 147 | /// the `innerPosDims` of a shape of rank `rank`. |
| 148 | /// b) Compute the permutation vector to move outer dims if the |
| 149 | /// `outerPerm` parameter is not empty. |
| 150 | /// Apply (b) permutation on (a) permutation to get the final permutation. |
| 151 | static SmallVector<int64_t> |
| 152 | computePackUnPackPerm(int64_t rank, ArrayRef<int64_t> &innerDimsPos, |
| 153 | ArrayRef<int64_t> &outerPerm, |
| 154 | PackingMetadata &packingMetadata) { |
| 155 | int64_t numPackedDims = innerDimsPos.size(); |
| 156 | auto lastDims = |
| 157 | llvm::to_vector(Range: llvm::seq<int64_t>(Begin: rank - numPackedDims, End: rank)); |
| 158 | packingMetadata = computePackingMetadata(packedRank: rank, innerDimPos: innerDimsPos); |
| 159 | SmallVector<int64_t> innerPositionsPerm = |
| 160 | computePermutationVector(permSize: rank, positions: lastDims, desiredPositions: packingMetadata.insertPositions); |
| 161 | |
| 162 | SmallVector<int64_t> outerPos = packingMetadata.outerPositions; |
| 163 | if (!outerPerm.empty()) |
| 164 | applyPermutationToVector(inVec&: outerPos, permutation: outerPerm); |
| 165 | SmallVector<int64_t> outerPositionPerm = |
| 166 | computePermutationVector(permSize: rank, positions: packingMetadata.outerPositions, desiredPositions: outerPos); |
| 167 | |
| 168 | SmallVector<int64_t> packInverseDestPermutation = innerPositionsPerm; |
| 169 | applyPermutationToVector(inVec&: packInverseDestPermutation, permutation: outerPositionPerm); |
| 170 | return packInverseDestPermutation; |
| 171 | } |
| 172 | |
| 173 | namespace mlir { |
| 174 | namespace linalg { |
| 175 | |
| 176 | SmallVector<int64_t> getPackInverseDestPerm(PackOp packOp, |
| 177 | PackingMetadata &metadata) { |
| 178 | |
| 179 | int64_t packedRank = packOp.getDestType().getRank(); |
| 180 | ArrayRef<int64_t> innerDimPos = packOp.getInnerDimsPos(); |
| 181 | ArrayRef<int64_t> outerPerm = packOp.getOuterDimsPerm(); |
| 182 | SmallVector<int64_t> packInvDestPerm = |
| 183 | computePackUnPackPerm(rank: packedRank, innerDimsPos&: innerDimPos, outerPerm, packingMetadata&: metadata); |
| 184 | return packInvDestPerm; |
| 185 | } |
| 186 | |
| 187 | SmallVector<int64_t> getUnPackInverseSrcPerm(UnPackOp unpackOp, |
| 188 | PackingMetadata &metadata) { |
| 189 | int64_t packedRank = unpackOp.getSourceType().getRank(); |
| 190 | ArrayRef<int64_t> innerDimPos = unpackOp.getInnerDimsPos(); |
| 191 | ArrayRef<int64_t> outerPerm = unpackOp.getOuterDimsPerm(); |
| 192 | SmallVector<int64_t> unpackInvSrcPerm = |
| 193 | computePackUnPackPerm(rank: packedRank, innerDimsPos&: innerDimPos, outerPerm, packingMetadata&: metadata); |
| 194 | return unpackInvSrcPerm; |
| 195 | } |
| 196 | |
| 197 | bool allIndexingsAreProjectedPermutation(LinalgOp op) { |
| 198 | return llvm::all_of(Range: op.getIndexingMapsArray(), P: [](AffineMap m) { |
| 199 | return m.isProjectedPermutation(/*allowZeroInResults=*/true); |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | bool hasOnlyScalarElementwiseOp(Region &r) { |
| 204 | if (!r.hasOneBlock()) |
| 205 | return false; |
| 206 | for (Operation &op : r.front()) { |
| 207 | if (!(isa<arith::ConstantOp, func::ConstantOp, tensor::ExtractOp, |
| 208 | linalg::YieldOp, linalg::IndexOp, AffineApplyOp>(Val: op) || |
| 209 | OpTrait::hasElementwiseMappableTraits(op: &op)) || |
| 210 | llvm::any_of(Range: op.getResultTypes(), |
| 211 | P: [](Type type) { return !type.isIntOrIndexOrFloat(); })) |
| 212 | return false; |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | bool isElementwise(LinalgOp op) { |
| 218 | if (op.getNumLoops() != op.getNumParallelLoops()) |
| 219 | return false; |
| 220 | |
| 221 | if (!allIndexingsAreProjectedPermutation(op)) |
| 222 | return false; |
| 223 | |
| 224 | // TODO: relax the restrictions on indexing map. |
| 225 | for (OpOperand &opOperand : op.getDpsInitsMutable()) { |
| 226 | if (!op.getMatchingIndexingMap(opOperand: &opOperand).isPermutation()) |
| 227 | return false; |
| 228 | } |
| 229 | return hasOnlyScalarElementwiseOp(r&: op->getRegion(index: 0)); |
| 230 | } |
| 231 | |
| 232 | bool isParallelIterator(utils::IteratorType iteratorType) { |
| 233 | return iteratorType == utils::IteratorType::parallel; |
| 234 | } |
| 235 | |
| 236 | bool isReductionIterator(utils::IteratorType iteratorType) { |
| 237 | return iteratorType == utils::IteratorType::reduction; |
| 238 | } |
| 239 | |
| 240 | //===----------------------------------------------------------------------===// |
| 241 | // Convolution matcher utilities |
| 242 | //===----------------------------------------------------------------------===// |
| 243 | |
| 244 | /// Returns the BlockArgument that leads to `val`, if any. Traverses optional |
| 245 | /// ext*/sitofp ops. |
| 246 | static BlockArgument getBlockArgumentWithOptionalCastOps(Value val) { |
| 247 | BlockArgument blockArg = dyn_cast<BlockArgument>(Val&: val); |
| 248 | if ((blockArg)) |
| 249 | return blockArg; |
| 250 | |
| 251 | Operation *defOp = val.getDefiningOp(); |
| 252 | if (!dyn_cast_if_present<arith::ExtFOp>(Val: defOp) && |
| 253 | !dyn_cast_if_present<arith::ExtSIOp>(Val: defOp) && |
| 254 | !dyn_cast_if_present<arith::ExtUIOp>(Val: defOp) && |
| 255 | !dyn_cast_if_present<arith::SIToFPOp>(Val: defOp)) { |
| 256 | return nullptr; |
| 257 | } |
| 258 | return dyn_cast<BlockArgument>(Val: defOp->getOperand(idx: 0)); |
| 259 | } |
| 260 | |
| 261 | /// Utility function to match the zero point offset body of quantized |
| 262 | /// convolution ops. |
| 263 | /// |
| 264 | /// Quantized convolutions have a body of the form: |
| 265 | /// %out + ((%input - %inputZp) * (%filter - %filterZp)) |
| 266 | /// where: |
| 267 | /// - %input is the input tensor element (block arg 0) |
| 268 | /// - %filter is the filter tensor element (block arg 1) |
| 269 | /// - %inputZp is the input zero-point scalar (block arg 2) |
| 270 | /// - %filterZp is the filter zero-point scalar (block arg 3) |
| 271 | /// - %out is the output accumulator (block arg 4) |
| 272 | /// |
| 273 | /// This function verifies that the multiplication operands are subtraction |
| 274 | /// operations matching this pattern. |
| 275 | static bool bodyMatcherForZeroPointOffsets(Operation *addOp, Operation *mulOp, |
| 276 | Block *body) { |
| 277 | // The multiplication should have two subtraction operands: |
| 278 | // one for (input - inputZp) and one for (filter - filterZp). |
| 279 | Operation *inputSubOp = mulOp->getOperand(idx: 0).getDefiningOp(); |
| 280 | if (!isa_and_present<arith::SubIOp, arith::SubFOp>(Val: inputSubOp)) |
| 281 | return false; |
| 282 | |
| 283 | Operation *filterSubOp = mulOp->getOperand(idx: 1).getDefiningOp(); |
| 284 | if (!isa_and_present<arith::SubIOp, arith::SubFOp>(Val: filterSubOp)) |
| 285 | return false; |
| 286 | |
| 287 | // Extract block arguments from subtraction operands. |
| 288 | BlockArgument inputBlockArg = |
| 289 | getBlockArgumentWithOptionalCastOps(val: inputSubOp->getOperand(idx: 0)); |
| 290 | BlockArgument inputZpBlockArg = |
| 291 | getBlockArgumentWithOptionalCastOps(val: inputSubOp->getOperand(idx: 1)); |
| 292 | BlockArgument filterBlockArg = |
| 293 | getBlockArgumentWithOptionalCastOps(val: filterSubOp->getOperand(idx: 0)); |
| 294 | BlockArgument filterZpBlockArg = |
| 295 | getBlockArgumentWithOptionalCastOps(val: filterSubOp->getOperand(idx: 1)); |
| 296 | BlockArgument outBlockArg = |
| 297 | getBlockArgumentWithOptionalCastOps(val: addOp->getOperand(idx: 0)); |
| 298 | |
| 299 | // Verify all block arguments are valid. |
| 300 | if (!inputBlockArg || !inputZpBlockArg || !filterBlockArg || |
| 301 | !filterZpBlockArg || !outBlockArg) |
| 302 | return false; |
| 303 | |
| 304 | // Verify all block arguments belong to the convolution body. |
| 305 | if (inputBlockArg.getOwner() != body || inputZpBlockArg.getOwner() != body || |
| 306 | filterBlockArg.getOwner() != body || |
| 307 | filterZpBlockArg.getOwner() != body || outBlockArg.getOwner() != body) |
| 308 | return false; |
| 309 | |
| 310 | // Verify block arguments have expected indices: |
| 311 | // arg0: input, arg1: filter, arg2: inputZp, arg3: filterZp, arg4: output |
| 312 | if (inputBlockArg.getArgNumber() != 0 || filterBlockArg.getArgNumber() != 1 || |
| 313 | inputZpBlockArg.getArgNumber() != 2 || |
| 314 | filterZpBlockArg.getArgNumber() != 3 || outBlockArg.getArgNumber() != 4) |
| 315 | return false; |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | /// Utility to match block body for convolution ops. |
| 321 | /// The body is thus expected to yield :- |
| 322 | /// %out + (%lhs * %rhs) |
| 323 | /// where: %lhs, %rhs and %out are block arguments and |
| 324 | /// %lhs and %rhs can have optional upcast operation. |
| 325 | /// For i1 element types, the pattern matches: |
| 326 | /// %out | (%lhs & %rhs) |
| 327 | /// using arith.ori for accumulation and arith.andi for multiplication. |
| 328 | /// NOTE: In case of zero point offset convolution ops %lhs and %rhs would be :- |
| 329 | /// %input - %input_scalar |
| 330 | /// where, %input_scalar can have optional upcast operation. |
| 331 | static bool bodyMatcherForConvolutionOps(Value yieldVal, Block *body, |
| 332 | bool containsZeroPointOffset = false) { |
| 333 | bool isOrOp = false; |
| 334 | Operation *accOp = yieldVal.getDefiningOp(); |
| 335 | if (!isa_and_present<arith::AddIOp, arith::AddFOp>(Val: accOp)) { |
| 336 | if (!isa_and_present<arith::OrIOp>(Val: accOp)) |
| 337 | return false; |
| 338 | isOrOp = true; |
| 339 | } |
| 340 | |
| 341 | Operation *mulOp = accOp->getOperand(idx: 1).getDefiningOp(); |
| 342 | if (!isOrOp && !isa_and_present<arith::MulIOp, arith::MulFOp>(Val: mulOp)) |
| 343 | return false; |
| 344 | if (isOrOp && !isa_and_present<arith::AndIOp>(Val: mulOp)) |
| 345 | return false; |
| 346 | |
| 347 | if (containsZeroPointOffset) { |
| 348 | return bodyMatcherForZeroPointOffsets(addOp: accOp, mulOp, body); |
| 349 | } |
| 350 | BlockArgument lhsBlockArg = |
| 351 | getBlockArgumentWithOptionalCastOps(val: mulOp->getOperand(idx: 0)); |
| 352 | BlockArgument rhsBlockArg = |
| 353 | getBlockArgumentWithOptionalCastOps(val: mulOp->getOperand(idx: 1)); |
| 354 | BlockArgument outBlockArg = |
| 355 | getBlockArgumentWithOptionalCastOps(val: accOp->getOperand(idx: 0)); |
| 356 | if (!lhsBlockArg || !rhsBlockArg || !outBlockArg || |
| 357 | lhsBlockArg.getOwner() != body || rhsBlockArg.getOwner() != body || |
| 358 | outBlockArg.getOwner() != body || lhsBlockArg.getArgNumber() != 0 || |
| 359 | rhsBlockArg.getArgNumber() != 1 || outBlockArg.getArgNumber() != 2) |
| 360 | return false; |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | /// Utility to match block body for linalg.pool* ops. |
| 365 | template <typename... OpTypes> |
| 366 | static bool bodyMatcherForPoolOps(Value yieldVal, Block *body) { |
| 367 | Operation *defOp = yieldVal.getDefiningOp(); |
| 368 | if (!(isa_and_present<OpTypes>(defOp) || ...)) |
| 369 | return false; |
| 370 | |
| 371 | BlockArgument lhsArg = |
| 372 | getBlockArgumentWithOptionalCastOps(val: defOp->getOperand(idx: 0)); |
| 373 | BlockArgument rhsArg = |
| 374 | getBlockArgumentWithOptionalCastOps(val: defOp->getOperand(idx: 1)); |
| 375 | if (!lhsArg || !rhsArg || lhsArg.getOwner() != body || |
| 376 | rhsArg.getOwner() != body || lhsArg.getArgNumber() != 2 || |
| 377 | rhsArg.getArgNumber() != 0) |
| 378 | return false; |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | static bool bodyMatcherForMaxSignedPoolOps(Value yieldVal, Block *body) { |
| 383 | return bodyMatcherForPoolOps<arith::MaximumFOp, arith::MaxSIOp>(yieldVal, |
| 384 | body); |
| 385 | } |
| 386 | |
| 387 | static bool bodyMatcherForMaxUnsignedPoolOps(Value yieldVal, Block *body) { |
| 388 | return bodyMatcherForPoolOps<arith::MaxUIOp>(yieldVal, body); |
| 389 | } |
| 390 | |
| 391 | static bool bodyMatcherForMinSignedPoolOps(Value yieldVal, Block *body) { |
| 392 | return bodyMatcherForPoolOps<arith::MinimumFOp, arith::MinSIOp>(yieldVal, |
| 393 | body); |
| 394 | } |
| 395 | |
| 396 | static bool bodyMatcherForMinUnsignedPoolOps(Value yieldVal, Block *body) { |
| 397 | return bodyMatcherForPoolOps<arith::MinUIOp>(yieldVal, body); |
| 398 | } |
| 399 | |
| 400 | /// Matches sum pooling body pattern. For i1 element types, arith.ori is used |
| 401 | /// instead of arith.addi/arith.addf for accumulation. |
| 402 | static bool bodyMatcherForSumPoolOps(Value yieldVal, Block *body) { |
| 403 | return bodyMatcherForPoolOps<arith::AddIOp, arith::AddFOp, arith::OrIOp>( |
| 404 | yieldVal, body); |
| 405 | } |
| 406 | |
| 407 | static AffineExpr getAffineMapDim(ArrayAttr indexingMaps, uint32_t mapIndex, |
| 408 | uint32_t dimIndex) { |
| 409 | auto affineMap = cast<AffineMapAttr>(Val: indexingMaps[mapIndex]).getValue(); |
| 410 | if (dimIndex < affineMap.getNumResults()) |
| 411 | return affineMap.getResult(idx: dimIndex); |
| 412 | return nullptr; |
| 413 | } |
| 414 | |
| 415 | /// Check if `expr` is either: |
| 416 | /// - a dimension expr alone (implying multiplication by 1), or |
| 417 | /// - a multiplication of dimension expr by any positive constant != 1 |
| 418 | /// In both cases we will capture the dimension expression into `dim` and |
| 419 | /// return the constant multiplier. Returns -1 in case of a match failure. |
| 420 | static int64_t isDimTimesConstantOrDimOnly(AffineExpr expr, AffineExpr &dim) { |
| 421 | if ((dim = dyn_cast<AffineDimExpr>(Val&: expr))) |
| 422 | return 1; |
| 423 | |
| 424 | auto mulExpr = dyn_cast<AffineBinaryOpExpr>(Val&: expr); |
| 425 | if (!mulExpr || mulExpr.getKind() != AffineExprKind::Mul) |
| 426 | return -1; |
| 427 | |
| 428 | AffineExpr lhs = mulExpr.getLHS(); |
| 429 | AffineExpr rhs = mulExpr.getRHS(); |
| 430 | |
| 431 | AffineConstantExpr cst = nullptr; |
| 432 | if (((dim = dyn_cast<AffineDimExpr>(Val&: lhs)) && |
| 433 | (cst = dyn_cast<AffineConstantExpr>(Val&: rhs))) || |
| 434 | ((dim = dyn_cast<AffineDimExpr>(Val&: rhs)) && |
| 435 | (cst = dyn_cast<AffineConstantExpr>(Val&: lhs)))) |
| 436 | return cst.getValue(); |
| 437 | return -1; |
| 438 | } |
| 439 | |
| 440 | /// Given an array of AffineMaps `indexingMaps` verify the following |
| 441 | /// commutatively:- |
| 442 | /// indexingMaps[0].getResult(iDim) == |
| 443 | /// indexingMaps[1].getResult(fDim) * <c0> + |
| 444 | /// indexingMaps[n-1].getResult(oDim) * <c1> |
| 445 | /// where, |
| 446 | /// - c0 and c1 can be any constant, |
| 447 | /// - n is the size of the indexingMaps' array, |
| 448 | /// - 0, 1 and n-1 are input, filter and output map indices respectively, |
| 449 | /// - iDim, fDim and oDim are the input, filter and output dimension |
| 450 | /// indices in their respective indexing maps |
| 451 | /// Example: |
| 452 | /// #inputMap = affine_map<(d0, d1, d2, d3, d4, d5, d6) |
| 453 | /// -> (d0, d1 * 2 + d4 * 3, d2 + d5, d6)> |
| 454 | /// #filterMap = affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d4, d5, d6, d3)> |
| 455 | /// #outputMap = affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d0, d1, d2, d3)> |
| 456 | /// |
| 457 | /// Here, |
| 458 | /// #inputMap[1] = #outputMap[1] * 2 + #filterMap[0] * 3 |
| 459 | /// Therefore, |
| 460 | /// matchConvDimAddExprPattern(indexingMaps, 1, 0, 1, dilation, stride) |
| 461 | /// would return true and update dilation = 3 and stride = 2 |
| 462 | static bool matchConvDimAddExprPattern(ArrayAttr indexingMaps, unsigned iDim, |
| 463 | unsigned fDim, unsigned oDim, |
| 464 | int64_t &dilation, int64_t &stride) { |
| 465 | unsigned inputMapIdx = 0, filterMapIdx = 1, |
| 466 | outputMapIdx = indexingMaps.size() - 1; |
| 467 | AffineExpr inpExpr = getAffineMapDim(indexingMaps, mapIndex: inputMapIdx, dimIndex: iDim); |
| 468 | auto addExpr = dyn_cast_or_null<AffineBinaryOpExpr>(Val&: inpExpr); |
| 469 | if (!addExpr || addExpr.getKind() != AffineExprKind::Add) |
| 470 | return false; |
| 471 | |
| 472 | AffineExpr dim0, dim1; |
| 473 | int64_t c0 = isDimTimesConstantOrDimOnly(expr: addExpr.getLHS(), dim&: dim0); |
| 474 | int64_t c1 = isDimTimesConstantOrDimOnly(expr: addExpr.getRHS(), dim&: dim1); |
| 475 | |
| 476 | if (c0 == -1 || c1 == -1) |
| 477 | return false; |
| 478 | // Pattern matched with dims and constants extracted. |
| 479 | AffineExpr fExpr = getAffineMapDim(indexingMaps, mapIndex: filterMapIdx, dimIndex: fDim); |
| 480 | AffineExpr oExpr = getAffineMapDim(indexingMaps, mapIndex: outputMapIdx, dimIndex: oDim); |
| 481 | if (dim0 == fExpr && dim1 == oExpr) { |
| 482 | dilation = c0; |
| 483 | stride = c1; |
| 484 | return true; |
| 485 | } |
| 486 | if (dim1 == fExpr && dim0 == oExpr) { |
| 487 | dilation = c1; |
| 488 | stride = c0; |
| 489 | return true; |
| 490 | } |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | /// Returns true if the given indexing maps matches with the expected indexing |
| 495 | /// maps. |
| 496 | static bool convLayoutMatches(ArrayRef<ArrayRef<AffineExpr>> mapListExpected, |
| 497 | ArrayAttr indexingMaps, MLIRContext *context) { |
| 498 | SmallVector<AffineMap, 4> expectedIndexingMaps = |
| 499 | AffineMap::inferFromExprList(exprsList: mapListExpected, context); |
| 500 | return indexingMaps == |
| 501 | ArrayAttr::get(context, |
| 502 | value: llvm::map_to_vector<4>(C&: expectedIndexingMaps, |
| 503 | F: [&](AffineMap m) -> Attribute { |
| 504 | return AffineMapAttr::get(value: m); |
| 505 | })); |
| 506 | } |
| 507 | |
| 508 | /// Enum representing pooling operation types used by ConvMatcherBuilder. |
| 509 | enum class PoolingType { |
| 510 | None, |
| 511 | MaxSigned, |
| 512 | MaxUnsigned, |
| 513 | MinSigned, |
| 514 | MinUnsigned, |
| 515 | Sum |
| 516 | }; |
| 517 | |
| 518 | /// Helper class for building convolution op matchers with minimal boilerplate. |
| 519 | /// Reduces repetitive code across Conv1D/2D/3D and Depthwise variants as well |
| 520 | /// as Pooling ops. |
| 521 | /// |
| 522 | /// Usage: Create an instance with the op, spatial rank, and output pointers for |
| 523 | /// extracted dilations/strides. Then chain matchStride() calls for each spatial |
| 524 | /// dimension, followed by matchMaps() to verify indexing maps, and finally |
| 525 | /// matchBody() to verify the operation body pattern. |
| 526 | /// |
| 527 | /// The `matched` flag starts as `true` and is set to `false` if any match step |
| 528 | /// fails. This allows chaining multiple match calls; once any match fails, all |
| 529 | /// subsequent calls become no-ops and the final result is `false`. |
| 530 | /// |
| 531 | /// The `dilations` and `strides` pointers are output parameters that get |
| 532 | /// populated with the extracted dilation and stride values from the operation's |
| 533 | /// indexing maps during matchStride() calls. These values are initially set to |
| 534 | /// 1 for each spatial dimension and updated as patterns are matched. |
| 535 | class ConvMatcherBuilder { |
| 536 | LinalgOp op; |
| 537 | MLIRContext *ctx; |
| 538 | SmallVector<int64_t> *dilations, *strides; |
| 539 | ArrayAttr indexingMaps; |
| 540 | PoolingType poolingType; |
| 541 | bool matched = true; |
| 542 | |
| 543 | public: |
| 544 | ConvMatcherBuilder(LinalgOp op, unsigned spatialRank, SmallVector<int64_t> *d, |
| 545 | SmallVector<int64_t> *s, |
| 546 | PoolingType poolingType = PoolingType::None) |
| 547 | : op(op), ctx(op->getContext()), dilations(d), strides(s), |
| 548 | indexingMaps(op.getIndexingMaps()), poolingType(poolingType) { |
| 549 | *dilations = SmallVector<int64_t>(spatialRank, 1); |
| 550 | *strides = SmallVector<int64_t>(spatialRank, 1); |
| 551 | } |
| 552 | |
| 553 | /// Get affine dimension expression for dimension `i`. |
| 554 | AffineExpr dim(unsigned i) { return getAffineDimExpr(position: i, context: ctx); } |
| 555 | |
| 556 | /// Build strided expression: base * stride[idx] + kernel * dilation[idx]. |
| 557 | AffineExpr strided(AffineExpr base, AffineExpr kernel, unsigned idx) { |
| 558 | return base * (*strides)[idx] + kernel * (*dilations)[idx]; |
| 559 | } |
| 560 | |
| 561 | /// Match stride/dilation pattern for a spatial dimension. |
| 562 | /// Returns *this for method chaining. |
| 563 | ConvMatcherBuilder &matchStride(unsigned iDim, unsigned fDim, unsigned oDim, |
| 564 | unsigned idx) { |
| 565 | if (matched) { |
| 566 | matched &= matchConvDimAddExprPattern(indexingMaps, iDim, fDim, oDim, |
| 567 | dilation&: (*dilations)[idx], stride&: (*strides)[idx]); |
| 568 | } |
| 569 | return *this; |
| 570 | } |
| 571 | |
| 572 | /// Match expected indexing maps layout. Returns *this for method chaining. |
| 573 | ConvMatcherBuilder &matchMaps(ArrayRef<ArrayRef<AffineExpr>> maps) { |
| 574 | if (matched) |
| 575 | matched &= convLayoutMatches(mapListExpected: maps, indexingMaps, context: ctx); |
| 576 | return *this; |
| 577 | } |
| 578 | |
| 579 | /// Match body pattern. This should be called last. |
| 580 | bool matchBody(bool containsZeroPointOffset = false) { |
| 581 | if (!matched) |
| 582 | return false; |
| 583 | Block *body = op.getBlock(); |
| 584 | auto yieldOp = cast<linalg::YieldOp>(Val: body->getTerminator()); |
| 585 | switch (poolingType) { |
| 586 | case PoolingType::None: |
| 587 | return bodyMatcherForConvolutionOps(yieldVal: yieldOp.getOperand(i: 0), body, |
| 588 | containsZeroPointOffset); |
| 589 | case PoolingType::MaxSigned: |
| 590 | return bodyMatcherForMaxSignedPoolOps(yieldVal: yieldOp.getOperand(i: 0), body); |
| 591 | case PoolingType::MaxUnsigned: |
| 592 | return bodyMatcherForMaxUnsignedPoolOps(yieldVal: yieldOp.getOperand(i: 0), body); |
| 593 | case PoolingType::MinSigned: |
| 594 | return bodyMatcherForMinSignedPoolOps(yieldVal: yieldOp.getOperand(i: 0), body); |
| 595 | case PoolingType::MinUnsigned: |
| 596 | return bodyMatcherForMinUnsignedPoolOps(yieldVal: yieldOp.getOperand(i: 0), body); |
| 597 | case PoolingType::Sum: |
| 598 | return bodyMatcherForSumPoolOps(yieldVal: yieldOp.getOperand(i: 0), body); |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | }; |
| 603 | |
| 604 | //===----------------------------------------------------------------------===// |
| 605 | // Matchers for specific convolution operation. |
| 606 | //===----------------------------------------------------------------------===// |
| 607 | |
| 608 | template <> |
| 609 | std::optional<DilationsAndStrides> |
| 610 | matchConvolutionOpOfType<linalg::Conv1DOp>(LinalgOp op) { |
| 611 | DilationsAndStrides result; |
| 612 | if (isa<linalg::Conv1DOp>(Val: op)) { |
| 613 | // Conv1DOp has no strides/dilations attributes, default to 1. |
| 614 | result.dilations = SmallVector<int64_t>(1, 1); |
| 615 | result.strides = SmallVector<int64_t>(1, 1); |
| 616 | return result; |
| 617 | } |
| 618 | |
| 619 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 620 | return std::nullopt; |
| 621 | |
| 622 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 623 | &result.strides); |
| 624 | AffineExpr W = m.dim(i: 0); |
| 625 | AffineExpr w = m.dim(i: 1); |
| 626 | |
| 627 | if (m.matchStride(/*iDim=*/0, /*fDim=*/0, /*oDim=*/0, /*idx=*/0) |
| 628 | .matchMaps(maps: {/*inputMap=*/{m.strided(base: W, kernel: w, idx: 0)}, |
| 629 | /*filterMap=*/{w}, |
| 630 | /*outputMap=*/{W}}) |
| 631 | .matchBody()) |
| 632 | return result; |
| 633 | return std::nullopt; |
| 634 | } |
| 635 | |
| 636 | template <> |
| 637 | std::optional<DilationsAndStrides> |
| 638 | matchConvolutionOpOfType<linalg::Conv1DNwcWcfOp>(LinalgOp op) { |
| 639 | DilationsAndStrides result; |
| 640 | if (auto convOp = dyn_cast<linalg::Conv1DNwcWcfOp>(Val: op.getOperation())) { |
| 641 | result.dilations = |
| 642 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 643 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 644 | return result; |
| 645 | } |
| 646 | |
| 647 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 648 | return std::nullopt; |
| 649 | |
| 650 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 651 | &result.strides); |
| 652 | AffineExpr N = m.dim(i: 0); |
| 653 | AffineExpr W = m.dim(i: 1); |
| 654 | AffineExpr F = m.dim(i: 2); |
| 655 | AffineExpr w = m.dim(i: 3); |
| 656 | AffineExpr c = m.dim(i: 4); |
| 657 | |
| 658 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 659 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), c}, |
| 660 | /*filterMap=*/{w, c, F}, |
| 661 | /*outputMap=*/{N, W, F}}) |
| 662 | .matchBody()) |
| 663 | return result; |
| 664 | return std::nullopt; |
| 665 | } |
| 666 | |
| 667 | template <> |
| 668 | std::optional<DilationsAndStrides> |
| 669 | matchConvolutionOpOfType<linalg::Conv1DNcwFcwOp>(LinalgOp op) { |
| 670 | DilationsAndStrides result; |
| 671 | if (auto convOp = dyn_cast<linalg::Conv1DNcwFcwOp>(Val: op.getOperation())) { |
| 672 | result.dilations = |
| 673 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 674 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 675 | return result; |
| 676 | } |
| 677 | |
| 678 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 679 | return std::nullopt; |
| 680 | |
| 681 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 682 | &result.strides); |
| 683 | AffineExpr N = m.dim(i: 0); |
| 684 | AffineExpr F = m.dim(i: 1); |
| 685 | AffineExpr W = m.dim(i: 2); |
| 686 | AffineExpr c = m.dim(i: 3); |
| 687 | AffineExpr w = m.dim(i: 4); |
| 688 | |
| 689 | if (m.matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/0) |
| 690 | .matchMaps(maps: {/*inputMap=*/{N, c, m.strided(base: W, kernel: w, idx: 0)}, |
| 691 | /*filterMap=*/{F, c, w}, |
| 692 | /*outputMap=*/{N, F, W}}) |
| 693 | .matchBody()) |
| 694 | return result; |
| 695 | return std::nullopt; |
| 696 | } |
| 697 | |
| 698 | template <> |
| 699 | std::optional<DilationsAndStrides> |
| 700 | matchConvolutionOpOfType<linalg::Conv2DOp>(LinalgOp op) { |
| 701 | DilationsAndStrides result; |
| 702 | if (isa<linalg::Conv2DOp>(Val: op)) { |
| 703 | // Conv2DOp has no strides/dilations attributes, default to 1. |
| 704 | result.dilations = SmallVector<int64_t>(2, 1); |
| 705 | result.strides = SmallVector<int64_t>(2, 1); |
| 706 | return result; |
| 707 | } |
| 708 | |
| 709 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 710 | return std::nullopt; |
| 711 | |
| 712 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 713 | &result.strides); |
| 714 | AffineExpr H = m.dim(i: 0); |
| 715 | AffineExpr W = m.dim(i: 1); |
| 716 | AffineExpr h = m.dim(i: 2); |
| 717 | AffineExpr w = m.dim(i: 3); |
| 718 | |
| 719 | if (m.matchStride(/*iDim=*/0, /*fDim=*/0, /*oDim=*/0, /*idx=*/0) |
| 720 | .matchStride(/*iDim=*/1, /*fDim=*/1, /*oDim=*/1, /*idx=*/1) |
| 721 | .matchMaps(maps: {/*inputMap=*/{m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 722 | /*filterMap=*/{h, w}, |
| 723 | /*outputMap=*/{H, W}}) |
| 724 | .matchBody()) |
| 725 | return result; |
| 726 | return std::nullopt; |
| 727 | } |
| 728 | |
| 729 | template <> |
| 730 | std::optional<DilationsAndStrides> |
| 731 | matchConvolutionOpOfType<linalg::Conv2DNhwcHwcfOp>(LinalgOp op) { |
| 732 | DilationsAndStrides result; |
| 733 | if (auto convOp = dyn_cast<linalg::Conv2DNhwcHwcfOp>(Val: op.getOperation())) { |
| 734 | result.dilations = |
| 735 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 736 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 737 | return result; |
| 738 | } |
| 739 | |
| 740 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 741 | return std::nullopt; |
| 742 | |
| 743 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 744 | &result.strides); |
| 745 | AffineExpr N = m.dim(i: 0); |
| 746 | AffineExpr H = m.dim(i: 1); |
| 747 | AffineExpr W = m.dim(i: 2); |
| 748 | AffineExpr F = m.dim(i: 3); |
| 749 | AffineExpr h = m.dim(i: 4); |
| 750 | AffineExpr w = m.dim(i: 5); |
| 751 | AffineExpr c = m.dim(i: 6); |
| 752 | |
| 753 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 754 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 755 | .matchMaps( |
| 756 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), c}, |
| 757 | /*filterMap=*/{h, w, c, F}, |
| 758 | /*outputMap=*/{N, H, W, F}}) |
| 759 | .matchBody()) |
| 760 | return result; |
| 761 | return std::nullopt; |
| 762 | } |
| 763 | |
| 764 | template <> |
| 765 | std::optional<DilationsAndStrides> |
| 766 | matchConvolutionOpOfType<linalg::Conv2DNhwcHwcfQOp>(LinalgOp op) { |
| 767 | DilationsAndStrides result; |
| 768 | if (auto convOp = dyn_cast<linalg::Conv2DNhwcHwcfQOp>(Val: op.getOperation())) { |
| 769 | result.dilations = |
| 770 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 771 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 772 | return result; |
| 773 | } |
| 774 | |
| 775 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 776 | return std::nullopt; |
| 777 | |
| 778 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 779 | &result.strides); |
| 780 | AffineExpr N = m.dim(i: 0); |
| 781 | AffineExpr H = m.dim(i: 1); |
| 782 | AffineExpr W = m.dim(i: 2); |
| 783 | AffineExpr F = m.dim(i: 3); |
| 784 | AffineExpr h = m.dim(i: 4); |
| 785 | AffineExpr w = m.dim(i: 5); |
| 786 | AffineExpr c = m.dim(i: 6); |
| 787 | |
| 788 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 789 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 790 | .matchMaps( |
| 791 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), c}, |
| 792 | /*filterMap=*/{h, w, c, F}, |
| 793 | /*scalarMap=*/{}, |
| 794 | /*scalarMap=*/{}, |
| 795 | /*outputMap=*/{N, H, W, F}}) |
| 796 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 797 | return result; |
| 798 | return std::nullopt; |
| 799 | } |
| 800 | |
| 801 | template <> |
| 802 | std::optional<DilationsAndStrides> |
| 803 | matchConvolutionOpOfType<linalg::Conv2DNhwcFhwcOp>(LinalgOp op) { |
| 804 | DilationsAndStrides result; |
| 805 | if (auto convOp = dyn_cast<linalg::Conv2DNhwcFhwcOp>(Val: op.getOperation())) { |
| 806 | result.dilations = |
| 807 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 808 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 809 | return result; |
| 810 | } |
| 811 | |
| 812 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 813 | return std::nullopt; |
| 814 | |
| 815 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 816 | &result.strides); |
| 817 | AffineExpr N = m.dim(i: 0); |
| 818 | AffineExpr H = m.dim(i: 1); |
| 819 | AffineExpr W = m.dim(i: 2); |
| 820 | AffineExpr F = m.dim(i: 3); |
| 821 | AffineExpr h = m.dim(i: 4); |
| 822 | AffineExpr w = m.dim(i: 5); |
| 823 | AffineExpr c = m.dim(i: 6); |
| 824 | |
| 825 | if (m.matchStride(/*iDim=*/1, /*fDim=*/1, /*oDim=*/1, /*idx=*/0) |
| 826 | .matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/1) |
| 827 | .matchMaps( |
| 828 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), c}, |
| 829 | /*filterMap=*/{F, h, w, c}, |
| 830 | /*outputMap=*/{N, H, W, F}}) |
| 831 | .matchBody()) |
| 832 | return result; |
| 833 | return std::nullopt; |
| 834 | } |
| 835 | |
| 836 | template <> |
| 837 | std::optional<DilationsAndStrides> |
| 838 | matchConvolutionOpOfType<linalg::Conv2DNhwcFhwcQOp>(LinalgOp op) { |
| 839 | DilationsAndStrides result; |
| 840 | if (auto convOp = dyn_cast<linalg::Conv2DNhwcFhwcQOp>(Val: op.getOperation())) { |
| 841 | result.dilations = |
| 842 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 843 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 844 | return result; |
| 845 | } |
| 846 | |
| 847 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 848 | return std::nullopt; |
| 849 | |
| 850 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 851 | &result.strides); |
| 852 | AffineExpr N = m.dim(i: 0); |
| 853 | AffineExpr H = m.dim(i: 1); |
| 854 | AffineExpr W = m.dim(i: 2); |
| 855 | AffineExpr F = m.dim(i: 3); |
| 856 | AffineExpr h = m.dim(i: 4); |
| 857 | AffineExpr w = m.dim(i: 5); |
| 858 | AffineExpr c = m.dim(i: 6); |
| 859 | |
| 860 | if (m.matchStride(/*iDim=*/1, /*fDim=*/1, /*oDim=*/1, /*idx=*/0) |
| 861 | .matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/1) |
| 862 | .matchMaps( |
| 863 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), c}, |
| 864 | /*filterMap=*/{F, h, w, c}, |
| 865 | /*scalarMap=*/{}, |
| 866 | /*scalarMap=*/{}, |
| 867 | /*outputMap=*/{N, H, W, F}}) |
| 868 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 869 | return result; |
| 870 | return std::nullopt; |
| 871 | } |
| 872 | |
| 873 | template <> |
| 874 | std::optional<DilationsAndStrides> |
| 875 | matchConvolutionOpOfType<linalg::Conv2DNchwFchwOp>(LinalgOp op) { |
| 876 | DilationsAndStrides result; |
| 877 | if (auto convOp = dyn_cast<linalg::Conv2DNchwFchwOp>(Val: op.getOperation())) { |
| 878 | result.dilations = |
| 879 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 880 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 881 | return result; |
| 882 | } |
| 883 | |
| 884 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 885 | return std::nullopt; |
| 886 | |
| 887 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 888 | &result.strides); |
| 889 | AffineExpr N = m.dim(i: 0); |
| 890 | AffineExpr F = m.dim(i: 1); |
| 891 | AffineExpr H = m.dim(i: 2); |
| 892 | AffineExpr W = m.dim(i: 3); |
| 893 | AffineExpr c = m.dim(i: 4); |
| 894 | AffineExpr h = m.dim(i: 5); |
| 895 | AffineExpr w = m.dim(i: 6); |
| 896 | |
| 897 | if (m.matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/0) |
| 898 | .matchStride(/*iDim=*/3, /*fDim=*/3, /*oDim=*/3, /*idx=*/1) |
| 899 | .matchMaps( |
| 900 | maps: {/*inputMap=*/{N, c, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 901 | /*filterMap=*/{F, c, h, w}, |
| 902 | /*outputMap=*/{N, F, H, W}}) |
| 903 | .matchBody()) |
| 904 | return result; |
| 905 | return std::nullopt; |
| 906 | } |
| 907 | |
| 908 | template <> |
| 909 | std::optional<DilationsAndStrides> |
| 910 | matchConvolutionOpOfType<linalg::Conv2DNchwFchwQOp>(LinalgOp op) { |
| 911 | DilationsAndStrides result; |
| 912 | if (auto convOp = dyn_cast<linalg::Conv2DNchwFchwQOp>(Val: op.getOperation())) { |
| 913 | result.dilations = |
| 914 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 915 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 916 | return result; |
| 917 | } |
| 918 | |
| 919 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 920 | return std::nullopt; |
| 921 | |
| 922 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 923 | &result.strides); |
| 924 | AffineExpr N = m.dim(i: 0); |
| 925 | AffineExpr F = m.dim(i: 1); |
| 926 | AffineExpr H = m.dim(i: 2); |
| 927 | AffineExpr W = m.dim(i: 3); |
| 928 | AffineExpr c = m.dim(i: 4); |
| 929 | AffineExpr h = m.dim(i: 5); |
| 930 | AffineExpr w = m.dim(i: 6); |
| 931 | |
| 932 | if (m.matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/0) |
| 933 | .matchStride(/*iDim=*/3, /*fDim=*/3, /*oDim=*/3, /*idx=*/1) |
| 934 | .matchMaps( |
| 935 | maps: {/*inputMap=*/{N, c, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 936 | /*filterMap=*/{F, c, h, w}, |
| 937 | /*scalarMap=*/{}, |
| 938 | /*scalarMap=*/{}, |
| 939 | /*outputMap=*/{N, F, H, W}}) |
| 940 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 941 | return result; |
| 942 | return std::nullopt; |
| 943 | } |
| 944 | |
| 945 | template <> |
| 946 | std::optional<DilationsAndStrides> |
| 947 | matchConvolutionOpOfType<linalg::Conv2DNgchwFgchwOp>(LinalgOp op) { |
| 948 | DilationsAndStrides result; |
| 949 | if (auto convOp = dyn_cast<linalg::Conv2DNgchwFgchwOp>(Val: op.getOperation())) { |
| 950 | result.dilations = |
| 951 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 952 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 953 | return result; |
| 954 | } |
| 955 | |
| 956 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 957 | return std::nullopt; |
| 958 | |
| 959 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 960 | &result.strides); |
| 961 | AffineExpr N = m.dim(i: 0); |
| 962 | AffineExpr G = m.dim(i: 1); |
| 963 | AffineExpr F = m.dim(i: 2); |
| 964 | AffineExpr H = m.dim(i: 3); |
| 965 | AffineExpr W = m.dim(i: 4); |
| 966 | AffineExpr c = m.dim(i: 5); |
| 967 | AffineExpr h = m.dim(i: 6); |
| 968 | AffineExpr w = m.dim(i: 7); |
| 969 | |
| 970 | if (m.matchStride(/*iDim=*/3, /*fDim=*/3, /*oDim=*/3, /*idx=*/0) |
| 971 | .matchStride(/*iDim=*/4, /*fDim=*/4, /*oDim=*/4, /*idx=*/1) |
| 972 | .matchMaps( |
| 973 | maps: {/*inputMap=*/{N, G, c, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 974 | /*filterMap=*/{F, G, c, h, w}, |
| 975 | /*outputMap=*/{N, G, F, H, W}}) |
| 976 | .matchBody()) |
| 977 | return result; |
| 978 | return std::nullopt; |
| 979 | } |
| 980 | |
| 981 | template <> |
| 982 | std::optional<DilationsAndStrides> |
| 983 | matchConvolutionOpOfType<linalg::Conv2DNgchwGfchwOp>(LinalgOp op) { |
| 984 | DilationsAndStrides result; |
| 985 | if (auto convOp = dyn_cast<linalg::Conv2DNgchwGfchwOp>(Val: op.getOperation())) { |
| 986 | result.dilations = |
| 987 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 988 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 989 | return result; |
| 990 | } |
| 991 | |
| 992 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 993 | return std::nullopt; |
| 994 | |
| 995 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 996 | &result.strides); |
| 997 | AffineExpr N = m.dim(i: 0); |
| 998 | AffineExpr G = m.dim(i: 1); |
| 999 | AffineExpr F = m.dim(i: 2); |
| 1000 | AffineExpr H = m.dim(i: 3); |
| 1001 | AffineExpr W = m.dim(i: 4); |
| 1002 | AffineExpr c = m.dim(i: 5); |
| 1003 | AffineExpr h = m.dim(i: 6); |
| 1004 | AffineExpr w = m.dim(i: 7); |
| 1005 | |
| 1006 | if (m.matchStride(/*iDim=*/3, /*fDim=*/3, /*oDim=*/3, /*idx=*/0) |
| 1007 | .matchStride(/*iDim=*/4, /*fDim=*/4, /*oDim=*/4, /*idx=*/1) |
| 1008 | .matchMaps( |
| 1009 | maps: {/*inputMap=*/{N, G, c, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 1010 | /*filterMap=*/{G, F, c, h, w}, |
| 1011 | /*outputMap=*/{N, G, F, H, W}}) |
| 1012 | .matchBody()) |
| 1013 | return result; |
| 1014 | return std::nullopt; |
| 1015 | } |
| 1016 | |
| 1017 | template <> |
| 1018 | std::optional<DilationsAndStrides> |
| 1019 | matchConvolutionOpOfType<linalg::Conv2DNgchwGfchwQOp>(LinalgOp op) { |
| 1020 | DilationsAndStrides result; |
| 1021 | if (auto convOp = dyn_cast<linalg::Conv2DNgchwGfchwQOp>(Val: op.getOperation())) { |
| 1022 | result.dilations = |
| 1023 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1024 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1025 | return result; |
| 1026 | } |
| 1027 | |
| 1028 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1029 | return std::nullopt; |
| 1030 | |
| 1031 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1032 | &result.strides); |
| 1033 | AffineExpr N = m.dim(i: 0); |
| 1034 | AffineExpr G = m.dim(i: 1); |
| 1035 | AffineExpr F = m.dim(i: 2); |
| 1036 | AffineExpr H = m.dim(i: 3); |
| 1037 | AffineExpr W = m.dim(i: 4); |
| 1038 | AffineExpr c = m.dim(i: 5); |
| 1039 | AffineExpr h = m.dim(i: 6); |
| 1040 | AffineExpr w = m.dim(i: 7); |
| 1041 | |
| 1042 | if (m.matchStride(/*iDim=*/3, /*fDim=*/3, /*oDim=*/3, /*idx=*/0) |
| 1043 | .matchStride(/*iDim=*/4, /*fDim=*/4, /*oDim=*/4, /*idx=*/1) |
| 1044 | .matchMaps( |
| 1045 | maps: {/*inputMap=*/{N, G, c, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 1046 | /*filterMap=*/{G, F, c, h, w}, |
| 1047 | /*scalarMap=*/{}, |
| 1048 | /*scalarMap=*/{}, |
| 1049 | /*outputMap=*/{N, G, F, H, W}}) |
| 1050 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 1051 | return result; |
| 1052 | return std::nullopt; |
| 1053 | } |
| 1054 | |
| 1055 | template <> |
| 1056 | std::optional<DilationsAndStrides> |
| 1057 | matchConvolutionOpOfType<linalg::Conv2DNhwgcGfhwcOp>(LinalgOp op) { |
| 1058 | DilationsAndStrides result; |
| 1059 | if (auto convOp = dyn_cast<linalg::Conv2DNhwgcGfhwcOp>(Val: op.getOperation())) { |
| 1060 | result.dilations = |
| 1061 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1062 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1063 | return result; |
| 1064 | } |
| 1065 | |
| 1066 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1067 | return std::nullopt; |
| 1068 | |
| 1069 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1070 | &result.strides); |
| 1071 | AffineExpr N = m.dim(i: 0); |
| 1072 | AffineExpr H = m.dim(i: 1); |
| 1073 | AffineExpr W = m.dim(i: 2); |
| 1074 | AffineExpr G = m.dim(i: 3); |
| 1075 | AffineExpr F = m.dim(i: 4); |
| 1076 | AffineExpr h = m.dim(i: 5); |
| 1077 | AffineExpr w = m.dim(i: 6); |
| 1078 | AffineExpr c = m.dim(i: 7); |
| 1079 | |
| 1080 | if (m.matchStride(/*iDim=*/1, /*fDim=*/2, /*oDim=*/1, /*idx=*/0) |
| 1081 | .matchStride(/*iDim=*/2, /*fDim=*/3, /*oDim=*/2, /*idx=*/1) |
| 1082 | .matchMaps( |
| 1083 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), G, c}, |
| 1084 | /*filterMap=*/{G, F, h, w, c}, |
| 1085 | /*outputMap=*/{N, H, W, G, F}}) |
| 1086 | .matchBody()) |
| 1087 | return result; |
| 1088 | return std::nullopt; |
| 1089 | } |
| 1090 | |
| 1091 | template <> |
| 1092 | std::optional<DilationsAndStrides> |
| 1093 | matchConvolutionOpOfType<linalg::Conv2DNhwgcGfhwcQOp>(LinalgOp op) { |
| 1094 | DilationsAndStrides result; |
| 1095 | if (auto convOp = dyn_cast<linalg::Conv2DNhwgcGfhwcQOp>(Val: op.getOperation())) { |
| 1096 | result.dilations = |
| 1097 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1098 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1099 | return result; |
| 1100 | } |
| 1101 | |
| 1102 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1103 | return std::nullopt; |
| 1104 | |
| 1105 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1106 | &result.strides); |
| 1107 | AffineExpr N = m.dim(i: 0); |
| 1108 | AffineExpr H = m.dim(i: 1); |
| 1109 | AffineExpr W = m.dim(i: 2); |
| 1110 | AffineExpr G = m.dim(i: 3); |
| 1111 | AffineExpr F = m.dim(i: 4); |
| 1112 | AffineExpr h = m.dim(i: 5); |
| 1113 | AffineExpr w = m.dim(i: 6); |
| 1114 | AffineExpr c = m.dim(i: 7); |
| 1115 | |
| 1116 | if (m.matchStride(/*iDim=*/1, /*fDim=*/2, /*oDim=*/1, /*idx=*/0) |
| 1117 | .matchStride(/*iDim=*/2, /*fDim=*/3, /*oDim=*/2, /*idx=*/1) |
| 1118 | .matchMaps( |
| 1119 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), G, c}, |
| 1120 | /*filterMap=*/{G, F, h, w, c}, |
| 1121 | /*scalarMap=*/{}, |
| 1122 | /*scalarMap=*/{}, |
| 1123 | /*outputMap=*/{N, H, W, G, F}}) |
| 1124 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 1125 | return result; |
| 1126 | return std::nullopt; |
| 1127 | } |
| 1128 | |
| 1129 | template <> |
| 1130 | std::optional<DilationsAndStrides> |
| 1131 | matchConvolutionOpOfType<linalg::Conv3DOp>(LinalgOp op) { |
| 1132 | DilationsAndStrides result; |
| 1133 | if (isa<linalg::Conv3DOp>(Val: op)) { |
| 1134 | // Conv3DOp has no strides/dilations attributes, default to 1. |
| 1135 | result.dilations = SmallVector<int64_t>(3, 1); |
| 1136 | result.strides = SmallVector<int64_t>(3, 1); |
| 1137 | return result; |
| 1138 | } |
| 1139 | |
| 1140 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1141 | return std::nullopt; |
| 1142 | |
| 1143 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1144 | &result.strides); |
| 1145 | AffineExpr D = m.dim(i: 0); |
| 1146 | AffineExpr H = m.dim(i: 1); |
| 1147 | AffineExpr W = m.dim(i: 2); |
| 1148 | AffineExpr d = m.dim(i: 3); |
| 1149 | AffineExpr h = m.dim(i: 4); |
| 1150 | AffineExpr w = m.dim(i: 5); |
| 1151 | |
| 1152 | if (m.matchStride(/*iDim=*/0, /*fDim=*/0, /*oDim=*/0, /*idx=*/0) |
| 1153 | .matchStride(/*iDim=*/1, /*fDim=*/1, /*oDim=*/1, /*idx=*/1) |
| 1154 | .matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/2) |
| 1155 | .matchMaps(maps: {/*inputMap=*/{m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 1156 | m.strided(base: W, kernel: w, idx: 2)}, |
| 1157 | /*filterMap=*/{d, h, w}, |
| 1158 | /*outputMap=*/{D, H, W}}) |
| 1159 | .matchBody()) |
| 1160 | return result; |
| 1161 | return std::nullopt; |
| 1162 | } |
| 1163 | |
| 1164 | template <> |
| 1165 | std::optional<DilationsAndStrides> |
| 1166 | matchConvolutionOpOfType<linalg::Conv3DNdhwcDhwcfOp>(LinalgOp op) { |
| 1167 | DilationsAndStrides result; |
| 1168 | if (auto convOp = dyn_cast<linalg::Conv3DNdhwcDhwcfOp>(Val: op.getOperation())) { |
| 1169 | result.dilations = |
| 1170 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1171 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1172 | return result; |
| 1173 | } |
| 1174 | |
| 1175 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1176 | return std::nullopt; |
| 1177 | |
| 1178 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1179 | &result.strides); |
| 1180 | AffineExpr N = m.dim(i: 0); |
| 1181 | AffineExpr D = m.dim(i: 1); |
| 1182 | AffineExpr H = m.dim(i: 2); |
| 1183 | AffineExpr W = m.dim(i: 3); |
| 1184 | AffineExpr F = m.dim(i: 4); |
| 1185 | AffineExpr d = m.dim(i: 5); |
| 1186 | AffineExpr h = m.dim(i: 6); |
| 1187 | AffineExpr w = m.dim(i: 7); |
| 1188 | AffineExpr c = m.dim(i: 8); |
| 1189 | |
| 1190 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1191 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1192 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 1193 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 1194 | m.strided(base: W, kernel: w, idx: 2), c}, |
| 1195 | /*filterMap=*/{d, h, w, c, F}, |
| 1196 | /*outputMap=*/{N, D, H, W, F}}) |
| 1197 | .matchBody()) |
| 1198 | return result; |
| 1199 | return std::nullopt; |
| 1200 | } |
| 1201 | |
| 1202 | template <> |
| 1203 | std::optional<DilationsAndStrides> |
| 1204 | matchConvolutionOpOfType<linalg::Conv3DNdhwcDhwcfQOp>(LinalgOp op) { |
| 1205 | DilationsAndStrides result; |
| 1206 | if (auto convOp = dyn_cast<linalg::Conv3DNdhwcDhwcfQOp>(Val: op.getOperation())) { |
| 1207 | result.dilations = |
| 1208 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1209 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1210 | return result; |
| 1211 | } |
| 1212 | |
| 1213 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1214 | return std::nullopt; |
| 1215 | |
| 1216 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1217 | &result.strides); |
| 1218 | AffineExpr N = m.dim(i: 0); |
| 1219 | AffineExpr D = m.dim(i: 1); |
| 1220 | AffineExpr H = m.dim(i: 2); |
| 1221 | AffineExpr W = m.dim(i: 3); |
| 1222 | AffineExpr F = m.dim(i: 4); |
| 1223 | AffineExpr d = m.dim(i: 5); |
| 1224 | AffineExpr h = m.dim(i: 6); |
| 1225 | AffineExpr w = m.dim(i: 7); |
| 1226 | AffineExpr c = m.dim(i: 8); |
| 1227 | |
| 1228 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1229 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1230 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 1231 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 1232 | m.strided(base: W, kernel: w, idx: 2), c}, |
| 1233 | /*filterMap=*/{d, h, w, c, F}, |
| 1234 | /*scalarMap=*/{}, |
| 1235 | /*scalarMap=*/{}, |
| 1236 | /*outputMap=*/{N, D, H, W, F}}) |
| 1237 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 1238 | return result; |
| 1239 | return std::nullopt; |
| 1240 | } |
| 1241 | |
| 1242 | template <> |
| 1243 | std::optional<DilationsAndStrides> |
| 1244 | matchConvolutionOpOfType<linalg::Conv3DNcdhwFcdhwOp>(LinalgOp op) { |
| 1245 | DilationsAndStrides result; |
| 1246 | if (auto convOp = dyn_cast<linalg::Conv3DNcdhwFcdhwOp>(Val: op.getOperation())) { |
| 1247 | result.dilations = |
| 1248 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1249 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1250 | return result; |
| 1251 | } |
| 1252 | |
| 1253 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1254 | return std::nullopt; |
| 1255 | |
| 1256 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1257 | &result.strides); |
| 1258 | AffineExpr N = m.dim(i: 0); |
| 1259 | AffineExpr F = m.dim(i: 1); |
| 1260 | AffineExpr D = m.dim(i: 2); |
| 1261 | AffineExpr H = m.dim(i: 3); |
| 1262 | AffineExpr W = m.dim(i: 4); |
| 1263 | AffineExpr c = m.dim(i: 5); |
| 1264 | AffineExpr d = m.dim(i: 6); |
| 1265 | AffineExpr h = m.dim(i: 7); |
| 1266 | AffineExpr w = m.dim(i: 8); |
| 1267 | |
| 1268 | if (m.matchStride(/*iDim=*/2, /*fDim=*/2, /*oDim=*/2, /*idx=*/0) |
| 1269 | .matchStride(/*iDim=*/3, /*fDim=*/3, /*oDim=*/3, /*idx=*/1) |
| 1270 | .matchStride(/*iDim=*/4, /*fDim=*/4, /*oDim=*/4, /*idx=*/2) |
| 1271 | .matchMaps(maps: {/*inputMap=*/{N, c, m.strided(base: D, kernel: d, idx: 0), |
| 1272 | m.strided(base: H, kernel: h, idx: 1), m.strided(base: W, kernel: w, idx: 2)}, |
| 1273 | /*filterMap=*/{F, c, d, h, w}, |
| 1274 | /*outputMap=*/{N, F, D, H, W}}) |
| 1275 | .matchBody()) |
| 1276 | return result; |
| 1277 | return std::nullopt; |
| 1278 | } |
| 1279 | |
| 1280 | template <> |
| 1281 | std::optional<DilationsAndStrides> |
| 1282 | matchConvolutionOpOfType<linalg::DepthwiseConv1DNcwCwOp>(LinalgOp op) { |
| 1283 | DilationsAndStrides result; |
| 1284 | if (auto convOp = |
| 1285 | dyn_cast<linalg::DepthwiseConv1DNcwCwOp>(Val: op.getOperation())) { |
| 1286 | result.dilations = |
| 1287 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1288 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1289 | return result; |
| 1290 | } |
| 1291 | |
| 1292 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1293 | return std::nullopt; |
| 1294 | |
| 1295 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 1296 | &result.strides); |
| 1297 | AffineExpr N = m.dim(i: 0); |
| 1298 | AffineExpr W = m.dim(i: 1); |
| 1299 | AffineExpr C = m.dim(i: 2); |
| 1300 | AffineExpr w = m.dim(i: 3); |
| 1301 | |
| 1302 | if (m.matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/0) |
| 1303 | .matchMaps(maps: {/*inputMap=*/{N, C, m.strided(base: W, kernel: w, idx: 0)}, |
| 1304 | /*filterMap=*/{C, w}, |
| 1305 | /*outputMap=*/{N, C, W}}) |
| 1306 | .matchBody()) |
| 1307 | return result; |
| 1308 | return std::nullopt; |
| 1309 | } |
| 1310 | |
| 1311 | template <> |
| 1312 | std::optional<DilationsAndStrides> |
| 1313 | matchConvolutionOpOfType<linalg::DepthwiseConv1DNwcWcOp>(LinalgOp op) { |
| 1314 | DilationsAndStrides result; |
| 1315 | if (auto convOp = |
| 1316 | dyn_cast<linalg::DepthwiseConv1DNwcWcOp>(Val: op.getOperation())) { |
| 1317 | result.dilations = |
| 1318 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1319 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1320 | return result; |
| 1321 | } |
| 1322 | |
| 1323 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1324 | return std::nullopt; |
| 1325 | |
| 1326 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 1327 | &result.strides); |
| 1328 | AffineExpr N = m.dim(i: 0); |
| 1329 | AffineExpr W = m.dim(i: 1); |
| 1330 | AffineExpr C = m.dim(i: 2); |
| 1331 | AffineExpr w = m.dim(i: 3); |
| 1332 | |
| 1333 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1334 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 1335 | /*filterMap=*/{w, C}, |
| 1336 | /*outputMap=*/{N, W, C}}) |
| 1337 | .matchBody()) |
| 1338 | return result; |
| 1339 | return std::nullopt; |
| 1340 | } |
| 1341 | |
| 1342 | template <> |
| 1343 | std::optional<DilationsAndStrides> |
| 1344 | matchConvolutionOpOfType<linalg::DepthwiseConv1DNwcWcmOp>(LinalgOp op) { |
| 1345 | DilationsAndStrides result; |
| 1346 | if (auto convOp = |
| 1347 | dyn_cast<linalg::DepthwiseConv1DNwcWcmOp>(Val: op.getOperation())) { |
| 1348 | result.dilations = |
| 1349 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1350 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1351 | return result; |
| 1352 | } |
| 1353 | |
| 1354 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1355 | return std::nullopt; |
| 1356 | |
| 1357 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 1358 | &result.strides); |
| 1359 | AffineExpr N = m.dim(i: 0); |
| 1360 | AffineExpr W = m.dim(i: 1); |
| 1361 | AffineExpr C = m.dim(i: 2); |
| 1362 | AffineExpr CM = m.dim(i: 3); |
| 1363 | AffineExpr w = m.dim(i: 4); |
| 1364 | |
| 1365 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1366 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 1367 | /*filterMap=*/{w, C, CM}, |
| 1368 | /*outputMap=*/{N, W, C, CM}}) |
| 1369 | .matchBody()) |
| 1370 | return result; |
| 1371 | return std::nullopt; |
| 1372 | } |
| 1373 | |
| 1374 | template <> |
| 1375 | std::optional<DilationsAndStrides> |
| 1376 | matchConvolutionOpOfType<linalg::DepthwiseConv2DNchwChwOp>(LinalgOp op) { |
| 1377 | DilationsAndStrides result; |
| 1378 | if (auto convOp = |
| 1379 | dyn_cast<linalg::DepthwiseConv2DNchwChwOp>(Val: op.getOperation())) { |
| 1380 | result.dilations = |
| 1381 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1382 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1383 | return result; |
| 1384 | } |
| 1385 | |
| 1386 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1387 | return std::nullopt; |
| 1388 | |
| 1389 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1390 | &result.strides); |
| 1391 | AffineExpr N = m.dim(i: 0); |
| 1392 | AffineExpr H = m.dim(i: 1); |
| 1393 | AffineExpr W = m.dim(i: 2); |
| 1394 | AffineExpr C = m.dim(i: 3); |
| 1395 | AffineExpr h = m.dim(i: 4); |
| 1396 | AffineExpr w = m.dim(i: 5); |
| 1397 | |
| 1398 | if (m.matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/0) |
| 1399 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/1) |
| 1400 | .matchMaps( |
| 1401 | maps: {/*inputMap=*/{N, C, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 1402 | /*filterMap=*/{C, h, w}, |
| 1403 | /*outputMap=*/{N, C, H, W}}) |
| 1404 | .matchBody()) |
| 1405 | return result; |
| 1406 | return std::nullopt; |
| 1407 | } |
| 1408 | |
| 1409 | template <> |
| 1410 | std::optional<DilationsAndStrides> |
| 1411 | matchConvolutionOpOfType<linalg::DepthwiseConv2DNhwcHwcOp>(LinalgOp op) { |
| 1412 | DilationsAndStrides result; |
| 1413 | if (auto convOp = |
| 1414 | dyn_cast<linalg::DepthwiseConv2DNhwcHwcOp>(Val: op.getOperation())) { |
| 1415 | result.dilations = |
| 1416 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1417 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1418 | return result; |
| 1419 | } |
| 1420 | |
| 1421 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1422 | return std::nullopt; |
| 1423 | |
| 1424 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1425 | &result.strides); |
| 1426 | AffineExpr N = m.dim(i: 0); |
| 1427 | AffineExpr H = m.dim(i: 1); |
| 1428 | AffineExpr W = m.dim(i: 2); |
| 1429 | AffineExpr C = m.dim(i: 3); |
| 1430 | AffineExpr h = m.dim(i: 4); |
| 1431 | AffineExpr w = m.dim(i: 5); |
| 1432 | |
| 1433 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1434 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1435 | .matchMaps( |
| 1436 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1437 | /*filterMap=*/{h, w, C}, |
| 1438 | /*outputMap=*/{N, H, W, C}}) |
| 1439 | .matchBody()) |
| 1440 | return result; |
| 1441 | return std::nullopt; |
| 1442 | } |
| 1443 | |
| 1444 | template <> |
| 1445 | std::optional<DilationsAndStrides> |
| 1446 | matchConvolutionOpOfType<linalg::DepthwiseConv2DNhwcHwcQOp>(LinalgOp op) { |
| 1447 | DilationsAndStrides result; |
| 1448 | if (auto convOp = |
| 1449 | dyn_cast<linalg::DepthwiseConv2DNhwcHwcQOp>(Val: op.getOperation())) { |
| 1450 | result.dilations = |
| 1451 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1452 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1453 | return result; |
| 1454 | } |
| 1455 | |
| 1456 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1457 | return std::nullopt; |
| 1458 | |
| 1459 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1460 | &result.strides); |
| 1461 | AffineExpr N = m.dim(i: 0); |
| 1462 | AffineExpr H = m.dim(i: 1); |
| 1463 | AffineExpr W = m.dim(i: 2); |
| 1464 | AffineExpr C = m.dim(i: 3); |
| 1465 | AffineExpr h = m.dim(i: 4); |
| 1466 | AffineExpr w = m.dim(i: 5); |
| 1467 | |
| 1468 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1469 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1470 | .matchMaps( |
| 1471 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1472 | /*filterMap=*/{h, w, C}, |
| 1473 | /*scalarMap=*/{}, |
| 1474 | /*scalarMap=*/{}, |
| 1475 | /*outputMap=*/{N, H, W, C}}) |
| 1476 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 1477 | return result; |
| 1478 | return std::nullopt; |
| 1479 | } |
| 1480 | |
| 1481 | template <> |
| 1482 | std::optional<DilationsAndStrides> |
| 1483 | matchConvolutionOpOfType<linalg::DepthwiseConv2DNhwcHwcmOp>(LinalgOp op) { |
| 1484 | DilationsAndStrides result; |
| 1485 | if (auto convOp = |
| 1486 | dyn_cast<linalg::DepthwiseConv2DNhwcHwcmOp>(Val: op.getOperation())) { |
| 1487 | result.dilations = |
| 1488 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1489 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1490 | return result; |
| 1491 | } |
| 1492 | |
| 1493 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1494 | return std::nullopt; |
| 1495 | |
| 1496 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1497 | &result.strides); |
| 1498 | AffineExpr N = m.dim(i: 0); |
| 1499 | AffineExpr H = m.dim(i: 1); |
| 1500 | AffineExpr W = m.dim(i: 2); |
| 1501 | AffineExpr C = m.dim(i: 3); |
| 1502 | AffineExpr CM = m.dim(i: 4); |
| 1503 | AffineExpr h = m.dim(i: 5); |
| 1504 | AffineExpr w = m.dim(i: 6); |
| 1505 | |
| 1506 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1507 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1508 | .matchMaps( |
| 1509 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1510 | /*filterMap=*/{h, w, C, CM}, |
| 1511 | /*outputMap=*/{N, H, W, C, CM}}) |
| 1512 | .matchBody()) |
| 1513 | return result; |
| 1514 | return std::nullopt; |
| 1515 | } |
| 1516 | |
| 1517 | template <> |
| 1518 | std::optional<DilationsAndStrides> |
| 1519 | matchConvolutionOpOfType<linalg::DepthwiseConv2DNhwcHwcmQOp>(LinalgOp op) { |
| 1520 | DilationsAndStrides result; |
| 1521 | if (auto convOp = |
| 1522 | dyn_cast<linalg::DepthwiseConv2DNhwcHwcmQOp>(Val: op.getOperation())) { |
| 1523 | result.dilations = |
| 1524 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1525 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1526 | return result; |
| 1527 | } |
| 1528 | |
| 1529 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1530 | return std::nullopt; |
| 1531 | |
| 1532 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1533 | &result.strides); |
| 1534 | AffineExpr N = m.dim(i: 0); |
| 1535 | AffineExpr H = m.dim(i: 1); |
| 1536 | AffineExpr W = m.dim(i: 2); |
| 1537 | AffineExpr C = m.dim(i: 3); |
| 1538 | AffineExpr CM = m.dim(i: 4); |
| 1539 | AffineExpr h = m.dim(i: 5); |
| 1540 | AffineExpr w = m.dim(i: 6); |
| 1541 | |
| 1542 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1543 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1544 | .matchMaps( |
| 1545 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1546 | /*filterMap=*/{h, w, C, CM}, |
| 1547 | /*scalarMap=*/{}, |
| 1548 | /*scalarMap=*/{}, |
| 1549 | /*outputMap=*/{N, H, W, C, CM}}) |
| 1550 | .matchBody(/*containsZeroPointOffset=*/true)) |
| 1551 | return result; |
| 1552 | return std::nullopt; |
| 1553 | } |
| 1554 | |
| 1555 | template <> |
| 1556 | std::optional<DilationsAndStrides> |
| 1557 | matchConvolutionOpOfType<linalg::DepthwiseConv3DNdhwcDhwcOp>(LinalgOp op) { |
| 1558 | DilationsAndStrides result; |
| 1559 | if (auto convOp = |
| 1560 | dyn_cast<linalg::DepthwiseConv3DNdhwcDhwcOp>(Val: op.getOperation())) { |
| 1561 | result.dilations = |
| 1562 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1563 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1564 | return result; |
| 1565 | } |
| 1566 | |
| 1567 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1568 | return std::nullopt; |
| 1569 | |
| 1570 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1571 | &result.strides); |
| 1572 | AffineExpr N = m.dim(i: 0); |
| 1573 | AffineExpr D = m.dim(i: 1); |
| 1574 | AffineExpr H = m.dim(i: 2); |
| 1575 | AffineExpr W = m.dim(i: 3); |
| 1576 | AffineExpr d = m.dim(i: 4); |
| 1577 | AffineExpr h = m.dim(i: 5); |
| 1578 | AffineExpr w = m.dim(i: 6); |
| 1579 | AffineExpr C = m.dim(i: 7); |
| 1580 | |
| 1581 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1582 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1583 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 1584 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 1585 | m.strided(base: W, kernel: w, idx: 2), C}, |
| 1586 | /*filterMap=*/{d, h, w, C}, |
| 1587 | /*outputMap=*/{N, D, H, W, C}}) |
| 1588 | .matchBody()) |
| 1589 | return result; |
| 1590 | return std::nullopt; |
| 1591 | } |
| 1592 | |
| 1593 | template <> |
| 1594 | std::optional<DilationsAndStrides> |
| 1595 | matchConvolutionOpOfType<linalg::DepthwiseConv3DNcdhwCdhwOp>(LinalgOp op) { |
| 1596 | DilationsAndStrides result; |
| 1597 | if (auto convOp = |
| 1598 | dyn_cast<linalg::DepthwiseConv3DNcdhwCdhwOp>(Val: op.getOperation())) { |
| 1599 | result.dilations = |
| 1600 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1601 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1602 | return result; |
| 1603 | } |
| 1604 | |
| 1605 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1606 | return std::nullopt; |
| 1607 | |
| 1608 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1609 | &result.strides); |
| 1610 | AffineExpr N = m.dim(i: 0); |
| 1611 | AffineExpr D = m.dim(i: 1); |
| 1612 | AffineExpr H = m.dim(i: 2); |
| 1613 | AffineExpr W = m.dim(i: 3); |
| 1614 | AffineExpr d = m.dim(i: 4); |
| 1615 | AffineExpr h = m.dim(i: 5); |
| 1616 | AffineExpr w = m.dim(i: 6); |
| 1617 | AffineExpr C = m.dim(i: 7); |
| 1618 | |
| 1619 | if (m.matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/0) |
| 1620 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/1) |
| 1621 | .matchStride(/*iDim=*/4, /*fDim=*/3, /*oDim=*/4, /*idx=*/2) |
| 1622 | .matchMaps(maps: {/*inputMap=*/{N, C, m.strided(base: D, kernel: d, idx: 0), |
| 1623 | m.strided(base: H, kernel: h, idx: 1), m.strided(base: W, kernel: w, idx: 2)}, |
| 1624 | /*filterMap=*/{C, d, h, w}, |
| 1625 | /*outputMap=*/{N, C, D, H, W}}) |
| 1626 | .matchBody()) |
| 1627 | return result; |
| 1628 | return std::nullopt; |
| 1629 | } |
| 1630 | |
| 1631 | template <> |
| 1632 | std::optional<DilationsAndStrides> |
| 1633 | matchConvolutionOpOfType<linalg::DepthwiseConv3DNdhwcDhwcmOp>(LinalgOp op) { |
| 1634 | DilationsAndStrides result; |
| 1635 | if (auto convOp = |
| 1636 | dyn_cast<linalg::DepthwiseConv3DNdhwcDhwcmOp>(Val: op.getOperation())) { |
| 1637 | result.dilations = |
| 1638 | llvm::to_vector(Range: convOp.getDilations().getValues<int64_t>()); |
| 1639 | result.strides = llvm::to_vector(Range: convOp.getStrides().getValues<int64_t>()); |
| 1640 | return result; |
| 1641 | } |
| 1642 | |
| 1643 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1644 | return std::nullopt; |
| 1645 | |
| 1646 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 1647 | &result.strides); |
| 1648 | AffineExpr N = m.dim(i: 0); |
| 1649 | AffineExpr D = m.dim(i: 1); |
| 1650 | AffineExpr H = m.dim(i: 2); |
| 1651 | AffineExpr W = m.dim(i: 3); |
| 1652 | AffineExpr CM = m.dim(i: 4); |
| 1653 | AffineExpr d = m.dim(i: 5); |
| 1654 | AffineExpr h = m.dim(i: 6); |
| 1655 | AffineExpr w = m.dim(i: 7); |
| 1656 | AffineExpr C = m.dim(i: 8); |
| 1657 | |
| 1658 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1659 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1660 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 1661 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 1662 | m.strided(base: W, kernel: w, idx: 2), C}, |
| 1663 | /*filterMap=*/{d, h, w, C, CM}, |
| 1664 | /*outputMap=*/{N, D, H, W, C, CM}}) |
| 1665 | .matchBody()) |
| 1666 | return result; |
| 1667 | return std::nullopt; |
| 1668 | } |
| 1669 | |
| 1670 | template <> |
| 1671 | std::optional<DilationsAndStrides> |
| 1672 | matchConvolutionOpOfType<linalg::PoolingNhwcMaxOp>(LinalgOp op) { |
| 1673 | DilationsAndStrides result; |
| 1674 | if (auto poolOp = dyn_cast<linalg::PoolingNhwcMaxOp>(Val: op.getOperation())) { |
| 1675 | result.dilations = |
| 1676 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1677 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1678 | return result; |
| 1679 | } |
| 1680 | |
| 1681 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1682 | return std::nullopt; |
| 1683 | |
| 1684 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1685 | &result.strides, PoolingType::MaxSigned); |
| 1686 | AffineExpr N = m.dim(i: 0); |
| 1687 | AffineExpr H = m.dim(i: 1); |
| 1688 | AffineExpr W = m.dim(i: 2); |
| 1689 | AffineExpr C = m.dim(i: 3); |
| 1690 | AffineExpr h = m.dim(i: 4); |
| 1691 | AffineExpr w = m.dim(i: 5); |
| 1692 | |
| 1693 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1694 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1695 | .matchMaps( |
| 1696 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1697 | /*filterMap=*/{h, w}, |
| 1698 | /*outputMap=*/{N, H, W, C}}) |
| 1699 | .matchBody()) |
| 1700 | return result; |
| 1701 | return std::nullopt; |
| 1702 | } |
| 1703 | |
| 1704 | template <> |
| 1705 | std::optional<DilationsAndStrides> |
| 1706 | matchConvolutionOpOfType<linalg::PoolingNhwcMinOp>(LinalgOp op) { |
| 1707 | DilationsAndStrides result; |
| 1708 | if (auto poolOp = dyn_cast<linalg::PoolingNhwcMinOp>(Val: op.getOperation())) { |
| 1709 | result.dilations = |
| 1710 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1711 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1712 | return result; |
| 1713 | } |
| 1714 | |
| 1715 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1716 | return std::nullopt; |
| 1717 | |
| 1718 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1719 | &result.strides, PoolingType::MinSigned); |
| 1720 | AffineExpr N = m.dim(i: 0); |
| 1721 | AffineExpr H = m.dim(i: 1); |
| 1722 | AffineExpr W = m.dim(i: 2); |
| 1723 | AffineExpr C = m.dim(i: 3); |
| 1724 | AffineExpr h = m.dim(i: 4); |
| 1725 | AffineExpr w = m.dim(i: 5); |
| 1726 | |
| 1727 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1728 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1729 | .matchMaps( |
| 1730 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1731 | /*filterMap=*/{h, w}, |
| 1732 | /*outputMap=*/{N, H, W, C}}) |
| 1733 | .matchBody()) |
| 1734 | return result; |
| 1735 | return std::nullopt; |
| 1736 | } |
| 1737 | |
| 1738 | template <> |
| 1739 | std::optional<DilationsAndStrides> |
| 1740 | matchConvolutionOpOfType<linalg::PoolingNhwcSumOp>(LinalgOp op) { |
| 1741 | DilationsAndStrides result; |
| 1742 | if (auto poolOp = dyn_cast<linalg::PoolingNhwcSumOp>(Val: op.getOperation())) { |
| 1743 | result.dilations = |
| 1744 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1745 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1746 | return result; |
| 1747 | } |
| 1748 | |
| 1749 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1750 | return std::nullopt; |
| 1751 | |
| 1752 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1753 | &result.strides, PoolingType::Sum); |
| 1754 | AffineExpr N = m.dim(i: 0); |
| 1755 | AffineExpr H = m.dim(i: 1); |
| 1756 | AffineExpr W = m.dim(i: 2); |
| 1757 | AffineExpr C = m.dim(i: 3); |
| 1758 | AffineExpr h = m.dim(i: 4); |
| 1759 | AffineExpr w = m.dim(i: 5); |
| 1760 | |
| 1761 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1762 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1763 | .matchMaps( |
| 1764 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1765 | /*filterMap=*/{h, w}, |
| 1766 | /*outputMap=*/{N, H, W, C}}) |
| 1767 | .matchBody()) |
| 1768 | return result; |
| 1769 | return std::nullopt; |
| 1770 | } |
| 1771 | |
| 1772 | template <> |
| 1773 | std::optional<DilationsAndStrides> |
| 1774 | matchConvolutionOpOfType<linalg::PoolingNhwcMaxUnsignedOp>(LinalgOp op) { |
| 1775 | DilationsAndStrides result; |
| 1776 | if (auto poolOp = |
| 1777 | dyn_cast<linalg::PoolingNhwcMaxUnsignedOp>(Val: op.getOperation())) { |
| 1778 | result.dilations = |
| 1779 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1780 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1781 | return result; |
| 1782 | } |
| 1783 | |
| 1784 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1785 | return std::nullopt; |
| 1786 | |
| 1787 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1788 | &result.strides, PoolingType::MaxUnsigned); |
| 1789 | AffineExpr N = m.dim(i: 0); |
| 1790 | AffineExpr H = m.dim(i: 1); |
| 1791 | AffineExpr W = m.dim(i: 2); |
| 1792 | AffineExpr C = m.dim(i: 3); |
| 1793 | AffineExpr h = m.dim(i: 4); |
| 1794 | AffineExpr w = m.dim(i: 5); |
| 1795 | |
| 1796 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1797 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1798 | .matchMaps( |
| 1799 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1800 | /*filterMap=*/{h, w}, |
| 1801 | /*outputMap=*/{N, H, W, C}}) |
| 1802 | .matchBody()) |
| 1803 | return result; |
| 1804 | return std::nullopt; |
| 1805 | } |
| 1806 | |
| 1807 | template <> |
| 1808 | std::optional<DilationsAndStrides> |
| 1809 | matchConvolutionOpOfType<linalg::PoolingNhwcMinUnsignedOp>(LinalgOp op) { |
| 1810 | DilationsAndStrides result; |
| 1811 | if (auto poolOp = |
| 1812 | dyn_cast<linalg::PoolingNhwcMinUnsignedOp>(Val: op.getOperation())) { |
| 1813 | result.dilations = |
| 1814 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1815 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1816 | return result; |
| 1817 | } |
| 1818 | |
| 1819 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1820 | return std::nullopt; |
| 1821 | |
| 1822 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1823 | &result.strides, PoolingType::MinUnsigned); |
| 1824 | AffineExpr N = m.dim(i: 0); |
| 1825 | AffineExpr H = m.dim(i: 1); |
| 1826 | AffineExpr W = m.dim(i: 2); |
| 1827 | AffineExpr C = m.dim(i: 3); |
| 1828 | AffineExpr h = m.dim(i: 4); |
| 1829 | AffineExpr w = m.dim(i: 5); |
| 1830 | |
| 1831 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1832 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 1833 | .matchMaps( |
| 1834 | maps: {/*inputMap=*/{N, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1), C}, |
| 1835 | /*filterMap=*/{h, w}, |
| 1836 | /*outputMap=*/{N, H, W, C}}) |
| 1837 | .matchBody()) |
| 1838 | return result; |
| 1839 | return std::nullopt; |
| 1840 | } |
| 1841 | |
| 1842 | template <> |
| 1843 | std::optional<DilationsAndStrides> |
| 1844 | matchConvolutionOpOfType<linalg::PoolingNchwSumOp>(LinalgOp op) { |
| 1845 | DilationsAndStrides result; |
| 1846 | if (auto poolOp = dyn_cast<linalg::PoolingNchwSumOp>(Val: op.getOperation())) { |
| 1847 | result.dilations = |
| 1848 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1849 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1850 | return result; |
| 1851 | } |
| 1852 | |
| 1853 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1854 | return std::nullopt; |
| 1855 | |
| 1856 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1857 | &result.strides, PoolingType::Sum); |
| 1858 | AffineExpr N = m.dim(i: 0); |
| 1859 | AffineExpr C = m.dim(i: 1); |
| 1860 | AffineExpr H = m.dim(i: 2); |
| 1861 | AffineExpr W = m.dim(i: 3); |
| 1862 | AffineExpr h = m.dim(i: 4); |
| 1863 | AffineExpr w = m.dim(i: 5); |
| 1864 | |
| 1865 | if (m.matchStride(/*iDim=*/2, /*fDim=*/0, /*oDim=*/2, /*idx=*/0) |
| 1866 | .matchStride(/*iDim=*/3, /*fDim=*/1, /*oDim=*/3, /*idx=*/1) |
| 1867 | .matchMaps( |
| 1868 | maps: {/*inputMap=*/{N, C, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 1869 | /*filterMap=*/{h, w}, |
| 1870 | /*outputMap=*/{N, C, H, W}}) |
| 1871 | .matchBody()) |
| 1872 | return result; |
| 1873 | return std::nullopt; |
| 1874 | } |
| 1875 | |
| 1876 | template <> |
| 1877 | std::optional<DilationsAndStrides> |
| 1878 | matchConvolutionOpOfType<linalg::PoolingNchwMaxOp>(LinalgOp op) { |
| 1879 | DilationsAndStrides result; |
| 1880 | if (auto poolOp = dyn_cast<linalg::PoolingNchwMaxOp>(Val: op.getOperation())) { |
| 1881 | result.dilations = |
| 1882 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1883 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1884 | return result; |
| 1885 | } |
| 1886 | |
| 1887 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1888 | return std::nullopt; |
| 1889 | |
| 1890 | ConvMatcherBuilder m(op, /*spatialRank=*/2, &result.dilations, |
| 1891 | &result.strides, PoolingType::MaxSigned); |
| 1892 | AffineExpr N = m.dim(i: 0); |
| 1893 | AffineExpr C = m.dim(i: 1); |
| 1894 | AffineExpr H = m.dim(i: 2); |
| 1895 | AffineExpr W = m.dim(i: 3); |
| 1896 | AffineExpr h = m.dim(i: 4); |
| 1897 | AffineExpr w = m.dim(i: 5); |
| 1898 | |
| 1899 | if (m.matchStride(/*iDim=*/2, /*fDim=*/0, /*oDim=*/2, /*idx=*/0) |
| 1900 | .matchStride(/*iDim=*/3, /*fDim=*/1, /*oDim=*/3, /*idx=*/1) |
| 1901 | .matchMaps( |
| 1902 | maps: {/*inputMap=*/{N, C, m.strided(base: H, kernel: h, idx: 0), m.strided(base: W, kernel: w, idx: 1)}, |
| 1903 | /*filterMap=*/{h, w}, |
| 1904 | /*outputMap=*/{N, C, H, W}}) |
| 1905 | .matchBody()) |
| 1906 | return result; |
| 1907 | return std::nullopt; |
| 1908 | } |
| 1909 | |
| 1910 | template <> |
| 1911 | std::optional<DilationsAndStrides> |
| 1912 | matchConvolutionOpOfType<linalg::PoolingNwcSumOp>(LinalgOp op) { |
| 1913 | DilationsAndStrides result; |
| 1914 | if (auto poolOp = dyn_cast<linalg::PoolingNwcSumOp>(Val: op.getOperation())) { |
| 1915 | result.dilations = |
| 1916 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1917 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1918 | return result; |
| 1919 | } |
| 1920 | |
| 1921 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1922 | return std::nullopt; |
| 1923 | |
| 1924 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 1925 | &result.strides, PoolingType::Sum); |
| 1926 | AffineExpr N = m.dim(i: 0); |
| 1927 | AffineExpr W = m.dim(i: 1); |
| 1928 | AffineExpr C = m.dim(i: 2); |
| 1929 | AffineExpr w = m.dim(i: 3); |
| 1930 | |
| 1931 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1932 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 1933 | /*filterMap=*/{w}, |
| 1934 | /*outputMap=*/{N, W, C}}) |
| 1935 | .matchBody()) |
| 1936 | return result; |
| 1937 | return std::nullopt; |
| 1938 | } |
| 1939 | |
| 1940 | template <> |
| 1941 | std::optional<DilationsAndStrides> |
| 1942 | matchConvolutionOpOfType<linalg::PoolingNcwSumOp>(LinalgOp op) { |
| 1943 | DilationsAndStrides result; |
| 1944 | if (auto poolOp = dyn_cast<linalg::PoolingNcwSumOp>(Val: op.getOperation())) { |
| 1945 | result.dilations = |
| 1946 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1947 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1948 | return result; |
| 1949 | } |
| 1950 | |
| 1951 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1952 | return std::nullopt; |
| 1953 | |
| 1954 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 1955 | &result.strides, PoolingType::Sum); |
| 1956 | AffineExpr N = m.dim(i: 0); |
| 1957 | AffineExpr C = m.dim(i: 1); |
| 1958 | AffineExpr W = m.dim(i: 2); |
| 1959 | AffineExpr w = m.dim(i: 3); |
| 1960 | |
| 1961 | if (m.matchStride(/*iDim=*/2, /*fDim=*/0, /*oDim=*/2, /*idx=*/0) |
| 1962 | .matchMaps(maps: {/*inputMap=*/{N, C, m.strided(base: W, kernel: w, idx: 0)}, |
| 1963 | /*filterMap=*/{w}, |
| 1964 | /*outputMap=*/{N, C, W}}) |
| 1965 | .matchBody()) |
| 1966 | return result; |
| 1967 | return std::nullopt; |
| 1968 | } |
| 1969 | |
| 1970 | template <> |
| 1971 | std::optional<DilationsAndStrides> |
| 1972 | matchConvolutionOpOfType<linalg::PoolingNwcMaxOp>(LinalgOp op) { |
| 1973 | DilationsAndStrides result; |
| 1974 | if (auto poolOp = dyn_cast<linalg::PoolingNwcMaxOp>(Val: op.getOperation())) { |
| 1975 | result.dilations = |
| 1976 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 1977 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 1978 | return result; |
| 1979 | } |
| 1980 | |
| 1981 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 1982 | return std::nullopt; |
| 1983 | |
| 1984 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 1985 | &result.strides, PoolingType::MaxSigned); |
| 1986 | AffineExpr N = m.dim(i: 0); |
| 1987 | AffineExpr W = m.dim(i: 1); |
| 1988 | AffineExpr C = m.dim(i: 2); |
| 1989 | AffineExpr w = m.dim(i: 3); |
| 1990 | |
| 1991 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 1992 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 1993 | /*filterMap=*/{w}, |
| 1994 | /*outputMap=*/{N, W, C}}) |
| 1995 | .matchBody()) |
| 1996 | return result; |
| 1997 | return std::nullopt; |
| 1998 | } |
| 1999 | |
| 2000 | template <> |
| 2001 | std::optional<DilationsAndStrides> |
| 2002 | matchConvolutionOpOfType<linalg::PoolingNwcMaxUnsignedOp>(LinalgOp op) { |
| 2003 | DilationsAndStrides result; |
| 2004 | if (auto poolOp = |
| 2005 | dyn_cast<linalg::PoolingNwcMaxUnsignedOp>(Val: op.getOperation())) { |
| 2006 | result.dilations = |
| 2007 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2008 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2009 | return result; |
| 2010 | } |
| 2011 | |
| 2012 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2013 | return std::nullopt; |
| 2014 | |
| 2015 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 2016 | &result.strides, PoolingType::MaxUnsigned); |
| 2017 | AffineExpr N = m.dim(i: 0); |
| 2018 | AffineExpr W = m.dim(i: 1); |
| 2019 | AffineExpr C = m.dim(i: 2); |
| 2020 | AffineExpr w = m.dim(i: 3); |
| 2021 | |
| 2022 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 2023 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 2024 | /*filterMap=*/{w}, |
| 2025 | /*outputMap=*/{N, W, C}}) |
| 2026 | .matchBody()) |
| 2027 | return result; |
| 2028 | return std::nullopt; |
| 2029 | } |
| 2030 | |
| 2031 | template <> |
| 2032 | std::optional<DilationsAndStrides> |
| 2033 | matchConvolutionOpOfType<linalg::PoolingNcwMaxOp>(LinalgOp op) { |
| 2034 | DilationsAndStrides result; |
| 2035 | if (auto poolOp = dyn_cast<linalg::PoolingNcwMaxOp>(Val: op.getOperation())) { |
| 2036 | result.dilations = |
| 2037 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2038 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2039 | return result; |
| 2040 | } |
| 2041 | |
| 2042 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2043 | return std::nullopt; |
| 2044 | |
| 2045 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 2046 | &result.strides, PoolingType::MaxSigned); |
| 2047 | AffineExpr N = m.dim(i: 0); |
| 2048 | AffineExpr C = m.dim(i: 1); |
| 2049 | AffineExpr W = m.dim(i: 2); |
| 2050 | AffineExpr w = m.dim(i: 3); |
| 2051 | |
| 2052 | if (m.matchStride(/*iDim=*/2, /*fDim=*/0, /*oDim=*/2, /*idx=*/0) |
| 2053 | .matchMaps(maps: {/*inputMap=*/{N, C, m.strided(base: W, kernel: w, idx: 0)}, |
| 2054 | /*filterMap=*/{w}, |
| 2055 | /*outputMap=*/{N, C, W}}) |
| 2056 | .matchBody()) |
| 2057 | return result; |
| 2058 | return std::nullopt; |
| 2059 | } |
| 2060 | |
| 2061 | template <> |
| 2062 | std::optional<DilationsAndStrides> |
| 2063 | matchConvolutionOpOfType<linalg::PoolingNwcMinOp>(LinalgOp op) { |
| 2064 | DilationsAndStrides result; |
| 2065 | if (auto poolOp = dyn_cast<linalg::PoolingNwcMinOp>(Val: op.getOperation())) { |
| 2066 | result.dilations = |
| 2067 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2068 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2069 | return result; |
| 2070 | } |
| 2071 | |
| 2072 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2073 | return std::nullopt; |
| 2074 | |
| 2075 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 2076 | &result.strides, PoolingType::MinSigned); |
| 2077 | AffineExpr N = m.dim(i: 0); |
| 2078 | AffineExpr W = m.dim(i: 1); |
| 2079 | AffineExpr C = m.dim(i: 2); |
| 2080 | AffineExpr w = m.dim(i: 3); |
| 2081 | |
| 2082 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 2083 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 2084 | /*filterMap=*/{w}, |
| 2085 | /*outputMap=*/{N, W, C}}) |
| 2086 | .matchBody()) |
| 2087 | return result; |
| 2088 | return std::nullopt; |
| 2089 | } |
| 2090 | |
| 2091 | template <> |
| 2092 | std::optional<DilationsAndStrides> |
| 2093 | matchConvolutionOpOfType<linalg::PoolingNwcMinUnsignedOp>(LinalgOp op) { |
| 2094 | DilationsAndStrides result; |
| 2095 | if (auto poolOp = |
| 2096 | dyn_cast<linalg::PoolingNwcMinUnsignedOp>(Val: op.getOperation())) { |
| 2097 | result.dilations = |
| 2098 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2099 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2100 | return result; |
| 2101 | } |
| 2102 | |
| 2103 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2104 | return std::nullopt; |
| 2105 | |
| 2106 | ConvMatcherBuilder m(op, /*spatialRank=*/1, &result.dilations, |
| 2107 | &result.strides, PoolingType::MinUnsigned); |
| 2108 | AffineExpr N = m.dim(i: 0); |
| 2109 | AffineExpr W = m.dim(i: 1); |
| 2110 | AffineExpr C = m.dim(i: 2); |
| 2111 | AffineExpr w = m.dim(i: 3); |
| 2112 | |
| 2113 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 2114 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: W, kernel: w, idx: 0), C}, |
| 2115 | /*filterMap=*/{w}, |
| 2116 | /*outputMap=*/{N, W, C}}) |
| 2117 | .matchBody()) |
| 2118 | return result; |
| 2119 | return std::nullopt; |
| 2120 | } |
| 2121 | |
| 2122 | template <> |
| 2123 | std::optional<DilationsAndStrides> |
| 2124 | matchConvolutionOpOfType<linalg::PoolingNdhwcSumOp>(LinalgOp op) { |
| 2125 | DilationsAndStrides result; |
| 2126 | if (auto poolOp = dyn_cast<linalg::PoolingNdhwcSumOp>(Val: op.getOperation())) { |
| 2127 | result.dilations = |
| 2128 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2129 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2130 | return result; |
| 2131 | } |
| 2132 | |
| 2133 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2134 | return std::nullopt; |
| 2135 | |
| 2136 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 2137 | &result.strides, PoolingType::Sum); |
| 2138 | AffineExpr N = m.dim(i: 0); |
| 2139 | AffineExpr D = m.dim(i: 1); |
| 2140 | AffineExpr H = m.dim(i: 2); |
| 2141 | AffineExpr W = m.dim(i: 3); |
| 2142 | AffineExpr C = m.dim(i: 4); |
| 2143 | AffineExpr d = m.dim(i: 5); |
| 2144 | AffineExpr h = m.dim(i: 6); |
| 2145 | AffineExpr w = m.dim(i: 7); |
| 2146 | |
| 2147 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 2148 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 2149 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 2150 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 2151 | m.strided(base: W, kernel: w, idx: 2), C}, |
| 2152 | /*filterMap=*/{d, h, w}, |
| 2153 | /*outputMap=*/{N, D, H, W, C}}) |
| 2154 | .matchBody()) |
| 2155 | return result; |
| 2156 | return std::nullopt; |
| 2157 | } |
| 2158 | |
| 2159 | template <> |
| 2160 | std::optional<DilationsAndStrides> |
| 2161 | matchConvolutionOpOfType<linalg::PoolingNdhwcMaxOp>(LinalgOp op) { |
| 2162 | DilationsAndStrides result; |
| 2163 | if (auto poolOp = dyn_cast<linalg::PoolingNdhwcMaxOp>(Val: op.getOperation())) { |
| 2164 | result.dilations = |
| 2165 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2166 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2167 | return result; |
| 2168 | } |
| 2169 | |
| 2170 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2171 | return std::nullopt; |
| 2172 | |
| 2173 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 2174 | &result.strides, PoolingType::MaxSigned); |
| 2175 | AffineExpr N = m.dim(i: 0); |
| 2176 | AffineExpr D = m.dim(i: 1); |
| 2177 | AffineExpr H = m.dim(i: 2); |
| 2178 | AffineExpr W = m.dim(i: 3); |
| 2179 | AffineExpr C = m.dim(i: 4); |
| 2180 | AffineExpr d = m.dim(i: 5); |
| 2181 | AffineExpr h = m.dim(i: 6); |
| 2182 | AffineExpr w = m.dim(i: 7); |
| 2183 | |
| 2184 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 2185 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 2186 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 2187 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 2188 | m.strided(base: W, kernel: w, idx: 2), C}, |
| 2189 | /*filterMap=*/{d, h, w}, |
| 2190 | /*outputMap=*/{N, D, H, W, C}}) |
| 2191 | .matchBody()) |
| 2192 | return result; |
| 2193 | return std::nullopt; |
| 2194 | } |
| 2195 | |
| 2196 | template <> |
| 2197 | std::optional<DilationsAndStrides> |
| 2198 | matchConvolutionOpOfType<linalg::PoolingNdhwcMinOp>(LinalgOp op) { |
| 2199 | DilationsAndStrides result; |
| 2200 | if (auto poolOp = dyn_cast<linalg::PoolingNdhwcMinOp>(Val: op.getOperation())) { |
| 2201 | result.dilations = |
| 2202 | llvm::to_vector(Range: poolOp.getDilations().getValues<int64_t>()); |
| 2203 | result.strides = llvm::to_vector(Range: poolOp.getStrides().getValues<int64_t>()); |
| 2204 | return result; |
| 2205 | } |
| 2206 | |
| 2207 | if (!isaConvolutionOpInterface(linalgOp: op)) |
| 2208 | return std::nullopt; |
| 2209 | |
| 2210 | ConvMatcherBuilder m(op, /*spatialRank=*/3, &result.dilations, |
| 2211 | &result.strides, PoolingType::MinSigned); |
| 2212 | AffineExpr N = m.dim(i: 0); |
| 2213 | AffineExpr D = m.dim(i: 1); |
| 2214 | AffineExpr H = m.dim(i: 2); |
| 2215 | AffineExpr W = m.dim(i: 3); |
| 2216 | AffineExpr C = m.dim(i: 4); |
| 2217 | AffineExpr d = m.dim(i: 5); |
| 2218 | AffineExpr h = m.dim(i: 6); |
| 2219 | AffineExpr w = m.dim(i: 7); |
| 2220 | |
| 2221 | if (m.matchStride(/*iDim=*/1, /*fDim=*/0, /*oDim=*/1, /*idx=*/0) |
| 2222 | .matchStride(/*iDim=*/2, /*fDim=*/1, /*oDim=*/2, /*idx=*/1) |
| 2223 | .matchStride(/*iDim=*/3, /*fDim=*/2, /*oDim=*/3, /*idx=*/2) |
| 2224 | .matchMaps(maps: {/*inputMap=*/{N, m.strided(base: D, kernel: d, idx: 0), m.strided(base: H, kernel: h, idx: 1), |
| 2225 | m.strided(base: W, kernel: w, idx: 2), C}, |
| 2226 | /*filterMap=*/{d, h, w}, |
| 2227 | /*outputMap=*/{N, D, H, W, C}}) |
| 2228 | .matchBody()) |
| 2229 | return result; |
| 2230 | return std::nullopt; |
| 2231 | } |
| 2232 | |
| 2233 | Value makeComposedPadHighOp(OpBuilder &b, Location loc, RankedTensorType type, |
| 2234 | Value source, Value pad, bool nofold, |
| 2235 | ValueRange typeDynDims) { |
| 2236 | // Exit if `source` is not defined by an ExtractSliceOp. |
| 2237 | auto sliceOp = source.getDefiningOp<tensor::ExtractSliceOp>(); |
| 2238 | if (!sliceOp) |
| 2239 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2240 | dynOutDims: typeDynDims); |
| 2241 | |
| 2242 | // Search the `source` use-def chain for padded LinalgOps. |
| 2243 | Value current = sliceOp.getSource(); |
| 2244 | while (current) { |
| 2245 | auto linalgOp = current.getDefiningOp<LinalgOp>(); |
| 2246 | if (!linalgOp) |
| 2247 | break; |
| 2248 | OpResult opResult = cast<OpResult>(Val&: current); |
| 2249 | current = linalgOp.getDpsInitOperand(i: opResult.getResultNumber())->get(); |
| 2250 | } |
| 2251 | auto padOp = current ? current.getDefiningOp<tensor::PadOp>() : nullptr; |
| 2252 | |
| 2253 | // Exit if the search fails to match a tensor::PadOp at the end of the matched |
| 2254 | // LinalgOp sequence. |
| 2255 | if (!padOp) |
| 2256 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2257 | dynOutDims: typeDynDims); |
| 2258 | |
| 2259 | // Exit if the padded result type does not match. |
| 2260 | if (sliceOp.getSource().getType() != type) |
| 2261 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2262 | dynOutDims: typeDynDims); |
| 2263 | |
| 2264 | // Exit if the LinalgOps are not high padded. |
| 2265 | if (llvm::any_of(Range: padOp.getMixedLowPad(), P: [](OpFoldResult ofr) { |
| 2266 | return getConstantIntValue(ofr) != static_cast<int64_t>(0); |
| 2267 | })) |
| 2268 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2269 | dynOutDims: typeDynDims); |
| 2270 | |
| 2271 | // Exit if `padOpSliceOp`, which defines the slice used by |
| 2272 | // `padOp`, is rank-reducing. |
| 2273 | auto padOpSliceOp = padOp.getSource().getDefiningOp<tensor::ExtractSliceOp>(); |
| 2274 | if (!padOpSliceOp || |
| 2275 | sliceOp.getMixedSizes().size() != padOpSliceOp.getMixedSizes().size()) |
| 2276 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2277 | dynOutDims: typeDynDims); |
| 2278 | |
| 2279 | // Exit if the sizes of the dynamic sizes of `sliceOp` do not match the size |
| 2280 | // of the slice padded by `padOp`. |
| 2281 | if (llvm::any_of( |
| 2282 | Range: llvm::zip(t: sliceOp.getMixedSizes(), u: padOpSliceOp.getMixedSizes()), |
| 2283 | P: [](std::tuple<OpFoldResult, OpFoldResult> it) { |
| 2284 | return !isEqualConstantIntOrValue(ofr1: std::get<0>(t&: it), ofr2: std::get<1>(t&: it)); |
| 2285 | })) |
| 2286 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2287 | dynOutDims: typeDynDims); |
| 2288 | |
| 2289 | // Exit if the padding values do not match. |
| 2290 | Attribute padOpPadAttr, padAttr; |
| 2291 | Value padOpPad = padOp.getConstantPaddingValue(); |
| 2292 | if (!padOpPad || !matchPattern(value: padOpPad, pattern: m_Constant(bind_value: &padOpPadAttr)) || |
| 2293 | !matchPattern(value: pad, pattern: m_Constant(bind_value: &padAttr)) || padOpPadAttr != padAttr) |
| 2294 | return tensor::createPadHighOp(resType: type, source, pad, nofold, loc, builder&: b, |
| 2295 | dynOutDims: typeDynDims); |
| 2296 | |
| 2297 | // Return the padded result if the padding values and sizes match. |
| 2298 | return sliceOp.getSource(); |
| 2299 | } |
| 2300 | |
| 2301 | GenericOp makeMemRefCopyOp(OpBuilder &b, Location loc, Value from, Value to) { |
| 2302 | auto memrefTypeTo = cast<MemRefType>(Val: to.getType()); |
| 2303 | #ifndef NDEBUG |
| 2304 | auto memrefTypeFrom = cast<MemRefType>(from.getType()); |
| 2305 | assert(memrefTypeFrom.getRank() == memrefTypeTo.getRank() && |
| 2306 | "`from` and `to` memref must have the same rank" ); |
| 2307 | #endif // NDEBUG |
| 2308 | |
| 2309 | AffineMap id = |
| 2310 | AffineMap::getMultiDimIdentityMap(numDims: memrefTypeTo.getRank(), context: b.getContext()); |
| 2311 | SmallVector<utils::IteratorType> iteratorTypes(memrefTypeTo.getRank(), |
| 2312 | utils::IteratorType::parallel); |
| 2313 | return linalg::GenericOp::create( |
| 2314 | builder&: b, location: loc, |
| 2315 | /*inputs=*/from, |
| 2316 | /*outputs=*/outputBuffers: to, |
| 2317 | /*indexingMaps=*/llvm::ArrayRef({id, id}), |
| 2318 | /*iteratorTypes=*/iteratorTypes, |
| 2319 | odsArg4: [](OpBuilder &b, Location loc, ValueRange args) { |
| 2320 | linalg::YieldOp::create(builder&: b, location: loc, values: args.front()); |
| 2321 | }); |
| 2322 | } |
| 2323 | |
| 2324 | /// Specialization to build an scf "for" nest. |
| 2325 | template <> |
| 2326 | void GenerateLoopNest<scf::ForOp>::doit( |
| 2327 | OpBuilder &b, Location loc, ArrayRef<Range> loopRanges, LinalgOp linalgOp, |
| 2328 | ArrayRef<utils::IteratorType> iteratorTypes, |
| 2329 | function_ref<scf::ValueVector(OpBuilder &, Location, ValueRange, |
| 2330 | ValueRange)> |
| 2331 | bodyBuilderFn, |
| 2332 | ArrayRef<linalg::ProcInfo> procInfo) { |
| 2333 | assert((procInfo.empty() || (procInfo.size() == loopRanges.size())) && |
| 2334 | "expected as many entries for proc info as number of loops, even if " |
| 2335 | "they are null entries" ); |
| 2336 | SmallVector<Value> iterArgInitValues; |
| 2337 | if (!linalgOp.hasPureBufferSemantics()) |
| 2338 | llvm::append_range(C&: iterArgInitValues, R: linalgOp.getDpsInits()); |
| 2339 | SmallVector<Value, 4> lbs, ubs, steps; |
| 2340 | unpackRanges(builder&: b, loc, ranges: loopRanges, lbs, ubs, steps); |
| 2341 | LoopNest loopNest = mlir::scf::buildLoopNest( |
| 2342 | builder&: b, loc, lbs, ubs, steps, iterArgs: iterArgInitValues, |
| 2343 | bodyBuilder: [&](OpBuilder &b, Location loc, ValueRange ivs, ValueRange iterArgs) { |
| 2344 | assert(iterArgs.size() == iterArgInitValues.size() && |
| 2345 | "expect the number of output tensors and iter args to match" ); |
| 2346 | SmallVector<Value> operandValuesToUse = linalgOp->getOperands(); |
| 2347 | if (!iterArgs.empty()) { |
| 2348 | operandValuesToUse = linalgOp.getDpsInputs(); |
| 2349 | operandValuesToUse.append(in_start: iterArgs.begin(), in_end: iterArgs.end()); |
| 2350 | } |
| 2351 | return bodyBuilderFn(b, loc, ivs, operandValuesToUse); |
| 2352 | }); |
| 2353 | |
| 2354 | if (loopNest.loops.empty() || procInfo.empty()) |
| 2355 | return; |
| 2356 | |
| 2357 | // Filter out scf.for loops that were created out of parallel dimensions. |
| 2358 | for (const auto &loop : llvm::enumerate(First&: loopNest.loops)) { |
| 2359 | if (procInfo[loop.index()].distributionMethod == |
| 2360 | DistributionMethod::Cyclic) { |
| 2361 | mapLoopToProcessorIds(forOp: loop.value(), processorId: procInfo[loop.index()].procId, |
| 2362 | numProcessors: procInfo[loop.index()].nprocs); |
| 2363 | } |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | /// Specialization to build affine "for" nest. |
| 2368 | template <> |
| 2369 | void GenerateLoopNest<AffineForOp>::doit( |
| 2370 | OpBuilder &b, Location loc, ArrayRef<Range> loopRanges, LinalgOp linalgOp, |
| 2371 | ArrayRef<utils::IteratorType> iteratorTypes, |
| 2372 | function_ref<scf::ValueVector(OpBuilder &, Location, ValueRange, |
| 2373 | ValueRange)> |
| 2374 | bodyBuilderFn, |
| 2375 | ArrayRef<linalg::ProcInfo> /*procInfo*/) { |
| 2376 | SmallVector<Value> iterArgInitValues; |
| 2377 | if (!linalgOp.hasPureBufferSemantics()) |
| 2378 | llvm::append_range(C&: iterArgInitValues, R: linalgOp.getDpsInits()); |
| 2379 | assert(iterArgInitValues.empty() && "unexpected AffineForOp init values" ); |
| 2380 | SmallVector<Value, 4> lbs, ubs, steps; |
| 2381 | unpackRanges(builder&: b, loc, ranges: loopRanges, lbs, ubs, steps); |
| 2382 | |
| 2383 | // Affine loops require constant steps. |
| 2384 | SmallVector<int64_t, 4> constantSteps; |
| 2385 | constantSteps.reserve(N: steps.size()); |
| 2386 | for (Value v : steps) { |
| 2387 | auto constVal = getConstantIntValue(ofr: v); |
| 2388 | assert(constVal.has_value() && "Affine loops require constant steps" ); |
| 2389 | constantSteps.push_back(Elt: constVal.value()); |
| 2390 | } |
| 2391 | |
| 2392 | affine::buildAffineLoopNest(builder&: b, loc, lbs, ubs, steps: constantSteps, |
| 2393 | bodyBuilderFn: [&](OpBuilder &b, Location loc, ValueRange ivs) { |
| 2394 | bodyBuilderFn(b, loc, ivs, |
| 2395 | linalgOp->getOperands()); |
| 2396 | }); |
| 2397 | } |
| 2398 | |
| 2399 | /// Update the `lb`, `ub` and `step` to get per processor `lb`, `ub` and `step`. |
| 2400 | void updateBoundsForCyclicDistribution(OpBuilder &b, Location loc, Value procId, |
| 2401 | Value nprocs, Value &lb, Value &ub, |
| 2402 | Value &step) { |
| 2403 | AffineExpr d0, d1; |
| 2404 | bindDims(ctx: b.getContext(), exprs&: d0, exprs&: d1); |
| 2405 | AffineExpr s0 = getAffineSymbolExpr(position: 0, context: b.getContext()); |
| 2406 | lb = |
| 2407 | affine::makeComposedAffineApply(b, loc, e: d0 + d1 * s0, operands: {lb, procId, step}); |
| 2408 | step = affine::makeComposedAffineApply(b, loc, e: d0 * s0, operands: {nprocs, step}); |
| 2409 | } |
| 2410 | |
| 2411 | /// Generates a loop nest consisting of scf.parallel and scf.for, depending |
| 2412 | /// on the `iteratorTypes.` Consecutive parallel loops create a single |
| 2413 | /// scf.parallel operation; each sequential loop creates a new scf.for |
| 2414 | /// operation. The body of the innermost loop is populated by |
| 2415 | /// `bodyBuilderFn` that accepts a range of induction variables for all |
| 2416 | /// loops. `ivStorage` is used to store the partial list of induction |
| 2417 | /// variables. |
| 2418 | // TODO: this function can be made iterative instead. However, it |
| 2419 | // will have at most as many recursive calls as nested loops, which rarely |
| 2420 | // exceeds 10. |
| 2421 | static void generateParallelLoopNest( |
| 2422 | OpBuilder &b, Location loc, ValueRange lbs, ValueRange ubs, |
| 2423 | ValueRange steps, ArrayRef<utils::IteratorType> iteratorTypes, |
| 2424 | ArrayRef<linalg::ProcInfo> procInfo, |
| 2425 | function_ref<void(OpBuilder &, Location, ValueRange)> bodyBuilderFn, |
| 2426 | SmallVectorImpl<Value> &ivStorage) { |
| 2427 | assert(lbs.size() == ubs.size()); |
| 2428 | assert(lbs.size() == steps.size()); |
| 2429 | assert(lbs.size() == iteratorTypes.size()); |
| 2430 | assert(procInfo.empty() || (lbs.size() == procInfo.size())); |
| 2431 | |
| 2432 | // If there are no (more) loops to be generated, generate the body and be |
| 2433 | // done with it. |
| 2434 | if (iteratorTypes.empty()) { |
| 2435 | bodyBuilderFn(b, loc, ivStorage); |
| 2436 | return; |
| 2437 | } |
| 2438 | |
| 2439 | // If there are no outer parallel loops, generate one sequential loop and |
| 2440 | // recurse. |
| 2441 | if (!isParallelIterator(iteratorType: iteratorTypes.front())) { |
| 2442 | LoopNest singleLoop = buildLoopNest( |
| 2443 | builder&: b, loc, lbs: lbs.take_front(), ubs: ubs.take_front(), steps: steps.take_front(), |
| 2444 | bodyBuilder: [&](OpBuilder &b, Location loc, ValueRange ivs) { |
| 2445 | ivStorage.append(in_start: ivs.begin(), in_end: ivs.end()); |
| 2446 | generateParallelLoopNest( |
| 2447 | b, loc, lbs: lbs.drop_front(), ubs: ubs.drop_front(), steps: steps.drop_front(), |
| 2448 | iteratorTypes: iteratorTypes.drop_front(), |
| 2449 | procInfo: procInfo.empty() ? procInfo : procInfo.drop_front(), |
| 2450 | bodyBuilderFn, ivStorage); |
| 2451 | }); |
| 2452 | return; |
| 2453 | } |
| 2454 | |
| 2455 | unsigned nLoops = iteratorTypes.size(); |
| 2456 | unsigned numProcessed = 0; |
| 2457 | DistributionMethod distributionMethod = DistributionMethod::None; |
| 2458 | if (procInfo.empty()) { |
| 2459 | numProcessed = nLoops - iteratorTypes.drop_while(Pred: isParallelIterator).size(); |
| 2460 | } else { |
| 2461 | distributionMethod = procInfo.front().distributionMethod; |
| 2462 | numProcessed = |
| 2463 | nLoops - procInfo |
| 2464 | .drop_while(Pred: [&](linalg::ProcInfo p) { |
| 2465 | return p.distributionMethod == distributionMethod; |
| 2466 | }) |
| 2467 | .size(); |
| 2468 | } |
| 2469 | |
| 2470 | auto remainderProcInfo = |
| 2471 | procInfo.empty() ? procInfo : procInfo.drop_front(N: numProcessed); |
| 2472 | switch (distributionMethod) { |
| 2473 | case DistributionMethod::None: { |
| 2474 | // Generate a single parallel loop-nest operation for all outermost |
| 2475 | // parallel loops and recurse. |
| 2476 | scf::ParallelOp::create( |
| 2477 | builder&: b, location: loc, lowerBounds: lbs.take_front(n: numProcessed), upperBounds: ubs.take_front(n: numProcessed), |
| 2478 | steps: steps.take_front(n: numProcessed), |
| 2479 | bodyBuilderFn: [&](OpBuilder &nestedBuilder, Location nestedLoc, ValueRange localIvs) { |
| 2480 | ivStorage.append(in_start: localIvs.begin(), in_end: localIvs.end()); |
| 2481 | generateParallelLoopNest( |
| 2482 | b&: nestedBuilder, loc: nestedLoc, lbs: lbs.drop_front(n: numProcessed), |
| 2483 | ubs: ubs.drop_front(n: numProcessed), steps: steps.drop_front(n: numProcessed), |
| 2484 | iteratorTypes: iteratorTypes.drop_front(N: numProcessed), procInfo: remainderProcInfo, |
| 2485 | bodyBuilderFn, ivStorage); |
| 2486 | }); |
| 2487 | return; |
| 2488 | } |
| 2489 | case DistributionMethod::Cyclic: { |
| 2490 | // Generate a single parallel loop-nest operation for all outermost |
| 2491 | // parallel loops and recurse. |
| 2492 | scf::ParallelOp::create( |
| 2493 | builder&: b, location: loc, lowerBounds: lbs.take_front(n: numProcessed), upperBounds: ubs.take_front(n: numProcessed), |
| 2494 | steps: steps.take_front(n: numProcessed), |
| 2495 | bodyBuilderFn: [&](OpBuilder &nestedBuilder, Location nestedLoc, ValueRange localIvs) { |
| 2496 | ivStorage.append(in_start: localIvs.begin(), in_end: localIvs.end()); |
| 2497 | generateParallelLoopNest( |
| 2498 | b&: nestedBuilder, loc: nestedLoc, lbs: lbs.drop_front(n: numProcessed), |
| 2499 | ubs: ubs.drop_front(n: numProcessed), steps: steps.drop_front(n: numProcessed), |
| 2500 | iteratorTypes: iteratorTypes.drop_front(N: numProcessed), procInfo: remainderProcInfo, |
| 2501 | bodyBuilderFn, ivStorage); |
| 2502 | }); |
| 2503 | return; |
| 2504 | } |
| 2505 | case DistributionMethod::CyclicNumProcsGeNumIters: { |
| 2506 | // Check (for the processed loops) that the iteration is in-bounds. |
| 2507 | ArithBuilder ab(b, loc); |
| 2508 | Value cond = ab.slt(lhs: lbs[0], rhs: ubs[0]); |
| 2509 | for (unsigned i = 1; i < numProcessed; ++i) |
| 2510 | cond = ab._and(lhs: cond, rhs: ab.slt(lhs: lbs[i], rhs: ubs[i])); |
| 2511 | ivStorage.append(in_start: lbs.begin(), in_end: std::next(x: lbs.begin(), n: numProcessed)); |
| 2512 | scf::IfOp::create(builder&: b, location: loc, cond, thenBuilder: [&](OpBuilder &b, Location loc) { |
| 2513 | generateParallelLoopNest(b, loc, lbs: lbs.drop_front(n: numProcessed), |
| 2514 | ubs: ubs.drop_front(n: numProcessed), |
| 2515 | steps: steps.drop_front(n: numProcessed), |
| 2516 | iteratorTypes: iteratorTypes.drop_front(N: numProcessed), |
| 2517 | procInfo: remainderProcInfo, bodyBuilderFn, ivStorage); |
| 2518 | scf::YieldOp::create(builder&: b, location: loc, results: ValueRange{}); |
| 2519 | }); |
| 2520 | return; |
| 2521 | } |
| 2522 | case DistributionMethod::CyclicNumProcsEqNumIters: |
| 2523 | // No check/loops needed here. Set the `%iv` to be the `%lb` and proceed |
| 2524 | // with inner loop generation. |
| 2525 | ivStorage.append(in_start: lbs.begin(), in_end: std::next(x: lbs.begin(), n: numProcessed)); |
| 2526 | generateParallelLoopNest( |
| 2527 | b, loc, lbs: lbs.drop_front(n: numProcessed), ubs: ubs.drop_front(n: numProcessed), |
| 2528 | steps: steps.drop_front(n: numProcessed), iteratorTypes: iteratorTypes.drop_front(N: numProcessed), |
| 2529 | procInfo: remainderProcInfo, bodyBuilderFn, ivStorage); |
| 2530 | return; |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | /// Specialization for generating a mix of parallel and sequential scf loops. |
| 2535 | template <> |
| 2536 | void GenerateLoopNest<scf::ParallelOp>::doit( |
| 2537 | OpBuilder &b, Location loc, ArrayRef<Range> loopRanges, LinalgOp linalgOp, |
| 2538 | ArrayRef<utils::IteratorType> iteratorTypes, |
| 2539 | function_ref<scf::ValueVector(OpBuilder &, Location, ValueRange, |
| 2540 | ValueRange)> |
| 2541 | bodyBuilderFn, |
| 2542 | ArrayRef<linalg::ProcInfo> procInfo) { |
| 2543 | SmallVector<Value> iterArgInitValues; |
| 2544 | if (!linalgOp.hasPureBufferSemantics()) |
| 2545 | llvm::append_range(C&: iterArgInitValues, R: linalgOp.getDpsInits()); |
| 2546 | assert(iterArgInitValues.empty() && "unexpected ParallelOp init values" ); |
| 2547 | // This function may be passed more iterator types than ranges. |
| 2548 | assert(iteratorTypes.size() >= loopRanges.size() && |
| 2549 | "expected iterator type for all ranges" ); |
| 2550 | assert((procInfo.empty() || (procInfo.size() == loopRanges.size())) && |
| 2551 | "expected proc information for all loops when present" ); |
| 2552 | iteratorTypes = iteratorTypes.take_front(N: loopRanges.size()); |
| 2553 | SmallVector<Value, 8> lbsStorage, ubsStorage, stepsStorage, ivs; |
| 2554 | unsigned numLoops = iteratorTypes.size(); |
| 2555 | ivs.reserve(N: numLoops); |
| 2556 | lbsStorage.reserve(N: numLoops); |
| 2557 | ubsStorage.reserve(N: numLoops); |
| 2558 | stepsStorage.reserve(N: numLoops); |
| 2559 | |
| 2560 | // Get the loop lb, ub, and step. |
| 2561 | unpackRanges(builder&: b, loc, ranges: loopRanges, lbs&: lbsStorage, ubs&: ubsStorage, steps&: stepsStorage); |
| 2562 | |
| 2563 | // Modify the lb, ub, and step based on the distribution options. |
| 2564 | for (const auto &it : llvm::enumerate(First&: procInfo)) { |
| 2565 | if (it.value().distributionMethod != linalg::DistributionMethod::None) { |
| 2566 | updateBoundsForCyclicDistribution( |
| 2567 | b, loc, procId: it.value().procId, nprocs: it.value().nprocs, lb&: lbsStorage[it.index()], |
| 2568 | ub&: ubsStorage[it.index()], step&: stepsStorage[it.index()]); |
| 2569 | } |
| 2570 | } |
| 2571 | ValueRange lbs(lbsStorage), ubs(ubsStorage), steps(stepsStorage); |
| 2572 | generateParallelLoopNest( |
| 2573 | b, loc, lbs, ubs, steps, iteratorTypes, procInfo, |
| 2574 | bodyBuilderFn: [&](OpBuilder &b, Location loc, ValueRange ivs) { |
| 2575 | bodyBuilderFn(b, loc, ivs, linalgOp->getOperands()); |
| 2576 | }, |
| 2577 | ivStorage&: ivs); |
| 2578 | |
| 2579 | assert(ivs.size() == iteratorTypes.size() && "did not generate enough loops" ); |
| 2580 | } |
| 2581 | |
| 2582 | static Operation *materializeTiledShape(OpBuilder &builder, Location loc, |
| 2583 | Value valueToTile, |
| 2584 | const SliceParameters &sliceParams) { |
| 2585 | auto shapedType = dyn_cast<ShapedType>(Val: valueToTile.getType()); |
| 2586 | auto *sliceOp = TypeSwitch<ShapedType, Operation *>(shapedType) |
| 2587 | .Case(caseFn: [&](MemRefType) { |
| 2588 | return memref::SubViewOp::create( |
| 2589 | builder, location: loc, source: valueToTile, offsets: sliceParams.offsets, |
| 2590 | sizes: sliceParams.sizes, strides: sliceParams.strides); |
| 2591 | }) |
| 2592 | .Case(caseFn: [&](RankedTensorType) { |
| 2593 | return tensor::ExtractSliceOp::create( |
| 2594 | builder, location: loc, source: valueToTile, offsets: sliceParams.offsets, |
| 2595 | sizes: sliceParams.sizes, strides: sliceParams.strides); |
| 2596 | }) |
| 2597 | .DefaultUnreachable(message: "Unexpected shaped type" ); |
| 2598 | return sliceOp; |
| 2599 | } |
| 2600 | |
| 2601 | Operation *makeTiledShape(OpBuilder &builder, Location loc, Value valueToTile, |
| 2602 | ArrayRef<OpFoldResult> tileSizes, AffineMap map, |
| 2603 | ArrayRef<OpFoldResult> lbs, |
| 2604 | ArrayRef<OpFoldResult> ubs, |
| 2605 | ArrayRef<OpFoldResult> subShapeSizes, |
| 2606 | bool omitPartialTileCheck) { |
| 2607 | SliceParameters sliceParams = |
| 2608 | computeSliceParameters(builder, loc, valueToTile, tileSizes, map, lbs, |
| 2609 | ubs, subShapeSizes, omitPartialTileCheck); |
| 2610 | return materializeTiledShape(builder, loc, valueToTile, sliceParams); |
| 2611 | } |
| 2612 | |
| 2613 | SliceParameters |
| 2614 | computeSliceParameters(OpBuilder &builder, Location loc, Value valueToTile, |
| 2615 | ArrayRef<OpFoldResult> tileSizes, AffineMap map, |
| 2616 | ArrayRef<OpFoldResult> lbs, ArrayRef<OpFoldResult> ubs, |
| 2617 | ArrayRef<OpFoldResult> subShapeSizes, |
| 2618 | bool omitPartialTileCheck) { |
| 2619 | auto shapedType = dyn_cast<ShapedType>(Val: valueToTile.getType()); |
| 2620 | assert(shapedType && "only shaped types can be tiled" ); |
| 2621 | ArrayRef<int64_t> shape = shapedType.getShape(); |
| 2622 | int64_t rank = shapedType.getRank(); |
| 2623 | |
| 2624 | // Compute offsets/sizes/strides for the tile. |
| 2625 | SliceParameters sliceParams; |
| 2626 | sliceParams.offsets.reserve(N: rank); |
| 2627 | sliceParams.sizes.reserve(N: rank); |
| 2628 | sliceParams.strides.reserve(N: rank); |
| 2629 | for (unsigned r = 0; r < rank; ++r) { |
| 2630 | LLVM_DEBUG(llvm::dbgs() << "computeSliceParameters: for dim#" << r); |
| 2631 | if (!isTiled(map: map.getSubMap(resultPos: {r}), tileSizes)) { |
| 2632 | sliceParams.offsets.push_back(Elt: builder.getIndexAttr(value: 0)); |
| 2633 | OpFoldResult dim = createFoldedDimOp(b&: builder, loc, val: valueToTile, dim: r); |
| 2634 | sliceParams.sizes.push_back(Elt: dim); |
| 2635 | sliceParams.strides.push_back(Elt: builder.getIndexAttr(value: 1)); |
| 2636 | LLVM_DEBUG(llvm::dbgs() << ": not tiled: use size: " << dim << "\n" ); |
| 2637 | continue; |
| 2638 | } |
| 2639 | LLVM_DEBUG(llvm::dbgs() << ": tiled: figure out subsize...\n" ); |
| 2640 | |
| 2641 | // Tiling creates a new slice at the proper index, the slice step is 1 |
| 2642 | // (i.e. the op does not subsample, stepping occurs in the loop). |
| 2643 | auto m = map.getSubMap(resultPos: {r}); |
| 2644 | LLVM_DEBUG(llvm::dbgs() << "computeSliceParameters: submap: " << m << "\n" ); |
| 2645 | IRRewriter rewriter(builder); |
| 2646 | // The offset of the slice is m(lbs) - m(0). |
| 2647 | SmallVector<Attribute> zeros(lbs.size(), rewriter.getIndexAttr(value: 0)); |
| 2648 | SmallVector<Attribute> mAtZero; |
| 2649 | [[maybe_unused]] auto res = m.constantFold(operandConstants: zeros, results&: mAtZero); |
| 2650 | assert(succeeded(res) && "affine_map must be evaluatable (not symbols)" ); |
| 2651 | int64_t mAtZeroInt = |
| 2652 | cast<IntegerAttr>(Val&: mAtZero[0]).getValue().getSExtValue(); |
| 2653 | OpFoldResult offset = makeComposedFoldedAffineApply( |
| 2654 | b&: rewriter, loc, expr: m.getResult(idx: 0) - mAtZeroInt, operands: lbs); |
| 2655 | sliceParams.offsets.push_back(Elt: offset); |
| 2656 | |
| 2657 | OpFoldResult closedIntSize = |
| 2658 | makeComposedFoldedAffineApply(b&: rewriter, loc, map: m, operands: subShapeSizes); |
| 2659 | // Resulting size needs to be made half open interval again. |
| 2660 | AffineExpr s0 = getAffineSymbolExpr(position: 0, context: builder.getContext()); |
| 2661 | OpFoldResult size = |
| 2662 | makeComposedFoldedAffineApply(b&: rewriter, loc, expr: s0 + 1, operands: closedIntSize); |
| 2663 | LLVM_DEBUG(llvm::dbgs() |
| 2664 | << "computeSliceParameters: raw size: " << size << "\n" ); |
| 2665 | LLVM_DEBUG(llvm::dbgs() |
| 2666 | << "computeSliceParameters: new offset: " << offset << "\n" ); |
| 2667 | sliceParams.strides.push_back(Elt: builder.getIndexAttr(value: 1)); |
| 2668 | |
| 2669 | if (omitPartialTileCheck) { |
| 2670 | // We statically know that the partial/boundary tile condition is |
| 2671 | // unnecessary. |
| 2672 | LLVM_DEBUG(llvm::dbgs() << "makeTiledShape: new size: " << size << "\n" ); |
| 2673 | sliceParams.sizes.push_back(Elt: size); |
| 2674 | continue; |
| 2675 | } |
| 2676 | |
| 2677 | // The size of the subview / extract_slice should be trimmed to avoid |
| 2678 | // out-of-bounds accesses, unless: |
| 2679 | // a. We statically know the subshape size divides the shape size evenly. |
| 2680 | // b. The subshape size is 1. According to the way the loops are set up, |
| 2681 | // tensors with "0" dimensions would never be constructed. |
| 2682 | int64_t shapeSize = shape[r]; |
| 2683 | std::optional<int64_t> sizeCst = getConstantIntValue(ofr: size); |
| 2684 | auto hasTileSizeOne = sizeCst == 1; |
| 2685 | auto dividesEvenly = sizeCst && ShapedType::isStatic(dValue: shapeSize) && |
| 2686 | ((shapeSize % *sizeCst) == 0); |
| 2687 | if (!hasTileSizeOne && !dividesEvenly) { |
| 2688 | LLVM_DEBUG(llvm::dbgs() << "makeTiledShape: shapeSize=" << shapeSize |
| 2689 | << ", size: " << size |
| 2690 | << ": make sure in bound with affine.min\n" ); |
| 2691 | |
| 2692 | AffineExpr dim0, dim1, dim2; |
| 2693 | MLIRContext *context = builder.getContext(); |
| 2694 | bindDims(ctx: context, exprs&: dim0, exprs&: dim1, exprs&: dim2); |
| 2695 | |
| 2696 | // Get the dimension size for this dimension. We need to first calculate |
| 2697 | // the max index and then plus one. This is important because for |
| 2698 | // convolution ops, we have its input window dimension's affine map of the |
| 2699 | // form `(d0 * s0 + d1)`, where `d0`/`d1 is an output/filter window |
| 2700 | // dimension and `s0` is stride. Directly use the dimension size of |
| 2701 | // output/filer window dimensions will cause incorrect calculation. |
| 2702 | AffineMap minusOneMap = AffineMap::inferFromExprList( |
| 2703 | exprsList: {ArrayRef<AffineExpr>{dim0 - 1}}, context) |
| 2704 | .front(); |
| 2705 | AffineMap plusOneMap = AffineMap::inferFromExprList( |
| 2706 | exprsList: {ArrayRef<AffineExpr>{dim0 + 1}}, context) |
| 2707 | .front(); |
| 2708 | SmallVector<OpFoldResult> maxIndices = |
| 2709 | llvm::map_to_vector(C&: ubs, F: [&](OpFoldResult ub) { |
| 2710 | return makeComposedFoldedAffineApply(b&: rewriter, loc, map: minusOneMap, |
| 2711 | operands: {ub}); |
| 2712 | }); |
| 2713 | OpFoldResult maxIndex = |
| 2714 | makeComposedFoldedAffineApply(b&: rewriter, loc, map: m, operands: maxIndices); |
| 2715 | OpFoldResult d = |
| 2716 | makeComposedFoldedAffineApply(b&: rewriter, loc, map: plusOneMap, operands: {maxIndex}); |
| 2717 | |
| 2718 | // Compute min(dim - offset, size) to avoid out-of-bounds accesses. |
| 2719 | AffineMap minMap = AffineMap::inferFromExprList( |
| 2720 | exprsList: {ArrayRef<AffineExpr>{dim1 - dim2, dim0}}, context) |
| 2721 | .front(); |
| 2722 | size = |
| 2723 | makeComposedFoldedAffineMin(b&: rewriter, loc, map: minMap, operands: {size, d, offset}); |
| 2724 | } |
| 2725 | LLVM_DEBUG(llvm::dbgs() << "makeTiledShape: new size: " << size << "\n" ); |
| 2726 | sliceParams.sizes.push_back(Elt: size); |
| 2727 | } |
| 2728 | return sliceParams; |
| 2729 | } |
| 2730 | |
| 2731 | SmallVector<OpFoldResult> computeTileOffsets(OpBuilder &b, Location loc, |
| 2732 | ArrayRef<OpFoldResult> ivs, |
| 2733 | ArrayRef<OpFoldResult> tileSizes) { |
| 2734 | SmallVector<OpFoldResult> offsets; |
| 2735 | for (unsigned idx = 0, idxIvs = 0, e = tileSizes.size(); idx < e; ++idx) { |
| 2736 | LLVM_DEBUG(llvm::dbgs() << "makeTiledShapes: for loop#" << idx << "\n" ); |
| 2737 | bool isTiled = !isZeroInteger(v: tileSizes[idx]); |
| 2738 | offsets.push_back(Elt: isTiled ? ivs[idxIvs++] : b.getIndexAttr(value: 0)); |
| 2739 | LLVM_DEBUG(llvm::dbgs() |
| 2740 | << "computeTileOffsets: " << offsets.back() << "\n" ); |
| 2741 | } |
| 2742 | return offsets; |
| 2743 | } |
| 2744 | |
| 2745 | SmallVector<OpFoldResult> computeTileSizes(OpBuilder &b, Location loc, |
| 2746 | ArrayRef<OpFoldResult> tileSizes, |
| 2747 | ArrayRef<OpFoldResult> sizeBounds) { |
| 2748 | SmallVector<OpFoldResult> sizes; |
| 2749 | for (unsigned idx = 0, e = tileSizes.size(); idx < e; ++idx) { |
| 2750 | bool isTiled = !isZeroInteger(v: tileSizes[idx]); |
| 2751 | // Before composing, we need to make range a closed interval. |
| 2752 | OpFoldResult size = isTiled ? tileSizes[idx] : sizeBounds[idx]; |
| 2753 | AffineExpr d0 = getAffineDimExpr(position: 0, context: b.getContext()); |
| 2754 | IRRewriter rewriter(b); |
| 2755 | sizes.push_back(Elt: makeComposedFoldedAffineApply(b&: rewriter, loc, expr: d0 - 1, operands: size)); |
| 2756 | LLVM_DEBUG(llvm::dbgs() << "computeTileSizes: " << sizes.back() << "\n" ); |
| 2757 | } |
| 2758 | return sizes; |
| 2759 | } |
| 2760 | |
| 2761 | SmallVector<Type> getTensorOutputTypes(LinalgOp op, ValueRange operands) { |
| 2762 | if (op.hasPureBufferSemantics()) |
| 2763 | return {}; |
| 2764 | return llvm::map_to_vector( |
| 2765 | C: op.getDpsInitsMutable(), F: [&](OpOperand &opOperand) { |
| 2766 | return operands[opOperand.getOperandNumber()].getType(); |
| 2767 | }); |
| 2768 | } |
| 2769 | |
| 2770 | SmallVector<Value> insertSlicesBack(OpBuilder &builder, Location loc, |
| 2771 | LinalgOp op, ValueRange operands, |
| 2772 | ValueRange results) { |
| 2773 | if (op.hasPureBufferSemantics()) |
| 2774 | return {}; |
| 2775 | SmallVector<Value> tensorResults; |
| 2776 | tensorResults.reserve(N: results.size()); |
| 2777 | // Insert a insert_slice for each output tensor. |
| 2778 | unsigned resultIdx = 0; |
| 2779 | for (OpOperand &opOperand : op.getDpsInitsMutable()) { |
| 2780 | // TODO: use an interface/adaptor to avoid leaking position in |
| 2781 | // `tiledOperands`. |
| 2782 | Value outputTensor = operands[opOperand.getOperandNumber()]; |
| 2783 | if (auto sliceOp = outputTensor.getDefiningOp<tensor::ExtractSliceOp>()) { |
| 2784 | Value inserted = tensor::InsertSliceOp::create( |
| 2785 | builder, location: loc, result: sliceOp.getSource().getType(), source: results[resultIdx], |
| 2786 | dest: sliceOp.getSource(), offsets: sliceOp.getOffsets(), sizes: sliceOp.getSizes(), |
| 2787 | strides: sliceOp.getStrides(), static_offsets: sliceOp.getStaticOffsets(), |
| 2788 | static_sizes: sliceOp.getStaticSizes(), static_strides: sliceOp.getStaticStrides()); |
| 2789 | tensorResults.push_back(Elt: inserted); |
| 2790 | } else { |
| 2791 | tensorResults.push_back(Elt: results[resultIdx]); |
| 2792 | } |
| 2793 | ++resultIdx; |
| 2794 | } |
| 2795 | return tensorResults; |
| 2796 | } |
| 2797 | |
| 2798 | SmallVector<std::optional<SliceParameters>> |
| 2799 | computeAllSliceParameters(OpBuilder &builder, Location loc, LinalgOp linalgOp, |
| 2800 | ValueRange valuesToTile, ArrayRef<OpFoldResult> ivs, |
| 2801 | ArrayRef<OpFoldResult> tileSizes, |
| 2802 | ArrayRef<OpFoldResult> sizeBounds, |
| 2803 | bool omitPartialTileCheck) { |
| 2804 | assert(ivs.size() == static_cast<size_t>(llvm::count_if( |
| 2805 | llvm::make_range(tileSizes.begin(), tileSizes.end()), |
| 2806 | [](OpFoldResult v) { return !isZeroInteger(v); })) && |
| 2807 | "expected as many ivs as non-zero sizes" ); |
| 2808 | |
| 2809 | // Construct (potentially temporary) mins and maxes on which to apply maps |
| 2810 | // that define tile subshapes. |
| 2811 | SmallVector<OpFoldResult> lbs = |
| 2812 | computeTileOffsets(b&: builder, loc, ivs, tileSizes); |
| 2813 | SmallVector<OpFoldResult> subShapeSizes = |
| 2814 | computeTileSizes(b&: builder, loc, tileSizes, sizeBounds); |
| 2815 | |
| 2816 | assert(static_cast<int64_t>(valuesToTile.size()) <= |
| 2817 | linalgOp->getNumOperands() && |
| 2818 | "more value to tile than operands." ); |
| 2819 | SmallVector<std::optional<SliceParameters>> allSliceParams; |
| 2820 | allSliceParams.reserve(N: valuesToTile.size()); |
| 2821 | for (auto [opOperand, val] : |
| 2822 | llvm::zip(t: linalgOp->getOpOperands(), u&: valuesToTile)) { |
| 2823 | Value shapedOp = val; |
| 2824 | LLVM_DEBUG(llvm::dbgs() << "makeTiledShapes: for operand " << shapedOp); |
| 2825 | AffineMap map = linalgOp.getMatchingIndexingMap(opOperand: &opOperand); |
| 2826 | // Use `opOperand` as is if it is not tiled and not an output tensor. Having |
| 2827 | // an extract/insert slice pair for all output tensors simplifies follow up |
| 2828 | // transformations such as padding and bufferization since the |
| 2829 | // extract/insert slice pairs make the accessed iteration argument |
| 2830 | // subdomains explicit. |
| 2831 | |
| 2832 | Type operandType = opOperand.get().getType(); |
| 2833 | if (!isTiled(map, tileSizes) && !(isa<RankedTensorType>(Val: operandType) && |
| 2834 | linalgOp.isDpsInit(opOperand: &opOperand))) { |
| 2835 | allSliceParams.push_back(Elt: std::nullopt); |
| 2836 | LLVM_DEBUG(llvm::dbgs() |
| 2837 | << ": not tiled: use shape: " << operandType << "\n" ); |
| 2838 | continue; |
| 2839 | } |
| 2840 | LLVM_DEBUG(llvm::dbgs() << ": tiled: figure out subshape...\n" ); |
| 2841 | |
| 2842 | allSliceParams.push_back(Elt: computeSliceParameters( |
| 2843 | builder, loc, valueToTile: shapedOp, tileSizes, map, lbs, ubs: sizeBounds, subShapeSizes, |
| 2844 | omitPartialTileCheck)); |
| 2845 | } |
| 2846 | |
| 2847 | return allSliceParams; |
| 2848 | } |
| 2849 | |
| 2850 | SmallVector<Value> makeTiledShapes(OpBuilder &builder, Location loc, |
| 2851 | LinalgOp linalgOp, ValueRange valuesToTile, |
| 2852 | ArrayRef<OpFoldResult> ivs, |
| 2853 | ArrayRef<OpFoldResult> tileSizes, |
| 2854 | ArrayRef<OpFoldResult> sizeBounds, |
| 2855 | bool omitPartialTileCheck) { |
| 2856 | SmallVector<std::optional<SliceParameters>> allSliceParameter = |
| 2857 | computeAllSliceParameters(builder, loc, linalgOp, valuesToTile, ivs, |
| 2858 | tileSizes, sizeBounds, omitPartialTileCheck); |
| 2859 | SmallVector<Value> tiledShapes; |
| 2860 | for (auto item : llvm::zip(t&: valuesToTile, u&: allSliceParameter)) { |
| 2861 | Value valueToTile = std::get<0>(t&: item); |
| 2862 | std::optional<SliceParameters> sliceParams = std::get<1>(t&: item); |
| 2863 | tiledShapes.push_back( |
| 2864 | Elt: sliceParams.has_value() |
| 2865 | ? materializeTiledShape(builder, loc, valueToTile, sliceParams: *sliceParams) |
| 2866 | ->getResult(idx: 0) |
| 2867 | : valueToTile); |
| 2868 | } |
| 2869 | return tiledShapes; |
| 2870 | } |
| 2871 | |
| 2872 | void offsetIndices(OpBuilder &b, LinalgOp linalgOp, |
| 2873 | ArrayRef<OpFoldResult> offsets) { |
| 2874 | IRRewriter rewriter(b); |
| 2875 | offsetIndices(b&: rewriter, linalgOp, offests: offsets); |
| 2876 | } |
| 2877 | |
| 2878 | void offsetIndices(RewriterBase &b, LinalgOp linalgOp, |
| 2879 | ArrayRef<OpFoldResult> offsets) { |
| 2880 | if (!linalgOp.hasIndexSemantics()) |
| 2881 | return; |
| 2882 | |
| 2883 | for (IndexOp indexOp : linalgOp.getBlock()->getOps<IndexOp>()) { |
| 2884 | if (indexOp.getDim() >= offsets.size() || !offsets[indexOp.getDim()]) |
| 2885 | continue; |
| 2886 | OpBuilder::InsertionGuard guard(b); |
| 2887 | b.setInsertionPointAfter(indexOp); |
| 2888 | AffineExpr index, offset; |
| 2889 | bindDims(ctx: b.getContext(), exprs&: index, exprs&: offset); |
| 2890 | OpFoldResult applied = makeComposedFoldedAffineApply( |
| 2891 | b, loc: indexOp.getLoc(), expr: index + offset, |
| 2892 | operands: {getAsOpFoldResult(val: indexOp.getResult()), offsets[indexOp.getDim()]}); |
| 2893 | Value materialized = |
| 2894 | getValueOrCreateConstantIndexOp(b, loc: indexOp.getLoc(), ofr: applied); |
| 2895 | b.replaceUsesWithIf(from: indexOp, to: materialized, functor: [&](OpOperand &use) { |
| 2896 | return use.getOwner() != materialized.getDefiningOp(); |
| 2897 | }); |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | /// Get the reassociation maps to fold the result of a extract_slice (or source |
| 2902 | /// of a insert_slice) operation with given offsets, and sizes to its |
| 2903 | /// rank-reduced version. This is only done for the cases where the size is 1 |
| 2904 | /// and offset is 0. Strictly speaking the offset 0 is not required in general, |
| 2905 | /// but non-zero offsets are not handled by SPIR-V backend at this point (and |
| 2906 | /// potentially cannot be handled). |
| 2907 | std::optional<SmallVector<ReassociationIndices>> |
| 2908 | getReassociationMapForFoldingUnitDims(ArrayRef<OpFoldResult> mixedSizes) { |
| 2909 | SmallVector<ReassociationIndices> reassociation; |
| 2910 | ReassociationIndices curr; |
| 2911 | for (const auto &it : llvm::enumerate(First&: mixedSizes)) { |
| 2912 | auto dim = it.index(); |
| 2913 | auto size = it.value(); |
| 2914 | curr.push_back(Elt: dim); |
| 2915 | auto attr = llvm::dyn_cast_if_present<Attribute>(Val&: size); |
| 2916 | if (attr && cast<IntegerAttr>(Val&: attr).getInt() == 1) |
| 2917 | continue; |
| 2918 | reassociation.emplace_back(Args: ReassociationIndices{}); |
| 2919 | std::swap(LHS&: reassociation.back(), RHS&: curr); |
| 2920 | } |
| 2921 | // When the reassociations are not empty, then fold the remaining |
| 2922 | // unit-dimensions into the last dimension. If the reassociations so far is |
| 2923 | // empty, then leave it emtpy. This will fold everything to a rank-0 tensor. |
| 2924 | if (!curr.empty() && !reassociation.empty()) |
| 2925 | reassociation.back().append(in_start: curr.begin(), in_end: curr.end()); |
| 2926 | return reassociation; |
| 2927 | } |
| 2928 | |
| 2929 | } // namespace linalg |
| 2930 | } // namespace mlir |
| 2931 | |