diff options
| author | Quinn Dawkins <quinn.dawkins@gmail.com> | 2023-12-01 10:01:28 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-01 10:01:28 -0500 |
| commit | fdf84cbf87198d16fe17aed0c31989ee31051d82 (patch) | |
| tree | 7011f15b26c9d313dcab7983a7fbe095fffb5ac8 /mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp | |
| parent | b92693ac6afc522ea56bede0b9805ca7c138754c (diff) | |
[mlir][vector] Fix unit dim dropping pattern for masked writes (#74038)
This does the same as #72142 for vector.transfer_write. Previously the
pattern would silently drop the mask.
Diffstat (limited to 'mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp')
| -rw-r--r-- | mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp index d2c6ba557b9b..75e1abead973 100644 --- a/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp @@ -260,14 +260,6 @@ void TransferOptimization::storeToLoadForwarding(vector::TransferReadOp read) { opToErase.push_back(read.getOperation()); } -/// Returns a copy of `shape` without unit dims. -static SmallVector<int64_t> getReducedShape(ArrayRef<int64_t> shape) { - SmallVector<int64_t> reducedShape; - llvm::copy_if(shape, std::back_inserter(reducedShape), - [](int64_t dimSize) { return dimSize != 1; }); - return reducedShape; -} - /// Converts OpFoldResults to int64_t shape without unit dims. static SmallVector<int64_t> getReducedShape(ArrayRef<OpFoldResult> mixedSizes) { SmallVector<int64_t> reducedShape; @@ -340,7 +332,7 @@ static FailureOr<Value> createMaskDropNonScalableUnitDims(PatternRewriter &rewriter, Location loc, vector::CreateMaskOp op) { auto type = op.getType(); - auto reducedType = trimNonScalableUnitDims(type); + VectorType reducedType = trimNonScalableUnitDims(type); if (reducedType.getRank() == type.getRank()) return failure(); @@ -391,7 +383,7 @@ class TransferReadDropUnitDimsPattern return failure(); // Check if the reduced vector shape matches the reduced source shape. // Otherwise, this case is not supported yet. - auto reducedVectorType = trimNonScalableUnitDims(vectorType); + VectorType reducedVectorType = trimNonScalableUnitDims(vectorType); if (reducedRank != reducedVectorType.getRank()) return failure(); if (llvm::any_of(transferReadOp.getIndices(), [](Value v) { @@ -446,9 +438,7 @@ class TransferWriteDropUnitDimsPattern Value source = transferWriteOp.getSource(); MemRefType sourceType = dyn_cast<MemRefType>(source.getType()); // TODO: support tensor type. - if (!sourceType || !sourceType.hasStaticShape()) - return failure(); - if (sourceType.getNumElements() != vectorType.getNumElements()) + if (!sourceType) return failure(); // TODO: generalize this pattern, relax the requirements here. if (transferWriteOp.hasOutOfBoundsDim()) @@ -461,25 +451,39 @@ class TransferWriteDropUnitDimsPattern return failure(); // Check if the reduced vector shape matches the reduced destination shape. // Otherwise, this case is not supported yet. - int vectorReducedRank = getReducedRank(vectorType.getShape()); - if (reducedRank != vectorReducedRank) + VectorType reducedVectorType = trimNonScalableUnitDims(vectorType); + if (reducedRank != reducedVectorType.getRank()) return failure(); if (llvm::any_of(transferWriteOp.getIndices(), [](Value v) { return getConstantIntValue(v) != static_cast<int64_t>(0); })) return failure(); + + Value maskOp = transferWriteOp.getMask(); + if (maskOp) { + auto createMaskOp = maskOp.getDefiningOp<vector::CreateMaskOp>(); + if (!createMaskOp) + return rewriter.notifyMatchFailure( + transferWriteOp, + "unsupported mask op, only 'vector.create_mask' is " + "currently supported"); + FailureOr<Value> rankReducedCreateMask = + createMaskDropNonScalableUnitDims(rewriter, loc, createMaskOp); + if (failed(rankReducedCreateMask)) + return failure(); + maskOp = *rankReducedCreateMask; + } Value reducedShapeSource = rankReducingSubviewDroppingUnitDims(rewriter, loc, source); Value c0 = rewriter.create<arith::ConstantIndexOp>(loc, 0); SmallVector<Value> zeros(reducedRank, c0); auto identityMap = rewriter.getMultiDimIdentityMap(reducedRank); - VectorType reducedVectorType = VectorType::get( - getReducedShape(vectorType.getShape()), vectorType.getElementType()); - + SmallVector<bool> inBounds(reducedVectorType.getRank(), true); auto shapeCast = rewriter.createOrFold<vector::ShapeCastOp>( loc, reducedVectorType, vector); rewriter.replaceOpWithNewOp<vector::TransferWriteOp>( - transferWriteOp, shapeCast, reducedShapeSource, zeros, identityMap); + transferWriteOp, Type(), shapeCast, reducedShapeSource, zeros, + identityMap, maskOp, rewriter.getBoolArrayAttr(inBounds)); return success(); } |
