[clang] [clang][Driver] NetBSD: support LTO (PR #249261)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 09:27:28 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Elias Benali (ebenali)

<details>
<summary>Changes</summary>

Fixes #<!-- -->249246

Linking LLVM bitcode for netBSD targets failed in the driver before any linker ran since the NetBSD toolchain lacked two pieces other ELF toolchains have:
- `HasNativeLLVMSupport()` override
- `netbsd::Linker::ConstructJob()` never called `addLTOOptions()`

Mirrors the FreeBSD toolchain; works as is with lld, GNU ld needs
the LLVMgold plugin.
    
Adds driver test for LTO link lines.

---
Full diff: https://github.com/llvm/llvm-project/pull/249261.diff


3 Files Affected:

- (modified) clang/lib/Driver/ToolChains/NetBSD.cpp (+5) 
- (modified) clang/lib/Driver/ToolChains/NetBSD.h (+2) 
- (modified) clang/test/Driver/netbsd.c (+12) 


``````````diff
diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 552191a49a875..420dd6233d53d 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -274,6 +274,9 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                             options::OPT_s, options::OPT_t});
   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
 
+  if (auto LTO = ToolChain.getLTOMode(Args); LTO != LTOK_None)
+    addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs, LTO == LTOK_Thin);
+
   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs, C);
   bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
@@ -437,6 +440,8 @@ Tool *NetBSD::buildAssembler() const {
 
 Tool *NetBSD::buildLinker() const { return new tools::netbsd::Linker(*this); }
 
+bool NetBSD::HasNativeLLVMSupport() const { return true; }
+
 ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() const {
   switch (getArch()) {
   case llvm::Triple::aarch64:
diff --git a/clang/lib/Driver/ToolChains/NetBSD.h b/clang/lib/Driver/ToolChains/NetBSD.h
index bb0953c02e62e..b058e84660e99 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.h
+++ b/clang/lib/Driver/ToolChains/NetBSD.h
@@ -53,6 +53,8 @@ class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
   NetBSD(const Driver &D, const llvm::Triple &Triple,
          const llvm::opt::ArgList &Args);
 
+  bool HasNativeLLVMSupport() const override;
+
   bool IsMathErrnoDefault() const override { return false; }
   bool IsObjCNonFragileABIDefault() const override { return true; }
 
diff --git a/clang/test/Driver/netbsd.c b/clang/test/Driver/netbsd.c
index 8404bb6bbd528..d251dcb8181a1 100644
--- a/clang/test/Driver/netbsd.c
+++ b/clang/test/Driver/netbsd.c
@@ -338,3 +338,15 @@
 // RUN: %clang --target=riscv64-unknown-netbsd -mno-relax -### %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=RISCV-FLAGS %s
 // RISCV-FLAGS: "-X" "--no-relax"
+
+// Check that LTO links: driver passes bitcode to linker with the LTO options
+// RUN: %clang --target=x86_64-unknown-netbsd \
+// RUN:   --sysroot=%S/Inputs/basic_netbsd_tree -fuse-ld=lld -flto -O2 \
+// RUN:   -### %s 2>&1 | FileCheck -check-prefix=LTO %s
+// RUN: %clang --target=x86_64-unknown-netbsd \
+// RUN:   --sysroot=%S/Inputs/basic_netbsd_tree -fuse-ld=lld -flto=thin -O2 \
+// RUN:   -### %s 2>&1 | FileCheck -check-prefixes=LTO,THINLTO %s
+// LTO-NOT: unable to pass LLVM bit-code files to linker
+// LTO: "-plugin-opt=mcpu=x86-64"
+// LTO-SAME: "-plugin-opt=O2"
+// THINLTO-SAME: "-plugin-opt=thinlto"

``````````

</details>


https://github.com/llvm/llvm-project/pull/249261


More information about the cfe-commits mailing list