[llvm] [LLVM] Refactor Autoupgrade function attributes from Module attributes. (PR #84494)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 9 06:57:00 PST 2024
https://github.com/DanielKristofKiss updated https://github.com/llvm/llvm-project/pull/84494
>From 36544d98f20dad644591feaded013cca34b5a6f1 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Fri, 8 Mar 2024 15:06:28 +0100
Subject: [PATCH 1/4] [NFC][LLVM] Refactor Autoupgrade function attributes from
Module attributes.
Refactoring #82763 to cache module attributes.
---
llvm/lib/IR/AutoUpgrade.cpp | 71 +++++++++++++------------------------
1 file changed, 24 insertions(+), 47 deletions(-)
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index 25992395e471b3..856b6d656fc891 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -5185,62 +5185,39 @@ static bool isModuleAttributeSet(Module &M, const StringRef &ModAttr) {
return Attr && Attr->getZExtValue();
}
-// Copy an attribute from module to the function if exists.
-// First value of the pair is used when the module attribute is not zero
-// the second otherwise.
-static void
-CopyModuleAttributeToFunction(Function &F, StringRef FnAttrName,
- StringRef ModAttrName,
- std::pair<StringRef, StringRef> Values) {
- if (F.hasFnAttribute(FnAttrName))
- return;
- F.addFnAttr(FnAttrName, isModuleAttributeSet(*F.getParent(), ModAttrName)
- ? Values.first
- : Values.second);
-}
-
-// Copy a boolean attribute from module to the function if exists.
-// Module attribute treated false if zero otherwise true.
-static void CopyModuleAttributeToFunction(Function &F, StringRef AttrName) {
- CopyModuleAttributeToFunction(
- F, AttrName, AttrName,
- std::make_pair<StringRef, StringRef>("true", "false"));
-}
-
-// Copy an attribute from module to the function if exists.
-// First value of the pair is used when the module attribute is not zero
-// the second otherwise.
-static void
-CopyModuleAttributeToFunction(Function &F, StringRef AttrName,
- std::pair<StringRef, StringRef> Values) {
- CopyModuleAttributeToFunction(F, AttrName, AttrName, Values);
-}
-
void llvm::CopyModuleAttrToFunctions(Module &M) {
Triple T(M.getTargetTriple());
if (!T.isThumb() && !T.isARM() && !T.isAArch64())
return;
+ StringRef SignTypeValue = "none";
+ if (isModuleAttributeSet(M, "sign-return-address"))
+ SignTypeValue = "non-leaf";
+ if (isModuleAttributeSet(M, "sign-return-address-all"))
+ SignTypeValue = "all";
+
+ StringRef BTEValue =
+ isModuleAttributeSet(M, "branch-target-enforcement") ? "true" : "false";
+ StringRef BPPLValue =
+ isModuleAttributeSet(M, "branch-protection-pauth-lr") ? "true" : "false";
+ StringRef GCSValue =
+ isModuleAttributeSet(M, "guarded-control-stack") ? "true" : "false";
+ StringRef SignKeyValue =
+ isModuleAttributeSet(M, "sign-return-address-with-bkey") ? "b_key" : "a_key";
+
for (Function &F : M.getFunctionList()) {
if (F.isDeclaration())
continue;
+ auto SetFunctionAttrIfNotSet = [&](StringRef FnAttrName, StringRef Value) {
+ if (!F.hasFnAttribute(FnAttrName))
+ F.addFnAttr(FnAttrName, Value);
+ };
- if (!F.hasFnAttribute("sign-return-address")) {
- StringRef SignType = "none";
- if (isModuleAttributeSet(M, "sign-return-address"))
- SignType = "non-leaf";
-
- if (isModuleAttributeSet(M, "sign-return-address-all"))
- SignType = "all";
-
- F.addFnAttr("sign-return-address", SignType);
- }
- CopyModuleAttributeToFunction(F, "branch-target-enforcement");
- CopyModuleAttributeToFunction(F, "branch-protection-pauth-lr");
- CopyModuleAttributeToFunction(F, "guarded-control-stack");
- CopyModuleAttributeToFunction(
- F, "sign-return-address-key",
- std::make_pair<StringRef, StringRef>("b_key", "a_key"));
+ SetFunctionAttrIfNotSet("sign-return-address", SignTypeValue);
+ SetFunctionAttrIfNotSet("branch-target-enforcement", BTEValue);
+ SetFunctionAttrIfNotSet("branch-protection-pauth-lr", BPPLValue);
+ SetFunctionAttrIfNotSet("guarded-control-stack", GCSValue);
+ SetFunctionAttrIfNotSet("sign-return-address-key", SignKeyValue);
}
}
>From f7370edabd77d7574906702b8233e7fd59e540b7 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Fri, 8 Mar 2024 17:27:08 +0100
Subject: [PATCH 2/4] fixup! [NFC][LLVM] Refactor Autoupgrade function
attributes from Module attributes.
---
llvm/lib/IR/AutoUpgrade.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index 856b6d656fc891..7801df7172dc4a 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -5203,7 +5203,8 @@ void llvm::CopyModuleAttrToFunctions(Module &M) {
StringRef GCSValue =
isModuleAttributeSet(M, "guarded-control-stack") ? "true" : "false";
StringRef SignKeyValue =
- isModuleAttributeSet(M, "sign-return-address-with-bkey") ? "b_key" : "a_key";
+ isModuleAttributeSet(M, "sign-return-address-with-bkey") ? "b_key"
+ : "a_key";
for (Function &F : M.getFunctionList()) {
if (F.isDeclaration())
>From 187a9d18f04bd73c1d60d4e1e24a8400a1fee249 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Fri, 8 Mar 2024 21:20:16 +0100
Subject: [PATCH 3/4] fixup! [NFC][LLVM] Refactor Autoupgrade function
attributes from Module attributes.
---
llvm/lib/IR/AutoUpgrade.cpp | 27 ++++++++++---------
.../LTO/AArch64/link-sign-return-address.ll | 4 +--
2 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index 7801df7172dc4a..f1c1a9c2b03024 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -5182,7 +5182,14 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
static bool isModuleAttributeSet(Module &M, const StringRef &ModAttr) {
const auto *Attr =
mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr));
- return Attr && Attr->getZExtValue();
+ return Attr && Attr->isOne();
+}
+
+// Check if the function attribute is not present and set it.
+static void SetFunctionAttrIfNotSet(Function &F, StringRef FnAttrName,
+ StringRef Value) {
+ if (!F.hasFnAttribute(FnAttrName))
+ F.addFnAttr(FnAttrName, Value);
}
void llvm::CopyModuleAttrToFunctions(Module &M) {
@@ -5191,10 +5198,10 @@ void llvm::CopyModuleAttrToFunctions(Module &M) {
return;
StringRef SignTypeValue = "none";
- if (isModuleAttributeSet(M, "sign-return-address"))
- SignTypeValue = "non-leaf";
if (isModuleAttributeSet(M, "sign-return-address-all"))
SignTypeValue = "all";
+ else if (isModuleAttributeSet(M, "sign-return-address"))
+ SignTypeValue = "non-leaf";
StringRef BTEValue =
isModuleAttributeSet(M, "branch-target-enforcement") ? "true" : "false";
@@ -5209,16 +5216,12 @@ void llvm::CopyModuleAttrToFunctions(Module &M) {
for (Function &F : M.getFunctionList()) {
if (F.isDeclaration())
continue;
- auto SetFunctionAttrIfNotSet = [&](StringRef FnAttrName, StringRef Value) {
- if (!F.hasFnAttribute(FnAttrName))
- F.addFnAttr(FnAttrName, Value);
- };
- SetFunctionAttrIfNotSet("sign-return-address", SignTypeValue);
- SetFunctionAttrIfNotSet("branch-target-enforcement", BTEValue);
- SetFunctionAttrIfNotSet("branch-protection-pauth-lr", BPPLValue);
- SetFunctionAttrIfNotSet("guarded-control-stack", GCSValue);
- SetFunctionAttrIfNotSet("sign-return-address-key", SignKeyValue);
+ SetFunctionAttrIfNotSet(F, "sign-return-address", SignTypeValue);
+ SetFunctionAttrIfNotSet(F, "branch-target-enforcement", BTEValue);
+ SetFunctionAttrIfNotSet(F, "branch-protection-pauth-lr", BPPLValue);
+ SetFunctionAttrIfNotSet(F, "guarded-control-stack", GCSValue);
+ SetFunctionAttrIfNotSet(F, "sign-return-address-key", SignKeyValue);
}
}
diff --git a/llvm/test/LTO/AArch64/link-sign-return-address.ll b/llvm/test/LTO/AArch64/link-sign-return-address.ll
index c25857ceed7b40..6d940d2cbacb14 100644
--- a/llvm/test/LTO/AArch64/link-sign-return-address.ll
+++ b/llvm/test/LTO/AArch64/link-sign-return-address.ll
@@ -29,9 +29,9 @@ entry:
!3 = !{i32 8, !"sign-return-address-with-bkey", i32 0}
; CHECK-DUMP: <foo>:
-; CHECK-DUMP: paciasp
+; CHECK-DUMP: pacibsp
; CHECK-DUMP: mov w0, #0x2a
-; CHECK-DUMP: autiasp
+; CHECK-DUMP: autibsp
; CHECK-DUMP: ret
; CHECK-DUMP: <main>:
; CHECK-DUMP-NOT: paciasp
>From 5a062e9b9d4da12573d67fcf02f9a1baf7159115 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Sat, 9 Mar 2024 15:55:41 +0100
Subject: [PATCH 4/4] Update the
llvm/test/LTO/AArch64/link-branch-target-enforcement.ll too.
Foo is signed with the Bkey.
---
llvm/test/LTO/AArch64/link-branch-target-enforcement.ll | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/llvm/test/LTO/AArch64/link-branch-target-enforcement.ll b/llvm/test/LTO/AArch64/link-branch-target-enforcement.ll
index 74d9c86881d52c..8313d812e189a4 100644
--- a/llvm/test/LTO/AArch64/link-branch-target-enforcement.ll
+++ b/llvm/test/LTO/AArch64/link-branch-target-enforcement.ll
@@ -30,9 +30,11 @@ entry:
; CHECK-NOT: linking module flags 'branch-target-enforcement': IDs have conflicting values in
; CHECK-DUMP: <main>:
+; CHECK-DUMP: paciasp
+; CHECK-DUMP: str
; CHECK-DUMP: bl 0x8 <main+0x8>
; CHECK-DUMP: <foo>:
-; CHECK-DUMP: paciasp
+; CHECK-DUMP: pacibsp
; `main` doesn't support BTI while `foo` does, so in the binary
; we should see only PAC which is supported by both.
More information about the llvm-commits
mailing list