Skip to content

[LV] Check for vector-to-scalar casts in legalizer #106244

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
Sep 6, 2024

Conversation

ErikHogeman
Copy link
Contributor

The code makes assumptions later on the operations and their inputs being scalar in the loops that are processed, so we should make sure this is the case in the legalizer.

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 Aug 27, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (ErikHogeman)

Changes

The code makes assumptions later on the operations and their inputs being scalar in the loops that are processed, so we should make sure this is the case in the legalizer.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+3)
  • (added) llvm/test/Transforms/LoopVectorize/vector-to-scalar-cast.ll (+32)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 66a779da8c25bc..b1366b484587d6 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -943,9 +943,12 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
         VecCallVariantsFound = true;
 
       // Check that the instruction return type is vectorizable.
+      // We can't vectorize casts from vector type to scalar type.
       // Also, we can't vectorize extractelement instructions.
       if ((!VectorType::isValidElementType(I.getType()) &&
            !I.getType()->isVoidTy()) ||
+          (isa<CastInst>(&I) &&
+           !VectorType::isValidElementType(I.getOperand(0)->getType())) ||
           isa<ExtractElementInst>(I)) {
         reportVectorizationFailure("Found unvectorizable type",
             "instruction return type cannot be vectorized",
diff --git a/llvm/test/Transforms/LoopVectorize/vector-to-scalar-cast.ll b/llvm/test/Transforms/LoopVectorize/vector-to-scalar-cast.ll
new file mode 100644
index 00000000000000..1a34c815f5a18d
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/vector-to-scalar-cast.ll
@@ -0,0 +1,32 @@
+; RUN: opt --force-widen-divrem-via-safe-divisor=false -passes=loop-vectorize -S --debug-only=loop-vectorize < %s 2>&1 | FileCheck %s
+
+; CHECK: LV: Not vectorizing: Found unvectorizable type   %s2 = bitcast <2 x i16> %vec1 to i32
+; CHECK-NOT: Assertion
+
+; Function Attrs: willreturn
+define void @__start() #0 {
+entry:
+  %vec0 = insertelement <2 x i16> undef, i16 0, i64 0
+  %vec1 = insertelement <2 x i16> %vec0, i16 0, i64 1
+  br label %bb0
+
+bb0:
+  %s0 = phi i32 [ %s1, %bb1 ], [ 1, %entry ]
+  br i1 0, label %bb2, label %bb1
+
+bb1:
+  %s1 = add nuw nsw i32 %s0, 1
+  %exitcond = icmp ne i32 %s1, 11
+  br i1 %exitcond, label %bb0, label %bb3
+
+bb2:
+  %s2 = bitcast <2 x i16> %vec1 to i32
+  %s3 = srem i32 0, %s2
+  br label %bb1
+
+bb3:
+  ret void
+}
+
+attributes #0 = { willreturn }
+

Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

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

Might be good to retitle this s/LoopVectorize/LV/.

@ErikHogeman
Copy link
Contributor Author

Might be good to retitle this s/LoopVectorize/LV/.

Do you mean in the commit message?

Thanks for the review, I will fix your comments tomorrow.

@artagnon
Copy link
Contributor

Might be good to retitle this s/LoopVectorize/LV/.

Do you mean in the commit message?

You can just re-title the PR using the GitHub interface, and that will be used as the commit message when you merge.

@ErikHogeman ErikHogeman changed the title [LoopVectorize] Check for vector-to-scalar casts in legalizer [LV] Check for vector-to-scalar casts in legalizer Aug 28, 2024
@ErikHogeman
Copy link
Contributor Author

Might be good to retitle this s/LoopVectorize/LV/.

Do you mean in the commit message?

You can just re-title the PR using the GitHub interface, and that will be used as the commit message when you merge.

Thanks, updated the title.

I also added a use of the "srem" now in the loop, and added a user-defined vectorization width to make it more robust that it triggers the bad code path. The test still crashes without the fix.

Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

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

LGTM, modulo some comments. Kindly wait for @fhahn or someone else on the reviewer list for the final sign-off.

@ErikHogeman
Copy link
Contributor Author

LGTM, modulo some comments. Kindly wait for @fhahn or someone else on the reviewer list for the final sign-off.

Thanks again for the review!

I will wait until the other reviewers have had time to have a look as well.

Copy link
Contributor

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

@ErikHogeman
Copy link
Contributor Author

LGTM, thanks!

Thank you, I will squash the commits and finalize the patch then.

The code makes assumptions later on the operations and
their inputs being scalar in the loops that are processed,
so we should make sure this is the case in the legalizer.
@svenvh svenvh merged commit 78e1e6a into llvm:main Sep 6, 2024
8 checks passed
Copy link

github-actions bot commented Sep 6, 2024

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

@ErikHogeman ErikHogeman deleted the loop-vectorizer-fix branch September 6, 2024 11:21
@ErikHogeman
Copy link
Contributor Author

I just realized that while rebasing and squashing I reverted to the old commit message using the full name for LoopVectorize instead of LV as suggested in the review, apologies for that. Let me know if there's anything you want me to do, I assume it's too late to change now.

@fhahn
Copy link
Contributor

fhahn commented Sep 6, 2024

I just realized that while rebasing and squashing I reverted to the old commit message using the full name for LoopVectorize instead of LV as suggested in the review, apologies for that. Let me know if there's anything you want me to do, I assume it's too late to change now.

Once the PR is created, adjusting the commit messages won't update the PR title/description. Only the PR title/descriptions are used for the final commit. So the commit message of the landed commit should be fine?

@ErikHogeman
Copy link
Contributor Author

Once the PR is created, adjusting the commit messages won't update the PR title/description. Only the PR title/descriptions are used for the final commit. So the commit message of the landed commit should be fine?

Ah, yes you're right, I got confused by the commit in the "commits" tab which had the message I kept while squashing.
OK so no problems then, great, thanks for clarifying that!

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.

5 participants