@@ -4610,10 +4610,6 @@ void CodeGenModule::emitMultiVersionFunctions() {
46104610 }
46114611 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
46124612
4613- ResolverFunc->setLinkage (getMultiversionLinkage (*this , GD));
4614- setGlobalVisibility (ResolverFunc, /* D=*/ nullptr );
4615- setDSOLocal (ResolverFunc);
4616-
46174613 if (!ResolverFunc->hasLocalLinkage () && supportsCOMDAT ())
46184614 ResolverFunc->setComdat (
46194615 getModule ().getOrInsertComdat (ResolverFunc->getName ()));
@@ -4626,6 +4622,7 @@ void CodeGenModule::emitMultiVersionFunctions() {
46264622 });
46274623 CodeGenFunction CGF (*this );
46284624 CGF.EmitMultiVersionResolver (ResolverFunc, Options);
4625+ setMultiVersionResolverAttributes (ResolverFunc, GD);
46294626 }
46304627
46314628 // Ensure that any additions to the deferred decls list caused by emitting a
@@ -4676,9 +4673,6 @@ void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
46764673
46774674 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction (
46784675 ResolverName, ResolverType, ResolverGD, /* ForVTable=*/ false ));
4679- ResolverFunc->setLinkage (getMultiversionLinkage (*this , GD));
4680- setGlobalVisibility (ResolverFunc, /* D=*/ nullptr );
4681- setDSOLocal (ResolverFunc);
46824676
46834677 if (supportsCOMDAT ())
46844678 ResolverFunc->setComdat (
@@ -4745,6 +4739,7 @@ void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
47454739
47464740 CodeGenFunction CGF (*this );
47474741 CGF.EmitMultiVersionResolver (ResolverFunc, Options);
4742+ setMultiVersionResolverAttributes (ResolverFunc, GD);
47484743
47494744 if (getTarget ().supportsIFunc ()) {
47504745 llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage (*this , GD);
@@ -4836,13 +4831,6 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
48364831 if (FD->isTargetMultiVersion () || FD->isTargetClonesMultiVersion ())
48374832 AddDeferredMultiVersionResolverToEmit (GD);
48384833
4839- auto SetResolverAttrs = [&](llvm::Function *Resolver) {
4840- // Set the default target-specific attributes, such as PAC and BTI ones on
4841- // AArch64. Not passing Decl to prevent setting unrelated attributes,
4842- // as Resolver can be shared by multiple declarations.
4843- getTargetCodeGenInfo ().setTargetAttributes (/* D=*/ nullptr , Resolver, *this );
4844- };
4845-
48464834 // For cpu_specific, don't create an ifunc yet because we don't know if the
48474835 // cpu_dispatch will be emitted in this translation unit.
48484836 if (ShouldReturnIFunc) {
@@ -4857,7 +4845,6 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
48574845 " " , Resolver, &getModule ());
48584846 GIF->setName (ResolverName);
48594847 SetCommonAttributes (FD, GIF);
4860- SetResolverAttrs (cast<llvm::Function>(Resolver));
48614848 if (ResolverGV)
48624849 replaceDeclarationWith (ResolverGV, GIF);
48634850 return GIF;
@@ -4868,10 +4855,29 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
48684855 assert (isa<llvm::GlobalValue>(Resolver) && !ResolverGV &&
48694856 " Resolver should be created for the first time" );
48704857 SetCommonAttributes (FD, cast<llvm::GlobalValue>(Resolver));
4871- SetResolverAttrs (cast<llvm::Function>(Resolver));
48724858 return Resolver;
48734859}
48744860
4861+ void CodeGenModule::setMultiVersionResolverAttributes (llvm::Function *Resolver,
4862+ GlobalDecl GD) {
4863+ const NamedDecl *D = dyn_cast_or_null<NamedDecl>(GD.getDecl ());
4864+ Resolver->setLinkage (getMultiversionLinkage (*this , GD));
4865+
4866+ // Function body has to be emitted before calling setGlobalVisibility
4867+ // for Resolver to be considered as definition.
4868+ setGlobalVisibility (Resolver, D);
4869+
4870+ setDSOLocal (Resolver);
4871+
4872+ // Set the default target-specific attributes, such as PAC and BTI ones on
4873+ // AArch64. Not passing Decl to prevent setting unrelated attributes,
4874+ // as Resolver can be shared by multiple declarations.
4875+ // FIXME Some targets may require a non-null D to set some attributes
4876+ // (such as "stackrealign" on X86, even when it is requested via
4877+ // "-mstackrealign" command line option).
4878+ getTargetCodeGenInfo ().setTargetAttributes (/* D=*/ nullptr , Resolver, *this );
4879+ }
4880+
48754881bool CodeGenModule::shouldDropDLLAttribute (const Decl *D,
48764882 const llvm::GlobalValue *GV) const {
48774883 auto SC = GV->getDLLStorageClass ();
0 commit comments