Skip to content

Commit 6b7b18a

Browse files
committed
[SLP]Fix PR87329: crash on alternate cast vectorization.
Need to fix the analysis for the alternate instructions, based on int extension operations. If the alternate extension node is resized, but not the operand, need to resize the node and do not shuffle final result, we end up only with trunc instruction.
1 parent 15abe09 commit 6b7b18a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9061,6 +9061,17 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
90619061
Type *Src1SclTy = E->getAltOp()->getOperand(0)->getType();
90629062
auto *Src0Ty = FixedVectorType::get(Src0SclTy, VL.size());
90639063
auto *Src1Ty = FixedVectorType::get(Src1SclTy, VL.size());
9064+
if (It != MinBWs.end()) {
9065+
if (!MinBWs.contains(getOperandEntry(E, 0)))
9066+
VecCost =
9067+
TTIRef.getCastInstrCost(Instruction::Trunc, VecTy, Src0Ty,
9068+
TTI::CastContextHint::None, CostKind);
9069+
LLVM_DEBUG({
9070+
dbgs() << "SLP: alternate extension, which should be truncated.\n";
9071+
E->dump();
9072+
});
9073+
return VecCost;
9074+
}
90649075
VecCost = TTIRef.getCastInstrCost(E->getOpcode(), VecTy, Src0Ty,
90659076
TTI::CastContextHint::None, CostKind);
90669077
VecCost +=
@@ -12571,6 +12582,16 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1257112582
CmpInst::Predicate AltPred = AltCI->getPredicate();
1257212583
V1 = Builder.CreateCmp(AltPred, LHS, RHS);
1257312584
} else {
12585+
if (It != MinBWs.end()) {
12586+
if (!MinBWs.contains(getOperandEntry(E, 0)))
12587+
LHS = Builder.CreateIntCast(LHS, VecTy, It->second.first);
12588+
assert(LHS->getType() == VecTy && "Expected same type as operand.");
12589+
if (auto *I = dyn_cast<Instruction>(LHS))
12590+
LHS = propagateMetadata(I, E->Scalars);
12591+
E->VectorizedValue = LHS;
12592+
++NumVectorInstructions;
12593+
return LHS;
12594+
}
1257412595
V0 = Builder.CreateCast(
1257512596
static_cast<Instruction::CastOps>(E->getOpcode()), LHS, VecTy);
1257612597
V1 = Builder.CreateCast(
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 4
2+
; RUN: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu -slp-threshold=-99999 < %s | FileCheck %s
3+
4+
define i64 @wombat() {
5+
; CHECK-LABEL: define i64 @wombat() {
6+
; CHECK-NEXT: bb:
7+
; CHECK-NEXT: br label [[BB2:%.*]]
8+
; CHECK: bb1:
9+
; CHECK-NEXT: br label [[BB2]]
10+
; CHECK: bb2:
11+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 0, [[BB:%.*]] ], [ 0, [[BB1:%.*]] ]
12+
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i32> poison, i32 [[PHI]], i32 0
13+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <2 x i32> [[TMP0]], <2 x i32> poison, <2 x i32> zeroinitializer
14+
; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i32> [[TMP1]] to <2 x i1>
15+
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i1> [[TMP2]], i32 0
16+
; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i64
17+
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i1> [[TMP2]], i32 1
18+
; CHECK-NEXT: [[TMP6:%.*]] = zext i1 [[TMP5]] to i64
19+
; CHECK-NEXT: [[OR:%.*]] = or i64 [[TMP4]], [[TMP6]]
20+
; CHECK-NEXT: ret i64 [[OR]]
21+
;
22+
bb:
23+
br label %bb2
24+
25+
bb1:
26+
br label %bb2
27+
28+
bb2:
29+
%phi = phi i32 [ 0, %bb ], [ 0, %bb1 ]
30+
%zext = zext i32 %phi to i64
31+
%sext = sext i32 %phi to i64
32+
%or = or i64 %zext, %sext
33+
ret i64 %or
34+
}

0 commit comments

Comments
 (0)