Skip to content

Commit 6aa723d

Browse files
authored
[TLI] Add support for nan libfunc (#101356)
Reference: https://en.cppreference.com/w/cpp/numeric/math/nan
1 parent 055893f commit 6aa723d

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.def

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,21 @@ TLI_DEFINE_ENUM_INTERNAL(modfl)
18071807
TLI_DEFINE_STRING_INTERNAL("modfl")
18081808
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, Ptr)
18091809

1810+
/// double nan(const char *arg);
1811+
TLI_DEFINE_ENUM_INTERNAL(nan)
1812+
TLI_DEFINE_STRING_INTERNAL("nan")
1813+
TLI_DEFINE_SIG_INTERNAL(Dbl, Ptr)
1814+
1815+
/// float nanf(const char *arg);
1816+
TLI_DEFINE_ENUM_INTERNAL(nanf)
1817+
TLI_DEFINE_STRING_INTERNAL("nanf")
1818+
TLI_DEFINE_SIG_INTERNAL(Flt, Ptr)
1819+
1820+
/// long double nanl(const char *arg);
1821+
TLI_DEFINE_ENUM_INTERNAL(nanl)
1822+
TLI_DEFINE_STRING_INTERNAL("nanl")
1823+
TLI_DEFINE_SIG_INTERNAL(LDbl, Ptr)
1824+
18101825
/// double nearbyint(double x);
18111826
TLI_DEFINE_ENUM_INTERNAL(nearbyint)
18121827
TLI_DEFINE_STRING_INTERNAL("nearbyint")

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
279279
Changed |= setNonLazyBind(F);
280280

281281
switch (TheLibFunc) {
282+
case LibFunc_nan:
283+
case LibFunc_nanf:
284+
case LibFunc_nanl:
282285
case LibFunc_strlen:
283286
case LibFunc_strnlen:
284287
case LibFunc_wcslen:

llvm/test/Transforms/InferFunctionAttrs/annotate.ll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,15 @@ declare float @modff(float, ptr)
717717
; CHECK: declare x86_fp80 @modfl(x86_fp80, ptr nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
718718
declare x86_fp80 @modfl(x86_fp80, ptr)
719719

720+
; CHECK: declare double @nan(ptr nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN:#[0-9]+]]
721+
declare double @nan(ptr)
722+
723+
; CHECK: declare float @nanf(ptr nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN]]
724+
declare float @nanf(ptr)
725+
726+
; CHECK: declare x86_fp80 @nanl(ptr nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN]]
727+
declare x86_fp80 @nanl(ptr)
728+
720729
; CHECK: declare double @nearbyint(double) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
721730
declare double @nearbyint(double)
722731

@@ -956,7 +965,7 @@ declare ptr @strncpy(ptr, ptr, i64)
956965
; CHECK: declare noalias ptr @strndup(ptr nocapture readonly, i64 noundef) [[INACCESSIBLEMEMORARGONLY_NOFREE_NOUNWIND_WILLRETURN_FAMILY_MALLOC]]
957966
declare ptr @strndup(ptr, i64)
958967

959-
; CHECK: declare i64 @strnlen(ptr nocapture, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN:#[0-9]+]]
968+
; CHECK: declare i64 @strnlen(ptr nocapture, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN]]
960969
declare i64 @strnlen(ptr, i64)
961970

962971
; CHECK: declare ptr @strpbrk(ptr, ptr nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN]]

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@
3434
#
3535
# CHECK: << Total TLI yes SDK no: 8
3636
# CHECK: >> Total TLI no SDK yes: 0
37-
# CHECK: == Total TLI yes SDK yes: 245
37+
# CHECK: == Total TLI yes SDK yes: 248
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)
4141
# WRONG_DETAIL-COUNT-8: << TLI yes SDK no : {{.*}}__hot_cold_t
4242
# WRONG_SUMMARY: << Total TLI yes SDK no: 9{{$}}
4343
# WRONG_SUMMARY: >> Total TLI no SDK yes: 1{{$}}
44-
# WRONG_SUMMARY: == Total TLI yes SDK yes: 244
44+
# WRONG_SUMMARY: == Total TLI yes SDK yes: 247
4545
#
4646
## The -COUNT suffix doesn't care if there are too many matches, so check
4747
## the exact count first; the two directives should add up to that.
4848
## Yes, this means additions to TLI will fail this test, but the argument
4949
## to -COUNT can't be an expression.
50-
# AVAIL: TLI knows 486 symbols, 253 available
51-
# AVAIL-COUNT-253: {{^}} available
50+
# AVAIL: TLI knows 489 symbols, 256 available
51+
# AVAIL-COUNT-256: {{^}} available
5252
# AVAIL-NOT: {{^}} available
5353
# UNAVAIL-COUNT-233: not available
5454
# UNAVAIL-NOT: not available
@@ -703,6 +703,18 @@ DynamicSymbols:
703703
Type: STT_FUNC
704704
Section: .text
705705
Binding: STB_GLOBAL
706+
- Name: nan
707+
Type: STT_FUNC
708+
Section: .text
709+
Binding: STB_GLOBAL
710+
- Name: nanf
711+
Type: STT_FUNC
712+
Section: .text
713+
Binding: STB_GLOBAL
714+
- Name: nanl
715+
Type: STT_FUNC
716+
Section: .text
717+
Binding: STB_GLOBAL
706718
- Name: nearbyint
707719
Type: STT_FUNC
708720
Section: .text

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
255255
"declare double @modf(double, double*)\n"
256256
"declare float @modff(float, float*)\n"
257257
"declare x86_fp80 @modfl(x86_fp80, x86_fp80*)\n"
258+
"declare double @nan(ptr)\n"
259+
"declare float @nanf(ptr)\n"
260+
"declare x86_fp80 @nanl(ptr)\n"
258261
"declare double @nearbyint(double)\n"
259262
"declare float @nearbyintf(float)\n"
260263
"declare x86_fp80 @nearbyintl(x86_fp80)\n"

0 commit comments

Comments
 (0)