|
| 1 | +//===- FoldAddIntoDest.cpp ---------------------------------------*- C++-*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 10 | +#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h" |
| 11 | +#include "mlir/Dialect/Linalg/Transforms/Transforms.h" |
| 12 | +#include "mlir/IR/Dominance.h" |
| 13 | +#include "mlir/Interfaces/DestinationStyleOpInterface.h" |
| 14 | + |
| 15 | +using namespace mlir; |
| 16 | + |
| 17 | +/// Replace a linalg.add with one operand the single user of a contraction, |
| 18 | +/// which has a zero-filled, "identity-mapped" destination and is dominated by |
| 19 | +/// the `other` operand, by the contraction with `other` as its dest. |
| 20 | +struct FoldAddIntoDest final : public OpRewritePattern<linalg::AddOp> { |
| 21 | + using OpRewritePattern<linalg::AddOp>::OpRewritePattern; |
| 22 | + |
| 23 | + LogicalResult matchAndRewrite(linalg::AddOp addOp, |
| 24 | + PatternRewriter &rewriter) const override { |
| 25 | + Value dominatingOperand = nullptr; |
| 26 | + linalg::LinalgOp dominatedOp = nullptr; |
| 27 | + { |
| 28 | + auto firstOperand = addOp.getOperand(0); |
| 29 | + auto secondOperand = addOp.getOperand(1); |
| 30 | + |
| 31 | + // Can only put one of addOp's operands in the dest/out arg of the other's |
| 32 | + // defining op based on suitable dominance. |
| 33 | + if (auto secondOp = secondOperand.getDefiningOp<linalg::LinalgOp>()) { |
| 34 | + DominanceInfo domInfo(secondOp); |
| 35 | + if (domInfo.properlyDominates(firstOperand, secondOp)) { |
| 36 | + dominatingOperand = firstOperand; |
| 37 | + dominatedOp = secondOp; |
| 38 | + } |
| 39 | + } |
| 40 | + if (auto firstOp = firstOperand.getDefiningOp<linalg::LinalgOp>()) { |
| 41 | + DominanceInfo domInfo(firstOp); |
| 42 | + if (domInfo.properlyDominates(secondOperand, firstOp)) { |
| 43 | + dominatingOperand = secondOperand; |
| 44 | + dominatedOp = firstOp; |
| 45 | + } |
| 46 | + } |
| 47 | + if (!dominatingOperand || !dominatedOp) |
| 48 | + return failure(); |
| 49 | + // NB: As linalg.add's generalisation ignores the out argument in its |
| 50 | + // region there is no need to perform checks on addOp's out argument. |
| 51 | + } |
| 52 | + |
| 53 | + // Dominated op must be a contraction for it to accumulate on its out arg. |
| 54 | + // E.g., AddOp is not a contraction and hence ignores its out arg's value. |
| 55 | + auto dominatedDestOp = |
| 56 | + dyn_cast<DestinationStyleOpInterface>((Operation *)dominatedOp); |
| 57 | + if (dominatedOp->getNumResults() != 1 || |
| 58 | + !linalg::isaContractionOpInterface(dominatedOp) || |
| 59 | + (!dominatedDestOp || dominatedDestOp.getNumDpsInits() != 1)) |
| 60 | + return rewriter.notifyMatchFailure( |
| 61 | + dominatedOp, "expected dominated op to be single-result " |
| 62 | + "destination-passing contraction"); |
| 63 | + |
| 64 | + // To change the contraction's result, `addOp` must be its only user. |
| 65 | + if (!dominatedOp->getResult(0).hasOneUse()) |
| 66 | + return rewriter.notifyMatchFailure( |
| 67 | + dominatedOp, |
| 68 | + "expected linalg.add to be single user of contraction's result"); |
| 69 | + |
| 70 | + // As `dominatedOp` was already accumulating on its out argument, it is only |
| 71 | + // safe to no longer use its current out arg when it is the additive zero. |
| 72 | + auto *destOperand = dominatedDestOp.getDpsInitOperand(0); |
| 73 | + if (!linalg::isZeroTensor(destOperand->get())) |
| 74 | + return rewriter.notifyMatchFailure( |
| 75 | + dominatedOp, "expected dominated op's dest to be additive zero"); |
| 76 | + // TODO: If the other op is a contraction and has additive zero as dest, we |
| 77 | + // can swap the dests and achieve the proper sum, given suitable dominance. |
| 78 | + |
| 79 | + // As an operand to `addOp`, `dominatingOperand` has an identity affine_map. |
| 80 | + // Hence, we can only substitute `dominatingOperand` for the dest of the |
| 81 | + // contraction when dest's indexing_map corresponds to an identity map |
| 82 | + // w.r.t. just the dimensions of dest, i.e. is an ordered projection. |
| 83 | + SmallVector<AffineMap> indexMaps = dominatedOp.getIndexingMapsArray(); |
| 84 | + int prevDimPos = -1; |
| 85 | + for (auto expr : indexMaps[destOperand->getOperandNumber()].getResults()) { |
| 86 | + auto dim = dyn_cast<AffineDimExpr>(expr); |
| 87 | + if (!dim || prevDimPos >= (int)dim.getPosition()) |
| 88 | + return rewriter.notifyMatchFailure( |
| 89 | + dominatedOp, "expected index_map for contraction's dest to be an " |
| 90 | + "ordered projection"); |
| 91 | + prevDimPos = dim.getPosition(); |
| 92 | + } |
| 93 | + |
| 94 | + // Replace the additive-zero out argument of the dominated op by the |
| 95 | + // dominating summand. This makes the dominated op's result the sum of both |
| 96 | + // of addOp's arguments - therefore we replace addOp and it uses by it. |
| 97 | + rewriter.modifyOpInPlace( |
| 98 | + dominatedOp, [&]() { dominatedOp->setOperand(2, dominatingOperand); }); |
| 99 | + rewriter.replaceAllOpUsesWith(addOp, dominatedOp->getResult(0)); |
| 100 | + return success(); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +void linalg::populateFoldAddIntoDestPatterns(RewritePatternSet &patterns) { |
| 105 | + // Replace linalg.add when destination passing suffices for achieving the sum. |
| 106 | + patterns.add<FoldAddIntoDest>(patterns.getContext()); |
| 107 | +} |
0 commit comments