Skip to content

[TLI] Add support for lgamma libcall. #114536

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MouriNaruto
Copy link
Contributor

@MouriNaruto MouriNaruto commented Nov 1, 2024

This patch adds basic support for lgamma. Constant folding support will be submitted in a subsequent patch.

Related issue: #113804

Note: I'm sorry that I made a similar PR that I had created (#114343) recently because one of my friends, @fawdlstty, hopes me to help him solve that issue in private. (It is important to finish promises made to friends.) I think it's a good time for me to practice again. I hope I can make this PR merged successfully without reverting.

I hope I can create PRs for different topics that I am interested in for the next time instead of solving friends' issues. I know this style PRs should be left to other people who want to participate in the LLVM for the first time.

I hope @arsenm and @c8ef can help me review the code.

Kenji Mouri

@llvmbot
Copy link
Member

llvmbot commented Nov 1, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Kenji Mouri / 毛利 研二 (MouriNaruto)

Changes

This patch adds basic support for lgamma. Constant folding support will be submitted in a subsequent patch.

Related issue: #113804

Note: I'm sorry that I made a similar PR that I had created (#114343) recently because one of my friends, @fawdlstty, hopes me to help him solve that issue in private. I think it's a good time for me to practice again. I hope I can make this PR merged successfully without reverting.

I hope I can create PRs for different topics that I am interested in for the next time instead of solving friends' issues. I know this style PRs should be left to other people who want to participate in the LLVM for the first time.

I hope @arsenm and @c8ef can help me review the code.

Kenji Mouri


Full diff: https://github.com/llvm/llvm-project/pull/114536.diff

6 Files Affected:

  • (modified) llvm/include/llvm/Analysis/TargetLibraryInfo.def (+15)
  • (modified) llvm/lib/Analysis/TargetLibraryInfo.cpp (+2)
  • (modified) llvm/lib/Transforms/Utils/BuildLibCalls.cpp (+3)
  • (modified) llvm/test/Transforms/InferFunctionAttrs/annotate.ll (+9)
  • (modified) llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml (+16-4)
  • (modified) llvm/unittests/Analysis/TargetLibraryInfoTest.cpp (+3)
diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.def b/llvm/include/llvm/Analysis/TargetLibraryInfo.def
index fd53a26ef8fc11..04d6aef56783d5 100644
--- a/llvm/include/llvm/Analysis/TargetLibraryInfo.def
+++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.def
@@ -1726,6 +1726,21 @@ TLI_DEFINE_ENUM_INTERNAL(ldexpl)
 TLI_DEFINE_STRING_INTERNAL("ldexpl")
 TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, Int)
 
+/// double lgamma(double x);
+TLI_DEFINE_ENUM_INTERNAL(lgamma)
+TLI_DEFINE_STRING_INTERNAL("lgamma")
+TLI_DEFINE_SIG_INTERNAL(Dbl, Dbl)
+
+/// float lgammaf(float x);
+TLI_DEFINE_ENUM_INTERNAL(lgammaf)
+TLI_DEFINE_STRING_INTERNAL("lgammaf")
+TLI_DEFINE_SIG_INTERNAL(Flt, Flt)
+
+/// long double lgammal(long double x);
+TLI_DEFINE_ENUM_INTERNAL(lgammal)
+TLI_DEFINE_STRING_INTERNAL("lgammal")
+TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl)
+
 /// long long int llabs(long long int j);
 TLI_DEFINE_ENUM_INTERNAL(llabs)
 TLI_DEFINE_STRING_INTERNAL("llabs")
diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp
index 7f0b98ab3c1514..a54ab8c9966c3c 100644
--- a/llvm/lib/Analysis/TargetLibraryInfo.cpp
+++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp
@@ -301,6 +301,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
       TLI.setUnavailable(LibFunc_floorf);
       TLI.setUnavailable(LibFunc_fmodf);
       TLI.setUnavailable(LibFunc_hypotf);
+      TLI.setUnavailable(LibFunc_lgammaf);
       TLI.setUnavailable(LibFunc_log10f);
       TLI.setUnavailable(LibFunc_logf);
       TLI.setUnavailable(LibFunc_modff);
@@ -334,6 +335,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
     TLI.setUnavailable(LibFunc_frexpl);
     TLI.setUnavailable(LibFunc_hypotl);
     TLI.setUnavailable(LibFunc_ldexpl);
+    TLI.setUnavailable(LibFunc_lgammal);
     TLI.setUnavailable(LibFunc_log10l);
     TLI.setUnavailable(LibFunc_logl);
     TLI.setUnavailable(LibFunc_modfl);
diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
index e039457f313b29..35001809daeecb 100644
--- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -1221,6 +1221,9 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
   case LibFunc_isascii:
   case LibFunc_isdigit:
   case LibFunc_labs:
+  case LibFunc_lgamma:
+  case LibFunc_lgammaf:
+  case LibFunc_lgammal:
   case LibFunc_llabs:
   case LibFunc_log:
   case LibFunc_log10:
diff --git a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
index 452d90aa98d88d..e10e60f3a86763 100644
--- a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
+++ b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
@@ -619,6 +619,15 @@ declare float @ldexpf(float, i32)
 ; CHECK: declare x86_fp80 @ldexpl(x86_fp80, i32) [[NOFREE_WILLRETURN]]
 declare x86_fp80 @ldexpl(x86_fp80, i32)
 
+; CHECK: declare double @lgamma(double) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
+declare double @lgamma(double)
+
+; CHECK: declare float @lgammaf(float) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
+declare float @lgammaf(float)
+
+; CHECK: declare x86_fp80 @lgammal(x86_fp80) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
+declare x86_fp80 @lgammal(x86_fp80)
+
 ; CHECK: declare i64 @llabs(i64) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
 declare i64 @llabs(i64)
 
diff --git a/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml b/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
index 6702725e4fc8a4..83e35b50c12920 100644
--- a/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
+++ b/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
@@ -34,7 +34,7 @@
 #
 # CHECK: << Total TLI yes SDK no:  18
 # CHECK: >> Total TLI no  SDK yes: 0
-# CHECK: == Total TLI yes SDK yes: 271
+# CHECK: == Total TLI yes SDK yes: 274
 #
 # WRONG_DETAIL: << TLI yes SDK no : '_ZdaPv' aka operator delete[](void*)
 # WRONG_DETAIL: >> TLI no  SDK yes: '_ZdaPvj' aka operator delete[](void*, unsigned int)
@@ -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: 270
+# WRONG_SUMMARY: == Total TLI yes SDK yes: 273
 #
 ## 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 522 symbols, 289 available
-# AVAIL-COUNT-289: {{^}} available
+# AVAIL: TLI knows 525 symbols, 292 available
+# AVAIL-COUNT-292: {{^}} available
 # AVAIL-NOT:       {{^}} available
 # UNAVAIL-COUNT-233: not available
 # UNAVAIL-NOT:       not available
@@ -634,6 +634,18 @@ DynamicSymbols:
     Type:            STT_FUNC
     Section:         .text
     Binding:         STB_GLOBAL
+  - Name:            lgamma
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+  - Name:            lgammaf
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+  - Name:            lgammal
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
   - Name:            llabs
     Type:            STT_FUNC
     Section:         .text
diff --git a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
index 982d00c5d33593..34d8cae9ab3927 100644
--- a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
+++ b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
@@ -272,6 +272,9 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
       "declare i32 @ilogb(double)\n"
       "declare i32 @ilogbf(float)\n"
       "declare i32 @ilogbl(x86_fp80)\n"
+      "declare double @lgamma(double)\n"
+      "declare float @lgammaf(float)\n"
+      "declare x86_fp80 @lgammal(x86_fp80)\n"
       "declare double @logb(double)\n"
       "declare float @logbf(float)\n"
       "declare x86_fp80 @logbl(x86_fp80)\n"

@llvmbot
Copy link
Member

llvmbot commented Nov 1, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Kenji Mouri / 毛利 研二 (MouriNaruto)

Changes

This patch adds basic support for lgamma. Constant folding support will be submitted in a subsequent patch.

Related issue: #113804

Note: I'm sorry that I made a similar PR that I had created (#114343) recently because one of my friends, @fawdlstty, hopes me to help him solve that issue in private. I think it's a good time for me to practice again. I hope I can make this PR merged successfully without reverting.

I hope I can create PRs for different topics that I am interested in for the next time instead of solving friends' issues. I know this style PRs should be left to other people who want to participate in the LLVM for the first time.

I hope @arsenm and @c8ef can help me review the code.

Kenji Mouri


Full diff: https://github.com/llvm/llvm-project/pull/114536.diff

6 Files Affected:

  • (modified) llvm/include/llvm/Analysis/TargetLibraryInfo.def (+15)
  • (modified) llvm/lib/Analysis/TargetLibraryInfo.cpp (+2)
  • (modified) llvm/lib/Transforms/Utils/BuildLibCalls.cpp (+3)
  • (modified) llvm/test/Transforms/InferFunctionAttrs/annotate.ll (+9)
  • (modified) llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml (+16-4)
  • (modified) llvm/unittests/Analysis/TargetLibraryInfoTest.cpp (+3)
diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.def b/llvm/include/llvm/Analysis/TargetLibraryInfo.def
index fd53a26ef8fc11..04d6aef56783d5 100644
--- a/llvm/include/llvm/Analysis/TargetLibraryInfo.def
+++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.def
@@ -1726,6 +1726,21 @@ TLI_DEFINE_ENUM_INTERNAL(ldexpl)
 TLI_DEFINE_STRING_INTERNAL("ldexpl")
 TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl, Int)
 
+/// double lgamma(double x);
+TLI_DEFINE_ENUM_INTERNAL(lgamma)
+TLI_DEFINE_STRING_INTERNAL("lgamma")
+TLI_DEFINE_SIG_INTERNAL(Dbl, Dbl)
+
+/// float lgammaf(float x);
+TLI_DEFINE_ENUM_INTERNAL(lgammaf)
+TLI_DEFINE_STRING_INTERNAL("lgammaf")
+TLI_DEFINE_SIG_INTERNAL(Flt, Flt)
+
+/// long double lgammal(long double x);
+TLI_DEFINE_ENUM_INTERNAL(lgammal)
+TLI_DEFINE_STRING_INTERNAL("lgammal")
+TLI_DEFINE_SIG_INTERNAL(LDbl, LDbl)
+
 /// long long int llabs(long long int j);
 TLI_DEFINE_ENUM_INTERNAL(llabs)
 TLI_DEFINE_STRING_INTERNAL("llabs")
diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp
index 7f0b98ab3c1514..a54ab8c9966c3c 100644
--- a/llvm/lib/Analysis/TargetLibraryInfo.cpp
+++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp
@@ -301,6 +301,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
       TLI.setUnavailable(LibFunc_floorf);
       TLI.setUnavailable(LibFunc_fmodf);
       TLI.setUnavailable(LibFunc_hypotf);
+      TLI.setUnavailable(LibFunc_lgammaf);
       TLI.setUnavailable(LibFunc_log10f);
       TLI.setUnavailable(LibFunc_logf);
       TLI.setUnavailable(LibFunc_modff);
@@ -334,6 +335,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
     TLI.setUnavailable(LibFunc_frexpl);
     TLI.setUnavailable(LibFunc_hypotl);
     TLI.setUnavailable(LibFunc_ldexpl);
+    TLI.setUnavailable(LibFunc_lgammal);
     TLI.setUnavailable(LibFunc_log10l);
     TLI.setUnavailable(LibFunc_logl);
     TLI.setUnavailable(LibFunc_modfl);
diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
index e039457f313b29..35001809daeecb 100644
--- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -1221,6 +1221,9 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
   case LibFunc_isascii:
   case LibFunc_isdigit:
   case LibFunc_labs:
+  case LibFunc_lgamma:
+  case LibFunc_lgammaf:
+  case LibFunc_lgammal:
   case LibFunc_llabs:
   case LibFunc_log:
   case LibFunc_log10:
diff --git a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
index 452d90aa98d88d..e10e60f3a86763 100644
--- a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
+++ b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
@@ -619,6 +619,15 @@ declare float @ldexpf(float, i32)
 ; CHECK: declare x86_fp80 @ldexpl(x86_fp80, i32) [[NOFREE_WILLRETURN]]
 declare x86_fp80 @ldexpl(x86_fp80, i32)
 
+; CHECK: declare double @lgamma(double) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
+declare double @lgamma(double)
+
+; CHECK: declare float @lgammaf(float) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
+declare float @lgammaf(float)
+
+; CHECK: declare x86_fp80 @lgammal(x86_fp80) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
+declare x86_fp80 @lgammal(x86_fp80)
+
 ; CHECK: declare i64 @llabs(i64) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
 declare i64 @llabs(i64)
 
diff --git a/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml b/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
index 6702725e4fc8a4..83e35b50c12920 100644
--- a/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
+++ b/llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
@@ -34,7 +34,7 @@
 #
 # CHECK: << Total TLI yes SDK no:  18
 # CHECK: >> Total TLI no  SDK yes: 0
-# CHECK: == Total TLI yes SDK yes: 271
+# CHECK: == Total TLI yes SDK yes: 274
 #
 # WRONG_DETAIL: << TLI yes SDK no : '_ZdaPv' aka operator delete[](void*)
 # WRONG_DETAIL: >> TLI no  SDK yes: '_ZdaPvj' aka operator delete[](void*, unsigned int)
@@ -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: 270
+# WRONG_SUMMARY: == Total TLI yes SDK yes: 273
 #
 ## 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 522 symbols, 289 available
-# AVAIL-COUNT-289: {{^}} available
+# AVAIL: TLI knows 525 symbols, 292 available
+# AVAIL-COUNT-292: {{^}} available
 # AVAIL-NOT:       {{^}} available
 # UNAVAIL-COUNT-233: not available
 # UNAVAIL-NOT:       not available
@@ -634,6 +634,18 @@ DynamicSymbols:
     Type:            STT_FUNC
     Section:         .text
     Binding:         STB_GLOBAL
+  - Name:            lgamma
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+  - Name:            lgammaf
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+  - Name:            lgammal
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
   - Name:            llabs
     Type:            STT_FUNC
     Section:         .text
diff --git a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
index 982d00c5d33593..34d8cae9ab3927 100644
--- a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
+++ b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
@@ -272,6 +272,9 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
       "declare i32 @ilogb(double)\n"
       "declare i32 @ilogbf(float)\n"
       "declare i32 @ilogbl(x86_fp80)\n"
+      "declare double @lgamma(double)\n"
+      "declare float @lgammaf(float)\n"
+      "declare x86_fp80 @lgammal(x86_fp80)\n"
       "declare double @logb(double)\n"
       "declare float @logbf(float)\n"
       "declare x86_fp80 @logbl(x86_fp80)\n"

Copy link
Contributor

@c8ef c8ef left a comment

Choose a reason for hiding this comment

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

Looks good. Please wait for another review : )

@c8ef c8ef requested a review from arsenm November 1, 2024 14:18
@c8ef
Copy link
Contributor

c8ef commented Nov 1, 2024

Constant folding support will be submitted in a subsequent patch.

In my experience, constant folding the gamma function is not easy because it requires invoking the corresponding libc function, which is implemented differently on various platforms. I have noticed different return values on Windows and Linux (x86), two of the most commonly used platforms. Writing a test case to satisfy the LLVM build bot may not be that trivial. Good luck!

@MouriNaruto
Copy link
Contributor Author

MouriNaruto commented Nov 1, 2024

Constant folding support will be submitted in a subsequent patch.

In my experience, constant folding the gamma function is not easy because it requires invoking the corresponding libc function, which is implemented differently on various platforms. I have noticed different return values on Windows and Linux (x86), two of the most commonly used platforms. Writing a test case to satisfy the LLVM build bot may not be that trivial. Good luck!

Thanks. I will try my best to implement this constant fold from the current LLVM codebase. I think I will learn a lot if I can make a proper PR for that.

Kenji Mouri

@@ -334,6 +335,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
TLI.setUnavailable(LibFunc_frexpl);
TLI.setUnavailable(LibFunc_hypotl);
TLI.setUnavailable(LibFunc_ldexpl);
TLI.setUnavailable(LibFunc_lgammal);
Copy link
Contributor

Choose a reason for hiding this comment

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

As a pre-commit can you add new baseline TLI checker tests for the windows and linux cases? This is untested, and this is a problem in every one of these new libcall patches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the PR: #114556

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants