Skip to content

Commit d6eecfa

Browse files
authored
[SYCL][Driver] Link with sycl libs at link step of clang-cl -fsycl (#12793)
This PR is addressing the following scenario: clang-cl -I[path to sycl headers] sycl_program.cpp # program compiled but without sycl libs clang-cl -fsycl sycl_program.obj # user expects sycl libs to be linked automatically here. Without this fix this scenario fails at link step because sycl libraries are not pulled in. This scenario already works for clang driver, so only clang-cl needs a fix.
1 parent 0fc5129 commit d6eecfa

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,23 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
130130
CmdArgs.push_back("-defaultlib:oldnames");
131131
}
132132

133-
if ((!C.getDriver().IsCLMode() && Args.hasArg(options::OPT_fsycl) &&
133+
if ((Args.hasArg(options::OPT_fsycl) &&
134134
!Args.hasArg(options::OPT_nolibsycl)) ||
135135
Args.hasArg(options::OPT_fsycl_host_compiler_EQ)) {
136136
CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
137137
TC.getDriver().Dir + "/../lib"));
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")) {
138+
if (!Args.hasArg(options::OPT__SLASH_MDd) &&
139+
!isDependentLibAdded(Args, "msvcrtd")) {
141140
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
142141
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION "-preview.lib");
143142
else
144143
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION ".lib");
144+
} else {
145+
if (Args.hasArg(options::OPT_fpreview_breaking_changes))
146+
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION
147+
"-previewd.lib");
148+
else
149+
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION "d.lib");
145150
}
146151
CmdArgs.push_back("-defaultlib:sycl-devicelib-host.lib");
147152
}

clang/test/Driver/sycl-offload.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@
363363
// RUN: %clang -fsycl -target x86_64-unknown-windows-msvc %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL %s
364364
// RUN: %clang_cl -fsycl %s -o %t -### 2>&1 | FileCheck -check-prefix=CHECK-LINK-SYCL-CL %s
365365
// CHECK-LINK-SYCL-CL: "--dependent-lib=sycl{{[0-9]*}}"
366-
// CHECK-LINK-SYCL-CL-NOT: "-defaultlib:sycl{{[0-9]*}}.lib"
366+
// CHECK-LINK-SYCL-CL: "-defaultlib:sycl{{[0-9]*}}.lib"
367367
// CHECK-LINK-SYCL: "-defaultlib:sycl{{[0-9]*}}.lib"
368368

369369
/// Check no SYCL runtime is linked with -nolibsycl
@@ -688,3 +688,16 @@
688688
// FSYCL-PREVIEW-BREAKING-CHANGES-DEBUG-CHECK: --dependent-lib=sycl{{[0-9]*}}-previewd
689689
// FSYCL-PREVIEW-BREAKING-CHANGES-DEBUG-CHECK-NOT: -defaultlib:sycl{{[0-9]*}}.lib
690690
// FSYCL-PREVIEW-BREAKING-CHANGES-DEBUG-CHECK-NOT: -defaultlib:sycl{{[0-9]*}}-preview.lib
691+
692+
693+
/// Check that at link step of "clang-cl -fsycl" we pull in sycl.lib even if at the compilation step sycl libraries were not provided (this is possible if user compiles manually without -fsycl by provided paths to the headers).
694+
// RUN: %clang_cl -### -fsycl -nolibsycl -target x86_64-unknown-windows-msvc -c %s 2>&1 | FileCheck -check-prefix FSYCL-CL-COMPILE-NOLIBS-CHECK %s
695+
// RUN: %clang_cl -### -fsycl %s 2>&1 | FileCheck -check-prefix FSYCL-CL-LINK-CHECK %s
696+
// FSYCL-CL-COMPILE-NOLIBS-CHECK-NOT: "--dependent-lib=sycl{{[0-9]*}}"
697+
// FSYCL-CL-LINK-CHECK: "-defaultlib:sycl{{[0-9]*}}.lib"
698+
699+
/// Check that at link step of "clang-cl -fsycl /MDd" we pull in sycld.lib even if at the compilation step sycl libraries were not provided (this is possible if user compiles manually without -fsycl by provided paths to the headers).
700+
// RUN: %clang_cl -### -fsycl -nolibsycl /MDd -target x86_64-unknown-windows-msvc -c %s 2>&1 | FileCheck -check-prefix FSYCL-CL-COMPILE-NOLIBS-MDd-CHECK %s
701+
// RUN: %clang_cl -### -fsycl /MDd %s 2>&1 | FileCheck -check-prefix FSYCL-CL-LINK--MDd-CHECK %s
702+
// FSYCL-CL-COMPILE-NOLIBS-MDd-CHECK-NOT: "--dependent-lib=sycl{{[0-9]*}}d"
703+
// FSYCL-CL-LINK--MDd-CHECK: "-defaultlib:sycl{{[0-9]*}}d.lib"

0 commit comments

Comments
 (0)