-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR][XeGPU] Add support for elementwise ops in Wg to Sg distribute pass [1/N] #142797
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 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
08f7eb9
Add support elementwise ops in Wg to Sg distribute pass
nbpatel e215e22
Clean up
nbpatel 6960b5c
Refine
nbpatel 37ea147
Clean up
nbpatel adcdec5
Use OpTrait instead of templating
nbpatel ac183e3
Merge branch 'main' into xegpu_wg_sg_elementwise
nbpatel d255680
refactor
nbpatel 94d0f1b
newline
nbpatel 7fd9976
Clean up tests
nbpatel 8a0b3df
Newline
nbpatel 077ff34
Merge branch 'main' into xegpu_wg_sg_elementwise
nbpatel 5f7c8f3
Clean up
nbpatel 69b5786
Feedback
nbpatel b15c720
Address comments
nbpatel b629797
fix
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 |
|---|---|---|
|
|
@@ -8,15 +8,18 @@ | |
| #include "mlir/Dialect/XeGPU/Transforms/Passes.h" | ||
|
|
||
| #include "mlir/Dialect/Affine/Utils.h" | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/Arith/Utils/Utils.h" | ||
| #include "mlir/Dialect/GPU/IR/GPUDialect.h" | ||
| #include "mlir/Dialect/Index/IR/IndexDialect.h" | ||
| #include "mlir/Dialect/Index/IR/IndexOps.h" | ||
| #include "mlir/Dialect/Math/IR/Math.h" | ||
| #include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
| #include "mlir/Dialect/Utils/IndexingUtils.h" | ||
| #include "mlir/Dialect/XeGPU/IR/XeGPU.h" | ||
| #include "mlir/Dialect/XeGPU/Transforms/Transforms.h" | ||
| #include "mlir/Transforms/DialectConversion.h" | ||
| #include <optional> | ||
|
|
||
| namespace mlir { | ||
| namespace xegpu { | ||
|
|
@@ -314,6 +317,90 @@ struct WgToSgPrefetchNdOp : public OpConversionPattern<xegpu::PrefetchNdOp> { | |
| } | ||
| }; | ||
|
|
||
| // This pattern transforms elementwise ops (unary/binary) in math/arith dialect | ||
| template <typename Op> | ||
| struct WgToSgElementwiseOp : public OpConversionPattern<Op> { | ||
adam-smnk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using OpConversionPattern<Op>::OpConversionPattern; | ||
| using OneToNOpAdaptor = typename OpConversionPattern<Op>::OneToNOpAdaptor; | ||
|
|
||
| LogicalResult | ||
| matchAndRewrite(Op op, OneToNOpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
| // All operands/results must be 1D or 2D vectors | ||
| auto resultType = dyn_cast<VectorType>(op.getResult().getType()); | ||
| if (!resultType || (resultType.getRank() != 1 && resultType.getRank() != 2)) | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return rewriter.notifyMatchFailure( | ||
| op, "Result type is not a 1D or 2D vector"); | ||
|
|
||
nbpatel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ArrayRef<int64_t> shape = resultType.getShape(); | ||
| for (Value operand : op->getOperands()) { | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| auto operandType = dyn_cast<VectorType>(operand.getType()); | ||
| if (!operandType || operandType.getRank() != resultType.getRank() || | ||
| operandType.getShape() != shape) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Operand type is not a 1D or 2D vector with the same shape as " | ||
| "result type"); | ||
| } | ||
| } | ||
|
|
||
| auto layout = dyn_cast_or_null<xegpu::LayoutAttr>(op->getAttr("layout")); | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!layout || !layout.getSgLayout()) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Operation does not have a valid layout attribute for subgroup " | ||
| "distribution"); | ||
|
|
||
| // Extract sgShape from layout | ||
| SmallVector<int64_t> sgShape; | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (auto sgDataAttr = layout.getSgData()) { | ||
| sgShape = llvm::to_vector_of<int64_t>(sgDataAttr.asArrayRef()); | ||
| } else { | ||
| auto sgLayoutArr = layout.getSgLayout(); | ||
| sgShape.reserve(shape.size()); | ||
| for (size_t i = 0; i < shape.size(); ++i) { | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert(sgLayoutArr[i] != 0 && "sgLayout elements must be non-zero"); | ||
| sgShape.push_back(shape[i] / sgLayoutArr[i]); | ||
| } | ||
| } | ||
|
|
||
| size_t numVariants = adaptor.getOperands().empty() | ||
| ? 0 | ||
| : adaptor.getOperands().front().size(); | ||
adam-smnk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (auto &operandVec : adaptor.getOperands()) | ||
| if (operandVec.size() != numVariants) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "Operand lists have mismatched sizes"); | ||
|
|
||
| SmallVector<Value> newResults; | ||
|
|
||
| auto origResultType = dyn_cast<VectorType>(op->getResult(0).getType()); | ||
adam-smnk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| VectorType newResultType = | ||
| origResultType | ||
| ? VectorType::get(sgShape, origResultType.getElementType()) | ||
| : VectorType::get(sgShape, resultType.getElementType()); | ||
|
|
||
| for (size_t i = 0; i < numVariants; ++i) { | ||
| SmallVector<Value> operands; | ||
| for (auto &operandVec : adaptor.getOperands()) | ||
| operands.push_back(operandVec[i]); | ||
|
|
||
| auto newOp = rewriter.create<Op>(op.getLoc(), newResultType, operands); | ||
|
|
||
| // Copy all attributes except "layout", and add "layout_result_0" with | ||
| // sgLayout/data dropped | ||
| for (auto attr : op->getAttrs()) { | ||
| if (attr.getName() != "layout") | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| newOp->setAttr(attr.getName(), attr.getValue()); | ||
| } | ||
| newOp->setAttr("layout_result_0", layout.dropSgLayoutAndData()); | ||
nbpatel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| newResults.push_back(newOp.getResult()); | ||
| } | ||
|
|
||
| rewriter.replaceOpWithMultiple(op, {newResults}); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace mlir { | ||
|
|
@@ -322,6 +409,57 @@ void populateXeGPUWgToSgDistributePatterns(RewritePatternSet &patterns) { | |
| patterns.add<WgToSgCreateNdOp, WgToSgLoadNdOp, WgToSgStoreNdOp, | ||
| WgToSgUpdateNdOffsetOp, WgToSgDpasOp, WgToSgPrefetchNdOp>( | ||
| patterns.getContext()); | ||
| // Add elementwise operations that can be distributed to subgroups | ||
| patterns.add< | ||
| WgToSgElementwiseOp<arith::AddFOp>, WgToSgElementwiseOp<arith::SubFOp>, | ||
| WgToSgElementwiseOp<math::ExpOp>, WgToSgElementwiseOp<math::SqrtOp>, | ||
| WgToSgElementwiseOp<math::AbsFOp>, WgToSgElementwiseOp<math::CosOp>, | ||
| WgToSgElementwiseOp<math::CoshOp>, WgToSgElementwiseOp<math::AcosOp>, | ||
| WgToSgElementwiseOp<math::AcoshOp>, WgToSgElementwiseOp<math::SinOp>, | ||
| WgToSgElementwiseOp<math::SinhOp>, WgToSgElementwiseOp<math::AsinOp>, | ||
| WgToSgElementwiseOp<math::AsinhOp>, WgToSgElementwiseOp<math::TanOp>, | ||
| WgToSgElementwiseOp<math::TanhOp>, WgToSgElementwiseOp<math::AtanOp>, | ||
| WgToSgElementwiseOp<math::Atan2Op>, WgToSgElementwiseOp<math::AtanhOp>, | ||
| WgToSgElementwiseOp<math::ErfOp>, WgToSgElementwiseOp<math::LogOp>, | ||
| WgToSgElementwiseOp<math::Log2Op>, WgToSgElementwiseOp<math::FloorOp>, | ||
| WgToSgElementwiseOp<math::CeilOp>, WgToSgElementwiseOp<math::PowFOp>, | ||
| WgToSgElementwiseOp<math::RsqrtOp>, WgToSgElementwiseOp<arith::NegFOp>, | ||
| WgToSgElementwiseOp<arith::AddIOp>, WgToSgElementwiseOp<arith::SubIOp>, | ||
| WgToSgElementwiseOp<arith::MulFOp>, WgToSgElementwiseOp<arith::MulIOp>, | ||
| WgToSgElementwiseOp<arith::ShLIOp>, WgToSgElementwiseOp<arith::ShRSIOp>, | ||
| WgToSgElementwiseOp<arith::ShRUIOp>, WgToSgElementwiseOp<arith::DivFOp>, | ||
| WgToSgElementwiseOp<arith::DivSIOp>, WgToSgElementwiseOp<arith::DivUIOp>, | ||
| WgToSgElementwiseOp<arith::MaximumFOp>, | ||
| WgToSgElementwiseOp<arith::MinimumFOp>, | ||
| WgToSgElementwiseOp<arith::RemSIOp>, WgToSgElementwiseOp<arith::RemUIOp>, | ||
| WgToSgElementwiseOp<arith::TruncFOp>, | ||
| WgToSgElementwiseOp<arith::TruncIOp>, WgToSgElementwiseOp<arith::ExtFOp>, | ||
| WgToSgElementwiseOp<arith::ExtSIOp>, WgToSgElementwiseOp<arith::ExtUIOp>, | ||
| WgToSgElementwiseOp<arith::SIToFPOp>, | ||
| WgToSgElementwiseOp<arith::UIToFPOp>, | ||
| WgToSgElementwiseOp<arith::FPToSIOp>, | ||
| WgToSgElementwiseOp<arith::FPToUIOp>, | ||
| WgToSgElementwiseOp<arith::IndexCastUIOp>, | ||
| WgToSgElementwiseOp<arith::IndexCastOp>, | ||
| WgToSgElementwiseOp<arith::BitcastOp>, WgToSgElementwiseOp<arith::CmpIOp>, | ||
| WgToSgElementwiseOp<arith::CmpFOp>, WgToSgElementwiseOp<arith::AndIOp>, | ||
| WgToSgElementwiseOp<arith::CeilDivSIOp>, | ||
| WgToSgElementwiseOp<arith::CeilDivUIOp>, | ||
| WgToSgElementwiseOp<arith::FloorDivSIOp>, | ||
| WgToSgElementwiseOp<arith::MaxNumFOp>, | ||
| WgToSgElementwiseOp<arith::MaxSIOp>, WgToSgElementwiseOp<arith::MaxUIOp>, | ||
| WgToSgElementwiseOp<arith::MinNumFOp>, | ||
| WgToSgElementwiseOp<arith::MinSIOp>, WgToSgElementwiseOp<arith::MinUIOp>, | ||
| WgToSgElementwiseOp<arith::OrIOp>, WgToSgElementwiseOp<arith::RemFOp>, | ||
| WgToSgElementwiseOp<arith::XOrIOp>, WgToSgElementwiseOp<math::AbsIOp>, | ||
| WgToSgElementwiseOp<math::CbrtOp>, WgToSgElementwiseOp<math::CopySignOp>, | ||
| WgToSgElementwiseOp<math::CtPopOp>, WgToSgElementwiseOp<math::ErfcOp>, | ||
| WgToSgElementwiseOp<math::Exp2Op>, WgToSgElementwiseOp<math::ExpM1Op>, | ||
| WgToSgElementwiseOp<math::FPowIOp>, WgToSgElementwiseOp<math::IPowIOp>, | ||
| WgToSgElementwiseOp<math::Log10Op>, WgToSgElementwiseOp<math::Log1pOp>, | ||
| WgToSgElementwiseOp<math::RoundOp>, | ||
| WgToSgElementwiseOp<math::RoundEvenOp>, | ||
| WgToSgElementwiseOp<math::TruncOp>>(patterns.getContext()); | ||
| } | ||
| } // namespace xegpu | ||
| } // namespace mlir | ||
|
|
@@ -368,6 +506,31 @@ void XeGPUWgToSgDistributePass::runOnOperation() { | |
| auto layout = dyn_cast_or_null<xegpu::LayoutAttr>(op->getAttr("layout")); | ||
| return isLegal(layout); | ||
| }); | ||
| target.addDynamicallyLegalDialect<math::MathDialect, arith::ArithDialect>( | ||
| [=](Operation *op) -> std::optional<bool> { | ||
| // Handle unary and binary operations | ||
| if (op->getNumOperands() < 1 || op->getNumOperands() > 2) | ||
| return true; | ||
|
|
||
| // check if input and output are vectors | ||
| VectorType resultType = | ||
| dyn_cast<VectorType>(op->getResult(0).getType()); | ||
| if (!resultType || resultType.getRank() != 2) | ||
| return true; | ||
|
|
||
| // Check if all operands are vectors | ||
| for (Value operand : op->getOperands()) { | ||
|
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. nit: consider the use of
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. this loop is equivalent to |
||
| VectorType operandType = dyn_cast<VectorType>(operand.getType()); | ||
| if (!operandType || operandType.getRank() != 2 || | ||
| operandType.getShape() != resultType.getShape()) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| auto layout = dyn_cast_or_null<xegpu::LayoutAttr>( | ||
| op->getAttrOfType<xegpu::LayoutAttr>("layout")); | ||
| return isLegal(layout); | ||
| }); | ||
|
|
||
| target.markUnknownOpDynamicallyLegal([](Operation *) { return true; }); | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.