-
Notifications
You must be signed in to change notification settings - Fork 17.3k
[MLIR][XeGPU] Add distribution pattern for xegpu load & store matrix from sg to wi #183179
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
6 commits
Select commit
Hold shift + click to select a range
3681387
Add distribution patterns for load & store matrix
nbpatel 284e96b
clang-format
nbpatel 01cc047
clean up
nbpatel e0f7430
Merge branch 'main' into xegpu-load-store-matrix
nbpatel 2dd43ca
Use CHECK-DAG
nbpatel a30faa6
Merge branch 'main' into xegpu-load-store-matrix
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #include "mlir/Dialect/GPU/IR/GPUDialect.h" | ||
| #include "mlir/Dialect/Index/IR/IndexDialect.h" | ||
| #include "mlir/Dialect/Math/IR/Math.h" | ||
| #include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
|
|
@@ -597,6 +598,137 @@ struct SgToWiMultiDimReduction | |
| } | ||
| }; | ||
|
|
||
| /// Helper to compute distributed coordinates for matrix ops. | ||
| /// When not using subgroup_block_io, each workitem computes its own | ||
| /// coordinates based on the layout and lane ID. | ||
| static SmallVector<Value> computeDistributedCoordsForMatrixOp( | ||
| ConversionPatternRewriter &rewriter, Location loc, | ||
| xegpu::DistributeLayoutAttr layout, ArrayRef<int64_t> payloadShape, | ||
| ValueRange origOffsets) { | ||
| Value laneId = gpu::LaneIdOp::create(rewriter, loc, rewriter.getIndexType(), | ||
| /*upperBound=*/mlir::IntegerAttr()); | ||
| auto maybeCoords = | ||
| layout.computeDistributedCoords(rewriter, loc, laneId, payloadShape); | ||
| if (failed(maybeCoords)) | ||
| return {}; | ||
| assert(maybeCoords.value().size() == 1 && | ||
| "Expected one set of distributed offsets"); | ||
| SmallVector<OpFoldResult> ofrVec = xegpu::addWithRightAligned( | ||
| rewriter, loc, getAsOpFoldResult(maybeCoords.value()[0]), | ||
| getAsOpFoldResult(origOffsets)); | ||
| return llvm::map_to_vector(ofrVec, llvm::CastTo<Value>); | ||
| } | ||
|
|
||
| /// This pattern distributes a subgroup-level LoadMatrix op to workitem-level. | ||
| struct SgToWiLoadMatrix : public OpConversionPattern<xegpu::LoadMatrixOp> { | ||
| using OpConversionPattern<xegpu::LoadMatrixOp>::OpConversionPattern; | ||
|
|
||
| LogicalResult | ||
| matchAndRewrite(xegpu::LoadMatrixOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
| auto layout = op.getLayoutAttr(); | ||
| // If no layout, nothing to do. | ||
| if (!layout) | ||
|
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. I wonder if we could generalize it to a legality check for |
||
| return failure(); | ||
|
|
||
| VectorType sgPayloadTy = dyn_cast<VectorType>(op.getResult().getType()); | ||
| if (!sgPayloadTy) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "the matrix op payload must be a vector type"); | ||
|
|
||
| auto loc = op.getLoc(); | ||
| auto offsets = op.getMixedOffsets(); | ||
| if (offsets.empty()) | ||
| return rewriter.notifyMatchFailure(op, "the load op must have offsets"); | ||
|
|
||
| FailureOr<VectorType> distPayloadTyOrFailure = | ||
| getDistVecTypeBasedOnLaneLayout(layout, sgPayloadTy); | ||
| if (failed(distPayloadTyOrFailure)) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Failed to distribute matrix op payload based on layout."); | ||
|
|
||
| SmallVector<Value> offsetsAsValues = | ||
| vector::getAsValues(rewriter, loc, offsets); | ||
|
|
||
| SmallVector<Value> newCoords = offsetsAsValues; | ||
| if (!op.getSubgroupBlockIoAttr()) { | ||
| newCoords = computeDistributedCoordsForMatrixOp( | ||
| rewriter, loc, layout, sgPayloadTy.getShape(), offsetsAsValues); | ||
| if (newCoords.empty()) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Failed to compute distributed coordinates."); | ||
| } | ||
|
|
||
| SmallVector<int64_t> newConstOffsets(op.getConstOffsets().size(), | ||
| ShapedType::kDynamic); | ||
| DenseI64ArrayAttr newConstOffsetsAttr = | ||
| rewriter.getDenseI64ArrayAttr(newConstOffsets); | ||
|
|
||
| auto newOp = xegpu::LoadMatrixOp::create( | ||
| rewriter, loc, *distPayloadTyOrFailure, adaptor.getMemDesc(), | ||
| ValueRange(newCoords), newConstOffsetsAttr, op.getSubgroupBlockIoAttr(), | ||
| xegpu::DistributeLayoutAttr{}); | ||
| rewriter.replaceOp(op, newOp.getResult()); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| /// This pattern distributes a subgroup-level StoreMatrix op to workitem-level. | ||
| struct SgToWiStoreMatrix : public OpConversionPattern<xegpu::StoreMatrixOp> { | ||
| using OpConversionPattern<xegpu::StoreMatrixOp>::OpConversionPattern; | ||
|
|
||
| LogicalResult | ||
| matchAndRewrite(xegpu::StoreMatrixOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
| auto layout = op.getLayoutAttr(); | ||
| // If no layout, nothing to do. | ||
| if (!layout) | ||
| return failure(); | ||
|
|
||
| VectorType sgPayloadTy = dyn_cast<VectorType>(op.getData().getType()); | ||
| if (!sgPayloadTy) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "the matrix op payload must be a vector type"); | ||
|
|
||
| auto loc = op.getLoc(); | ||
| auto offsets = op.getMixedOffsets(); | ||
| if (offsets.empty()) | ||
| return rewriter.notifyMatchFailure(op, "the store op must have offsets"); | ||
|
|
||
| FailureOr<VectorType> distPayloadTyOrFailure = | ||
| getDistVecTypeBasedOnLaneLayout(layout, sgPayloadTy); | ||
| if (failed(distPayloadTyOrFailure)) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Failed to distribute matrix op payload based on layout."); | ||
|
|
||
| SmallVector<Value> offsetsAsValues = | ||
| vector::getAsValues(rewriter, loc, offsets); | ||
|
|
||
| SmallVector<Value> newCoords = offsetsAsValues; | ||
| if (!op.getSubgroupBlockIoAttr()) { | ||
| newCoords = computeDistributedCoordsForMatrixOp( | ||
| rewriter, loc, layout, sgPayloadTy.getShape(), offsetsAsValues); | ||
| if (newCoords.empty()) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Failed to compute distributed coordinates."); | ||
| } | ||
|
|
||
| SmallVector<int64_t> newConstOffsets(op.getConstOffsets().size(), | ||
| ShapedType::kDynamic); | ||
| DenseI64ArrayAttr newConstOffsetsAttr = | ||
| rewriter.getDenseI64ArrayAttr(newConstOffsets); | ||
|
|
||
| xegpu::StoreMatrixOp::create( | ||
| rewriter, loc, TypeRange{}, | ||
| castValueTo(rewriter, cast<TypedValue<VectorType>>(adaptor.getData()), | ||
| distPayloadTyOrFailure.value()), | ||
| adaptor.getMemDesc(), ValueRange(newCoords), newConstOffsetsAttr, | ||
| op.getSubgroupBlockIoAttr(), xegpu::DistributeLayoutAttr{}); | ||
| rewriter.eraseOp(op); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| /// Distributes a subgroup-level StoreScatter (xegpu.store) op to | ||
| /// workitem-level. | ||
| /// | ||
|
|
@@ -901,5 +1033,6 @@ void xegpu::populateXeGPUSgToWiDistributeTypeConversionAndLegality( | |
| patterns.add<SgToWiCreateNdDesc, SgToWiLoadNd, SgToWiStoreNd, SgToWiDpas, | ||
| SgToWiElementWise, SgToWiArithConstant, SgToWiPrefetchNd, | ||
| SgToWiLoadGather, SgToWiStoreScatter, SgToWiVectorReduction, | ||
| SgToWiMultiDimReduction>(typeConverter, patterns.getContext()); | ||
| SgToWiMultiDimReduction, SgToWiLoadMatrix, SgToWiStoreMatrix>( | ||
| typeConverter, patterns.getContext()); | ||
| } | ||
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.
If this is just a copy & paste of computeDistributedCoordinatesForMatrixOp(), please move it to XeGPULayoutImpl
Uh oh!
There was an error while loading. Please reload this page.
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, but the other one (in the old pass) will be removed eventually once we move to this pass
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.
@Jianhui-Li are you ok with this?