-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][VectorOps] Add fold vector.shuffle -> vector.interleave (4/4) #80968
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2479,11 +2479,51 @@ class ShuffleSplat final : public OpRewritePattern<ShuffleOp> { | |
} | ||
}; | ||
|
||
/// Pattern to rewrite a fixed-size interleave via vector.shuffle to | ||
/// vector.interleave. | ||
class ShuffleInterleave : public OpRewritePattern<ShuffleOp> { | ||
public: | ||
using OpRewritePattern::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(ShuffleOp op, | ||
PatternRewriter &rewriter) const override { | ||
VectorType resultType = op.getResultVectorType(); | ||
if (resultType.isScalable()) | ||
return rewriter.notifyMatchFailure( | ||
op, "ShuffleOp can't represent a scalable interleave"); | ||
|
||
if (resultType.getRank() != 1) | ||
return rewriter.notifyMatchFailure( | ||
op, "ShuffleOp can't represent an n-D interleave"); | ||
|
||
VectorType sourceType = op.getV1VectorType(); | ||
if (sourceType != op.getV2VectorType() || | ||
Comment on lines
+2499
to
+2500
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. Can shuffle inputs have different types? 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. I don't think so. From the shuffle docs:
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. I thought their shapes should also match. Anyways... |
||
sourceType.getNumElements() * 2 != resultType.getNumElements()) { | ||
return rewriter.notifyMatchFailure( | ||
op, "ShuffleOp types don't match an interleave"); | ||
} | ||
|
||
ArrayAttr shuffleMask = op.getMask(); | ||
int64_t resultVectorSize = resultType.getNumElements(); | ||
for (int i = 0, e = resultVectorSize / 2; i < e; ++i) { | ||
int64_t maskValueA = cast<IntegerAttr>(shuffleMask[i * 2]).getInt(); | ||
int64_t maskValueB = cast<IntegerAttr>(shuffleMask[(i * 2) + 1]).getInt(); | ||
if (maskValueA != i || maskValueB != (resultVectorSize / 2) + i) | ||
return rewriter.notifyMatchFailure(op, | ||
"ShuffleOp mask not interleaving"); | ||
} | ||
|
||
rewriter.replaceOpWithNewOp<InterleaveOp>(op, op.getV1(), op.getV2()); | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void ShuffleOp::getCanonicalizationPatterns(RewritePatternSet &results, | ||
MLIRContext *context) { | ||
results.add<ShuffleSplat, Canonicalize0DShuffleOp>(context); | ||
results.add<ShuffleSplat, ShuffleInterleave, Canonicalize0DShuffleOp>( | ||
context); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case shouldn't be allowed by the verifier so I would replace this with an assert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's currently no checks on
vector.shuffle
for scalable vectors 😔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be better to add that to the verified