[clang] [llvm] [OpenMP][CodeGen] Preserve libcalls for declare simd redecls (PR #193881)
Alexey Bataev via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 04:04:20 PST 2026
alexey-bataev wrote:
Null dereference on non-FunctionDecl CurGD, AVX2 mismatch in blocks, and uncached feature map rebuild per call.
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12339,14 +12339,11 @@ CGOpenMPRuntime::getDeclareSimdVariantMappings(CodeGenFunction &CGF,
// Filter by the caller features: a function may have its own target
// features, and full LTO keeps a single declaration of the scalar function.
// CurGD is used instead of CurCodeDecl to keep the MultiVersionIndex, so
// that cpu_specific/cpu_dispatch/target_clones functions get the features of
- // the version being compiled. It is empty for e.g. global initializers.
- llvm::StringMap<bool> Features;
- if (CGF.CurGD.getDecl())
- CGM.getContext().getFunctionFeatureMap(Features, CGF.CurGD);
- else
- CGM.getContext().getFunctionFeatureMap(Features, /*FD=*/nullptr);
+ // the version being compiled. Fall back to TU features when CurCodeDecl
+ // differs from CurGD (e.g. blocks, outlined regions, global initializers).
+ const llvm::StringMap<bool> &Features =
+ CGM.getContext().getFunctionFeatureMap(
+ CGF.CurCodeDecl == CGF.CurGD.getDecl() ? CGF.CurGD : GlobalDecl());
```
Trailing plain redeclaration drops _ZGV* and mappings while CGBuiltin still suppresses the intrinsic
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12319,12 +12319,16 @@ static unsigned getVFISARank(llvm::VFISAKind ISA) {
}
bool CGOpenMPRuntime::hasLibraryDeclareSimdVariants(const FunctionDecl *FD) {
- if (!FD->hasAttr<OMPDeclareSimdDeclAttr>())
+ if (!FD || FD->hasAttr<AsmLabelAttr>())
return false;
unsigned BuiltinID = FD->getBuiltinID();
- return BuiltinID &&
- FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID);
+ if (!BuiltinID ||
+ !FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
+ return false;
+
+ return llvm::any_of(FD->redecls(), [](const FunctionDecl *Redecl) {
+ return Redecl->hasAttr<OMPDeclareSimdDeclAttr>();
+ });
}
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18,6 +18,7 @@
#include "CGDebugInfo.h"
#include "CGObjCRuntime.h"
#include "CGOpenCLRuntime.h"
+#include "CGOpenMPRuntime.h"
#include "CGRecordLayout.h"
#include "CGValue.h"
#include "CodeGenFunction.h"
@@ -30,7 +31,6 @@
#include "clang/Basic/TargetInfo.h"
#include "clang/CodeGenUtils/TargetUtils.h"
#include "llvm/ADT/APFloat.h"
-#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Intrinsics.h"
@@ -73,16 +73,6 @@ static bool shouldEmitBuiltinAsIR(unsigned BuiltinID,
return false;
}
-static bool shouldPreserveLibCallForDeclareSimd(const FunctionDecl *FD,
- const LangOptions &LangOpts) {
- if (!FD || !LangOpts.OpenMP)
- return false;
-
- return llvm::any_of(FD->redecls(), [](const FunctionDecl *Redecl) {
- return Redecl->hasAttr<OMPDeclareSimdDeclAttr>();
- });
-}
-
static Value *EmitTargetArchBuiltinExpr(CodeGenFunction *CGF,
unsigned BuiltinID, const CallExpr *E,
ReturnValueSlot ReturnValue,
@@ -2780,7 +2770,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
OptNone, IsOptimizationEnabled);
if (GenerateFPMathIntrinsics &&
- shouldPreserveLibCallForDeclareSimd(FD, getLangOpts()))
+ getLangOpts().OpenMP &&
+ CGOpenMPRuntime::hasLibraryDeclareSimdVariants(FD))
GenerateFPMathIntrinsics = false;
if (GenerateFPMathIntrinsics) {
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3932,7 +3932,10 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
setKCFIType(FD, F);
- if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
+ if (getLangOpts().OpenMP &&
+ llvm::any_of(FD->redecls(), [](const FunctionDecl *Redecl) {
+ return Redecl->hasAttr<OMPDeclareSimdDeclAttr>();
+ }))
getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
```
Emits VFABI mappings and llvm.compiler.used entries on nobuiltin / -fno-builtin* / __attribute__((no_builtin(...))) calls.
```
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6513,10 +6513,15 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
// The mapping goes on the call, so that each caller only gets the vector
// variants it can run.
if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
getLangOpts().OpenMP && FD && isa<llvm::CallInst>(CI) &&
+ !Attrs.hasFnAttr(llvm::Attribute::NoBuiltin) &&
+ !CurFn->hasFnAttribute("no-builtins") &&
+ !CurFn->hasFnAttribute(("no-builtin-" + FD->getName()).str()) &&
CGOpenMPRuntime::hasLibraryDeclareSimdVariants(FD)) {
if (llvm::Function *F = CI->getCalledFunction();
- F && F->getFunctionType() == CI->getFunctionType()) {
+ F && !F->hasFnAttribute(llvm::Attribute::NoBuiltin) &&
+ F->getFunctionType() == CI->getFunctionType()) {
std::string Mappings =
CGM.getOpenMPRuntime().getDeclareSimdVariantMappings(*this, F);
if (!Mappings.empty())
```
Newly created VecFn declarations carry no function attributes (nounwind, willreturn, memory(...), etc.).
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12409,6 +12409,19 @@ CGOpenMPRuntime::getDeclareSimdVariantMappings(CodeGenFunction &CGF,
VecFn = llvm::Function::Create(VectorTy,
llvm::GlobalValue::ExternalLinkage,
Var.Info.VectorName, M);
+ for (llvm::Attribute::AttrKind Kind :
+ {llvm::Attribute::NoUnwind, llvm::Attribute::WillReturn,
+ llvm::Attribute::NoFree, llvm::Attribute::NoSync,
+ llvm::Attribute::NoCallback})
+ if (Fn->hasFnAttribute(Kind))
+ VecFn->addFnAttr(Kind);
+ if (llvm::Attribute Mem = Fn->getFnAttribute(llvm::Attribute::Memory);
+ Mem.isValid() &&
+ (Mem.getMemoryEffects().getModRef(llvm::IRMemLocation::ArgMem) ==
+ llvm::ModRefInfo::NoModRef ||
+ llvm::none_of(VectorTy->params(), [](llvm::Type *Ty) {
+ return Ty->isVectorTy() && Ty->isPtrOrPtrVectorTy();
+ })))
+ VecFn->addFnAttr(Mem);
}
// The mapping attribute is not a use, so keep the vector function in the
```
https://github.com/llvm/llvm-project/pull/193881
More information about the cfe-commits
mailing list