Skip to content

Commit c411965

Browse files
committed
[SLP]Fix PR61224: Compiler hits infinite loop.
IRBuilder in many cases is able to fold constant code automatically, but in some cases (for some intrinsics) it cannot do it. Need to perform manual calculation, if constant provided in these corner cases, to avoid infinite loop.
1 parent 6b53881 commit c411965

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12235,6 +12235,7 @@ class HorizontalReduction {
1223512235
static Value *createOp(IRBuilder<> &Builder, RecurKind Kind, Value *LHS,
1223612236
Value *RHS, const Twine &Name, bool UseSelect) {
1223712237
unsigned RdxOpcode = RecurrenceDescriptor::getOpcode(Kind);
12238+
bool IsConstant = isConstant(LHS) && isConstant(RHS);
1223812239
switch (Kind) {
1223912240
case RecurKind::Or:
1224012241
if (UseSelect &&
@@ -12256,29 +12257,37 @@ class HorizontalReduction {
1225612257
return Builder.CreateBinOp((Instruction::BinaryOps)RdxOpcode, LHS, RHS,
1225712258
Name);
1225812259
case RecurKind::FMax:
12260+
if (IsConstant)
12261+
return ConstantFP::get(LHS->getType(),
12262+
maxnum(cast<ConstantFP>(LHS)->getValueAPF(),
12263+
cast<ConstantFP>(RHS)->getValueAPF()));
1225912264
return Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS);
1226012265
case RecurKind::FMin:
12266+
if (IsConstant)
12267+
return ConstantFP::get(LHS->getType(),
12268+
minnum(cast<ConstantFP>(LHS)->getValueAPF(),
12269+
cast<ConstantFP>(RHS)->getValueAPF()));
1226112270
return Builder.CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS);
1226212271
case RecurKind::SMax:
12263-
if (UseSelect) {
12272+
if (IsConstant || UseSelect) {
1226412273
Value *Cmp = Builder.CreateICmpSGT(LHS, RHS, Name);
1226512274
return Builder.CreateSelect(Cmp, LHS, RHS, Name);
1226612275
}
1226712276
return Builder.CreateBinaryIntrinsic(Intrinsic::smax, LHS, RHS);
1226812277
case RecurKind::SMin:
12269-
if (UseSelect) {
12278+
if (IsConstant || UseSelect) {
1227012279
Value *Cmp = Builder.CreateICmpSLT(LHS, RHS, Name);
1227112280
return Builder.CreateSelect(Cmp, LHS, RHS, Name);
1227212281
}
1227312282
return Builder.CreateBinaryIntrinsic(Intrinsic::smin, LHS, RHS);
1227412283
case RecurKind::UMax:
12275-
if (UseSelect) {
12284+
if (IsConstant || UseSelect) {
1227612285
Value *Cmp = Builder.CreateICmpUGT(LHS, RHS, Name);
1227712286
return Builder.CreateSelect(Cmp, LHS, RHS, Name);
1227812287
}
1227912288
return Builder.CreateBinaryIntrinsic(Intrinsic::umax, LHS, RHS);
1228012289
case RecurKind::UMin:
12281-
if (UseSelect) {
12290+
if (IsConstant || UseSelect) {
1228212291
Value *Cmp = Builder.CreateICmpULT(LHS, RHS, Name);
1228312292
return Builder.CreateSelect(Cmp, LHS, RHS, Name);
1228412293
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -mtriple=x86_64 -passes=slp-vectorizer < %s | FileCheck %s
3+
4+
define double @test(double %m, double %a) {
5+
; CHECK-LABEL: @test(
6+
; CHECK-NEXT: entry:
7+
; CHECK-NEXT: [[TMP0:%.*]] = call fast double @llvm.maxnum.f64(double 0.000000e+00, double [[M:%.*]])
8+
; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.maxnum.f64(double [[TMP0]], double [[A:%.*]])
9+
; CHECK-NEXT: ret double [[TMP1]]
10+
;
11+
entry:
12+
%c = tail call fast double @llvm.maxnum.f64(double %m, double 0.000000e+00)
13+
%c1 = tail call fast double @llvm.maxnum.f64(double %a, double %c)
14+
%c2 = tail call fast double @llvm.maxnum.f64(double %c1, double 0.000000e+00)
15+
ret double %c2
16+
}
17+
18+
declare double @llvm.maxnum.f64(double, double)

0 commit comments

Comments
 (0)