[llvm] [ConstantFolding] Add edge cases for llvm.log{, 2, 10} (PR #173304)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 22 12:22:36 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Stefan Weigl-Bosker (sweiglbosker)
<details>
<summary>Changes</summary>
Addresses https://github.com/llvm/llvm-project/issues/173267.
- folds log(-x) -> NaN
- folds log(0) -> -inf
- also folds log(1) -> 0.0 without host libm
> note: log(inf) is also doable but it causes some other tests to fail so I avoided it for now
---
Full diff: https://github.com/llvm/llvm-project/pull/173304.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+18)
- (modified) llvm/test/Transforms/InstSimplify/ConstProp/calls.ll (+19)
``````````diff
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index a9b51065a1d99..3642b7df69cd2 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2719,11 +2719,29 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
switch (IntrinsicID) {
default: break;
case Intrinsic::log:
+ if (U.isZero())
+ return ConstantFP::getInfinity(Ty, true);
+ else if (U.isNegative())
+ return ConstantFP::getNaN(Ty);
+ else if (U.isExactlyValue(1))
+ return ConstantFP::getZero(Ty);
return ConstantFoldFP(log, APF, Ty);
case Intrinsic::log2:
+ if (U.isZero())
+ return ConstantFP::getInfinity(Ty, true);
+ else if (U.isNegative())
+ return ConstantFP::getNaN(Ty);
+ else if (U.isExactlyValue(1))
+ return ConstantFP::getZero(Ty);
// TODO: What about hosts that lack a C99 library?
return ConstantFoldFP(log2, APF, Ty);
case Intrinsic::log10:
+ if (U.isZero())
+ return ConstantFP::getInfinity(Ty, true);
+ else if (U.isNegative())
+ return ConstantFP::getNaN(Ty);
+ else if (U.isExactlyValue(1))
+ return ConstantFP::getZero(Ty);
// TODO: What about hosts that lack a C99 library?
return ConstantFoldFP(log10, APF, Ty);
case Intrinsic::exp:
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll b/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
index 26fb8c0d7a1c6..b2c3b6a8d460c 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
@@ -214,5 +214,24 @@ entry:
ret float %1
}
+; log(0.0) -> -inf
+; Check that log instrinsics fold through cases where libc will throw an exception
+define float @test_intrinsic_log_zero() {
+; CHECK-LABEL: @test_intrinsic_log_zero(
+; CHECK-NOT: call
+; CHECK: ret float 0xFFF0000000000000
+ %1 = call float @llvm.log.f32(float 0.0)
+ ret float %1
+}
+
+; log(-x) -> NaN
+define float @test_intrinsic_log_neg() {
+; CHECK-LABEL: @test_intrinsic_log_neg(
+; CHECK-NOT: call
+; CHECK: ret float 0x7FF8000000000000
+ %1 = call float @llvm.log2.f32(float -20.0)
+ ret float %1
+}
+
declare double @llvm.pow.f64(double, double) nounwind readonly
declare float @llvm.pow.f32(float, float) nounwind readonly
``````````
</details>
https://github.com/llvm/llvm-project/pull/173304
More information about the llvm-commits
mailing list