Skip to content

Commit 4d5a8cc

Browse files
authored
[Driver] Add LTO support for Haiku and OpenBSD (#72047)
1 parent 38b34c6 commit 4d5a8cc

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
598598
const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
599599
const Driver &D = ToolChain.getDriver();
600600
if (llvm::sys::path::filename(Linker) != "ld.lld" &&
601-
llvm::sys::path::stem(Linker) != "ld.lld") {
601+
llvm::sys::path::stem(Linker) != "ld.lld" &&
602+
!ToolChain.getTriple().isOSOpenBSD()) {
602603
// Tell the linker to load the plugin. This has to come before
603604
// AddLinkerInputs as gold requires -plugin and AIX ld requires -bplugin to
604605
// come before any -plugin-opt/-bplugin_opt that -Wl might forward.

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
8383
options::OPT_s, options::OPT_t, options::OPT_r});
8484
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
8585

86+
if (D.isUsingLTO()) {
87+
assert(!Inputs.empty() && "Must have at least one input.");
88+
// Find the first filename InputInfo object.
89+
auto Input = llvm::find_if(
90+
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
91+
if (Input == Inputs.end())
92+
// For a very rare case, all of the inputs to the linker are
93+
// InputArg. If that happens, just use the first InputInfo.
94+
Input = Inputs.begin();
95+
96+
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
97+
D.getLTOMode() == LTOK_Thin);
98+
}
99+
86100
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
87101
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
88102

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
195195
Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
196196
options::OPT_t, options::OPT_r});
197197

198+
if (D.isUsingLTO()) {
199+
assert(!Inputs.empty() && "Must have at least one input.");
200+
// Find the first filename InputInfo object.
201+
auto Input = llvm::find_if(
202+
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
203+
if (Input == Inputs.end())
204+
// For a very rare case, all of the inputs to the linker are
205+
// InputArg. If that happens, just use the first InputInfo.
206+
Input = Inputs.begin();
207+
208+
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
209+
D.getLTOMode() == LTOK_Thin);
210+
}
211+
198212
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
199213
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
200214
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);

clang/test/Driver/emulated-tls.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
// RUN: | FileCheck %s --check-prefix=LTO_EMUTLS
4040
// RUN: %clang -### -flto --target=riscv64-linux-android10000 -fno-emulated-tls %s 2>&1 \
4141
// RUN: | FileCheck %s --check-prefix=LTO_NOEMUTLS
42+
// RUN: %clang -### -flto --target=amd64-unknown-openbsd %s 2>&1 \
43+
// RUN: | FileCheck %s --check-prefix=LTO_EMUTLS
4244

4345
// Default without -f[no-]emulated-tls, will be decided by the target triple.
4446
// DEFAULT-NOT: "-cc1" {{.*}}"-femulated-tls"

clang/test/Driver/haiku.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@
7575
// RUN: %clang -### %s 2>&1 --target=arm-unknown-haiku \
7676
// RUN: | FileCheck --check-prefix=CHECK-ARM-CPU %s
7777
// CHECK-ARM-CPU: "-target-cpu" "arm1176jzf-s"
78+
79+
// Check passing LTO flags to the linker
80+
// RUN: %clang --target=x86_64-unknown-haiku -flto -### %s 2>&1 \
81+
// RUN: | FileCheck -check-prefix=CHECK-LTO-FLAGS %s
82+
// CHECK-LTO-FLAGS: "-plugin-opt=mcpu=x86-64"

clang/test/Driver/openbsd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,8 @@
131131
// RUN: %clang --target=riscv64-unknown-openbsd -### %s 2>&1 \
132132
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAGS %s
133133
// CHECK-RISCV64-FLAGS: "-X"
134+
135+
// Check passing LTO flags to the linker
136+
// RUN: %clang --target=amd64-unknown-openbsd -flto -### %s 2>&1 \
137+
// RUN: | FileCheck -check-prefix=CHECK-LTO-FLAGS %s
138+
// CHECK-LTO-FLAGS: "-plugin-opt=mcpu=x86-64"

clang/test/Driver/save-stats.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424
// RUN: %clang --target=x86_64-unknown-linux -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
2525
// RUN: %clang --target=x86_64-unknown-freebsd -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
2626
// RUN: %clang --target=x86_64-unknown-freebsd -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
27+
// RUN: %clang --target=x86_64-unknown-openbsd -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
28+
// RUN: %clang --target=x86_64-unknown-openbsd -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
2729
// RUN: %clang --target=x86_64-unknown-fuchsia -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
2830
// RUN: %clang --target=x86_64-unknown-fuchsia -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
31+
// RUN: %clang --target=x86_64-unknown-haiku -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
32+
// RUN: %clang --target=x86_64-unknown-haiku -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
2933
// RUN: %clang --target=avr -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
3034
// RUN: %clang --target=avr -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
3135
// CHECK-LTO: "-stats-file=save-stats.stats"
3236
// CHECK-LTO: "-o" "obj/dir{{/|\\\\}}save-stats.exe"
3337
// CHECK-LTO: "-plugin-opt=stats-file=save-stats.stats"
3438

35-
3639
// RUN: %clang --target=powerpc-unknown-aix -save-stats -flto -o obj/dir/save-stats %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-AIX-LTO
3740
// RUN: %clang --target=powerpc-unknown-aix -save-stats -flto -o obj/dir/save-stats -Wl,-bplugin-opt:-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-AIX-LTO
3841

0 commit comments

Comments
 (0)