-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[mlir][XeGPU] add unroll patterns for load_matrix and store_matrix #154637
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 7 commits
22d4193
d20da85
87e4fd2
b3af260
442c18a
a368430
3f5d692
a0512d9
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 |
---|---|---|
|
@@ -682,13 +682,90 @@ struct UnrollUpdateOffsetOp : public UnrollPattern<xegpu::UpdateOffsetOp> { | |
} | ||
}; | ||
|
||
struct UnrollLoadMatrixOp : public UnrollPattern<xegpu::LoadMatrixOp> { | ||
using UnrollPattern<xegpu::LoadMatrixOp>::UnrollPattern; | ||
LogicalResult matchAndRewrite(xegpu::LoadMatrixOp op, | ||
PatternRewriter &rewriter) const override { | ||
std::optional<SmallVector<int64_t>> targetShape = getTargetShape(op); | ||
if (!targetShape) | ||
return failure(); | ||
|
||
Location loc = op.getLoc(); | ||
VectorType valueTy = op.getType(); | ||
Type elemTy = valueTy.getElementType(); | ||
ArrayRef<int64_t> shape = valueTy.getShape(); | ||
auto layout = dyn_cast<xegpu::LayoutAttr>(op.getLayoutAttr()); | ||
|
||
VectorType newValueTy = valueTy.cloneWith(*targetShape, elemTy); | ||
|
||
SmallVector<OpFoldResult> mixedOffsets = op.getMixedOffsets(); | ||
SmallVector<SmallVector<OpFoldResult>> offsetsList; | ||
for (SmallVector<int64_t> offsets : | ||
StaticTileOffsetRange(shape, *targetShape)) { | ||
adam-smnk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto adds = xegpu::addWithRightAligned( | ||
rewriter, loc, mixedOffsets, | ||
getAsIndexOpFoldResult(op.getContext(), offsets)); | ||
offsetsList.push_back(adds); | ||
} | ||
|
||
SmallVector<Value> newOps; | ||
for (SmallVector<OpFoldResult> offsets : offsetsList) { | ||
auto newOp = rewriter.create<xegpu::LoadMatrixOp>( | ||
op.getLoc(), newValueTy, op.getMemDesc(), offsets, | ||
layout.dropInstData()); | ||
adam-smnk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
newOps.push_back(newOp); | ||
} | ||
Value castOp = unpack(newOps, op.getType(), *targetShape, loc, rewriter); | ||
rewriter.replaceOp(op, castOp); | ||
return success(); | ||
} | ||
}; | ||
|
||
struct UnrollStoreMatrixOp : public UnrollPattern<xegpu::StoreMatrixOp> { | ||
using UnrollPattern<xegpu::StoreMatrixOp>::UnrollPattern; | ||
LogicalResult matchAndRewrite(xegpu::StoreMatrixOp op, | ||
PatternRewriter &rewriter) const override { | ||
std::optional<SmallVector<int64_t>> targetShape = getTargetShape(op); | ||
if (!targetShape) | ||
return failure(); | ||
|
||
Location loc = op.getLoc(); | ||
VectorType valueTy = op.getData().getType(); | ||
ArrayRef<int64_t> shape = valueTy.getShape(); | ||
auto layout = dyn_cast<xegpu::LayoutAttr>(op.getLayoutAttr()); | ||
|
||
SmallVector<Type> convertedValTypes = | ||
getUnrolledTypes(valueTy, *targetShape); | ||
SmallVector<Value> convertedValues = | ||
pack(op.getData(), convertedValTypes, *targetShape, loc, rewriter); | ||
|
||
SmallVector<OpFoldResult> mixedOffsets = op.getMixedOffsets(); | ||
SmallVector<SmallVector<OpFoldResult>> offsetsList; | ||
for (SmallVector<int64_t> offsets : | ||
StaticTileOffsetRange(shape, *targetShape)) { | ||
auto adds = xegpu::addWithRightAligned( | ||
|
||
rewriter, loc, mixedOffsets, | ||
getAsIndexOpFoldResult(op.getContext(), offsets)); | ||
offsetsList.push_back(adds); | ||
} | ||
|
||
for (auto [v, offsets] : llvm::zip_equal(convertedValues, offsetsList)) | ||
rewriter.create<xegpu::StoreMatrixOp>(loc, v, op.getMemDesc(), offsets, | ||
layout.dropInstData()); | ||
|
||
rewriter.eraseOp(op); | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::xegpu::populateXeGPUUnrollPatterns( | ||
RewritePatternSet &patterns, const xegpu::UnrollOptions &options) { | ||
patterns.add<UnrollCreateNdOp, UnrollUpdateNdOffsetOp, UnrollPrefetchNdOp, | ||
UnrollLoadNdOp, UnrollStoreNdOp, UnrollDpasOp, | ||
UnrollCreateDescOp, UnrollLoadGatherOp, UnrollStoreScatterOp, | ||
UnrollPrefetchOp, UnrollUpdateOffsetOp>(patterns.getContext(), | ||
options); | ||
patterns | ||
.add<UnrollCreateNdOp, UnrollUpdateNdOffsetOp, UnrollPrefetchNdOp, | ||
UnrollLoadNdOp, UnrollStoreNdOp, UnrollDpasOp, UnrollCreateDescOp, | ||
UnrollLoadGatherOp, UnrollStoreScatterOp, UnrollPrefetchOp, | ||
UnrollUpdateOffsetOp, UnrollLoadMatrixOp, UnrollStoreMatrixOp>( | ||
patterns.getContext(), options); | ||
} |
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: does it use index dialect at all in the end?
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, it is used in addElementwise, so the pass needs to load it.