-
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
Changes from 5 commits
d97a3c2
4d2c284
62c5c38
78b057d
4ce2415
eb5fbc7
54a8299
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 |
|---|---|---|
|
|
@@ -97,6 +97,17 @@ static LogicalResult transferPreconditions(PatternRewriter &rewriter, | |
| return success(); | ||
| } | ||
|
|
||
| // Common preconditions for the lowering of vector.gather and vector.scatter: | ||
| // 1. Source is a memref. | ||
| static LogicalResult gatherScatterPreconditions(PatternRewriter &rewriter, | ||
dchigarev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
adam-smnk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
dchigarev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Operation *op, Type baseType) { | ||
| auto srcTy = dyn_cast<MemRefType>(baseType); | ||
| if (!srcTy) | ||
| return rewriter.notifyMatchFailure(op, "Expects memref source"); | ||
|
|
||
| return success(); | ||
| } | ||
|
|
||
| static xegpu::CreateNdDescOp | ||
| createNdDescriptor(PatternRewriter &rewriter, Location loc, | ||
| xegpu::TensorDescType descType, TypedValue<MemRefType> src, | ||
|
|
@@ -183,11 +194,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 +212,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 +252,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(); | ||
|
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. 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 commentThe 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). If a memref has its own permutation this should be handled automatically by |
||
| // Adjust strides according to the permutation map (e.g., for transpose) | ||
| adjustStridesForPermutation(permMap, strides); | ||
| } | ||
|
|
||
| return {strides, offsetVal}; | ||
| } | ||
|
|
||
|
|
@@ -339,9 +365,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 +607,71 @@ struct TransferWriteLowering | |
| } | ||
| }; | ||
|
|
||
| struct GatherLowering : public OpRewritePattern<vector::GatherOp> { | ||
| using OpRewritePattern<vector::GatherOp>::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(vector::GatherOp gatherOp, | ||
| PatternRewriter &rewriter) const override { | ||
| if (failed(gatherScatterPreconditions(rewriter, gatherOp, | ||
| gatherOp.getBase().getType()))) | ||
| return failure(); | ||
|
|
||
| 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 { | ||
| if (failed(gatherScatterPreconditions(rewriter, scatterOp, | ||
| scatterOp.getBase().getType()))) | ||
| return failure(); | ||
|
|
||
| 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 +787,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()); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.