Skip to content

[MLIR] Avoid vector.extract_strided_slice when not needed #115941

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 1 commit into from
Nov 12, 2024

Conversation

lialan
Copy link
Member

@lialan lialan commented Nov 12, 2024

In staticallyExtractSubvector, When the extracting slice is the same as source vector, do not need to emit vector.extract_strided_slice.

This fixes the lit test case @vector_store_i4 in
mlir\test\Dialect\Vector\vector-emulate-narrow-type.mlir, where converting from vector<8xi4> to vector<4xi8> does not need slice extraction.

The issue was introduced in #113411 and #115070, CI failure link: https://buildkite.com/llvm-project/github-pull-requests/builds/118845

This PR does not include a lit test case because it is a fix and the above mentioned @vector_store_i4 test actually tests the mechanism.

In `staticallyExtractSubvector`, When the extracting slice is the same as source vector,
do not need to emit `vector.extract_strided_slice`.

This fixes the lit test `@vector_store_i4` in
`mlir\test\Dialect\Vector\vector-emulate-narrow-type.mlir`, where converting
from `vector<8xi4>` to `vector<4xi8>` does not need slice extraction.

Signed-off-by: Alan Li <[email protected]>
@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2024

@llvm/pr-subscribers-mlir-vector

@llvm/pr-subscribers-mlir

Author: lialan (lialan)

Changes

In staticallyExtractSubvector, When the extracting slice is the same as source vector, do not need to emit vector.extract_strided_slice.

This fixes the lit test case @<!-- -->vector_store_i4 in
mlir\test\Dialect\Vector\vector-emulate-narrow-type.mlir, where converting from vector&lt;8xi4&gt; to vector&lt;4xi8&gt; does not need slice extraction.


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

1 Files Affected:

  • (modified) mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp (+7)
diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp
index 7578aadee23a6e..6fd6ccc04e8045 100644
--- a/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp
+++ b/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp
@@ -150,6 +150,13 @@ static Value staticallyExtractSubvector(OpBuilder &rewriter, Location loc,
   assert((vectorType.getRank() == 1 && extractType.getRank() == 1) &&
          "expected 1-D source and destination types");
   (void)vectorType;
+  assert(frontOffset + subvecSize <= vectorType.getNumElements() &&
+         "subvector out of bounds");
+
+  // do not need extraction if the subvector size is the same as the source
+  if (vectorType.getNumElements() == subvecSize)
+    return source;
+
   auto offsets = rewriter.getI64ArrayAttr({frontOffset});
   auto sizes = rewriter.getI64ArrayAttr({subvecSize});
   auto strides = rewriter.getI64ArrayAttr({1});

Copy link
Contributor

@MaheshRavishankar MaheshRavishankar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a lit test with this? I thought that this was meant to fix a lit test?

@hanhanW hanhanW requested a review from banach-space November 12, 2024 21:27
Copy link
Contributor

@hanhanW hanhanW left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lialan told me that it is fixing the post CI failures. E.g., https://buildkite.com/llvm-project/github-pull-requests/builds/118845

Please add the link to the PR description which makes is clearer.

@lialan
Copy link
Member Author

lialan commented Nov 12, 2024

@hanhanW Thanks Hanhan, updated the PR description to include more information about this.

@lialan
Copy link
Member Author

lialan commented Nov 12, 2024

Is there a lit test with this? I thought that this was meant to fix a lit test?

Tried to build a lit test for it, but it actually totally overlaps with the existing failing test. So I specifically mentioned such case int the PR description.

@joker-eph
Copy link
Collaborator

joker-eph commented Nov 12, 2024

Do we understand why this fails there but not for the post-merge bots? The test is passing here at head for example: https://lab.llvm.org/buildbot/#/builders/138/builds/6300/steps/6/logs/stdio

I'd really like a test I could reproduce locally for this!

@hanhanW hanhanW merged commit 24a8092 into llvm:main Nov 12, 2024
10 of 11 checks passed
@hanhanW
Copy link
Contributor

hanhanW commented Nov 12, 2024

Do we understand why this fails there but not for the post-merge bots? The test is passing here at head for example: https://lab.llvm.org/buildbot/#/builders/138/builds/6300/steps/6/logs/stdio

Ah, there is a race when I clicked the merge button and you left a comment.. Any preference how to proceed here?

assert(frontOffset + subvecSize <= vectorType.getNumElements() &&
"subvector out of bounds");

// do not need extraction if the subvector size is the same as the source
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use proper sentences in code comments: https://llvm.org/docs/CodingStandards.html#commenting

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.

6 participants