Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
// If extracting a specified index from the vector, see if we can recursively
// find a previously computed scalar that was inserted into the vector.
auto *IndexC = dyn_cast<ConstantInt>(Index);
auto *II = dyn_cast<IntrinsicInst>(SrcVec);
bool HasKnownValidIndex = false;
if (IndexC) {
// Canonicalize type of constant indices to i64 to simplify CSE
Expand All @@ -429,7 +430,7 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
unsigned NumElts = EC.getKnownMinValue();
HasKnownValidIndex = IndexC->getValue().ult(NumElts);

if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(SrcVec)) {
if (II) {
Intrinsic::ID IID = II->getIntrinsicID();
// Index needs to be lower than the minimum size of the vector, because
// for scalable vector, the vector size is known at run time.
Expand Down Expand Up @@ -462,6 +463,12 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
return ScalarPHI;
}

// If SrcVec is a subvector starting at index 0, extract from the
// wider source vector
if (II && II->getIntrinsicID() == Intrinsic::vector_extract)
if (cast<ConstantInt>(II->getArgOperand(1))->isZero())
return ExtractElementInst::Create(II->getArgOperand(0), Index);
Copy link
Collaborator

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);  


// TODO come up with a n-ary matcher that subsumes both unary and
// binary matchers.
UnaryOperator *UO;
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; 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 @extract_const_idx(<vscale x 4 x i1> %a) {
; CHECK-LABEL: define i1 @extract_const_idx(
; 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
}

define float @extract_variable_idx(<vscale x 4 x float> %a, i32 %idx) {
; CHECK-LABEL: define float @extract_variable_idx(
; CHECK-SAME: <vscale x 4 x float> [[A:%.*]], i32 [[IDX:%.*]]) {
; CHECK-NEXT: [[ELT:%.*]] = extractelement <vscale x 4 x float> [[A]], i32 [[IDX]]
; CHECK-NEXT: ret float [[ELT]]
;
%subvec = call <vscale x 2 x float> @llvm.vector.extract.nxv2f32.nxv4f32.i64(<vscale x 4 x float> %a, i64 0)
%elt = extractelement <vscale x 2 x float> %subvec, i32 %idx
ret float %elt
}

define float @negative_test(<vscale x 4 x float> %a) {
; CHECK-LABEL: define float @negative_test(
; CHECK-SAME: <vscale x 4 x float> [[A:%.*]]) {
; CHECK-NEXT: [[SUBVEC:%.*]] = call <vscale x 2 x float> @llvm.vector.extract.nxv2f32.nxv4f32(<vscale x 4 x float> [[A]], i64 2)
; CHECK-NEXT: [[ELT:%.*]] = extractelement <vscale x 2 x float> [[SUBVEC]], i64 1
; CHECK-NEXT: ret float [[ELT]]
;
%subvec = call <vscale x 2 x float> @llvm.vector.extract.nxv2f32.nxv4f32.i64(<vscale x 4 x float> %a, i64 2)
%elt = extractelement <vscale x 2 x float> %subvec, i32 1
ret float %elt
}