Skip to content

Commit ece73ad

Browse files
authored
[NewOffloadModel][SYCL DeviceLib] Generate SYCL device library objects using new offload model (#13579)
Following are changes in this PR: 1. Generate new SYCL device library files using new offload model 2. Pass these new files to clang-linker-wrapper 3. Avoid use of these files in old offload model 4. Remove support for bundled objects in clang-linker-wrapper (This can be added back in a cleaner way if there is need for backward compatibility). Thanks --------- Signed-off-by: Arvind Sudarsanam <arvind.sudarsanam@intel.com>
1 parent 64cb0cf commit ece73ad

File tree

12 files changed

+259
-176
lines changed

12 files changed

+259
-176
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3684,8 +3684,11 @@ getLinkerArgs(Compilation &C, DerivedArgList &Args, bool IncludeObj = false) {
36843684
static bool IsSYCLDeviceLibObj(std::string ObjFilePath, bool isMSVCEnv) {
36853685
StringRef ObjFileName = llvm::sys::path::filename(ObjFilePath);
36863686
StringRef ObjSuffix = isMSVCEnv ? ".obj" : ".o";
3687+
StringRef NewObjSuffix = isMSVCEnv ? ".new.obj" : ".new.o";
36873688
bool Ret =
3688-
(ObjFileName.starts_with("libsycl-") && ObjFileName.ends_with(ObjSuffix))
3689+
(ObjFileName.starts_with("libsycl-") &&
3690+
ObjFileName.ends_with(ObjSuffix) &&
3691+
!ObjFileName.ends_with(NewObjSuffix)) // Avoid new-offload-driver objs
36893692
? true
36903693
: false;
36913694
return Ret;
@@ -7878,6 +7881,11 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
78787881
break;
78797882
}
78807883

7884+
// Backend/Assemble actions are not used for the SYCL device side
7885+
if (Kind == Action::OFK_SYCL &&
7886+
(Phase == phases::Backend || Phase == phases::Assemble))
7887+
continue;
7888+
78817889
auto TCAndArch = TCAndArchs.begin();
78827890
for (Action *&A : DeviceActions) {
78837891
if (A->getType() == types::TY_Nothing)

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11024,7 +11024,10 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
1102411024
SYCLInstallationDetector SYCLInstallation(D);
1102511025
SYCLInstallation.getSYCLDeviceLibPath(LibLocCandidates);
1102611026
SmallString<128> LibName("libsycl-crt");
11027-
StringRef LibSuffix = TheTriple.isWindowsMSVCEnvironment() ? ".obj" : ".o";
11027+
bool IsNewOffload = D.getUseNewOffloadingDriver();
11028+
StringRef LibSuffix = TheTriple.isWindowsMSVCEnvironment()
11029+
? (IsNewOffload ? ".new.obj" : ".obj")
11030+
: (IsNewOffload ? ".new.o" : ".o");
1102811031
llvm::sys::path::replace_extension(LibName, LibSuffix);
1102911032
for (const auto &LibLoc : LibLocCandidates) {
1103011033
SmallString<128> FullLibName(LibLoc);

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,16 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
291291
const SYCLDeviceLibsList SYCLDeviceSanitizerLibs = {
292292
{"libsycl-sanitizer", "internal"}};
293293
#endif
294+
bool IsWindowsMSVCEnv =
295+
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
296+
bool IsNewOffload = C.getDriver().getUseNewOffloadingDriver();
294297
StringRef LibSuffix = ".bc";
295298
if (TargetTriple.isNVPTX())
296299
// For NVidia, we are unbundling objects.
297-
LibSuffix = C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment()
298-
? ".obj"
299-
: ".o";
300+
LibSuffix = IsWindowsMSVCEnv ? ".obj" : ".o";
301+
if (IsNewOffload)
302+
// For new offload model, we use packaged .bc files.
303+
LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o";
300304
auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) {
301305
for (const DeviceLibOptInfo &Lib : LibsList) {
302306
if (!DeviceLibLinkInfo[Lib.DeviceLibOption])
@@ -441,19 +445,26 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
441445
C.getDriver().IsCLMode())
442446
LibPostfix = ".obj";
443447
}
448+
StringRef NewLibPostfix = ".new.o";
449+
if (HostTC->getTriple().isWindowsMSVCEnvironment() &&
450+
C.getDriver().IsCLMode())
451+
NewLibPostfix = ".new.obj";
444452
std::string FileName = this->getToolChain().getInputFilename(II);
445453
StringRef InputFilename = llvm::sys::path::filename(FileName);
446454
if (IsNVPTX || IsSYCLNativeCPU) {
447455
// Linking SYCL Device libs requires libclc as well as libdevice
448456
if ((InputFilename.find("libspirv") != InputFilename.npos ||
449457
InputFilename.find("libdevice") != InputFilename.npos))
450458
return true;
451-
if (IsNVPTX)
459+
if (IsNVPTX) {
452460
LibPostfix = ".cubin";
461+
NewLibPostfix = ".new.cubin";
462+
}
453463
}
454464
StringRef LibSyclPrefix("libsycl-");
455465
if (!InputFilename.starts_with(LibSyclPrefix) ||
456-
!InputFilename.ends_with(LibPostfix))
466+
!InputFilename.ends_with(LibPostfix) ||
467+
InputFilename.ends_with(NewLibPostfix))
457468
return false;
458469
// Skip the prefix "libsycl-"
459470
std::string PureLibName =
68.6 KB
Binary file not shown.
59.3 KB
Binary file not shown.
29.5 KB
Binary file not shown.
36.6 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: system-windows
2+
3+
/// Check for list of commands for standalone clang-linker-wrapper run for sycl
4+
// RUN: clang-linker-wrapper -sycl-device-library-location=%S/Inputs -sycl-device-libraries=libsycl-crt.new.obj,libsycl-complex.new.obj -sycl-post-link-options="SYCL_POST_LINK_OPTIONS" -llvm-spirv-options="LLVM_SPIRV_OPTIONS" "--host-triple=x86_64-pc-windows-msvc" "--triple=spir64" "--linker-path=/usr/bin/ld" "--" HOST_LINKER_FLAGS "-dynamic-linker" HOST_DYN_LIB "-o" "a.out" HOST_LIB_PATH HOST_STAT_LIB %S/Inputs/test-sycl.o --dry-run 2>&1 | FileCheck -check-prefix=CHK-CMDS %s
5+
// CHK-CMDS: "{{.*}}spirv-to-ir-wrapper.exe" {{.*}} -o [[FIRSTLLVMLINKIN:.*]].bc --llvm-spirv-opts=--spirv-preserve-auxdata --llvm-spirv-opts=--spirv-target-env=SPV-IR --llvm-spirv-opts=--spirv-builtin-format=global
6+
// CHK-CMDS-NEXT: "{{.*}}llvm-link.exe" [[FIRSTLLVMLINKIN:.*]].bc -o [[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
7+
// CHK-CMDS-NEXT: "{{.*}}llvm-link.exe" -only-needed [[FIRSTLLVMLINKOUT]].bc {{.*}}.bc {{.*}}.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
8+
// CHK-CMDS-NEXT: "{{.*}}sycl-post-link.exe" SYCL_POST_LINK_OPTIONS -o [[SYCLPOSTLINKOUT:.*]].table [[SECONDLLVMLINKOUT]].bc
9+
// LLVM-SPIRV is not called in dry-run
10+
// CHK-CMDS-NEXT: offload-wrapper: input: [[LLVMSPIRVOUT:.*]].table, output: [[WRAPPEROUT:.*]].bc
11+
// CHK-CMDS-NEXT: "{{.*}}llc.exe" -filetype=obj -o [[LLCOUT:.*]].o [[WRAPPEROUT]].bc
12+
// CHK-CMDS-NEXT: "{{.*}}/ld" -- HOST_LINKER_FLAGS -dynamic-linker HOST_DYN_LIB -o a.out [[LLCOUT]].o HOST_LIB_PATH HOST_STAT_LIB {{.*}}test-sycl.o

clang/test/Driver/linker-wrapper-sycl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// REQUIRES: system-linux
22

33
/// Check for list of commands for standalone clang-linker-wrapper run for sycl
4-
// RUN: clang-linker-wrapper -sycl-device-library-location=%S/Inputs -sycl-device-libraries=libsycl-crt.o,libsycl-complex.o -sycl-post-link-options="SYCL_POST_LINK_OPTIONS" -llvm-spirv-options="LLVM_SPIRV_OPTIONS" "--host-triple=x86_64-unknown-linux-gnu" "--triple=spir64" "--linker-path=/usr/bin/ld" "--" HOST_LINKER_FLAGS "-dynamic-linker" HOST_DYN_LIB "-o" "a.out" HOST_LIB_PATH HOST_STAT_LIB %S/Inputs/test-sycl.o --dry-run 2>&1 | FileCheck -check-prefix=CHK-CMDS %s
5-
// CHK-CMDS: "{{.*}}llvm-link" [[INPUT:.*]].bc -o [[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
6-
// CHK-CMDS-NEXT: "{{.*}}clang-offload-bundler" -type=o -targets=sycl-spir64-unknown-unknown -input={{.*}}libsycl-crt.o -output=[[FIRSTUNBUNDLEDLIB:.*]].bc -unbundle -allow-missing-bundles
7-
// CHK-CMDS-NEXT: "{{.*}}clang-offload-bundler" -type=o -targets=sycl-spir64-unknown-unknown -input={{.*}}libsycl-complex.o -output=[[SECONDUNBUNDLEDLIB:.*]].bc -unbundle -allow-missing-bundles
8-
// CHK-CMDS-NEXT: "{{.*}}llvm-link" -only-needed [[FIRSTLLVMLINKOUT]].bc [[FIRSTUNBUNDLEDLIB]].bc [[SECONDUNBUNDLEDLIB]].bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
4+
// RUN: clang-linker-wrapper -sycl-device-library-location=%S/Inputs -sycl-device-libraries=libsycl-crt.new.o,libsycl-complex.new.o -sycl-post-link-options="SYCL_POST_LINK_OPTIONS" -llvm-spirv-options="LLVM_SPIRV_OPTIONS" "--host-triple=x86_64-unknown-linux-gnu" "--triple=spir64" "--linker-path=/usr/bin/ld" "--" HOST_LINKER_FLAGS "-dynamic-linker" HOST_DYN_LIB "-o" "a.out" HOST_LIB_PATH HOST_STAT_LIB %S/Inputs/test-sycl.o --dry-run 2>&1 | FileCheck -check-prefix=CHK-CMDS %s
5+
// CHK-CMDS: "{{.*}}spirv-to-ir-wrapper" {{.*}} -o [[FIRSTLLVMLINKIN:.*]].bc --llvm-spirv-opts=--spirv-preserve-auxdata --llvm-spirv-opts=--spirv-target-env=SPV-IR --llvm-spirv-opts=--spirv-builtin-format=global
6+
// CHK-CMDS-NEXT: "{{.*}}llvm-link" [[FIRSTLLVMLINKIN:.*]].bc -o [[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
7+
// CHK-CMDS-NEXT: "{{.*}}llvm-link" -only-needed [[FIRSTLLVMLINKOUT]].bc {{.*}}.bc {{.*}}.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
98
// CHK-CMDS-NEXT: "{{.*}}sycl-post-link" SYCL_POST_LINK_OPTIONS -o [[SYCLPOSTLINKOUT:.*]].table [[SECONDLLVMLINKOUT]].bc
109
// LLVM-SPIRV is not called in dry-run
1110
// CHK-CMDS-NEXT: offload-wrapper: input: [[LLVMSPIRVOUT:.*]].table, output: [[WRAPPEROUT:.*]].bc

clang/test/Driver/sycl-offload-new-driver.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// REQUIRES: system-linux
2-
32
/// Verify --offload-new-driver option phases
43
// RUN: %clang --target=x86_64-unknown-linux-gnu -fsycl -fsycl-targets=nvptx64-nvidia-cuda,spir64 --offload-new-driver -ccc-print-phases %s 2>&1 \
54
// RUN: | FileCheck -check-prefix=OFFLOAD-NEW-DRIVER %s
@@ -10,20 +9,16 @@
109
// OFFLOAD-NEW-DRIVER: 4: input, "[[INPUT]]", c++, (device-sycl)
1110
// OFFLOAD-NEW-DRIVER: 5: preprocessor, {4}, c++-cpp-output, (device-sycl)
1211
// OFFLOAD-NEW-DRIVER: 6: compiler, {5}, ir, (device-sycl)
13-
// OFFLOAD-NEW-DRIVER: 7: backend, {6}, assembler, (device-sycl)
14-
// OFFLOAD-NEW-DRIVER: 8: assembler, {7}, object, (device-sycl)
15-
// OFFLOAD-NEW-DRIVER: 9: offload, "device-sycl (nvptx64-nvidia-cuda)" {8}, object
16-
// OFFLOAD-NEW-DRIVER: 10: input, "[[INPUT]]", c++, (device-sycl)
17-
// OFFLOAD-NEW-DRIVER: 11: preprocessor, {10}, c++-cpp-output, (device-sycl)
18-
// OFFLOAD-NEW-DRIVER: 12: compiler, {11}, ir, (device-sycl)
19-
// OFFLOAD-NEW-DRIVER: 13: backend, {12}, assembler, (device-sycl)
20-
// OFFLOAD-NEW-DRIVER: 14: assembler, {13}, object, (device-sycl)
21-
// OFFLOAD-NEW-DRIVER: 15: offload, "device-sycl (spir64-unknown-unknown)" {14}, object
22-
// OFFLOAD-NEW-DRIVER: 16: clang-offload-packager, {9, 15}, image, (device-sycl)
23-
// OFFLOAD-NEW-DRIVER: 17: offload, "host-sycl (x86_64-unknown-linux-gnu)" {3}, "device-sycl (x86_64-unknown-linux-gnu)" {16}, ir
24-
// OFFLOAD-NEW-DRIVER: 18: backend, {17}, assembler, (host-sycl)
25-
// OFFLOAD-NEW-DRIVER: 19: assembler, {18}, object, (host-sycl)
26-
// OFFLOAD-NEW-DRIVER: 20: clang-linker-wrapper, {19}, image, (host-sycl)
12+
// OFFLOAD-NEW-DRIVER: 7: offload, "device-sycl (nvptx64-nvidia-cuda)" {6}, ir
13+
// OFFLOAD-NEW-DRIVER: 8: input, "[[INPUT]]", c++, (device-sycl)
14+
// OFFLOAD-NEW-DRIVER: 9: preprocessor, {8}, c++-cpp-output, (device-sycl)
15+
// OFFLOAD-NEW-DRIVER: 10: compiler, {9}, ir, (device-sycl)
16+
// OFFLOAD-NEW-DRIVER: 11: offload, "device-sycl (spir64-unknown-unknown)" {10}, ir
17+
// OFFLOAD-NEW-DRIVER: 12: clang-offload-packager, {7, 11}, image, (device-sycl)
18+
// OFFLOAD-NEW-DRIVER: 13: offload, "host-sycl (x86_64-unknown-linux-gnu)" {3}, "device-sycl (x86_64-unknown-linux-gnu)" {12}, ir
19+
// OFFLOAD-NEW-DRIVER: 14: backend, {13}, assembler, (host-sycl)
20+
// OFFLOAD-NEW-DRIVER: 15: assembler, {14}, object, (host-sycl)
21+
// OFFLOAD-NEW-DRIVER: 16: clang-linker-wrapper, {15}, image, (host-sycl)
2722

2823
/// Check the toolflow for SYCL compilation using new offload model
2924
// RUN: %clangxx -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-targets=spir64 --offload-new-driver %s 2>&1 | FileCheck -check-prefix=CHK-FLOW %s
@@ -38,7 +33,7 @@
3833
// RUN: --sysroot=%S/Inputs/SYCL -### %s 2>&1 \
3934
// RUN: | FileCheck -check-prefix WRAPPER_OPTIONS %s
4035
// WRAPPER_OPTIONS: clang-linker-wrapper{{.*}} "--triple=spir64"
41-
// WRAPPER_OPTIONS-SAME: "-sycl-device-libraries=libsycl-crt.bc,libsycl-complex.bc,libsycl-complex-fp64.bc,libsycl-cmath.bc,libsycl-cmath-fp64.bc,libsycl-imf.bc,libsycl-imf-fp64.bc,libsycl-imf-bf16.bc,libsycl-itt-user-wrappers.bc,libsycl-itt-compiler-wrappers.bc,libsycl-itt-stubs.bc"
36+
// WRAPPER_OPTIONS-SAME: "-sycl-device-libraries=libsycl-crt.new.o,libsycl-complex.new.o,libsycl-complex-fp64.new.o,libsycl-cmath.new.o,libsycl-cmath-fp64.new.o,libsycl-imf.new.o,libsycl-imf-fp64.new.o,libsycl-imf-bf16.new.o,libsycl-itt-user-wrappers.new.o,libsycl-itt-compiler-wrappers.new.o,libsycl-itt-stubs.new.o"
4237
// WRAPPER_OPTIONS-SAME: "-sycl-device-library-location={{.*}}/lib"
4338

4439
// RUN: %clangxx --target=x86_64-unknown-linux-gnu -fsycl --offload-new-driver \

0 commit comments

Comments
 (0)