https://github.com/srividya-sundaram updated https://github.com/llvm/llvm-project/pull/174877
>From bcaab110a073894c44ab47889aa4fe41b95dde42 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Wed, 7 Jan 2026 14:42:11 -0800 Subject: [PATCH 01/15] [SYCL][Driver] Pass -lsycl by default for SYCL compilation. --- clang/include/clang/Options/Options.td | 4 ++++ clang/lib/Driver/ToolChains/Gnu.cpp | 7 +++++++ clang/test/Driver/sycl-offload-jit.cpp | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index f336542992993..aa3cd218fc199 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7276,6 +7276,10 @@ def fsycl : Flag<["-"], "fsycl">, HelpText<"Enable SYCL C++ extensions">; def fno_sycl : Flag<["-"], "fno-sycl">, HelpText<"Disable SYCL C++ extensions">; +def nolibsycl : Flag<["-"], "nolibsycl">, + Flags<[NoXarchOption]>, + Visibility<[ClangOption, CLOption, ]>, + HelpText<"Do not link SYCL runtime library">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; def fsycl_host_only : Flag<["-"], "fsycl-host-only">, diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 7ecdbe7c57650..943a3250e94f0 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -514,6 +514,13 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, AddRunTimeLibs(ToolChain, D, CmdArgs, Args); + // For SYCL compilations, pass the linker option '-lsycl' by default to + // the clang-linker-wrapper tool which links the SYCL runtime library. + if (Args.hasArg(options::OPT_fsycl) && + !Args.hasArg(options::OPT_nolibsycl)) { + CmdArgs.push_back("-lsycl"); + } + // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so // forcibly link with libatomic as a workaround. // TODO: Issue #41880 and D118021. diff --git a/clang/test/Driver/sycl-offload-jit.cpp b/clang/test/Driver/sycl-offload-jit.cpp index 72c2390a3fe4b..7f8fe64866882 100644 --- a/clang/test/Driver/sycl-offload-jit.cpp +++ b/clang/test/Driver/sycl-offload-jit.cpp @@ -29,6 +29,11 @@ // CHK-DEVICE-TRIPLE-SAME: "-O2" // CHK-DEVICE-TRIPLE: llvm-offload-binary{{.*}} "--image=file={{.*}}.bc,triple=spirv64-unknown-unknown,arch=generic,kind=sycl" +// Check if -lsycl is passed to clang-linker-wrapper tool by default for SYCL compilation. +// RUN: %clang -### -fsycl %s 2>&1 \ +// RUN: | FileCheck -check-prefix=CHECK-LSYCL %s +// CHECK-LSYCL: clang-linker-wrapper{{.*}} "-L/lib"{{.*}} "-lsycl" + /// Check -fsycl-is-device is passed when compiling for the device. /// Check -fsycl-is-host is passed when compiling for host. // RUN: %clang -### -fsycl -c %s 2>&1 \ >From e1c86868c00dca67957abefcfec500e997b0ce38 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Wed, 7 Jan 2026 14:57:11 -0800 Subject: [PATCH 02/15] Remove extra colon. --- clang/include/clang/Options/Options.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index aa3cd218fc199..188e34b513e25 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7278,7 +7278,7 @@ def fno_sycl : Flag<["-"], "fno-sycl">, HelpText<"Disable SYCL C++ extensions">; def nolibsycl : Flag<["-"], "nolibsycl">, Flags<[NoXarchOption]>, - Visibility<[ClangOption, CLOption, ]>, + Visibility<[ClangOption, CLOption]>, HelpText<"Do not link SYCL runtime library">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; >From e74bbcdcbf5e671bca0d4852be8d47141d12cd26 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Thu, 8 Jan 2026 14:55:17 -0800 Subject: [PATCH 03/15] Remove redundant visibility code. --- clang/include/clang/Options/Options.td | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 188e34b513e25..2c9c7ce36e15b 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7278,7 +7278,6 @@ def fno_sycl : Flag<["-"], "fno-sycl">, HelpText<"Disable SYCL C++ extensions">; def nolibsycl : Flag<["-"], "nolibsycl">, Flags<[NoXarchOption]>, - Visibility<[ClangOption, CLOption]>, HelpText<"Do not link SYCL runtime library">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; >From a5ca0dd55ee27ba1ac14faa8683dd1103743b527 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Fri, 9 Jan 2026 16:38:13 -0800 Subject: [PATCH 04/15] Add -L/path/to/sycl/lib along with -lsycl for SYCL offload. --- clang/lib/Driver/ToolChains/Linux.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index cdbf21fb90263..226c609ab8dcf 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -357,6 +357,15 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); + // Add -L/path/to/sycl/lib when -fsycl is specified or when libsycl.so is + // available. The -lsycl option is added implicitly by -fsycl and links + // against the SYCL runtime library (libsycl.so), which is located in this + // directory. + if (StringRef(D.Dir).starts_with(SysRoot) && + (Args.hasArg(options::OPT_fsycl) || + D.getVFS().exists(D.Dir + "/../lib/libsycl.so"))) + addPathIfExists(D, D.Dir + "/../lib", Paths); + addPathIfExists(D, concat(SysRoot, "/lib"), Paths); addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths); } >From 9e87341d6eecb4662daa6b5abad4c05790f5b31e Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Mon, 12 Jan 2026 11:43:03 -0800 Subject: [PATCH 05/15] Include SYCL header search locations by default for SYCL compilation. --- .../clang/Driver/SyclInstallationDetector.h | 5 +++++ clang/lib/Driver/ToolChains/SYCL.cpp | 19 +++++++++++++++---- clang/test/Driver/sycl-offload-jit.cpp | 6 ++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Driver/SyclInstallationDetector.h b/clang/include/clang/Driver/SyclInstallationDetector.h index 6925ec24bcd29..e2526bb4b3517 100644 --- a/clang/include/clang/Driver/SyclInstallationDetector.h +++ b/clang/include/clang/Driver/SyclInstallationDetector.h @@ -16,11 +16,16 @@ namespace driver { class SYCLInstallationDetector { public: + SYCLInstallationDetector(const Driver &D); SYCLInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args); void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const; + +private: + const Driver &D; + llvm::SmallVector<llvm::SmallString<128>, 4> InstallationCandidates; }; } // namespace driver diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index 85859f344b491..fa73f808cb090 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "SYCL.h" #include "clang/Driver/CommonArgs.h" +#include "llvm/Support/VirtualFileSystem.h" using namespace clang::driver; using namespace clang::driver::toolchains; @@ -14,18 +15,28 @@ using namespace clang::driver::tools; using namespace clang; using namespace llvm::opt; +SYCLInstallationDetector::SYCLInstallationDetector(const Driver &D) + : D(D), InstallationCandidates() { + InstallationCandidates.emplace_back(D.Dir + "/.."); +} + SYCLInstallationDetector::SYCLInstallationDetector( const Driver &D, const llvm::Triple &HostTriple, - const llvm::opt::ArgList &Args) {} + const llvm::opt::ArgList &Args) + : SYCLInstallationDetector(D) {} void SYCLInstallationDetector::addSYCLIncludeArgs( const ArgList &DriverArgs, ArgStringList &CC1Args) const { if (DriverArgs.hasArg(options::OPT_nobuiltininc)) return; - // Add the SYCL header search locations in the specified order. - // FIXME: Add the header file locations once the SYCL library and headers - // are properly established within the build. + // Add the SYCL header search locations. + // These are icluded for both SYCL host and device compilations. + SmallString<128> IncludePath(D.Dir); + llvm::sys::path::append(IncludePath, ".."); + llvm::sys::path::append(IncludePath, "include"); + CC1Args.push_back("-internal-isystem"); + CC1Args.push_back(DriverArgs.MakeArgString(IncludePath)); } // Unsupported options for SYCL device compilation. diff --git a/clang/test/Driver/sycl-offload-jit.cpp b/clang/test/Driver/sycl-offload-jit.cpp index 7f8fe64866882..dca4297ee3c34 100644 --- a/clang/test/Driver/sycl-offload-jit.cpp +++ b/clang/test/Driver/sycl-offload-jit.cpp @@ -31,8 +31,10 @@ // Check if -lsycl is passed to clang-linker-wrapper tool by default for SYCL compilation. // RUN: %clang -### -fsycl %s 2>&1 \ -// RUN: | FileCheck -check-prefix=CHECK-LSYCL %s -// CHECK-LSYCL: clang-linker-wrapper{{.*}} "-L/lib"{{.*}} "-lsycl" +// RUN: | FileCheck -check-prefixes=CHECK-LSYCL,CHECK-SYCL-HEADERS-HOST,CHECK-SYCL-HEADERS-DEVICE %s +// CHECK-SYCL-HEADERS-DEVICE: "-fsycl-is-device"{{.*}} "-internal-isystem" "{{.*}}build/bin/../include" +// CHECK-SYCL-HEADERS-HOST: "-fsycl-is-host"{{.*}} "-internal-isystem" "{{.*}}build/bin/../include" +// CHECK-LSYCL: clang-linker-wrapper{{.*}} "-L{{.*}}build/bin/../lib"{{.*}} "-lsycl" /// Check -fsycl-is-device is passed when compiling for the device. /// Check -fsycl-is-host is passed when compiling for host. >From f2f7b7da05f7d4888258eef18d74ae1a5f52e6fc Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Mon, 12 Jan 2026 11:46:45 -0800 Subject: [PATCH 06/15] Fix spelling. --- clang/lib/Driver/ToolChains/SYCL.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index fa73f808cb090..2a9b2055b73e6 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -31,7 +31,7 @@ void SYCLInstallationDetector::addSYCLIncludeArgs( return; // Add the SYCL header search locations. - // These are icluded for both SYCL host and device compilations. + // These are included for both SYCL host and device compilations. SmallString<128> IncludePath(D.Dir); llvm::sys::path::append(IncludePath, ".."); llvm::sys::path::append(IncludePath, "include"); >From 7ee6b1bca0332a06d6bdeb7fe6e4bccd56761115 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Mon, 12 Jan 2026 13:26:52 -0800 Subject: [PATCH 07/15] Address review comments. --- clang/lib/Driver/ToolChains/SYCL.cpp | 3 +-- clang/test/Driver/sycl-offload-jit.cpp | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index 2a9b2055b73e6..da2d196df948e 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -33,8 +33,7 @@ void SYCLInstallationDetector::addSYCLIncludeArgs( // Add the SYCL header search locations. // These are included for both SYCL host and device compilations. SmallString<128> IncludePath(D.Dir); - llvm::sys::path::append(IncludePath, ".."); - llvm::sys::path::append(IncludePath, "include"); + llvm::sys::path::append(IncludePath, "..", "include"); CC1Args.push_back("-internal-isystem"); CC1Args.push_back(DriverArgs.MakeArgString(IncludePath)); } diff --git a/clang/test/Driver/sycl-offload-jit.cpp b/clang/test/Driver/sycl-offload-jit.cpp index dca4297ee3c34..ae3c50067b180 100644 --- a/clang/test/Driver/sycl-offload-jit.cpp +++ b/clang/test/Driver/sycl-offload-jit.cpp @@ -32,9 +32,9 @@ // Check if -lsycl is passed to clang-linker-wrapper tool by default for SYCL compilation. // RUN: %clang -### -fsycl %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-LSYCL,CHECK-SYCL-HEADERS-HOST,CHECK-SYCL-HEADERS-DEVICE %s -// CHECK-SYCL-HEADERS-DEVICE: "-fsycl-is-device"{{.*}} "-internal-isystem" "{{.*}}build/bin/../include" -// CHECK-SYCL-HEADERS-HOST: "-fsycl-is-host"{{.*}} "-internal-isystem" "{{.*}}build/bin/../include" -// CHECK-LSYCL: clang-linker-wrapper{{.*}} "-L{{.*}}build/bin/../lib"{{.*}} "-lsycl" +// CHECK-SYCL-HEADERS-DEVICE: "-fsycl-is-device"{{.*}} "-internal-isystem" "{{.*}}bin/../include" +// CHECK-SYCL-HEADERS-HOST: "-fsycl-is-host"{{.*}} "-internal-isystem" "{{.*}}bin/../include" +// CHECK-LSYCL: clang-linker-wrapper{{.*}} "-L{{.*}}bin/../lib"{{.*}} "-lsycl" /// Check -fsycl-is-device is passed when compiling for the device. /// Check -fsycl-is-host is passed when compiling for host. >From f2ed5d370ed954feb8b97326573cee8ea45cf72a Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Mon, 12 Jan 2026 15:38:58 -0800 Subject: [PATCH 08/15] Update comment. --- clang/test/Driver/sycl-offload-jit.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/test/Driver/sycl-offload-jit.cpp b/clang/test/Driver/sycl-offload-jit.cpp index ae3c50067b180..39f74e474bdc9 100644 --- a/clang/test/Driver/sycl-offload-jit.cpp +++ b/clang/test/Driver/sycl-offload-jit.cpp @@ -30,6 +30,7 @@ // CHK-DEVICE-TRIPLE: llvm-offload-binary{{.*}} "--image=file={{.*}}.bc,triple=spirv64-unknown-unknown,arch=generic,kind=sycl" // Check if -lsycl is passed to clang-linker-wrapper tool by default for SYCL compilation. +// The test also checks if SYCL header include paths are added to the SYCL host and device compilation. // RUN: %clang -### -fsycl %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-LSYCL,CHECK-SYCL-HEADERS-HOST,CHECK-SYCL-HEADERS-DEVICE %s // CHECK-SYCL-HEADERS-DEVICE: "-fsycl-is-device"{{.*}} "-internal-isystem" "{{.*}}bin/../include" >From ebeeec263643ff68e5ad9a1624a4b586169fb021 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Tue, 13 Jan 2026 13:34:46 -0800 Subject: [PATCH 09/15] Fix comment. --- clang/lib/Driver/ToolChains/Gnu.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 943a3250e94f0..6ea8c43c728fe 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -514,8 +514,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, AddRunTimeLibs(ToolChain, D, CmdArgs, Args); - // For SYCL compilations, pass the linker option '-lsycl' by default to - // the clang-linker-wrapper tool which links the SYCL runtime library. + // For SYCL compilations, pass -lsycl to the platform linker by default. if (Args.hasArg(options::OPT_fsycl) && !Args.hasArg(options::OPT_nolibsycl)) { CmdArgs.push_back("-lsycl"); >From 0f091f972d7f38a5f94a7f2b4fe03379760d676a Mon Sep 17 00:00:00 2001 From: Joseph Huber <[email protected]> Date: Wed, 14 Jan 2026 12:12:03 -0600 Subject: [PATCH 10/15] Pass HIP library directly and refactor Summary: Currently we pass `-L` and `-l` to get the HIP library. Because we are attached to a single HIP installation it's far better to pass it by filename. This is because the `-L` could be out of order with other user libraries and those could override it. If someone uses HIP with a specific ROCm installation they most likely want that library, otherwise incompatibilities can occur. This is still overridable with command line flags if users want to pass a different one for some reason. This PR also refactors the handling to be more generic for future additions. --- clang/include/clang/Driver/ToolChain.h | 8 ++--- clang/lib/Driver/ToolChains/CommonArgs.cpp | 15 ---------- clang/lib/Driver/ToolChains/Gnu.cpp | 2 +- clang/lib/Driver/ToolChains/Linux.cpp | 31 +++++++++++++------- clang/lib/Driver/ToolChains/Linux.h | 5 ++-- clang/lib/Driver/ToolChains/MSVC.cpp | 17 +++++++---- clang/lib/Driver/ToolChains/MSVC.h | 4 +-- clang/test/Driver/hip-runtime-libs-linux.hip | 10 +++---- clang/test/Driver/hipstdpar.c | 2 +- clang/test/Driver/rocm-detect.hip | 6 ++-- 10 files changed, 50 insertions(+), 50 deletions(-) diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h index 1425714d34110..16573a6589813 100644 --- a/clang/include/clang/Driver/ToolChain.h +++ b/clang/include/clang/Driver/ToolChain.h @@ -805,10 +805,10 @@ class ToolChain { getDeviceLibs(const llvm::opt::ArgList &Args, const Action::OffloadKind DeviceOffloadingKind) const; - /// Add the system specific linker arguments to use - /// for the given HIP runtime library type. - virtual void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) const {} + /// Add the system specific libraries for the active offload kinds. + virtual void addOffloadRTLibs(unsigned ActiveKinds, + const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const {} /// Return sanitizers which are available in this toolchain. virtual SanitizerMask getSupportedSanitizers() const; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 10a1a412eea08..f83d6d40af79d 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -3045,21 +3045,6 @@ void tools::addOpenMPDeviceRTL(const Driver &D, << LibOmpTargetName << ArchPrefix; } } -void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C, - const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) { - if ((C.getActiveOffloadKinds() & Action::OFK_HIP) && - (!Args.hasArg(options::OPT_nostdlib) || - TC.getTriple().isKnownWindowsMSVCEnvironment()) && - !Args.hasArg(options::OPT_no_hip_rt) && !Args.hasArg(options::OPT_r)) { - TC.AddHIPRuntimeLibArgs(Args, CmdArgs); - } else { - // Claim "no HIP libraries" arguments if any - for (auto *Arg : Args.filtered(options::OPT_no_hip_rt)) { - Arg->claim(); - } - } -} void tools::addOpenCLBuiltinsLib(const Driver &D, const llvm::opt::ArgList &DriverArgs, diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 6ea8c43c728fe..7bbb4c9849ee1 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -451,7 +451,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); - addHIPRuntimeLibArgs(ToolChain, C, Args, CmdArgs); + ToolChain.addOffloadRTLibs(C.getActiveOffloadKinds(), Args, CmdArgs); // The profile runtime also needs access to system libraries. getToolChain().addProfileRTLibs(Args, CmdArgs); diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index 226c609ab8dcf..f585ed61f11ba 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -841,19 +841,28 @@ void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs, RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); } -void Linux::AddHIPRuntimeLibArgs(const ArgList &Args, - ArgStringList &CmdArgs) const { - CmdArgs.push_back( - Args.MakeArgString(StringRef("-L") + RocmInstallation->getLibPath())); +void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args, + ArgStringList &CmdArgs) const { + if (Args.hasArg(options::OPT_nostdlib) || + Args.hasArg(options::OPT_no_hip_rt) || Args.hasArg(options::OPT_r)) + return; - if (Args.hasFlag(options::OPT_frtlib_add_rpath, - options::OPT_fno_rtlib_add_rpath, false)) { - SmallString<0> p = RocmInstallation->getLibPath(); - llvm::sys::path::remove_dots(p, true); - CmdArgs.append({"-rpath", Args.MakeArgString(p)}); - } + llvm::SmallVector<std::pair<StringRef, StringRef>> Libraries; + if (ActiveKinds & Action::OFK_HIP) + Libraries.emplace_back(RocmInstallation->getLibPath(), "libamdhip64.so"); - CmdArgs.push_back("-lamdhip64"); + for (auto [Path, Library] : Libraries) { + if (Args.hasFlag(options::OPT_frtlib_add_rpath, + options::OPT_fno_rtlib_add_rpath, false)) { + SmallString<0> p = Path; + llvm::sys::path::remove_dots(p, true); + CmdArgs.append({"-rpath", Args.MakeArgString(p)}); + } + + SmallString<0> p = Path; + llvm::sys::path::append(p, Library); + CmdArgs.push_back(Args.MakeArgString(p)); + } } void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, diff --git a/clang/lib/Driver/ToolChains/Linux.h b/clang/lib/Driver/ToolChains/Linux.h index 97bad77cb1caa..b4bdc1b1206b1 100644 --- a/clang/lib/Driver/ToolChains/Linux.h +++ b/clang/lib/Driver/ToolChains/Linux.h @@ -37,12 +37,13 @@ class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF { llvm::opt::ArgStringList &CC1Args) const override; void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; - void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) const override; void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; + + void addOffloadRTLibs(unsigned ActiveKinds, const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const override; RuntimeLibType GetDefaultRuntimeLibType() const override; unsigned GetDefaultDwarfVersion() const override; CXXStdlibType GetDefaultCXXStdlibType() const override; diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp index fcae5b7a18f34..229be08ea7a5c 100644 --- a/clang/lib/Driver/ToolChains/MSVC.cpp +++ b/clang/lib/Driver/ToolChains/MSVC.cpp @@ -325,7 +325,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA, A.renderAsInput(Args, CmdArgs); } - addHIPRuntimeLibArgs(TC, C, Args, CmdArgs); + TC.addOffloadRTLibs(C.getActiveOffloadKinds(), Args, CmdArgs); TC.addProfileRTLibs(Args, CmdArgs); @@ -516,11 +516,16 @@ void MSVCToolChain::addSYCLIncludeArgs(const ArgList &DriverArgs, SYCLInstallation->addSYCLIncludeArgs(DriverArgs, CC1Args); } -void MSVCToolChain::AddHIPRuntimeLibArgs(const ArgList &Args, - ArgStringList &CmdArgs) const { - CmdArgs.append({Args.MakeArgString(StringRef("-libpath:") + - RocmInstallation->getLibPath()), - "amdhip64.lib"}); +void MSVCToolChain::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args, + ArgStringList &CmdArgs) const { + if (Args.hasArg(options::OPT_no_hip_rt) || Args.hasArg(options::OPT_r)) + return; + + if (ActiveKinds & Action::OFK_HIP) { + CmdArgs.append({Args.MakeArgString(StringRef("-libpath:") + + RocmInstallation->getLibPath()), + "amdhip64.lib"}); + } } void MSVCToolChain::printVerboseInfo(raw_ostream &OS) const { diff --git a/clang/lib/Driver/ToolChains/MSVC.h b/clang/lib/Driver/ToolChains/MSVC.h index 5c17edce087c7..80a6cbd9bc15b 100644 --- a/clang/lib/Driver/ToolChains/MSVC.h +++ b/clang/lib/Driver/ToolChains/MSVC.h @@ -98,8 +98,8 @@ class LLVM_LIBRARY_VISIBILITY MSVCToolChain : public ToolChain { void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; - void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) const override; + void addOffloadRTLibs(unsigned ActiveKinds, const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const override; void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; diff --git a/clang/test/Driver/hip-runtime-libs-linux.hip b/clang/test/Driver/hip-runtime-libs-linux.hip index eda87d0aa4b6c..d814cae241813 100644 --- a/clang/test/Driver/hip-runtime-libs-linux.hip +++ b/clang/test/Driver/hip-runtime-libs-linux.hip @@ -58,10 +58,10 @@ // RUN: --rocm-path=%S/Inputs/rocm %s 2>&1 \ // RUN: | FileCheck -check-prefixes=ROCM-PATH %s -// ROCM-PATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-lamdhip64" -// ROCM-RPATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64" -// ROCM-RPATH-CANONICAL: "-rpath" "{{.*/rocm/lib}}" "-lamdhip64" -// ROCM-REL: "-L[[HIPRT:.*/opt/rocm-3.10.0/lib]]" "-lamdhip64" +// ROCM-PATH: "{{.*/Inputs/rocm/lib/libamdhip64.so}}" +// ROCM-RPATH: "-rpath" "{{.*/Inputs/rocm/lib}}" +// ROCM-RPATH-CANONICAL: "-rpath" "{{.*/rocm/lib}}" +// ROCM-REL: "{{.*/opt/rocm-3.10.0/lib/libamdhip64.so}}" // NOHIPRT-NOT: "-L{{.*/Inputs/rocm/lib}}" // NOHIPRT-NOT: "-rpath" "{{.*/Inputs/rocm/lib}}" -// NOHIPRT-NOT: "-lamdhip64" +// NOHIPRT-NOT: "libamdhip64.so" diff --git a/clang/test/Driver/hipstdpar.c b/clang/test/Driver/hipstdpar.c index b759c5fb2084a..7d02e0258853e 100644 --- a/clang/test/Driver/hipstdpar.c +++ b/clang/test/Driver/hipstdpar.c @@ -20,4 +20,4 @@ // HIPSTDPAR-COMPILE: "-idirafter" "{{.*/Inputs/hipstdpar}}" // HIPSTDPAR-COMPILE: "-include" "hipstdpar_lib.hpp" // HIPSTDPAR-LINK: "-rpath" -// HIPSTDPAR-LINK: "-l{{.*hip.*}}" +// HIPSTDPAR-LINK: "{{.*hip.*}}" diff --git a/clang/test/Driver/rocm-detect.hip b/clang/test/Driver/rocm-detect.hip index b28b2bc6379dd..2dde98dff070a 100644 --- a/clang/test/Driver/rocm-detect.hip +++ b/clang/test/Driver/rocm-detect.hip @@ -104,17 +104,17 @@ // HIP-PATH: "-mlink-builtin-bitcode" "[[ROCM_PATH]]/amdgcn/bitcode/oclc_isa_version_1010.bc" // HIP-PATH: "-idirafter" "[[HIP_PATH:.*/myhip]]/include" -// HIP-PATH: "-L[[HIP_PATH]]/lib" {{.*}}"-lamdhip64" +// HIP-PATH: "[[HIP_PATH]]/lib/libamdhip64.so" // ROCM-PATH: ROCm installation search path: [[ROCM_PATH:.*/Inputs/rocm]] // ROCM-PATH: "-mlink-builtin-bitcode" "[[ROCM_PATH]]/amdgcn/bitcode/oclc_isa_version_1010.bc" // ROCM-PATH: "-idirafter" "[[ROCM_PATH]]/include" -// ROCM-PATH: "-L[[ROCM_PATH]]/lib" {{.*}}"-lamdhip64" +// ROCM-PATH: "[[ROCM_PATH]]/lib/libamdhip64.so" // USR: ROCm installation search path: [[ROCM_PATH:.*/usr$]] // USR: "-mlink-builtin-bitcode" "[[ROCM_PATH]]/amdgcn/bitcode/oclc_isa_version_1010.bc" // USR: "-idirafter" "[[ROCM_PATH]]/include" -// USR: "-L[[ROCM_PATH]]/lib" {{.*}}"-lamdhip64" +// USR: "[[ROCM_PATH]]/lib/libamdhip64.so" // ROCM-REL: ROCm installation search path: {{.*}}/opt/rocm // ROCM-REL: ROCm installation search path: {{.*}}/opt/rocm-3.10.0 >From 2e0c03af92221dde59399ce34c7487ff2f64443c Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Tue, 20 Jan 2026 14:37:51 -0800 Subject: [PATCH 11/15] Pass path to libsycl.so for SYCL compilations. --- .../clang/Driver/SyclInstallationDetector.h | 7 +++++-- clang/include/clang/Options/Options.td | 6 +----- clang/lib/Driver/ToolChains/Gnu.cpp | 6 ------ clang/lib/Driver/ToolChains/Linux.cpp | 3 +++ clang/lib/Driver/ToolChains/SYCL.cpp | 17 +++++++++++------ clang/test/Driver/sycl-offload-jit.cpp | 4 ++-- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/clang/include/clang/Driver/SyclInstallationDetector.h b/clang/include/clang/Driver/SyclInstallationDetector.h index e2526bb4b3517..f92228817f045 100644 --- a/clang/include/clang/Driver/SyclInstallationDetector.h +++ b/clang/include/clang/Driver/SyclInstallationDetector.h @@ -16,16 +16,19 @@ namespace driver { class SYCLInstallationDetector { public: - SYCLInstallationDetector(const Driver &D); SYCLInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args); void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const; + // Return the filesystem path to the SYCL runtime library (libsycl.so), that + // was detected. + StringRef getSYCLRTLibPath() const { return SYCLRTLibPath; } + private: const Driver &D; - llvm::SmallVector<llvm::SmallString<128>, 4> InstallationCandidates; + SmallString<0> SYCLRTLibPath; }; } // namespace driver diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index ff2e59e2b79f6..a0dce00c90988 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7281,11 +7281,7 @@ defm : FlangIgnoredDiagOpt<"target-lifetime">; let Group = sycl_Group in { def fsycl : Flag<["-"], "fsycl">, HelpText<"Enable SYCL C++ extensions">; -def fno_sycl : Flag<["-"], "fno-sycl">, - HelpText<"Disable SYCL C++ extensions">; -def nolibsycl : Flag<["-"], "nolibsycl">, - Flags<[NoXarchOption]>, - HelpText<"Do not link SYCL runtime library">; +def fno_sycl : Flag<["-"], "fno-sycl">, HelpText<"Disable SYCL C++ extensions">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; def fsycl_host_only : Flag<["-"], "fsycl-host-only">, diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 7bbb4c9849ee1..250f4182c4207 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -514,12 +514,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, AddRunTimeLibs(ToolChain, D, CmdArgs, Args); - // For SYCL compilations, pass -lsycl to the platform linker by default. - if (Args.hasArg(options::OPT_fsycl) && - !Args.hasArg(options::OPT_nolibsycl)) { - CmdArgs.push_back("-lsycl"); - } - // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so // forcibly link with libatomic as a workaround. // TODO: Issue #41880 and D118021. diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index f585ed61f11ba..1fb58abd9b4d8 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -850,6 +850,9 @@ void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args, llvm::SmallVector<std::pair<StringRef, StringRef>> Libraries; if (ActiveKinds & Action::OFK_HIP) Libraries.emplace_back(RocmInstallation->getLibPath(), "libamdhip64.so"); + else if (ActiveKinds & Action::OFK_SYCL) { + Libraries.emplace_back(SYCLInstallation->getSYCLRTLibPath(), "libsycl.so"); + } for (auto [Path, Library] : Libraries) { if (Args.hasFlag(options::OPT_frtlib_add_rpath, diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index da2d196df948e..33c2b9b0bc6a8 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -15,15 +15,20 @@ using namespace clang::driver::tools; using namespace clang; using namespace llvm::opt; -SYCLInstallationDetector::SYCLInstallationDetector(const Driver &D) - : D(D), InstallationCandidates() { - InstallationCandidates.emplace_back(D.Dir + "/.."); -} - SYCLInstallationDetector::SYCLInstallationDetector( const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args) - : SYCLInstallationDetector(D) {} + : D(D) { + // Detect the presence of the SYCL runtime library (libsycl.so) in the + // filesystem. This is used to determine whether a usable SYCL installation + // is available for the current driver invocation. + StringRef SysRoot = D.SysRoot; + if (StringRef(D.Dir).starts_with(SysRoot) && + (Args.hasArg(options::OPT_fsycl) || + D.getVFS().exists(D.Dir + "/../lib/libsycl.so"))) { + SYCLRTLibPath = D.Dir + "/../lib"; + } +} void SYCLInstallationDetector::addSYCLIncludeArgs( const ArgList &DriverArgs, ArgStringList &CC1Args) const { diff --git a/clang/test/Driver/sycl-offload-jit.cpp b/clang/test/Driver/sycl-offload-jit.cpp index 39f74e474bdc9..4015d89a8d9a4 100644 --- a/clang/test/Driver/sycl-offload-jit.cpp +++ b/clang/test/Driver/sycl-offload-jit.cpp @@ -29,13 +29,13 @@ // CHK-DEVICE-TRIPLE-SAME: "-O2" // CHK-DEVICE-TRIPLE: llvm-offload-binary{{.*}} "--image=file={{.*}}.bc,triple=spirv64-unknown-unknown,arch=generic,kind=sycl" -// Check if -lsycl is passed to clang-linker-wrapper tool by default for SYCL compilation. +// Check if path to libsycl.so is passed to clang-linker-wrapper tool by default for SYCL compilation. // The test also checks if SYCL header include paths are added to the SYCL host and device compilation. // RUN: %clang -### -fsycl %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-LSYCL,CHECK-SYCL-HEADERS-HOST,CHECK-SYCL-HEADERS-DEVICE %s // CHECK-SYCL-HEADERS-DEVICE: "-fsycl-is-device"{{.*}} "-internal-isystem" "{{.*}}bin/../include" // CHECK-SYCL-HEADERS-HOST: "-fsycl-is-host"{{.*}} "-internal-isystem" "{{.*}}bin/../include" -// CHECK-LSYCL: clang-linker-wrapper{{.*}} "-L{{.*}}bin/../lib"{{.*}} "-lsycl" +// CHECK-LSYCL: clang-linker-wrapper{{.*}} "{{.*}}bin/../lib/libsycl.so" /// Check -fsycl-is-device is passed when compiling for the device. /// Check -fsycl-is-host is passed when compiling for host. >From 6ac4b9790575c276e419ce569ba977ce400288ae Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Tue, 20 Jan 2026 14:50:57 -0800 Subject: [PATCH 12/15] Remove passing -L/path/to/sycl/lib --- clang/lib/Driver/ToolChains/Linux.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index 1fb58abd9b4d8..f3f841f2586e1 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -357,15 +357,6 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); - // Add -L/path/to/sycl/lib when -fsycl is specified or when libsycl.so is - // available. The -lsycl option is added implicitly by -fsycl and links - // against the SYCL runtime library (libsycl.so), which is located in this - // directory. - if (StringRef(D.Dir).starts_with(SysRoot) && - (Args.hasArg(options::OPT_fsycl) || - D.getVFS().exists(D.Dir + "/../lib/libsycl.so"))) - addPathIfExists(D, D.Dir + "/../lib", Paths); - addPathIfExists(D, concat(SysRoot, "/lib"), Paths); addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths); } >From 05f3011df08b31bb315380f84cf4400d39f23340 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Tue, 20 Jan 2026 14:56:56 -0800 Subject: [PATCH 13/15] Remove fno-sycl. --- clang/include/clang/Options/Options.td | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index a0dce00c90988..2c9c01f4cc5cd 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7281,7 +7281,6 @@ defm : FlangIgnoredDiagOpt<"target-lifetime">; let Group = sycl_Group in { def fsycl : Flag<["-"], "fsycl">, HelpText<"Enable SYCL C++ extensions">; -def fno_sycl : Flag<["-"], "fno-sycl">, HelpText<"Disable SYCL C++ extensions">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; def fsycl_host_only : Flag<["-"], "fsycl-host-only">, >From f77cd185c29b3419ab628fb7bab32a8608a2701a Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Tue, 20 Jan 2026 14:59:26 -0800 Subject: [PATCH 14/15] Put back accidental removal of fno_sycl --- clang/include/clang/Options/Options.td | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 2c9c01f4cc5cd..8e877715b0e97 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7281,6 +7281,8 @@ defm : FlangIgnoredDiagOpt<"target-lifetime">; let Group = sycl_Group in { def fsycl : Flag<["-"], "fsycl">, HelpText<"Enable SYCL C++ extensions">; +def fno_sycl : Flag<["-"], "fno-sycl">, + HelpText<"Disable SYCL C++ extensions">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; def fsycl_host_only : Flag<["-"], "fsycl-host-only">, >From 206277d22b412f93fb034eb069a1b0468f4f4e9d Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Thu, 22 Jan 2026 11:04:51 -0800 Subject: [PATCH 15/15] Remove whitespace. --- clang/include/clang/Options/Options.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 8e877715b0e97..d48ca15864060 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7282,7 +7282,7 @@ let Group = sycl_Group in { def fsycl : Flag<["-"], "fsycl">, HelpText<"Enable SYCL C++ extensions">; def fno_sycl : Flag<["-"], "fno-sycl">, - HelpText<"Disable SYCL C++ extensions">; + HelpText<"Disable SYCL C++ extensions">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">; def fsycl_host_only : Flag<["-"], "fsycl-host-only">, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
