-
Notifications
You must be signed in to change notification settings - Fork 769
[SYCL] Link device lib via mlink-builtin-bitcode #2833
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
Changes from all commits
9203c16
f12fcc8
b0a57d9
7dfcd9b
2820a54
a35765a
37697e5
5a9bdf1
a82798e
3afa020
faadbe7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "SYCL.h" | ||
#include "CommonArgs.h" | ||
#include "InputInfo.h" | ||
|
@@ -417,10 +416,96 @@ SYCLToolChain::SYCLToolChain(const Driver &D, const llvm::Triple &Triple, | |
getProgramPaths().push_back(getDriver().Dir); | ||
} | ||
|
||
static bool doLLVMBCEmit(llvm::opt::ArgStringList &CC1Args) { | ||
for (auto StrArg : CC1Args) | ||
if (std::string(StrArg) == "-emit-llvm-bc") | ||
return true; | ||
return false; | ||
} | ||
|
||
void SYCLToolChain::AddSYCLDeviceLibs(const llvm::opt::ArgList &Args, | ||
llvm::opt::ArgStringList &CC1Args) const { | ||
enum SYCLDeviceLibType { sycl_devicelib_wrapper, sycl_devicelib_fallback }; | ||
struct DeviceLibOptInfo { | ||
StringRef devicelib_name; | ||
StringRef devicelib_option; | ||
}; | ||
|
||
bool NoDeviceLibs = false; | ||
// Currently, libc, libm-fp32 will be linked in by default. In order | ||
// to use libm-fp64, -fsycl-device-lib=libm-fp64/all should be used. | ||
llvm::StringMap<bool> devicelib_link_info = { | ||
{"libc", true}, {"libm-fp32", true}, {"libm-fp64", true}}; | ||
if (Arg *A = Args.getLastArg(options::OPT_fsycl_device_lib_EQ, | ||
options::OPT_fno_sycl_device_lib_EQ)) { | ||
if (A->getValues().size() == 0) | ||
getDriver().Diag(diag::warn_drv_empty_joined_argument) | ||
<< A->getAsString(Args); | ||
else { | ||
if (A->getOption().matches(options::OPT_fno_sycl_device_lib_EQ)) | ||
NoDeviceLibs = true; | ||
|
||
for (StringRef Val : A->getValues()) { | ||
if (Val == "all") { | ||
for (auto &K : devicelib_link_info.keys()) | ||
devicelib_link_info[K] = true && !NoDeviceLibs; | ||
break; | ||
} | ||
auto LinkInfoIter = devicelib_link_info.find(Val); | ||
if (LinkInfoIter == devicelib_link_info.end()) { | ||
getDriver().Diag(diag::err_drv_unsupported_option_argument) | ||
<< A->getOption().getName() << Val; | ||
} | ||
devicelib_link_info[Val] = true && !NoDeviceLibs; | ||
} | ||
} | ||
} | ||
bool isSpirvAOT = true; | ||
SmallString<128> LibLoc(getDriver().Dir); | ||
llvm::sys::path::append(LibLoc, "/../lib"); | ||
SmallVector<DeviceLibOptInfo, 5> sycl_device_wrapper_libs = { | ||
{"libsycl-crt", "libc"}, | ||
{"libsycl-complex", "libm-fp32"}, | ||
{"libsycl-complex-fp64", "libm-fp64"}, | ||
{"libsycl-cmath", "libm-fp32"}, | ||
{"libsycl-cmath-fp64", "libm-fp64"}}; | ||
// For AOT compilation, we need to link sycl_device_fallback_libs as | ||
// default too. | ||
SmallVector<DeviceLibOptInfo, 5> sycl_device_fallback_libs = { | ||
{"libsycl-fallback-cassert", "libc"}, | ||
{"libsycl-fallback-complex", "libm-fp32"}, | ||
{"libsycl-fallback-complex-fp64", "libm-fp64"}, | ||
{"libsycl-fallback-cmath", "libm-fp32"}, | ||
{"libsycl-fallback-cmath-fp64", "libm-fp64"}}; | ||
|
||
auto addInputs = [&](SYCLDeviceLibType t, StringRef SYCLArchName) { | ||
auto sycl_libs = (t == sycl_devicelib_wrapper) ? sycl_device_wrapper_libs | ||
: sycl_device_fallback_libs; | ||
for (const DeviceLibOptInfo &Lib : sycl_libs) { | ||
if (!devicelib_link_info[Lib.devicelib_option]) | ||
continue; | ||
SmallString<128> LibName(LibLoc); | ||
llvm::sys::path::append(LibName, Lib.devicelib_name); | ||
LibName.append("-"); | ||
LibName.append(SYCLArchName); | ||
llvm::sys::path::replace_extension(LibName, ".bc"); | ||
if (llvm::sys::fs::exists(LibName)) { | ||
CC1Args.push_back("-mlink-builtin-bitcode"); | ||
CC1Args.push_back(Args.MakeArgString(LibName.c_str())); | ||
} | ||
} | ||
}; | ||
addInputs(sycl_devicelib_wrapper, getTriple().getArchName()); | ||
if (isSpirvAOT) | ||
addInputs(sycl_devicelib_fallback, getTriple().getArchName()); | ||
} | ||
|
||
void SYCLToolChain::addClangTargetOptions( | ||
const llvm::opt::ArgList &DriverArgs, | ||
llvm::opt::ArgStringList &CC1Args, | ||
Action::OffloadKind DeviceOffloadingKind) const { | ||
if (DeviceOffloadingKind == Action::OFK_SYCL && doLLVMBCEmit(CC1Args)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @bader There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to link them always. Integration header is generated from Sema, so adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @bader There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the right place to make this decision. We must always link devicelib unless user explicitly disable the linking. We should have some "optimizing" logic here as it's a error prone. I can image a few cases already where this can lead to bugs. |
||
AddSYCLDeviceLibs(DriverArgs, CC1Args); | ||
HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you see any problems with linking
libm-fp64
by default?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @bader
I think it should be OK to link libm-fp64 by default too. I will find a some machine without fp64 extension to see if any problem exposed.
Thanks very much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be no problems. If math functions with doubles are not used there will be no unsupported functions in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bader @mdtoguchi @vzakhari
Have updated the patch to link libm-fp64 by default. But I suggest to re-discuss the behavior of "-fsycl-device-lib=..."
Currently, "-fsycl-device-lib=.." won't impact our "default" link behavior, it seems that current behavior of "-fsycl-device-lib=" is useless after we link all device libraries by default.
Maybe aligning behaviors of "-fsycl-device-lib" and "-fno-sycl-device-lib" is more reasonable, if user addes "-fsycl-device-lib=libm-fp32", we won't do "default" link, instead, we only link libm-fp32 device libraries, the same to libc, libm-fp64...
And I think we should re-consider the "-fsycl-device-lib=..." no matter we choose this PR or #2783 in the end.
Thanks very much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say we have just one option -
-fsycl
analog for-nostdlib
i.e.-fno-sycl-device-lib
.I don't think we really need a fine grained control over which part of standard library is linked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @bader. We shouldn't modify what we link by default with an option that adjusts the adding of libs. Similar to adding
-lgcc
on the command line, that doesn't override the default libs already linked in. A single option that disables all is reasonable.