-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR][XeGPU] Distribute non-splat constant from wg to sg #161416
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
+223
−15
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6712150
Add support for non splatable constant
nbpatel 7d3746a
Support 1:N conversion
nbpatel 1b00dc7
All cases work
nbpatel 512478b
Merge branch 'main' into xegpu-wg-sg-constant
nbpatel 1381174
Fix CHECKS
nbpatel e77eddd
Support 2D case
nbpatel 1b779b7
Add 2D test case
nbpatel 1b8db0e
Clean up
nbpatel fabb419
Clean up
nbpatel 2c81dee
Refactor
nbpatel 29d3f45
Add test
nbpatel ff1bb3b
Merge branch 'main' into xegpu-wg-sg-constant
nbpatel 77afdcc
address comment
nbpatel bbb427a
Merge branch 'main' into xegpu-wg-sg-constant
nbpatel e10d769
remove unused variable
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 |
---|---|---|
|
@@ -720,7 +720,7 @@ struct WgToSgArithConstantOp : public OpConversionPattern<arith::ConstantOp> { | |
ConversionPatternRewriter &rewriter) const override { | ||
auto vecAttr = dyn_cast<DenseElementsAttr>(op.getValue()); | ||
auto vecType = dyn_cast<VectorType>(op.getType()); | ||
if (!vecAttr || !vecAttr.isSplat() || !vecType) | ||
if (!vecAttr || !vecType) | ||
return failure(); | ||
|
||
xegpu::DistributeLayoutAttr layout = | ||
|
@@ -733,22 +733,139 @@ struct WgToSgArithConstantOp : public OpConversionPattern<arith::ConstantOp> { | |
int count; | ||
std::tie(sgShape, count) = getSgShapeAndCount(wgShape, layout); | ||
|
||
// Current limitation: constant of vector with single value. | ||
// TODO: support more complex cases, e.g., vector with multiple values. | ||
Attribute singleVal = vecAttr.getSplatValue<Attribute>(); | ||
|
||
auto newType = VectorType::get(sgShape, vecType.getElementType()); | ||
auto sgAttr = DenseElementsAttr::get(newType, singleVal); | ||
auto cstOp = | ||
arith::ConstantOp::create(rewriter, op.getLoc(), newType, sgAttr); | ||
if (!layout.getEffectiveLaneLayoutAsInt().empty() || | ||
!layout.getEffectiveInstDataAsInt().empty()) | ||
xegpu::setDistributeLayoutAttr(cstOp->getResult(0), | ||
layout.dropSgLayoutAndData()); | ||
SmallVector<Value> newConsts(count, cstOp); | ||
Location loc = op.getLoc(); | ||
auto eltType = vecType.getElementType(); | ||
|
||
rewriter.replaceOpWithMultiple(op, {newConsts}); | ||
return success(); | ||
auto setLayoutIfNeeded = [&](Value val) { | ||
if (!layout.getEffectiveLaneLayoutAsInt().empty() || | ||
!layout.getEffectiveInstDataAsInt().empty()) { | ||
xegpu::setDistributeLayoutAttr(llvm::dyn_cast<OpResult>(val), | ||
layout.dropSgLayoutAndData()); | ||
} | ||
}; | ||
|
||
if (vecAttr.isSplat()) { | ||
// Splat: single value for all subgroups | ||
Attribute singleVal = vecAttr.getSplatValue<Attribute>(); | ||
auto sgAttr = DenseElementsAttr::get(newType, singleVal); | ||
auto cstOp = arith::ConstantOp::create(rewriter, loc, newType, sgAttr); | ||
setLayoutIfNeeded(cstOp->getResult(0)); | ||
rewriter.replaceOp(op, cstOp); | ||
return success(); | ||
} else if (sgShape == wgShape) { // if the entire vector is shared by all | ||
// subgroups, don't distribute | ||
auto newConstOp = | ||
arith::ConstantOp::create(rewriter, op.getLoc(), vecType, vecAttr); | ||
setLayoutIfNeeded(newConstOp->getResult(0)); | ||
rewriter.replaceOp(op, newConstOp); | ||
return success(); | ||
} else { | ||
// Non-splat constant | ||
// Only supports 1D & 2D | ||
// TODO: support other cases that require SLM access | ||
if (!eltType.isIndex()) | ||
return rewriter.notifyMatchFailure( | ||
op, "Unsupported element type for non-splat constant op."); | ||
|
||
if (wgShape.size() > 2) | ||
return rewriter.notifyMatchFailure( | ||
op, "Only 1D & 2D vector constant supported"); | ||
|
||
SmallVector<Attribute> values(vecAttr.getValues<Attribute>()); | ||
int64_t rowStride = 0, colStride = 0; | ||
int64_t rows = wgShape.size() == 1 ? 1 : wgShape[0]; | ||
int64_t cols = wgShape.size() == 1 ? wgShape[0] : wgShape[1]; | ||
|
||
// Compute colStride and rowStride, and check for constant strides. | ||
if (cols > 1) { | ||
colStride = cast<IntegerAttr>(values[1]).getInt() - | ||
cast<IntegerAttr>(values[0]).getInt(); | ||
} | ||
if (rows > 1) { | ||
rowStride = cast<IntegerAttr>(values[cols]).getInt() - | ||
cast<IntegerAttr>(values[0]).getInt(); | ||
} | ||
|
||
for (int64_t r = 0; r < rows; ++r) { | ||
for (int64_t c = 0; c < cols; ++c) { | ||
int64_t idx = r * cols + c; | ||
// Check column stride | ||
if (c > 0 && cols > 1) { | ||
int64_t prevIdx = r * cols + (c - 1); | ||
int64_t diff = cast<IntegerAttr>(values[idx]).getInt() - | ||
cast<IntegerAttr>(values[prevIdx]).getInt(); | ||
if (diff != colStride) | ||
return rewriter.notifyMatchFailure( | ||
op, "Non-constant column stride in constant op."); | ||
} | ||
// Check row stride | ||
if (r > 0 && rows > 1) { | ||
int64_t prevIdx = (r - 1) * cols + c; | ||
int64_t diff = cast<IntegerAttr>(values[idx]).getInt() - | ||
cast<IntegerAttr>(values[prevIdx]).getInt(); | ||
if (diff != rowStride) | ||
return rewriter.notifyMatchFailure( | ||
op, "Non-constant row stride in constant op."); | ||
} | ||
} | ||
} | ||
|
||
// Create a constant for the base tile. | ||
// For 2D case, extract the top-left sgShape[0] x sgShape[1] submatrix. | ||
// For 1D case, extract the first sgShape[0] elements. | ||
SmallVector<Attribute> baseTileValues; | ||
int baseTileCols = sgShape[sgShape.size() - 1]; | ||
int64_t baseTileRows = sgShape.size() == 1 ? 1 : sgShape[0]; | ||
for (int64_t r = 0; r < baseTileRows; ++r) { | ||
for (int64_t c = 0; c < baseTileCols; ++c) { | ||
baseTileValues.push_back(values[r * cols + c]); | ||
} | ||
} | ||
|
||
auto tileAttr = DenseElementsAttr::get(VectorType::get(sgShape, eltType), | ||
baseTileValues); | ||
auto baseConstVec = rewriter.create<arith::ConstantOp>(loc, tileAttr); | ||
|
||
// Get subgroup id | ||
Value sgId = | ||
gpu::SubgroupIdOp::create(rewriter, loc, /*upper_bound=*/nullptr); | ||
|
||
auto sgOffsets = layout.getOffsets(rewriter, loc, sgId, wgShape); | ||
if (failed(sgOffsets)) | ||
return failure(); | ||
|
||
SmallVector<Value, 2> strideConsts; | ||
strideConsts.push_back( | ||
rewriter.create<arith::ConstantIndexOp>(loc, colStride)); | ||
if (rows > 1) | ||
strideConsts.insert( | ||
strideConsts.begin(), | ||
rewriter.create<arith::ConstantIndexOp>(loc, rowStride)); | ||
|
||
SmallVector<Value> newConstOps; | ||
for (auto offsets : *sgOffsets) { | ||
// Multiply offset with stride, broadcast it and add to baseConstVec | ||
Value mulOffset = rewriter.create<arith::ConstantIndexOp>(loc, 0); | ||
for (size_t i = 0; i < strideConsts.size(); ++i) { | ||
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: The later AddIOp can be merged into this loop. |
||
Value mul = rewriter.create<arith::MulIOp>( | ||
loc, rewriter.getIndexType(), offsets[i], strideConsts[i]); | ||
mulOffset = rewriter.create<arith::AddIOp>( | ||
loc, rewriter.getIndexType(), mulOffset, mul); | ||
} | ||
// Broadcast to baseConstVec size | ||
auto bcastOffset = rewriter.create<vector::BroadcastOp>( | ||
loc, baseConstVec.getType(), mulOffset); | ||
auto finalConst = | ||
arith::AddIOp::create(rewriter, loc, baseConstVec, bcastOffset); | ||
setLayoutIfNeeded(baseConstVec); | ||
setLayoutIfNeeded(bcastOffset); | ||
setLayoutIfNeeded(finalConst); | ||
newConstOps.push_back(finalConst); | ||
} | ||
rewriter.replaceOpWithMultiple(op, {newConstOps}); | ||
return success(); | ||
} | ||
} | ||
}; | ||
|
||
|
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.
Uh oh!
There was an error while loading. Please reload this page.