llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

log1p(x), logb(x), ilogb(x) are not folded at the compilation time when x is constant

Open k-arrows opened this issue 2 years ago • 11 comments

Consider the following C++ example. The functions log2(x), log10(x) and log(x) are folded, but log1p(x), logb(x), ilogb(x) are not. https://godbolt.org/z/nzrncEE37

#include <cmath>

double f1() {
  double num = 8.0;
  return std::log2(num);
}

double f2() {
  double num = 100.0;
  return std::log10(num);
}

double f3() {
  double num = 2.718281828459045;
  return std::log(num);
}

double f4() {
  double num = 2.718281828459045;
  return std::log1p(num - 1);
}

double f5() {
  double num = 48.0;
  return std::logb(num);
}

int f6() {
  double num = 48.0;
  return std::ilogb(num);
}

k-arrows avatar Aug 04 '24 08:08 k-arrows

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Aug 04 '24 14:08 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: None (k-arrows)

Consider the following C++ example. The functions log2(x), log10(x) and log(x) are folded, but log1p(x), logb(x), ilogb(x) are not. https://godbolt.org/z/nzrncEE37 ```cpp #include <cmath>

double f1() { double num = 8.0; return std::log2(num); }

double f2() { double num = 100.0; return std::log10(num); }

double f3() { double num = 2.718281828459045; return std::log(num); }

double f4() { double num = 2.718281828459045; return std::log1p(num - 1); }

double f5() { double num = 48.0; return std::logb(num); }

int f6() { double num = 48.0; return std::ilogb(num); }

</details>

llvmbot avatar Aug 04 '24 14:08 llvmbot

Can you please assign this issue to me? I'm working on it.

tharunvk avatar Aug 05 '24 05:08 tharunvk

Can you please assign this issue to me? I'm working on it.

Please split these changes into two PRs: TLI + ConstantFolding/SimplifyLibCall Example: https://github.com/llvm/llvm-project/pull/99611 + https://github.com/llvm/llvm-project/pull/99647

Good luck!

dtcxzyw avatar Aug 05 '24 05:08 dtcxzyw

@tharunvk Hi, are you still work on it?

fawdlstty avatar Sep 02 '24 04:09 fawdlstty

Yes. I'm done with logb and log1p. Almost done with ilogb. I'll raise the pull request soon

tharunvk avatar Sep 02 '24 06:09 tharunvk

happy to give this a try if you're busy @tharunvk

badumbatish avatar Sep 13 '24 06:09 badumbatish

Hi @tharunvk, are you still working on this?

c8ef avatar Oct 04 '24 01:10 c8ef

I checked for log1px and logbx it already works But for ilogbx the call instruction is not removed so it has to be optimized

IR:

define dso_local noundef i32 @_Z2f6v() local_unnamed_addr #0 {
entry:
  %call = tail call i32 @ilogb(double noundef 4.800000e+01) #2, !tbaa !5
  ret i32 5
} 

@dtcxzyw

kshitijvp avatar Jan 11 '25 05:01 kshitijvp

Maybe some extra work is needed here: https://github.com/llvm/llvm-project/blob/5f1f69368e1f17ff9e32428f36cf34ffb6255ae5/llvm/lib/Analysis/ConstantFolding.cpp#L3520

c8ef avatar Jan 11 '25 05:01 c8ef

Maybe some extra work is needed here:

llvm-project/llvm/lib/Analysis/ConstantFolding.cpp

Line 3520 in 5f1f693 bool llvm::isMathLibCallNoop(const CallBase *Call,

EarlyCSE seems to be doing the elimination. But LibCallsShrinkWrapPass pads other calls but not ilogb, so further opt is blocked.

VedantParanjape avatar Jan 11 '25 06:01 VedantParanjape