-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[mlir][XeGPU] add unroll patterns for load_matrix and store_matrix #154637
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22d4193
add unroll pattern and unit test for load_matrix and store_matrix
chencha3 d20da85
add unroll pattern and unit test for load_matrix and store_matrix
chencha3 87e4fd2
Merge branch 'ld_st_matrix_unroll_patterns' of https://github.com/che…
chencha3 b3af260
Merge branch 'main' into ld_st_matrix_unroll_patterns
chencha3 442c18a
merge
chencha3 a368430
roll back unnecessary change
chencha3 3f5d692
add unit test
chencha3 a0512d9
address comments
chencha3 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
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
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
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 |
---|---|---|
|
@@ -682,13 +682,90 @@ struct UnrollUpdateOffsetOp : public UnrollPattern<xegpu::UpdateOffsetOp> { | |
} | ||
}; | ||
|
||
struct UnrollLoadMatrixOp : public UnrollPattern<xegpu::LoadMatrixOp> { | ||
using UnrollPattern<xegpu::LoadMatrixOp>::UnrollPattern; | ||
LogicalResult matchAndRewrite(xegpu::LoadMatrixOp op, | ||
PatternRewriter &rewriter) const override { | ||
Location loc = op.getLoc(); | ||
VectorType valueTy = op.getType(); | ||
std::optional<SmallVector<int64_t>> targetShape = getTargetShape(op); | ||
if (!targetShape || targetShape->size() != (size_t)valueTy.getRank()) | ||
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. nit: You might also need to check that original |
||
return failure(); | ||
|
||
Type elemTy = valueTy.getElementType(); | ||
ArrayRef<int64_t> shape = valueTy.getShape(); | ||
auto layout = dyn_cast<xegpu::LayoutAttr>(op.getLayoutAttr()); | ||
|
||
VectorType newValueTy = valueTy.cloneWith(*targetShape, elemTy); | ||
|
||
SmallVector<OpFoldResult> mixedOffsets = op.getMixedOffsets(); | ||
SmallVector<SmallVector<OpFoldResult>> offsetsList; | ||
for (SmallVector<int64_t> offsets : | ||
StaticTileOffsetRange(shape, *targetShape)) { | ||
adam-smnk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto adds = xegpu::addElementwise( | ||
rewriter, loc, mixedOffsets, | ||
getAsIndexOpFoldResult(op.getContext(), offsets)); | ||
offsetsList.push_back(adds); | ||
} | ||
|
||
SmallVector<Value> newOps; | ||
layout = layout.dropInstData(); | ||
for (SmallVector<OpFoldResult> offsets : offsetsList) { | ||
auto newOp = rewriter.create<xegpu::LoadMatrixOp>( | ||
op.getLoc(), newValueTy, op.getMemDesc(), offsets, layout); | ||
newOps.push_back(newOp); | ||
} | ||
Value castOp = unpack(newOps, op.getType(), *targetShape, loc, rewriter); | ||
rewriter.replaceOp(op, castOp); | ||
return success(); | ||
} | ||
}; | ||
|
||
struct UnrollStoreMatrixOp : public UnrollPattern<xegpu::StoreMatrixOp> { | ||
using UnrollPattern<xegpu::StoreMatrixOp>::UnrollPattern; | ||
LogicalResult matchAndRewrite(xegpu::StoreMatrixOp op, | ||
PatternRewriter &rewriter) const override { | ||
std::optional<SmallVector<int64_t>> targetShape = getTargetShape(op); | ||
if (!targetShape) | ||
return failure(); | ||
|
||
Location loc = op.getLoc(); | ||
VectorType valueTy = op.getData().getType(); | ||
ArrayRef<int64_t> shape = valueTy.getShape(); | ||
auto layout = dyn_cast<xegpu::LayoutAttr>(op.getLayoutAttr()); | ||
|
||
SmallVector<Type> convertedValTypes = | ||
getUnrolledTypes(valueTy, *targetShape); | ||
SmallVector<Value> convertedValues = | ||
pack(op.getData(), convertedValTypes, *targetShape, loc, rewriter); | ||
|
||
SmallVector<OpFoldResult> mixedOffsets = op.getMixedOffsets(); | ||
SmallVector<SmallVector<OpFoldResult>> offsetsList; | ||
for (SmallVector<int64_t> offsets : | ||
StaticTileOffsetRange(shape, *targetShape)) { | ||
auto adds = xegpu::addElementwise( | ||
rewriter, loc, mixedOffsets, | ||
getAsIndexOpFoldResult(op.getContext(), offsets)); | ||
offsetsList.push_back(adds); | ||
} | ||
|
||
for (auto [v, offsets] : llvm::zip_equal(convertedValues, offsetsList)) | ||
rewriter.create<xegpu::StoreMatrixOp>(loc, v, op.getMemDesc(), offsets, | ||
layout.dropInstData()); | ||
|
||
rewriter.eraseOp(op); | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::xegpu::populateXeGPUUnrollPatterns( | ||
RewritePatternSet &patterns, const xegpu::UnrollOptions &options) { | ||
patterns.add<UnrollCreateNdOp, UnrollUpdateNdOffsetOp, UnrollPrefetchNdOp, | ||
UnrollLoadNdOp, UnrollStoreNdOp, UnrollDpasOp, | ||
UnrollCreateDescOp, UnrollLoadGatherOp, UnrollStoreScatterOp, | ||
UnrollPrefetchOp, UnrollUpdateOffsetOp>(patterns.getContext(), | ||
options); | ||
patterns | ||
.add<UnrollCreateNdOp, UnrollUpdateNdOffsetOp, UnrollPrefetchNdOp, | ||
UnrollLoadNdOp, UnrollStoreNdOp, UnrollDpasOp, UnrollCreateDescOp, | ||
UnrollLoadGatherOp, UnrollStoreScatterOp, UnrollPrefetchOp, | ||
UnrollUpdateOffsetOp, UnrollLoadMatrixOp, UnrollStoreMatrixOp>( | ||
patterns.getContext(), options); | ||
} |
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
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
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.
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.
nit: does it use index dialect at all in the end?
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.
Yes, it is used in addElementwise, so the pass needs to load it.