[llvm] [llvm][ARM][AArch64] Add attributes to synthetic functions. (PR #83153)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 08:53:01 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Dani (DanielKristofKiss)

<details>
<summary>Changes</summary>

Module flags represent the original intention.

---
Full diff: https://github.com/llvm/llvm-project/pull/83153.diff


1 Files Affected:

- (modified) llvm/lib/IR/Function.cpp (+36) 


``````````diff
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index 056e4f31981a72..5e217cc5680186 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -65,6 +65,8 @@
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ModRef.h"
+#include "llvm/TargetParser/ARMTargetParser.h"
+#include "llvm/TargetParser/Triple.h"
 #include <cassert>
 #include <cstddef>
 #include <cstdint>
@@ -378,6 +380,40 @@ Function *Function::createWithDefaultAttr(FunctionType *Ty,
   }
   if (M->getModuleFlag("function_return_thunk_extern"))
     B.addAttribute(Attribute::FnRetThunkExtern);
+
+  Triple T(M->getTargetTriple());
+
+  auto Profile = llvm::ARM::parseArchProfile(T.getArchName());
+  if ((T.isArmT32() && Profile == llvm::ARM::ProfileKind::M) || T.isAArch64()) {
+    // Check if the module attribute is present and not zero.
+    auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool {
+      const auto *Attr =
+          mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr));
+      return Attr && Attr->getZExtValue();
+    };
+
+    auto AddAttributeIfSet = [&](const StringRef &ModAttr) {
+      if (isModuleAttributeSet(ModAttr))
+        B.addAttribute(ModAttr, "true");
+    };
+
+    StringRef SignType = "none";
+    if (isModuleAttributeSet("sign-return-address"))
+      SignType = "non-leaf";
+    if (isModuleAttributeSet("sign-return-address-all"))
+      SignType = "all";
+    if (SignType != "none") {
+      B.addAttribute("sign-return-address", SignType);
+      if (T.isAArch64())
+        B.addAttribute("sign-return-address-key",
+                       isModuleAttributeSet("sign-return-address-with-bkey")
+                           ? "b_key"
+                           : "a_key");
+    }
+    AddAttributeIfSet("branch-target-enforcement");
+    AddAttributeIfSet("branch-protection-pauth-lr");
+    AddAttributeIfSet("guarded-control-stack");
+  }
   F->addFnAttrs(B);
   return F;
 }

``````````

</details>


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


More information about the llvm-commits mailing list