Skip to content

[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

Merged
merged 11 commits into from
Apr 24, 2025

Conversation

douyixuan
Copy link
Contributor

@douyixuan douyixuan commented Mar 31, 2025

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

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>
  }
}

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot
Copy link
Member

llvmbot commented Mar 31, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-vector

Author: Cedric (douyixuan)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/133721.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/Vector/IR/VectorOps.cpp (+8-1)
  • (modified) mlir/test/Dialect/Vector/invalid.mlir (+9)
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

@CoTinker CoTinker requested a review from joker-eph April 1, 2025 03:21
Copy link
Contributor

@banach-space banach-space left a 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.

Copy link
Contributor

@dcaballe dcaballe left a 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

Copy link

github-actions bot commented Apr 3, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@banach-space banach-space left a 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.

Copy link
Contributor

@banach-space banach-space left a 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 🙏🏻

@douyixuan douyixuan requested a review from banach-space April 15, 2025 14:16
Copy link
Contributor

@CoTinker CoTinker left a 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.

@douyixuan
Copy link
Contributor Author

LGTM, thanks. And please wait @banach-space to check finally.

@banach-space Could you please help check this issue again?

Copy link
Contributor

@banach-space banach-space left a 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?

@douyixuan
Copy link
Contributor Author

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.

@CoTinker CoTinker merged commit a68c8e8 into llvm:main Apr 24, 2025
11 checks passed
Copy link

@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!

banach-space added a commit to banach-space/llvm-project that referenced this pull request Apr 24, 2025
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).
@banach-space
Copy link
Contributor

Apologize for the long comments.

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.

I am happy to look into this separately.

Done - please take a look: #137144

banach-space added a commit that referenced this pull request Apr 25, 2025
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).
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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>
  }
}
```
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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).
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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>
  }
}
```
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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).
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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>
  }
}
```
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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).
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
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>
  }
}
```
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mlir] TransferReadOp::parse crashes in AffineMap::getMinorIdentityMap with Assertion `dims >= results && "Dimension mismatch"' failed
5 participants