Skip to content

Commit 07015e1

Browse files
committed
[SLP]Fix PR59053: trying to erase instruction with users.
Need to count the reduced values, vectorized in the tree but not in the top node. Such scalars still must be extracted out of the vector node instead of the original scalar.
1 parent 6b852ff commit 07015e1

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11873,23 +11873,36 @@ class HorizontalReduction {
1187311873
Value *V = Candidates[Cnt];
1187411874
++NumUses.try_emplace(V, 0).first->getSecond();
1187511875
}
11876+
SmallPtrSet<Value *, 4> VLScalars(VL.begin(), VL.end());
1187611877
// Gather externally used values.
1187711878
SmallPtrSet<Value *, 4> Visited;
1187811879
for (unsigned Cnt = 0; Cnt < Pos; ++Cnt) {
11879-
Value *V = Candidates[Cnt];
11880-
if (!Visited.insert(V).second)
11880+
Value *RdxVal = Candidates[Cnt];
11881+
if (!Visited.insert(RdxVal).second)
11882+
continue;
11883+
// Check if the scalar was vectorized as part of the vectorization
11884+
// tree but not the top node.
11885+
if (!VLScalars.contains(RdxVal) && V.isVectorized(RdxVal)) {
11886+
LocalExternallyUsedValues[RdxVal];
1188111887
continue;
11882-
unsigned NumOps = VectorizedVals.lookup(V) + NumUses[V];
11883-
if (NumOps != ReducedValsToOps.find(V)->second.size())
11884-
LocalExternallyUsedValues[V];
11888+
}
11889+
unsigned NumOps = VectorizedVals.lookup(RdxVal) + NumUses[RdxVal];
11890+
if (NumOps != ReducedValsToOps.find(RdxVal)->second.size())
11891+
LocalExternallyUsedValues[RdxVal];
1188511892
}
1188611893
for (unsigned Cnt = Pos + ReduxWidth; Cnt < NumReducedVals; ++Cnt) {
11887-
Value *V = Candidates[Cnt];
11888-
if (!Visited.insert(V).second)
11894+
Value *RdxVal = Candidates[Cnt];
11895+
if (!Visited.insert(RdxVal).second)
11896+
continue;
11897+
// Check if the scalar was vectorized as part of the vectorization
11898+
// tree but not the top node.
11899+
if (!VLScalars.contains(RdxVal) && V.isVectorized(RdxVal)) {
11900+
LocalExternallyUsedValues[RdxVal];
1188911901
continue;
11890-
unsigned NumOps = VectorizedVals.lookup(V) + NumUses[V];
11891-
if (NumOps != ReducedValsToOps.find(V)->second.size())
11892-
LocalExternallyUsedValues[V];
11902+
}
11903+
unsigned NumOps = VectorizedVals.lookup(RdxVal) + NumUses[RdxVal];
11904+
if (NumOps != ReducedValsToOps.find(RdxVal)->second.size())
11905+
LocalExternallyUsedValues[RdxVal];
1189311906
}
1189411907
for (Value *RdxVal : VL)
1189511908
if (RequiredExtract.contains(RdxVal))
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -passes=slp-vectorizer -mtriple=x86_64-unknown-linux < %s | FileCheck %s
3+
define void @test() {
4+
; CHECK-LABEL: @test(
5+
; CHECK-NEXT: bb:
6+
; CHECK-NEXT: br i1 false, label [[PH:%.*]], label [[EXIT:%.*]]
7+
; CHECK: ph:
8+
; CHECK-NEXT: [[TMP0:%.*]] = call i8 @llvm.vector.reduce.and.v8i8(<8 x i8> zeroinitializer)
9+
; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.vector.reduce.and.v4i8(<4 x i8> zeroinitializer)
10+
; CHECK-NEXT: [[OP_RDX:%.*]] = and i8 [[TMP0]], [[TMP1]]
11+
; CHECK-NEXT: [[TMP2:%.*]] = call i8 @llvm.vector.reduce.and.v8i8(<8 x i8> zeroinitializer)
12+
; CHECK-NEXT: [[OP_RDX1:%.*]] = and i8 [[OP_RDX]], [[TMP2]]
13+
; CHECK-NEXT: [[OP_RDX2:%.*]] = and i8 [[OP_RDX1]], 0
14+
; CHECK-NEXT: br label [[EXIT]]
15+
; CHECK: exit:
16+
; CHECK-NEXT: [[PHI:%.*]] = phi i8 [ [[OP_RDX2]], [[PH]] ], [ 0, [[BB:%.*]] ]
17+
; CHECK-NEXT: ret void
18+
;
19+
bb:
20+
br i1 false, label %ph, label %exit
21+
22+
ph:
23+
%add1 = add i8 0, 0
24+
%add2 = add i8 %add1, 0
25+
%add3 = add i8 %add1, 0
26+
%add4 = add i8 %add1, 0
27+
%add5 = add i8 %add1, 0
28+
%add6 = add i8 %add1, 0
29+
%add7 = add i8 %add1, 0
30+
%add8 = add i8 %add1, 0
31+
%add9 = add i8 0, 0
32+
%add10 = add i8 0, %add9
33+
%0 = and i8 %add10, %add3
34+
%1 = and i8 %0, %add2
35+
%2 = and i8 %1, %add4
36+
%3 = and i8 %2, 0
37+
%4 = and i8 %3, %add5
38+
%5 = and i8 %4, %add6
39+
%6 = and i8 %5, 0
40+
%7 = and i8 %6, 0
41+
%8 = and i8 %7, 0
42+
%9 = and i8 %8, 0
43+
%10 = and i8 %9, 0
44+
%11 = and i8 %10, 0
45+
%12 = and i8 %11, 0
46+
%13 = and i8 %12, %add7
47+
%14 = and i8 %13, %add8
48+
%15 = and i8 %14, 0
49+
%16 = and i8 %15, 0
50+
%17 = and i8 %16, 0
51+
%18 = and i8 %17, 0
52+
%and = and i8 %18, %add1
53+
br label %exit
54+
55+
exit:
56+
%phi = phi i8 [ %and, %ph ], [ 0, %bb ]
57+
ret void
58+
}

0 commit comments

Comments
 (0)