Skip to content

Commit 2ffaf35

Browse files
Daniel KissDanielKristofKiss
Daniel Kiss
authored andcommitted
[NFC][Clang] Move setfunctions of BranchProtectionInfo.
Move the to TargetCodeGenInfo. Refactor of llvm#98329
1 parent bf4167f commit 2ffaf35

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
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: 23 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,28 @@ llvm::Value *TargetCodeGenInfo::createEnqueuedBlockKernel(
206207
return F;
207208
}
208209

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

clang/lib/CodeGen/TargetInfo.h

Lines changed: 8 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,12 @@ class TargetCodeGenInfo {
413414
return nullptr;
414415
}
415416

417+
void setFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
418+
llvm::Function &F) const;
419+
420+
void setFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
421+
llvm::AttrBuilder &FuncAttrs) const;
422+
416423
protected:
417424
static std::string qualifyWindowsLibrary(StringRef Lib);
418425

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+
CGM.getTargetCodeGenInfo().setFnAttributes(BPI, *Fn);
137137
}
138138

139139
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,

clang/lib/CodeGen/Targets/ARM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
152152
diag::warn_target_unsupported_branch_protection_attribute)
153153
<< Arch;
154154
} else {
155-
BPI.setFnAttributes(*Fn);
155+
CGM.getTargetCodeGenInfo().setFnAttributes(BPI, (*Fn));
156156
}
157157
} else if (CGM.getLangOpts().BranchTargetEnforcement ||
158158
CGM.getLangOpts().hasSignReturnAddress()) {
@@ -168,7 +168,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
168168
} else if (CGM.getTarget().isBranchProtectionSupportedArch(
169169
CGM.getTarget().getTargetOpts().CPU)) {
170170
TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
171-
BPI.setFnAttributes(*Fn);
171+
CGM.getTargetCodeGenInfo().setFnAttributes(BPI, (*Fn));
172172
}
173173

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

0 commit comments

Comments
 (0)