Skip to content
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
42 changes: 25 additions & 17 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5901,23 +5901,31 @@ class OffloadingActionBuilder final {
++NumOfDeviceLibLinked;
Arg *InputArg = MakeInputArg(Args, C.getDriver().getOpts(),
Args.MakeArgString(LibName));
auto *SYCLDeviceLibsInputAction =
C.MakeAction<InputAction>(*InputArg, types::TY_Object);
auto *SYCLDeviceLibsUnbundleAction =
C.MakeAction<OffloadUnbundlingJobAction>(
SYCLDeviceLibsInputAction);

// We are using BoundArch="" here since the NVPTX bundles in
// the devicelib .o files do not contain any arch information
SYCLDeviceLibsUnbundleAction->registerDependentActionInfo(
TC, /*BoundArch=*/"", Action::OFK_SYCL);
OffloadAction::DeviceDependences Dep;
Dep.add(*SYCLDeviceLibsUnbundleAction, *TC, /*BoundArch=*/"",
Action::OFK_SYCL);
auto *SYCLDeviceLibsDependenciesAction =
C.MakeAction<OffloadAction>(
Dep, SYCLDeviceLibsUnbundleAction->getType());
DeviceLinkObjects.push_back(SYCLDeviceLibsDependenciesAction);
if (TC->getTriple().isNVPTX()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a separate path for NVPTX backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are specific cubin files that are in the fat objects that are used. I only created .bc device libraries for the SYCL specific linking.

auto *SYCLDeviceLibsInputAction =
C.MakeAction<InputAction>(*InputArg, types::TY_Object);
auto *SYCLDeviceLibsUnbundleAction =
C.MakeAction<OffloadUnbundlingJobAction>(
SYCLDeviceLibsInputAction);

// We are using BoundArch="" here since the NVPTX bundles in
// the devicelib .o files do not contain any arch information
SYCLDeviceLibsUnbundleAction->registerDependentActionInfo(
TC, /*BoundArch=*/"", Action::OFK_SYCL);
OffloadAction::DeviceDependences Dep;
Dep.add(*SYCLDeviceLibsUnbundleAction, *TC, /*BoundArch=*/"",
Action::OFK_SYCL);
auto *SYCLDeviceLibsDependenciesAction =
C.MakeAction<OffloadAction>(
Dep, SYCLDeviceLibsUnbundleAction->getType());
DeviceLinkObjects.push_back(SYCLDeviceLibsDependenciesAction);
} else {
// We are using the LLVM-IR device libraries directly, no need
// to unbundle any objects.
auto *SYCLDeviceLibsInputAction =
C.MakeAction<InputAction>(*InputArg, types::TY_LLVM_BC);
DeviceLinkObjects.push_back(SYCLDeviceLibsInputAction);
}
if (!LibLocSelected)
LibLocSelected = !LibLocSelected;
}
Expand Down
73 changes: 47 additions & 26 deletions clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
}
}
}
StringRef LibSuffix =
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() ? ".obj"
: ".o";
using SYCLDeviceLibsList = SmallVector<DeviceLibOptInfo, 5>;

const SYCLDeviceLibsList SYCLDeviceWrapperLibs = {
Expand Down Expand Up @@ -294,7 +291,12 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
const SYCLDeviceLibsList SYCLDeviceSanitizerLibs = {
{"libsycl-sanitizer", "internal"}};
#endif

StringRef LibSuffix = ".bc";
if (TargetTriple.isNVPTX())
// For NVidia, we are unbundling objects.
LibSuffix = C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment()
? ".obj"
: ".o";
auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) {
for (const DeviceLibOptInfo &Lib : LibsList) {
if (!DeviceLibLinkInfo[Lib.DeviceLibOption])
Expand Down Expand Up @@ -367,19 +369,35 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
// The list should match pre-built SYCL device library files located in
// compiler package. Once we add or remove any SYCL device library files,
// the list should be updated accordingly.
static llvm::SmallVector<StringRef, 16> SYCLDeviceLibList {
"bfloat16", "crt", "cmath", "cmath-fp64", "complex", "complex-fp64",
static llvm::SmallVector<StringRef, 16> SYCLDeviceLibList{
"bfloat16",
"crt",
"cmath",
"cmath-fp64",
"complex",
"complex-fp64",
#if defined(_WIN32)
"msvc-math",
"msvc-math",
#else
"sanitizer",
"sanitizer",
#endif
"imf", "imf-fp64", "itt-compiler-wrappers", "itt-stubs",
"itt-user-wrappers", "fallback-cassert", "fallback-cstring",
"fallback-cmath", "fallback-cmath-fp64", "fallback-complex",
"fallback-complex-fp64", "fallback-imf", "fallback-imf-fp64",
"fallback-imf-bf16", "fallback-bfloat16", "native-bfloat16"
};
"imf",
"imf-fp64",
"imf-bf16",
"itt-compiler-wrappers",
"itt-stubs",
"itt-user-wrappers",
"fallback-cassert",
"fallback-cstring",
"fallback-cmath",
"fallback-cmath-fp64",
"fallback-complex",
"fallback-complex-fp64",
"fallback-imf",
"fallback-imf-fp64",
"fallback-imf-bf16",
"fallback-bfloat16",
"native-bfloat16"};

const char *SYCL::Linker::constructLLVMLinkCommand(
Compilation &C, const JobAction &JA, const InputInfo &Output,
Expand Down Expand Up @@ -415,15 +433,16 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
};
auto isSYCLDeviceLib = [&](const InputInfo &II) {
const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
StringRef LibPostfix = ".o";
if (isNoRDCDeviceCodeLink(II))
LibPostfix = ".bc";
else if (HostTC->getTriple().isWindowsMSVCEnvironment() &&
C.getDriver().IsCLMode())
LibPostfix = ".obj";
const bool IsNVPTX = this->getToolChain().getTriple().isNVPTX();
StringRef LibPostfix = ".bc";
if (IsNVPTX) {
LibPostfix = ".o";
if (HostTC->getTriple().isWindowsMSVCEnvironment() &&
C.getDriver().IsCLMode())
LibPostfix = ".obj";
}
std::string FileName = this->getToolChain().getInputFilename(II);
StringRef InputFilename = llvm::sys::path::filename(FileName);
const bool IsNVPTX = this->getToolChain().getTriple().isNVPTX();
if (IsNVPTX || IsSYCLNativeCPU) {
// Linking SYCL Device libs requires libclc as well as libdevice
if ((InputFilename.find("libspirv") != InputFilename.npos ||
Expand All @@ -434,8 +453,7 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
}
StringRef LibSyclPrefix("libsycl-");
if (!InputFilename.starts_with(LibSyclPrefix) ||
!InputFilename.ends_with(LibPostfix) ||
(InputFilename.count('-') < 2))
!InputFilename.ends_with(LibPostfix))
return false;
// Skip the prefix "libsycl-"
std::string PureLibName =
Expand All @@ -452,7 +470,10 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
PureLibName.substr(0, FinalDashPos) + PureLibName.substr(DotPos);
}
for (const auto &L : SYCLDeviceLibList) {
if (StringRef(PureLibName).starts_with(L))
std::string DeviceLibName(L);
DeviceLibName.append(LibPostfix);
if (StringRef(PureLibName).equals(DeviceLibName) ||
(IsNVPTX && StringRef(PureLibName).starts_with(L)))
return true;
}
return false;
Expand All @@ -463,11 +484,11 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
for (size_t Idx = 1; Idx < InputFileNum; ++Idx)
LinkSYCLDeviceLibs =
LinkSYCLDeviceLibs && isSYCLDeviceLib(InputFiles[Idx]);
// Go through the Inputs to the link. When a listfile is encountered, we
// know it is an unbundled generated list.
if (LinkSYCLDeviceLibs) {
Opts.push_back("-only-needed");
}
// Go through the Inputs to the link. When a listfile is encountered, we
// know it is an unbundled generated list.
for (const auto &II : InputFiles) {
std::string FileName = getToolChain().getInputFilename(II);
if (II.getType() == types::TY_Tempfilelist) {
Expand Down
Empty file.
Empty file.
Empty file.
22 changes: 11 additions & 11 deletions clang/test/Driver/sycl-bfloat16-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@
// RUN: --sysroot=%S/Inputs/SYCL -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=BFLOAT16-FALLBACK-FALLBACK

// BFLOAT16-NOT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-{{fallback|native}}-bfloat16.o" "-output={{.*}}libsycl-{{fallback|native}}-{{.*}}.o" "-unbundle"
// BFLOAT16-NOT: llvm-link{{.*}} "{{.*}}libsycl-{{fallback|native}}-bfloat16.bc"

// BFLOAT16-NATIVE: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_gen-unknown-unknown" "-input={{.*}}libsycl-native-bfloat16.o"
// BFLOAT16-NATIVE: llvm-link{{.*}} "{{.*}}libsycl-native-bfloat16.bc"

// BFLOAT16-FALLBACK: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-{{spir64_gen-|spir64-}}unknown-unknown" "-input={{.*}}libsycl-fallback-bfloat16.o"
// BFLOAT16-FALLBACK: llvm-link{{.*}} "{{.*}}libsycl-fallback-bfloat16.bc"

// BFLOAT16-NONE-NATIVE-NOT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}-bfloat16.o"
// BFLOAT16-NONE-NATIVE: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_gen-unknown-unknown" "-input={{.*}}libsycl-native-bfloat16.o"
// BFLOAT16-NONE-NATIVE-NOT: llvm-link{{.*}} "{{.*}}-bfloat16.bc"
// BFLOAT16-NONE-NATIVE: llvm-link{{.*}} "{{.*}}libsycl-native-bfloat16.bc"

// BFLOAT16-NONE-FALLBACK-NOT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}-bfloat16.o"
// BFLOAT16-NONE-FALLBACK: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_gen-unknown-unknown" "-input={{.*}}libsycl-fallback-bfloat16.o"
// BFLOAT16-NONE-FALLBACK-NOT: llvm-link{{.*}} "{{.*}}-bfloat16.bc"
// BFLOAT16-NONE-FALLBACK: llvm-link{{.*}} "{{.*}}libsycl-fallback-bfloat16.bc"

// BFLOAT16-FALLBACK-NATIVE: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_x86_64-unknown-unknown" "-input={{.*}}libsycl-fallback-bfloat16.o" "-output={{.*}}libsycl-fallback-bfloat16-{{.*}}.o" "-unbundle"
// BFLOAT16-FALLBACK-NATIVE: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_gen-unknown-unknown" "-input={{.*}}libsycl-native-bfloat16.o"
// BFLOAT16-FALLBACK-NATIVE: llvm-link{{.*}} "{{.*}}libsycl-fallback-bfloat16.bc"
// BFLOAT16-FALLBACK-NATIVE: {{.*}}libsycl-native-bfloat16.bc"

// BFLOAT16-FALLBACK-FALLBACK: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_x86_64-unknown-unknown" "-input={{.*}}libsycl-fallback-bfloat16.o" "-output={{.*}}libsycl-fallback-bfloat16-{{.*}}.o" "-unbundle"
// BFLOAT16-FALLBACK-FALLBACK: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_gen-unknown-unknown" "-input={{.*}}libsycl-fallback-bfloat16.o"
// BFLOAT16-FALLBACK-FALLBACK: llvm-link{{.*}} "{{.*}}libsycl-fallback-bfloat16.bc"
// BFLOAT16-FALLBACK-FALLBACK: "{{.*}}libsycl-fallback-bfloat16.bc"
Loading