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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 22 11:10:44 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;
----------------
arsenm wrote:

Inside ConstantFoldScalarSincosCall, you assume they can fail independently. Are there really any cases where one can fail and not the other? In any case, the error handling should be consistent in both places 

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


More information about the llvm-commits mailing list