[clang] [llvm] Revert "[Clang][ARM][AArch64] Alway emit protection attributes for functions." (PR #98284)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 10 01:23:06 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lto

@llvm/pr-subscribers-clang

Author: Daniel Kiss (DanielKristofKiss)

<details>
<summary>Changes</summary>

Reverts llvm/llvm-project#<!-- -->82819

---

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


60 Files Affected:

- (modified) clang/include/clang/Basic/TargetInfo.h (+3-41) 
- (modified) clang/lib/CodeGen/Targets/AArch64.cpp (+30-13) 
- (modified) clang/lib/CodeGen/Targets/ARM.cpp (+3-5) 
- (modified) clang/test/CodeGen/aarch64-branch-protection-attr.c (+13-13) 
- (modified) clang/test/CodeGen/aarch64-sign-return-address.c (+3-9) 
- (modified) clang/test/CodeGen/arm-branch-protection-attr-1.c (+6-6) 
- (modified) clang/test/CodeGen/arm-branch-protection-attr-2.c (+4-9) 
- (removed) clang/test/Frontend/arm-branch-protection-lto.c (-24) 
- (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (+11-1) 
- (modified) llvm/lib/IR/Verifier.cpp (+1-19) 
- (modified) llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp (+3-3) 
- (modified) llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp (+33-4) 
- (modified) llvm/lib/Target/ARM/ARMMachineFunctionInfo.cpp (+21-1) 
- (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+2-4) 
- (modified) llvm/test/CodeGen/AArch64/branch-target-enforcement-indirect-calls.ll (+9-9) 
- (modified) llvm/test/CodeGen/AArch64/bti-branch-relaxation.ll (+1-1) 
- (modified) llvm/test/CodeGen/AArch64/kcfi-bti.ll (+3-4) 
- (modified) llvm/test/CodeGen/AArch64/machine-outliner-2fixup-blr-terminator.mir (+1-1) 
- (modified) llvm/test/CodeGen/AArch64/machine-outliner-bti.mir (+1-1) 
- (modified) llvm/test/CodeGen/AArch64/machine-outliner-outline-bti.ll (+2-2) 
- (modified) llvm/test/CodeGen/AArch64/note-gnu-property-pac-bti-0.ll (+1-1) 
- (modified) llvm/test/CodeGen/AArch64/note-gnu-property-pac-bti-4.ll (+2-2) 
- (modified) llvm/test/CodeGen/AArch64/pacbti-llvm-generated-funcs-1.ll (+2-2) 
- (modified) llvm/test/CodeGen/AArch64/pacbti-llvm-generated-funcs-2.ll (+7-3) 
- (modified) llvm/test/CodeGen/AArch64/pacbti-module-attrs.ll (+6-6) 
- (modified) llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll (+5-5) 
- (modified) llvm/test/CodeGen/AArch64/setjmp-bti-outliner.ll (+9-6) 
- (modified) llvm/test/CodeGen/AArch64/setjmp-bti.ll (+4-2) 
- (modified) llvm/test/CodeGen/AArch64/sign-return-address-pauth-lr.ll (+19-17) 
- (modified) llvm/test/CodeGen/AArch64/sign-return-address.ll (+4-4) 
- (modified) llvm/test/CodeGen/AArch64/wineh-bti.ll (+6-1) 
- (modified) llvm/test/CodeGen/AArch64/wineh-pac.ll (+5-2) 
- (modified) llvm/test/CodeGen/ARM/setjmp-bti-basic.ll (+4-1) 
- (modified) llvm/test/CodeGen/ARM/setjmp-bti-outliner.ll (+5-2) 
- (modified) llvm/test/CodeGen/Thumb2/bti-entry-blocks.ll (+5-2) 
- (modified) llvm/test/CodeGen/Thumb2/bti-indirect-branches.ll (+6-3) 
- (modified) llvm/test/CodeGen/Thumb2/bti-outliner-1.ll (+5-4) 
- (modified) llvm/test/CodeGen/Thumb2/bti-outliner-2.ll (+8-4) 
- (modified) llvm/test/CodeGen/Thumb2/bti-outliner-cost-2.ll (+5-1) 
- (modified) llvm/test/CodeGen/Thumb2/bti-pac-replace-1.mir (+7-1) 
- (modified) llvm/test/CodeGen/Thumb2/bti-pac-replace-2.ll (+6-1) 
- (modified) llvm/test/CodeGen/Thumb2/jump-table-bti.ll (+5-5) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-basic.ll (+3-3) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-indirect-tail-call.ll (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-1.ll (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-3.ll (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-4.ll (+3-3) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-outliner-5.ll (+2-2) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-overalign.ll (+1-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-stack-arg.ll (+2-2) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-unsupported-arch.ll (+7-1) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-varargs-1.ll (+2-2) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-varargs-2.ll (+2-2) 
- (modified) llvm/test/CodeGen/Thumb2/pacbti-m-vla.ll (+1-1) 
- (modified) llvm/test/LTO/AArch64/link-branch-target-enforcement.ll (+1-1) 
- (modified) llvm/test/Transforms/Inline/inline-sign-return-address.ll (+7-6) 
- (modified) llvm/test/Transforms/LowerTypeTests/function-arm-thumb.ll (+1-1) 
- (modified) llvm/test/Transforms/LowerTypeTests/function-thumb-bti.ll (+2-2) 
- (modified) llvm/test/Transforms/LowerTypeTests/function.ll (+2-2) 
- (modified) llvm/test/Verifier/branch-prot-attrs.ll (+1-15) 


``````````diff
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 1f208b40f92cb..9b0ae2102e098 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -32,9 +32,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Frontend/OpenMP/OMPGridValues.h"
-#include "llvm/IR/Attributes.h"
 #include "llvm/IR/DerivedTypes.h"
-#include "llvm/IR/Function.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/VersionTuple.h"
@@ -1402,15 +1400,15 @@ class TargetInfo : public TransferrableTargetInfo,
     return true;
   }
 
-  class BranchProtectionInfo {
-  public:
+  struct BranchProtectionInfo {
     LangOptions::SignReturnAddressScopeKind SignReturnAddr;
     LangOptions::SignReturnAddressKeyKind SignKey;
     bool BranchTargetEnforcement;
     bool BranchProtectionPAuthLR;
     bool GuardedControlStack;
 
-  protected:
+    BranchProtectionInfo() = default;
+
     const char *getSignReturnAddrStr() const {
       switch (SignReturnAddr) {
       case LangOptions::SignReturnAddressScopeKind::None:
@@ -1432,42 +1430,6 @@ class TargetInfo : public TransferrableTargetInfo,
       }
       llvm_unreachable("Unexpected SignReturnAddressKeyKind");
     }
-
-  public:
-    BranchProtectionInfo() = default;
-    BranchProtectionInfo(const LangOptions &LangOpts) {
-      SignReturnAddr =
-          LangOpts.hasSignReturnAddress()
-              ? (LangOpts.isSignReturnAddressScopeAll()
-                     ? LangOptions::SignReturnAddressScopeKind::All
-                     : LangOptions::SignReturnAddressScopeKind::NonLeaf)
-              : LangOptions::SignReturnAddressScopeKind::None;
-      SignKey = LangOpts.isSignReturnAddressWithAKey()
-                    ? LangOptions::SignReturnAddressKeyKind::AKey
-                    : LangOptions::SignReturnAddressKeyKind::BKey;
-      BranchTargetEnforcement = LangOpts.BranchTargetEnforcement;
-      BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR;
-      GuardedControlStack = LangOpts.GuardedControlStack;
-    }
-
-    void setFnAttributes(llvm::Function &F) {
-      llvm::AttrBuilder FuncAttrs(F.getContext());
-      setFnAttributes(FuncAttrs);
-      F.addFnAttrs(FuncAttrs);
-    }
-
-    void setFnAttributes(llvm::AttrBuilder &FuncAttrs) {
-      if (SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
-        FuncAttrs.addAttribute("sign-return-address", getSignReturnAddrStr());
-        FuncAttrs.addAttribute("sign-return-address-key", getSignKeyStr());
-      }
-      if (BranchTargetEnforcement)
-        FuncAttrs.addAttribute("branch-target-enforcement");
-      if (BranchProtectionPAuthLR)
-        FuncAttrs.addAttribute("branch-protection-pauth-lr");
-      if (GuardedControlStack)
-        FuncAttrs.addAttribute("guarded-control-stack");
-    }
   };
 
   /// Determine if the Architecture in this TargetInfo supports branch
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index 3891f9fc8174b..8ebf3d9a51b50 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -120,20 +120,37 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
     if (!FD)
       return;
 
-    TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
-
-    if (const auto *TA = FD->getAttr<TargetAttr>()) {
-      ParsedTargetAttr Attr =
-          CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
-      if (!Attr.BranchProtection.empty()) {
-        StringRef Error;
-        (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
-                                                       Attr.CPU, BPI, Error);
-        assert(Error.empty());
-      }
-    }
+    const auto *TA = FD->getAttr<TargetAttr>();
+    if (TA == nullptr)
+      return;
+
+    ParsedTargetAttr Attr =
+        CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
+    if (Attr.BranchProtection.empty())
+      return;
+
+    TargetInfo::BranchProtectionInfo BPI;
+    StringRef Error;
+    (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
+                                                   Attr.CPU, BPI, Error);
+    assert(Error.empty());
+
     auto *Fn = cast<llvm::Function>(GV);
-    BPI.setFnAttributes(*Fn);
+    Fn->addFnAttr("sign-return-address", BPI.getSignReturnAddrStr());
+
+    if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
+      Fn->addFnAttr("sign-return-address-key",
+                    BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
+                        ? "a_key"
+                        : "b_key");
+    }
+
+    Fn->addFnAttr("branch-target-enforcement",
+                  BPI.BranchTargetEnforcement ? "true" : "false");
+    Fn->addFnAttr("branch-protection-pauth-lr",
+                  BPI.BranchProtectionPAuthLR ? "true" : "false");
+    Fn->addFnAttr("guarded-control-stack",
+                  BPI.GuardedControlStack ? "true" : "false");
   }
 
   bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
diff --git a/clang/lib/CodeGen/Targets/ARM.cpp b/clang/lib/CodeGen/Targets/ARM.cpp
index d449b97cdc685..f362e34f38dea 100644
--- a/clang/lib/CodeGen/Targets/ARM.cpp
+++ b/clang/lib/CodeGen/Targets/ARM.cpp
@@ -152,7 +152,9 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
               diag::warn_target_unsupported_branch_protection_attribute)
               << Arch;
         } else {
-          BPI.setFnAttributes(*Fn);
+          Fn->addFnAttr("sign-return-address", BPI.getSignReturnAddrStr());
+          Fn->addFnAttr("branch-target-enforcement",
+                        BPI.BranchTargetEnforcement ? "true" : "false");
         }
       } else if (CGM.getLangOpts().BranchTargetEnforcement ||
                  CGM.getLangOpts().hasSignReturnAddress()) {
@@ -165,10 +167,6 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
               diag::warn_target_unsupported_branch_protection_attribute)
               << Attr.CPU;
       }
-    } else if (CGM.getTarget().isBranchProtectionSupportedArch(
-                   CGM.getTarget().getTargetOpts().CPU)) {
-      TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
-      BPI.setFnAttributes(*Fn);
     }
 
     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
diff --git a/clang/test/CodeGen/aarch64-branch-protection-attr.c b/clang/test/CodeGen/aarch64-branch-protection-attr.c
index e7ae7fb1570c9..f0e1dcccd1e82 100644
--- a/clang/test/CodeGen/aarch64-branch-protection-attr.c
+++ b/clang/test/CodeGen/aarch64-branch-protection-attr.c
@@ -67,29 +67,29 @@ __attribute__ ((target("branch-protection=gcs")))
 void gcs() {}
 // CHECK: define{{.*}} void @gcs() #[[#GCS:]]
 
-// CHECK-DAG: attributes #[[#NONE]] = { {{.*}}
+// CHECK-DAG: attributes #[[#NONE]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}} "sign-return-address"="none"
 
-// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement" "guarded-control-stack" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement"="true" "guarded-control-stack"="true" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
 
-// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"
+// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"="true" "guarded-control-stack"="false" {{.*}} "sign-return-address"="none"
 
-// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
 
-// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "sign-return-address"="all" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
 
-// CHECK-DAG: attributes #[[#PACBKEY]] = { {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
+// CHECK-DAG: attributes #[[#PACBKEY]] = { {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
 
-// CHECK-DAG: attributes #[[#PACBKEYLEAF]] = { {{.*}} "sign-return-address"="all" "sign-return-address-key"="b_key"
+// CHECK-DAG: attributes #[[#PACBKEYLEAF]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="b_key"
 
-// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}} "branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}}"branch-target-enforcement"="true" "guarded-control-stack"="false" {{.*}} "sign-return-address"="all" "sign-return-address-key"="a_key"
 
 
-// CHECK-DAG: attributes #[[#PAUTHLR]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#PAUTHLR]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
 
-// CHECK-DAG: attributes #[[#PAUTHLR_BKEY]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
+// CHECK-DAG: attributes #[[#PAUTHLR_BKEY]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
 
-// CHECK-DAG: attributes #[[#PAUTHLR_LEAF]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#PAUTHLR_LEAF]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
 
-// CHECK-DAG: attributes #[[#PAUTHLR_BTI]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
+// CHECK-DAG: attributes #[[#PAUTHLR_BTI]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="true" "guarded-control-stack"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
 
-// CHECK-DAG: attributes #[[#GCS]] = { {{.*}} "guarded-control-stack"
+// CHECK-DAG: attributes #[[#GCS]] = { {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="true" {{.*}} "sign-return-address"="none"
diff --git a/clang/test/CodeGen/aarch64-sign-return-address.c b/clang/test/CodeGen/aarch64-sign-return-address.c
index 11dd683a3d7e9..8bc54b1a56c38 100644
--- a/clang/test/CodeGen/aarch64-sign-return-address.c
+++ b/clang/test/CodeGen/aarch64-sign-return-address.c
@@ -13,15 +13,9 @@
 
 // CHECK-LABEL: @foo() #[[#ATTR:]]
 
-// NONE-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
-// NONE-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
-// NONE-NOT:  attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
-
-// ALL:   attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
-// PART:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"="a_key"
-// B-KEY: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"="b_key"
-// BTE:   attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
-
+// CHECK-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
+// CHECK-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
+// CHECK-NOT:  attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
 
 // Check module attributes
 
diff --git a/clang/test/CodeGen/arm-branch-protection-attr-1.c b/clang/test/CodeGen/arm-branch-protection-attr-1.c
index dd38cf347f04f..2a9cc600742f3 100644
--- a/clang/test/CodeGen/arm-branch-protection-attr-1.c
+++ b/clang/test/CodeGen/arm-branch-protection-attr-1.c
@@ -29,14 +29,14 @@ __attribute__((target("branch-protection=pac-ret+leaf"))) void leaf() {}
 __attribute__((target("branch-protection=pac-ret+leaf+bti"))) void btileaf() {}
 // CHECK: define{{.*}} void @btileaf() #[[#BTIPACLEAF:]]
 
-// CHECK-DAG: attributes #[[#NONE]] = { {{.*}}
+// CHECK-DAG: attributes #[[#NONE]] = { {{.*}} "branch-target-enforcement"="false" {{.*}} "sign-return-address"="none"
 
-// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement" {{.*}} "sign-return-address"="non-leaf"
+// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement"="true" {{.*}} "sign-return-address"="non-leaf"
 
-// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"
+// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"="true" {{.*}} "sign-return-address"="none"
 
-// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "sign-return-address"="non-leaf"
+// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "branch-target-enforcement"="false" {{.*}} "sign-return-address"="non-leaf"
 
-// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "sign-return-address"="all"
+// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "branch-target-enforcement"="false" {{.*}}"sign-return-address"="all"
 
-// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}} "branch-target-enforcement" {{.*}} "sign-return-address"="all"
+// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}}"branch-target-enforcement"="true" {{.*}} "sign-return-address"="all"
diff --git a/clang/test/CodeGen/arm-branch-protection-attr-2.c b/clang/test/CodeGen/arm-branch-protection-attr-2.c
index fad5dc0707fb9..1f3c00873043e 100644
--- a/clang/test/CodeGen/arm-branch-protection-attr-2.c
+++ b/clang/test/CodeGen/arm-branch-protection-attr-2.c
@@ -5,18 +5,13 @@
 // RUN: %clang -target arm-arm-none-eabi -march=armv8.1-m.main -S -emit-llvm -o - -mbranch-protection=pac-ret+b-key %s | FileCheck %s --check-prefix=CHECK --check-prefix=PART
 // RUN: %clang -target arm-arm-none-eabi -march=armv8.1-m.main -S -emit-llvm -o - -mbranch-protection=bti %s           | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
 
-// Check there are branch protection function attributes
+// Check there are no branch protection function attributes
 
 // CHECK-LABEL: @foo() #[[#ATTR:]]
 
-// NONE-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
-// NONE-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
-// NONE-NOT:  attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
-
-// ALL:   attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="all"
-// PART:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="non-leaf"
-// BTE:   attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
-
+// CHECK-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
+// CHECK-NOT:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
+// CHECK-NOT:  attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
 
 // Check module attributes
 
diff --git a/clang/test/Frontend/arm-branch-protection-lto.c b/clang/test/Frontend/arm-branch-protection-lto.c
deleted file mode 100644
index 6309a040d70dc..0000000000000
--- a/clang/test/Frontend/arm-branch-protection-lto.c
+++ /dev/null
@@ -1,24 +0,0 @@
-// REQUIRES: arm-registered-target
-
-// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=SIGN
-// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
-// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce -msign-return-address=all %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALL
-
-// RUN: %clang_cc1 -flto -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=SIGN
-// RUN: %clang_cc1 -flto -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
-// RUN: %clang_cc1 -flto -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce -msign-return-address=all %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALL
-
-// RUN: %clang_cc1 -flto=thin -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=SIGN
-// RUN: %clang_cc1 -flto=thin -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce  %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
-// RUN: %clang_cc1 -flto=thin -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce -msign-return-address=all %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALL
-
-void foo() {}
-
-// Check there are branch protection function attributes.
-// CHECK-LABEL: @foo() #[[#ATTR:]]
-
-// SIGN-NOT: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
-// SIGN: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="non-leaf"
-// BTE:  attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
-// BTE:  attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
-// ALL:  attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"{{.*}} "sign-return-address"="all"
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 276d980c1dcca..33c96ebdba091 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -12012,7 +12012,17 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
         // table branch.
         if (FallthroughUnreachable) {
           Function &CurFunc = CurMF->getFunction();
-          if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
+          bool HasBranchTargetEnforcement = false;
+          if (CurFunc.hasFnAttribute("branch-target-enforcement")) {
+            HasBranchTargetEnforcement =
+                CurFunc.getFnAttribute("branch-target-enforcement")
+                    .getValueAsBool();
+          } else {
+            HasBranchTargetEnforcement =
+                CurMF->getMMI().getModule()->getModuleFlag(
+                    "branch-target-enforcement");
+          }
+          if (!HasBranchTargetEnforcement)
             JTH->FallthroughUnreachable = true;
         }
 
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 3b6d76e76e9c7..d156eaec4c172 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2348,33 +2348,15 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
     if (S != "a_key" && S != "b_key")
       CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
                   V);
-    if (auto AA = Attrs.getFnAttr("sign-return-address"); !AA.isValid()) {
-      CheckFailed(
-          "'sign-return-address-key' present without `sign-return-address`");
-    }
   }
 
   if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
     StringRef S = A.getValueAsString();
-    if (S != "" && S != "true" && S != "false")
+    if (S != "true" && S != "false")
       CheckFailed(
           "invalid value for 'branch-target-enforcement' attribute: " + S, V);
   }
 
-  if (auto A = Attrs.getFnAttr("branch-protection-pauth-lr"); A.isValid()) {
-    StringRef S = A.getValueAsString();
-    if (S != "" && S != "true" && S != "false")
-      CheckFailed(
-          "invalid value for 'branch-protection-pauth-lr' attribute: " + S, V);
-  }
-
-  if (auto A = Attrs.getFnAttr("guarded-control-stack"); A.isValid()) {
-    StringRef S = A.getValueAsString();
-    if (S != "" && S != "true" && S != "false")
-      CheckFailed("invalid value for 'guarded-control-stack' attribute: " + S,
-                  V);
-  }
-
   if (auto A = Attrs.getFnAttr("vector-function-abi-variant"); A.isValid()) {
     StringRef S = A.getValueAsS...
[truncated]

``````````

</details>


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


More information about the cfe-commits mailing list