Skip to content

Commit e84b2fb

Browse files
[LV][NFCI]Use integer for cost/trip count calculations instead of double, fix possible UB.
Using fp type in the compiler is not the best idea, here it used with the comparison for equal to 0 and may cause undefined behavior in some cases. Reviewers: fhahn Reviewed By: fhahn Pull Request: #87241
1 parent e185978 commit e84b2fb

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9722,7 +9722,7 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
97229722
}
97239723

97249724
// The scalar cost should only be 0 when vectorizing with a user specified VF/IC. In those cases, runtime checks should always be generated.
9725-
double ScalarC = *VF.ScalarCost.getValue();
9725+
uint64_t ScalarC = *VF.ScalarCost.getValue();
97269726
if (ScalarC == 0)
97279727
return true;
97289728

@@ -9749,7 +9749,7 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
97499749
// RtC + VecC * (TC / VF) + EpiC < ScalarC * TC
97509750
//
97519751
// Now we can compute the minimum required trip count TC as
9752-
// (RtC + EpiC) / (ScalarC - (VecC / VF)) < TC
9752+
// VF * (RtC + EpiC) / (ScalarC * VF - VecC) < TC
97539753
//
97549754
// For now we assume the epilogue cost EpiC = 0 for simplicity. Note that
97559755
// the computations are performed on doubles, not integers and the result
@@ -9761,9 +9761,9 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
97619761
AssumedMinimumVscale = *VScale;
97629762
IntVF *= AssumedMinimumVscale;
97639763
}
9764-
double VecCOverVF = double(*VF.Cost.getValue()) / IntVF;
9765-
double RtC = *CheckCost.getValue();
9766-
double MinTC1 = RtC / (ScalarC - VecCOverVF);
9764+
uint64_t RtC = *CheckCost.getValue();
9765+
uint64_t Div = ScalarC * IntVF - *VF.Cost.getValue();
9766+
uint64_t MinTC1 = Div == 0 ? 0 : divideCeil(RtC * IntVF, Div);
97679767

97689768
// Second, compute a minimum iteration count so that the cost of the
97699769
// runtime checks is only a fraction of the total scalar loop cost. This
@@ -9772,12 +9772,12 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
97729772
// * TC. To bound the runtime check to be a fraction 1/X of the scalar
97739773
// cost, compute
97749774
// RtC < ScalarC * TC * (1 / X) ==> RtC * X / ScalarC < TC
9775-
double MinTC2 = RtC * 10 / ScalarC;
9775+
uint64_t MinTC2 = divideCeil(RtC * 10, ScalarC);
97769776

97779777
// Now pick the larger minimum. If it is not a multiple of VF and a scalar
97789778
// epilogue is allowed, choose the next closest multiple of VF. This should
97799779
// partly compensate for ignoring the epilogue cost.
9780-
uint64_t MinTC = std::ceil(std::max(MinTC1, MinTC2));
9780+
uint64_t MinTC = std::max(MinTC1, MinTC2);
97819781
if (SEL == CM_ScalarEpilogueAllowed)
97829782
MinTC = alignTo(MinTC, IntVF);
97839783
VF.MinProfitableTripCount = ElementCount::getFixed(MinTC);

llvm/test/Transforms/LoopVectorize/vectorize-force-tail-with-evl.ll

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
; RUN: -prefer-predicate-over-epilogue=predicate-else-scalar-epilogue -force-vector-width=4 \
1010
; RUN: -force-target-supports-scalable-vectors -scalable-vectorization=on -S < %s | FileCheck --check-prefix=NO-VP %s
1111

12+
; RUN: opt -passes=loop-vectorize \
13+
; RUN: -force-tail-folding-style=none \
14+
; RUN: -prefer-predicate-over-epilogue=predicate-else-scalar-epilogue \
15+
; RUN: -force-target-supports-scalable-vectors -scalable-vectorization=on -S < %s | FileCheck --check-prefix=NO-VP-DEF %s
16+
1217
; The target does not support predicated vectorization.
1318
define void @foo(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
1419
; IF-EVL-LABEL: @foo(
@@ -80,6 +85,54 @@ define void @foo(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
8085
; NO-VP: for.cond.cleanup:
8186
; NO-VP-NEXT: ret void
8287
;
88+
; NO-VP-DEF-LABEL: @foo(
89+
; NO-VP-DEF-NEXT: entry:
90+
; NO-VP-DEF-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
91+
; NO-VP-DEF-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], [[TMP0]]
92+
; NO-VP-DEF-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
93+
; NO-VP-DEF: vector.ph:
94+
; NO-VP-DEF-NEXT: [[TMP1:%.*]] = call i64 @llvm.vscale.i64()
95+
; NO-VP-DEF-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP1]]
96+
; NO-VP-DEF-NEXT: [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
97+
; NO-VP-DEF-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
98+
; NO-VP-DEF-NEXT: br label [[VECTOR_BODY:%.*]]
99+
; NO-VP-DEF: vector.body:
100+
; NO-VP-DEF-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
101+
; NO-VP-DEF-NEXT: [[TMP3:%.*]] = add i64 [[INDEX]], 0
102+
; NO-VP-DEF-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[B:%.*]], i64 [[TMP3]]
103+
; NO-VP-DEF-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, ptr [[TMP4]], i32 0
104+
; NO-VP-DEF-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 1 x i32>, ptr [[TMP5]], align 4
105+
; NO-VP-DEF-NEXT: [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[C:%.*]], i64 [[TMP3]]
106+
; NO-VP-DEF-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[TMP6]], i32 0
107+
; NO-VP-DEF-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 1 x i32>, ptr [[TMP7]], align 4
108+
; NO-VP-DEF-NEXT: [[TMP8:%.*]] = add nsw <vscale x 1 x i32> [[WIDE_LOAD1]], [[WIDE_LOAD]]
109+
; NO-VP-DEF-NEXT: [[TMP9:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP3]]
110+
; NO-VP-DEF-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[TMP9]], i32 0
111+
; NO-VP-DEF-NEXT: store <vscale x 1 x i32> [[TMP8]], ptr [[TMP10]], align 4
112+
; NO-VP-DEF-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP2]]
113+
; NO-VP-DEF-NEXT: [[TMP11:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
114+
; NO-VP-DEF-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
115+
; NO-VP-DEF: middle.block:
116+
; NO-VP-DEF-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
117+
; NO-VP-DEF-NEXT: br i1 [[CMP_N]], label [[FOR_COND_CLEANUP:%.*]], label [[SCALAR_PH]]
118+
; NO-VP-DEF: scalar.ph:
119+
; NO-VP-DEF-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
120+
; NO-VP-DEF-NEXT: br label [[FOR_BODY:%.*]]
121+
; NO-VP-DEF: for.body:
122+
; NO-VP-DEF-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
123+
; NO-VP-DEF-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[IV]]
124+
; NO-VP-DEF-NEXT: [[TMP12:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
125+
; NO-VP-DEF-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds i32, ptr [[C]], i64 [[IV]]
126+
; NO-VP-DEF-NEXT: [[TMP13:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
127+
; NO-VP-DEF-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP13]], [[TMP12]]
128+
; NO-VP-DEF-NEXT: [[ARRAYIDX4:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[IV]]
129+
; NO-VP-DEF-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX4]], align 4
130+
; NO-VP-DEF-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
131+
; NO-VP-DEF-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
132+
; NO-VP-DEF-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_COND_CLEANUP]], label [[FOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
133+
; NO-VP-DEF: for.cond.cleanup:
134+
; NO-VP-DEF-NEXT: ret void
135+
;
83136
entry:
84137
br label %for.body
85138

0 commit comments

Comments
 (0)