Skip to content

Commit 403ee01

Browse files
authored
[SYCL][Driver] Update devicelib link logic to stop handling deprecated options. (#19592)
We have marked "f[no-]sycl-device-lib=" option as deprecated and will remove in future major release and we also decide to reuse community "no-offloadlib" option to disable all sycl device libraries for debug purpose. This PR updates getDeviceLibraries to get rid of deprecated options and honor "no-offloadlib" while keeping previous legacy logic to handle deprecated options since they haven't been removed. Once users touch the deprecated option, will delegate the handling to legacy "getDeviceLibraries" function. We will remove the legacy function in next major release. --------- Signed-off-by: jinge90 <[email protected]>
1 parent 430a3b3 commit 403ee01

File tree

9 files changed

+127
-15
lines changed

9 files changed

+127
-15
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5959,7 +5959,7 @@ def no_offloadlib
59595959
: Flag<["--"], "no-offloadlib">,
59605960
MarshallingInfoFlag<LangOpts<"NoGPULib">>,
59615961
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
5962-
HelpText<"Do not link device library for CUDA/HIP device compilation">;
5962+
HelpText<"Do not link device library for CUDA/HIP/SYCL device compilation">;
59635963
def offloadlib : Flag<["--"], "offloadlib">,
59645964
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
59655965
HelpText<"Link device libraries for GPU device compilation">;

clang/lib/Driver/Driver.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5647,10 +5647,9 @@ class OffloadingActionBuilder final {
56475647
} else
56485648
FullLinkObjects = LinkObjects;
56495649

5650-
// FIXME: Link all wrapper and fallback device libraries as default,
5651-
// When spv online link is supported by all backends, the fallback
5652-
// device libraries are only needed when current toolchain is using
5653-
// AOT compilation.
5650+
// TODO: spv online link is deprecated and will be removed in the
5651+
// future, need to remove the logic handling jit link when the option
5652+
// is removed in compiler.
56545653
bool SYCLDeviceLibLinked = false;
56555654
Action *NativeCPULib = nullptr;
56565655
if (IsSPIR || IsNVPTX || IsAMDGCN || IsNativeCPU) {

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,7 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT,
434434
const llvm::opt::ArgList &Args = C.getArgs();
435435
enum { JIT = 0, AOT_CPU, AOT_DG2, AOT_PVC };
436436
auto addSingleLibrary = [&](StringRef DeviceLibName) {
437-
SmallString<128> LibName(DeviceLibName);
438-
llvm::sys::path::replace_extension(LibName, LibSuffix);
439-
LibraryList.push_back(Args.MakeArgString(LibName));
437+
LibraryList.push_back(Args.MakeArgString(Twine(DeviceLibName) + LibSuffix));
440438
};
441439

442440
// This function is used to check whether there is only one GPU device
@@ -561,8 +559,13 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT,
561559
}
562560
#endif
563561

564-
SmallVector<std::string, 8>
565-
SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
562+
// Get the list of SYCL device libraries to link with user's device image if
563+
// some deprecated options are used including: -f[no-]sycl-device-lib=xxx,
564+
// -f[no-]sycl-device-lib-jit-link.
565+
// TODO: remove getDeviceLibrariesLegacy when we remove deprecated options
566+
// related to sycl device library link.
567+
static SmallVector<std::string, 8>
568+
getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple,
566569
bool IsSpirvAOT) {
567570
SmallVector<std::string, 8> LibraryList;
568571
const llvm::opt::ArgList &Args = C.getArgs();
@@ -740,6 +743,111 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
740743
return LibraryList;
741744
}
742745

746+
// Get the list of SYCL device libraries to link with user's device image.
747+
SmallVector<std::string, 8>
748+
SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
749+
bool IsSpirvAOT) {
750+
SmallVector<std::string, 8> LibraryList;
751+
const llvm::opt::ArgList &Args = C.getArgs();
752+
if (Args.getLastArg(options::OPT_fsycl_device_lib_EQ,
753+
options::OPT_fno_sycl_device_lib_EQ) ||
754+
Args.getLastArg(options::OPT_fsycl_device_lib_jit_link,
755+
options::OPT_fno_sycl_device_lib_jit_link))
756+
return getDeviceLibrariesLegacy(C, TargetTriple, IsSpirvAOT);
757+
758+
bool NoOffloadLib =
759+
!Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true);
760+
if (TargetTriple.isNVPTX()) {
761+
if (!NoOffloadLib)
762+
LibraryList.push_back(
763+
Args.MakeArgString("devicelib-nvptx64-nvidia-cuda.bc"));
764+
return LibraryList;
765+
}
766+
767+
if (TargetTriple.isAMDGCN()) {
768+
if (!NoOffloadLib)
769+
LibraryList.push_back(
770+
Args.MakeArgString("devicelib-amdgcn-amd-amdhsa.bc"));
771+
return LibraryList;
772+
}
773+
774+
// Ignore no-offloadlib for NativeCPU device library, it provides some
775+
// critical builtins which must be linked with user's device image.
776+
if (TargetTriple.isNativeCPU()) {
777+
LibraryList.push_back(Args.MakeArgString("libsycl-nativecpu_utils.bc"));
778+
return LibraryList;
779+
}
780+
781+
using SYCLDeviceLibsList = SmallVector<StringRef>;
782+
const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt",
783+
"libsycl-complex",
784+
"libsycl-complex-fp64",
785+
"libsycl-cmath",
786+
"libsycl-cmath-fp64",
787+
#if defined(_WIN32)
788+
"libsycl-msvc-math",
789+
#endif
790+
"libsycl-imf",
791+
"libsycl-imf-fp64",
792+
"libsycl-imf-bf16",
793+
"libsycl-fallback-cassert",
794+
"libsycl-fallback-cstring",
795+
"libsycl-fallback-complex",
796+
"libsycl-fallback-complex-fp64",
797+
"libsycl-fallback-cmath",
798+
"libsycl-fallback-cmath-fp64",
799+
"libsycl-fallback-imf",
800+
"libsycl-fallback-imf-fp64",
801+
"libsycl-fallback-imf-bf16"};
802+
bool IsWindowsMSVCEnv =
803+
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
804+
bool IsNewOffload = C.getDriver().getUseNewOffloadingDriver();
805+
StringRef LibSuffix = ".bc";
806+
if (IsNewOffload)
807+
// For new offload model, we use packaged .bc files.
808+
LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o";
809+
auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) {
810+
for (const StringRef &Lib : LibsList) {
811+
LibraryList.push_back(Args.MakeArgString(Twine(Lib) + LibSuffix));
812+
}
813+
};
814+
815+
if (!NoOffloadLib)
816+
addLibraries(SYCLDeviceLibs);
817+
818+
// ITT annotation libraries are linked in separately whenever the device
819+
// code instrumentation is enabled.
820+
const SYCLDeviceLibsList SYCLDeviceAnnotationLibs = {
821+
"libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers",
822+
"libsycl-itt-stubs"};
823+
if (Args.hasFlag(options::OPT_fsycl_instrument_device_code,
824+
options::OPT_fno_sycl_instrument_device_code, true))
825+
addLibraries(SYCLDeviceAnnotationLibs);
826+
827+
const SYCLDeviceLibsList SYCLDeviceBfloat16FallbackLib = {
828+
"libsycl-fallback-bfloat16"};
829+
const SYCLDeviceLibsList SYCLDeviceBfloat16NativeLib = {
830+
"libsycl-native-bfloat16"};
831+
bool NativeBfloatLibs;
832+
bool NeedBfloatLibs = selectBfloatLibs(TargetTriple, C, NativeBfloatLibs);
833+
if (NeedBfloatLibs && !NoOffloadLib) {
834+
// Add native or fallback bfloat16 library.
835+
if (NativeBfloatLibs)
836+
addLibraries(SYCLDeviceBfloat16NativeLib);
837+
else
838+
addLibraries(SYCLDeviceBfloat16FallbackLib);
839+
}
840+
841+
// Currently, device sanitizer support is required by some developers on
842+
// Linux platform only, so compiler only provides device sanitizer libraries
843+
// on Linux platform.
844+
#if !defined(_WIN32)
845+
addSYCLDeviceSanitizerLibs(C, IsSpirvAOT, LibSuffix, LibraryList);
846+
#endif
847+
848+
return LibraryList;
849+
}
850+
743851
/// Reads device config file to find information about the SYCL targets in
744852
/// `Targets`, and defines device traits macros accordingly.
745853
void SYCL::populateSYCLDeviceTraitsMacrosArgs(

clang/test/Driver/sycl-device-lib-amdgcn.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
// Check that llvm-link uses the "-only-needed" flag.
3131
// Not using the flag breaks kernel bundles.
32-
// RUN: %clangxx -### -nogpulib -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL \
33-
// RUN: -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx906 %s 2>&1 \
32+
// RUN: %clangxx -### -fsycl -fsycl-targets=amdgcn-amd-amdhsa -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL \
33+
// RUN: -Xsycl-target-backend --offload-arch=gfx908 --rocm-path=%S/Inputs/rocm %s 2>&1 \
3434
// RUN: | FileCheck -check-prefix=CHK-ONLY-NEEDED %s
3535

3636
// CHK-ONLY-NEEDED: llvm-link"{{.*}}"-only-needed"{{.*}}"{{.*}}devicelib-amdgcn-amd-amdhsa.bc"{{.*}}

clang/test/Driver/sycl-device-lib-nvptx.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
// Check that llvm-link uses the "-only-needed" flag.
3131
// Not using the flag breaks kernel bundles.
32-
// RUN: %clangxx -### -nocudalib -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL -fsycl -fsycl-targets=nvptx64-nvidia-cuda %s 2>&1 \
33-
// RUN: | FileCheck -check-prefix=CHK-ONLY-NEEDED %s
32+
// RUN: %clangxx -### --cuda-path=%S/Inputs/CUDA/usr/local/cuda -fno-sycl-libspirv --sysroot=%S/Inputs/SYCL \
33+
// RUN: -fsycl -fsycl-targets=nvptx64-nvidia-cuda %s 2>&1 | FileCheck -check-prefix=CHK-ONLY-NEEDED %s
3434

3535
// CHK-ONLY-NEEDED: llvm-link"{{.*}}"-only-needed"{{.*}}"{{.*}}devicelib-nvptx64-nvidia-cuda.bc"{{.*}}

clang/test/Driver/sycl-device-lib-old-model.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
134134
// RUN: %clangxx -fsycl --no-offload-new-driver %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL -### 2>&1 \
135135
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
136+
// RUN: %clangxx -fsycl --no-offload-new-driver %s --no-offloadlib --sysroot=%S/Inputs/SYCL -### 2>&1 \
137+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
136138
// SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown"
137139
// SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB-NOT: libsycl-cmath.bc
138140

clang/test/Driver/sycl-device-lib-win.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
134134
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL -### 2>&1 \
135135
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
136+
// RUN: %clangxx -fsycl %s --no-offloadlib --sysroot=%S/Inputs/SYCL -### 2>&1 \
137+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
136138
// SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown"
137139
// SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB-NOT: libsycl-cmath.bc
138140

clang/test/Driver/sycl-device-lib.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
135135
// RUN: %clangxx -fsycl --offload-new-driver %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL -### 2>&1 \
136136
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
137+
// RUN: %clangxx -fsycl --offload-new-driver %s --no-offloadlib --sysroot=%S/Inputs/SYCL -### 2>&1 \
138+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB
137139
// SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown"
138140
// SYCL_DEVICE_LIB_LINK_NO_DEVICE_LIB-NOT: libsycl-cmath.new.o
139141

clang/test/Driver/sycl-nvptx-link.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
// CHECK: llvm-link
4141
// CHECK-SAME: -only-needed
42-
// CHECK-SAME: devicelib-nvptx64-nvidia-cuda.bc
4342
// CHECK-SAME: libspirv-nvptx64-nvidia-cuda.bc
4443
// LIBDEVICE10-SAME: libdevice.10.bc
4544
// LIBDEVICE30-SAME: libdevice.compute_30.10.bc

0 commit comments

Comments
 (0)