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

Read target-abi from the first bitcode file for LTO #543

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion lld/test/ELF/lto/riscv32.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

; RUN: llvm-as %s -o %t.o
; RUN: ld.lld %t.o -o %t
; RUN: llvm-readelf --syms %t | FileCheck %s
; CHECK: Symbol table '.symtab' contains 3 entries:
; CHECK: FUNC GLOBAL DEFAULT 1 _start
; CHECK-EMPTY:

target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-unknown-elf"

define void @f() {
define void @_start() {
ret void
}

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"ilp32"}
35 changes: 35 additions & 0 deletions lld/test/ELF/lto/riscv64-purecap-infer-abi.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; REQUIRES: riscv

; RUN: llvm-as %s -o %t.o
; Note: -mattr is needed since there is no global metadata for the target features,
; but we should be able to infer target-abi from the first input file
; RUN: ld.lld %t.o -o %t -plugin-opt=-mattr=+xcheri -e purecap_fn
; RUN: llvm-readelf --syms --arch-specific %t | FileCheck %s
; RUN: llvm-objdump -d %t | FileCheck %s --check-prefix=DUMP
; CHECK: Symbol table '.symtab' contains 3 entries:
; CHECK: FUNC GLOBAL DEFAULT 1 purecap_fn
; CHECK: Attribute {
; CHECK: Tag: 5
; CHECK-NEXT: TagName: arch
; CHECK-NEXT: Value: rv64i2p0_xcheri0p0
; CHECK-NEXT: }
; DUMP: 0000000000011120 <purecap_fn>:
; DUMP-NEXT: 11120: 5b 80 c0 fe cret

; RUN: llvm-as %S/riscv64.ll -o %t-nocheri.o
; RUN: not ld.lld -o %t -plugin-opt=-mattr=+xcheri %t.o %t-nocheri.o -e purecap_fn 2>&1 | FileCheck %s --check-prefix INCOMPATIBLE
; INCOMPATIBLE: warning: Linking two modules of different data layouts: '{{.+}}riscv64-purecap-infer-abi.ll.tmp-nocheri.o' is 'e-m:e-p:64:64-i64:64-i128:128-n64-S128' whereas 'ld-temp.o' is 'e-m:e-pf200:128:128:128:64-p:64:64-i64:64-i128:128-n64-S128-A200-P200-G200'
; INCOMPATIBLE: warning: Linking two modules of different target triples: '{{.+}}riscv64-purecap-infer-abi.ll.tmp-nocheri.o' is 'riscv64-unknown-elf' whereas 'ld-temp.o' is 'riscv64-unknown-freebsd'
; INCOMPATIBLE: error: linking module flags 'target-abi': IDs have conflicting values in '{{.+}}riscv64-purecap-infer-abi.ll.tmp-nocheri.o' and 'ld-temp.o'

target datalayout = "e-m:e-pf200:128:128:128:64-p:64:64-i64:64-i128:128-n64-S128-A200-P200-G200"
target triple = "riscv64-unknown-freebsd"

define void @purecap_fn() addrspace(200) #0 {
entry:
ret void
}

attributes #0 = { nounwind "target-features"="+cap-mode,+xcheri" }
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"l64pc128"}
9 changes: 8 additions & 1 deletion lld/test/ELF/lto/riscv64.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

; RUN: llvm-as %s -o %t.o
; RUN: ld.lld %t.o -o %t
; RUN: llvm-readelf --syms %t | FileCheck %s
; CHECK: Symbol table '.symtab' contains 3 entries:
; CHECK: FUNC GLOBAL DEFAULT 1 _start
; CHECK-EMPTY:
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
target triple = "riscv64-unknown-elf"

define void @f() {
define void @_start() {
ret void
}

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"lp64"}
13 changes: 11 additions & 2 deletions llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,24 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
RelocModel =
M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;

TargetOptions TOpts = Conf.Options;
if (const MDString *ModuleTargetABI =
dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi"))) {
if (Conf.Options.MCOptions.ABIName != ModuleTargetABI->getString()) {
/* report_fatal_error("target-abi metadata was ignored!"); */
TOpts.MCOptions.ABIName = ModuleTargetABI->getString().str();
}
}

Optional<CodeModel::Model> CodeModel;
if (Conf.CodeModel)
CodeModel = *Conf.CodeModel;
else
CodeModel = M.getCodeModel();

std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
CodeModel, Conf.CGOptLevel));
TheTriple, Conf.CPU, Features.getString(), TOpts, RelocModel, CodeModel,
Conf.CGOptLevel));
assert(TM && "Failed to create target machine");
return TM;
}
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ static std::string computeDataLayout(const Triple &TT, StringRef FS,

StringRef CapTypes = "";
StringRef PurecapOptions = "";
if (FS.contains("+xcheri")) {
RISCVABI::ABI ABI = RISCVABI::getTargetABI(Options.MCOptions.getABIName());
bool IsPurecapABI =
ABI != RISCVABI::ABI_Unknown && RISCVABI::isCheriPureCapABI(ABI);
if (FS.contains("+xcheri") || IsPurecapABI) {
if (TT.isArch64Bit())
CapTypes = "-pf200:128:128:128:64";
else
CapTypes = "-pf200:64:64:64:32";

RISCVABI::ABI ABI = RISCVABI::getTargetABI(Options.MCOptions.getABIName());
if (ABI != RISCVABI::ABI_Unknown && RISCVABI::isCheriPureCapABI(ABI))
if (IsPurecapABI)
PurecapOptions = "-A200-P200-G200";
}

Expand Down