alexey-bataev wrote:

CurCodeDecl drops MultiVersionIndex (SIGILL under target_clones / cpu_specific)
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12332,9 +12332,8 @@ 
CGOpenMPRuntime::getDeclareSimdVariantMappings(CodeGenFunction &CGF,
                                                llvm::Function *Fn) {
   // Filter by the caller features: a function may have its own target
   // features, and full LTO keeps a single declaration of the scalar function.
-  llvm::StringMap<bool> Features;
-  CGM.getContext().getFunctionFeatureMap(
-      Features, dyn_cast_or_null<FunctionDecl>(CGF.CurCodeDecl));
+  const llvm::StringMap<bool> &Features =
+      CGM.getContext().getFunctionFeatureMap(CGF.CurGD);

```
Crash on non-vectorizable IR types (float _Complex cexpf) and invalid 
vararg/mismatched-type mappings (printf)
```
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6516,7 +6516,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo 
&CallInfo,
   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
       getLangOpts().OpenMP && FD && isa<llvm::CallInst>(CI) &&
       CGOpenMPRuntime::hasLibraryDeclareSimdVariants(FD)) {
-    if (llvm::Function *F = CI->getCalledFunction()) {
+    if (llvm::Function *F = CI->getCalledFunction();
+        F && F->getFunctionType() == CI->getFunctionType()) {
       std::string Mappings =
           CGM.getOpenMPRuntime().getDeclareSimdVariantMappings(*this, F);
       if (!Mappings.empty())
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -42,6 +42,7 @@
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/VFABIDemangler.h"
 #include "llvm/IR/Value.h"
+#include "llvm/IR/VectorTypeUtils.h"
 #include "llvm/Support/AtomicOrdering.h"
 #include "llvm/Support/VirtualFileSystem.h"
@@ -12330,6 +12331,11 @@ bool 
CGOpenMPRuntime::hasLibraryDeclareSimdVariants(const FunctionDecl *FD) {
 std::string
 CGOpenMPRuntime::getDeclareSimdVariantMappings(CodeGenFunction &CGF,
                                                llvm::Function *Fn) {
+  llvm::FunctionType *ScalarFTy = Fn->getFunctionType();
+  if (ScalarFTy->isVarArg() ||
+      !llvm::canVectorizeTy(ScalarFTy->getReturnType()))
+    return {};
+
   // Filter by the caller features: a function may have its own target
   // features, and full LTO keeps a single declaration of the scalar function.
@@ -12358,9 +12364,15 @@ 
CGOpenMPRuntime::getDeclareSimdVariantMappings(CodeGenFunction &CGF,
       continue;
 
     std::optional<llvm::VFInfo> Info =
-        llvm::VFABI::tryDemangleForVFABI(VariantName, Fn->getFunctionType());
+        llvm::VFABI::tryDemangleForVFABI(VariantName, ScalarFTy);
     if (!Info || !isVFISASupported(Info->ISA, Features))
       continue;
+    if (llvm::any_of(Info->Shape.Parameters, [&](const llvm::VFParameter &P) {
+          return P.ParamKind == llvm::VFParamKind::Vector &&
+                 !llvm::VectorType::isValidElementType(
+                     ScalarFTy->getParamType(P.ParamPos));
+        }))
+      continue;

```
Unchecked cast<llvm::Function> crashes on symbol collision and emits wrong-type 
calls on signature collision
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12381,10 +12381,17 @@ 
CGOpenMPRuntime::getDeclareSimdVariantMappings(CodeGenFunction &CGF,
 
     llvm::FunctionType *VectorTy =
         llvm::VFABI::createFunctionType(Var.Info, Fn->getFunctionType());
-    llvm::Function *VecFn = llvm::cast<llvm::Function>(
-        Fn->getParent()
-            ->getOrInsertFunction(Var.Info.VectorName, VectorTy)
-            .getCallee());
+    llvm::Module &M = CGM.getModule();
+    llvm::Function *VecFn = nullptr;
+    if (llvm::GlobalValue *GV = M.getNamedValue(Var.Info.VectorName)) {
+      VecFn = dyn_cast<llvm::Function>(GV);
+      if (!VecFn || VecFn->getFunctionType() != VectorTy)
+        continue;
+    } else {
+      VecFn = llvm::Function::Create(
+          VectorTy, llvm::GlobalValue::ExternalLinkage, Var.Info.VectorName, 
M);
+    }

```
optimizeDoubleFP corrupts/breaks all non-sin/cos double calls and 
optimizeSymmetricCall drops call-site mappings
```
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2099,7 +2099,8 @@ static Value *optimizeDoubleFP(CallInst *CI, 
IRBuilderBase &B,
                                bool isBinary, const TargetLibraryInfo *TLI,
                                bool isPrecise = false) {
   Function *CalleeFn = CI->getCalledFunction();
-  if (!CI->getType()->isDoubleTy() || !CalleeFn)
+  if (!CI->getType()->isDoubleTy() || !CalleeFn ||
+      CI->hasFnAttr(VFABI::MappingsAttrName))
     return nullptr;
 
   // If not all the uses of the function are converted to float, then bail out.
@@ -3177,6 +3178,9 @@ static Value *optimizeSymmetricCall(CallInst *CI, bool 
IsEven,
   if (match(Src, m_OneUse(m_FNegOrFSubNegZero(m_Value(X)))) &&
       (IsEven || !mayFlushDenormalsToPositiveZero(CI))) {
     auto *Call = B.CreateCall(CI->getCalledFunction(), {X}, /*FMFSource=*/CI);
+    if (Attribute A = CI->getAttributes().getFnAttr(VFABI::MappingsAttrName);
+        A.isValid())
+      Call->addFnAttr(A);
     auto *CallInst = copyFlags(*CI, Call);
     if (IsEven) {
       // Even function: f(-x) = f(x)
@@ -3190,6 +3194,9 @@ static Value *optimizeSymmetricCall(CallInst *CI, bool 
IsEven,
   if (IsEven && (match(Src, m_FAbs(m_Value(X))) ||
                  match(Src, m_CopySign(m_Value(X), m_Value())))) {
     auto *Call = B.CreateCall(CI->getCalledFunction(), {X}, /*FMFSource=*/CI);
+    if (Attribute A = CI->getAttributes().getFnAttr(VFABI::MappingsAttrName);
+        A.isValid())
+      Call->addFnAttr(A);
     return copyFlags(*CI, Call);
   }
 
@@ -4331,7 +4338,7 @@ Value 
*LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
     return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
   case LibFunc_sin:
   case LibFunc_cos:
-    if (UnsafeFPShrink && !HasVectorMappings &&
+    if (UnsafeFPShrink &&
         hasFloatVersion(M, CI->getCalledFunction()->getName()))
       if (Value *V = optimizeUnaryDoubleFP(CI, Builder, TLI, true))
         return V;

```

https://github.com/llvm/llvm-project/pull/193881
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to