@@ -209,13 +209,37 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
209209
210210void TargetCodeGenInfo::setBranchProtectionFnAttributes (
211211 const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) {
212- llvm::AttrBuilder FuncAttrs (F.getContext ());
213- setBranchProtectionFnAttributes (BPI, FuncAttrs);
214- F.addFnAttrs (FuncAttrs);
212+ // Called on already created and initialized function where attributes already
213+ // set from command line attributes but some might need to be removed as the
214+ // actual BPI is different.
215+ if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
216+ F.addFnAttr (" sign-return-address" , BPI.getSignReturnAddrStr ());
217+ F.addFnAttr (" sign-return-address-key" , BPI.getSignKeyStr ());
218+ } else {
219+ if (F.hasFnAttribute (" sign-return-address" ))
220+ F.removeFnAttr (" sign-return-address" );
221+ if (F.hasFnAttribute (" sign-return-address-key" ))
222+ F.removeFnAttr (" sign-return-address-key" );
223+ }
224+
225+ auto AddRemoveAttributeAsSet = [&](bool Set, const StringRef &ModAttr) {
226+ if (Set)
227+ F.addFnAttr (ModAttr);
228+ else if (F.hasFnAttribute (ModAttr))
229+ F.removeFnAttr (ModAttr);
230+ };
231+
232+ AddRemoveAttributeAsSet (BPI.BranchTargetEnforcement ,
233+ " branch-target-enforcement" );
234+ AddRemoveAttributeAsSet (BPI.BranchProtectionPAuthLR ,
235+ " branch-protection-pauth-lr" );
236+ AddRemoveAttributeAsSet (BPI.GuardedControlStack , " guarded-control-stack" );
215237}
216238
217- void TargetCodeGenInfo::setBranchProtectionFnAttributes (
239+ void TargetCodeGenInfo::initBranchProtectionFnAttributes (
218240 const TargetInfo::BranchProtectionInfo &BPI, llvm::AttrBuilder &FuncAttrs) {
241+ // Only used for initializing attributes in the AttrBuilder, which will not
242+ // contain any of these attributes so no need to remove anything.
219243 if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
220244 FuncAttrs.addAttribute (" sign-return-address" , BPI.getSignReturnAddrStr ());
221245 FuncAttrs.addAttribute (" sign-return-address-key" , BPI.getSignKeyStr ());
0 commit comments