Skip to content

Commit ebf6c59

Browse files
authored
[Driver][SYCL] Improve way sycld is pulled in for Linux based Windows… (#6974)
… driver Update the way sycld is pulled in by matching if msvcrtd is brought in via the command line. Cmake uses '-Xclang --dependent-lib=msvcrtd' when there is the desire to link in with the debug libraries. Check for this and use an equivalent '--dependent-lib=sycld' to pull in the SYCL runtime library. This is only done for the Linux based drivers on Windows. The MSVC based driver behavior already pulls in the proper libraries given /MDd.
1 parent c7b1a00 commit ebf6c59

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6100,11 +6100,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
61006100
if (D.IsCLMode())
61016101
AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
61026102

6103-
// Add debug macro for debug library usage when using non-cl driver on
6104-
// Windows as we are using the debug sycld.lib with -g
6103+
// Add the sycld debug library when --dependent-lib=msvcrtd is used from
6104+
// the command line. This is to allow for CMake based builds using the
6105+
// Linux based driver on Windows to correctly pull in the expected debug
6106+
// library.
61056107
if (!D.IsCLMode() && TC.getTriple().isWindowsMSVCEnvironment() &&
6106-
Args.hasArg(options::OPT_fsycl) && Args.hasArg(options::OPT_g_Flag))
6107-
CmdArgs.push_back("-D_DEBUG");
6108+
Args.hasArg(options::OPT_fsycl)) {
6109+
if (isDependentLibAdded(Args, "msvcrtd"))
6110+
CmdArgs.push_back("--dependent-lib=sycl" SYCL_MAJOR_VERSION "d");
6111+
}
61086112

61096113
DwarfFissionKind DwarfFission = DwarfFissionKind::None;
61106114
renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView,

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,15 @@ bool tools::areOptimizationsEnabled(const ArgList &Args) {
11111111
return false;
11121112
}
11131113

1114+
bool tools::isDependentLibAdded(const ArgList &Args, StringRef Lib) {
1115+
// Check if given Lib is added via --dependent-lib
1116+
SmallString<64> DepLib("--dependent-lib=");
1117+
DepLib += Lib;
1118+
return llvm::any_of(
1119+
Args.getAllArgValues(options::OPT_Xclang),
1120+
[&DepLib](StringRef Option) { return Option.equals(DepLib); });
1121+
}
1122+
11141123
const char *tools::SplitDebugName(const JobAction &JA, const ArgList &Args,
11151124
const InputInfo &Input,
11161125
const InputInfo &Output) {

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ llvm::StringRef getLTOParallelism(const llvm::opt::ArgList &Args,
146146

147147
bool areOptimizationsEnabled(const llvm::opt::ArgList &Args);
148148

149+
bool isDependentLibAdded(const llvm::opt::ArgList &Args, StringRef Lib);
150+
149151
bool isUseSeparateSections(const llvm::Triple &Triple);
150152

151153
/// \p EnvVar is split by system delimiter for environment variables.

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
124124
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
125125
!C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
126126
if (Args.hasArg(options::OPT_fsycl) && !Args.hasArg(options::OPT_nolibsycl))
127-
if (Args.hasArg(options::OPT_g_Flag))
128-
CmdArgs.push_back("-defaultlib:msvcrtd");
129-
else
130-
CmdArgs.push_back("-defaultlib:msvcrt");
127+
CmdArgs.push_back("-defaultlib:msvcrt");
131128
else
132129
CmdArgs.push_back("-defaultlib:libcmt");
133130
CmdArgs.push_back("-defaultlib:oldnames");
@@ -138,9 +135,9 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
138135
Args.hasArg(options::OPT_fsycl_host_compiler_EQ)) {
139136
CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
140137
TC.getDriver().Dir + "/../lib"));
141-
if (Args.hasArg(options::OPT_g_Flag))
142-
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION "d.lib");
143-
else
138+
// When msvcrtd is added via --dependent-lib, we add the sycld
139+
// equivalent. Do not add the -defaultlib as it conflicts.
140+
if (!isDependentLibAdded(Args, "msvcrtd"))
144141
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION ".lib");
145142
CmdArgs.push_back("-defaultlib:sycl-devicelib-host.lib");
146143
}

clang/test/Driver/sycl-offload.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -661,15 +661,14 @@
661661
// CHECK-LINK-NOSTDLIB: "{{.*}}link{{(.exe)?}}"
662662
// CHECK-LINK-NOSTDLIB: "-defaultlib:sycl{{[0-9]*}}.lib"
663663

664-
/// Check sycld.lib is chosen with /MDd or -g
665-
// RUN: %clang -fsycl -g -target x86_64-unknown-windows-msvc %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
666-
// RUN: %clang_cl -fsycl /MDd %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG-CL %s
667-
// CHECK-LINK-SYCL-DEBUG-CL: "--dependent-lib=sycl{{[0-9]*}}d"
668-
// CHECK-LINK-SYCL-DEBUG-CL-NOT: "-defaultlib:sycl{{[0-9]*}}d.lib"
669-
// CHECK-LINK-SYCL-DEBUG: "-D_DEBUG"
670-
// CHECK-LINK-SYCL-DEBUG: "-defaultlib:msvcrtd"
671-
// CHECK-LINK-SYCL-DEBUG: "-defaultlib:sycl{{[0-9]*}}d.lib"
672-
// CHECK-LINK-SYCL-DEBUG-NOT: "--dependent-lib=sycl{{[0-9]*}}d"
664+
/// Check sycld.lib is chosen with /MDd
665+
// RUN: %clang_cl -fsycl /MDd %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
666+
/// Check sycld is pulled in when msvcrtd is used
667+
// RUN: %clangxx -fsycl -Xclang --dependent-lib=msvcrtd \
668+
// RUN: -target x86_64-unknown-windows-msvc -### %s 2>&1 \
669+
// RUN: | FileCheck -check-prefix=CHECK-LINK-SYCL-DEBUG %s
670+
// CHECK-LINK-SYCL-DEBUG: "--dependent-lib=sycl{{[0-9]*}}d"
671+
// CHECK-LINK-SYCL-DEBUG-NOT: "-defaultlib:sycl{{[0-9]*}}.lib"
673672

674673
/// Check "-spirv-allow-unknown-intrinsics=llvm.genx." option is emitted for llvm-spirv tool
675674
// RUN: %clangxx %s -fsycl -### 2>&1 | FileCheck %s --check-prefix=CHK-ALLOW-INTRIN

0 commit comments

Comments
 (0)