Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5959,7 +5959,7 @@ def no_offloadlib
: Flag<["--"], "no-offloadlib">,
MarshallingInfoFlag<LangOpts<"NoGPULib">>,
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">;
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
118 changes: 113 additions & 5 deletions clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -561,8 +559,13 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT,
}
#endif

SmallVector<std::string, 8>
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<std::string, 8>
getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment describing what this functions does and why it is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Thanks very much.

bool IsSpirvAOT) {
SmallVector<std::string, 8> LibraryList;
const llvm::opt::ArgList &Args = C.getArgs();
Expand Down Expand Up @@ -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<std::string, 8>
SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a quick note/comment about what this function does and a description of the edge cases if applicable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Thanks very much.

bool IsSpirvAOT) {
SmallVector<std::string, 8> 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<StringRef>;
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(
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Driver/sycl-device-lib-amdgcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is --offload-arch value changed from gfx906 to gfx908 and -nogpulib removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @srividya-sundaram
Previously, the test didn't add --rocm-path, so the test would run fail on machines without AMD toolchain and reported some amd device rtl files were missing. To workaround this, "nogpulib" was added and this option wouldn't impact sycl device libraries link at that time.
Now, we reuse community "--no-offloadlib" option to disable all sycl device libraries and "nogpulib" is an alias for "--no-offloadlib", so we must remove this option otherwise all sycl device libraries will be disabled and the test will fail.
After removing "nogpulib", we need to specify --rocm-path to indicate where to find the required amd device rtl files and current syclos only have dummy amd device rtl files for gfx908, so we have to change offload-arch to gfx908 otherwise the test still reported error that amd device rtl files are missing.
Thanks very much.

// RUN: | FileCheck -check-prefix=CHK-ONLY-NEEDED %s

// CHK-ONLY-NEEDED: llvm-link"{{.*}}"-only-needed"{{.*}}"{{.*}}devicelib-amdgcn-amd-amdhsa.bc"{{.*}}
4 changes: 2 additions & 2 deletions clang/test/Driver/sycl-device-lib-nvptx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"{{.*}}
2 changes: 2 additions & 0 deletions clang/test/Driver/sycl-device-lib-old-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions clang/test/Driver/sycl-device-lib-win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions clang/test/Driver/sycl-device-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion clang/test/Driver/sycl-nvptx-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading