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

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 25 03:06:08 PST 2024


================
@@ -3466,6 +3467,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;
----------------
MacDue wrote:

> Are there really any cases where one can fail and not the other?

Probably not, but they both look like calls that could return null, so checking them both seems justified.

https://github.com/llvm/llvm-project/pull/114527


More information about the llvm-commits mailing list