MLIR 24.0.0git
TosaReduceReshapes.cpp
Go to the documentation of this file.
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
10
15
16namespace mlir {
17namespace tosa {
18#define GEN_PASS_DEF_TOSAREDUCERESHAPES
19#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
20} // namespace tosa
21} // namespace mlir
22
23using namespace mlir;
24using namespace mlir::tosa;
25
26namespace {
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.
38static 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(reassociation)) {
49 if (operandType.getDimSize(sourceIndex) ==
50 sourceType.getDimSize(sourceIndex)) {
51 for (int64_t targetIndex : targetIndices) {
52 expandedShape.push_back(targetType.getDimSize(targetIndex));
53 }
54 } else if (operandType.getDimSize(sourceIndex) == 1) {
55 expandedShape.append(targetIndices.size(), 1);
56 } else {
57 return failure();
58 }
59 }
60 return expandedShape;
61}
62
63static bool isSupportedProducer(Operation *op) {
64 bool preservesRank = op->hasTrait<OpTrait::SameOperandsAndResultRank>() ||
66
67 bool isSupportedCast = false;
68 if (auto castOp = dyn_cast<tosa::CastOp>(op)) {
69 // Only ordinary scalar casts are independent of the tensor shape.
70 auto inputType = cast<ShapedType>(castOp.getInput().getType());
71 auto outputType = cast<ShapedType>(castOp.getOutput().getType());
72 isSupportedCast = inputType.getElementType().isIntOrIndexOrFloat() &&
73 outputType.getElementType().isIntOrIndexOrFloat();
74 }
75
76 return preservesRank &&
77 (isSupportedCast ||
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.
84static bool canFoldOperandReshape(Value operand, RankedTensorType operandType) {
85 if (isa_and_nonnull<tosa::ReshapeOp>(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>(values)) {
96 return true;
97 }
98 auto denseValues = dyn_cast<DenseElementsAttr>(values);
99 return denseValues && (denseValues.isSplat() || operand.hasOneUse());
100}
101
102struct PropagateExpandingReshape : public OpRewritePattern<tosa::ReshapeOp> {
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>(reshape.getInput1().getType());
117 auto targetType = dyn_cast<RankedTensorType>(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>(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);
143 if (failed(expandedShape)) {
144 return failure();
145 }
146 expandedTypes.push_back(operandType.clone(*expandedShape));
147 }
148
149 SmallVector<Value> expandedOperands;
150 for (auto [operand, expandedType] :
151 llvm::zip_equal(op->getOperands(), expandedTypes)) {
152 Value shape =
153 getTosaConstShape(rewriter, op->getLoc(), expandedType.getShape());
154 expandedOperands.push_back(tosa::ReshapeOp::create(
155 rewriter, op->getLoc(), expandedType, operand, shape));
156 }
157
158 Value expandedResult =
159 rewriter
160 .create(op->getLoc(), op->getName().getIdentifier(),
161 expandedOperands, targetType, op->getAttrs())
162 ->getResult(0);
163 rewriter.replaceOp(reshape, expandedResult);
164 rewriter.eraseOp(op);
165 return success();
166 }
167};
168
169struct 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>(&getContext());
176 if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) {
177 signalPassFailure();
178 }
179 }
180};
181} // namespace
return success()
b getContext())
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition Builders.cpp:466
This class verifies that op has same ranks for all operands and results types, if known.
This class provides verification for ops that are known to have the same operand and result shape: bo...
This class indicates that an op is tosa-elementwise (permits broadcasting, unlike Elementwise trait).
Definition TosaOps.h:58
StringAttr getIdentifier() const
Return the name of this operation as a StringAttr.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:803
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition Operation.h:566
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:434
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:115
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:405
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:431
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
bool hasOneUse() const
Returns true if this value has exactly one use.
Definition Value.h:197
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:853
Value getTosaConstShape(ImplicitLocOpBuilder &builder, llvm::ArrayRef< int64_t > shape)
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
std::optional< SmallVector< ReassociationIndices > > getReassociationIndicesForReshape(ShapedType sourceType, ShapedType targetType)
Return the reassociations maps to use to reshape given the source type and the target type when possi...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...