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

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 08:52:28 PST 2024


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

Module flags represent the original intention.

>From d1f6a1f6851cd11178ca2474a5ae28c3026b94cc Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Tue, 27 Feb 2024 10:38:04 +0100
Subject: [PATCH] Add attributes to synthetic functions.

Module flags represent the original intention.
---
 llvm/lib/IR/Function.cpp | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

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;
 }



More information about the llvm-commits mailing list