[llvm-branch-commits] [llvm] 0d2efbb - [LV] Always use add to add scalar iv and (startidx + step) for ints.

Florian Hahn via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Apr 25 05:38:34 PDT 2022


Author: Florian Hahn
Date: 2022-04-25T13:18:22+01:00
New Revision: 0d2efbb8b82c13ea6e4aef727c5360eea6679605

URL: https://github.com/llvm/llvm-project/commit/0d2efbb8b82c13ea6e4aef727c5360eea6679605
DIFF: https://github.com/llvm/llvm-project/commit/0d2efbb8b82c13ea6e4aef727c5360eea6679605.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
    llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index e1cc7946073ea..93eaed6551302 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -2544,19 +2544,19 @@ void InnerLoopVectorizer::widenIntOrFpInduction(
     Type *ScalarTy = IntegerType::get(ScalarIV->getContext(),
                                       Step->getType()->getScalarSizeInBits());
 
-    Instruction::BinaryOps IncOp = ID.getInductionOpcode();
-    if (IncOp == Instruction::BinaryOpsEnd)
-      IncOp = Instruction::Add;
     for (unsigned Part = 0; Part < UF; ++Part) {
       Value *StartIdx = ConstantInt::get(ScalarTy, Part);
-      Instruction::BinaryOps MulOp = Instruction::Mul;
+      Value *EntryPart;
       if (Step->getType()->isFloatingPointTy()) {
         StartIdx = Builder.CreateUIToFP(StartIdx, Step->getType());
-        MulOp = Instruction::FMul;
+        Value *MulOp = Builder.CreateFMul(StartIdx, Step);
+        EntryPart = Builder.CreateBinOp(ID.getInductionOpcode(), ScalarIV,
+                                        MulOp, "induction");
+      } else {
+        EntryPart = Builder.CreateAdd(
+            ScalarIV, Builder.CreateMul(StartIdx, Step), "induction");
+        EntryPart->dump();
       }
-
-      Value *Mul = Builder.CreateBinOp(MulOp, StartIdx, Step);
-      Value *EntryPart = Builder.CreateBinOp(IncOp, ScalarIV, Mul, "induction");
       State.set(Def, EntryPart, Part);
       if (Trunc) {
         assert(!Step->getType()->isFloatingPointTy() &&

diff  --git a/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll b/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll
index a4e595e98fb7b..5266b3f9eaecd 100644
--- a/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll
+++ b/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll
@@ -15,9 +15,9 @@ define void @test_nonconst_start_and_step(i32* %dst, i32 %start, i32 %step, i64
 ; CHECK-NEXT:    [[TMP3:%.*]] = mul i32 [[TMP2]], [[NEG_STEP]]
 ; CHECK-NEXT:    [[OFFSET_IDX:%.*]] = add i32 %start, [[TMP3]]
 ; CHECK-NEXT:    [[TMP4:%.*]] = mul i32 0, [[NEG_STEP]]
-; CHECK-NEXT:    [[INDUCTION:%.*]] = sub i32 [[OFFSET_IDX]], [[TMP4]]
+; CHECK-NEXT:    [[INDUCTION:%.*]] = add i32 [[OFFSET_IDX]], [[TMP4]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 1, [[NEG_STEP]]
-; CHECK-NEXT:    [[INDUCTION2:%.*]] = sub i32 [[OFFSET_IDX]], [[TMP5]]
+; CHECK-NEXT:    [[INDUCTION2:%.*]] = add i32 [[OFFSET_IDX]], [[TMP5]]
 ; CHECK-NEXT:    [[TMP6:%.*]] = sub nsw i32 [[INDUCTION]], %step
 ; CHECK-NEXT:    [[TMP7:%.*]] = sub nsw i32 [[INDUCTION2]], %step
 ; CHECK-NEXT:    [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[DST:%.*]], i64 [[INDUCTION3]]


        


More information about the llvm-branch-commits mailing list