[llvm] [ConstantFold] Fold `erf` and `erff` when the input parameter is a constant value. (PR #113079)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 21 17:35:31 PDT 2024
https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/113079
>From d6360d2e54493b1c46ffdd1ab7a30e006ef05537 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 20 Oct 2024 17:58:37 +0800
Subject: [PATCH 1/5] const fold erf
---
llvm/lib/Analysis/ConstantFolding.cpp | 9 +-
llvm/test/Transforms/InstCombine/erf.ll | 185 ++++++++++++++++++++++++
2 files changed, 192 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/Transforms/InstCombine/erf.ll
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c0104d2bc26112..082df2972dfda1 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1671,8 +1671,8 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
Name == "cos" || Name == "cosf" ||
Name == "cosh" || Name == "coshf";
case 'e':
- return Name == "exp" || Name == "expf" ||
- Name == "exp2" || Name == "exp2f";
+ return Name == "exp" || Name == "expf" || Name == "exp2" ||
+ Name == "exp2f" || Name == "erf" || Name == "erff";
case 'f':
return Name == "fabs" || Name == "fabsf" ||
Name == "floor" || Name == "floorf" ||
@@ -2411,6 +2411,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
break;
case LibFunc_logl:
return nullptr;
+ case LibFunc_erf:
+ case LibFunc_erff:
+ if (TLI->has(Func))
+ return ConstantFoldFP(erf, APF, Ty);
+ break;
case LibFunc_nearbyint:
case LibFunc_nearbyintf:
case LibFunc_rint:
diff --git a/llvm/test/Transforms/InstCombine/erf.ll b/llvm/test/Transforms/InstCombine/erf.ll
new file mode 100644
index 00000000000000..e2f1abca074669
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/erf.ll
@@ -0,0 +1,185 @@
+; 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 @erff_const() {
+; CHECK-LABEL: define float @erff_const() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 5.000000e-01)
+; CHECK-NEXT: ret float 0x3FE0A7EF60000000
+;
+ %r = call float @erff(float 5.000000e-01)
+ ret float %r
+}
+
+define double @erf_const() {
+; CHECK-LABEL: define double @erf_const() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double -5.000000e-01)
+; CHECK-NEXT: ret double 0xBFE0A7EF5C18EDD2
+;
+ %r = call double @erf(double -5.000000e-01)
+ ret double %r
+}
+
+define float @erff_zero() {
+; CHECK-LABEL: define float @erff_zero() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0.000000e+00)
+; CHECK-NEXT: ret float 0.000000e+00
+;
+ %r = call float @erff(float 0.000000e+00)
+ ret float %r
+}
+
+define double @erf_zero() {
+; CHECK-LABEL: define double @erf_zero() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0.000000e+00)
+; CHECK-NEXT: ret double 0.000000e+00
+;
+ %r = call double @erf(double 0.000000e+00)
+ ret double %r
+}
+
+define float @erff_neg_zero() {
+; CHECK-LABEL: define float @erff_neg_zero() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float -0.000000e+00)
+; CHECK-NEXT: ret float -0.000000e+00
+;
+ %r = call float @erff(float -0.000000e+00)
+ ret float %r
+}
+
+define double @erf_neg_zero() {
+; CHECK-LABEL: define double @erf_neg_zero() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double -0.000000e+00)
+; CHECK-NEXT: ret double -0.000000e+00
+;
+ %r = call double @erf(double -0.000000e+00)
+ ret double %r
+}
+
+define float @erff_inf() {
+; CHECK-LABEL: define float @erff_inf() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF0000000000000)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0x7FF0000000000000)
+ ret float %r
+}
+
+define double @erf_inf() {
+; CHECK-LABEL: define double @erf_inf() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF0000000000000)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0x7FF0000000000000)
+ ret double %r
+}
+
+define float @erff_inf_memory_none() {
+; CHECK-LABEL: define float @erff_inf_memory_none() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF0000000000000) #[[ATTR0:[0-9]+]]
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0x7FF0000000000000) readnone
+ ret float %r
+}
+
+define double @erf_inf_memory_none() {
+; CHECK-LABEL: define double @erf_inf_memory_none() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF0000000000000) #[[ATTR0]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0x7FF0000000000000) readnone
+ ret double %r
+}
+
+define float @erff_neg_inf() {
+; CHECK-LABEL: define float @erff_neg_inf() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0xFFF0000000000000)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0xFFF0000000000000)
+ ret float %r
+}
+
+define double @erf_neg_inf() {
+; CHECK-LABEL: define double @erf_neg_inf() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0xFFF0000000000000)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0xFFF0000000000000)
+ ret double %r
+}
+
+define float @erff_neg_inf_memory_none() {
+; CHECK-LABEL: define float @erff_neg_inf_memory_none() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0xFFF0000000000000) #[[ATTR0]]
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0xFFF0000000000000) readnone
+ ret float %r
+}
+
+define double @erf_neg_inf_memory_none() {
+; CHECK-LABEL: define double @erf_neg_inf_memory_none() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0xFFF0000000000000) #[[ATTR0]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0xFFF0000000000000) readnone
+ ret double %r
+}
+
+define float @erff_nan() {
+; CHECK-LABEL: define float @erff_nan() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0x7FF8000000000000)
+ ret float %r
+}
+
+define double @erf_nan() {
+; CHECK-LABEL: define double @erf_nan() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0x7FF8000000000000)
+ ret double %r
+}
+
+define float @erff_nan_memory_none() {
+; CHECK-LABEL: define float @erff_nan_memory_none() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) #[[ATTR0]]
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0x7FF8000000000000) readnone
+ ret float %r
+}
+
+define double @erf_nan_memory_none() {
+; CHECK-LABEL: define double @erf_nan_memory_none() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) #[[ATTR0]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0x7FF8000000000000) readnone
+ ret double %r
+}
+
+define float @erff_poison() {
+; CHECK-LABEL: define float @erff_poison() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float poison)
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float poison)
+ ret float %r
+}
+
+define double @erf_poison() {
+; CHECK-LABEL: define double @erf_poison() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double poison)
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double poison)
+ ret double %r
+}
+
+declare float @erff(float)
+declare double @erf(double)
>From f7ece1876bd4dfbd1abd49ec36c97dc52fd704fb Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 20 Oct 2024 22:30:59 +0800
Subject: [PATCH 2/5] target triple
---
llvm/test/Transforms/InstCombine/erf.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/Transforms/InstCombine/erf.ll b/llvm/test/Transforms/InstCombine/erf.ll
index e2f1abca074669..613581fe8b7179 100644
--- a/llvm/test/Transforms/InstCombine/erf.ll
+++ b/llvm/test/Transforms/InstCombine/erf.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+; RUN: opt < %s -passes=instcombine -mtriple x86_64-unknown-linux-gnu -S | FileCheck %s
define float @erff_const() {
; CHECK-LABEL: define float @erff_const() {
>From c29a50f9a531dbe96d003b4f7f3556184918d0fd Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 20 Oct 2024 23:26:49 +0800
Subject: [PATCH 3/5] fix test
---
llvm/lib/Analysis/ConstantFolding.cpp | 5 ++++-
llvm/test/Transforms/InstCombine/erf.ll | 22 ++++++++--------------
2 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 082df2972dfda1..64bfe04434fdd6 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3601,7 +3601,6 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
// Per POSIX, this MAY fail if Op is denormal. We choose not failing.
return true;
-
case LibFunc_asinl:
case LibFunc_asin:
case LibFunc_asinf:
@@ -3629,6 +3628,10 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
case LibFunc_sqrtf:
return Op.isNaN() || Op.isZero() || !Op.isNegative();
+ case LibFunc_erf:
+ case LibFunc_erff:
+ return true;
+
// FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
// maybe others?
default:
diff --git a/llvm/test/Transforms/InstCombine/erf.ll b/llvm/test/Transforms/InstCombine/erf.ll
index 613581fe8b7179..4c43c7c2a663d6 100644
--- a/llvm/test/Transforms/InstCombine/erf.ll
+++ b/llvm/test/Transforms/InstCombine/erf.ll
@@ -3,7 +3,6 @@
define float @erff_const() {
; CHECK-LABEL: define float @erff_const() {
-; CHECK-NEXT: [[R:%.*]] = call float @erff(float 5.000000e-01)
; CHECK-NEXT: ret float 0x3FE0A7EF60000000
;
%r = call float @erff(float 5.000000e-01)
@@ -12,7 +11,6 @@ define float @erff_const() {
define double @erf_const() {
; CHECK-LABEL: define double @erf_const() {
-; CHECK-NEXT: [[R:%.*]] = call double @erf(double -5.000000e-01)
; CHECK-NEXT: ret double 0xBFE0A7EF5C18EDD2
;
%r = call double @erf(double -5.000000e-01)
@@ -21,7 +19,6 @@ define double @erf_const() {
define float @erff_zero() {
; CHECK-LABEL: define float @erff_zero() {
-; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0.000000e+00)
; CHECK-NEXT: ret float 0.000000e+00
;
%r = call float @erff(float 0.000000e+00)
@@ -30,7 +27,6 @@ define float @erff_zero() {
define double @erf_zero() {
; CHECK-LABEL: define double @erf_zero() {
-; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0.000000e+00)
; CHECK-NEXT: ret double 0.000000e+00
;
%r = call double @erf(double 0.000000e+00)
@@ -39,7 +35,6 @@ define double @erf_zero() {
define float @erff_neg_zero() {
; CHECK-LABEL: define float @erff_neg_zero() {
-; CHECK-NEXT: [[R:%.*]] = call float @erff(float -0.000000e+00)
; CHECK-NEXT: ret float -0.000000e+00
;
%r = call float @erff(float -0.000000e+00)
@@ -48,7 +43,6 @@ define float @erff_neg_zero() {
define double @erf_neg_zero() {
; CHECK-LABEL: define double @erf_neg_zero() {
-; CHECK-NEXT: [[R:%.*]] = call double @erf(double -0.000000e+00)
; CHECK-NEXT: ret double -0.000000e+00
;
%r = call double @erf(double -0.000000e+00)
@@ -75,7 +69,7 @@ define double @erf_inf() {
define float @erff_inf_memory_none() {
; CHECK-LABEL: define float @erff_inf_memory_none() {
-; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF0000000000000) #[[ATTR0:[0-9]+]]
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF0000000000000) #[[ATTR1:[0-9]+]]
; CHECK-NEXT: ret float [[R]]
;
%r = call float @erff(float 0x7FF0000000000000) readnone
@@ -84,7 +78,7 @@ define float @erff_inf_memory_none() {
define double @erf_inf_memory_none() {
; CHECK-LABEL: define double @erf_inf_memory_none() {
-; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF0000000000000) #[[ATTR0]]
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF0000000000000) #[[ATTR1]]
; CHECK-NEXT: ret double [[R]]
;
%r = call double @erf(double 0x7FF0000000000000) readnone
@@ -111,7 +105,7 @@ define double @erf_neg_inf() {
define float @erff_neg_inf_memory_none() {
; CHECK-LABEL: define float @erff_neg_inf_memory_none() {
-; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0xFFF0000000000000) #[[ATTR0]]
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0xFFF0000000000000) #[[ATTR1]]
; CHECK-NEXT: ret float [[R]]
;
%r = call float @erff(float 0xFFF0000000000000) readnone
@@ -120,7 +114,7 @@ define float @erff_neg_inf_memory_none() {
define double @erf_neg_inf_memory_none() {
; CHECK-LABEL: define double @erf_neg_inf_memory_none() {
-; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0xFFF0000000000000) #[[ATTR0]]
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0xFFF0000000000000) #[[ATTR1]]
; CHECK-NEXT: ret double [[R]]
;
%r = call double @erf(double 0xFFF0000000000000) readnone
@@ -147,7 +141,7 @@ define double @erf_nan() {
define float @erff_nan_memory_none() {
; CHECK-LABEL: define float @erff_nan_memory_none() {
-; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) #[[ATTR0]]
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) #[[ATTR1]]
; CHECK-NEXT: ret float [[R]]
;
%r = call float @erff(float 0x7FF8000000000000) readnone
@@ -156,7 +150,7 @@ define float @erff_nan_memory_none() {
define double @erf_nan_memory_none() {
; CHECK-LABEL: define double @erf_nan_memory_none() {
-; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) #[[ATTR0]]
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) #[[ATTR1]]
; CHECK-NEXT: ret double [[R]]
;
%r = call double @erf(double 0x7FF8000000000000) readnone
@@ -181,5 +175,5 @@ define double @erf_poison() {
ret double %r
}
-declare float @erff(float)
-declare double @erf(double)
+declare float @erff(float) willreturn
+declare double @erf(double) willreturn
>From 270d720f346b2312d5ef81aa5c3dd0a41cad9050 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 20 Oct 2024 23:35:45 +0800
Subject: [PATCH 4/5] add strict test
---
llvm/test/Transforms/InstCombine/erf.ll | 36 +++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/erf.ll b/llvm/test/Transforms/InstCombine/erf.ll
index 4c43c7c2a663d6..b5779205c52ccc 100644
--- a/llvm/test/Transforms/InstCombine/erf.ll
+++ b/llvm/test/Transforms/InstCombine/erf.ll
@@ -175,5 +175,41 @@ define double @erf_poison() {
ret double %r
}
+define float @erff_const_strictfp() {
+; CHECK-LABEL: define float @erff_const_strictfp() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 5.000000e-01) #[[ATTR2:[0-9]+]]
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 5.000000e-01) strictfp
+ ret float %r
+}
+
+define double @erf_const_strictfp() {
+; CHECK-LABEL: define double @erf_const_strictfp() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double -5.000000e-01) #[[ATTR2]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double -5.000000e-01) strictfp
+ ret double %r
+}
+
+define float @erff_nan_strictfp() {
+; CHECK-LABEL: define float @erff_nan_strictfp() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) #[[ATTR2]]
+; CHECK-NEXT: ret float [[R]]
+;
+ %r = call float @erff(float 0x7FF8000000000000) strictfp
+ ret float %r
+}
+
+define double @erf_nan_strictfp() {
+; CHECK-LABEL: define double @erf_nan_strictfp() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) #[[ATTR2]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = call double @erf(double 0x7FF8000000000000) strictfp
+ ret double %r
+}
+
declare float @erff(float) willreturn
declare double @erf(double) willreturn
>From aaf3681bbf402df3ae393ba7048aea3b90210b8b Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 21 Oct 2024 21:57:24 +0800
Subject: [PATCH 5/5] revert
---
llvm/lib/Analysis/ConstantFolding.cpp | 4 ----
llvm/test/Transforms/InstCombine/erf.ll | 6 ++++++
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 64bfe04434fdd6..ff33a41abfd3c0 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3628,10 +3628,6 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
case LibFunc_sqrtf:
return Op.isNaN() || Op.isZero() || !Op.isNegative();
- case LibFunc_erf:
- case LibFunc_erff:
- return true;
-
// FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
// maybe others?
default:
diff --git a/llvm/test/Transforms/InstCombine/erf.ll b/llvm/test/Transforms/InstCombine/erf.ll
index b5779205c52ccc..13d6545acd5bea 100644
--- a/llvm/test/Transforms/InstCombine/erf.ll
+++ b/llvm/test/Transforms/InstCombine/erf.ll
@@ -3,6 +3,7 @@
define float @erff_const() {
; CHECK-LABEL: define float @erff_const() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 5.000000e-01)
; CHECK-NEXT: ret float 0x3FE0A7EF60000000
;
%r = call float @erff(float 5.000000e-01)
@@ -11,6 +12,7 @@ define float @erff_const() {
define double @erf_const() {
; CHECK-LABEL: define double @erf_const() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double -5.000000e-01)
; CHECK-NEXT: ret double 0xBFE0A7EF5C18EDD2
;
%r = call double @erf(double -5.000000e-01)
@@ -19,6 +21,7 @@ define double @erf_const() {
define float @erff_zero() {
; CHECK-LABEL: define float @erff_zero() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0.000000e+00)
; CHECK-NEXT: ret float 0.000000e+00
;
%r = call float @erff(float 0.000000e+00)
@@ -27,6 +30,7 @@ define float @erff_zero() {
define double @erf_zero() {
; CHECK-LABEL: define double @erf_zero() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0.000000e+00)
; CHECK-NEXT: ret double 0.000000e+00
;
%r = call double @erf(double 0.000000e+00)
@@ -35,6 +39,7 @@ define double @erf_zero() {
define float @erff_neg_zero() {
; CHECK-LABEL: define float @erff_neg_zero() {
+; CHECK-NEXT: [[R:%.*]] = call float @erff(float -0.000000e+00)
; CHECK-NEXT: ret float -0.000000e+00
;
%r = call float @erff(float -0.000000e+00)
@@ -43,6 +48,7 @@ define float @erff_neg_zero() {
define double @erf_neg_zero() {
; CHECK-LABEL: define double @erf_neg_zero() {
+; CHECK-NEXT: [[R:%.*]] = call double @erf(double -0.000000e+00)
; CHECK-NEXT: ret double -0.000000e+00
;
%r = call double @erf(double -0.000000e+00)
More information about the llvm-commits
mailing list