[clang] [lld] [llvm] [ARM][AArch64] BTI,GCS,PAC Module flag update. (PR #86212)

2024-03-22 Thread Daniel Kiss via cfe-commits

https://github.com/DanielKristofKiss updated 
https://github.com/llvm/llvm-project/pull/86212

>From 33d8277d188f82847d914273be2379151dd33d41 Mon Sep 17 00:00:00 2001
From: Daniel Kiss 
Date: Fri, 8 Mar 2024 15:06:28 +0100
Subject: [PATCH 1/2] BTI,GCS,PAC Module flag update.

Module flag is used to indicate the feature to be propagated to the
function. As now the frontend emits all attributes accoringly let's
help the automerger to only do work when old and new bitcodes are
merged.
Autoupgrade function attributes from Module attributes when needed.
---
 clang/lib/CodeGen/CodeGenModule.cpp   |  19 ++--
 .../CodeGen/aarch64-sign-return-address.c |  12 +--
 .../CodeGen/arm-branch-protection-attr-2.c|   8 +-
 .../arm-ignore-branch-protection-option.c |   2 +-
 lld/test/ELF/lto/aarch64_inline.ll|  73 +
 llvm/include/llvm/IR/AutoUpgrade.h|   3 +
 llvm/lib/AsmParser/LLParser.cpp   |   1 +
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp |   2 +
 llvm/lib/IR/AutoUpgrade.cpp   | 100 +
 llvm/lib/Linker/IRMover.cpp   |  10 ++
 llvm/lib/Target/ARM/ARMAsmPrinter.cpp |   2 +-
 llvm/lib/Transforms/IPO/LowerTypeTests.cpp|   2 +-
 .../CodeGen/Thumb2/pacbti-m-outliner-5.ll |   2 +-
 llvm/test/LTO/AArch64/Inputs/foo.ll   |  16 ---
 llvm/test/LTO/AArch64/TestInputs/bar.ll   |  35 ++
 llvm/test/LTO/AArch64/TestInputs/foo.ll   |  38 +++
 llvm/test/LTO/AArch64/TestInputs/old.ll   |  46 
 .../AArch64/link-branch-target-enforcement.ll |   5 +-
 .../LTO/AArch64/link-sign-return-address.ll   | 102 ++
 llvm/test/Linker/link-arm-and-thumb.ll|   6 +-
 20 files changed, 444 insertions(+), 40 deletions(-)
 create mode 100644 lld/test/ELF/lto/aarch64_inline.ll
 delete mode 100644 llvm/test/LTO/AArch64/Inputs/foo.ll
 create mode 100644 llvm/test/LTO/AArch64/TestInputs/bar.ll
 create mode 100644 llvm/test/LTO/AArch64/TestInputs/foo.ll
 create mode 100644 llvm/test/LTO/AArch64/TestInputs/old.ll
 create mode 100644 llvm/test/LTO/AArch64/link-sign-return-address.ll

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index cb153066b28dd1..1acc0510256268 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1173,22 +1173,29 @@ void CodeGenModule::Release() {
   "tag-stack-memory-buildattr", 1);
 
   if (T.isARM() || T.isThumb() || T.isAArch64()) {
+// Previously 1 is used and meant for the backed to derive the function
+// attribute form it. 2 now means function attributes already set for all
+// functions in this module, so no need to propagate those from the module
+// flag. Value is only used in case of LTO module merge because the backend
+// will see all required function attribute set already. Value is used
+// before modules got merged. Any posive value means the feature is active
+// and required binary markings need to be emit accordingly.
 if (LangOpts.BranchTargetEnforcement)
   getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
-1);
+2);
 if (LangOpts.BranchProtectionPAuthLR)
   getModule().addModuleFlag(llvm::Module::Min, 
"branch-protection-pauth-lr",
-1);
+2);
 if (LangOpts.GuardedControlStack)
-  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
 if (LangOpts.hasSignReturnAddress())
-  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 2);
 if (LangOpts.isSignReturnAddressScopeAll())
   getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
-1);
+2);
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
-"sign-return-address-with-bkey", 1);
+"sign-return-address-with-bkey", 2);
   }
 
   if (CodeGenOpts.StackClashProtector)
diff --git a/clang/test/CodeGen/aarch64-sign-return-address.c 
b/clang/test/CodeGen/aarch64-sign-return-address.c
index 8bc54b1a56c38c..35c56889e07071 100644
--- a/clang/test/CodeGen/aarch64-sign-return-address.c
+++ b/clang/test/CodeGen/aarch64-sign-return-address.c
@@ -22,17 +22,17 @@
 // NONE-NOT:  !"branch-target-enforcement"
 // ALL-NOT:   !"branch-target-enforcement"
 // PART-NOT:  !"branch-target-enforcement"
-// BTE:   !{i32 8, !"branch-target-enforcement", i32 1}
+// BTE:   !{i32 8, !"branch-target-enforcement", i32 2}
 // B-KEY-NOT: !"branch-target-enforcement"
 
 // NONE-NOT:  

[clang] [lld] [llvm] [ARM][AArch64] BTI,GCS,PAC Module flag update. (PR #86212)

2024-03-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-lld

Author: Daniel Kiss (DanielKristofKiss)


Changes

Module flag is used to indicate the feature to be propagated to the function. 
As now the frontend emits all attributes accordingly let's help the auto 
upgrade to only do work when old and new bitcodes are merged.

Depends on #82819 and #86031

---

Patch is 26.38 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/86212.diff


20 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+13-6) 
- (modified) clang/test/CodeGen/aarch64-sign-return-address.c (+6-6) 
- (modified) clang/test/CodeGen/arm-branch-protection-attr-2.c (+4-4) 
- (modified) clang/test/Frontend/arm-ignore-branch-protection-option.c (+1-1) 
- (added) lld/test/ELF/lto/aarch64_inline.ll (+73) 
- (modified) llvm/include/llvm/IR/AutoUpgrade.h (+3) 
- (modified) llvm/lib/AsmParser/LLParser.cpp (+1) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+2) 
- (modified) llvm/lib/IR/AutoUpgrade.cpp (+100) 
- (modified) llvm/lib/Linker/IRMover.cpp (+10) 
- (modified) llvm/lib/Target/ARM/ARMAsmPrinter.cpp (+1-1) 
- (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-5.ll (+1-1) 
- (removed) llvm/test/LTO/AArch64/Inputs/foo.ll (-16) 
- (added) llvm/test/LTO/AArch64/TestInputs/bar.ll (+35) 
- (added) llvm/test/LTO/AArch64/TestInputs/foo.ll (+38) 
- (added) llvm/test/LTO/AArch64/TestInputs/old.ll (+46) 
- (modified) llvm/test/LTO/AArch64/link-branch-target-enforcement.ll (+4-1) 
- (added) llvm/test/LTO/AArch64/link-sign-return-address.ll (+102) 
- (modified) llvm/test/Linker/link-arm-and-thumb.ll (+3-3) 


``diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index cb153066b28dd1..1acc0510256268 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1173,22 +1173,29 @@ void CodeGenModule::Release() {
   "tag-stack-memory-buildattr", 1);
 
   if (T.isARM() || T.isThumb() || T.isAArch64()) {
+// Previously 1 is used and meant for the backed to derive the function
+// attribute form it. 2 now means function attributes already set for all
+// functions in this module, so no need to propagate those from the module
+// flag. Value is only used in case of LTO module merge because the backend
+// will see all required function attribute set already. Value is used
+// before modules got merged. Any posive value means the feature is active
+// and required binary markings need to be emit accordingly.
 if (LangOpts.BranchTargetEnforcement)
   getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
-1);
+2);
 if (LangOpts.BranchProtectionPAuthLR)
   getModule().addModuleFlag(llvm::Module::Min, 
"branch-protection-pauth-lr",
-1);
+2);
 if (LangOpts.GuardedControlStack)
-  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
 if (LangOpts.hasSignReturnAddress())
-  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 2);
 if (LangOpts.isSignReturnAddressScopeAll())
   getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
-1);
+2);
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
-"sign-return-address-with-bkey", 1);
+"sign-return-address-with-bkey", 2);
   }
 
   if (CodeGenOpts.StackClashProtector)
diff --git a/clang/test/CodeGen/aarch64-sign-return-address.c 
b/clang/test/CodeGen/aarch64-sign-return-address.c
index 8bc54b1a56c38c..35c56889e07071 100644
--- a/clang/test/CodeGen/aarch64-sign-return-address.c
+++ b/clang/test/CodeGen/aarch64-sign-return-address.c
@@ -22,17 +22,17 @@
 // NONE-NOT:  !"branch-target-enforcement"
 // ALL-NOT:   !"branch-target-enforcement"
 // PART-NOT:  !"branch-target-enforcement"
-// BTE:   !{i32 8, !"branch-target-enforcement", i32 1}
+// BTE:   !{i32 8, !"branch-target-enforcement", i32 2}
 // B-KEY-NOT: !"branch-target-enforcement"
 
 // NONE-NOT:  !"sign-return-address"
-// ALL:   !{i32 8, !"sign-return-address", i32 1}
-// PART:  !{i32 8, !"sign-return-address", i32 1}
+// ALL:   !{i32 8, !"sign-return-address", i32 2}
+// PART:  !{i32 8, !"sign-return-address", i32 2}
 // BTE-NOT:   !"sign-return-address"
-// B-KEY: !{i32 8, !"sign-return-address", i32 1}
+// B-KEY: !{i32 8, !"sign-return-address", i32 2}
 
 // NONE-NOT:  !"sign-return-address-all"
-// ALL:   !{i32 8, 

[clang] [lld] [llvm] [ARM][AArch64] BTI,GCS,PAC Module flag update. (PR #86212)

2024-03-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-ir

Author: Daniel Kiss (DanielKristofKiss)


Changes

Module flag is used to indicate the feature to be propagated to the function. 
As now the frontend emits all attributes accordingly let's help the auto 
upgrade to only do work when old and new bitcodes are merged.

Depends on #82819 and #86031

---

Patch is 26.38 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/86212.diff


20 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+13-6) 
- (modified) clang/test/CodeGen/aarch64-sign-return-address.c (+6-6) 
- (modified) clang/test/CodeGen/arm-branch-protection-attr-2.c (+4-4) 
- (modified) clang/test/Frontend/arm-ignore-branch-protection-option.c (+1-1) 
- (added) lld/test/ELF/lto/aarch64_inline.ll (+73) 
- (modified) llvm/include/llvm/IR/AutoUpgrade.h (+3) 
- (modified) llvm/lib/AsmParser/LLParser.cpp (+1) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+2) 
- (modified) llvm/lib/IR/AutoUpgrade.cpp (+100) 
- (modified) llvm/lib/Linker/IRMover.cpp (+10) 
- (modified) llvm/lib/Target/ARM/ARMAsmPrinter.cpp (+1-1) 
- (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-5.ll (+1-1) 
- (removed) llvm/test/LTO/AArch64/Inputs/foo.ll (-16) 
- (added) llvm/test/LTO/AArch64/TestInputs/bar.ll (+35) 
- (added) llvm/test/LTO/AArch64/TestInputs/foo.ll (+38) 
- (added) llvm/test/LTO/AArch64/TestInputs/old.ll (+46) 
- (modified) llvm/test/LTO/AArch64/link-branch-target-enforcement.ll (+4-1) 
- (added) llvm/test/LTO/AArch64/link-sign-return-address.ll (+102) 
- (modified) llvm/test/Linker/link-arm-and-thumb.ll (+3-3) 


``diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index cb153066b28dd1..1acc0510256268 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1173,22 +1173,29 @@ void CodeGenModule::Release() {
   "tag-stack-memory-buildattr", 1);
 
   if (T.isARM() || T.isThumb() || T.isAArch64()) {
+// Previously 1 is used and meant for the backed to derive the function
+// attribute form it. 2 now means function attributes already set for all
+// functions in this module, so no need to propagate those from the module
+// flag. Value is only used in case of LTO module merge because the backend
+// will see all required function attribute set already. Value is used
+// before modules got merged. Any posive value means the feature is active
+// and required binary markings need to be emit accordingly.
 if (LangOpts.BranchTargetEnforcement)
   getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
-1);
+2);
 if (LangOpts.BranchProtectionPAuthLR)
   getModule().addModuleFlag(llvm::Module::Min, 
"branch-protection-pauth-lr",
-1);
+2);
 if (LangOpts.GuardedControlStack)
-  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
 if (LangOpts.hasSignReturnAddress())
-  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 2);
 if (LangOpts.isSignReturnAddressScopeAll())
   getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
-1);
+2);
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
-"sign-return-address-with-bkey", 1);
+"sign-return-address-with-bkey", 2);
   }
 
   if (CodeGenOpts.StackClashProtector)
diff --git a/clang/test/CodeGen/aarch64-sign-return-address.c 
b/clang/test/CodeGen/aarch64-sign-return-address.c
index 8bc54b1a56c38c..35c56889e07071 100644
--- a/clang/test/CodeGen/aarch64-sign-return-address.c
+++ b/clang/test/CodeGen/aarch64-sign-return-address.c
@@ -22,17 +22,17 @@
 // NONE-NOT:  !"branch-target-enforcement"
 // ALL-NOT:   !"branch-target-enforcement"
 // PART-NOT:  !"branch-target-enforcement"
-// BTE:   !{i32 8, !"branch-target-enforcement", i32 1}
+// BTE:   !{i32 8, !"branch-target-enforcement", i32 2}
 // B-KEY-NOT: !"branch-target-enforcement"
 
 // NONE-NOT:  !"sign-return-address"
-// ALL:   !{i32 8, !"sign-return-address", i32 1}
-// PART:  !{i32 8, !"sign-return-address", i32 1}
+// ALL:   !{i32 8, !"sign-return-address", i32 2}
+// PART:  !{i32 8, !"sign-return-address", i32 2}
 // BTE-NOT:   !"sign-return-address"
-// B-KEY: !{i32 8, !"sign-return-address", i32 1}
+// B-KEY: !{i32 8, !"sign-return-address", i32 2}
 
 // NONE-NOT:  !"sign-return-address-all"
-// ALL:   !{i32 8, 

[clang] [lld] [llvm] [ARM][AArch64] BTI,GCS,PAC Module flag update. (PR #86212)

2024-03-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Daniel Kiss (DanielKristofKiss)


Changes

Module flag is used to indicate the feature to be propagated to the function. 
As now the frontend emits all attributes accordingly let's help the auto 
upgrade to only do work when old and new bitcodes are merged.

Depends on #82819 and #86031

---

Patch is 26.38 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/86212.diff


20 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+13-6) 
- (modified) clang/test/CodeGen/aarch64-sign-return-address.c (+6-6) 
- (modified) clang/test/CodeGen/arm-branch-protection-attr-2.c (+4-4) 
- (modified) clang/test/Frontend/arm-ignore-branch-protection-option.c (+1-1) 
- (added) lld/test/ELF/lto/aarch64_inline.ll (+73) 
- (modified) llvm/include/llvm/IR/AutoUpgrade.h (+3) 
- (modified) llvm/lib/AsmParser/LLParser.cpp (+1) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+2) 
- (modified) llvm/lib/IR/AutoUpgrade.cpp (+100) 
- (modified) llvm/lib/Linker/IRMover.cpp (+10) 
- (modified) llvm/lib/Target/ARM/ARMAsmPrinter.cpp (+1-1) 
- (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-5.ll (+1-1) 
- (removed) llvm/test/LTO/AArch64/Inputs/foo.ll (-16) 
- (added) llvm/test/LTO/AArch64/TestInputs/bar.ll (+35) 
- (added) llvm/test/LTO/AArch64/TestInputs/foo.ll (+38) 
- (added) llvm/test/LTO/AArch64/TestInputs/old.ll (+46) 
- (modified) llvm/test/LTO/AArch64/link-branch-target-enforcement.ll (+4-1) 
- (added) llvm/test/LTO/AArch64/link-sign-return-address.ll (+102) 
- (modified) llvm/test/Linker/link-arm-and-thumb.ll (+3-3) 


``diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index cb153066b28dd1..1acc0510256268 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1173,22 +1173,29 @@ void CodeGenModule::Release() {
   "tag-stack-memory-buildattr", 1);
 
   if (T.isARM() || T.isThumb() || T.isAArch64()) {
+// Previously 1 is used and meant for the backed to derive the function
+// attribute form it. 2 now means function attributes already set for all
+// functions in this module, so no need to propagate those from the module
+// flag. Value is only used in case of LTO module merge because the backend
+// will see all required function attribute set already. Value is used
+// before modules got merged. Any posive value means the feature is active
+// and required binary markings need to be emit accordingly.
 if (LangOpts.BranchTargetEnforcement)
   getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
-1);
+2);
 if (LangOpts.BranchProtectionPAuthLR)
   getModule().addModuleFlag(llvm::Module::Min, 
"branch-protection-pauth-lr",
-1);
+2);
 if (LangOpts.GuardedControlStack)
-  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
 if (LangOpts.hasSignReturnAddress())
-  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 2);
 if (LangOpts.isSignReturnAddressScopeAll())
   getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
-1);
+2);
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
-"sign-return-address-with-bkey", 1);
+"sign-return-address-with-bkey", 2);
   }
 
   if (CodeGenOpts.StackClashProtector)
diff --git a/clang/test/CodeGen/aarch64-sign-return-address.c 
b/clang/test/CodeGen/aarch64-sign-return-address.c
index 8bc54b1a56c38c..35c56889e07071 100644
--- a/clang/test/CodeGen/aarch64-sign-return-address.c
+++ b/clang/test/CodeGen/aarch64-sign-return-address.c
@@ -22,17 +22,17 @@
 // NONE-NOT:  !"branch-target-enforcement"
 // ALL-NOT:   !"branch-target-enforcement"
 // PART-NOT:  !"branch-target-enforcement"
-// BTE:   !{i32 8, !"branch-target-enforcement", i32 1}
+// BTE:   !{i32 8, !"branch-target-enforcement", i32 2}
 // B-KEY-NOT: !"branch-target-enforcement"
 
 // NONE-NOT:  !"sign-return-address"
-// ALL:   !{i32 8, !"sign-return-address", i32 1}
-// PART:  !{i32 8, !"sign-return-address", i32 1}
+// ALL:   !{i32 8, !"sign-return-address", i32 2}
+// PART:  !{i32 8, !"sign-return-address", i32 2}
 // BTE-NOT:   !"sign-return-address"
-// B-KEY: !{i32 8, !"sign-return-address", i32 1}
+// B-KEY: !{i32 8, !"sign-return-address", i32 2}
 
 // NONE-NOT:  !"sign-return-address-all"
-// ALL:   !{i32 8, 

[clang] [lld] [llvm] [ARM][AArch64] BTI,GCS,PAC Module flag update. (PR #86212)

2024-03-21 Thread Daniel Kiss via cfe-commits

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

Module flag is used to indicate the feature to be propagated to the function. 
As now the frontend emits all attributes accordingly let's help the auto 
upgrade to only do work when old and new bitcodes are merged.

Depends on #82819 and #86031

>From 33d8277d188f82847d914273be2379151dd33d41 Mon Sep 17 00:00:00 2001
From: Daniel Kiss 
Date: Fri, 8 Mar 2024 15:06:28 +0100
Subject: [PATCH] BTI,GCS,PAC Module flag update.

Module flag is used to indicate the feature to be propagated to the
function. As now the frontend emits all attributes accoringly let's
help the automerger to only do work when old and new bitcodes are
merged.
Autoupgrade function attributes from Module attributes when needed.
---
 clang/lib/CodeGen/CodeGenModule.cpp   |  19 ++--
 .../CodeGen/aarch64-sign-return-address.c |  12 +--
 .../CodeGen/arm-branch-protection-attr-2.c|   8 +-
 .../arm-ignore-branch-protection-option.c |   2 +-
 lld/test/ELF/lto/aarch64_inline.ll|  73 +
 llvm/include/llvm/IR/AutoUpgrade.h|   3 +
 llvm/lib/AsmParser/LLParser.cpp   |   1 +
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp |   2 +
 llvm/lib/IR/AutoUpgrade.cpp   | 100 +
 llvm/lib/Linker/IRMover.cpp   |  10 ++
 llvm/lib/Target/ARM/ARMAsmPrinter.cpp |   2 +-
 llvm/lib/Transforms/IPO/LowerTypeTests.cpp|   2 +-
 .../CodeGen/Thumb2/pacbti-m-outliner-5.ll |   2 +-
 llvm/test/LTO/AArch64/Inputs/foo.ll   |  16 ---
 llvm/test/LTO/AArch64/TestInputs/bar.ll   |  35 ++
 llvm/test/LTO/AArch64/TestInputs/foo.ll   |  38 +++
 llvm/test/LTO/AArch64/TestInputs/old.ll   |  46 
 .../AArch64/link-branch-target-enforcement.ll |   5 +-
 .../LTO/AArch64/link-sign-return-address.ll   | 102 ++
 llvm/test/Linker/link-arm-and-thumb.ll|   6 +-
 20 files changed, 444 insertions(+), 40 deletions(-)
 create mode 100644 lld/test/ELF/lto/aarch64_inline.ll
 delete mode 100644 llvm/test/LTO/AArch64/Inputs/foo.ll
 create mode 100644 llvm/test/LTO/AArch64/TestInputs/bar.ll
 create mode 100644 llvm/test/LTO/AArch64/TestInputs/foo.ll
 create mode 100644 llvm/test/LTO/AArch64/TestInputs/old.ll
 create mode 100644 llvm/test/LTO/AArch64/link-sign-return-address.ll

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index cb153066b28dd1..1acc0510256268 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1173,22 +1173,29 @@ void CodeGenModule::Release() {
   "tag-stack-memory-buildattr", 1);
 
   if (T.isARM() || T.isThumb() || T.isAArch64()) {
+// Previously 1 is used and meant for the backed to derive the function
+// attribute form it. 2 now means function attributes already set for all
+// functions in this module, so no need to propagate those from the module
+// flag. Value is only used in case of LTO module merge because the backend
+// will see all required function attribute set already. Value is used
+// before modules got merged. Any posive value means the feature is active
+// and required binary markings need to be emit accordingly.
 if (LangOpts.BranchTargetEnforcement)
   getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
-1);
+2);
 if (LangOpts.BranchProtectionPAuthLR)
   getModule().addModuleFlag(llvm::Module::Min, 
"branch-protection-pauth-lr",
-1);
+2);
 if (LangOpts.GuardedControlStack)
-  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
 if (LangOpts.hasSignReturnAddress())
-  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
+  getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 2);
 if (LangOpts.isSignReturnAddressScopeAll())
   getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
-1);
+2);
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
-"sign-return-address-with-bkey", 1);
+"sign-return-address-with-bkey", 2);
   }
 
   if (CodeGenOpts.StackClashProtector)
diff --git a/clang/test/CodeGen/aarch64-sign-return-address.c 
b/clang/test/CodeGen/aarch64-sign-return-address.c
index 8bc54b1a56c38c..35c56889e07071 100644
--- a/clang/test/CodeGen/aarch64-sign-return-address.c
+++ b/clang/test/CodeGen/aarch64-sign-return-address.c
@@ -22,17 +22,17 @@
 // NONE-NOT:  !"branch-target-enforcement"
 // ALL-NOT:   !"branch-target-enforcement"
 //