Skip to content

Commit 0d2efbb

Browse files
committed
[LV] Always use add to add scalar iv and (startidx + step) for ints.
In the integer case, step will be negative and InductionOpCode will be Sub for inductions counting down. By using the InductionOpCode for integers, we would incorrectly subtract a negative value, when it should be added instead. This fixes #54427 on the 14.x branch.
1 parent e7a9fd4 commit 0d2efbb

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,19 +2544,19 @@ void InnerLoopVectorizer::widenIntOrFpInduction(
25442544
Type *ScalarTy = IntegerType::get(ScalarIV->getContext(),
25452545
Step->getType()->getScalarSizeInBits());
25462546

2547-
Instruction::BinaryOps IncOp = ID.getInductionOpcode();
2548-
if (IncOp == Instruction::BinaryOpsEnd)
2549-
IncOp = Instruction::Add;
25502547
for (unsigned Part = 0; Part < UF; ++Part) {
25512548
Value *StartIdx = ConstantInt::get(ScalarTy, Part);
2552-
Instruction::BinaryOps MulOp = Instruction::Mul;
2549+
Value *EntryPart;
25532550
if (Step->getType()->isFloatingPointTy()) {
25542551
StartIdx = Builder.CreateUIToFP(StartIdx, Step->getType());
2555-
MulOp = Instruction::FMul;
2552+
Value *MulOp = Builder.CreateFMul(StartIdx, Step);
2553+
EntryPart = Builder.CreateBinOp(ID.getInductionOpcode(), ScalarIV,
2554+
MulOp, "induction");
2555+
} else {
2556+
EntryPart = Builder.CreateAdd(
2557+
ScalarIV, Builder.CreateMul(StartIdx, Step), "induction");
2558+
EntryPart->dump();
25562559
}
2557-
2558-
Value *Mul = Builder.CreateBinOp(MulOp, StartIdx, Step);
2559-
Value *EntryPart = Builder.CreateBinOp(IncOp, ScalarIV, Mul, "induction");
25602560
State.set(Def, EntryPart, Part);
25612561
if (Trunc) {
25622562
assert(!Step->getType()->isFloatingPointTy() &&

llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ define void @test_nonconst_start_and_step(i32* %dst, i32 %start, i32 %step, i64
1515
; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[TMP2]], [[NEG_STEP]]
1616
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i32 %start, [[TMP3]]
1717
; CHECK-NEXT: [[TMP4:%.*]] = mul i32 0, [[NEG_STEP]]
18-
; CHECK-NEXT: [[INDUCTION:%.*]] = sub i32 [[OFFSET_IDX]], [[TMP4]]
18+
; CHECK-NEXT: [[INDUCTION:%.*]] = add i32 [[OFFSET_IDX]], [[TMP4]]
1919
; CHECK-NEXT: [[TMP5:%.*]] = mul i32 1, [[NEG_STEP]]
20-
; CHECK-NEXT: [[INDUCTION2:%.*]] = sub i32 [[OFFSET_IDX]], [[TMP5]]
20+
; CHECK-NEXT: [[INDUCTION2:%.*]] = add i32 [[OFFSET_IDX]], [[TMP5]]
2121
; CHECK-NEXT: [[TMP6:%.*]] = sub nsw i32 [[INDUCTION]], %step
2222
; CHECK-NEXT: [[TMP7:%.*]] = sub nsw i32 [[INDUCTION2]], %step
2323
; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[DST:%.*]], i64 [[INDUCTION3]]

0 commit comments

Comments
 (0)