Skip to content

Commit 441f94f

Browse files
committed
[SLP]Fix PR102279: check the tracked values for extractelements, not the original values
If the reduced value was replaced by the extractelement instruction during vectorization and we attempt to check if this is so, need to check the tracked value, not the original (deleted) instruction. Otherwise, the compiler may crash Fixes #102279
1 parent f7e1efe commit 441f94f

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17336,27 +17336,23 @@ class HorizontalReduction {
1733617336
// Try to handle shuffled extractelements.
1733717337
if (S.getOpcode() == Instruction::ExtractElement && !S.isAltShuffle() &&
1733817338
I + 1 < E) {
17339-
InstructionsState NextS = getSameOpcode(ReducedVals[I + 1], TLI);
17340-
if (NextS.getOpcode() == Instruction::ExtractElement &&
17341-
!NextS.isAltShuffle()) {
17342-
SmallVector<Value *> CommonCandidates(Candidates);
17343-
for (Value *RV : ReducedVals[I + 1]) {
17344-
Value *RdxVal = TrackedVals.find(RV)->second;
17345-
// Check if the reduction value was not overriden by the
17346-
// extractelement instruction because of the vectorization and
17347-
// exclude it, if it is not compatible with other values.
17348-
if (auto *Inst = dyn_cast<Instruction>(RdxVal))
17349-
if (!NextS.getOpcode() || !NextS.isOpcodeOrAlt(Inst))
17350-
continue;
17351-
CommonCandidates.push_back(RdxVal);
17352-
TrackedToOrig.try_emplace(RdxVal, RV);
17353-
}
17354-
SmallVector<int> Mask;
17355-
if (isFixedVectorShuffle(CommonCandidates, Mask)) {
17356-
++I;
17357-
Candidates.swap(CommonCandidates);
17358-
ShuffledExtracts = true;
17359-
}
17339+
SmallVector<Value *> CommonCandidates(Candidates);
17340+
for (Value *RV : ReducedVals[I + 1]) {
17341+
Value *RdxVal = TrackedVals.find(RV)->second;
17342+
// Check if the reduction value was not overriden by the
17343+
// extractelement instruction because of the vectorization and
17344+
// exclude it, if it is not compatible with other values.
17345+
auto *Inst = dyn_cast<ExtractElementInst>(RdxVal);
17346+
if (!Inst)
17347+
continue;
17348+
CommonCandidates.push_back(RdxVal);
17349+
TrackedToOrig.try_emplace(RdxVal, RV);
17350+
}
17351+
SmallVector<int> Mask;
17352+
if (isFixedVectorShuffle(CommonCandidates, Mask)) {
17353+
++I;
17354+
Candidates.swap(CommonCandidates);
17355+
ShuffledExtracts = true;
1736017356
}
1736117357
}
1736217358

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S --passes=slp-vectorizer -slp-threshold=-99999 < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s
3+
4+
define void @test() {
5+
; CHECK-LABEL: define void @test() {
6+
; CHECK-NEXT: [[BB:.*]]:
7+
; CHECK-NEXT: br label %[[BB1:.*]]
8+
; CHECK: [[BB1]]:
9+
; CHECK-NEXT: [[TMP0:%.*]] = phi <2 x i32> [ zeroinitializer, %[[BB]] ], [ [[TMP4:%.*]], %[[BB1]] ]
10+
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[TMP0]], i32 1
11+
; CHECK-NEXT: [[TMP2:%.*]] = call i8 @llvm.vector.reduce.mul.v4i8(<4 x i8> zeroinitializer)
12+
; CHECK-NEXT: [[TMP3:%.*]] = zext i8 [[TMP2]] to i32
13+
; CHECK-NEXT: [[OP_RDX:%.*]] = mul i32 [[TMP3]], [[TMP1]]
14+
; CHECK-NEXT: [[OP_RDX1:%.*]] = mul i32 [[OP_RDX]], 0
15+
; CHECK-NEXT: [[TMP4]] = insertelement <2 x i32> <i32 0, i32 poison>, i32 [[OP_RDX1]], i32 1
16+
; CHECK-NEXT: br label %[[BB1]]
17+
;
18+
bb:
19+
br label %bb1
20+
21+
bb1:
22+
%phi = phi i32 [ 0, %bb ], [ %mul9, %bb1 ]
23+
%phi2 = phi i32 [ 0, %bb ], [ 0, %bb1 ]
24+
%trunc = trunc i64 0 to i32
25+
%mul = mul i32 0, %trunc
26+
%mul3 = mul i32 %trunc, %phi
27+
%mul4 = mul i32 %mul3, %mul
28+
%mul5 = mul i32 %mul4, %mul
29+
%trunc6 = trunc i64 0 to i32
30+
%mul7 = mul i32 0, %trunc6
31+
%mul8 = mul i32 %mul5, %mul7
32+
%mul9 = mul i32 %mul8, %mul7
33+
br label %bb1
34+
}

llvm/test/Transforms/SLPVectorizer/X86/reduction-gather-non-scheduled-extracts.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ define void @tes() {
1010
; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <2 x i1> zeroinitializer, <2 x i1> [[TMP0]], <4 x i32> <i32 0, i32 0, i32 0, i32 2>
1111
; CHECK-NEXT: [[TMP4:%.*]] = call i1 @llvm.vector.reduce.and.v4i1(<4 x i1> [[TMP3]])
1212
; CHECK-NEXT: [[OP_RDX:%.*]] = select i1 false, i1 [[TMP4]], i1 false
13-
; CHECK-NEXT: br i1 [[OP_RDX]], label [[TMP6:%.*]], label [[TMP5:%.*]]
13+
; CHECK-NEXT: [[OP_RDX1:%.*]] = select i1 false, i1 [[OP_RDX]], i1 false
14+
; CHECK-NEXT: br i1 [[OP_RDX1]], label [[TMP6:%.*]], label [[TMP5:%.*]]
1415
; CHECK: 4:
1516
; CHECK-NEXT: ret void
1617
; CHECK: 5:

0 commit comments

Comments
 (0)