Skip to content

Commit 36556aa

Browse files
committed
Set default target features for autogenerated functions from frontend or from the Module level TargetABI
1 parent aabddc9 commit 36556aa

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

llvm/lib/Transforms/IPO/CrossDSOCFI.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ ConstantInt *CrossDSOCFI::extractNumericTypeId(MDNode *MD) {
5757
return C;
5858
}
5959

60+
void setDefaultSubtargetFeatures(const Triple &Triple,
61+
const StringRef TargetABI, Function *F) {
62+
if (Triple.isRISCV64()) {
63+
if (TargetABI.contains("lp64d"))
64+
F->addFnAttr("target-features", "+d");
65+
else if (TargetABI.contains("lp64f"))
66+
F->addFnAttr("target-features", "+f");
67+
else if (TargetABI.contains("lp64q"))
68+
F->addFnAttr("target-features", "+q");
69+
} else if (Triple.isRISCV32() && TargetABI.contains("ilp32f")) {
70+
F->addFnAttr("target-features", "+f");
71+
}
72+
}
73+
74+
StringRef getTargetABIFromMD(const Module &M) {
75+
StringRef TargetABI = "";
76+
if (auto *TargetABIMD =
77+
dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi")))
78+
TargetABI = TargetABIMD->getString();
79+
return TargetABI;
80+
}
81+
6082
/// buildCFICheck - emits __cfi_check for the current module.
6183
void CrossDSOCFI::buildCFICheck(Module &M) {
6284
// FIXME: verify that __cfi_check ends up near the end of the code section,
@@ -96,6 +118,14 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
96118
if (T.isARM() || T.isThumb())
97119
F->addFnAttr("target-features", "+thumb-mode");
98120

121+
StringRef DefaultTargetFeatures = Ctx.getDefaultTargetFeatures();
122+
if (DefaultTargetFeatures.empty()) {
123+
auto TargetABI = getTargetABIFromMD(M);
124+
setDefaultSubtargetFeatures(T, TargetABI, F);
125+
} else {
126+
F->addFnAttr("target-features", DefaultTargetFeatures);
127+
}
128+
99129
auto args = F->arg_begin();
100130
Value &CallSiteTypeId = *(args++);
101131
CallSiteTypeId.setName("CallSiteTypeId");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt -S -passes=cross-dso-cfi < %s | FileCheck --check-prefix=RISCV64 %s
2+
3+
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
4+
target triple = "riscv64-unknown-linux-gnu"
5+
6+
define signext i8 @f() !type !0 !type !1 {
7+
entry:
8+
ret i8 1
9+
}
10+
11+
!llvm.module.flags = !{!2, !3}
12+
13+
!0 = !{i64 0, !"_ZTSFcvE"}
14+
!1 = !{i64 0, i64 111}
15+
!2 = !{i32 4, !"Cross-DSO CFI", i32 1}
16+
!3 = !{i32 1, !"target-abi", !"lp64d"}
17+
18+
; RISCV64: define void @__cfi_check({{.*}} #[[A:.*]] align 4096
19+
; RISCV64: attributes #[[A]] = { {{.*}}"target-features"="+d"

0 commit comments

Comments
 (0)