Skip to content

Commit 7e1ea22

Browse files
authored
[Driver][SYCL] Update sycl lib linking with -fms-runtime-lib (#19380)
The usage of `-fms-runtime-lib=val` will pull in the default libraries for MSVC linking as well as the dependent SYCL libraries when using the Linux based driver with the MSVC target triple. There are special interactions with the --dependent-lib=msvcrtd library we have that control the inclusion of sycl.lib to prevent linktime problems. Extend these behaviors to usage of `-fms-runtime-lib=dll_dbg`, preventing duplicate instances of the sycl.lib in the --dependent-lib usage as well as when performing the final link.
1 parent b999657 commit 7e1ea22

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5428,19 +5428,13 @@ static void ProcessVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args,
54285428
// Add SYCL dependent library
54295429
if (Args.hasArg(options::OPT_fsycl) &&
54305430
!Args.hasArg(options::OPT_nolibsycl)) {
5431-
if (RTOptionID == options::OPT__SLASH_MDd) {
5432-
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
5433-
CmdArgs.push_back("--dependent-lib=sycl" SYCL_MAJOR_VERSION
5434-
"-previewd");
5435-
else
5436-
CmdArgs.push_back("--dependent-lib=sycl" SYCL_MAJOR_VERSION "d");
5437-
} else {
5438-
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
5439-
CmdArgs.push_back("--dependent-lib=sycl" SYCL_MAJOR_VERSION
5440-
"-preview");
5441-
else
5442-
CmdArgs.push_back("--dependent-lib=sycl" SYCL_MAJOR_VERSION);
5443-
}
5431+
SmallString<128> SYCLLibName("sycl" SYCL_MAJOR_VERSION);
5432+
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
5433+
SYCLLibName += "-preview";
5434+
if (RTOptionID == options::OPT__SLASH_MDd)
5435+
SYCLLibName += "d";
5436+
CmdArgs.push_back(
5437+
Args.MakeArgString(Twine("--dependent-lib=") + SYCLLibName));
54445438
CmdArgs.push_back("--dependent-lib=sycl-devicelib-host");
54455439
}
54465440
}
@@ -7044,11 +7038,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
70447038
// Add the sycld debug library when --dependent-lib=msvcrtd is used from
70457039
// the command line. This is to allow for CMake based builds using the
70467040
// Linux based driver on Windows to correctly pull in the expected debug
7047-
// library.
7041+
// library. Do not add when -fms-runtime-lib is used, as that pulls in the
7042+
// libraries separately.
70487043
if (Args.hasArg(options::OPT_fsycl) && !Args.hasArg(options::OPT_nolibsycl) &&
70497044
!D.IsCLMode()) {
70507045
if (TC.getTriple().isWindowsMSVCEnvironment()) {
7051-
if (isDependentLibAdded(Args, "msvcrtd")) {
7046+
if (isDependentLibAdded(Args, "msvcrtd") &&
7047+
!Args.hasArg(options::OPT_fms_runtime_lib_EQ)) {
70527048
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
70537049
CmdArgs.push_back("--dependent-lib=sycl" SYCL_MAJOR_VERSION
70547050
"-previewd");

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
9999
Args.hasArg(options::OPT_fsycl_host_compiler_EQ)) {
100100
CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
101101
TC.getDriver().Dir + "/../lib"));
102-
// When msvcrtd is added via --dependent-lib, we add the sycld
103-
// equivalent. Do not add the -defaultlib as it conflicts.
104-
if (!isDependentLibAdded(Args, "msvcrtd")) {
102+
// When msvcrtd is added via --dependent-lib or -fms-runtime-lib=dll_dbg we
103+
// add the sycld equivalent. Do not add the -defaultlib as it conflicts.
104+
StringRef RuntimeVal;
105+
if (const Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ))
106+
RuntimeVal = A->getValue();
107+
if (!isDependentLibAdded(Args, "msvcrtd") && RuntimeVal != "dll_dbg") {
105108
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
106109
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION "-preview.lib");
107110
else

clang/test/Driver/sycl-offload-old-model.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@
503503
// RUN: %clangxx -fsycl --no-offload-new-driver -Xclang --dependent-lib=msvcrtd \
504504
// RUN: -target x86_64-unknown-windows-msvc -### %s 2>&1 \
505505
// RUN: | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
506+
/// Check sycld is pulled in when -fms-runtime-lib=dll_dbg
507+
// RUN: %clangxx -fsycl --no-offload-new-driver -fms-runtime-lib=dll_dbg \
508+
// RUN: -target x86_64-unknown-windows-msvc -### %s 2>&1 \
509+
// RUN: | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
506510
// CHECK-LINK-SYCL-DEBUG: "--dependent-lib=sycl{{[0-9]*}}d"
507511
// CHECK-LINK-SYCL-DEBUG-NOT: "-defaultlib:sycl{{[0-9]*}}.lib"
508512

clang/test/Driver/sycl-offload.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,21 @@
292292
// RUN: %clangxx -fsycl --offload-new-driver -Xclang --dependent-lib=msvcrtd \
293293
// RUN: -target x86_64-unknown-windows-msvc -### %s 2>&1 \
294294
// RUN: | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
295+
/// Check sycld.lib is pulled in with -fms-runtime-lib=dll_dbg
296+
// RUN: %clangxx -fsycl --offload-new-driver -fms-runtime-lib=dll_dbg \
297+
// RUN: -target x86_64-unknown-windows-msvc -### %s 2>&1 \
298+
// RUN: | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
295299
// CHECK-LINK-SYCL-DEBUG: "--dependent-lib=sycl{{[0-9]*}}d"
296300
// CHECK-LINK-SYCL-DEBUG-NOT: "-defaultlib:sycl{{[0-9]*}}.lib"
297301

302+
/// Only a single instance of sycld should be pulled in when both the
303+
/// -Xclang --dependent-lib=msvcrtd and -fms-runtime-lib=dll_dbg is used.
304+
// RUN: %clangxx -fsycl --offload-new-driver -fms-runtime-lib=dll_dbg -Xclang \
305+
// RUN: --dependent-lib=msvcrtd --target=x86_64-unknown-windows-msvc -### %s 2>&1 \
306+
// RUN: | FileCheck -check-prefix=CHECK-LINK-SYCLD %s
307+
// CHECK-LINK-SYCLD: "--dependent-lib=sycl{{[0-9]*}}d"
308+
// CHECK-LINK-SYCLD-NOT: "--dependent-lib=sycl{{[0-9]*}}d"
309+
298310
/// ###########################################################################
299311

300312
/// Check -Xsycl-target-frontend does not trigger an error when no -fsycl-targets is specified

0 commit comments

Comments
 (0)