-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][vector] Refine Vector to LLVM lowering options #159553
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
banach-space
merged 3 commits into
llvm:main
from
banach-space:andrzej/vector/rename_pass_optoins
Sep 23, 2025
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -1489,8 +1489,8 @@ def ConvertVectorToLLVMPass : Pass<"convert-vector-to-llvm"> { | |
VectorContractLoweringAttr.summary, [{::llvm::cl::values( | ||
clEnumValN(::mlir::vector::VectorContractLowering::Dot, "dot", | ||
"Progressively lower to finer grained `vector.contract` and dot-products. (default)"), | ||
clEnumValN(::mlir::vector::VectorContractLowering::Matmul, "matmul", | ||
"Lower to `vector.matrix_multiply`, maps 1-1 to LLVM matrix intrinsics."), | ||
clEnumValN(::mlir::vector::VectorContractLowering::LLVMIntr, "llvmintr", | ||
"Lower directly to `llvm.intr.matrix.multiply`."), | ||
clEnumValN(::mlir::vector::VectorContractLowering::OuterProduct, "outerproduct", | ||
"Lower to `vector.outerproduct`."), | ||
clEnumValN(::mlir::vector::VectorContractLowering::ParallelArith, "parallelarith", | ||
|
@@ -1502,8 +1502,8 @@ def ConvertVectorToLLVMPass : Pass<"convert-vector-to-llvm"> { | |
VectorTransposeLoweringAttr.summary, [{::llvm::cl::values( | ||
clEnumValN(::mlir::vector::VectorTransposeLowering::EltWise, "eltwise", | ||
"Lower transpose into element-wise extract and inserts (default)"), | ||
clEnumValN(::mlir::vector::VectorTransposeLowering::Flat, "flat", | ||
"Lower 2-D transpose to `vector.flat_transpose`, maps 1-1 to LLVM matrix intrinsics"), | ||
clEnumValN(::mlir::vector::VectorTransposeLowering::LLVMIntr, "llvmintr", | ||
"Lower 2-D transpose directly to `llvm.intr.matrix.transpose`"), | ||
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. same, in general, throughout the file |
||
clEnumValN(::mlir::vector::VectorTransposeLowering::Shuffle1D, "shuffle1d", | ||
"Lower 2-D transpose to `vector.shuffle` on 1-D vector."), | ||
clEnumValN(::mlir::vector::VectorTransposeLowering::Shuffle16x16, "shuffle16x16", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1987,41 +1987,36 @@ struct VectorScalableStepOpLowering | |
/// %e = add %c, %d | ||
/// ``` | ||
/// `vector.matrix_multiply` later lowers to `llvm.matrix.multiply`. | ||
// | ||
/// This only kicks in when vectorContractLowering is set to Matmul and | ||
/// the vector.contract op is a row-major matrix multiply. | ||
class ContractionOpToMatmulOpLowering | ||
: public vector::MaskableOpRewritePattern<vector::ContractionOp> { | ||
public: | ||
using MaskableOpRewritePattern::MaskableOpRewritePattern; | ||
|
||
ContractionOpToMatmulOpLowering( | ||
vector::VectorContractLowering vectorContractLowering, | ||
MLIRContext *context, PatternBenefit benefit = 100) | ||
ContractionOpToMatmulOpLowering(MLIRContext *context, | ||
PatternBenefit benefit = 100) | ||
: MaskableOpRewritePattern<vector::ContractionOp>(context, benefit) {} | ||
|
||
FailureOr<Value> | ||
matchAndRewriteMaskableOp(vector::ContractionOp op, MaskingOpInterface maskOp, | ||
PatternRewriter &rewriter) const override; | ||
}; | ||
|
||
/// Progressively lower a `vector.contract %a, %b, %c` with row-major matmul | ||
/// semantics to: | ||
/// Lower a qualifying `vector.contract %a, %b, %c` (with row-major matmul | ||
/// semantics directly into `llvm.intr.matrix.multiply`: | ||
/// BEFORE: | ||
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/optional: you can tag code blocks with mlir, e.g.:
also below |
||
/// ```mlir | ||
/// %res = vector.contract #matmat_trait %lhs, %rhs, %acc | ||
/// : vector<2x4xf32>, vector<4x3xf32> into vector<2x3xf32> | ||
/// ``` | ||
/// %mta = maybe_transpose | ||
/// %mtb = maybe_transpose | ||
/// %flattened_a = vector.shape_cast %mta | ||
/// %flattened_b = vector.shape_cast %mtb | ||
/// %flattened_d = llvm.intr.matrix.multiply %flattened_a, %flattened_b | ||
/// %mtd = vector.shape_cast %flattened_d | ||
/// %d = maybe_untranspose %mtd | ||
/// %e = add %c, %d | ||
/// | ||
/// AFTER: | ||
/// ```mlir | ||
/// %lhs = vector.shape_cast %arg0 : vector<2x4xf32> to vector<8xf32> | ||
/// %rhs = vector.shape_cast %arg1 : vector<4x3xf32> to vector<12xf32> | ||
/// %matmul = llvm.intr.matrix.multiply %lhs, %rhs | ||
/// %res = arith.addf %acc, %matmul : vector<2x3xf32> | ||
/// ``` | ||
// | ||
/// This only kicks in when vectorContractLowering is set to `Matmul`. | ||
/// vector.transpose operations are inserted if the vector.contract op is not a | ||
/// row-major matrix multiply. | ||
/// | ||
/// Scalable vectors are not supported. | ||
FailureOr<Value> ContractionOpToMatmulOpLowering::matchAndRewriteMaskableOp( | ||
vector::ContractionOp op, MaskingOpInterface maskOp, | ||
|
@@ -2116,7 +2111,19 @@ FailureOr<Value> ContractionOpToMatmulOpLowering::matchAndRewriteMaskableOp( | |
return res; | ||
} | ||
|
||
/// Lowers vector.transpose to llvm.intr.matrix.transpose | ||
/// Lowers vector.transpose directly to llvm.intr.matrix.transpose | ||
/// | ||
/// BEFORE: | ||
/// ```mlir | ||
/// %tr = vector.transpose %vec, [1, 0] : vector<2x4xf32> to vector<4x2xf32> | ||
/// ``` | ||
/// AFTER: | ||
/// ```mlir | ||
/// %vec_cs = vector.shape_cast %vec : vector<2x4xf32> to vector<8xf32> | ||
/// %tr = llvm.intr.matrix.transpose %vec_sc | ||
/// {columns = 2 : i32, rows = 4 : i32} : vector<8xf32> into vector<8xf32> | ||
/// %res = vector.shape_cast %tr : vector<8xf32> to vector<4x2xf32> | ||
/// ``` | ||
class TransposeOpToMatrixTransposeOpLowering | ||
: public OpRewritePattern<vector::TransposeOp> { | ||
public: | ||
|
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
2 changes: 1 addition & 1 deletion
2
mlir/test/Dialect/Vector/vector-contract-to-matrix-intrinsics-transforms.mlir
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
2 changes: 1 addition & 1 deletion
2
mlir/test/Dialect/Vector/vector-transpose-to-matrix-intrinsics-transform.mlir
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.
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.
llvm
only could be misleading as that's not the only option we have to lower to llvm. What about ~llvm-matmul
?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.
Good point!
Since this is about lowering to "LLVM intrinsics", and the intrinsics are:
llvm.intr.matrix.multiply
(i.e. llvm.intr.<...>)llvm.intr.matrix.transpose
(i.e. llvm.intr.<...>),let me rename this as
llvmintr
.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.
LLVM intrinsic sounds like the best name for me