-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR][XeGPU] Distribute load_gather/store_scatter op from Wg To Sg #154420
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
11 commits
Select commit
Hold shift + click to select a range
b94a37f
Add pattern for load_gather and store_scatter ops
nbpatel 4f490ef
Merge branch 'main' into xegpu-scatter-ops-backup
nbpatel 459e98a
Add tests
nbpatel e3c02a6
Merge branch 'main' into xegpu-scatter-ops
nbpatel a25c40d
Feedback
nbpatel bdbf14f
Cleanup
nbpatel c93090f
Add check
nbpatel a7b780d
Feedback
nbpatel 21f1f4f
Add check
nbpatel 73204b7
Merge branch 'main' into xegpu-scatter-ops
nbpatel efc211b
use updated api's for layout attr
nbpatel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -765,6 +765,110 @@ struct WgToSgArithConstantOp : public OpConversionPattern<arith::ConstantOp> { | |
| } | ||
| }; | ||
|
|
||
| // This pattern transforms the LoadGatherOp with explicit offsets to load | ||
| // subgroup data | ||
| struct WgToSgLoadGatherOpWithOffset | ||
| : public OpConversionPattern<xegpu::LoadGatherOp> { | ||
| using OpConversionPattern<xegpu::LoadGatherOp>::OpConversionPattern; | ||
| LogicalResult | ||
| matchAndRewrite(xegpu::LoadGatherOp op, OneToNOpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
|
|
||
| if (!op.getOffsets()) | ||
| return failure(); | ||
|
|
||
| Location loc = op.getLoc(); | ||
| VectorType resultType = dyn_cast<VectorType>(op.getResult().getType()); | ||
| if (!resultType) | ||
| return failure(); | ||
| ArrayRef<int64_t> wgShape = resultType.getShape(); | ||
|
|
||
| xegpu::DistributeLayoutAttr layout = | ||
| xegpu::getDistributeLayoutAttr(op.getResult()); | ||
| if (!layout || !layout.isForWorkgroup()) | ||
| return failure(); | ||
|
|
||
| SmallVector<int64_t> sgShape = getSgShapeAndCount(wgShape, layout).first; | ||
|
|
||
| // The offsets need to be distributed | ||
| auto offsetsVecType = | ||
| dyn_cast<VectorType>(adaptor.getOffsets().front().getType()); | ||
| auto maskVecType = | ||
| dyn_cast<VectorType>(adaptor.getMask().front().getType()); | ||
| if (!offsetsVecType || !maskVecType || | ||
| offsetsVecType.getShape() != maskVecType.getShape()) { | ||
| return rewriter.notifyMatchFailure(op, | ||
| "offsets have not been distributed"); | ||
| } | ||
|
|
||
| SmallVector<Value> newLoadOps; | ||
| auto chunkSizeAttr = | ||
| rewriter.getI64IntegerAttr(op.getChunkSize().value_or(1)); | ||
| VectorType newTy = VectorType::get(sgShape, resultType.getElementType()); | ||
| for (auto [offsets, mask] : | ||
| llvm::zip(adaptor.getOffsets(), adaptor.getMask())) { | ||
| auto newLoadOp = rewriter.create<xegpu::LoadGatherOp>( | ||
| loc, newTy, op.getSource(), offsets, mask, chunkSizeAttr, | ||
| op.getL1HintAttr(), op.getL2HintAttr(), op.getL3HintAttr()); | ||
| xegpu::setDistributeLayoutAttr(newLoadOp->getResult(0), | ||
| layout.dropSgLayoutAndData()); | ||
| newLoadOps.push_back(newLoadOp); | ||
| } | ||
| rewriter.replaceOpWithMultiple(op, {newLoadOps}); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| // This pattern transforms the StoreScatterOp with explicit offsets to store | ||
| // subgroup data | ||
| struct WgToSgStoreScatterOpWithOffset | ||
| : public OpConversionPattern<xegpu::StoreScatterOp> { | ||
| using OpConversionPattern<xegpu::StoreScatterOp>::OpConversionPattern; | ||
| LogicalResult | ||
| matchAndRewrite(xegpu::StoreScatterOp op, OneToNOpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
|
|
||
| if (!op.getOffsets()) | ||
| return failure(); | ||
|
|
||
| Location loc = op.getLoc(); | ||
| VectorType valueType = dyn_cast<VectorType>(op.getValue().getType()); | ||
| if (!valueType) | ||
| return failure(); | ||
|
|
||
| xegpu::DistributeLayoutAttr layout = | ||
| xegpu::getDistributeLayoutAttr(op.getValue()); | ||
| if (!layout || !layout.isForWorkgroup()) | ||
| return failure(); | ||
|
|
||
| // The offsets need to be distributed | ||
| auto offsetsVecType = | ||
| dyn_cast<VectorType>(adaptor.getOffsets().front().getType()); | ||
| auto maskVecType = | ||
| dyn_cast<VectorType>(adaptor.getMask().front().getType()); | ||
| if (!offsetsVecType || !maskVecType || | ||
| offsetsVecType.getShape() != maskVecType.getShape()) { | ||
| return rewriter.notifyMatchFailure(op, | ||
| "offsets have not been distributed"); | ||
| } | ||
|
|
||
| auto chunkSizeOpt = op.getChunkSize(); | ||
| int64_t chunkSize = chunkSizeOpt ? static_cast<int64_t>(*chunkSizeOpt) : 1; | ||
| auto chunkSizeAttr = rewriter.getI64IntegerAttr(chunkSize); | ||
| for (auto [val, offs, mask] : llvm::zip( | ||
|
Contributor
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. same check for offsets as above. |
||
| adaptor.getValue(), adaptor.getOffsets(), adaptor.getMask())) { | ||
| rewriter.create<xegpu::StoreScatterOp>( | ||
| loc, val, op.getDest(), offs, mask, chunkSizeAttr, op.getL1HintAttr(), | ||
| op.getL2HintAttr(), op.getL3HintAttr()); | ||
| // Update the layout attribute to drop sg_layout and sg_data. | ||
| if (auto newLayout = layout.dropSgLayoutAndData()) | ||
| op->setAttr("layout", newLayout); | ||
| } | ||
| rewriter.eraseOp(op); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct WgToSgLoadMatrixOp : public OpConversionPattern<xegpu::LoadMatrixOp> { | ||
| using OpConversionPattern<xegpu::LoadMatrixOp>::OpConversionPattern; | ||
| LogicalResult | ||
|
|
@@ -826,8 +930,9 @@ void populateXeGPUWgToSgDistributePatterns(RewritePatternSet &patterns) { | |
| WgToSgUpdateNdOffsetOp, WgToSgDpasOp, WgToSgPrefetchNdOp, | ||
| WgToSgPrefetchNdOpWithOffset, UnrealizedConversionCastOpPattern, | ||
| WgToSgElementwiseOp, WgToSgVectorBroadcastOp, WgToSgConvertLayoutOp, | ||
| WgToSgArithConstantOp, WgToSgLoadMatrixOp, WgToSgStoreMatrixOp>( | ||
| patterns.getContext()); | ||
| WgToSgArithConstantOp, WgToSgLoadGatherOpWithOffset, | ||
| WgToSgStoreScatterOpWithOffset, WgToSgLoadMatrixOp, | ||
| WgToSgStoreMatrixOp>(patterns.getContext()); | ||
| } | ||
| } // namespace xegpu | ||
| } // namespace mlir | ||
|
|
@@ -952,6 +1057,21 @@ void XeGPUWgToSgDistributePass::runOnOperation() { | |
| return isLegal(xegpu::getDistributeLayoutAttr(op.getResult())); | ||
| }); | ||
|
|
||
| target.addDynamicallyLegalOp<xegpu::LoadGatherOp>( | ||
| [=](xegpu::LoadGatherOp op) -> bool { | ||
| auto layout = xegpu::getDistributeLayoutAttr(op.getResult()); | ||
| return isLegal(layout); | ||
| }); | ||
|
|
||
| target.addDynamicallyLegalOp<xegpu::StoreScatterOp>( | ||
| [=](xegpu::StoreScatterOp op) -> bool { | ||
| // Check if the layout attribute is present on the result. | ||
| auto layout = op->getAttrOfType<xegpu::LayoutAttr>("layout"); | ||
| if (!layout) | ||
| return true; | ||
| return isLegal(layout); | ||
| }); | ||
|
|
||
| target.addDynamicallyLegalOp<vector::BroadcastOp>( | ||
| [=](vector::BroadcastOp op) -> bool { | ||
| return isLegal(xegpu::getDistributeLayoutAttr(op.getResult())); | ||
|
|
||
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: Here the code assums the offset has been distributed by its defining op. It is not always true currently,
e.g., the offsets is from an function parameter, or non-splat arith constant. Thus, it would be better to check
whether the offsets has been distributed yet.
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.
+1, could you add a test for this?
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.
with the current design, its not possible to add negative tests