Skip to content

[SYCL][Driver][libdevice] Link libspirv in -fsycl-device-only mode #19136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: sycl
Choose a base branch
from
Draft
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
13 changes: 0 additions & 13 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,19 +1567,6 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
}
}

// -fno-sycl-libspirv flag is reserved for very unusual cases where the
// libspirv library is not linked when using CUDA/HIP: so output appropriate
// warnings.
if (C.getInputArgs().hasArg(options::OPT_fno_sycl_libspirv)) {
for (auto &TT : UniqueSYCLTriplesVec) {
if (TT.isNVPTX() || TT.isAMDGCN()) {
Diag(diag::warn_flag_no_sycl_libspirv) << TT.getTriple();
continue;
}
Diag(diag::warn_drv_unsupported_option_for_target)
<< "-fno-sycl-libspirv" << TT.getTriple() << 0;
}
}
// -fsycl-fp64-conv-emu is valid only for AOT compilation with an Intel GPU
// target. For other scenarios, we emit a warning message.
if (C.getInputArgs().hasArg(options::OPT_fsycl_fp64_conv_emu)) {
Expand Down
29 changes: 16 additions & 13 deletions clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,9 @@ const char *SYCLInstallationDetector::findLibspirvPath(
const SmallString<64> Basename =
getLibSpirvBasename(DeviceTriple, HostTriple);
auto searchAt = [&](StringRef Path, const Twine &a = "", const Twine &b = "",
const Twine &c = "", const Twine &d = "",
const Twine &e = "") -> const char * {
const Twine &c = "") -> const char * {
SmallString<128> LibraryPath(Path);
llvm::sys::path::append(LibraryPath, a, b, c, d);
llvm::sys::path::append(LibraryPath, e, Basename);
llvm::sys::path::append(LibraryPath, a, b, c, Basename);

if (Args.hasArgNoClaim(options::OPT__HASH_HASH_HASH) ||
llvm::sys::fs::exists(LibraryPath))
Expand All @@ -195,24 +193,29 @@ const char *SYCLInstallationDetector::findLibspirvPath(
return nullptr;
};

// Otherwise, assume libclc is installed at the same prefix as clang
// Expected path w/out install.
if (const char *R = searchAt(D.ResourceDir, "..", "..", "clc"))
return R;
for (const auto &IC : InstallationCandidates) {
// Expected path w/out install.
if (const char *R = searchAt(IC, "lib", "clc"))
return R;

// Expected path w/ install.
if (const char *R = searchAt(D.ResourceDir, "..", "..", "..", "share", "clc"))
return R;
// Expected path w/ install.
if (const char *R = searchAt(IC, "share", "clc"))
return R;
}

return nullptr;
}

void SYCLInstallationDetector::addLibspirvLinkArgs(
const llvm::Triple &DeviceTriple, const llvm::opt::ArgList &DriverArgs,
const llvm::Triple &HostTriple, llvm::opt::ArgStringList &CC1Args) const {
if (DriverArgs.hasArg(options::OPT_fno_sycl_libspirv) ||
D.offloadDeviceOnly())
if (DriverArgs.hasArg(options::OPT_fno_sycl_libspirv)) {
// -fno-sycl-libspirv flag is reserved for very unusual cases where the
// libspirv library is not linked when required by the device: so output appropriate
// warnings.
D.Diag(diag::warn_flag_no_sycl_libspirv) << DeviceTriple.str();
return;
}

if (const char *LibSpirvFile =
findLibspirvPath(DeviceTriple, DriverArgs, HostTriple)) {
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions clang/test/Driver/sycl-fno-libspirv-warn.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Test that appropriate warnings are output when -fno-sycl-libspirv is used.

// RUN: not %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda,amdgcn-amd-amdhsa -fno-sycl-libspirv %s -### 2>&1 | FileCheck %s
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda,amdgcn-amd-amdhsa -Xsycl-target-backend=amdgcn-amd-amdhsa --offload-arch=gfx908 -nogpulib -fno-sycl-libspirv %s -### 2>&1 | FileCheck %s
// CHECK-DAG: warning: '-fno-sycl-libspirv' should not be used with target 'nvptx64-nvidia-cuda'; libspirv is required for correct behavior [-Wunsafe-libspirv-not-linked]
// CHECK-DAG: warning: '-fno-sycl-libspirv' should not be used with target 'amdgcn-amd-amdhsa'; libspirv is required for correct behavior [-Wunsafe-libspirv-not-linked]
// RUN: %clangxx -fsycl -fsycl-targets=spir64-unknown-unknown -fno-sycl-libspirv %s -### 2>&1 | FileCheck --check-prefix=CHECK-SPIR64 %s
// CHECK-SPIR64: ignoring '-fno-sycl-libspirv' option as it is not currently supported for target 'spir64-unknown-unknown' [-Woption-ignored]
// CHECK-SPIR64: argument unused during compilation: '-fno-sycl-libspirv' [-Wunused-command-line-argument]
25 changes: 12 additions & 13 deletions clang/test/Driver/sycl-libspirv-toolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,36 @@
// RUN: | FileCheck %s --check-prefixes=CHECK-LINUX
// CHECK-LINUX: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "{{.*[\\/]}}remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda.bc"
//
// RUN: %clang -### -resource-dir %{resource_dir} -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 -nogpulib -target x86_64-unknown-windows-msvc %s 2>&1 \
// RUN: %clang -### -ccc-install-dir %{install_dir} -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 -nogpulib -target x86_64-unknown-windows-msvc %s 2>&1 \
// RUN: | FileCheck %s --check-prefixes=CHECK-AMDGCN-WINDOWS
// CHECK-AMDGCN-WINDOWS: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "{{.*[\\/]}}remangled-l32-signed_char.libspirv-amdgcn-amd-amdhsa.bc"
//
// RUN: %clang -### -fsycl -fsycl-device-only -fsycl-targets=nvptx64-nvidia-cuda -nocudalib %s 2>&1 \
// RUN: | FileCheck %s --check-prefixes=CHECK-DEVICE-ONLY
// CHECK-DEVICE-ONLY: "-cc1"{{.*}} "-fsycl-is-device"
// CHECK-DEVICE-ONLY-NOT: "-mlink-builtin-bitcode" "{{.*}}.libspirv-{{.*}}.bc"
// CHECK-DEVICE-ONLY: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "{{.*[\\/]}}remangled-{{.*}}.libspirv-nvptx64-nvidia-cuda.bc"
//
// Only link libspirv in SYCL language mode, but `-fno-sycl-libspirv` does not result in a warning
// Only link libspirv in SYCL language mode, `-fno-sycl-libspirv` should result in a warning
// RUN: %clang -### -x cu -fno-sycl-libspirv -nocudainc -nocudalib %s 2>&1 | FileCheck %s --check-prefixes=CHECK-CUDA
// CHECK-CUDA-NOT: warning: argument unused during compilation: '-fno-sycl-libspirv' [-Wunused-command-line-argument]
// CHECK-CUDA: warning: argument unused during compilation: '-fno-sycl-libspirv' [-Wunused-command-line-argument]
// CHECK-CUDA: "-cc1"{{.*}} "-fcuda-is-device"
// CHECK-CUDA-NOT: "-mlink-builtin-bitcode" "{{.*}}.libspirv-{{.*}}.bc"
//
// The path to the remangled libspirv bitcode file is determined by the resource directory.
// The path to the remangled libspirv bitcode file is determined by the installation directory
// RUN: %clang -### -ccc-install-dir %{install_dir} -resource-dir %{resource_dir} -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib %s 2>&1 \
// RUN: | FileCheck %s -DINSTALL_DIR=%{install_dir} -DRESOURCE_DIR=%{resource_dir} --check-prefixes=CHECK-DIR
// CHECK-DIR: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "[[RESOURCE_DIR]]{{.*[\\/]}}remangled-{{.*}}.libspirv-nvptx64-nvidia-cuda.bc"
// CHECK-DIR: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "[[INSTALL_DIR]]{{.*[\\/]}}remangled-{{.*}}.libspirv-nvptx64-nvidia-cuda.bc"
//
// The `-###` option disables file existence checks
// DEFINE: %{nonexistent_dir} = %/S/Inputs/SYCL/does_not_exist/lib/clang/resource_dir
// RUN: %clang -### -resource-dir %{nonexistent_dir} -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib %s 2>&1 \
// RUN: | FileCheck %s -DDIR=%{nonexistent_dir} --check-prefixes=CHECK-HHH-NONEXISTENT
// RUN: %clang -### -ccc-install-dir %{nonexistent_dir} -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib %s 2>&1 \
// RUN: | FileCheck %s -DDIR=%{nonexistent_dir} --check-prefixes=CHECK-HHH-NONEXISTENT
// CHECK-HHH-NONEXISTENT: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "[[DIR]]{{.*[\\/]}}remangled-{{.*}}.libspirv-nvptx64-nvidia-cuda.bc"
//
// RUN: %clang -### -resource-dir %{nonexistent_dir} -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 -nogpulib %s 2>&1 \
// RUN: | FileCheck %s -DDIR=%{nonexistent_dir} --check-prefixes=CHECK-AMDGCN-HHH-NONEXISTENT
// RUN: %clang -### -ccc-install-dir %{nonexistent_dir} -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 -nogpulib %s 2>&1 \
// RUN: | FileCheck %s -DDIR=%{nonexistent_dir} --check-prefixes=CHECK-AMDGCN-HHH-NONEXISTENT
// CHECK-AMDGCN-HHH-NONEXISTENT: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "[[DIR]]{{.*[\\/]}}remangled-{{.*}}.libspirv-amdgcn-amd-amdhsa.bc"
//
// `-fdriver-only` has no such special handling, so it will not find the file
// RUN: not %clang -fdriver-only -resource-dir %{nonexistent_dir} -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib %s 2>&1 \
// RUN: | FileCheck %s --check-prefixes=CHECK-DO-NONEXISTENT
// RUN: not %clang -fdriver-only -ccc-install-dir %{nonexistent_dir} -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib %s 2>&1 \
// RUN: | FileCheck %s -DDIR=%{nonexistent_dir} --check-prefixes=CHECK-DO-NONEXISTENT
// CHECK-DO-NONEXISTENT: error: cannot find 'remangled-{{.*}}.libspirv-nvptx64-nvidia-cuda.bc'; provide path to libspirv library via '-fsycl-libspirv-path', or pass '-fno-sycl-libspirv' to build without linking with libspirv
4 changes: 2 additions & 2 deletions libdevice/cmake/modules/SYCLLibdevice.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ set(devicelib_arch)
if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND devicelib_arch nvptx64-nvidia-cuda)
set(compile_opts_nvptx64-nvidia-cuda "-fsycl-targets=nvptx64-nvidia-cuda"
"-Xsycl-target-backend" "--cuda-gpu-arch=sm_50" "-nocudalib")
"-Xsycl-target-backend" "--cuda-gpu-arch=sm_50" "-nocudalib" "-fno-sycl-libspirv" "-Wno-unsafe-libspirv-not-linked")
set(opt_flags_nvptx64-nvidia-cuda "-O3" "--nvvm-reflect-enable=false")
endif()
if("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND devicelib_arch amdgcn-amd-amdhsa)
set(compile_opts_amdgcn-amd-amdhsa "-nogpulib" "-fsycl-targets=amdgcn-amd-amdhsa"
"-Xsycl-target-backend" "--offload-arch=gfx942")
"-Xsycl-target-backend" "--offload-arch=gfx942" "-fno-sycl-libspirv" "-Wno-unsafe-libspirv-not-linked")
set(opt_flags_amdgcn-amd-amdhsa "-O3" "--amdgpu-oclc-reflect-enable=false")
endif()

Expand Down