Skip to content

Commit f609d4b

Browse files
committed
[SLP]Fix PR72833: do not crash if only operand is casted but the use
instruction. Need to check if only operand is casted, not the user instruction itself, if the types of the operands does not match the actual type.
1 parent c38ae74 commit f609d4b

File tree

2 files changed

+105
-29
lines changed

2 files changed

+105
-29
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11110,7 +11110,8 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1111011110
Builder.SetCurrentDebugLocation(PH->getDebugLoc());
1111111111
Value *Vec = vectorizeOperand(E, i, /*PostponedPHIs=*/true);
1111211112
if (VecTy != Vec->getType()) {
11113-
assert(It != MinBWs.end() && "Expected item in MinBWs.");
11113+
assert(MinBWs.contains(PH->getIncomingValue(i)) &&
11114+
"Expected item in MinBWs.");
1111411115
Vec = Builder.CreateIntCast(Vec, VecTy, It->second.second);
1111511116
}
1111611117
NewPhi->addIncoming(Vec, IBB);
@@ -11362,13 +11363,11 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1136211363
return E->VectorizedValue;
1136311364
}
1136411365
if (L->getType() != R->getType()) {
11365-
assert(It != MinBWs.end() && "Expected item in MinBWs.");
11366-
if (L == R) {
11367-
R = L = Builder.CreateIntCast(L, VecTy, IsSigned);
11368-
} else {
11369-
L = Builder.CreateIntCast(L, VecTy, IsSigned);
11370-
R = Builder.CreateIntCast(R, VecTy, IsSigned);
11371-
}
11366+
assert((MinBWs.contains(VL0->getOperand(0)) ||
11367+
MinBWs.contains(VL0->getOperand(1))) &&
11368+
"Expected item in MinBWs.");
11369+
L = Builder.CreateIntCast(L, VecTy, IsSigned);
11370+
R = Builder.CreateIntCast(R, VecTy, IsSigned);
1137211371
}
1137311372

1137411373
CmpInst::Predicate P0 = cast<CmpInst>(VL0)->getPredicate();
@@ -11401,13 +11400,11 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1140111400
return E->VectorizedValue;
1140211401
}
1140311402
if (True->getType() != False->getType()) {
11404-
assert(It != MinBWs.end() && "Expected item in MinBWs.");
11405-
if (True == False) {
11406-
True = False = Builder.CreateIntCast(True, VecTy, IsSigned);
11407-
} else {
11408-
True = Builder.CreateIntCast(True, VecTy, IsSigned);
11409-
False = Builder.CreateIntCast(False, VecTy, IsSigned);
11410-
}
11403+
assert((MinBWs.contains(VL0->getOperand(1)) ||
11404+
MinBWs.contains(VL0->getOperand(2))) &&
11405+
"Expected item in MinBWs.");
11406+
True = Builder.CreateIntCast(True, VecTy, IsSigned);
11407+
False = Builder.CreateIntCast(False, VecTy, IsSigned);
1141111408
}
1141211409

1141311410
Value *V = Builder.CreateSelect(Cond, True, False);
@@ -11471,13 +11468,11 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1147111468
return E->VectorizedValue;
1147211469
}
1147311470
if (LHS->getType() != RHS->getType()) {
11474-
assert(It != MinBWs.end() && "Expected item in MinBWs.");
11475-
if (LHS == RHS) {
11476-
RHS = LHS = Builder.CreateIntCast(LHS, VecTy, IsSigned);
11477-
} else {
11478-
LHS = Builder.CreateIntCast(LHS, VecTy, IsSigned);
11479-
RHS = Builder.CreateIntCast(RHS, VecTy, IsSigned);
11480-
}
11471+
assert((MinBWs.contains(VL0->getOperand(0)) ||
11472+
MinBWs.contains(VL0->getOperand(1))) &&
11473+
"Expected item in MinBWs.");
11474+
LHS = Builder.CreateIntCast(LHS, VecTy, IsSigned);
11475+
RHS = Builder.CreateIntCast(RHS, VecTy, IsSigned);
1148111476
}
1148211477

1148311478
Value *V = Builder.CreateBinOp(
@@ -11710,13 +11705,11 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1171011705
return E->VectorizedValue;
1171111706
}
1171211707
if (LHS && RHS && LHS->getType() != RHS->getType()) {
11713-
assert(It != MinBWs.end() && "Expected item in MinBWs.");
11714-
if (LHS == RHS) {
11715-
RHS = LHS = Builder.CreateIntCast(LHS, VecTy, IsSigned);
11716-
} else {
11717-
LHS = Builder.CreateIntCast(LHS, VecTy, IsSigned);
11718-
RHS = Builder.CreateIntCast(RHS, VecTy, IsSigned);
11719-
}
11708+
assert((MinBWs.contains(VL0->getOperand(0)) ||
11709+
MinBWs.contains(VL0->getOperand(1))) &&
11710+
"Expected item in MinBWs.");
11711+
LHS = Builder.CreateIntCast(LHS, VecTy, IsSigned);
11712+
RHS = Builder.CreateIntCast(RHS, VecTy, IsSigned);
1172011713
}
1172111714

1172211715
Value *V0, *V1;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -passes=slp-vectorizer -S -slp-threshold=-6 -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
3+
4+
define void @test(i64 %d.promoted.i) {
5+
; CHECK-LABEL: define void @test(
6+
; CHECK-SAME: i64 [[D_PROMOTED_I:%.*]]) {
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i64> <i64 poison, i64 0>, i64 [[D_PROMOTED_I]], i32 0
9+
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i64> zeroinitializer, [[TMP0]]
10+
; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i1>
11+
; CHECK-NEXT: [[TMP3:%.*]] = mul <2 x i1> [[TMP2]], zeroinitializer
12+
; CHECK-NEXT: [[TMP4:%.*]] = or <2 x i1> [[TMP3]], zeroinitializer
13+
; CHECK-NEXT: [[TMP5:%.*]] = or <2 x i1> [[TMP4]], zeroinitializer
14+
; CHECK-NEXT: [[TMP6:%.*]] = or <2 x i1> [[TMP5]], zeroinitializer
15+
; CHECK-NEXT: [[TMP7:%.*]] = or <2 x i1> [[TMP6]], zeroinitializer
16+
; CHECK-NEXT: [[TMP8:%.*]] = or <2 x i1> [[TMP7]], zeroinitializer
17+
; CHECK-NEXT: [[TMP9:%.*]] = or <2 x i1> [[TMP8]], zeroinitializer
18+
; CHECK-NEXT: [[TMP10:%.*]] = or <2 x i1> [[TMP9]], zeroinitializer
19+
; CHECK-NEXT: [[TMP11:%.*]] = extractelement <2 x i1> [[TMP10]], i32 0
20+
; CHECK-NEXT: [[TMP12:%.*]] = sext i1 [[TMP11]] to i32
21+
; CHECK-NEXT: [[TMP13:%.*]] = extractelement <2 x i1> [[TMP10]], i32 1
22+
; CHECK-NEXT: [[TMP14:%.*]] = sext i1 [[TMP13]] to i32
23+
; CHECK-NEXT: [[TMP15:%.*]] = or i32 [[TMP12]], [[TMP14]]
24+
; CHECK-NEXT: [[TMP16:%.*]] = and i32 [[TMP15]], 0
25+
; CHECK-NEXT: store i32 [[TMP16]], ptr null, align 4
26+
; CHECK-NEXT: ret void
27+
;
28+
entry:
29+
%add.1.i = add i64 0, 0
30+
%and.1.i = and i64 %add.1.i, %d.promoted.i
31+
%conv12.1.i = trunc i64 %and.1.i to i32
32+
%mul.i.1.i = mul i32 %conv12.1.i, 0
33+
%conv12.i = trunc i64 0 to i32
34+
%mul.i.i = mul i32 %conv12.i, 0
35+
%conv14104.i = or i32 %mul.i.1.i, %mul.i.i
36+
%conv12.2.i = trunc i64 0 to i32
37+
%mul.i.2.i = mul i32 %conv12.2.i, 0
38+
%0 = or i32 %conv14104.i, %mul.i.2.i
39+
%conv12.182.i = trunc i64 0 to i32
40+
%mul.i.183.i = mul i32 %conv12.182.i, 0
41+
%1 = or i32 %0, %mul.i.183.i
42+
%conv12.1.1.i = trunc i64 0 to i32
43+
%mul.i.1.1.i = mul i32 %conv12.1.1.i, 0
44+
%2 = or i32 %1, %mul.i.1.1.i
45+
%conv12.2.1.i = trunc i64 0 to i32
46+
%mul.i.2.1.i = mul i32 %conv12.2.1.i, 0
47+
%3 = or i32 %2, %mul.i.2.1.i
48+
%conv12.297.i = trunc i64 0 to i32
49+
%mul.i.298.i = mul i32 %conv12.297.i, 0
50+
%4 = or i32 %3, %mul.i.298.i
51+
%conv12.1.2.i = trunc i64 0 to i32
52+
%mul.i.1.2.i = mul i32 %conv12.1.2.i, 0
53+
%5 = or i32 %4, %mul.i.1.2.i
54+
%add.1.i.1 = add i64 0, 0
55+
%and.1.i.1 = and i64 %add.1.i.1, 0
56+
%conv12.1.i.1 = trunc i64 %and.1.i.1 to i32
57+
%mul.i.1.i.1 = mul i32 %conv12.1.i.1, 0
58+
%conv12.i.1 = trunc i64 0 to i32
59+
%mul.i.i.1 = mul i32 %conv12.i.1, 0
60+
%conv14104.i.1 = or i32 %mul.i.1.i.1, %mul.i.i.1
61+
%conv12.2.i.1 = trunc i64 0 to i32
62+
%mul.i.2.i.1 = mul i32 %conv12.2.i.1, 0
63+
%6 = or i32 %conv14104.i.1, %mul.i.2.i.1
64+
%conv12.182.i.1 = trunc i64 0 to i32
65+
%mul.i.183.i.1 = mul i32 %conv12.182.i.1, 0
66+
%7 = or i32 %6, %mul.i.183.i.1
67+
%conv12.1.1.i.1 = trunc i64 0 to i32
68+
%mul.i.1.1.i.1 = mul i32 %conv12.1.1.i.1, 0
69+
%8 = or i32 %7, %mul.i.1.1.i.1
70+
%conv12.2.1.i.1 = trunc i64 0 to i32
71+
%mul.i.2.1.i.1 = mul i32 %conv12.2.1.i.1, 0
72+
%9 = or i32 %8, %mul.i.2.1.i.1
73+
%conv12.297.i.1 = trunc i64 0 to i32
74+
%mul.i.298.i.1 = mul i32 %conv12.297.i.1, 0
75+
%10 = or i32 %9, %mul.i.298.i.1
76+
%conv12.1.2.i.1 = trunc i64 0 to i32
77+
%mul.i.1.2.i.1 = mul i32 %conv12.1.2.i.1, 0
78+
%11 = or i32 %10, %mul.i.1.2.i.1
79+
%12 = or i32 %5, %11
80+
%13 = and i32 %12, 0
81+
store i32 %13, ptr null, align 4
82+
ret void
83+
}

0 commit comments

Comments
 (0)