Skip to content

Commit 7e877fc

Browse files
authored
[Reland][TLI] Add support for hypot libcall. (#114343)
This patch adds basic support for `hypot`. Constant folding support will be submitted in a subsequent patch. Related issue: #113711 Note: It's my first time contributing to the LLVM with encouragement from one of my friends, @fawdlstty. I learned a lot from #99611, and thanks for that. Note: I had created the same PR and merged (#113724), but reverted caused by the merging issue. (The CI issue happened in 3 A.M. at my timezone. So, I need to fall asleep again after I replied about why issue happened.) So, I rebased to the latest main branch and recreate the PR and hope I won't have the third time to create the same PR. I hope @arsenm can help me review the code again. I’m sorry for that. Kenji Mouri
1 parent 880b3b2 commit 7e877fc

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.def

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,21 @@ TLI_DEFINE_ENUM_INTERNAL(htons)
16711671
TLI_DEFINE_STRING_INTERNAL("htons")
16721672
TLI_DEFINE_SIG_INTERNAL(Int16, Int16)
16731673

1674+
/// double hypot(double x, double y);
1675+
TLI_DEFINE_ENUM_INTERNAL(hypot)
1676+
TLI_DEFINE_STRING_INTERNAL("hypot")
1677+
TLI_DEFINE_SIG_INTERNAL(Dbl, Dbl, Dbl)
1678+
1679+
/// float hypotf(float x, float y);
1680+
TLI_DEFINE_ENUM_INTERNAL(hypotf)
1681+
TLI_DEFINE_STRING_INTERNAL("hypotf")
1682+
TLI_DEFINE_SIG_INTERNAL(Flt, Flt, Flt)
1683+
1684+
/// long double hypotl(long double x, long double y);
1685+
TLI_DEFINE_ENUM_INTERNAL(hypotl)
1686+
TLI_DEFINE_STRING_INTERNAL("hypotl")
1687+
TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, LDbl)
1688+
16741689
/// int iprintf(const char *format, ...);
16751690
TLI_DEFINE_ENUM_INTERNAL(iprintf)
16761691
TLI_DEFINE_STRING_INTERNAL("iprintf")

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
300300
TLI.setUnavailable(LibFunc_expf);
301301
TLI.setUnavailable(LibFunc_floorf);
302302
TLI.setUnavailable(LibFunc_fmodf);
303+
TLI.setUnavailable(LibFunc_hypotf);
303304
TLI.setUnavailable(LibFunc_log10f);
304305
TLI.setUnavailable(LibFunc_logf);
305306
TLI.setUnavailable(LibFunc_modff);
@@ -331,6 +332,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
331332
TLI.setUnavailable(LibFunc_floorl);
332333
TLI.setUnavailable(LibFunc_fmodl);
333334
TLI.setUnavailable(LibFunc_frexpl);
335+
TLI.setUnavailable(LibFunc_hypotl);
334336
TLI.setUnavailable(LibFunc_ldexpl);
335337
TLI.setUnavailable(LibFunc_log10l);
336338
TLI.setUnavailable(LibFunc_logl);

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,9 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
12151215
case LibFunc_fmod:
12161216
case LibFunc_fmodf:
12171217
case LibFunc_fmodl:
1218+
case LibFunc_hypot:
1219+
case LibFunc_hypotf:
1220+
case LibFunc_hypotl:
12181221
case LibFunc_isascii:
12191222
case LibFunc_isdigit:
12201223
case LibFunc_labs:

llvm/test/Transforms/InferFunctionAttrs/annotate.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,15 @@ declare ptr @gets(ptr)
589589
; CHECK: declare noundef i32 @gettimeofday(ptr nocapture noundef, ptr nocapture noundef) [[NOFREE_NOUNWIND]]
590590
declare i32 @gettimeofday(ptr, ptr)
591591

592+
; CHECK: declare double @hypot(double, double) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
593+
declare double @hypot(double, double)
594+
595+
; CHECK: declare float @hypotf(float, float) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
596+
declare float @hypotf(float, float)
597+
598+
; CHECK: declare x86_fp80 @hypotl(x86_fp80, x86_fp80) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
599+
declare x86_fp80 @hypotl(x86_fp80, x86_fp80)
600+
592601
; CHECK: declare i32 @isascii(i32) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
593602
declare i32 @isascii(i32)
594603

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,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: 268
37+
# CHECK: == Total TLI yes SDK yes: 271
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: 267
51+
# WRONG_SUMMARY: == Total TLI yes SDK yes: 270
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 519 symbols, 286 available
58-
# AVAIL-COUNT-286: {{^}} available
57+
# AVAIL: TLI knows 522 symbols, 289 available
58+
# AVAIL-COUNT-289: {{^}} available
5959
# AVAIL-NOT: {{^}} available
6060
# UNAVAIL-COUNT-233: not available
6161
# UNAVAIL-NOT: not available
@@ -602,6 +602,18 @@ DynamicSymbols:
602602
Type: STT_FUNC
603603
Section: .text
604604
Binding: STB_GLOBAL
605+
- Name: hypot
606+
Type: STT_FUNC
607+
Section: .text
608+
Binding: STB_GLOBAL
609+
- Name: hypotf
610+
Type: STT_FUNC
611+
Section: .text
612+
Binding: STB_GLOBAL
613+
- Name: hypotl
614+
Type: STT_FUNC
615+
Section: .text
616+
Binding: STB_GLOBAL
605617
- Name: isdigit
606618
Type: STT_FUNC
607619
Section: .text

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
249249
"declare %struct* @getpwnam(i8*)\n"
250250
"declare i8* @gets(i8*)\n"
251251
"declare i32 @gettimeofday(%struct*, i8*)\n"
252+
"declare double @hypot(double, double)\n"
253+
"declare float @hypotf(float, float)\n"
254+
"declare x86_fp80 @hypotl(x86_fp80, x86_fp80)\n"
252255
"declare i32 @_Z7isasciii(i32)\n"
253256
"declare i32 @_Z7isdigiti(i32)\n"
254257
"declare i64 @labs(i64)\n"

0 commit comments

Comments
 (0)