[clang] [Arm][AArch64][Clang] Respect function's branch protection attributes. (PR #101978)

Ties Stuij via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 9 02:47:28 PDT 2024


================
@@ -209,9 +209,28 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
 
 void TargetCodeGenInfo::setBranchProtectionFnAttributes(
     const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) {
-  llvm::AttrBuilder FuncAttrs(F.getContext());
-  setBranchProtectionFnAttributes(BPI, FuncAttrs);
-  F.addFnAttrs(FuncAttrs);
+  if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
+    F.addFnAttr("sign-return-address", BPI.getSignReturnAddrStr());
+    F.addFnAttr("sign-return-address-key", BPI.getSignKeyStr());
+  } else {
+    if (F.hasFnAttribute("sign-return-address"))
+      F.removeFnAttr("sign-return-address");
+    if (F.hasFnAttribute("sign-return-address-key"))
+      F.removeFnAttr("sign-return-address-key");
+  }
+
+  auto AddRemoveAttributeAsSet = [&](bool Set, const StringRef &ModAttr) {
+    if (Set)
+      F.addFnAttr(ModAttr);
+    else if (F.hasFnAttribute(ModAttr))
+      F.removeFnAttr(ModAttr);
+  };
+
+  AddRemoveAttributeAsSet(BPI.BranchTargetEnforcement,
+                          "branch-target-enforcement");
+  AddRemoveAttributeAsSet(BPI.BranchProtectionPAuthLR,
+                          "branch-protection-pauth-lr");
+  AddRemoveAttributeAsSet(BPI.GuardedControlStack, "guarded-control-stack");
 }
 
 void TargetCodeGenInfo::setBranchProtectionFnAttributes(
----------------
stuij wrote:

I can see that this AttrBuilder version of the fn is still used in `getTrivialDefaultFunctionAttributes` in `CGCall.cpp`. I guess in practice the outcome would still be the same for this use case between old and new implementation, but even if I didn't miss something it does feel a bit icky to do so.

Perhaps add a comment to explain the code duplication?

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


More information about the cfe-commits mailing list