Skip to content

Commit af21bc1

Browse files
committed
[SLP]Fix a crash on attempt to revectorize vectorized phi.
If the PHI node is vectorized during vectorization of its operands, no need to try to vectorize its operands once again.
1 parent 4a01079 commit af21bc1

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -18732,6 +18732,11 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
1873218732
},
1873318733
/*MaxVFOnly=*/true, R);
1873418734
Changed |= HaveVectorizedPhiNodes;
18735+
if (HaveVectorizedPhiNodes && any_of(PHIToOpcodes, [&](const auto &P) {
18736+
auto *PHI = dyn_cast<PHINode>(P.first);
18737+
return !PHI || R.isDeleted(PHI);
18738+
}))
18739+
PHIToOpcodes.clear();
1873518740
VisitedInstrs.insert(Incoming.begin(), Incoming.end());
1873618741
} while (HaveVectorizedPhiNodes);
1873718742

@@ -18804,7 +18809,7 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
1880418809
}
1880518810
// Try to vectorize the incoming values of the PHI, to catch reductions
1880618811
// that feed into PHIs.
18807-
for (unsigned I = 0, E = P->getNumIncomingValues(); I != E; I++) {
18812+
for (unsigned I : seq<unsigned>(P->getNumIncomingValues())) {
1880818813
// Skip if the incoming block is the current BB for now. Also, bypass
1880918814
// unreachable IR for efficiency and to avoid crashing.
1881018815
// TODO: Collect the skipped incoming values and try to vectorize them
@@ -18816,9 +18821,16 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
1881618821
// Postponed instructions should not be vectorized here, delay their
1881718822
// vectorization.
1881818823
if (auto *PI = dyn_cast<Instruction>(P->getIncomingValue(I));
18819-
PI && !IsInPostProcessInstrs(PI))
18820-
Changed |= vectorizeRootInstruction(nullptr, PI,
18824+
PI && !IsInPostProcessInstrs(PI)) {
18825+
bool Res = vectorizeRootInstruction(nullptr, PI,
1882118826
P->getIncomingBlock(I), R, TTI);
18827+
Changed |= Res;
18828+
if (Res && R.isDeleted(P)) {
18829+
It = BB->begin();
18830+
E = BB->end();
18831+
break;
18832+
}
18833+
}
1882218834
}
1882318835
continue;
1882418836
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S --passes=slp-vectorizer -mtriple=i386-pc-windows-msvc19.34.0 -mcpu=pentium4 < %s | FileCheck %s
3+
4+
define i32 @test(double %mul321.i) {
5+
; CHECK-LABEL: define i32 @test(
6+
; CHECK-SAME: double [[MUL321_I:%.*]]) #[[ATTR0:[0-9]+]] {
7+
; CHECK-NEXT: [[ENTRY:.*]]:
8+
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x double> poison, double [[MUL321_I]], i32 0
9+
; CHECK-NEXT: br label %[[DO_BODY220_I:.*]]
10+
; CHECK: [[DO_BODY220_I]]:
11+
; CHECK-NEXT: [[TMP1:%.*]] = phi <2 x double> [ [[TMP6:%.*]], %[[DO_BODY221_I:.*]] ], [ zeroinitializer, %[[ENTRY]] ]
12+
; CHECK-NEXT: br label %[[DO_BODY221_I]]
13+
; CHECK: [[DO_BODY221_I]]:
14+
; CHECK-NEXT: [[TMP2:%.*]] = fadd <2 x double> [[TMP1]], zeroinitializer
15+
; CHECK-NEXT: [[TMP3:%.*]] = fmul <2 x double> [[TMP2]], zeroinitializer
16+
; CHECK-NEXT: [[TMP4:%.*]] = extractelement <2 x double> [[TMP3]], i32 0
17+
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x double> [[TMP3]], i32 1
18+
; CHECK-NEXT: [[ADD318_I:%.*]] = fadd double [[TMP4]], [[TMP5]]
19+
; CHECK-NEXT: [[TMP6]] = insertelement <2 x double> [[TMP0]], double [[ADD318_I]], i32 1
20+
; CHECK-NEXT: br label %[[DO_BODY220_I]]
21+
;
22+
entry:
23+
br label %do.body220.i
24+
25+
do.body220.i:
26+
%c1.2.i = phi double [ %mul321.i, %do.body221.i ], [ 0.000000e+00, %entry ]
27+
%s1.1.i = phi double [ %add318.i, %do.body221.i ], [ 0.000000e+00, %entry ]
28+
br label %do.body221.i
29+
30+
do.body221.i: ; preds = %do.body220.i
31+
%sub311.i1 = fadd double %c1.2.i, 0.000000e+00
32+
%add315.i = fadd double %s1.1.i, 0.000000e+00
33+
%mul316.i = fmul double %sub311.i1, 0.000000e+00
34+
%mul317.i = fmul double %add315.i, 0.000000e+00
35+
%add318.i = fadd double %mul316.i, %mul317.i
36+
br label %do.body220.i
37+
}

0 commit comments

Comments
 (0)