Skip to content

Commit 8735bb8

Browse files
authored
[SYCL][Driver] Make /MD default with -fsycl for clang/clang++ drivers (#2480)
SYCL library is designed such a way that STL objects must cross the sycl.dll boundary, which is guaranteed to work safe on Windows only if the runtime in the app using sycl.dll and in sycl.dll is the same and is dynamic. It is not possible to implement safe approach for using sycl libraries built/linked with static C++ RT as it would cause having multiple copies of C++ objects (such as scheduler, etc), which are supposed to be singletones. sycl.dll is built with /MD (linked to dynamic C++ RT), this is why this patch makes /MD default with -fsycl for clang and clang++ drivers.
1 parent a09aed0 commit 8735bb8

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -6258,6 +6258,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
62586258
if (Args.hasFlag(options::OPT_fsycl_esimd, options::OPT_fno_sycl_esimd,
62596259
false))
62606260
CmdArgs.push_back("-fsycl-explicit-simd");
6261+
6262+
if (!D.IsCLMode()) {
6263+
// SYCL library is guaranteed to work correctly only with dynamic
6264+
// MSVC runtime.
6265+
llvm::Triple AuxT = C.getDefaultToolChain().getTriple();
6266+
if (Args.hasFlag(options::OPT_fsycl_device_only, OptSpecifier(), false))
6267+
AuxT = llvm::Triple(llvm::sys::getProcessTriple());
6268+
if (AuxT.isWindowsMSVCEnvironment()) {
6269+
CmdArgs.push_back("-D_MT");
6270+
CmdArgs.push_back("-D_DLL");
6271+
CmdArgs.push_back("--dependent-lib=msvcrt");
6272+
}
6273+
}
62616274
}
62626275
if (IsSYCLOffloadDevice && JA.getType() == types::TY_SYCL_Header) {
62636276
// Generating a SYCL Header
@@ -6836,8 +6849,7 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
68366849
// Add SYCL dependent library
68376850
if (Args.hasArg(options::OPT_fsycl) &&
68386851
!Args.hasArg(options::OPT_nolibsycl)) {
6839-
if (RTOptionID == options::OPT__SLASH_MDd ||
6840-
RTOptionID == options::OPT__SLASH_MTd)
6852+
if (RTOptionID == options::OPT__SLASH_MDd)
68416853
CmdArgs.push_back("--dependent-lib=sycld");
68426854
else
68436855
CmdArgs.push_back("--dependent-lib=sycl");

clang/lib/Driver/ToolChains/MSVC.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,12 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
370370
Args.MakeArgString(std::string("-out:") + Output.getFilename()));
371371

372372
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
373-
!C.getDriver().IsCLMode())
374-
CmdArgs.push_back("-defaultlib:libcmt");
373+
!C.getDriver().IsCLMode()) {
374+
if (Args.hasArg(options::OPT_fsycl) && !Args.hasArg(options::OPT_nolibsycl))
375+
CmdArgs.push_back("-defaultlib:msvcrt");
376+
else
377+
CmdArgs.push_back("-defaultlib:libcmt");
378+
}
375379

376380
if (!C.getDriver().IsCLMode() && !Args.hasArg(options::OPT_nostdlib) &&
377381
Args.hasArg(options::OPT_fsycl) && !Args.hasArg(options::OPT_nolibsycl)) {

clang/test/Driver/sycl-MD-default.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// REQUIRES: clang-driver
22

3+
// RUN: %clang -### -fsycl -c -target x86_64-unknown-windows-msvc %s 2>&1 \
4+
// RUN: | FileCheck -check-prefix=CHK-DEFAULT %s
5+
// RUN: %clangxx -### -fsycl -c -target x86_64-unknown-windows-msvc %s 2>&1 \
6+
// RUN: | FileCheck -check-prefix=CHK-DEFAULT %s
37
// RUN: %clang_cl -### -fsycl -c %s 2>&1 \
48
// RUN: | FileCheck -check-prefix=CHK-DEFAULT %s
59
// RUN: %clang_cl -### -MD -fsycl -c %s 2>&1 \

0 commit comments

Comments
 (0)