Skip to content

Commit f34a165

Browse files
[NFC][Clang] Move set functions out BranchProtectionInfo. (#98451)
To reduce build times move them to TargetCodeGenInfo. Refactor of #98329
1 parent ca715de commit f34a165

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
#include "llvm/ADT/StringRef.h"
3333
#include "llvm/ADT/StringSet.h"
3434
#include "llvm/Frontend/OpenMP/OMPGridValues.h"
35-
#include "llvm/IR/Attributes.h"
3635
#include "llvm/IR/DerivedTypes.h"
37-
#include "llvm/IR/Function.h"
3836
#include "llvm/Support/DataTypes.h"
3937
#include "llvm/Support/Error.h"
4038
#include "llvm/Support/VersionTuple.h"
@@ -1410,7 +1408,6 @@ class TargetInfo : public TransferrableTargetInfo,
14101408
bool BranchProtectionPAuthLR;
14111409
bool GuardedControlStack;
14121410

1413-
protected:
14141411
const char *getSignReturnAddrStr() const {
14151412
switch (SignReturnAddr) {
14161413
case LangOptions::SignReturnAddressScopeKind::None:
@@ -1433,7 +1430,6 @@ class TargetInfo : public TransferrableTargetInfo,
14331430
llvm_unreachable("Unexpected SignReturnAddressKeyKind");
14341431
}
14351432

1436-
public:
14371433
BranchProtectionInfo()
14381434
: SignReturnAddr(LangOptions::SignReturnAddressScopeKind::None),
14391435
SignKey(LangOptions::SignReturnAddressKeyKind::AKey),
@@ -1454,25 +1450,6 @@ class TargetInfo : public TransferrableTargetInfo,
14541450
BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR;
14551451
GuardedControlStack = LangOpts.GuardedControlStack;
14561452
}
1457-
1458-
void setFnAttributes(llvm::Function &F) {
1459-
llvm::AttrBuilder FuncAttrs(F.getContext());
1460-
setFnAttributes(FuncAttrs);
1461-
F.addFnAttrs(FuncAttrs);
1462-
}
1463-
1464-
void setFnAttributes(llvm::AttrBuilder &FuncAttrs) {
1465-
if (SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
1466-
FuncAttrs.addAttribute("sign-return-address", getSignReturnAddrStr());
1467-
FuncAttrs.addAttribute("sign-return-address-key", getSignKeyStr());
1468-
}
1469-
if (BranchTargetEnforcement)
1470-
FuncAttrs.addAttribute("branch-target-enforcement");
1471-
if (BranchProtectionPAuthLR)
1472-
FuncAttrs.addAttribute("branch-protection-pauth-lr");
1473-
if (GuardedControlStack)
1474-
FuncAttrs.addAttribute("guarded-control-stack");
1475-
}
14761453
};
14771454

14781455
/// Determine if the Architecture in this TargetInfo supports branch

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/CodeGen/CGFunctionInfo.h"
2020
#include "llvm/ADT/StringExtras.h"
2121
#include "llvm/ADT/Twine.h"
22+
#include "llvm/IR/Function.h"
2223
#include "llvm/IR/Type.h"
2324
#include "llvm/Support/raw_ostream.h"
2425

@@ -206,6 +207,27 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
206207
return F;
207208
}
208209

210+
void TargetCodeGenInfo::setBranchProtectionFnAttributes(
211+
const TargetInfo::BranchProtectionInfo &BPI, llvm::Function &F) {
212+
llvm::AttrBuilder FuncAttrs(F.getContext());
213+
setBranchProtectionFnAttributes(BPI, FuncAttrs);
214+
F.addFnAttrs(FuncAttrs);
215+
}
216+
217+
void TargetCodeGenInfo::setBranchProtectionFnAttributes(
218+
const TargetInfo::BranchProtectionInfo &BPI, llvm::AttrBuilder &FuncAttrs) {
219+
if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
220+
FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
221+
FuncAttrs.addAttribute("sign-return-address-key", BPI.getSignKeyStr());
222+
}
223+
if (BPI.BranchTargetEnforcement)
224+
FuncAttrs.addAttribute("branch-target-enforcement");
225+
if (BPI.BranchProtectionPAuthLR)
226+
FuncAttrs.addAttribute("branch-protection-pauth-lr");
227+
if (BPI.GuardedControlStack)
228+
FuncAttrs.addAttribute("guarded-control-stack");
229+
}
230+
209231
namespace {
210232
class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
211233
public:

clang/lib/CodeGen/TargetInfo.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
#define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
1616

1717
#include "CGBuilder.h"
18-
#include "CodeGenModule.h"
1918
#include "CGValue.h"
19+
#include "CodeGenModule.h"
2020
#include "clang/AST/Type.h"
2121
#include "clang/Basic/LLVM.h"
2222
#include "clang/Basic/SyncScope.h"
23+
#include "clang/Basic/TargetInfo.h"
2324
#include "llvm/ADT/SmallString.h"
2425
#include "llvm/ADT/StringRef.h"
2526

@@ -413,6 +414,14 @@ class TargetCodeGenInfo {
413414
return nullptr;
414415
}
415416

417+
static void
418+
setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
419+
llvm::Function &F);
420+
421+
static void
422+
setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
423+
llvm::AttrBuilder &FuncAttrs);
424+
416425
protected:
417426
static std::string qualifyWindowsLibrary(StringRef Lib);
418427

clang/lib/CodeGen/Targets/AArch64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
133133
}
134134
}
135135
auto *Fn = cast<llvm::Function>(GV);
136-
BPI.setFnAttributes(*Fn);
136+
setBranchProtectionFnAttributes(BPI, *Fn);
137137
}
138138

139139
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,

clang/lib/CodeGen/Targets/ARM.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,8 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
151151
D->getLocation(),
152152
diag::warn_target_unsupported_branch_protection_attribute)
153153
<< Arch;
154-
} else {
155-
BPI.setFnAttributes(*Fn);
156-
}
154+
} else
155+
setBranchProtectionFnAttributes(BPI, (*Fn));
157156
} else if (CGM.getLangOpts().BranchTargetEnforcement ||
158157
CGM.getLangOpts().hasSignReturnAddress()) {
159158
// If the Branch Protection attribute is missing, validate the target
@@ -168,7 +167,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
168167
} else if (CGM.getTarget().isBranchProtectionSupportedArch(
169168
CGM.getTarget().getTargetOpts().CPU)) {
170169
TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
171-
BPI.setFnAttributes(*Fn);
170+
setBranchProtectionFnAttributes(BPI, (*Fn));
172171
}
173172

174173
const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();

0 commit comments

Comments
 (0)