log1p(x), logb(x), ilogb(x) are not folded at the compilation time when x is constant
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);
}
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:
- 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.
- 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.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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.
@llvm/issue-subscribers-good-first-issue
Author: None (k-arrows)
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>
Can you please assign this issue to me? I'm working on it.
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!
@tharunvk Hi, are you still work on it?
Yes. I'm done with logb and log1p. Almost done with ilogb. I'll raise the pull request soon
happy to give this a try if you're busy @tharunvk
Hi @tharunvk, are you still working on this?
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
Maybe some extra work is needed here: https://github.com/llvm/llvm-project/blob/5f1f69368e1f17ff9e32428f36cf34ffb6255ae5/llvm/lib/Analysis/ConstantFolding.cpp#L3520
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.