[clang] [Driver] Add LTO support for Haiku and OpenBSD (PR #72047)
Brad Smith via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 12 01:23:27 PST 2023
https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/72047
None
>From 9f374ff55cb851534a8c882770d9237f1d40a8b8 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Sat, 21 Oct 2023 02:18:34 -0400
Subject: [PATCH] [Driver] Add LTO support for Haiku and OpenBSD
---
clang/lib/Driver/ToolChains/CommonArgs.cpp | 3 ++-
clang/lib/Driver/ToolChains/Haiku.cpp | 14 ++++++++++++++
clang/lib/Driver/ToolChains/OpenBSD.cpp | 14 ++++++++++++++
clang/test/Driver/emulated-tls.cpp | 2 ++
clang/test/Driver/haiku.c | 5 +++++
clang/test/Driver/openbsd.c | 5 +++++
clang/test/Driver/save-stats.c | 5 ++++-
7 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index a81c9b6201f8158..5d2cd1959b06925 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -598,7 +598,8 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
const Driver &D = ToolChain.getDriver();
if (llvm::sys::path::filename(Linker) != "ld.lld" &&
- llvm::sys::path::stem(Linker) != "ld.lld") {
+ llvm::sys::path::stem(Linker) != "ld.lld" &&
+ !ToolChain.getTriple().isOSOpenBSD()) {
// Tell the linker to load the plugin. This has to come before
// AddLinkerInputs as gold requires -plugin and AIX ld requires -bplugin to
// come before any -plugin-opt/-bplugin_opt that -Wl might forward.
diff --git a/clang/lib/Driver/ToolChains/Haiku.cpp b/clang/lib/Driver/ToolChains/Haiku.cpp
index 1c2d6bcaf9b457d..e0d94035823fd37 100644
--- a/clang/lib/Driver/ToolChains/Haiku.cpp
+++ b/clang/lib/Driver/ToolChains/Haiku.cpp
@@ -83,6 +83,20 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_s, options::OPT_t, options::OPT_r});
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
+ if (D.isUsingLTO()) {
+ assert(!Inputs.empty() && "Must have at least one input.");
+ // Find the first filename InputInfo object.
+ auto Input = llvm::find_if(
+ Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
+ if (Input == Inputs.end())
+ // For a very rare case, all of the inputs to the linker are
+ // InputArg. If that happens, just use the first InputInfo.
+ Input = Inputs.begin();
+
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ D.getLTOMode() == LTOK_Thin);
+ }
+
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index d9f6b20f43ad854..798b39ce86badce 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -195,6 +195,20 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
options::OPT_t, options::OPT_r});
+ if (D.isUsingLTO()) {
+ assert(!Inputs.empty() && "Must have at least one input.");
+ // Find the first filename InputInfo object.
+ auto Input = llvm::find_if(
+ Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
+ if (Input == Inputs.end())
+ // For a very rare case, all of the inputs to the linker are
+ // InputArg. If that happens, just use the first InputInfo.
+ Input = Inputs.begin();
+
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ D.getLTOMode() == LTOK_Thin);
+ }
+
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
diff --git a/clang/test/Driver/emulated-tls.cpp b/clang/test/Driver/emulated-tls.cpp
index 2044bc89a36634a..3ee901a8358589e 100644
--- a/clang/test/Driver/emulated-tls.cpp
+++ b/clang/test/Driver/emulated-tls.cpp
@@ -39,6 +39,8 @@
// RUN: | FileCheck %s --check-prefix=LTO_EMUTLS
// RUN: %clang -### -flto --target=riscv64-linux-android10000 -fno-emulated-tls %s 2>&1 \
// RUN: | FileCheck %s --check-prefix=LTO_NOEMUTLS
+// RUN: %clang -### -flto --target=amd64-unknown-openbsd %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LTO_EMUTLS
// Default without -f[no-]emulated-tls, will be decided by the target triple.
// DEFAULT-NOT: "-cc1" {{.*}}"-femulated-tls"
diff --git a/clang/test/Driver/haiku.c b/clang/test/Driver/haiku.c
index 965d3cf97bc36bf..e907c34b29b9955 100644
--- a/clang/test/Driver/haiku.c
+++ b/clang/test/Driver/haiku.c
@@ -75,3 +75,8 @@
// RUN: %clang -### %s 2>&1 --target=arm-unknown-haiku \
// RUN: | FileCheck --check-prefix=CHECK-ARM-CPU %s
// CHECK-ARM-CPU: "-target-cpu" "arm1176jzf-s"
+
+// Check passing LTO flags to the linker
+// RUN: %clang --target=x86_64-unknown-haiku -flto -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=CHECK-LTO-FLAGS %s
+// CHECK-LTO-FLAGS: "-plugin-opt=mcpu=x86-64"
diff --git a/clang/test/Driver/openbsd.c b/clang/test/Driver/openbsd.c
index 586c65f502a0d15..68d33c5dd01e227 100644
--- a/clang/test/Driver/openbsd.c
+++ b/clang/test/Driver/openbsd.c
@@ -131,3 +131,8 @@
// RUN: %clang --target=riscv64-unknown-openbsd -### %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAGS %s
// CHECK-RISCV64-FLAGS: "-X"
+
+// Check passing LTO flags to the linker
+// RUN: %clang --target=amd64-unknown-openbsd -flto -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=CHECK-LTO-FLAGS %s
+// CHECK-LTO-FLAGS: "-plugin-opt=mcpu=x86-64"
diff --git a/clang/test/Driver/save-stats.c b/clang/test/Driver/save-stats.c
index 54f222f3d57abdf..ad7867a9916860b 100644
--- a/clang/test/Driver/save-stats.c
+++ b/clang/test/Driver/save-stats.c
@@ -24,15 +24,18 @@
// 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
// 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
// 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
+// 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
+// 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
// 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
// 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
+// 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
+// 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
// RUN: %clang --target=avr -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
// 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
// CHECK-LTO: "-stats-file=save-stats.stats"
// CHECK-LTO: "-o" "obj/dir{{/|\\\\}}save-stats.exe"
// CHECK-LTO: "-plugin-opt=stats-file=save-stats.stats"
-
// RUN: %clang --target=powerpc-unknown-aix -save-stats -flto -o obj/dir/save-stats %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-AIX-LTO
// 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
More information about the cfe-commits
mailing list