-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][vector] Extend TransferReadDropUnitDimsPattern to support partially-static memrefs #72142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
c-rhodes
merged 2 commits into
llvm:main
from
c-rhodes:mlir-drop-unit-dims-dynamic-scalable
Nov 20, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -260,12 +260,37 @@ 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; | ||
for (const auto size : mixedSizes) { | ||
if (llvm::dyn_cast_if_present<Value>(size)) { | ||
reducedShape.push_back(ShapedType::kDynamic); | ||
continue; | ||
} | ||
|
||
auto value = cast<IntegerAttr>(size.get<Attribute>()).getValue(); | ||
if (value == 1) | ||
continue; | ||
reducedShape.push_back(value.getSExtValue()); | ||
} | ||
return reducedShape; | ||
} | ||
|
||
/// Drops unit dimensions from the input MemRefType. | ||
static MemRefType dropUnitDims(MemRefType inputType, ArrayRef<int64_t> offsets, | ||
ArrayRef<int64_t> sizes, | ||
ArrayRef<int64_t> strides) { | ||
SmallVector<int64_t> targetShape = llvm::to_vector( | ||
llvm::make_filter_range(sizes, [](int64_t sz) { return sz != 1; })); | ||
static MemRefType dropUnitDims(MemRefType inputType, | ||
ArrayRef<OpFoldResult> offsets, | ||
ArrayRef<OpFoldResult> sizes, | ||
ArrayRef<OpFoldResult> strides) { | ||
auto targetShape = getReducedShape(sizes); | ||
Type rankReducedType = memref::SubViewOp::inferRankReducedResultType( | ||
targetShape, inputType, offsets, sizes, strides); | ||
return canonicalizeStridedLayout(cast<MemRefType>(rankReducedType)); | ||
|
@@ -277,30 +302,63 @@ static Value rankReducingSubviewDroppingUnitDims(PatternRewriter &rewriter, | |
mlir::Location loc, | ||
Value input) { | ||
MemRefType inputType = cast<MemRefType>(input.getType()); | ||
assert(inputType.hasStaticShape()); | ||
SmallVector<int64_t> subViewOffsets(inputType.getRank(), 0); | ||
SmallVector<int64_t> subViewStrides(inputType.getRank(), 1); | ||
ArrayRef<int64_t> subViewSizes = inputType.getShape(); | ||
MemRefType resultType = | ||
dropUnitDims(inputType, subViewOffsets, subViewSizes, subViewStrides); | ||
SmallVector<OpFoldResult> offsets(inputType.getRank(), | ||
rewriter.getIndexAttr(0)); | ||
SmallVector<OpFoldResult> sizes = memref::getMixedSizes(rewriter, loc, input); | ||
SmallVector<OpFoldResult> strides(inputType.getRank(), | ||
rewriter.getIndexAttr(1)); | ||
MemRefType resultType = dropUnitDims(inputType, offsets, sizes, strides); | ||
|
||
if (canonicalizeStridedLayout(resultType) == | ||
canonicalizeStridedLayout(inputType)) | ||
return input; | ||
return rewriter.create<memref::SubViewOp>( | ||
loc, resultType, input, subViewOffsets, subViewSizes, subViewStrides); | ||
return rewriter.create<memref::SubViewOp>(loc, resultType, input, offsets, | ||
sizes, strides); | ||
} | ||
|
||
/// Returns the number of dims that aren't unit dims. | ||
static int getReducedRank(ArrayRef<int64_t> shape) { | ||
return llvm::count_if(shape, [](int64_t dimSize) { return dimSize != 1; }); | ||
} | ||
|
||
/// 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; | ||
/// Trims non-scalable one dimensions from `oldType` and returns the result | ||
/// type. | ||
static VectorType trimNonScalableUnitDims(VectorType oldType) { | ||
SmallVector<int64_t> newShape; | ||
SmallVector<bool> newScalableDims; | ||
for (auto [dimIdx, dimSize] : llvm::enumerate(oldType.getShape())) { | ||
if (dimSize == 1 && !oldType.getScalableDims()[dimIdx]) | ||
continue; | ||
newShape.push_back(dimSize); | ||
newScalableDims.push_back(oldType.getScalableDims()[dimIdx]); | ||
} | ||
return VectorType::get(newShape, oldType.getElementType(), newScalableDims); | ||
} | ||
|
||
// Rewrites vector.create_mask 'op' to drop non-scalable one dimensions. | ||
static FailureOr<Value> | ||
createMaskDropNonScalableUnitDims(PatternRewriter &rewriter, Location loc, | ||
vector::CreateMaskOp op) { | ||
auto type = op.getType(); | ||
auto reducedType = trimNonScalableUnitDims(type); | ||
if (reducedType.getRank() == type.getRank()) | ||
return failure(); | ||
|
||
SmallVector<Value> reducedOperands; | ||
for (auto [dim, dimIsScalable, operand] : llvm::zip_equal( | ||
type.getShape(), type.getScalableDims(), op.getOperands())) { | ||
if (dim == 1 && !dimIsScalable) { | ||
// If the mask for the unit dim is not a constant of 1, do nothing. | ||
auto constant = operand.getDefiningOp<arith::ConstantIndexOp>(); | ||
if (!constant || (constant.value() != 1)) | ||
return failure(); | ||
continue; | ||
} | ||
reducedOperands.push_back(operand); | ||
} | ||
return rewriter | ||
.create<vector::CreateMaskOp>(loc, reducedType, reducedOperands) | ||
.getResult(); | ||
} | ||
|
||
namespace { | ||
|
@@ -320,9 +378,7 @@ class TransferReadDropUnitDimsPattern | |
Value source = transferReadOp.getSource(); | ||
MemRefType sourceType = dyn_cast<MemRefType>(source.getType()); | ||
// TODO: support tensor types. | ||
if (!sourceType || !sourceType.hasStaticShape()) | ||
return failure(); | ||
if (sourceType.getNumElements() != vectorType.getNumElements()) | ||
if (!sourceType) | ||
return failure(); | ||
// TODO: generalize this pattern, relax the requirements here. | ||
if (transferReadOp.hasOutOfBoundsDim()) | ||
|
@@ -335,23 +391,38 @@ class TransferReadDropUnitDimsPattern | |
return failure(); | ||
// Check if the reduced vector shape matches the reduced source shape. | ||
// Otherwise, this case is not supported yet. | ||
int vectorReducedRank = getReducedRank(vectorType.getShape()); | ||
if (reducedRank != vectorReducedRank) | ||
auto reducedVectorType = trimNonScalableUnitDims(vectorType); | ||
if (reducedRank != reducedVectorType.getRank()) | ||
return failure(); | ||
if (llvm::any_of(transferReadOp.getIndices(), [](Value v) { | ||
return getConstantIntValue(v) != static_cast<int64_t>(0); | ||
})) | ||
return failure(); | ||
|
||
Value maskOp = transferReadOp.getMask(); | ||
if (maskOp) { | ||
auto createMaskOp = maskOp.getDefiningOp<vector::CreateMaskOp>(); | ||
if (!createMaskOp) | ||
return rewriter.notifyMatchFailure( | ||
transferReadOp, "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); | ||
auto reducedVectorType = VectorType::get( | ||
getReducedShape(vectorType.getShape()), vectorType.getElementType()); | ||
|
||
SmallVector<bool> inBounds(reducedVectorType.getRank(), true); | ||
auto newTransferReadOp = rewriter.create<vector::TransferReadOp>( | ||
loc, reducedVectorType, reducedShapeSource, zeros, identityMap); | ||
loc, reducedVectorType, reducedShapeSource, zeros, identityMap, | ||
transferReadOp.getPadding(), maskOp, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for adding the previously omitted mask! |
||
rewriter.getBoolArrayAttr(inBounds)); | ||
auto shapeCast = rewriter.createOrFold<vector::ShapeCastOp>( | ||
loc, vectorType, newTransferReadOp); | ||
rewriter.replaceOp(transferReadOp, shapeCast); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.