Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fdim constant fold #109235

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions llvm/include/llvm/Analysis/TargetLibraryInfo.def
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,21 @@ TLI_DEFINE_ENUM_INTERNAL(remquol)
TLI_DEFINE_STRING_INTERNAL("remquol")
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, LDbl, Ptr)

/// double fdim(double x, double y);
TLI_DEFINE_ENUM_INTERNAL(fdim)
TLI_DEFINE_STRING_INTERNAL("fdim")
TLI_DEFINE_SIG_INTERNAL(Dbl, Dbl, Dbl)

/// float fdimf(float x, float y);
TLI_DEFINE_ENUM_INTERNAL(fdimf)
TLI_DEFINE_STRING_INTERNAL("fdimf")
TLI_DEFINE_SIG_INTERNAL(Flt, Flt, Flt)

/// long double fdiml(long double x, long double y);
TLI_DEFINE_ENUM_INTERNAL(fdiml)
TLI_DEFINE_STRING_INTERNAL("fdiml")
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, LDbl)

/// int remove(const char *path);
TLI_DEFINE_ENUM_INTERNAL(remove)
TLI_DEFINE_STRING_INTERNAL("remove")
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class LibCallSimplifier {
Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
// Wrapper for all floating point library call optimizations
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
IRBuilderBase &B);
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Analysis/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
TLI.setUnavailable(LibFunc_powf);
TLI.setUnavailable(LibFunc_remainderf);
TLI.setUnavailable(LibFunc_remquof);
TLI.setUnavailable(LibFunc_fdimf);
TLI.setUnavailable(LibFunc_sinf);
TLI.setUnavailable(LibFunc_sinhf);
TLI.setUnavailable(LibFunc_sqrtf);
Expand Down Expand Up @@ -337,6 +338,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
TLI.setUnavailable(LibFunc_powl);
TLI.setUnavailable(LibFunc_remainderl);
TLI.setUnavailable(LibFunc_remquol);
TLI.setUnavailable(LibFunc_fdiml);
TLI.setUnavailable(LibFunc_sinl);
TLI.setUnavailable(LibFunc_sinhl);
TLI.setUnavailable(LibFunc_sqrtl);
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,9 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
case LibFunc_fabs:
case LibFunc_fabsf:
case LibFunc_fabsl:
case LibFunc_fdim:
case LibFunc_fdiml:
case LibFunc_fdimf:
case LibFunc_ffs:
case LibFunc_ffsl:
case LibFunc_ffsll:
Expand Down
39 changes: 39 additions & 0 deletions llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3080,6 +3080,41 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
return ConstantFP::get(CI->getType(), Rem);
}

/// Constant folds fdim
Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
const APFloat *X, *Y;
// Check if both values are constants
if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
!match(CI->getArgOperand(1), m_APFloat(Y)))
return nullptr;
// If either argument is NaN, NaN is returned
if (X->isNaN() || Y->isNaN())
return ConstantFP::getQNaN(CI->getType());

// If the difference if negative, return +0.0
if (*X <= *Y)
return ConstantFP::get(CI->getType(), 0.0);

APFloat ReturnVal = *X;
APFloat::opStatus Status =
ReturnVal.subtract(*Y, RoundingMode::NearestTiesToEven);
switch (Status) {
case APFloat::opStatus::opOK:
break;
case APFloat::opStatus::opOverflow:
return ConstantFP::get(
CI->getType(),
APFloat::getLargest(X->getSemantics(), /*Negative=*/false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result should be inf. Please add a tests for fdim(1e308, -1e308).

BTW, it would be better to use fmax(x - y, 0) instead of handling all corner cases yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I read that on cppref but wasn't sure if it will handle all corner cases,
will change to that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The special cases also set errno, in which case we cannot perform the fold unless the call is memory(none)

case APFloat::opStatus::opUnderflow:
return ConstantFP::get(
CI->getType(),
APFloat::getLargest(X->getSemantics(), /*Negative=*/true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is incorrect. Underflow means the result is subnormal.

default:
return nullptr;
}
return ConstantFP::get(CI->getType(), ReturnVal);
}

//===----------------------------------------------------------------------===//
// Integer Library Call Optimizations
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -4009,6 +4044,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
return optimizeBinaryDoubleFP(CI, Builder, TLI);
return nullptr;
case LibFunc_fdim:
case LibFunc_fdimf:
case LibFunc_fdiml:
return optimizeFdim(CI, Builder);
case LibFunc_fminf:
case LibFunc_fmin:
case LibFunc_fminl:
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/Transforms/InferFunctionAttrs/annotate.ll
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,16 @@ declare float @remquof(float, float, ptr)
; CHECK: declare x86_fp80 @remquol(x86_fp80, x86_fp80, ptr nocapture) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
declare x86_fp80 @remquol(x86_fp80, x86_fp80, ptr)


; CHECK: declare double @fdim(double, double) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
declare double @fdim(double, double)

; CHECK: declare float @fdimf(float, float) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
declare float @fdimf(float, float)

; CHECK: declare x86_fp80 @fdiml(x86_fp80, x86_fp80) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
declare x86_fp80 @fdiml(x86_fp80, x86_fp80)

; CHECK: declare noundef i32 @rename(ptr nocapture noundef readonly, ptr nocapture noundef readonly) [[NOFREE_NOUNWIND]]
declare i32 @rename(ptr, ptr)

Expand Down
37 changes: 37 additions & 0 deletions llvm/test/Transforms/InstCombine/fdim.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define double @fdim_double() {
; CHECK-LABEL: define double @fdim_double() {
; CHECK-NEXT: ret double 2.500000e+00
;
%dim = call double @fdim (double 10.5, double 8.0)
ret double %dim
}

define double @fdim_double1() {
; CHECK-LABEL: define double @fdim_double1() {
; CHECK-NEXT: ret double 0.000000e+00
;
%dim = call double @fdim (double 7.0, double 8.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before ( in all functions

ret double %dim
}

define float @fdim_float() {
; CHECK-LABEL: define float @fdim_float() {
; CHECK-NEXT: ret float 2.500000e+00
;
%dim = call float @fdimf (float 10.5, float 8.0)
ret float %dim
}

define float @fdim_float1() {
; CHECK-LABEL: define float @fdim_float1() {
; CHECK-NEXT: ret float 0.000000e+00
;
%dim = call float @fdimf (float 7.0, float 8.0)
ret float %dim
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs tests for all the edge cases. Can you also test poison/undef arguments in each position

declare double @fdim(double, double)
declare float @fdimf(float, float)
20 changes: 16 additions & 4 deletions llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#
# CHECK: << Total TLI yes SDK no: 18
# CHECK: >> Total TLI no SDK yes: 0
# CHECK: == Total TLI yes SDK yes: 250
# CHECK: == Total TLI yes SDK yes: 253
#
# WRONG_DETAIL: << TLI yes SDK no : '_ZdaPv' aka operator delete[](void*)
# WRONG_DETAIL: >> TLI no SDK yes: '_ZdaPvj' aka operator delete[](void*, unsigned int)
Expand All @@ -48,14 +48,14 @@
# WRONG_DETAIL: << TLI yes SDK no : 'fminimum_numl'
# WRONG_SUMMARY: << Total TLI yes SDK no: 19{{$}}
# WRONG_SUMMARY: >> Total TLI no SDK yes: 1{{$}}
# WRONG_SUMMARY: == Total TLI yes SDK yes: 249
# WRONG_SUMMARY: == Total TLI yes SDK yes: 252
#
## The -COUNT suffix doesn't care if there are too many matches, so check
## the exact count first; the two directives should add up to that.
## Yes, this means additions to TLI will fail this test, but the argument
## to -COUNT can't be an expression.
# AVAIL: TLI knows 501 symbols, 268 available
# AVAIL-COUNT-268: {{^}} available
# AVAIL: TLI knows 504 symbols, 271 available
# AVAIL-COUNT-271: {{^}} available
# AVAIL-NOT: {{^}} available
# UNAVAIL-COUNT-233: not available
# UNAVAIL-NOT: not available
Expand Down Expand Up @@ -814,6 +814,18 @@ DynamicSymbols:
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: fdim
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: fdimf
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: fdiml
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: rewind
Type: STT_FUNC
Section: .text
Expand Down
3 changes: 3 additions & 0 deletions llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
"declare double @remquo(double, double, ptr)\n"
"declare float @remquof(float, float, ptr)\n"
"declare x86_fp80 @remquol(x86_fp80, x86_fp80, ptr)\n"
"declare double @fdim(double, double)\n"
"declare float @fdimf(float, float)\n"
"declare x86_fp80 @fdiml(x86_fp80, x86_fp80)\n"
"declare i32 @rename(i8*, i8*)\n"
"declare void @rewind(%struct*)\n"
"declare double @rint(double)\n"
Expand Down
Loading