-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
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-llvm-transforms Author: None (ErikHogeman) ChangesThe 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:
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 }
+
|
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.
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. |
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. |
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, 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. |
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!
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.
06a9cd6
to
e4cd836
Compare
@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! |
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? |
Ah, yes you're right, I got confused by the commit in the "commits" tab which had the message I kept while squashing. |
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.