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/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 7aa5f2f3bbaf8..631931bec423b 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -5647,10 +5647,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 ef9c953d86b18..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 @@ -561,8 +559,13 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT, } #endif -SmallVector -SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, +// 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 +getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT) { SmallVector LibraryList; const llvm::opt::ArgList &Args = C.getArgs(); @@ -740,6 +743,111 @@ SYCL::getDeviceLibraries(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) { + 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) { + LibraryList.push_back(Args.MakeArgString(Twine(Lib) + LibSuffix)); + } + }; + + 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); + } + + // Currently, device sanitizer support is required by some developers on + // Linux platform only, so compiler only provides device sanitizer libraries + // on Linux platform. +#if !defined(_WIN32) + addSYCLDeviceSanitizerLibs(C, IsSpirvAOT, LibSuffix, LibraryList); +#endif + + 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/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-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 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