[clang] [NFC][Clang] Move set functions out BranchProtectionInfo. (PR #98451)

Daniel Kiss via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 11 12:10:14 PDT 2024


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

>From 2ffaf35f09be03e7374bde3d97ee798b01e7e3d1 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <dankis01 at e133702.arm.com>
Date: Thu, 11 Jul 2024 10:29:24 +0200
Subject: [PATCH 1/5] [NFC][Clang] Move setfunctions of BranchProtectionInfo.

Move the to TargetCodeGenInfo.
Refactor of #98329
---
 clang/include/clang/Basic/TargetInfo.h | 23 -----------------------
 clang/lib/CodeGen/TargetInfo.cpp       | 23 +++++++++++++++++++++++
 clang/lib/CodeGen/TargetInfo.h         |  9 ++++++++-
 clang/lib/CodeGen/Targets/AArch64.cpp  |  2 +-
 clang/lib/CodeGen/Targets/ARM.cpp      |  4 ++--
 5 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index cf7628553647c..a58fb5f979272 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"
@@ -1410,7 +1408,6 @@ class TargetInfo : public TransferrableTargetInfo,
     bool BranchProtectionPAuthLR;
     bool GuardedControlStack;
 
-  protected:
     const char *getSignReturnAddrStr() const {
       switch (SignReturnAddr) {
       case LangOptions::SignReturnAddressScopeKind::None:
@@ -1433,7 +1430,6 @@ class TargetInfo : public TransferrableTargetInfo,
       llvm_unreachable("Unexpected SignReturnAddressKeyKind");
     }
 
-  public:
     BranchProtectionInfo()
         : SignReturnAddr(LangOptions::SignReturnAddressScopeKind::None),
           SignKey(LangOptions::SignReturnAddressKeyKind::AKey),
@@ -1454,25 +1450,6 @@ class TargetInfo : public TransferrableTargetInfo,
       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/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index 60224d458f6a2..9ccf0dea9a738 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -19,6 +19,7 @@
 #include "clang/CodeGen/CGFunctionInfo.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/Twine.h"
+#include "llvm/IR/Function.h"
 #include "llvm/IR/Type.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -206,6 +207,28 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
   return F;
 }
 
+void TargetCodeGenInfo::setFnAttributes(
+    const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) const {
+  llvm::AttrBuilder FuncAttrs(F.getContext());
+  setFnAttributes(BPI, FuncAttrs);
+  F.addFnAttrs(FuncAttrs);
+}
+
+void TargetCodeGenInfo::setFnAttributes(
+    const TargetInfo::BranchProtectionInfo &BPI,
+    llvm::AttrBuilder &FuncAttrs) const {
+  if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
+    FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
+    FuncAttrs.addAttribute("sign-return-address-key", BPI.getSignKeyStr());
+  }
+  if (BPI.BranchTargetEnforcement)
+    FuncAttrs.addAttribute("branch-target-enforcement");
+  if (BPI.BranchProtectionPAuthLR)
+    FuncAttrs.addAttribute("branch-protection-pauth-lr");
+  if (BPI.GuardedControlStack)
+    FuncAttrs.addAttribute("guarded-control-stack");
+}
+
 namespace {
 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
 public:
diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h
index f242d9e36ed40..78c2f94508f8e 100644
--- a/clang/lib/CodeGen/TargetInfo.h
+++ b/clang/lib/CodeGen/TargetInfo.h
@@ -15,11 +15,12 @@
 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
 
 #include "CGBuilder.h"
-#include "CodeGenModule.h"
 #include "CGValue.h"
+#include "CodeGenModule.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SyncScope.h"
+#include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 
@@ -413,6 +414,12 @@ class TargetCodeGenInfo {
     return nullptr;
   }
 
+  void setFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
+                       llvm::Function &F) const;
+
+  void setFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
+                       llvm::AttrBuilder &FuncAttrs) const;
+
 protected:
   static std::string qualifyWindowsLibrary(StringRef Lib);
 
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index 3891f9fc8174b..d4b3c87705f08 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -133,7 +133,7 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
       }
     }
     auto *Fn = cast<llvm::Function>(GV);
-    BPI.setFnAttributes(*Fn);
+    CGM.getTargetCodeGenInfo().setFnAttributes(BPI, *Fn);
   }
 
   bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
diff --git a/clang/lib/CodeGen/Targets/ARM.cpp b/clang/lib/CodeGen/Targets/ARM.cpp
index 93fea94a77248..b2d4248b710c9 100644
--- a/clang/lib/CodeGen/Targets/ARM.cpp
+++ b/clang/lib/CodeGen/Targets/ARM.cpp
@@ -152,7 +152,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
               diag::warn_target_unsupported_branch_protection_attribute)
               << Arch;
         } else {
-          BPI.setFnAttributes(*Fn);
+          CGM.getTargetCodeGenInfo().setFnAttributes(BPI, (*Fn));
         }
       } else if (CGM.getLangOpts().BranchTargetEnforcement ||
                  CGM.getLangOpts().hasSignReturnAddress()) {
@@ -168,7 +168,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
     } else if (CGM.getTarget().isBranchProtectionSupportedArch(
                    CGM.getTarget().getTargetOpts().CPU)) {
       TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
-      BPI.setFnAttributes(*Fn);
+      CGM.getTargetCodeGenInfo().setFnAttributes(BPI, (*Fn));
     }
 
     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();

>From 419c8eeadb39ee3d45334023b44975273bda4dce Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Thu, 11 Jul 2024 17:30:08 +0200
Subject: [PATCH 2/5] address review comments

---
 clang/lib/CodeGen/TargetInfo.cpp      |  6 +++---
 clang/lib/CodeGen/TargetInfo.h        | 10 ++++++----
 clang/lib/CodeGen/Targets/AArch64.cpp |  2 +-
 clang/lib/CodeGen/Targets/ARM.cpp     |  4 ++--
 4 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index 9ccf0dea9a738..dbf6661847587 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -207,14 +207,14 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
   return F;
 }
 
-void TargetCodeGenInfo::setFnAttributes(
+void TargetCodeGenInfo::setBranchProtectionFnAttributes(
     const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) const {
   llvm::AttrBuilder FuncAttrs(F.getContext());
-  setFnAttributes(BPI, FuncAttrs);
+  setBranchProtectionFnAttributes(BPI, FuncAttrs);
   F.addFnAttrs(FuncAttrs);
 }
 
-void TargetCodeGenInfo::setFnAttributes(
+void TargetCodeGenInfo::setBranchProtectionFnAttributes(
     const TargetInfo::BranchProtectionInfo &BPI,
     llvm::AttrBuilder &FuncAttrs) const {
   if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h
index 78c2f94508f8e..f01582c7ac3d2 100644
--- a/clang/lib/CodeGen/TargetInfo.h
+++ b/clang/lib/CodeGen/TargetInfo.h
@@ -414,11 +414,13 @@ class TargetCodeGenInfo {
     return nullptr;
   }
 
-  void setFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
-                       llvm::Function &F) const;
+  void
+  setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
+                                  llvm::Function &F) const;
 
-  void setFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
-                       llvm::AttrBuilder &FuncAttrs) const;
+  void
+  setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
+                                  llvm::AttrBuilder &FuncAttrs) const;
 
 protected:
   static std::string qualifyWindowsLibrary(StringRef Lib);
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index d4b3c87705f08..b9df54b0c67c4 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -133,7 +133,7 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
       }
     }
     auto *Fn = cast<llvm::Function>(GV);
-    CGM.getTargetCodeGenInfo().setFnAttributes(BPI, *Fn);
+    setBranchProtectionFnAttributes(BPI, *Fn);
   }
 
   bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
diff --git a/clang/lib/CodeGen/Targets/ARM.cpp b/clang/lib/CodeGen/Targets/ARM.cpp
index b2d4248b710c9..633ad3d20e45b 100644
--- a/clang/lib/CodeGen/Targets/ARM.cpp
+++ b/clang/lib/CodeGen/Targets/ARM.cpp
@@ -152,7 +152,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
               diag::warn_target_unsupported_branch_protection_attribute)
               << Arch;
         } else {
-          CGM.getTargetCodeGenInfo().setFnAttributes(BPI, (*Fn));
+          setBranchProtectionFnAttributes(BPI, (*Fn));
         }
       } else if (CGM.getLangOpts().BranchTargetEnforcement ||
                  CGM.getLangOpts().hasSignReturnAddress()) {
@@ -168,7 +168,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
     } else if (CGM.getTarget().isBranchProtectionSupportedArch(
                    CGM.getTarget().getTargetOpts().CPU)) {
       TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
-      CGM.getTargetCodeGenInfo().setFnAttributes(BPI, (*Fn));
+      setBranchProtectionFnAttributes(BPI, (*Fn));
     }
 
     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();

>From db7706a84e25857fb37f5896653f3e453c9db842 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Thu, 11 Jul 2024 20:18:56 +0200
Subject: [PATCH 3/5] drop parens

---
 clang/lib/CodeGen/Targets/ARM.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/ARM.cpp b/clang/lib/CodeGen/Targets/ARM.cpp
index 633ad3d20e45b..d032b88d7683c 100644
--- a/clang/lib/CodeGen/Targets/ARM.cpp
+++ b/clang/lib/CodeGen/Targets/ARM.cpp
@@ -151,9 +151,8 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
               D->getLocation(),
               diag::warn_target_unsupported_branch_protection_attribute)
               << Arch;
-        } else {
+        } else
           setBranchProtectionFnAttributes(BPI, (*Fn));
-        }
       } else if (CGM.getLangOpts().BranchTargetEnforcement ||
                  CGM.getLangOpts().hasSignReturnAddress()) {
         // If the Branch Protection attribute is missing, validate the target

>From 6c3f68439fd102070f183a77b7d58d49f399b43c Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Thu, 11 Jul 2024 21:05:31 +0200
Subject: [PATCH 4/5] change to static

---
 clang/lib/CodeGen/TargetInfo.cpp | 4 ++--
 clang/lib/CodeGen/TargetInfo.h   | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index dbf6661847587..a9e4ff2a82249 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -208,7 +208,7 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
 }
 
 void TargetCodeGenInfo::setBranchProtectionFnAttributes(
-    const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) const {
+    const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) {
   llvm::AttrBuilder FuncAttrs(F.getContext());
   setBranchProtectionFnAttributes(BPI, FuncAttrs);
   F.addFnAttrs(FuncAttrs);
@@ -216,7 +216,7 @@ void TargetCodeGenInfo::setBranchProtectionFnAttributes(
 
 void TargetCodeGenInfo::setBranchProtectionFnAttributes(
     const TargetInfo::BranchProtectionInfo &BPI,
-    llvm::AttrBuilder &FuncAttrs) const {
+    llvm::AttrBuilder &FuncAttrs) {
   if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
     FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
     FuncAttrs.addAttribute("sign-return-address-key", BPI.getSignKeyStr());
diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h
index f01582c7ac3d2..0925609cc74aa 100644
--- a/clang/lib/CodeGen/TargetInfo.h
+++ b/clang/lib/CodeGen/TargetInfo.h
@@ -414,13 +414,13 @@ class TargetCodeGenInfo {
     return nullptr;
   }
 
-  void
+  static void
   setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
-                                  llvm::Function &F) const;
+                                  llvm::Function &F);
 
-  void
+  static void
   setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
-                                  llvm::AttrBuilder &FuncAttrs) const;
+                                  llvm::AttrBuilder &FuncAttrs);
 
 protected:
   static std::string qualifyWindowsLibrary(StringRef Lib);

>From 4512d91ce2f49e535a3c55d933028970b35b517e Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Thu, 11 Jul 2024 21:09:52 +0200
Subject: [PATCH 5/5] clang-format

---
 clang/lib/CodeGen/TargetInfo.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index a9e4ff2a82249..38faa50cf19cf 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -215,8 +215,7 @@ void TargetCodeGenInfo::setBranchProtectionFnAttributes(
 }
 
 void TargetCodeGenInfo::setBranchProtectionFnAttributes(
-    const TargetInfo::BranchProtectionInfo &BPI,
-    llvm::AttrBuilder &FuncAttrs) {
+    const TargetInfo::BranchProtectionInfo &BPI, llvm::AttrBuilder &FuncAttrs) {
   if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
     FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
     FuncAttrs.addAttribute("sign-return-address-key", BPI.getSignKeyStr());



More information about the cfe-commits mailing list