Skip to content

[Driver][SYCL] Improve way sycld is pulled in for Linux based Windows… #6974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6100,11 +6100,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (D.IsCLMode())
AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);

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

DwarfFissionKind DwarfFission = DwarfFissionKind::None;
renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView,
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,15 @@ bool tools::areOptimizationsEnabled(const ArgList &Args) {
return false;
}

bool tools::isDependentLibAdded(const ArgList &Args, StringRef Lib) {
// Check if given Lib is added via --dependent-lib
SmallString<64> DepLib("--dependent-lib=");
DepLib += Lib;
return llvm::any_of(
Args.getAllArgValues(options::OPT_Xclang),
[&DepLib](StringRef Option) { return Option.equals(DepLib); });
}

const char *tools::SplitDebugName(const JobAction &JA, const ArgList &Args,
const InputInfo &Input,
const InputInfo &Output) {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ llvm::StringRef getLTOParallelism(const llvm::opt::ArgList &Args,

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

bool isDependentLibAdded(const llvm::opt::ArgList &Args, StringRef Lib);

bool isUseSeparateSections(const llvm::Triple &Triple);

/// \p EnvVar is split by system delimiter for environment variables.
Expand Down
11 changes: 4 additions & 7 deletions clang/lib/Driver/ToolChains/MSVC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
!C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
if (Args.hasArg(options::OPT_fsycl) && !Args.hasArg(options::OPT_nolibsycl))
if (Args.hasArg(options::OPT_g_Flag))
CmdArgs.push_back("-defaultlib:msvcrtd");
else
CmdArgs.push_back("-defaultlib:msvcrt");
CmdArgs.push_back("-defaultlib:msvcrt");
else
CmdArgs.push_back("-defaultlib:libcmt");
CmdArgs.push_back("-defaultlib:oldnames");
Expand All @@ -138,9 +135,9 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.hasArg(options::OPT_fsycl_host_compiler_EQ)) {
CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
TC.getDriver().Dir + "/../lib"));
if (Args.hasArg(options::OPT_g_Flag))
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION "d.lib");
else
// When msvcrtd is added via --dependent-lib, we add the sycld
// equivalent. Do not add the -defaultlib as it conflicts.
if (!isDependentLibAdded(Args, "msvcrtd"))
CmdArgs.push_back("-defaultlib:sycl" SYCL_MAJOR_VERSION ".lib");
CmdArgs.push_back("-defaultlib:sycl-devicelib-host.lib");
}
Expand Down
17 changes: 8 additions & 9 deletions clang/test/Driver/sycl-offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,14 @@
// CHECK-LINK-NOSTDLIB: "{{.*}}link{{(.exe)?}}"
// CHECK-LINK-NOSTDLIB: "-defaultlib:sycl{{[0-9]*}}.lib"

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

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