From 04ec90be5ccb7037e0eb800e96b8329d4db45b5f Mon Sep 17 00:00:00 2001 From: jinge90 Date: Fri, 25 Jul 2025 15:06:01 +0800 Subject: [PATCH 1/9] [SYCL][Driver] Update devicelib link logic to get rid of deprecated option Signed-off-by: jinge90 --- clang/lib/Driver/Driver.cpp | 7 +++---- clang/lib/Driver/ToolChains/SYCL.cpp | 30 ++++++++++++++++++++++++++++ clang/lib/Driver/ToolChains/SYCL.h | 4 ++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index cae629698fac6..725c7399eb0a1 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -5689,10 +5689,9 @@ class OffloadingActionBuilder final { } else FullLinkObjects = LinkObjects; - // FIXME: Link all wrapper and fallback device libraries as default, - // When spv online link is supported by all backends, the fallback - // device libraries are only needed when current toolchain is using - // AOT compilation. + // TODO: spv online link is deprecated and will be removed in the + // future, need to remove the logic handling jit link when the option + // is removed in compiler. bool SYCLDeviceLibLinked = false; Action *NativeCPULib = nullptr; if (IsSPIR || IsNVPTX || IsAMDGCN || IsNativeCPU) { diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index e70c35a4f526b..a6136819b5b25 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -555,6 +555,36 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT) { SmallVector LibraryList; const llvm::opt::ArgList &Args = C.getArgs(); + if (Args.getLastArg(options::OPT_fsycl_device_lib_EQ, + options::OPT_fno_sycl_device_lib_EQ) || + Args.getLastArg(options::OPT_fsycl_device_lib_jit_link, + options::OPT_fno_sycl_device_lib_jit_link)) + return getDeviceLibrariesLegacy(C, TargetTriple, IsSpirvAOT); + + bool NoOffloadLib = + !Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true); + if (TargetTriple.isNVPTX()) { + if (!NoOffloadLib) + LibraryList.push_back( + Args.MakeArgString("devicelib-nvptx64-nvidia-cuda.bc")); + return LibraryList; + } + + if (TargetTriple.isAMDGCN()) { + if (!NoOffloadLib) + LibraryList.push_back( + Args.MakeArgString("devicelib-amdgcn-amd-amdhsa.bc")); + return LibraryList; + } + return LibraryList; +} + +// TODO: remove getDeviceLibrariesLegacy when we remove deprecated options +// related to sycl device library link. +SmallVector SYCL::getDeviceLibrariesLegacy( + const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT) { + SmallVector LibraryList; + const llvm::opt::ArgList &Args = C.getArgs(); // For NVPTX and AMDGCN we only use one single bitcode library and ignore // manually specified SYCL device libraries. diff --git a/clang/lib/Driver/ToolChains/SYCL.h b/clang/lib/Driver/ToolChains/SYCL.h index 7e3b4d0aca5bd..83955bd887664 100644 --- a/clang/lib/Driver/ToolChains/SYCL.h +++ b/clang/lib/Driver/ToolChains/SYCL.h @@ -173,6 +173,10 @@ SmallVector getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT); +SmallVector +getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple, + bool IsSpirvAOT); + // Populates the SYCL device traits macros. void populateSYCLDeviceTraitsMacrosArgs( Compilation &C, const llvm::opt::ArgList &Args, From b1e3af2b32133997b4989fdbcbc4e2830937e917 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Fri, 25 Jul 2025 17:03:42 +0800 Subject: [PATCH 2/9] link all sycl device libraries by default Signed-off-by: jinge90 --- clang/lib/Driver/ToolChains/SYCL.cpp | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index a6136819b5b25..18412d0a64259 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -576,6 +576,61 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, Args.MakeArgString("devicelib-amdgcn-amd-amdhsa.bc")); return LibraryList; } + + using SYCLDeviceLibsList = SmallVector; + const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt", + "libsycl-complex", + "libsycl-complex-fp64", + "libsycl-cmath", + "libsycl-cmath-fp64", +#if defined(_WIN32) + "libsycl-msvc-math", +#endif + "libsycl-imf", + "libsycl-imf-fp64", + "libsycl-imf-bf16", + "libsycl-fallback-cassert", + "libsycl-fallback-cstring", + "libsycl-fallback-complex", + "libsycl-fallback-complex-fp64", + "libsycl-fallback-cmath", + "libsycl-fallback-cmath-fp64", + "libsycl-fallback-imf", + "libsycl-fallback-imf-fp64", + "libsycl-fallback-imf-bf16"}; + bool IsWindowsMSVCEnv = + C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); + bool IsNewOffload = C.getDriver().getUseNewOffloadingDriver(); + StringRef LibSuffix = ".bc"; + if (IsNewOffload) + // For new offload model, we use packaged .bc files. + LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o"; + auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) { + for (const StringRef &Lib : LibsList) { + SmallString<128> LibName(Lib); + llvm::sys::path::replace_extension(LibName, LibSuffix); + LibraryList.push_back(Args.MakeArgString(LibName)); + } + }; + + // nativecpu only needs libsycl-nativecpu_utils. + const SYCLDeviceLibsList SYCLNativeCpuDeviceLibs = { + "libsycl-nativecpu_utils"}; + if (TargetTriple.isNativeCPU()) { + addLibraries(SYCLNativeCpuDeviceLibs); + return LibraryList; + } + + if (!NoOffloadLib) + addLibraries(SYCLDeviceLibs); + + // ITT annotation libraries are linked in separately whenever the device + // code instrumentation is enabled. + const SYCLDeviceLibsList SYCLDeviceAnnotationLibs = { + "libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers", + "libsycl-itt-stubs"}; + addLibraries(SYCLDeviceAnnotationLibs); + return LibraryList; } From 636ec9d1e479e7b38ef87a79c9c760297f595be7 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Fri, 15 Aug 2025 14:21:06 +0800 Subject: [PATCH 3/9] Always link nativecpu utils and link itt according to options Signed-off-by: jinge90 --- clang/lib/Driver/ToolChains/SYCL.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index b034ef5585ace..0b41591a25165 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -453,6 +453,13 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, return LibraryList; } + // Ignore no-offloadlib for NativeCPU device library, it provides some + // critical builtins which must be linked with user's device image. + if (TargetTriple.isNativeCPU()) { + LibraryList.push_back(Args.MakeArgString("libsycl-nativecpu_utils.bc")); + return LibraryList; + } + using SYCLDeviceLibsList = SmallVector; const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt", "libsycl-complex", @@ -489,14 +496,6 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, } }; - // nativecpu only needs libsycl-nativecpu_utils. - const SYCLDeviceLibsList SYCLNativeCpuDeviceLibs = { - "libsycl-nativecpu_utils"}; - if (TargetTriple.isNativeCPU()) { - addLibraries(SYCLNativeCpuDeviceLibs); - return LibraryList; - } - if (!NoOffloadLib) addLibraries(SYCLDeviceLibs); @@ -505,7 +504,9 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, const SYCLDeviceLibsList SYCLDeviceAnnotationLibs = { "libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers", "libsycl-itt-stubs"}; - addLibraries(SYCLDeviceAnnotationLibs); + if (Args.hasFlag(options::OPT_fsycl_instrument_device_code, + options::OPT_fno_sycl_instrument_device_code, true)) + addLibraries(SYCLDeviceAnnotationLibs); return LibraryList; } From f0ab62d997bf5f8fbcae2e660d182238b64b1d8b Mon Sep 17 00:00:00 2001 From: jinge90 Date: Fri, 15 Aug 2025 14:46:22 +0800 Subject: [PATCH 4/9] Select bfloat16 devicelib Signed-off-by: jinge90 --- clang/lib/Driver/ToolChains/SYCL.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index 0b41591a25165..7b0923255f3cd 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -508,6 +508,20 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, options::OPT_fno_sycl_instrument_device_code, true)) addLibraries(SYCLDeviceAnnotationLibs); + const SYCLDeviceLibsList SYCLDeviceBfloat16FallbackLib = { + "libsycl-fallback-bfloat16"}; + const SYCLDeviceLibsList SYCLDeviceBfloat16NativeLib = { + "libsycl-native-bfloat16"}; + bool NativeBfloatLibs; + bool NeedBfloatLibs = selectBfloatLibs(TargetTriple, C, NativeBfloatLibs); + if (NeedBfloatLibs && !NoOffloadLib) { + // Add native or fallback bfloat16 library. + if (NativeBfloatLibs) + addLibraries(SYCLDeviceBfloat16NativeLib); + else + addLibraries(SYCLDeviceBfloat16FallbackLib); + } + return LibraryList; } From ee5bcac8c5b5200cdbca6d501450e2780e6ea224 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Fri, 15 Aug 2025 15:00:30 +0800 Subject: [PATCH 5/9] make legacy API as static Signed-off-by: jinge90 --- clang/lib/Driver/ToolChains/SYCL.cpp | 203 ++++++++++++++------------- clang/lib/Driver/ToolChains/SYCL.h | 4 - 2 files changed, 102 insertions(+), 105 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index 7b0923255f3cd..b06d7d6c658e6 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -426,109 +426,11 @@ static bool checkPVCDevice(std::string SingleArg, std::string &DevArg) { return false; } -SmallVector -SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, - bool IsSpirvAOT) { - SmallVector LibraryList; - const llvm::opt::ArgList &Args = C.getArgs(); - if (Args.getLastArg(options::OPT_fsycl_device_lib_EQ, - options::OPT_fno_sycl_device_lib_EQ) || - Args.getLastArg(options::OPT_fsycl_device_lib_jit_link, - options::OPT_fno_sycl_device_lib_jit_link)) - return getDeviceLibrariesLegacy(C, TargetTriple, IsSpirvAOT); - - bool NoOffloadLib = - !Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true); - if (TargetTriple.isNVPTX()) { - if (!NoOffloadLib) - LibraryList.push_back( - Args.MakeArgString("devicelib-nvptx64-nvidia-cuda.bc")); - return LibraryList; - } - - if (TargetTriple.isAMDGCN()) { - if (!NoOffloadLib) - LibraryList.push_back( - Args.MakeArgString("devicelib-amdgcn-amd-amdhsa.bc")); - return LibraryList; - } - - // Ignore no-offloadlib for NativeCPU device library, it provides some - // critical builtins which must be linked with user's device image. - if (TargetTriple.isNativeCPU()) { - LibraryList.push_back(Args.MakeArgString("libsycl-nativecpu_utils.bc")); - return LibraryList; - } - - using SYCLDeviceLibsList = SmallVector; - const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt", - "libsycl-complex", - "libsycl-complex-fp64", - "libsycl-cmath", - "libsycl-cmath-fp64", -#if defined(_WIN32) - "libsycl-msvc-math", -#endif - "libsycl-imf", - "libsycl-imf-fp64", - "libsycl-imf-bf16", - "libsycl-fallback-cassert", - "libsycl-fallback-cstring", - "libsycl-fallback-complex", - "libsycl-fallback-complex-fp64", - "libsycl-fallback-cmath", - "libsycl-fallback-cmath-fp64", - "libsycl-fallback-imf", - "libsycl-fallback-imf-fp64", - "libsycl-fallback-imf-bf16"}; - bool IsWindowsMSVCEnv = - C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); - bool IsNewOffload = C.getDriver().getUseNewOffloadingDriver(); - StringRef LibSuffix = ".bc"; - if (IsNewOffload) - // For new offload model, we use packaged .bc files. - LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o"; - auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) { - for (const StringRef &Lib : LibsList) { - SmallString<128> LibName(Lib); - llvm::sys::path::replace_extension(LibName, LibSuffix); - LibraryList.push_back(Args.MakeArgString(LibName)); - } - }; - - if (!NoOffloadLib) - addLibraries(SYCLDeviceLibs); - - // ITT annotation libraries are linked in separately whenever the device - // code instrumentation is enabled. - const SYCLDeviceLibsList SYCLDeviceAnnotationLibs = { - "libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers", - "libsycl-itt-stubs"}; - if (Args.hasFlag(options::OPT_fsycl_instrument_device_code, - options::OPT_fno_sycl_instrument_device_code, true)) - addLibraries(SYCLDeviceAnnotationLibs); - - const SYCLDeviceLibsList SYCLDeviceBfloat16FallbackLib = { - "libsycl-fallback-bfloat16"}; - const SYCLDeviceLibsList SYCLDeviceBfloat16NativeLib = { - "libsycl-native-bfloat16"}; - bool NativeBfloatLibs; - bool NeedBfloatLibs = selectBfloatLibs(TargetTriple, C, NativeBfloatLibs); - if (NeedBfloatLibs && !NoOffloadLib) { - // Add native or fallback bfloat16 library. - if (NativeBfloatLibs) - addLibraries(SYCLDeviceBfloat16NativeLib); - else - addLibraries(SYCLDeviceBfloat16FallbackLib); - } - - return LibraryList; -} - // TODO: remove getDeviceLibrariesLegacy when we remove deprecated options // related to sycl device library link. -SmallVector SYCL::getDeviceLibrariesLegacy( - const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT) { +static SmallVector +getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple, + bool IsSpirvAOT) { SmallVector LibraryList; const llvm::opt::ArgList &Args = C.getArgs(); @@ -837,6 +739,105 @@ SmallVector SYCL::getDeviceLibrariesLegacy( return LibraryList; } +SmallVector +SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, + bool IsSpirvAOT) { + SmallVector LibraryList; + const llvm::opt::ArgList &Args = C.getArgs(); + if (Args.getLastArg(options::OPT_fsycl_device_lib_EQ, + options::OPT_fno_sycl_device_lib_EQ) || + Args.getLastArg(options::OPT_fsycl_device_lib_jit_link, + options::OPT_fno_sycl_device_lib_jit_link)) + return getDeviceLibrariesLegacy(C, TargetTriple, IsSpirvAOT); + + bool NoOffloadLib = + !Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true); + if (TargetTriple.isNVPTX()) { + if (!NoOffloadLib) + LibraryList.push_back( + Args.MakeArgString("devicelib-nvptx64-nvidia-cuda.bc")); + return LibraryList; + } + + if (TargetTriple.isAMDGCN()) { + if (!NoOffloadLib) + LibraryList.push_back( + Args.MakeArgString("devicelib-amdgcn-amd-amdhsa.bc")); + return LibraryList; + } + + // Ignore no-offloadlib for NativeCPU device library, it provides some + // critical builtins which must be linked with user's device image. + if (TargetTriple.isNativeCPU()) { + LibraryList.push_back(Args.MakeArgString("libsycl-nativecpu_utils.bc")); + return LibraryList; + } + + using SYCLDeviceLibsList = SmallVector; + const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt", + "libsycl-complex", + "libsycl-complex-fp64", + "libsycl-cmath", + "libsycl-cmath-fp64", +#if defined(_WIN32) + "libsycl-msvc-math", +#endif + "libsycl-imf", + "libsycl-imf-fp64", + "libsycl-imf-bf16", + "libsycl-fallback-cassert", + "libsycl-fallback-cstring", + "libsycl-fallback-complex", + "libsycl-fallback-complex-fp64", + "libsycl-fallback-cmath", + "libsycl-fallback-cmath-fp64", + "libsycl-fallback-imf", + "libsycl-fallback-imf-fp64", + "libsycl-fallback-imf-bf16"}; + bool IsWindowsMSVCEnv = + C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); + bool IsNewOffload = C.getDriver().getUseNewOffloadingDriver(); + StringRef LibSuffix = ".bc"; + if (IsNewOffload) + // For new offload model, we use packaged .bc files. + LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o"; + auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) { + for (const StringRef &Lib : LibsList) { + SmallString<128> LibName(Lib); + llvm::sys::path::replace_extension(LibName, LibSuffix); + LibraryList.push_back(Args.MakeArgString(LibName)); + } + }; + + if (!NoOffloadLib) + addLibraries(SYCLDeviceLibs); + + // ITT annotation libraries are linked in separately whenever the device + // code instrumentation is enabled. + const SYCLDeviceLibsList SYCLDeviceAnnotationLibs = { + "libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers", + "libsycl-itt-stubs"}; + if (Args.hasFlag(options::OPT_fsycl_instrument_device_code, + options::OPT_fno_sycl_instrument_device_code, true)) + addLibraries(SYCLDeviceAnnotationLibs); + + const SYCLDeviceLibsList SYCLDeviceBfloat16FallbackLib = { + "libsycl-fallback-bfloat16"}; + const SYCLDeviceLibsList SYCLDeviceBfloat16NativeLib = { + "libsycl-native-bfloat16"}; + bool NativeBfloatLibs; + bool NeedBfloatLibs = selectBfloatLibs(TargetTriple, C, NativeBfloatLibs); + if (NeedBfloatLibs && !NoOffloadLib) { + // Add native or fallback bfloat16 library. + if (NativeBfloatLibs) + addLibraries(SYCLDeviceBfloat16NativeLib); + else + addLibraries(SYCLDeviceBfloat16FallbackLib); + } + + return LibraryList; +} + /// Reads device config file to find information about the SYCL targets in /// `Targets`, and defines device traits macros accordingly. void SYCL::populateSYCLDeviceTraitsMacrosArgs( diff --git a/clang/lib/Driver/ToolChains/SYCL.h b/clang/lib/Driver/ToolChains/SYCL.h index a04817a9a0fc5..c1403ae1d5036 100644 --- a/clang/lib/Driver/ToolChains/SYCL.h +++ b/clang/lib/Driver/ToolChains/SYCL.h @@ -39,10 +39,6 @@ SmallVector getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT); -SmallVector -getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple, - bool IsSpirvAOT); - // Populates the SYCL device traits macros. void populateSYCLDeviceTraitsMacrosArgs( Compilation &C, const llvm::opt::ArgList &Args, From 0c966482c6949f401a5747c07833811c1368c841 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Thu, 21 Aug 2025 13:59:07 +0800 Subject: [PATCH 6/9] fix lit failure Signed-off-by: jinge90 --- clang/test/Driver/sycl-device-lib-amdgcn.cpp | 4 ++-- clang/test/Driver/sycl-device-lib-nvptx.cpp | 4 ++-- clang/test/Driver/sycl-nvptx-link.cpp | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/clang/test/Driver/sycl-device-lib-amdgcn.cpp b/clang/test/Driver/sycl-device-lib-amdgcn.cpp index 0cf7005771c75..1eb6616df9c1e 100644 --- a/clang/test/Driver/sycl-device-lib-amdgcn.cpp +++ b/clang/test/Driver/sycl-device-lib-amdgcn.cpp @@ -29,8 +29,8 @@ // Check that llvm-link uses the "-only-needed" flag. // Not using the flag breaks kernel bundles. -// RUN: %clangxx -### -nogpulib -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL \ -// RUN: -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx906 %s 2>&1 \ +// RUN: %clangxx -### -fsycl -fsycl-targets=amdgcn-amd-amdhsa -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL \ +// RUN: -Xsycl-target-backend --offload-arch=gfx908 --rocm-path=%S/Inputs/rocm %s 2>&1 \ // RUN: | FileCheck -check-prefix=CHK-ONLY-NEEDED %s // CHK-ONLY-NEEDED: llvm-link"{{.*}}"-only-needed"{{.*}}"{{.*}}devicelib-amdgcn-amd-amdhsa.bc"{{.*}} diff --git a/clang/test/Driver/sycl-device-lib-nvptx.cpp b/clang/test/Driver/sycl-device-lib-nvptx.cpp index a063d16de5c1d..c7c25c5ff6624 100644 --- a/clang/test/Driver/sycl-device-lib-nvptx.cpp +++ b/clang/test/Driver/sycl-device-lib-nvptx.cpp @@ -29,7 +29,7 @@ // Check that llvm-link uses the "-only-needed" flag. // Not using the flag breaks kernel bundles. -// RUN: %clangxx -### -nocudalib -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL -fsycl -fsycl-targets=nvptx64-nvidia-cuda %s 2>&1 \ -// RUN: | FileCheck -check-prefix=CHK-ONLY-NEEDED %s +// RUN: %clangxx -### --cuda-path=%S/Inputs/CUDA/usr/local/cuda -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL \ +// RUN: -fsycl -fsycl-targets=nvptx64-nvidia-cuda %s 2>&1 | FileCheck -check-prefix=CHK-ONLY-NEEDED %s // CHK-ONLY-NEEDED: llvm-link"{{.*}}"-only-needed"{{.*}}"{{.*}}devicelib-nvptx64-nvidia-cuda.bc"{{.*}} diff --git a/clang/test/Driver/sycl-nvptx-link.cpp b/clang/test/Driver/sycl-nvptx-link.cpp index 229fafcb23140..2e0650ada8b23 100644 --- a/clang/test/Driver/sycl-nvptx-link.cpp +++ b/clang/test/Driver/sycl-nvptx-link.cpp @@ -39,7 +39,6 @@ // CHECK: llvm-link // CHECK-SAME: -only-needed -// CHECK-SAME: devicelib-nvptx64-nvidia-cuda.bc // CHECK-SAME: libspirv-nvptx64-nvidia-cuda.bc // LIBDEVICE10-SAME: libdevice.10.bc // LIBDEVICE30-SAME: libdevice.compute_30.10.bc From 9c0ce3fe10ff9223ccc31988e683d753966cc173 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Thu, 21 Aug 2025 14:35:59 +0800 Subject: [PATCH 7/9] add lit test for --no-offloadlib Signed-off-by: jinge90 --- clang/include/clang/Driver/Options.td | 2 +- clang/test/Driver/sycl-device-lib-old-model.cpp | 2 ++ clang/test/Driver/sycl-device-lib-win.cpp | 2 ++ clang/test/Driver/sycl-device-lib.cpp | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 206f0d8b8fda7..4158599394cd3 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5959,7 +5959,7 @@ def no_offloadlib : Flag<["--"], "no-offloadlib">, MarshallingInfoFlag>, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>, - HelpText<"Do not link device library for CUDA/HIP device compilation">; + HelpText<"Do not link device library for CUDA/HIP/SYCL device compilation">; def offloadlib : Flag<["--"], "offloadlib">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>, HelpText<"Link device libraries for GPU device compilation">; diff --git a/clang/test/Driver/sycl-device-lib-old-model.cpp b/clang/test/Driver/sycl-device-lib-old-model.cpp index ffc752d73abd1..b7b85d9a07d05 100644 --- a/clang/test/Driver/sycl-device-lib-old-model.cpp +++ b/clang/test/Driver/sycl-device-lib-old-model.cpp @@ -133,6 +133,8 @@ // RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB // RUN: %clangxx -fsycl --no-offload-new-driver %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL -### 2>&1 \ // RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB +// RUN: %clangxx -fsycl --no-offload-new-driver %s --no-offloadlib --sysroot=%S/Inputs/SYCL -### 2>&1 \ +// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB // SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown" // SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB-NOT: libsycl-cmath.bc diff --git a/clang/test/Driver/sycl-device-lib-win.cpp b/clang/test/Driver/sycl-device-lib-win.cpp index 3f7267e017efa..65cbc111526ae 100644 --- a/clang/test/Driver/sycl-device-lib-win.cpp +++ b/clang/test/Driver/sycl-device-lib-win.cpp @@ -133,6 +133,8 @@ // RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB // RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL -### 2>&1 \ // RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB +// RUN: %clangxx -fsycl %s --no-offloadlib --sysroot=%S/Inputs/SYCL -### 2>&1 \ +// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB // SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown" // SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB-NOT: libsycl-cmath.bc diff --git a/clang/test/Driver/sycl-device-lib.cpp b/clang/test/Driver/sycl-device-lib.cpp index fd59997afda4b..da0ba26b270dd 100644 --- a/clang/test/Driver/sycl-device-lib.cpp +++ b/clang/test/Driver/sycl-device-lib.cpp @@ -134,6 +134,8 @@ // RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB // RUN: %clangxx -fsycl --offload-new-driver %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL -### 2>&1 \ // RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB +// RUN: %clangxx -fsycl --offload-new-driver %s --no-offloadlib --sysroot=%S/Inputs/SYCL -### 2>&1 \ +// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB // SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown" // SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB-NOT: libsycl-cmath.new.o From 8d49f7e846107a53fb305b392403af3fc373ddf0 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Tue, 26 Aug 2025 16:12:54 +0800 Subject: [PATCH 8/9] Remove unnecessary string copy Signed-off-by: jinge90 --- clang/lib/Driver/ToolChains/SYCL.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index ddd595a32c2a5..7ad5eca7fac02 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -561,6 +561,9 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT, } #endif +// Get the list of SYCL device libraries to link with user's device image if +// some deprecated options are used including: -f[no-]sycl-device-lib=xxx, +// -f[no-]sycl-device-lib-jit-link. // TODO: remove getDeviceLibrariesLegacy when we remove deprecated options // related to sycl device library link. static SmallVector @@ -742,6 +745,7 @@ getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple, return LibraryList; } +// Get the list of SYCL device libraries to link with user's device image. SmallVector SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT) { @@ -776,7 +780,7 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, return LibraryList; } - using SYCLDeviceLibsList = SmallVector; + using SYCLDeviceLibsList = SmallVector; const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt", "libsycl-complex", "libsycl-complex-fp64", @@ -806,9 +810,7 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o"; auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) { for (const StringRef &Lib : LibsList) { - SmallString<128> LibName(Lib); - llvm::sys::path::replace_extension(LibName, LibSuffix); - LibraryList.push_back(Args.MakeArgString(LibName)); + LibraryList.push_back(Args.MakeArgString(Twine(Lib) + LibSuffix)); } }; From a79ed5d5928f04d0030c2f8bbf6ffec952643c79 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Tue, 26 Aug 2025 16:19:26 +0800 Subject: [PATCH 9/9] remove redundant string create Signed-off-by: jinge90 --- clang/lib/Driver/ToolChains/SYCL.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index 7ad5eca7fac02..8cba3664aae63 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -434,9 +434,7 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT, const llvm::opt::ArgList &Args = C.getArgs(); enum { JIT = 0, AOT_CPU, AOT_DG2, AOT_PVC }; auto addSingleLibrary = [&](StringRef DeviceLibName) { - SmallString<128> LibName(DeviceLibName); - llvm::sys::path::replace_extension(LibName, LibSuffix); - LibraryList.push_back(Args.MakeArgString(LibName)); + LibraryList.push_back(Args.MakeArgString(Twine(DeviceLibName) + LibSuffix)); }; // This function is used to check whether there is only one GPU device