[clang] [Driver][OpenBSD] Simplify getCompilerRT (PR #248400)

Brad Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 03:36:32 PST 2026


https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/248400

Add the resource directory's lib/, where the base system installs the
compiler-rt runtimes, to the library paths so ToolChain::getCompilerRT
finds them, instead of duplicating its lookup.

Find the builtins, libcompiler_rt.a, with GetFilePath, the same way
libgcc.a is found for -print-libgcc-file-name. These are the paths
passed to the linker for -lcompiler_rt.

The per-target runtime directory is now searched before the base
system one.

Assisted-by: Claude Opus 5.5

>From d28e3e42634927e4e3e40bd9f272db6b6cf497e8 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Thu, 3 Dec 2026 06:25:47 -0500
Subject: [PATCH] [Driver][OpenBSD] Simplify getCompilerRT

Add the resource directory's lib/, where the base system installs the
compiler-rt runtimes, to the library paths so ToolChain::getCompilerRT
finds them, instead of duplicating its lookup.

Find the builtins, libcompiler_rt.a, with GetFilePath, the same way
libgcc.a is found for -print-libgcc-file-name. These are the paths
passed to the linker for -lcompiler_rt.

The per-target runtime directory is now searched before the base
system one.

Assisted-by: Claude Opus 5.5
---
 clang/lib/Driver/ToolChains/OpenBSD.cpp | 24 ++++++-------
 clang/test/Driver/openbsd.c             | 48 +++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 6e41f44ff91a04..11d7008163a059 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -313,6 +313,12 @@ OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
                  const ArgList &Args)
     : Generic_ELF(D, Triple, Args) {
   getFilePaths().push_back(concat(getDriver().SysRoot, "/usr/lib"));
+
+  // The base system installs the compiler-rt runtimes directly in the
+  // resource directory's lib/, without an OS subdirectory or arch suffix.
+  SmallString<128> P(D.ResourceDir);
+  llvm::sys::path::append(P, "lib");
+  getLibraryPaths().push_back(std::string(P));
 }
 
 void OpenBSD::AddClangSystemIncludeArgs(
@@ -362,19 +368,11 @@ void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
 
 std::string OpenBSD::getCompilerRT(const ArgList &Args, StringRef Component,
                                    FileType Type, bool IsFortran) const {
-  if (Component == "builtins") {
-    SmallString<128> Path(getDriver().SysRoot);
-    llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a");
-    if (getVFS().exists(Path))
-      return std::string(Path);
-  }
-  SmallString<128> P(getDriver().ResourceDir);
-  std::string CRTBasename = buildCompilerRTBasename(
-      Args, Component, Type, /*AddArch=*/false, IsFortran);
-  llvm::sys::path::append(P, "lib", CRTBasename);
-  // Checks if this is the base system case which uses a different location.
-  if (getVFS().exists(P))
-    return std::string(P);
+  // The builtins are libcompiler_rt.a, the library linked with -lcompiler_rt.
+  if (Component == "builtins")
+    if (std::optional<std::string> Path =
+            GetFilePathIfExists("libcompiler_rt.a"))
+      return *Path;
   return ToolChain::getCompilerRT(Args, Component, Type, IsFortran);
 }
 
diff --git a/clang/test/Driver/openbsd.c b/clang/test/Driver/openbsd.c
index 7f18b9a16fb769..536f06e4a6ceba 100644
--- a/clang/test/Driver/openbsd.c
+++ b/clang/test/Driver/openbsd.c
@@ -150,3 +150,51 @@
 // RUN: %clang --target=amd64-unknown-openbsd -### -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=CHECK-AMD64-IBT %s
 // CHECK-AMD64-IBT: "-fcf-protection=branch" "-fno-jump-tables"
+
+// Check where the compiler-rt runtimes are found. The base system has
+// libcompiler_rt.a in /usr/lib, and the runtimes directly in the resource
+// directory's lib/, without an OS subdirectory or arch suffix.
+// RUN: rm -rf %t && mkdir -p %t/empty
+// RUN: mkdir -p %t/base/usr/lib/clang/22/lib
+// RUN: touch %t/base/usr/lib/libcompiler_rt.a
+// RUN: touch %t/base/usr/lib/clang/22/lib/libclang_rt.profile.a
+// RUN: touch %t/base/usr/lib/clang/22/lib/libclang_rt.ubsan_minimal.a
+// RUN: mkdir -p %t/pertarget/lib/x86_64-unknown-openbsd
+// RUN: touch %t/pertarget/lib/x86_64-unknown-openbsd/libclang_rt.builtins.a
+// RUN: touch %t/pertarget/lib/x86_64-unknown-openbsd/libclang_rt.profile.a
+// RUN: mkdir -p %t/old/lib/openbsd
+// RUN: touch %t/old/lib/openbsd/libclang_rt.profile-x86_64.a
+// RUN: mkdir -p %t/both/lib/x86_64-unknown-openbsd
+// RUN: touch %t/both/lib/libclang_rt.profile.a
+// RUN: touch %t/both/lib/x86_64-unknown-openbsd/libclang_rt.profile.a
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/base \
+// RUN:   -resource-dir=%t/base/usr/lib/clang/22 -print-libgcc-file-name 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RT-BUILTINS-BASE %s
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/empty \
+// RUN:   -resource-dir=%t/pertarget -print-libgcc-file-name 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RT-BUILTINS-PERTARGET %s
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/base \
+// RUN:   -resource-dir=%t/base/usr/lib/clang/22 -fprofile-instr-generate \
+// RUN:   -### %s 2>&1 | FileCheck -check-prefix=CHECK-RT-PROFILE-BASE %s
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/base \
+// RUN:   -resource-dir=%t/base/usr/lib/clang/22 -fsanitize=undefined \
+// RUN:   -fsanitize-minimal-runtime -### %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RT-UBSAN-BASE %s
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/empty \
+// RUN:   -resource-dir=%t/pertarget -fprofile-instr-generate -### %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RT-PROFILE-PERTARGET %s
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/empty \
+// RUN:   -resource-dir=%t/old -fprofile-instr-generate -### %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RT-PROFILE-OLD %s
+// RUN: %clang --target=x86_64-unknown-openbsd --sysroot=%t/empty \
+// RUN:   -resource-dir=%t/both -fprofile-instr-generate -### %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RT-PROFILE-BOTH %s
+// CHECK-RT-BUILTINS-BASE: {{.*}}base{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}libcompiler_rt.a
+// CHECK-RT-BUILTINS-PERTARGET: {{.*}}pertarget{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-openbsd{{/|\\\\}}libclang_rt.builtins.a
+// CHECK-RT-PROFILE-BASE: "{{.*}}base{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}clang{{/|\\\\}}22{{/|\\\\}}lib{{/|\\\\}}libclang_rt.profile.a"
+// CHECK-RT-UBSAN-BASE: "{{.*}}base{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}clang{{/|\\\\}}22{{/|\\\\}}lib{{/|\\\\}}libclang_rt.ubsan_minimal.a"
+// CHECK-RT-UBSAN-BASE-SAME: "{{.*}}base{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}libcompiler_rt.a"
+// CHECK-RT-PROFILE-PERTARGET: "{{.*}}pertarget{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-openbsd{{/|\\\\}}libclang_rt.profile.a"
+// CHECK-RT-PROFILE-OLD: "{{.*}}old{{/|\\\\}}lib{{/|\\\\}}openbsd{{/|\\\\}}libclang_rt.profile-x86_64.a"
+// The per-target directory is searched before the base system one.
+// CHECK-RT-PROFILE-BOTH: "{{.*}}both{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-openbsd{{/|\\\\}}libclang_rt.profile.a"



More information about the cfe-commits mailing list