-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][vector] Fix parser of vector.transfer_read #133721
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
[mlir][vector] Fix parser of vector.transfer_read #133721
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-vector Author: Cedric (douyixuan) ChangesThis PR adds a check in the parser to prevent a crash when vector.transfer_read fails to create minor identity permutation. map. Full diff: https://github.com/llvm/llvm-project/pull/133721.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 5a3983699d5a3..d99c8ef0680f4 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -151,7 +151,7 @@ static bool isSupportedCombiningKind(CombiningKind combiningKind,
}
AffineMap mlir::vector::getTransferMinorIdentityMap(ShapedType shapedType,
- VectorType vectorType) {
+ VectorType vectorType) {
int64_t elementVectorRank = 0;
VectorType elementVectorType =
llvm::dyn_cast<VectorType>(shapedType.getElementType());
@@ -164,6 +164,9 @@ AffineMap mlir::vector::getTransferMinorIdentityMap(ShapedType shapedType,
return AffineMap::get(
/*numDims=*/0, /*numSymbols=*/0,
getAffineConstantExpr(0, shapedType.getContext()));
+ if (shapedType.getRank() < vectorType.getRank() - elementVectorRank) {
+ return AffineMap::get(shapedType.getContext());
+ }
return AffineMap::getMinorIdentityMap(
shapedType.getRank(), vectorType.getRank() - elementVectorRank,
shapedType.getContext());
@@ -4260,6 +4263,10 @@ ParseResult TransferReadOp::parse(OpAsmParser &parser, OperationState &result) {
AffineMap permMap;
if (!permMapAttr) {
permMap = getTransferMinorIdentityMap(shapedType, vectorType);
+ if (permMap.isEmpty()) {
+ return parser.emitError(typesLoc,
+ "failed to create minor identity permutation map");
+ }
result.attributes.set(permMapAttrName, AffineMapAttr::get(permMap));
} else {
permMap = llvm::cast<AffineMapAttr>(permMapAttr).getValue();
diff --git a/mlir/test/Dialect/Vector/invalid.mlir b/mlir/test/Dialect/Vector/invalid.mlir
index ea6d0021391fb..667e1615212f4 100644
--- a/mlir/test/Dialect/Vector/invalid.mlir
+++ b/mlir/test/Dialect/Vector/invalid.mlir
@@ -525,6 +525,15 @@ func.func @test_vector.transfer_read(%arg0: memref<?x?xvector<2x3xf32>>) {
// -----
+func.func @test_vector.transfer_read(%arg1: memref<?xindex>) -> vector<3x4xi32> {
+ %c3_i32 = arith.constant 3 : i32
+ // expected-error@+1 {{failed to create minor identity permutation map}}
+ %0 = vector.transfer_read %arg1[%c3_i32, %c3_i32], %c3_i32 : memref<?xindex>, vector<3x4xi32>
+ return %0 : vector<3x4xi32>
+}
+
+// -----
+
func.func @test_vector.transfer_write(%arg0: memref<?x?xf32>) {
%c3 = arith.constant 3 : index
%cst = arith.constant 3.0 : f32
|
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.
Thanks! I've left some suggestions.
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.
Thanks for fixing this! LGTM
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
Thanks for the updates, just a few final nits.
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.
The current implementation is very clean, well done! I've left a couple of final comments, but these are very minor.
Your contribution is much appreciated, thank you 🙏🏻
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.
LGTM, thanks. And please wait @banach-space to check finally.
@banach-space Could you please help check this issue again? |
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.
LGTM, thanks for the fix and for working on this, that's much appreciated!
As a side note, given that the description of getRealVectorRank
is much longer then the implementation, we should look into simplifying it. I am happy to look into this separately.
Do you have commit access?
No, I don't. Apologize for the long comments. |
@douyixuan Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
This is a small follow-on for llvm#133721: * Renamed `getRealVectorRank` as `getEffectiveVectorRankForXferOp` (to emphasise that this method was written specifically for transfer Ops). * Marginally tweaked the description for `getEffectiveVectorRankForXferOp` (mostly to highlight the two edge cases being covered). * Added tests for cases when the element type (of the shaped type) is a vector. * Unified the naming (and the order) of arguments in tests with the surrounding tests (e.g. `%vec_to_write` -> `%arg1`). Mostly for consistency (it would be good to use self-documenting names like `%vec_to_write` throughout).
No need to - this is incredibly fiddly and hard to document properly. In fact, I owe you an apology — I initially thought we could simplify it, but after trying myself, I realized it’s nearly impossible.
Done - please take a look: #137144 |
This is a small follow-on for #133721: * Renamed `getRealVectorRank` as `getEffectiveVectorRankForXferOp` (to emphasise that this method was written specifically for transfer Ops). * Marginally tweaked the description for `getEffectiveVectorRankForXferOp` (mostly to highlight the two edge cases being covered). * Added tests for cases when the element type (of the shaped type) is a vector. * Unified the naming (and the order) of arguments in tests with the surrounding tests (e.g. `%vec_to_write` -> `%arg1`). Mostly for consistency (it would be good to use self-documenting names like `%vec_to_write` throughout).
This PR adds a check in the parser to prevent a crash when vector.transfer_read fails to create minor identity permutation. map. Fixes llvm#132851 a.mlir ``` module { func.func @test_vector.transfer_read(%arg1: memref<?xindex>) -> vector<3x4xi32> { %c3_i32 = arith.constant 3 : i32 %0 = vector.transfer_read %arg1[%c3_i32, %c3_i32], %c3_i32 : memref<?xindex>, vector<3x4xi32> return %0 : vector<3x4xi32> } } ```
This is a small follow-on for llvm#133721: * Renamed `getRealVectorRank` as `getEffectiveVectorRankForXferOp` (to emphasise that this method was written specifically for transfer Ops). * Marginally tweaked the description for `getEffectiveVectorRankForXferOp` (mostly to highlight the two edge cases being covered). * Added tests for cases when the element type (of the shaped type) is a vector. * Unified the naming (and the order) of arguments in tests with the surrounding tests (e.g. `%vec_to_write` -> `%arg1`). Mostly for consistency (it would be good to use self-documenting names like `%vec_to_write` throughout).
This PR adds a check in the parser to prevent a crash when vector.transfer_read fails to create minor identity permutation. map. Fixes llvm#132851 a.mlir ``` module { func.func @test_vector.transfer_read(%arg1: memref<?xindex>) -> vector<3x4xi32> { %c3_i32 = arith.constant 3 : i32 %0 = vector.transfer_read %arg1[%c3_i32, %c3_i32], %c3_i32 : memref<?xindex>, vector<3x4xi32> return %0 : vector<3x4xi32> } } ```
This is a small follow-on for llvm#133721: * Renamed `getRealVectorRank` as `getEffectiveVectorRankForXferOp` (to emphasise that this method was written specifically for transfer Ops). * Marginally tweaked the description for `getEffectiveVectorRankForXferOp` (mostly to highlight the two edge cases being covered). * Added tests for cases when the element type (of the shaped type) is a vector. * Unified the naming (and the order) of arguments in tests with the surrounding tests (e.g. `%vec_to_write` -> `%arg1`). Mostly for consistency (it would be good to use self-documenting names like `%vec_to_write` throughout).
This PR adds a check in the parser to prevent a crash when vector.transfer_read fails to create minor identity permutation. map. Fixes llvm#132851 a.mlir ``` module { func.func @test_vector.transfer_read(%arg1: memref<?xindex>) -> vector<3x4xi32> { %c3_i32 = arith.constant 3 : i32 %0 = vector.transfer_read %arg1[%c3_i32, %c3_i32], %c3_i32 : memref<?xindex>, vector<3x4xi32> return %0 : vector<3x4xi32> } } ```
This is a small follow-on for llvm#133721: * Renamed `getRealVectorRank` as `getEffectiveVectorRankForXferOp` (to emphasise that this method was written specifically for transfer Ops). * Marginally tweaked the description for `getEffectiveVectorRankForXferOp` (mostly to highlight the two edge cases being covered). * Added tests for cases when the element type (of the shaped type) is a vector. * Unified the naming (and the order) of arguments in tests with the surrounding tests (e.g. `%vec_to_write` -> `%arg1`). Mostly for consistency (it would be good to use self-documenting names like `%vec_to_write` throughout).
This PR adds a check in the parser to prevent a crash when vector.transfer_read fails to create minor identity permutation. map. Fixes llvm#132851 a.mlir ``` module { func.func @test_vector.transfer_read(%arg1: memref<?xindex>) -> vector<3x4xi32> { %c3_i32 = arith.constant 3 : i32 %0 = vector.transfer_read %arg1[%c3_i32, %c3_i32], %c3_i32 : memref<?xindex>, vector<3x4xi32> return %0 : vector<3x4xi32> } } ```
This is a small follow-on for llvm#133721: * Renamed `getRealVectorRank` as `getEffectiveVectorRankForXferOp` (to emphasise that this method was written specifically for transfer Ops). * Marginally tweaked the description for `getEffectiveVectorRankForXferOp` (mostly to highlight the two edge cases being covered). * Added tests for cases when the element type (of the shaped type) is a vector. * Unified the naming (and the order) of arguments in tests with the surrounding tests (e.g. `%vec_to_write` -> `%arg1`). Mostly for consistency (it would be good to use self-documenting names like `%vec_to_write` throughout).
This PR adds a check in the parser to prevent a crash when vector.transfer_read fails to create minor identity permutation. map.
Fixes #132851
a.mlir