[llvm] [CodeGen] Teach ReplaceWithVeclib split vector llvm.sincos when only … (PR #194639)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 07:15:48 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Kito Cheng (kito-cheng)

<details>
<summary>Changes</summary>

…sin/cos veclib mappings exist

Some vector math libraries provide vector sin and vector cos but no vector sincos or no sincos with an ABI that LLVM can emit.

The one of the common case is glibc libmvec on x86: it exposes `_ZGV{b,c,d,e}N{2,4,8,16}vvv_sincos{,f}` symbols, but those use a vectors-of-pointers output ABI that expandMultipleResultFPLibCall does not currently support. As a result, sincos will falls back to scalar sincos calls even when the target has a fully working vector sin and vector cos.

So we trying to split it into separate sin and cos calls, which will then be replaced with vector calls if the target supports it, it generally better than scalarized sincos calls.

---
Full diff: https://github.com/llvm/llvm-project/pull/194639.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/ReplaceWithVeclib.cpp (+106-9) 
- (modified) llvm/test/CodeGen/X86/veclib-llvm.sincos.ll (+38-24) 


``````````diff
diff --git a/llvm/lib/CodeGen/ReplaceWithVeclib.cpp b/llvm/lib/CodeGen/ReplaceWithVeclib.cpp
index 407d6d503cb10..1a77fe590def8 100644
--- a/llvm/lib/CodeGen/ReplaceWithVeclib.cpp
+++ b/llvm/lib/CodeGen/ReplaceWithVeclib.cpp
@@ -208,19 +208,116 @@ static bool replaceWithCallToVeclib(const TargetLibraryInfo &TLI,
   return true;
 }
 
+/// Returns true when \p TLI has a vector mapping for the scalar function name
+/// \p Name at \p EC (matching either masked or unmasked variants).
+static bool hasVectorMapping(const TargetLibraryInfo &TLI, StringRef Name,
+                             ElementCount EC) {
+  return TLI.getVectorMappingInfo(Name, EC, /*Masked=*/false) ||
+         TLI.getVectorMappingInfo(Name, EC, /*Masked=*/true);
+}
+
+/// Returns true when \p TLI has a vector mapping for \p IID at the given
+/// element type and \p EC.
+static bool hasIntrinsicVectorMapping(const TargetLibraryInfo &TLI,
+                                      Intrinsic::ID IID, Type *ScalarTy,
+                                      ElementCount EC, Module *M) {
+  std::string Name = Intrinsic::getName(IID, {ScalarTy}, M);
+  return hasVectorMapping(TLI, Name, EC);
+}
+
+/// If \p II is a vector llvm.sincos with no direct vector library mapping but
+/// the target does have vector mappings for both llvm.sin and llvm.cos at the
+/// same element count, replace it with separate llvm.sin and llvm.cos calls
+/// and run the standard veclib replacement on each.
+static bool trySplitVectorSinCos(const TargetLibraryInfo &TLI,
+                                 IntrinsicInst *II,
+                                 SmallVectorImpl<Instruction *> &Replaced) {
+  if (II->getIntrinsicID() != Intrinsic::sincos)
+    return false;
+  Value *Arg = II->getArgOperand(0);
+  auto *VTy = dyn_cast<VectorType>(Arg->getType());
+  if (!VTy)
+    return false;
+
+  ElementCount EC = VTy->getElementCount();
+  Type *ScalarTy = VTy->getElementType();
+  Module *M = II->getModule();
+
+  // If a vector sincos mapping exists for the intrinsic name (e.g.
+  // "llvm.sincos.f32") or for the scalar libcall name ("sincos"/"sincosf"),
+  // leave the call alone -- SelectionDAG legalization will handle it via
+  // expandMultipleResultFPLibCall when the runtime libcall impl is enabled.
+  if (hasIntrinsicVectorMapping(TLI, Intrinsic::sincos, ScalarTy, EC, M))
+    return false;
+  StringRef LibcallName;
+  if (ScalarTy->isFloatTy())
+    LibcallName = "sincosf";
+  else if (ScalarTy->isDoubleTy())
+    LibcallName = "sincos";
+  if (!LibcallName.empty() && hasVectorMapping(TLI, LibcallName, EC))
+    return false;
+
+  // Splitting is only worthwhile when both sin and cos have vector mappings.
+  if (!hasIntrinsicVectorMapping(TLI, Intrinsic::sin, ScalarTy, EC, M) ||
+      !hasIntrinsicVectorMapping(TLI, Intrinsic::cos, ScalarTy, EC, M))
+    return false;
+
+  // All users must be extractvalue with index 0 or 1; otherwise we cannot
+  // safely rewire results.
+  for (User *U : II->users()) {
+    auto *EV = dyn_cast<ExtractValueInst>(U);
+    if (!EV || EV->getNumIndices() != 1 ||
+        (EV->getIndices()[0] != 0 && EV->getIndices()[0] != 1))
+      return false;
+  }
+
+  IRBuilder<> B(II);
+  Function *SinFn =
+      Intrinsic::getOrInsertDeclaration(M, Intrinsic::sin, Arg->getType());
+  Function *CosFn =
+      Intrinsic::getOrInsertDeclaration(M, Intrinsic::cos, Arg->getType());
+  CallInst *SinCall = B.CreateCall(SinFn, {Arg}, "sin");
+  CallInst *CosCall = B.CreateCall(CosFn, {Arg}, "cos");
+  SinCall->copyFastMathFlags(II);
+  CosCall->copyFastMathFlags(II);
+
+  // Forward extractvalue uses to the new calls.
+  for (User *U : llvm::make_early_inc_range(II->users())) {
+    auto *EV = cast<ExtractValueInst>(U);
+    EV->replaceAllUsesWith(EV->getIndices()[0] == 0 ? SinCall : CosCall);
+    EV->eraseFromParent();
+  }
+
+  // Replace each new call with the vector library function.
+  if (replaceWithCallToVeclib(TLI, cast<IntrinsicInst>(SinCall)))
+    Replaced.push_back(SinCall);
+  if (replaceWithCallToVeclib(TLI, cast<IntrinsicInst>(CosCall)))
+    Replaced.push_back(CosCall);
+
+  return true;
+}
+
 static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
   SmallVector<Instruction *> ReplacedCalls;
   for (auto &I : instructions(F)) {
-    // Process only intrinsic calls that return void or a vector.
-    if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
-      if (II->getIntrinsicID() == Intrinsic::not_intrinsic)
-        continue;
-      if (!II->getType()->isVectorTy() && !II->getType()->isVoidTy())
-        continue;
-
-      if (replaceWithCallToVeclib(TLI, II))
-        ReplacedCalls.push_back(&I);
+    auto *II = dyn_cast<IntrinsicInst>(&I);
+    if (!II || II->getIntrinsicID() == Intrinsic::not_intrinsic)
+      continue;
+
+    // Vector llvm.sincos returns a struct so it does not fit the generic
+    // path below; try to split it into separate sin and cos calls when the
+    // target has vector mappings for them.
+    if (trySplitVectorSinCos(TLI, II, ReplacedCalls)) {
+      ReplacedCalls.push_back(&I);
+      continue;
     }
+
+    // Process only intrinsic calls that return void or a vector.
+    if (!II->getType()->isVectorTy() && !II->getType()->isVoidTy())
+      continue;
+
+    if (replaceWithCallToVeclib(TLI, II))
+      ReplacedCalls.push_back(&I);
   }
   // Erase any intrinsic calls that were replaced with vector library calls.
   for (auto *I : ReplacedCalls)
diff --git a/llvm/test/CodeGen/X86/veclib-llvm.sincos.ll b/llvm/test/CodeGen/X86/veclib-llvm.sincos.ll
index 11bc122cb80c8..adca02ab0871f 100644
--- a/llvm/test/CodeGen/X86/veclib-llvm.sincos.ll
+++ b/llvm/test/CodeGen/X86/veclib-llvm.sincos.ll
@@ -9,11 +9,15 @@
 ; RUN: llc -mtriple=x86_64-unknown-linux-gnu -mcpu=x86-64-v4 -vector-library=LIBMVEC < %s | FileCheck %s --check-prefixes=CHECK,GLIBC,GLIBC-AVX512
 
 define void @test_sincos_v4f32(<4 x float> %x, ptr noalias %out_sin, ptr noalias %out_cos) {
-; CHECK-LABEL: test_sincos_v4f32:
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
+; AMD-LABEL: test_sincos_v4f32:
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+;
+; GLIBC-LABEL: test_sincos_v4f32:
+; GLIBC:    callq _ZGVbN4v_sinf at PLT
+; GLIBC:    callq _ZGVbN4v_cosf at PLT
   %result = call { <4 x float>, <4 x float> } @llvm.sincos.v4f32(<4 x float> %x)
   %result.0 = extractvalue { <4 x float>, <4 x float> } %result, 0
   %result.1 = extractvalue { <4 x float>, <4 x float> } %result, 1
@@ -23,15 +27,19 @@ define void @test_sincos_v4f32(<4 x float> %x, ptr noalias %out_sin, ptr noalias
 }
 
 define void @test_sincos_v8f32(<8 x float> %x, ptr noalias %out_sin, ptr noalias %out_cos) {
-; CHECK-LABEL: test_sincos_v8f32:
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
-; CHECK:    callq sincosf at PLT
+; AMD-LABEL: test_sincos_v8f32:
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+; AMD:    callq sincosf at PLT
+;
+; GLIBC-LABEL: test_sincos_v8f32:
+; GLIBC:    callq _ZGVdN8v_sinf at PLT
+; GLIBC:    callq _ZGVdN8v_cosf at PLT
   %result = call { <8 x float>, <8 x float> } @llvm.sincos.v8f32(<8 x float> %x)
   %result.0 = extractvalue { <8 x float>, <8 x float> } %result, 0
   %result.1 = extractvalue { <8 x float>, <8 x float> } %result, 1
@@ -67,9 +75,13 @@ define void @test_sincos_v16f32(<16 x float> %x, ptr noalias %out_sin, ptr noali
 }
 
 define void @test_sincos_v2f64(<2 x double> %x, ptr noalias %out_sin, ptr noalias %out_cos) {
-; CHECK-LABEL: test_sincos_v2f64:
-; CHECK:    callq sincos at PLT
-; CHECK:    callq sincos at PLT
+; AMD-LABEL: test_sincos_v2f64:
+; AMD:    callq sincos at PLT
+; AMD:    callq sincos at PLT
+;
+; GLIBC-LABEL: test_sincos_v2f64:
+; GLIBC:    callq _ZGVbN2v_sin at PLT
+; GLIBC:    callq _ZGVbN2v_cos at PLT
   %result = call { <2 x double>, <2 x double> } @llvm.sincos.v2f64(<2 x double> %x)
   %result.0 = extractvalue { <2 x double>, <2 x double> } %result, 0
   %result.1 = extractvalue { <2 x double>, <2 x double> } %result, 1
@@ -79,11 +91,15 @@ define void @test_sincos_v2f64(<2 x double> %x, ptr noalias %out_sin, ptr noalia
 }
 
 define void @test_sincos_v4f64(<4 x double> %x, ptr noalias %out_sin, ptr noalias %out_cos) {
-; CHECK-LABEL: test_sincos_v4f64:
-; CHECK:    callq sincos at PLT
-; CHECK:    callq sincos at PLT
-; CHECK:    callq sincos at PLT
-; CHECK:    callq sincos at PLT
+; AMD-LABEL: test_sincos_v4f64:
+; AMD:    callq sincos at PLT
+; AMD:    callq sincos at PLT
+; AMD:    callq sincos at PLT
+; AMD:    callq sincos at PLT
+;
+; GLIBC-LABEL: test_sincos_v4f64:
+; GLIBC:    callq _ZGVdN4v_sin at PLT
+; GLIBC:    callq _ZGVdN4v_cos at PLT
   %result = call { <4 x double>, <4 x double> } @llvm.sincos.v4f64(<4 x double> %x)
   %result.0 = extractvalue { <4 x double>, <4 x double> } %result, 0
   %result.1 = extractvalue { <4 x double>, <4 x double> } %result, 1
@@ -110,12 +126,10 @@ define void @test_sincos_v8f64(<8 x double> %x, ptr noalias %out_sin, ptr noalia
   ret void
 }
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
-; AMD: {{.*}}
 ; AMD-AVX: {{.*}}
 ; AMD-AVX2: {{.*}}
 ; AMD-AVX512: {{.*}}
 ; AMD-SSE: {{.*}}
-; GLIBC: {{.*}}
 ; GLIBC-AVX: {{.*}}
 ; GLIBC-AVX2: {{.*}}
 ; GLIBC-AVX512: {{.*}}

``````````

</details>


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


More information about the llvm-commits mailing list