-
Notifications
You must be signed in to change notification settings - Fork 15k
[MLIR][XeGPU][VectorToXeGPU] Add lowering from vector.gather/scatter to xegpu.load/store #158024
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
+592
−11
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d97a3c2
[MLIR][XeGPU] Add lowering from vector.gather/scatter to xegpu.load/s…
dchigarev 4d2c284
Add alignment handling
dchigarev 62c5c38
Align lowering with new utils behavior
dchigarev 78b057d
Handle non-unit inner stride
dchigarev 4ce2415
Correct memref.subview test
dchigarev eb5fbc7
Remove non-necessary comment
dchigarev 54a8299
Remove 'gatherScatterPreconditions' function
dchigarev 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 |
|---|---|---|
|
|
@@ -183,11 +183,15 @@ static void adjustStridesForPermutation(AffineMap permMap, | |
| // Computes memory strides and a memref offset for vector transfer operations, | ||
| // handling both static and dynamic memrefs while applying permutation | ||
| // transformations for XeGPU lowering. | ||
| template < | ||
| typename OpType, | ||
| typename = std::enable_if_t<llvm::is_one_of< | ||
| std::decay_t<OpType>, vector::TransferReadOp, vector::TransferWriteOp, | ||
| vector::GatherOp, vector::ScatterOp>::value>> | ||
| static std::pair<SmallVector<Value>, Value> | ||
| computeMemrefMeta(VectorTransferOpInterface xferOp, PatternRewriter &rewriter) { | ||
| computeMemrefMeta(OpType xferOp, PatternRewriter &rewriter) { | ||
| SmallVector<Value> strides; | ||
| Value baseMemref = xferOp.getBase(); | ||
| AffineMap permMap = xferOp.getPermutationMap(); | ||
| MemRefType memrefType = dyn_cast<MemRefType>(baseMemref.getType()); | ||
|
|
||
| Location loc = xferOp.getLoc(); | ||
|
|
@@ -197,9 +201,14 @@ computeMemrefMeta(VectorTransferOpInterface xferOp, PatternRewriter &rewriter) { | |
| SmallVector<int64_t> intStrides; | ||
| if (failed(memrefType.getStridesAndOffset(intStrides, offset))) | ||
| return {{}, offsetVal}; | ||
| // Wrap static strides as MLIR values | ||
| for (int64_t s : intStrides) | ||
| strides.push_back(arith::ConstantIndexOp::create(rewriter, loc, s)); | ||
| bool hasDynamicStrides = llvm::any_of(intStrides, [](int64_t strideVal) { | ||
| return ShapedType::isDynamic(strideVal); | ||
| }); | ||
|
|
||
| if (!hasDynamicStrides) | ||
| for (int64_t s : intStrides) | ||
| strides.push_back(arith::ConstantIndexOp::create(rewriter, loc, s)); | ||
|
|
||
| if (!ShapedType::isDynamic(offset)) | ||
| offsetVal = arith::ConstantIndexOp::create(rewriter, loc, offset); | ||
| } | ||
|
|
@@ -232,8 +241,14 @@ computeMemrefMeta(VectorTransferOpInterface xferOp, PatternRewriter &rewriter) { | |
| if (!offsetVal) | ||
| offsetVal = meta.getOffset(); | ||
| } | ||
| // Adjust strides according to the permutation map (e.g., for transpose) | ||
| adjustStridesForPermutation(permMap, strides); | ||
|
|
||
| if constexpr (llvm::is_one_of<std::decay_t<OpType>, vector::TransferReadOp, | ||
| vector::TransferWriteOp>::value) { | ||
| AffineMap permMap = xferOp.getPermutationMap(); | ||
| // Adjust strides according to the permutation map (e.g., for transpose) | ||
| adjustStridesForPermutation(permMap, strides); | ||
| } | ||
|
|
||
| return {strides, offsetVal}; | ||
| } | ||
|
|
||
|
|
@@ -339,9 +354,51 @@ static Value computeOffsets(VectorTransferOpInterface xferOp, | |
| return localOffsets; | ||
| } | ||
|
|
||
| // Compute the element-wise offsets for vector.gather or vector.scatter ops. | ||
| // | ||
| // This function linearizes the base offsets of the gather/scatter operation | ||
| // and combines them with the per-element indices to produce a final vector of | ||
| // memory offsets. | ||
| template < | ||
| typename OpType, | ||
| typename = std::enable_if_t<llvm::is_one_of< | ||
| std::decay_t<OpType>, vector::GatherOp, vector::ScatterOp>::value>> | ||
| static Value computeOffsets(PatternRewriter &rewriter, OpType gatScatOp, | ||
| ArrayRef<Value> strides, Value baseOffset) { | ||
| Location loc = gatScatOp.getLoc(); | ||
| SmallVector<Value> offsets = gatScatOp.getOffsets(); | ||
| for (size_t i = 0; i < offsets.size(); ++i) { | ||
| Value offsetContrib = | ||
| arith::MulIOp::create(rewriter, loc, offsets[i], strides[i]); | ||
| baseOffset = | ||
| arith::AddIOp::create(rewriter, loc, baseOffset, offsetContrib); | ||
| } | ||
| Value indices = gatScatOp.getIndices(); | ||
|
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 think these indices need to multiple with stride of innermost dim, if we allow the non-unit innermost dim stride. I believe this is the only change we need to support it. |
||
| VectorType vecType = cast<VectorType>(indices.getType()); | ||
|
|
||
| Value strideVector = | ||
| vector::BroadcastOp::create(rewriter, loc, vecType, strides.back()) | ||
| .getResult(); | ||
| Value stridedIndices = | ||
| arith::MulIOp::create(rewriter, loc, strideVector, indices).getResult(); | ||
|
|
||
| Value baseVector = | ||
| vector::BroadcastOp::create( | ||
| rewriter, loc, | ||
| VectorType::get(vecType.getShape(), rewriter.getIndexType()), | ||
| baseOffset) | ||
| .getResult(); | ||
| return arith::AddIOp::create(rewriter, loc, baseVector, stridedIndices) | ||
| .getResult(); | ||
| } | ||
|
|
||
| template < | ||
| typename OpType, | ||
| typename = std::enable_if_t<llvm::is_one_of< | ||
| std::decay_t<OpType>, vector::TransferReadOp, vector::TransferWriteOp, | ||
| vector::GatherOp, vector::ScatterOp>::value>> | ||
| // Convert memref to i64 base pointer | ||
| static Value memrefToIndexPtr(VectorTransferOpInterface xferOp, | ||
| PatternRewriter &rewriter) { | ||
| static Value memrefToIndexPtr(OpType xferOp, PatternRewriter &rewriter) { | ||
| Location loc = xferOp.getLoc(); | ||
| auto indexPtr = memref::ExtractAlignedPointerAsIndexOp::create( | ||
| rewriter, loc, xferOp.getBase()) | ||
|
|
@@ -539,6 +596,71 @@ struct TransferWriteLowering | |
| } | ||
| }; | ||
|
|
||
| struct GatherLowering : public OpRewritePattern<vector::GatherOp> { | ||
| using OpRewritePattern<vector::GatherOp>::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(vector::GatherOp gatherOp, | ||
| PatternRewriter &rewriter) const override { | ||
| auto srcTy = dyn_cast<MemRefType>(gatherOp.getBase().getType()); | ||
| if (!srcTy) | ||
| return rewriter.notifyMatchFailure(gatherOp, "Expects memref source"); | ||
|
|
||
| Location loc = gatherOp.getLoc(); | ||
| VectorType vectorType = gatherOp.getVectorType(); | ||
|
|
||
| auto meta = computeMemrefMeta(gatherOp, rewriter); | ||
| if (meta.first.empty()) | ||
| return rewriter.notifyMatchFailure(gatherOp, "Failed to compute strides"); | ||
|
|
||
| Value localOffsets = | ||
| computeOffsets(rewriter, gatherOp, meta.first, meta.second); | ||
| Value flatMemref = memrefToIndexPtr(gatherOp, rewriter); | ||
|
|
||
| auto xeGatherOp = xegpu::LoadGatherOp::create( | ||
| rewriter, loc, vectorType, flatMemref, localOffsets, gatherOp.getMask(), | ||
| /*chunk_size=*/IntegerAttr{}, | ||
| /*l1_hint=*/xegpu::CachePolicyAttr{}, | ||
| /*l2_hint=*/xegpu::CachePolicyAttr{}, | ||
| /*l3_hint=*/xegpu::CachePolicyAttr{}); | ||
|
|
||
| auto selectOp = | ||
| arith::SelectOp::create(rewriter, loc, gatherOp.getMask(), | ||
| xeGatherOp.getResult(), gatherOp.getPassThru()); | ||
| rewriter.replaceOp(gatherOp, selectOp.getResult()); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct ScatterLowering : public OpRewritePattern<vector::ScatterOp> { | ||
| using OpRewritePattern<vector::ScatterOp>::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(vector::ScatterOp scatterOp, | ||
| PatternRewriter &rewriter) const override { | ||
| auto srcTy = dyn_cast<MemRefType>(scatterOp.getBase().getType()); | ||
| if (!srcTy) | ||
| return rewriter.notifyMatchFailure(scatterOp, "Expects memref source"); | ||
|
|
||
| Location loc = scatterOp.getLoc(); | ||
| auto meta = computeMemrefMeta(scatterOp, rewriter); | ||
| if (meta.first.empty()) | ||
| return rewriter.notifyMatchFailure(scatterOp, | ||
| "Failed to compute strides"); | ||
|
|
||
| Value localOffsets = | ||
| computeOffsets(rewriter, scatterOp, meta.first, meta.second); | ||
| Value flatMemref = memrefToIndexPtr(scatterOp, rewriter); | ||
|
|
||
| xegpu::StoreScatterOp::create(rewriter, loc, scatterOp.getValueToStore(), | ||
| flatMemref, localOffsets, scatterOp.getMask(), | ||
| /*chunk_size=*/IntegerAttr{}, | ||
| /*l1_hint=*/xegpu::CachePolicyAttr{}, | ||
| /*l2_hint=*/xegpu::CachePolicyAttr{}, | ||
| /*l3_hint=*/xegpu::CachePolicyAttr{}); | ||
| rewriter.eraseOp(scatterOp); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct LoadLowering : public OpRewritePattern<vector::LoadOp> { | ||
| using OpRewritePattern<vector::LoadOp>::OpRewritePattern; | ||
|
|
||
|
|
@@ -654,6 +776,8 @@ struct ConvertVectorToXeGPUPass | |
|
|
||
| void mlir::populateVectorToXeGPUConversionPatterns( | ||
| RewritePatternSet &patterns) { | ||
| patterns.add<TransferReadLowering, TransferWriteLowering, LoadLowering, | ||
| StoreLowering, ContractionLowering>(patterns.getContext()); | ||
| patterns | ||
| .add<TransferReadLowering, TransferWriteLowering, LoadLowering, | ||
| ScatterLowering, GatherLowering, StoreLowering, ContractionLowering>( | ||
| patterns.getContext()); | ||
| } | ||
Oops, something went wrong.
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.
The permutation map could exist for the gather/store's memref, and I don't understand why we should treat them differently here.
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.
here we're accessing a permutation map of the operation (transfer_read/write).
vector.gather/scatterdon't have a permutation map, that's why we're skipping it in that caseIf a memref has its own permutation this should be handled automatically by
memref.extract_strided_metadata