[clang] [llvm] [OpenMP][CodeGen] Preserve libcalls for declare simd redecls (PR #193881)

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 18:02:53 PST 2026


alexey-bataev wrote:

1. x86 multi-register vector return ABI mismatch (SIGSEGV), unsupported x86_fp80/fp128/non-pow2 VF, AArch64 NEON > 512-bit HVA limit
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12484,38 +12484,48 @@ static bool isX86VFISA(llvm::VFISAKind ISA) {
          ISA == llvm::VFISAKind::AVX2 || ISA == llvm::VFISAKind::AVX512;
 }
 
 /// Returns true if the vector types of \p VectorTy are passed the way the
-/// variant of \p ISA expects them. A fixed vector wider than the variant's
-/// register width is only compatible when the caller cannot use wider
-/// registers either, so that both sides split it across registers of the
-/// variant's width. The AVX ISA class has no 256-bit integer operations, its
-/// integer vectors use 128-bit registers.
+/// variant of \p ISA expects them. On x86, a return vector wider than the
+/// variant's register width uses a hidden sret pointer in the SysV VFABI, which
+/// does not match LLVM's return lowering; a parameter wider than the variant's
+/// register width is only compatible when the caller cannot use wider
+/// registers either. The AVX ISA class uses 128-bit registers for integer
+/// vectors. On AArch64 Advanced SIMD, vectors wider than four 128-bit registers
+/// exceed the AAPCS64 HVA limit.
 static bool hasCompatibleVectorABI(llvm::VFISAKind ISA,
                                    llvm::FunctionType *VectorTy,
                                    unsigned CallerX86RegWidth,
                                    unsigned PreferWidth,
                                    const llvm::DataLayout &DL) {
   unsigned RegisterWidth = getVFISARegisterWidth(ISA);
   if (PreferWidth && RegisterWidth > PreferWidth)
     return false;
   bool IsX86 = isX86VFISA(ISA);
-  auto CheckTy = [&](llvm::Type *Ty) {
+  auto CheckTy = [&](llvm::Type *Ty, bool IsReturn) {
     auto *VTy = dyn_cast<llvm::FixedVectorType>(Ty);
     if (!VTy)
       return true;
     uint64_t Width = DL.getTypeSizeInBits(VTy).getFixedValue();
     if (PreferWidth && Width > PreferWidth)
       return false;
+    if (ISA == llvm::VFISAKind::AdvancedSIMD)
+      return Width <= 4 * RegisterWidth;
     if (!IsX86)
       return true;
-    unsigned VariantRegWidth = (ISA == llvm::VFISAKind::AVX &&
-                                !VTy->getElementType()->isFloatingPointTy())
+    llvm::Type *EltTy = VTy->getElementType();
+    if (!llvm::isPowerOf2_32(VTy->getNumElements()) ||
+        llvm::hasIrregularType(EltTy, DL) ||
+        DL.getTypeSizeInBits(EltTy).getFixedValue() > 64)
+      return false;
+    unsigned VariantRegWidth = (ISA == llvm::VFISAKind::AVX &&
+                                !EltTy->isFloatingPointTy())
                                    ? 128
                                    : RegisterWidth;
-    return Width <= VariantRegWidth || CallerX86RegWidth <= VariantRegWidth;
+    if (Width <= VariantRegWidth)
+      return true;
+    return !IsReturn && CallerX86RegWidth <= VariantRegWidth;
   };
   llvm::Type *RetTy = VectorTy->getReturnType();
-  if (!llvm::all_of(llvm::getContainedTypes(RetTy), CheckTy))
+  if (!llvm::all_of(llvm::getContainedTypes(RetTy),
+                    [&](llvm::Type *Ty) { return CheckTy(Ty, true); }))
     return false;
-  return llvm::all_of(VectorTy->params(), CheckTy);
+  return llvm::all_of(VectorTy->params(),
+                      [&](llvm::Type *Ty) { return CheckTy(Ty, false); });
 }

```
2. x86 UEFI64 and AArch64 Arm64EC ABI mismatch
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12553,13 +12553,15 @@ bool CGOpenMPRuntime::hasLibraryDeclareSimdVariants(const CodeGenFunction &CGF,
   const llvm::StringMap<bool> &Features = getCallerFeatureMap(CGF);
   const llvm::Triple &TT = Ctx.getTargetInfo().getTriple();
   if (TT.isX86()) {
-    // The Windows x86 calling conventions do not pass the vectors the way the
-    // SysV VFABI variants expect them.
-    if (TT.isOSWindows() || !Features.lookup("sse2"))
+    // Windows and UEFI x86 calling conventions do not pass vectors the way the
+    // SysV VFABI variants expect them.
+    if (TT.isOSWindows() || TT.isUEFI() || !Features.lookup("sse2"))
       return false;
   } else if (TT.getArch() == llvm::Triple::aarch64) {
+    if (TT.isWindowsArm64EC())
+      return false;
     if (Ctx.getTargetInfo().hasFeature("sve")) {
       if (!Features.lookup("sve"))
         return false;

```
3. 4 remaining paths drop VFABI mappings
```
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2344,7 +2344,8 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
 
       // Create new exp{,2}() with the product as its argument.
       Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
-      ExpFn = BaseFn->doesNotAccessMemory()
+      ExpFn = (BaseFn->doesNotAccessMemory() &&
+               !BaseFn->hasFnAttr(VFABI::MappingsAttrName))
                   ? B.CreateUnaryIntrinsic(ID, FMul, nullptr, ExpName)
                   : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
                                          LibFnLongDouble, B,
@@ -2645,7 +2646,8 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
 
   // powf(x, itofp(y)) -> powi(x, y)
   // The powi exponent must be a scalar integer, so a vector y is not usable.
-  if (AllowApprox && !Expo->getType()->isVectorTy() &&
+  if (!Pow->hasFnAttr(VFABI::MappingsAttrName) && AllowApprox &&
+      !Expo->getType()->isVectorTy() &&
       (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
     if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
       return copyFlags(*Pow, createPowWithIntegerExponent(Base, ExpoI, M, B));
@@ -3019,7 +3021,7 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
       OtherOp = Op0;
     }
   }
-  if (!RepeatOp)
+  if (!RepeatOp || (OtherOp && CI->hasFnAttr(VFABI::MappingsAttrName)))
     return Ret;
 
   // Fast math flags for any created instructions should match the sqrt
@@ -3338,7 +3340,8 @@ void LibCallSimplifier::classifyArgUse(
   Module *M = CI->getModule();
   Function *Callee = CI->getCalledFunction();
   LibFunc Func = Callee ? TLI->getLibFunc(*Callee) : NotLibFunc;
-  if (!isLibFuncEmittable(M, TLI, Func) || !isTrigLibCall(CI))
+  if (!isLibFuncEmittable(M, TLI, Func) || !isTrigLibCall(CI) ||
+      CI->hasFnAttr(VFABI::MappingsAttrName))
     return;
 
   if (IsFloat) {

```


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


More information about the cfe-commits mailing list