Skip to content

Commit e79528f

Browse files
authored
[TLI] Add basic support for nextafter/nexttoward libcalls (#166250)
First patch for #74368. Constant folding will be added in a follow-up patch.
1 parent 9e2f73f commit e79528f

File tree

6 files changed

+94
-4
lines changed

6 files changed

+94
-4
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.def

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,36 @@ TLI_DEFINE_ENUM_INTERNAL(nearbyintl)
19511951
TLI_DEFINE_STRING_INTERNAL("nearbyintl")
19521952
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl)
19531953

1954+
/// double nextafter(double x, double y);
1955+
TLI_DEFINE_ENUM_INTERNAL(nextafter)
1956+
TLI_DEFINE_STRING_INTERNAL("nextafter")
1957+
TLI_DEFINE_SIG_INTERNAL(Dbl, Dbl, Dbl)
1958+
1959+
/// float nextafterf(float x, float y);
1960+
TLI_DEFINE_ENUM_INTERNAL(nextafterf)
1961+
TLI_DEFINE_STRING_INTERNAL("nextafterf")
1962+
TLI_DEFINE_SIG_INTERNAL(Flt, Flt, Flt)
1963+
1964+
/// long double nextafterl(long double x, long double y);
1965+
TLI_DEFINE_ENUM_INTERNAL(nextafterl)
1966+
TLI_DEFINE_STRING_INTERNAL("nextafterl")
1967+
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, LDbl)
1968+
1969+
/// double nexttoward(double x, long double y);
1970+
TLI_DEFINE_ENUM_INTERNAL(nexttoward)
1971+
TLI_DEFINE_STRING_INTERNAL("nexttoward")
1972+
TLI_DEFINE_SIG_INTERNAL(Dbl, Dbl, LDbl)
1973+
1974+
/// float nexttowardf(float x, long double y);
1975+
TLI_DEFINE_ENUM_INTERNAL(nexttowardf)
1976+
TLI_DEFINE_STRING_INTERNAL("nexttowardf")
1977+
TLI_DEFINE_SIG_INTERNAL(Flt, Flt, LDbl)
1978+
1979+
/// long double nexttowardl(long double x, long double y);
1980+
TLI_DEFINE_ENUM_INTERNAL(nexttowardl)
1981+
TLI_DEFINE_STRING_INTERNAL("nexttowardl")
1982+
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, LDbl)
1983+
19541984
/// uint32_t ntohl(uint32_t netlong);
19551985
TLI_DEFINE_ENUM_INTERNAL(ntohl)
19561986
TLI_DEFINE_STRING_INTERNAL("ntohl")

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
388388
TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
389389
else
390390
TLI.setUnavailable(LibFunc_logbf);
391+
TLI.setUnavailable(LibFunc_nextafter);
392+
TLI.setUnavailable(LibFunc_nextafterf);
393+
TLI.setUnavailable(LibFunc_nexttoward);
394+
TLI.setUnavailable(LibFunc_nexttowardf);
391395
TLI.setUnavailable(LibFunc_rint);
392396
TLI.setUnavailable(LibFunc_rintf);
393397
TLI.setUnavailable(LibFunc_round);
@@ -418,6 +422,8 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
418422
TLI.setUnavailable(LibFunc_logbl);
419423
TLI.setUnavailable(LibFunc_ilogbl);
420424
TLI.setUnavailable(LibFunc_nearbyintl);
425+
TLI.setUnavailable(LibFunc_nextafterl);
426+
TLI.setUnavailable(LibFunc_nexttowardl);
421427
TLI.setUnavailable(LibFunc_rintl);
422428
TLI.setUnavailable(LibFunc_roundl);
423429
TLI.setUnavailable(LibFunc_scalblnl);

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,12 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
12831283
case LibFunc_ilogbl:
12841284
case LibFunc_logf:
12851285
case LibFunc_logl:
1286+
case LibFunc_nextafter:
1287+
case LibFunc_nextafterf:
1288+
case LibFunc_nextafterl:
1289+
case LibFunc_nexttoward:
1290+
case LibFunc_nexttowardf:
1291+
case LibFunc_nexttowardl:
12861292
case LibFunc_pow:
12871293
case LibFunc_powf:
12881294
case LibFunc_powl:

llvm/test/Transforms/InferFunctionAttrs/annotate.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,24 @@ declare float @nearbyintf(float)
762762
; CHECK: declare x86_fp80 @nearbyintl(x86_fp80) [[MEMNONE_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
763763
declare x86_fp80 @nearbyintl(x86_fp80)
764764

765+
; CHECK: declare double @nextafter(double, double) [[ERRNOMEMONLY_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
766+
declare double @nextafter(double, double)
767+
768+
; CHECK: declare float @nextafterf(float, float) [[ERRNOMEMONLY_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
769+
declare float @nextafterf(float, float)
770+
771+
; CHECK: declare x86_fp80 @nextafterl(x86_fp80, x86_fp80) [[ERRNOMEMONLY_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
772+
declare x86_fp80 @nextafterl(x86_fp80, x86_fp80)
773+
774+
; CHECK: declare double @nexttoward(double, x86_fp80) [[ERRNOMEMONLY_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
775+
declare double @nexttoward(double, x86_fp80)
776+
777+
; CHECK: declare float @nexttowardf(float, x86_fp80) [[ERRNOMEMONLY_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
778+
declare float @nexttowardf(float, x86_fp80)
779+
780+
; CHECK: declare x86_fp80 @nexttowardl(x86_fp80, x86_fp80) [[ERRNOMEMONLY_NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
781+
declare x86_fp80 @nexttowardl(x86_fp80, x86_fp80)
782+
765783
; CHECK-LINUX: declare noundef i32 @open(ptr noundef readonly captures(none), i32 noundef, ...) [[NOFREE]]
766784
; CHECK-OPEN: declare noundef i32 @open(ptr noundef readonly captures(none), i32 noundef, ...) [[NOFREE:#[0-9]+]]
767785
declare i32 @open(ptr, i32, ...)

llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#
3535
# CHECK: << Total TLI yes SDK no: 18
3636
# CHECK: >> Total TLI no SDK yes: 0
37-
# CHECK: == Total TLI yes SDK yes: 271
37+
# CHECK: == Total TLI yes SDK yes: 277
3838
#
3939
# WRONG_DETAIL: << TLI yes SDK no : '_ZdaPv' aka operator delete[](void*)
4040
# WRONG_DETAIL: >> TLI no SDK yes: '_ZdaPvj' aka operator delete[](void*, unsigned int)
@@ -48,14 +48,14 @@
4848
# WRONG_DETAIL: << TLI yes SDK no : 'fminimum_numl'
4949
# WRONG_SUMMARY: << Total TLI yes SDK no: 19{{$}}
5050
# WRONG_SUMMARY: >> Total TLI no SDK yes: 1{{$}}
51-
# WRONG_SUMMARY: == Total TLI yes SDK yes: 270
51+
# WRONG_SUMMARY: == Total TLI yes SDK yes: 276
5252
#
5353
## The -COUNT suffix doesn't care if there are too many matches, so check
5454
## the exact count first; the two directives should add up to that.
5555
## Yes, this means additions to TLI will fail this test, but the argument
5656
## to -COUNT can't be an expression.
57-
# AVAIL: TLI knows 524 symbols, 289 available
58-
# AVAIL-COUNT-289: {{^}} available
57+
# AVAIL: TLI knows 530 symbols, 295 available
58+
# AVAIL-COUNT-295: {{^}} available
5959
# AVAIL-NOT: {{^}} available
6060
# UNAVAIL-COUNT-235: not available
6161
# UNAVAIL-NOT: not available
@@ -778,6 +778,30 @@ DynamicSymbols:
778778
Type: STT_FUNC
779779
Section: .text
780780
Binding: STB_GLOBAL
781+
- Name: nextafter
782+
Type: STT_FUNC
783+
Section: .text
784+
Binding: STB_GLOBAL
785+
- Name: nextafterf
786+
Type: STT_FUNC
787+
Section: .text
788+
Binding: STB_GLOBAL
789+
- Name: nextafterl
790+
Type: STT_FUNC
791+
Section: .text
792+
Binding: STB_GLOBAL
793+
- Name: nexttoward
794+
Type: STT_FUNC
795+
Section: .text
796+
Binding: STB_GLOBAL
797+
- Name: nexttowardf
798+
Type: STT_FUNC
799+
Section: .text
800+
Binding: STB_GLOBAL
801+
- Name: nexttowardl
802+
Type: STT_FUNC
803+
Section: .text
804+
Binding: STB_GLOBAL
781805
- Name: perror
782806
Type: STT_FUNC
783807
Section: .text

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
277277
"declare x86_fp80 @logbl(x86_fp80)\n"
278278
"declare float @logf(float)\n"
279279
"declare x86_fp80 @logl(x86_fp80)\n"
280+
"declare double @nextafter(double, double)\n"
281+
"declare float @nextafterf(float, float)\n"
282+
"declare x86_fp80 @nextafterl(x86_fp80, x86_fp80)\n"
283+
"declare double @nexttoward(double, x86_fp80)\n"
284+
"declare float @nexttowardf(float, x86_fp80)\n"
285+
"declare x86_fp80 @nexttowardl(x86_fp80, x86_fp80)\n"
280286
"declare i8* @malloc(i64)\n"
281287
"declare i8* @memccpy(i8*, i8*, i32, i64)\n"
282288
"declare i8* @memchr(i8*, i32, i64)\n"

0 commit comments

Comments
 (0)