Skip to content

Commit 404d281

Browse files
authored
[SYCL][Windows] Improve windows sycl.lib linking (#6699)
This patch does two things, first it makes `-fsycl` ignore `-nostdlib` when linking the SYCL library. This is necessary because for Clang on Windows CMake will generate link commands using `-nostdlib` and explicitly list the system libraries, but of course it doesn't do it for SYCL, so we currently end up never linking the SYCL library when this is used. Ignoring `-nostdlib` for `-fsycl` on Windows seems like a reasonnable solution for this as this is also what is done for the OpenMP runtime libraries. See the CMake module: * https://github.com/Kitware/CMake/blob/aa2de7cd2a04699744a224ab84e0ca483559c5d3/Modules/Platform/Windows-Clang.cmake#L79 In addition this patch also adds a linker parameter to help clang find the `sycl.lib` file without requiring users to tweak their environments to link against it.
1 parent 436f0d8 commit 404d281

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
131131
CmdArgs.push_back("-defaultlib:oldnames");
132132
}
133133

134-
if ((!C.getDriver().IsCLMode() && !Args.hasArg(options::OPT_nostdlib) &&
135-
Args.hasArg(options::OPT_fsycl) &&
134+
if ((!C.getDriver().IsCLMode() && Args.hasArg(options::OPT_fsycl) &&
136135
!Args.hasArg(options::OPT_nolibsycl)) ||
137136
Args.hasArg(options::OPT_fsycl_host_compiler_EQ)) {
137+
CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
138+
TC.getDriver().Dir + "/../lib"));
138139
if (Args.hasArg(options::OPT__SLASH_MDd))
139140
CmdArgs.push_back("-defaultlib:sycld.lib");
140141
else

clang/test/Driver/sycl-offload.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,11 @@
634634
// CHECK-LD-NOLIBSYCL: "{{.*}}ld{{(.exe)?}}"
635635
// CHECK-LD-NOLIBSYCL-NOT: "-lsycl"
636636

637+
/// Check no SYCL runtime is linked with -nostdlib
638+
// RUN: %clang -fsycl -nostdlib -target x86_64-unknown-linux-gnu %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LD-NOSTDLIB %s
639+
// CHECK-LD-NOSTDLIB: "{{.*}}ld{{(.exe)?}}"
640+
// CHECK-LD-NOSTDLIB-NOT: "-lsycl"
641+
637642
/// Check for default linking of sycl.lib with -fsycl usage
638643
// RUN: %clang -fsycl -target x86_64-unknown-windows-msvc %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL %s
639644
// RUN: %clang_cl -fsycl %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL-CL %s
@@ -643,11 +648,19 @@
643648

644649
/// Check no SYCL runtime is linked with -nolibsycl
645650
// RUN: %clang -fsycl -nolibsycl -target x86_64-unknown-windows-msvc %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-NOLIBSYCL %s
646-
// RUN: %clang_cl -fsycl -nolibsycl %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-NOLIBSYCL %s
647-
// CHECK-LINK-NOLIBSYCL-NOT: "--dependent-lib=sycl"
651+
// RUN: %clang_cl -fsycl -nolibsycl %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-NOLIBSYCL-CL %s
652+
// CHECK-LINK-NOLIBSYCL-CL-NOT: "--dependent-lib=sycl"
648653
// CHECK-LINK-NOLIBSYCL: "{{.*}}link{{(.exe)?}}"
649654
// CHECK-LINK-NOLIBSYCL-NOT: "-defaultlib:sycl.lib"
650655

656+
/// Check SYCL runtime is linked despite -nostdlib on Windows, this is
657+
/// necessary for the Windows Clang CMake to work
658+
// RUN: %clang -fsycl -nostdlib -target x86_64-unknown-windows-msvc %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-NOSTDLIB %s
659+
// RUN: %clang_cl -fsycl -nostdlib %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-NOSTDLIB-CL %s
660+
// CHECK-LINK-NOSTDLIB-CL: "--dependent-lib=sycl"
661+
// CHECK-LINK-NOSTDLIB: "{{.*}}link{{(.exe)?}}"
662+
// CHECK-LINK-NOSTDLIB: "-defaultlib:sycl.lib"
663+
651664
/// Check sycld.lib is chosen with /MDd
652665
// RUN: %clang_cl -fsycl /MDd %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
653666
// CHECK-LINK-SYCL-DEBUG: "--dependent-lib=sycld"

0 commit comments

Comments
 (0)