| 1 | //===- TosaReduceReshapes.cpp --------------------------------------------===// |
| 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/Tosa/Transforms/Passes.h" |
| 10 | |
| 11 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 12 | #include "mlir/Dialect/Tosa/Utils/ConversionUtils.h" |
| 13 | #include "mlir/Dialect/Utils/ReshapeOpsUtils.h" |
| 14 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 15 | |
| 16 | namespace mlir { |
| 17 | namespace tosa { |
| 18 | #define GEN_PASS_DEF_TOSAREDUCERESHAPES |
| 19 | #include "mlir/Dialect/Tosa/Transforms/Passes.h.inc" |
| 20 | } // namespace tosa |
| 21 | } // namespace mlir |
| 22 | |
| 23 | using namespace mlir; |
| 24 | using namespace mlir::tosa; |
| 25 | |
| 26 | namespace { |
| 27 | /// Computes a higher-rank operand shape that preserves broadcasting. |
| 28 | /// |
| 29 | /// For example, expanding an operator result from [6, 4] to [2, 3, 4] gives |
| 30 | /// the reassociation {{0, 1}, {2}}. An operand with shape [6, 4] expands to |
| 31 | /// [2, 3, 4] because both of its dimensions match the source shape. A broadcast |
| 32 | /// operand with shape [1, 4] instead expands to [1, 1, 4]: the source dimension |
| 33 | /// of size 6 is split into two unit dimensions so that the operand continues to |
| 34 | /// broadcast over the corresponding target dimensions of sizes 2 and 3. |
| 35 | /// |
| 36 | /// An operand dimension must therefore either match its source dimension or be |
| 37 | /// one. Any other dimension is incompatible with the source result shape. |
| 38 | static FailureOr<SmallVector<int64_t>> getExpandedBroadcastShape( |
| 39 | RankedTensorType operandType, RankedTensorType sourceType, |
| 40 | RankedTensorType targetType, ArrayRef<ReassociationIndices> reassociation) { |
| 41 | SmallVector<int64_t> expandedShape; |
| 42 | // A scalar has no source dimensions, so the loop below would leave its |
| 43 | // operand scalar. Use the target shape to match the replacement op's rank. |
| 44 | if (sourceType.getRank() == 0) { |
| 45 | return SmallVector<int64_t>(targetType.getShape()); |
| 46 | } |
| 47 | |
| 48 | for (auto [sourceIndex, targetIndices] : llvm::enumerate(First&: reassociation)) { |
| 49 | if (operandType.getDimSize(idx: sourceIndex) == |
| 50 | sourceType.getDimSize(idx: sourceIndex)) { |
| 51 | for (int64_t targetIndex : targetIndices) { |
| 52 | expandedShape.push_back(Elt: targetType.getDimSize(idx: targetIndex)); |
| 53 | } |
| 54 | } else if (operandType.getDimSize(idx: sourceIndex) == 1) { |
| 55 | expandedShape.append(NumInputs: targetIndices.size(), Elt: 1); |
| 56 | } else { |
| 57 | return failure(); |
| 58 | } |
| 59 | } |
| 60 | return expandedShape; |
| 61 | } |
| 62 | |
| 63 | static bool isSupportedProducer(Operation *op) { |
| 64 | bool preservesRank = op->hasTrait<OpTrait::SameOperandsAndResultRank>() || |
| 65 | op->hasTrait<OpTrait::SameOperandsAndResultShape>(); |
| 66 | |
| 67 | bool isSupportedCast = false; |
| 68 | if (auto castOp = dyn_cast<tosa::CastOp>(Val: op)) { |
| 69 | // Only ordinary scalar casts are independent of the tensor shape. |
| 70 | auto inputType = cast<ShapedType>(Val: castOp.getInput().getType()); |
| 71 | auto outputType = cast<ShapedType>(Val: castOp.getOutput().getType()); |
| 72 | isSupportedCast = inputType.getElementType().isIntOrIndexOrFloat() && |
| 73 | outputType.getElementType().isIntOrIndexOrFloat(); |
| 74 | } |
| 75 | |
| 76 | return preservesRank && |
| 77 | (isSupportedCast || |
| 78 | op->hasTrait<OpTrait::tosa::TosaElementwiseOperator>()); |
| 79 | } |
| 80 | |
| 81 | /// Whether reshaping `operand` avoids leaving an additional reshape. An |
| 82 | /// existing reshape must have one use so that it can be removed after the |
| 83 | /// inserted reshape folds. Match the constant cases handled by ReshapeOp::fold. |
| 84 | static bool canFoldOperandReshape(Value operand, RankedTensorType operandType) { |
| 85 | if (isa_and_nonnull<tosa::ReshapeOp>(Val: operand.getDefiningOp())) { |
| 86 | return operand.hasOneUse(); |
| 87 | } |
| 88 | |
| 89 | auto constOp = operand.getDefiningOp<tosa::ConstOp>(); |
| 90 | if (!constOp || !operandType.getElementType().isIntOrIndexOrFloat()) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | ElementsAttr values = constOp.getValues(); |
| 95 | if (isa<DenseResourceElementsAttr>(Val: values)) { |
| 96 | return true; |
| 97 | } |
| 98 | auto denseValues = dyn_cast<DenseElementsAttr>(Val&: values); |
| 99 | return denseValues && (denseValues.isSplat() || operand.hasOneUse()); |
| 100 | } |
| 101 | |
| 102 | struct PropagateExpandingReshape : public OpRewritePattern<tosa::ReshapeOp> { |
| 103 | using OpRewritePattern::OpRewritePattern; |
| 104 | |
| 105 | LogicalResult matchAndRewrite(tosa::ReshapeOp reshape, |
| 106 | PatternRewriter &rewriter) const override { |
| 107 | if (!reshape.getInput1().hasOneUse()) { |
| 108 | return failure(); |
| 109 | } |
| 110 | |
| 111 | Operation *op = reshape.getInput1().getDefiningOp(); |
| 112 | if (!op || !isSupportedProducer(op) || op->getNumResults() != 1) { |
| 113 | return failure(); |
| 114 | } |
| 115 | |
| 116 | auto sourceType = dyn_cast<RankedTensorType>(Val: reshape.getInput1().getType()); |
| 117 | auto targetType = dyn_cast<RankedTensorType>(Val: reshape.getOutput().getType()); |
| 118 | if (!sourceType || !targetType || !sourceType.hasStaticShape() || |
| 119 | !targetType.hasStaticShape() || |
| 120 | targetType.getRank() <= sourceType.getRank()) { |
| 121 | return failure(); |
| 122 | } |
| 123 | |
| 124 | auto reassociation = |
| 125 | getReassociationIndicesForReshape(sourceType, targetType); |
| 126 | if (!reassociation) { |
| 127 | return failure(); |
| 128 | } |
| 129 | |
| 130 | unsigned nonFoldableOperands = 0; |
| 131 | SmallVector<RankedTensorType> expandedTypes; |
| 132 | for (Value operand : op->getOperands()) { |
| 133 | auto operandType = dyn_cast<RankedTensorType>(Val: operand.getType()); |
| 134 | if (!operandType || !operandType.hasStaticShape()) { |
| 135 | return failure(); |
| 136 | } |
| 137 | if (!canFoldOperandReshape(operand, operandType) && |
| 138 | ++nonFoldableOperands > 1) { |
| 139 | return failure(); |
| 140 | } |
| 141 | FailureOr<SmallVector<int64_t>> expandedShape = getExpandedBroadcastShape( |
| 142 | operandType, sourceType, targetType, reassociation: *reassociation); |
| 143 | if (failed(Result: expandedShape)) { |
| 144 | return failure(); |
| 145 | } |
| 146 | expandedTypes.push_back(Elt: operandType.clone(shape: *expandedShape)); |
| 147 | } |
| 148 | |
| 149 | SmallVector<Value> expandedOperands; |
| 150 | for (auto [operand, expandedType] : |
| 151 | llvm::zip_equal(t: op->getOperands(), u&: expandedTypes)) { |
| 152 | Value shape = |
| 153 | getTosaConstShape(rewriter, loc: op->getLoc(), shape: expandedType.getShape()); |
| 154 | expandedOperands.push_back(Elt: tosa::ReshapeOp::create( |
| 155 | builder&: rewriter, location: op->getLoc(), output: expandedType, input1: operand, shape)); |
| 156 | } |
| 157 | |
| 158 | Value expandedResult = |
| 159 | rewriter |
| 160 | .create(loc: op->getLoc(), opName: op->getName().getIdentifier(), |
| 161 | operands: expandedOperands, types: targetType, attributes: op->getAttrs()) |
| 162 | ->getResult(idx: 0); |
| 163 | rewriter.replaceOp(op: reshape, newValues: expandedResult); |
| 164 | rewriter.eraseOp(op); |
| 165 | return success(); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | struct TosaReduceReshapes final |
| 170 | : public tosa::impl::TosaReduceReshapesBase<TosaReduceReshapes> { |
| 171 | using Base::Base; |
| 172 | |
| 173 | void runOnOperation() override { |
| 174 | RewritePatternSet patterns(&getContext()); |
| 175 | patterns.add<PropagateExpandingReshape>(arg: &getContext()); |
| 176 | if (failed(Result: applyPatternsGreedily(op: getOperation(), patterns: std::move(patterns)))) { |
| 177 | signalPassFailure(); |
| 178 | } |
| 179 | } |
| 180 | }; |
| 181 | } // namespace |
| 182 | |