Skip to content

Commit

Permalink
[RISCV][LTO] enable LTO/ThinLTO on RISCV (D78035)
Browse files Browse the repository at this point in the history
  • Loading branch information
huettern committed Jun 30, 2021
1 parent eb264d1 commit e7b5f08
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/Driver/ToolChains/RISCVToolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,

std::string Linker = getToolChain().GetLinkerPath();

if (D.isUsingLTO()) {
assert(!Inputs.empty() && "Must have at least one input.");
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
D.getLTOMode() == LTOK_Thin);
}

bool WantCRTs =
!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/RISCVToolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class LLVM_LIBRARY_VISIBILITY RISCVToolChain : public Generic_ELF {
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind) const override;
bool HasNativeLLVMSupport() const override { return true; }
RuntimeLibType GetDefaultRuntimeLibType() const override;
UnwindLibType
GetUnwindLibType(const llvm::opt::ArgList &Args) const override;
Expand Down
13 changes: 13 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ RISCVTargetMachine::getSubtargetImpl(const Function &F) const {
return I.get();
}

void RISCVTargetMachine::setTargetOptionsWithModuleMetadata(
const Module &M LLVM_ATTRIBUTE_UNUSED) {
StringRef ABIName = Options.MCOptions.getABIName();
if (const MDString *ModuleTargetABI =
dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi"))) {
StringRef ModuleABIName = ModuleTargetABI->getString();
if (!ABIName.empty() && ModuleABIName != ABIName)
report_fatal_error("-target-abi option != target-abi module flag");
if (ABIName.empty())
Options.MCOptions.ABIName = ModuleABIName.str();
}
}

TargetTransformInfo
RISCVTargetMachine::getTargetTransformInfo(const Function &F) {
return TargetTransformInfo(RISCVTTIImpl(this, F));
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class RISCVTargetMachine : public LLVMTargetMachine {
return TLOF.get();
}

void setTargetOptionsWithModuleMetadata(
const Module &M LLVM_ATTRIBUTE_UNUSED) override;

TargetTransformInfo getTargetTransformInfo(const Function &F) override;

virtual bool isNoopAddrSpaceCast(unsigned SrcAS,
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/LTO/RISCV/Inputs/mabi-invalid.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-unknown-linux-gnu"

define float @foo(float %x) #0 {
%conv = fpext float %x to double
%add = fadd double %conv, 0x400921FD80C9BEFB
%conv1 = fptrunc double %add to float
ret float %conv1
}

attributes #0 = { nounwind "target-features"="+a,+c,+f,+m,+relax" }

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"ilp32"}
14 changes: 14 additions & 0 deletions llvm/test/LTO/RISCV/Inputs/mabi.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-unknown-linux-gnu"

define float @foo(float %x) #0 {
%conv = fpext float %x to double
%add = fadd double %conv, 0x400921FD80C9BEFB
%conv1 = fptrunc double %add to float
ret float %conv1
}

attributes #0 = { nounwind "target-features"="+a,+c,+f,+m,+relax" }

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"ilp32f"}
2 changes: 2 additions & 0 deletions llvm/test/LTO/RISCV/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if not 'RISCV' in config.root.targets:
config.unsupported = True
35 changes: 35 additions & 0 deletions llvm/test/LTO/RISCV/mabi-invalid.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; Check with regular LTO
; RUN: rm -f %t*
; RUN: llvm-as < %s >%t1
; RUN: llvm-as < %p/Inputs/mabi-invalid.ll >%t2
; Check old API
; RUN: not llvm-lto -exported-symbol=main -o %t3 %t1 %t2 2>&1 | FileCheck %s
; Check new API
; RUN: not llvm-lto2 run -r %t1,foo, -r %t1,main,plx -r %t2,foo,plx -o %t3.o %t1 %t2 2>&1 | FileCheck %s

; Check with ThinLTO.
; RUN: rm -f %t*
; RUN: opt -module-summary -o %t1 %s
; RUN: opt -module-summary -o %t2 %p/Inputs/mabi-invalid.ll
; Check old API
; RUN: not --crash llvm-lto -thinlto -thinlto-action=run %t1 %t2 -exported-symbol=main 2>&1 | FileCheck %s
; Check new API
; RUN: not --crash llvm-lto2 run -r %t1,foo, -r %t1,main,plx -r %t2,foo,plx -o %t3.o %t1 %t2 2>&1 | FileCheck %s

; CHECK: 'target-abi': IDs have conflicting values

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

declare float @foo(float) #1

define float @main(float %x) #0 {
%retval = call float @foo(float 10.0)
ret float %retval
}

attributes #0 = { nounwind "target-features"="+a,+c,+f,+m,+relax" }
attributes #1 = { nounwind "target-features"="+a,+c,+f,+m,+relax" }

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"ilp32f"}
44 changes: 44 additions & 0 deletions llvm/test/LTO/RISCV/mabi.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; Test target-abi module flag generate correct elf flags.

; Check with regular LTO
; RUN: rm -f %t*
; RUN: llvm-as < %s >%t1
; RUN: llvm-as < %p/Inputs/mabi.ll >%t2
; Check old API
; RUN: llvm-lto -exported-symbol=main -o %t3 %t1 %t2
; RUN: llvm-readelf -h %t3 | FileCheck %s
; Check new API
; RUN: llvm-lto2 run -r %t1,foo, -r %t1,main,plx -r %t2,foo,plx -o %t3.o %t1 %t2
; RUN: llvm-readelf -h %t3.o.0 | FileCheck %s

; Check with ThinLTO.
; RUN: rm -f %t*
; RUN: opt -module-summary -o %t1 %s
; RUN: opt -module-summary -o %t2 %p/Inputs/mabi.ll
; Check old API
; RUN: llvm-lto -thinlto -thinlto-action=run %t1 %t2 -exported-symbol=main
; RUN: llvm-readelf -h %t1.thinlto.o | FileCheck %s
; RUN: llvm-readelf -h %t2.thinlto.o | FileCheck %s
; Check new API
; RUN: llvm-lto2 run -r %t1,foo, -r %t1,main,plx -r %t2,foo,plx -o %t3.o %t1 %t2
; RUN: llvm-readelf -h %t3.o.1 | FileCheck %s
; RUN: llvm-readelf -h %t3.o.2 | FileCheck %s

; CHECK: Flags: 0x2, single-float ABI

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

declare float @foo(float) #1

define float @main(float %x) #0 {
%retval = call float @foo(float 10.0)
ret float %retval
}

attributes #0 = { nounwind "target-features"="+a,+c,+f,+m,+relax" }
attributes #1 = { nounwind "target-features"="+a,+c,+f,+m,+relax" }

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"ilp32f"}

0 comments on commit e7b5f08

Please sign in to comment.