18#define GEN_PASS_DEF_TOSAREDUCERESHAPES
19#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
38static FailureOr<SmallVector<int64_t>> getExpandedBroadcastShape(
39 RankedTensorType operandType, RankedTensorType sourceType,
44 if (sourceType.getRank() == 0) {
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));
54 }
else if (operandType.getDimSize(sourceIndex) == 1) {
55 expandedShape.append(targetIndices.size(), 1);
63static bool isSupportedProducer(
Operation *op) {
67 bool isSupportedCast =
false;
68 if (
auto castOp = dyn_cast<tosa::CastOp>(op)) {
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();
76 return preservesRank &&
84static bool canFoldOperandReshape(
Value operand, RankedTensorType operandType) {
85 if (isa_and_nonnull<tosa::ReshapeOp>(operand.
getDefiningOp())) {
90 if (!constOp || !operandType.getElementType().isIntOrIndexOrFloat()) {
94 ElementsAttr values = constOp.getValues();
95 if (isa<DenseResourceElementsAttr>(values)) {
98 auto denseValues = dyn_cast<DenseElementsAttr>(values);
99 return denseValues && (denseValues.isSplat() || operand.
hasOneUse());
102struct PropagateExpandingReshape :
public OpRewritePattern<tosa::ReshapeOp> {
105 LogicalResult matchAndRewrite(tosa::ReshapeOp reshape,
106 PatternRewriter &rewriter)
const override {
107 if (!reshape.getInput1().hasOneUse()) {
111 Operation *op = reshape.getInput1().getDefiningOp();
112 if (!op || !isSupportedProducer(op) || op->
getNumResults() != 1) {
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()) {
126 if (!reassociation) {
130 unsigned nonFoldableOperands = 0;
131 SmallVector<RankedTensorType> expandedTypes;
133 auto operandType = dyn_cast<RankedTensorType>(operand.
getType());
134 if (!operandType || !operandType.hasStaticShape()) {
137 if (!canFoldOperandReshape(operand, operandType) &&
138 ++nonFoldableOperands > 1) {
141 FailureOr<SmallVector<int64_t>> expandedShape = getExpandedBroadcastShape(
142 operandType, sourceType, targetType, *reassociation);
143 if (
failed(expandedShape)) {
146 expandedTypes.push_back(operandType.clone(*expandedShape));
149 SmallVector<Value> expandedOperands;
150 for (
auto [operand, expandedType] :
151 llvm::zip_equal(op->
getOperands(), expandedTypes)) {
154 expandedOperands.push_back(tosa::ReshapeOp::create(
155 rewriter, op->
getLoc(), expandedType, operand, shape));
158 Value expandedResult =
161 expandedOperands, targetType, op->
getAttrs())
163 rewriter.
replaceOp(reshape, expandedResult);
169struct TosaReduceReshapes final
173 void runOnOperation()
override {
175 patterns.add<PropagateExpandingReshape>(&
getContext());
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
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).
StringAttr getIdentifier() const
Return the name of this operation as a StringAttr.
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Location getLoc()
The source location the operation was defined or derived from.
OperationName getName()
The name of an operation is the key identifier for it.
operand_range getOperands()
Returns an iterator on the underlying Value's.
unsigned getNumResults()
Return the number of results held by this operation.
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...
Type getType() const
Return the type of this value.
bool hasOneUse() const
Returns true if this value has exactly one use.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Value getTosaConstShape(ImplicitLocOpBuilder &builder, llvm::ArrayRef< int64_t > shape)
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region ®ion, 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...