Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Module level target-abi to assign target features for codegenerated functions. #100833

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
const Twine &N, Module &M);

StringRef getDefaultTargetFeatures(const StringRef TargetABI);

/// Creates a function with some attributes recorded in llvm.module.flags
/// and the LLVMContext applied.
///
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,9 @@ class LLVM_ABI Module {

/// Set the target variant version build SDK version metadata.
void setDarwinTargetVariantSDKVersion(VersionTuple Version);

/// Returns target-abi from MDString, null if target-abi is absent.
StringRef getTargetABIFromMD();
};

/// Given "llvm.used" or "llvm.compiler.used" as a global name, collect the
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/IR/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ModRef.h"
#include "llvm/TargetParser/Triple.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -387,6 +388,25 @@ Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
}

StringRef Function::getDefaultTargetFeatures(const StringRef TargetABI) {
Triple T(getParent()->getTargetTriple());
StringRef attr = "";
if (T.isRISCV64()) {
if (TargetABI.contains("lp64d"))
attr = "+d";
else if (TargetABI.contains("lp64f"))
attr = "+f";
else if (TargetABI.contains("lp64q"))
attr = "+q";
} else if (T.isRISCV32() && TargetABI.contains("ilp32f")) {
attr = "+f";
} else if (T.isARM() || T.isThumb()) {
attr = "+thumb-mode";
}

return attr;
}

Function *Function::createWithDefaultAttr(FunctionType *Ty,
LinkageTypes Linkage,
unsigned AddrSpace, const Twine &N,
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,11 @@ VersionTuple Module::getDarwinTargetVariantSDKVersion() const {
void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) {
addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version");
}

StringRef Module::getTargetABIFromMD() {
StringRef TargetABI = "";
if (auto *TargetABIMD =
dyn_cast_or_null<MDString>(getModuleFlag("target-abi")))
TargetABI = TargetABIMD->getString();
return TargetABI;
}
12 changes: 9 additions & 3 deletions llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
"__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
PointerType::getUnqual(Ctx), PointerType::getUnqual(Ctx));
Function *F = cast<Function>(C.getCallee());
std::string DefaultFeatures =
F->getContext().getDefaultTargetFeatures().str();
// Take over the existing function. The frontend emits a weak stub so that the
// linker knows about the symbol; this pass replaces the function body.
F->deleteBody();
F->setAlignment(Align(4096));

Triple T(M.getTargetTriple());
if (T.isARM() || T.isThumb())
F->addFnAttr("target-features", "+thumb-mode");
// Set existing target-features.
if (!DefaultFeatures.empty())
F->addFnAttr("target-features", DefaultFeatures);

DefaultFeatures = F->getDefaultTargetFeatures(M.getTargetABIFromMD());
if (!DefaultFeatures.empty())
F->addFnAttr("target-features", DefaultFeatures);

auto args = F->arg_begin();
Value &CallSiteTypeId = *(args++);
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/Transforms/CrossDSOCFI/riscv.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; RUN: opt -S -passes=cross-dso-cfi < %s | FileCheck --check-prefix=RISCV64 %s

target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-linux-gnu"

define signext i8 @f() !type !0 !type !1 {
entry:
ret i8 1
}

!llvm.module.flags = !{!2, !3}

!0 = !{i64 0, !"_ZTSFcvE"}
!1 = !{i64 0, i64 111}
!2 = !{i32 4, !"Cross-DSO CFI", i32 1}
!3 = !{i32 1, !"target-abi", !"lp64d"}

; RISCV64: define void @__cfi_check({{.*}} #[[A:.*]] align 4096
; RISCV64: attributes #[[A]] = { {{.*}}"target-features"="+d"
Loading