Skip to content

Commit

Permalink
[SLP]Fix PR101213: Reuse extractelement, only if its vector operand c…
Browse files Browse the repository at this point in the history
…omes before new vector value.

When trying to reuse extractelement instruction, need to check that it
is inserted into proper position. Its original vector operand should
come before new vector value, otherwise new extractelement instruction
must be generated.

Fixes llvm#101213
  • Loading branch information
alexey-bataev committed Jul 30, 2024
1 parent 8364a6e commit f70f122
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13909,11 +13909,16 @@ Value *BoUpSLP::vectorizeTree(
}
if (!Ex) {
// "Reuse" the existing extract to improve final codegen.
if (auto *ES = dyn_cast<ExtractElementInst>(Scalar)) {
if (auto *ES = dyn_cast<ExtractElementInst>(Scalar);
ES && isa<Instruction>(Vec)) {
Value *V = ES->getVectorOperand();
if (const TreeEntry *ETE = getTreeEntry(V))
V = ETE->VectorizedValue;
Ex = Builder.CreateExtractElement(V, ES->getIndexOperand());
if (auto *IV = dyn_cast<Instruction>(V);
!IV || IV == Vec || IV->comesBefore(cast<Instruction>(Vec)))
Ex = Builder.CreateExtractElement(V, ES->getIndexOperand());
else
Ex = Builder.CreateExtractElement(Vec, Lane);
} else if (ReplaceGEP) {
// Leave the GEPs as is, they are free in most cases and better to
// keep them as GEPs.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S --passes=slp-vectorizer -slp-threshold=-99999 < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s

define void @test() {
; CHECK-LABEL: define void @test() {
; CHECK-NEXT: [[BB:.*]]:
; CHECK-NEXT: br label %[[BB43:.*]]
; CHECK: [[BB20:.*]]:
; CHECK-NEXT: br label %[[BB105:.*]]
; CHECK: [[BB43]]:
; CHECK-NEXT: [[TMP0:%.*]] = phi <2 x ptr addrspace(1)> [ [[TMP1:%.*]], %[[BB51:.*]] ], [ zeroinitializer, %[[BB]] ]
; CHECK-NEXT: br i1 false, label %[[BB105]], label %[[BB51]]
; CHECK: [[BB51]]:
; CHECK-NEXT: [[TMP1]] = phi <2 x ptr addrspace(1)> [ poison, %[[BB54:.*]] ], [ zeroinitializer, %[[BB43]] ]
; CHECK-NEXT: br label %[[BB43]]
; CHECK: [[BB54]]:
; CHECK-NEXT: br label %[[BB51]]
; CHECK: [[BB105]]:
; CHECK-NEXT: [[PHI106:%.*]] = phi ptr addrspace(1) [ null, %[[BB20]] ], [ null, %[[BB43]] ]
; CHECK-NEXT: ret void
;
bb:
%0 = shufflevector <2 x ptr addrspace(1)> zeroinitializer, <2 x ptr addrspace(1)> zeroinitializer, <2 x i32> <i32 1, i32 0>
%1 = extractelement <2 x ptr addrspace(1)> %0, i32 0
%2 = extractelement <2 x ptr addrspace(1)> %0, i32 1
br label %bb43

bb20:
br label %bb105

bb43:
%phi441 = phi ptr addrspace(1) [ %4, %bb51 ], [ %2, %bb ]
%phi452 = phi ptr addrspace(1) [ %5, %bb51 ], [ %1, %bb ]
br i1 false, label %bb105, label %bb51

bb51:
%3 = phi <2 x ptr addrspace(1)> [ poison, %bb54 ], [ zeroinitializer, %bb43 ]
%4 = extractelement <2 x ptr addrspace(1)> %3, i32 0
%5 = extractelement <2 x ptr addrspace(1)> %3, i32 1
br label %bb43

bb54:
br label %bb51

bb105:
%phi106 = phi ptr addrspace(1) [ %1, %bb20 ], [ null, %bb43 ]
ret void
}

0 comments on commit f70f122

Please sign in to comment.