[llvm] [ConstantFold] Fold `log1p` and `log1pf` when the input parameter is a constant value. (PR #112113)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 13 03:15:31 PDT 2024
================
@@ -0,0 +1,113 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define float @log1pf_const() {
+; CHECK-LABEL: define float @log1pf_const() {
+; CHECK-NEXT: [[R:%.*]] = call float @log1pf(float 7.000000e+00)
+; CHECK-NEXT: ret float 0x4000A2B240000000
+;
+ %r = call float @log1pf(float 7.000000e+00)
+ ret float %r
+}
+
+define double @log1p_const() {
+; CHECK-LABEL: define double @log1p_const() {
+; CHECK-NEXT: [[R:%.*]] = call double @log1p(double 7.000000e+00)
+; CHECK-NEXT: ret double 0x4000A2B23F3BAB73
+;
+ %r = call double @log1p(double 7.000000e+00)
+ ret double %r
+}
+
+define float @log1pf_minus_one() {
+; CHECK-LABEL: define float @log1pf_minus_one() {
+; CHECK-NEXT: [[R:%.*]] = call float @log1pf(float -1.000000e+00)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @log1pf(float -1.000000e+00)
+ ret float %r
+}
+
+define double @log1p_minus_one() {
+; CHECK-LABEL: define double @log1p_minus_one() {
+; CHECK-NEXT: [[R:%.*]] = call double @log1p(double -1.000000e+00)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @log1p(double -1.000000e+00)
+ ret double %r
+}
+
+define float @log1pf_zero() {
+; CHECK-LABEL: define float @log1pf_zero() {
+; CHECK-NEXT: [[R:%.*]] = call float @log1pf(float 0.000000e+00)
+; CHECK-NEXT: ret float 0.000000e+00
+;
+ %r = call float @log1pf(float 0.000000e+00)
+ ret float %r
+}
+
+define double @log1p_zero() {
+; CHECK-LABEL: define double @log1p_zero() {
+; CHECK-NEXT: [[R:%.*]] = call double @log1p(double 0.000000e+00)
+; CHECK-NEXT: ret double 0.000000e+00
+;
+ %r = call double @log1p(double 0.000000e+00)
+ ret double %r
+}
+
+define float @log1pf_inf() {
+; CHECK-LABEL: define float @log1pf_inf() {
+; CHECK-NEXT: [[R:%.*]] = call float @log1pf(float 0x7FF0000000000000)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @log1pf(float 0x7FF0000000000000)
+ ret float %r
+}
+
+define double @log1p_inf() {
+; CHECK-LABEL: define double @log1p_inf() {
+; CHECK-NEXT: [[R:%.*]] = call double @log1p(double 0x7FF0000000000000)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @log1p(double 0x7FF0000000000000)
+ ret double %r
+}
+
+define float @log1pf_nan() {
+; CHECK-LABEL: define float @log1pf_nan() {
+; CHECK-NEXT: [[R:%.*]] = call float @log1pf(float 0x7FF8000000000000)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @log1pf(float 0x7FF8000000000000)
+ ret float %r
+}
+
+define double @log1p_nan() {
+; CHECK-LABEL: define double @log1p_nan() {
+; CHECK-NEXT: [[R:%.*]] = call double @log1p(double 0x7FF8000000000000)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @log1p(double 0x7FF8000000000000)
+ ret double %r
+}
+
+define float @log1pf_poison() {
+; CHECK-LABEL: define float @log1pf_poison() {
+; CHECK-NEXT: [[R:%.*]] = call float @log1pf(float poison)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @log1pf(float poison)
+ ret float %r
+}
+
+define double @log1p_poison() {
+; CHECK-LABEL: define double @log1p_poison() {
+; CHECK-NEXT: [[R:%.*]] = call double @log1p(double poison)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @log1p(double poison)
+ ret double %r
+}
+
----------------
c8ef wrote:
I have indeed considered handling `memory(none)` cases. My current plan is not to handle libc functions on a case-by-case basis, but to directly use the value from the libc function regardless of its parameter. Essentially, what I want is to pass the `CallBase` into the `constantFold` function, which involves two scenarios. In Case 1, if there are no floating-point exceptions, everything is fine, and we return the folded value. In Case 2, if there is a floating-point exception, we return either nullptr or the exact value returned from the function (such as NaN, HUGE_VAL, etc.) depending on whether this call accesses memory. By doing this, we can eliminate the range check in the Constant Folding phase and avoid error handling based on each function. I look forward to hearing your thoughts on this initial plan.
```
Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V, Type *Ty,
CallBase *CB) {
llvm_fenv_clearexcept();
double Result = NativeFP(V.convertToDouble());
if (llvm_fenv_testexcept()) {
llvm_fenv_clearexcept();
if (CB->doesNotAccessMemory())
return GetConstantFoldFPValue(Result, Ty);
else
return nullptr;
}
return GetConstantFoldFPValue(Result, Ty);
}
```
Handling `memory(none)` cases will impact all current folding operations. I believe it may break some tests and require additional testing. Therefore, my preference is to constant fold most common math functions as much as possible, and then address edge cases afterwards. Would this approach be acceptable?
https://github.com/llvm/llvm-project/pull/112113
More information about the llvm-commits
mailing list