Skip to content

Commit 9c1029e

Browse files
committed
Use TargetABI to assign default-target features in getDefaultSubtargetFeatures
It is currently not possible to provide any reasonable target-features for compiler generated functions (See: #69780) Having a target-abi will provide a way to add minimal requirements for target-features like `+d` for RISC-V.
1 parent 61ea63b commit 9c1029e

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
190190
static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
191191
const Twine &N, Module &M);
192192

193+
StringRef getDefaultTargetFeatures(const StringRef TargetABI);
194+
193195
/// Creates a function with some attributes recorded in llvm.module.flags
194196
/// and the LLVMContext applied.
195197
///

llvm/include/llvm/IR/Module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,9 @@ class LLVM_ABI Module {
10651065

10661066
/// Set the target variant version build SDK version metadata.
10671067
void setDarwinTargetVariantSDKVersion(VersionTuple Version);
1068+
1069+
/// Returns target-abi from MDString, null if target-abi is absent.
1070+
StringRef getTargetABIFromMD();
10681071
};
10691072

10701073
/// Given "llvm.used" or "llvm.compiler.used" as a global name, collect the

llvm/lib/IR/Function.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Support/Compiler.h"
4949
#include "llvm/Support/ErrorHandling.h"
5050
#include "llvm/Support/ModRef.h"
51+
#include "llvm/TargetParser/Triple.h"
5152
#include <cassert>
5253
#include <cstddef>
5354
#include <cstdint>
@@ -387,6 +388,25 @@ Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
387388
return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
388389
}
389390

391+
StringRef Function::getDefaultTargetFeatures(const StringRef TargetABI) {
392+
Triple T(getParent()->getTargetTriple());
393+
StringRef attr = "";
394+
if (T.isRISCV64()) {
395+
if (TargetABI.equals_insensitive("lp64d"))
396+
attr = "+d";
397+
else if (TargetABI.equals_insensitive("lp64f"))
398+
attr = "+f";
399+
else if (TargetABI.equals_insensitive("lp64q"))
400+
attr = "+q";
401+
} else if (T.isRISCV32() && TargetABI.contains("ilp32f")) {
402+
attr = "+f";
403+
} else if (T.isARM() || T.isThumb()) {
404+
attr = "+thumb-mode";
405+
}
406+
407+
return attr;
408+
}
409+
390410
Function *Function::createWithDefaultAttr(FunctionType *Ty,
391411
LinkageTypes Linkage,
392412
unsigned AddrSpace, const Twine &N,

llvm/lib/IR/Module.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,3 +915,11 @@ VersionTuple Module::getDarwinTargetVariantSDKVersion() const {
915915
void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) {
916916
addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version");
917917
}
918+
919+
StringRef Module::getTargetABIFromMD() {
920+
StringRef TargetABI = "";
921+
if (auto *TargetABIMD =
922+
dyn_cast_or_null<MDString>(getModuleFlag("target-abi")))
923+
TargetABI = TargetABIMD->getString();
924+
return TargetABI;
925+
}

llvm/lib/Transforms/IPO/CrossDSOCFI.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,20 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
8686
"__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
8787
PointerType::getUnqual(Ctx), PointerType::getUnqual(Ctx));
8888
Function *F = cast<Function>(C.getCallee());
89+
std::string DefaultFeatures =
90+
F->getContext().getDefaultTargetFeatures().str();
8991
// Take over the existing function. The frontend emits a weak stub so that the
9092
// linker knows about the symbol; this pass replaces the function body.
9193
F->deleteBody();
9294
F->setAlignment(Align(4096));
9395

94-
Triple T(M.getTargetTriple());
95-
if (T.isARM() || T.isThumb())
96-
F->addFnAttr("target-features", "+thumb-mode");
96+
// Set existing target-features.
97+
if (!DefaultFeatures.empty())
98+
F->addFnAttr("target-features", DefaultFeatures);
99+
100+
DefaultFeatures = F->getDefaultTargetFeatures(M.getTargetABIFromMD());
101+
if (!DefaultFeatures.empty())
102+
F->addFnAttr("target-features", DefaultFeatures);
97103

98104
auto args = F->arg_begin();
99105
Value &CallSiteTypeId = *(args++);
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)