[llvm] Reland "[InstSimplify] Add basic constant folding for `llvm.sincos`" (PR #119192)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 9 02:08:06 PST 2024


https://github.com/MacDue created https://github.com/llvm/llvm-project/pull/119192

This calls into the existing constant folding for `llvm.sin` and `llvm.cos`, which currently does not fold for any non-finite values, so most tests are negative tests at the moment.

Note: The constant folding does not consider the `afn` fast-math flag and will produce the same result regardless of if the flag is set.

This is a reland of #114527 that updates the syntax of one of the tests from: `<float 1.000000e+00, float 1.000000e+00>` to `splat (float 1.000000e+00)`.

>From e5699a902908ba85f87942d22fab91b3472b9ede Mon Sep 17 00:00:00 2001
From: Benjamin Maxwell <benjamin.maxwell at arm.com>
Date: Fri, 6 Sep 2024 08:58:03 +0000
Subject: [PATCH 1/5] [InstSimplify] Add basic constant folding for
 `llvm.sincos`

This calls into the existing constant folding for `llvm.sin` and
`llvm.cos`, which currently does not fold for any non-finite values,
so most tests are negative tests.
---
 llvm/lib/Analysis/ConstantFolding.cpp       |  39 ++++++
 llvm/test/Transforms/InstSimplify/sincos.ll | 125 ++++++++++++++++++++
 2 files changed, 164 insertions(+)
 create mode 100644 llvm/test/Transforms/InstSimplify/sincos.ll

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index efbccee76f2c51..a702447c981457 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1632,6 +1632,7 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
   case Intrinsic::sqrt:
   case Intrinsic::sin:
   case Intrinsic::cos:
+  case Intrinsic::sincos:
   case Intrinsic::pow:
   case Intrinsic::powi:
   case Intrinsic::ldexp:
@@ -3534,6 +3535,44 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
       return nullptr;
     return ConstantStruct::get(StTy, Result0, Result1);
   }
+  case Intrinsic::sincos: {
+    Type *Ty = StTy->getContainedType(0);
+    Type *TyScalar = Ty->getScalarType();
+
+    auto ConstantFoldScalarSincosCall =
+        [&](Constant *Op) -> std::pair<Constant *, Constant *> {
+      Constant *SinResult =
+          ConstantFoldScalarCall(Name, Intrinsic::sin, TyScalar, Op, TLI, Call);
+      if (!SinResult)
+        return {};
+      Constant *CosResult =
+          ConstantFoldScalarCall(Name, Intrinsic::cos, TyScalar, Op, TLI, Call);
+      if (!CosResult)
+        return {};
+      return std::make_pair(SinResult, CosResult);
+    };
+
+    if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
+      SmallVector<Constant *, 4> SinResults(FVTy->getNumElements());
+      SmallVector<Constant *, 4> CosResults(FVTy->getNumElements());
+
+      for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
+        Constant *Lane = Operands[0]->getAggregateElement(I);
+        std::tie(SinResults[I], CosResults[I]) =
+            ConstantFoldScalarSincosCall(Lane);
+        if (!SinResults[I])
+          return nullptr;
+      }
+
+      return ConstantStruct::get(StTy, ConstantVector::get(SinResults),
+                                 ConstantVector::get(CosResults));
+    }
+
+    auto [SinResult, CosResult] = ConstantFoldScalarSincosCall(Operands[0]);
+    if (!SinResult)
+      return nullptr;
+    return ConstantStruct::get(StTy, SinResult, CosResult);
+  }
   default:
     // TODO: Constant folding of vector intrinsics that fall through here does
     // not work (e.g. overflow intrinsics)
diff --git a/llvm/test/Transforms/InstSimplify/sincos.ll b/llvm/test/Transforms/InstSimplify/sincos.ll
new file mode 100644
index 00000000000000..c6dc1357b4197a
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/sincos.ll
@@ -0,0 +1,125 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt -S -passes=instsimplify %s | FileCheck %s
+
+define { float, float } @sincos_zero() {
+; CHECK-LABEL: define { float, float } @sincos_zero() {
+; CHECK-NEXT:    ret { float, float } { float 0.000000e+00, float 1.000000e+00 }
+;
+  %ret = call { float, float } @llvm.sincos.f32(float zeroinitializer)
+  ret { float, float } %ret
+}
+
+define { float, float } @sincos_neg_zero() {
+; CHECK-LABEL: define { float, float } @sincos_neg_zero() {
+; CHECK-NEXT:    ret { float, float } { float -0.000000e+00, float 1.000000e+00 }
+;
+  %ret = call { float, float } @llvm.sincos.f32(float -0.0)
+  ret { float, float } %ret
+}
+
+define { <2 x float>, <2 x float> } @sincos_zero_vector() {
+; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_zero_vector() {
+; CHECK-NEXT:    ret { <2 x float>, <2 x float> } { <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00> }
+;
+  %ret = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> zeroinitializer)
+  ret { <2 x float>, <2 x float> } %ret
+}
+
+define { float, float } @sincos_poison() {
+; CHECK-LABEL: define { float, float } @sincos_poison() {
+; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float poison)
+; CHECK-NEXT:    ret { float, float } [[RET]]
+;
+  %ret = call { float, float } @llvm.sincos.f32(float poison)
+  ret { float, float } %ret
+}
+
+define { <2 x float>, <2 x float> } @sincos_poison_vector() {
+; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_poison_vector() {
+; CHECK-NEXT:    [[RET:%.*]] = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> poison)
+; CHECK-NEXT:    ret { <2 x float>, <2 x float> } [[RET]]
+;
+  %ret = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> poison)
+  ret { <2 x float>, <2 x float> } %ret
+}
+
+define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_poison_scalable_vector() {
+; CHECK-LABEL: define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_poison_scalable_vector() {
+; CHECK-NEXT:    [[RET:%.*]] = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> poison)
+; CHECK-NEXT:    ret { <vscale x 2 x float>, <vscale x 2 x float> } [[RET]]
+;
+  %ret = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> poison)
+  ret { <vscale x 2 x float>, <vscale x 2 x float> } %ret
+}
+
+define { float, float } @sincos_undef() {
+; CHECK-LABEL: define { float, float } @sincos_undef() {
+; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float undef)
+; CHECK-NEXT:    ret { float, float } [[RET]]
+;
+  %ret = call { float, float } @llvm.sincos.f32(float undef)
+  ret { float, float } %ret
+}
+
+define { <2 x float>, <2 x float> } @sincos_undef_vector() {
+; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_undef_vector() {
+; CHECK-NEXT:    [[RET:%.*]] = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> undef)
+; CHECK-NEXT:    ret { <2 x float>, <2 x float> } [[RET]]
+;
+  %ret = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> undef)
+  ret { <2 x float>, <2 x float> } %ret
+}
+
+define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_undef_scalable_vector() {
+; CHECK-LABEL: define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_undef_scalable_vector() {
+; CHECK-NEXT:    [[RET:%.*]] = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> undef)
+; CHECK-NEXT:    ret { <vscale x 2 x float>, <vscale x 2 x float> } [[RET]]
+;
+  %ret = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> undef)
+  ret { <vscale x 2 x float>, <vscale x 2 x float> } %ret
+}
+
+define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_zero_scalable_vector() {
+; CHECK-LABEL: define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_zero_scalable_vector() {
+; CHECK-NEXT:    [[RET:%.*]] = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> zeroinitializer)
+; CHECK-NEXT:    ret { <vscale x 2 x float>, <vscale x 2 x float> } [[RET]]
+;
+  %ret = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> zeroinitializer)
+  ret { <vscale x 2 x float>, <vscale x 2 x float> } %ret
+}
+
+define { float, float } @sincos_inf() {
+; CHECK-LABEL: define { float, float } @sincos_inf() {
+; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float 0x7FF0000000000000)
+; CHECK-NEXT:    ret { float, float } [[RET]]
+;
+  %ret = call { float, float } @llvm.sincos.f32(float 0x7FF0000000000000)
+  ret { float, float } %ret
+}
+
+define { float, float } @sincos_neginf() {
+; CHECK-LABEL: define { float, float } @sincos_neginf() {
+; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float 0xFFF0000000000000)
+; CHECK-NEXT:    ret { float, float } [[RET]]
+;
+  %ret = call { float, float } @llvm.sincos.f32(float 0xFFF0000000000000)
+  ret { float, float } %ret
+}
+
+define { float, float } @sincos_qnam() {
+; CHECK-LABEL: define { float, float } @sincos_qnam() {
+; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float 0x7FF8000000000000)
+; CHECK-NEXT:    ret { float, float } [[RET]]
+;
+  %ret = call { float, float } @llvm.sincos.f32(float 0x7FF8000000000000)
+  ret { float, float } %ret
+}
+
+define { float, float } @sincos_snam() {
+; CHECK-LABEL: define { float, float } @sincos_snam() {
+; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float 0x7FF0000020000000)
+; CHECK-NEXT:    ret { float, float } [[RET]]
+;
+  %ret = call { float, float } @llvm.sincos.f32(float bitcast (i32 2139095041 to float))
+  ret { float, float } %ret
+}

>From 0aa73f1b212ecabf671bd48910bcee5da0dbe61b Mon Sep 17 00:00:00 2001
From: Benjamin Maxwell <benjamin.maxwell at arm.com>
Date: Mon, 4 Nov 2024 14:38:29 +0000
Subject: [PATCH 2/5] Fix typo

---
 llvm/test/Transforms/InstSimplify/sincos.ll | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/test/Transforms/InstSimplify/sincos.ll b/llvm/test/Transforms/InstSimplify/sincos.ll
index c6dc1357b4197a..d493fe2c0e706a 100644
--- a/llvm/test/Transforms/InstSimplify/sincos.ll
+++ b/llvm/test/Transforms/InstSimplify/sincos.ll
@@ -106,8 +106,8 @@ define { float, float } @sincos_neginf() {
   ret { float, float } %ret
 }
 
-define { float, float } @sincos_qnam() {
-; CHECK-LABEL: define { float, float } @sincos_qnam() {
+define { float, float } @sincos_qnan() {
+; CHECK-LABEL: define { float, float } @sincos_qnan() {
 ; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float 0x7FF8000000000000)
 ; CHECK-NEXT:    ret { float, float } [[RET]]
 ;
@@ -115,8 +115,8 @@ define { float, float } @sincos_qnam() {
   ret { float, float } %ret
 }
 
-define { float, float } @sincos_snam() {
-; CHECK-LABEL: define { float, float } @sincos_snam() {
+define { float, float } @sincos_snan() {
+; CHECK-LABEL: define { float, float } @sincos_snan() {
 ; CHECK-NEXT:    [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float 0x7FF0000020000000)
 ; CHECK-NEXT:    ret { float, float } [[RET]]
 ;

>From 71ccb2f78efdf1eac0cff26fd0897001fe07e5c1 Mon Sep 17 00:00:00 2001
From: Benjamin Maxwell <benjamin.maxwell at arm.com>
Date: Fri, 8 Nov 2024 15:34:22 +0000
Subject: [PATCH 3/5] Add normal value tests

---
 llvm/test/Transforms/InstSimplify/sincos.ll | 25 ++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/InstSimplify/sincos.ll b/llvm/test/Transforms/InstSimplify/sincos.ll
index d493fe2c0e706a..8f6d346e6c624a 100644
--- a/llvm/test/Transforms/InstSimplify/sincos.ll
+++ b/llvm/test/Transforms/InstSimplify/sincos.ll
@@ -1,4 +1,3 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
 ; RUN: opt -S -passes=instsimplify %s | FileCheck %s
 
 define { float, float } @sincos_zero() {
@@ -17,6 +16,30 @@ define { float, float } @sincos_neg_zero() {
   ret { float, float } %ret
 }
 
+define { float, float } @sincos_one() {
+; CHECK-LABEL: define { float, float } @sincos_one() {
+; CHECK-NEXT:    ret { float, float } { float [[$SIN_ONE:.+]], float [[$COS_ONE:.+]] }
+;
+  %ret = call { float, float } @llvm.sincos.f32(float 1.0)
+  ret { float, float } %ret
+}
+
+define { float, float } @sincos_two() {
+; CHECK-LABEL: define { float, float } @sincos_two() {
+; CHECK-NEXT:    ret { float, float } { float [[$SIN_TWO:.+]], float [[$COS_TWO:.+]] }
+;
+  %ret = call { float, float } @llvm.sincos.f32(float 2.0)
+  ret { float, float } %ret
+}
+
+define { <2 x float>, <2 x float> } @sincos_vector() {
+; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_vector() {
+; CHECK-NEXT:    ret { <2 x float>, <2 x float> } { <2 x float> <float [[$SIN_ONE]], float [[$SIN_TWO]]>, <2 x float> <float [[$COS_ONE]], float [[$COS_TWO]]> }
+;
+  %ret = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> <float 1.0, float 2.0>)
+  ret { <2 x float>, <2 x float> } %ret
+}
+
 define { <2 x float>, <2 x float> } @sincos_zero_vector() {
 ; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_zero_vector() {
 ; CHECK-NEXT:    ret { <2 x float>, <2 x float> } { <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00> }

>From 2536ea85364f3b19f0a9b462e3314bd83894451d Mon Sep 17 00:00:00 2001
From: Benjamin Maxwell <benjamin.maxwell at arm.com>
Date: Mon, 25 Nov 2024 10:59:47 +0000
Subject: [PATCH 4/5] Fixups

---
 llvm/lib/Analysis/ConstantFolding.cpp | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index a702447c981457..0233d31459062d 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3543,24 +3543,20 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
         [&](Constant *Op) -> std::pair<Constant *, Constant *> {
       Constant *SinResult =
           ConstantFoldScalarCall(Name, Intrinsic::sin, TyScalar, Op, TLI, Call);
-      if (!SinResult)
-        return {};
       Constant *CosResult =
           ConstantFoldScalarCall(Name, Intrinsic::cos, TyScalar, Op, TLI, Call);
-      if (!CosResult)
-        return {};
       return std::make_pair(SinResult, CosResult);
     };
 
     if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
-      SmallVector<Constant *, 4> SinResults(FVTy->getNumElements());
-      SmallVector<Constant *, 4> CosResults(FVTy->getNumElements());
+      SmallVector<Constant *> SinResults(FVTy->getNumElements());
+      SmallVector<Constant *> CosResults(FVTy->getNumElements());
 
       for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
         Constant *Lane = Operands[0]->getAggregateElement(I);
         std::tie(SinResults[I], CosResults[I]) =
             ConstantFoldScalarSincosCall(Lane);
-        if (!SinResults[I])
+        if (!SinResults[I] || !CosResults[I])
           return nullptr;
       }
 
@@ -3569,7 +3565,7 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
     }
 
     auto [SinResult, CosResult] = ConstantFoldScalarSincosCall(Operands[0]);
-    if (!SinResult)
+    if (!SinResult || !CosResult)
       return nullptr;
     return ConstantStruct::get(StTy, SinResult, CosResult);
   }

>From 4c0f07128b0c5426bc2a2809a68267c5f9fed7cb Mon Sep 17 00:00:00 2001
From: Benjamin Maxwell <benjamin.maxwell at arm.com>
Date: Mon, 9 Dec 2024 10:03:32 +0000
Subject: [PATCH 5/5] Update test syntax

---
 llvm/test/Transforms/InstSimplify/sincos.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/InstSimplify/sincos.ll b/llvm/test/Transforms/InstSimplify/sincos.ll
index 8f6d346e6c624a..e0f81ee45af056 100644
--- a/llvm/test/Transforms/InstSimplify/sincos.ll
+++ b/llvm/test/Transforms/InstSimplify/sincos.ll
@@ -42,7 +42,7 @@ define { <2 x float>, <2 x float> } @sincos_vector() {
 
 define { <2 x float>, <2 x float> } @sincos_zero_vector() {
 ; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_zero_vector() {
-; CHECK-NEXT:    ret { <2 x float>, <2 x float> } { <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00> }
+; CHECK-NEXT:    ret { <2 x float>, <2 x float> } { <2 x float> zeroinitializer, <2 x float> splat (float 1.000000e+00) }
 ;
   %ret = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> zeroinitializer)
   ret { <2 x float>, <2 x float> } %ret



More information about the llvm-commits mailing list