-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[Instcombine] Combine extractelement from a vector_extract at index 0 #151491
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
[Instcombine] Combine extractelement from a vector_extract at index 0 #151491
Conversation
Extracting any element from a subvector starting at index 0 is equivalent to extracting from the original vector, i.e. extract_elt(vector_extract(x, 0), y) -> extract_elt(x, y)
@llvm/pr-subscribers-llvm-transforms Author: Kerry McLaughlin (kmclaughlin-arm) ChangesExtracting any element from a subvector starting at index 0 is Full diff: https://github.com/llvm/llvm-project/pull/151491.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index 00b877b8a07ef..6f2adba9e3f6b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -444,7 +444,11 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
else
Idx = PoisonValue::get(Ty);
return replaceInstUsesWith(EI, Idx);
- }
+ } else if (IID == Intrinsic::vector_extract)
+ // If II is a subvector starting at index 0, extract from the wider
+ // source vector
+ if (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 0)
+ return ExtractElementInst::Create(II->getArgOperand(0), Index);
}
// InstSimplify should handle cases where the index is invalid.
diff --git a/llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll b/llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll
new file mode 100644
index 0000000000000..38cbeb0df98bd
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll
@@ -0,0 +1,13 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define i1 @scalable_test(<vscale x 4 x i1> %a) {
+; CHECK-LABEL: define i1 @scalable_test(
+; CHECK-SAME: <vscale x 4 x i1> [[A:%.*]]) {
+; CHECK-NEXT: [[ELT:%.*]] = extractelement <vscale x 4 x i1> [[A]], i64 1
+; CHECK-NEXT: ret i1 [[ELT]]
+;
+ %subvec = call <vscale x 2 x i1> @llvm.vector.extract.nxv2i1.nxv4i1.i64(<vscale x 4 x i1> %a, i64 0)
+ %elt = extractelement <vscale x 2 x i1> %subvec, i32 1
+ ret i1 %elt
+}
|
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.
Sounds reasonable
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.
Add a negative test with non-zero vector.extract?
Also test a variable index extract?
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.
Thank you @nikic, while adding a variable index test I realised this case wasn't handled.
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.
Something to consider but otherwise this looks good to me.
if (II && II->getIntrinsicID() == Intrinsic::vector_extract) | ||
if (cast<ConstantInt>(II->getArgOperand(1))->isZero()) | ||
return ExtractElementInst::Create(II->getArgOperand(0), Index); |
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.
Up to you but using PatternMatch would remove the need for II and casts. For example:
Value *V;
if (match(SrcVec, m_Intrinsic<Intrinsic::vector_extract>(m_Value(V), m_Zero())))
return ExtractElementInst::Create(V, Index);
Extracting any element from a subvector starting at index 0 is
equivalent to extracting from the original vector, i.e.
extract_elt(vector_extract(x, 0), y) -> extract_elt(x, y)