[llvm] [ConstantFold] Special case log1p +/-0.0 (PR #114635)

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 1 20:31:30 PDT 2024


https://github.com/hubert-reinterpretcast created https://github.com/llvm/llvm-project/pull/114635

C's Annex F specifies that log1p +/-0.0 returns the input value;
however, this behavior is optional and host C libraries may behave
differently. This change applies the Annex F behavior to constant
folding by LLVM.

>From e2410939f16f83fd304656a4fdfe127b6e138c89 Mon Sep 17 00:00:00 2001
From: Hubert Tong <hubert.reinterpretcast at gmail.com>
Date: Fri, 1 Nov 2024 23:25:18 -0400
Subject: [PATCH 1/2] Revert "[AIX][test] XFAIL constant folding log1p test"

This reverts commit 900b6369e2f5fbc229371a142fdcd28b5280dbc0.
---
 llvm/test/Transforms/InstCombine/log1p.ll | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/test/Transforms/InstCombine/log1p.ll b/llvm/test/Transforms/InstCombine/log1p.ll
index bbf89db8c34105..81d3cc8a4f7ac9 100644
--- a/llvm/test/Transforms/InstCombine/log1p.ll
+++ b/llvm/test/Transforms/InstCombine/log1p.ll
@@ -1,4 +1,3 @@
-; XFAIL: target={{.*}}-aix{{.*}}
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 

>From c87de4c14f69b3072af3bc9debcd7bada50b7a26 Mon Sep 17 00:00:00 2001
From: Hubert Tong <hubert.reinterpretcast at gmail.com>
Date: Fri, 1 Nov 2024 23:27:32 -0400
Subject: [PATCH 2/2] [ConstantFold] Special case log1p +/-0.0

C's Annex F specifies that log1p +/-0.0 returns the input value;
however, this behavior is optional and host C libraries may behave
differently. This change applies the Annex F behavior to constant
folding by LLVM.
---
 llvm/lib/Analysis/ConstantFolding.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c5a2c2f52f8dc2..88db315ffd0bcb 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2407,6 +2407,9 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
       break;
     case LibFunc_log1p:
     case LibFunc_log1pf:
+      // Implement optional behavior from C's Annex F for +/-0.0.
+      if (U.isZero())
+        return ConstantFP::get(Ty->getContext(), U);
       if (APF > APFloat::getOne(APF.getSemantics(), true) && TLI->has(Func))
         return ConstantFoldFP(log1p, APF, Ty);
       break;



More information about the llvm-commits mailing list