Skip to content

Commit 1307a22

Browse files
authored
[SYCL] Remove dependency on libdevice binary for sycl-device-lib test (#3639)
Signed-off-by: gejin <[email protected]>
1 parent 534ccdf commit 1307a22

31 files changed

+105
-58
lines changed

clang/lib/Driver/Driver.cpp

+28-19
Original file line numberDiff line numberDiff line change
@@ -4150,8 +4150,10 @@ class OffloadingActionBuilder final {
41504150
}
41514151
}
41524152

4153-
SmallString<128> LibLoc(TC->getDriver().Dir);
4154-
llvm::sys::path::append(LibLoc, "/../lib");
4153+
const toolchains::SYCLToolChain *SYCLTC =
4154+
static_cast<const toolchains::SYCLToolChain *>(TC);
4155+
SmallVector<SmallString<128>, 4> LibLocCandidates;
4156+
SYCLTC->SYCLInstallation.getSYCLDeviceLibPath(LibLocCandidates);
41554157
StringRef LibSuffix = isMSVCEnv ? ".obj" : ".o";
41564158
SmallVector<DeviceLibOptInfo, 5> sycl_device_wrapper_libs = {
41574159
{"libsycl-crt", "libc"},
@@ -4171,23 +4173,30 @@ class OffloadingActionBuilder final {
41714173
auto sycl_libs = (t == sycl_devicelib_wrapper)
41724174
? sycl_device_wrapper_libs
41734175
: sycl_device_fallback_libs;
4174-
for (const DeviceLibOptInfo &Lib : sycl_libs) {
4175-
if (!devicelib_link_info[Lib.devicelib_option])
4176-
continue;
4177-
SmallString<128> LibName(LibLoc);
4178-
llvm::sys::path::append(LibName, Lib.devicelib_name);
4179-
llvm::sys::path::replace_extension(LibName, LibSuffix);
4180-
if (llvm::sys::fs::exists(LibName)) {
4181-
++NumOfDeviceLibLinked;
4182-
Arg *InputArg = MakeInputArg(Args, C.getDriver().getOpts(),
4183-
Args.MakeArgString(LibName));
4184-
auto *SYCLDeviceLibsInputAction =
4185-
C.MakeAction<InputAction>(*InputArg, types::TY_Object);
4186-
auto *SYCLDeviceLibsUnbundleAction =
4187-
C.MakeAction<OffloadUnbundlingJobAction>(
4188-
SYCLDeviceLibsInputAction);
4189-
addDeviceDepences(SYCLDeviceLibsUnbundleAction);
4190-
DeviceLinkObjects.push_back(SYCLDeviceLibsUnbundleAction);
4176+
bool LibLocSelected = false;
4177+
for (const auto &LLCandidate : LibLocCandidates) {
4178+
if (LibLocSelected)
4179+
break;
4180+
for (const DeviceLibOptInfo &Lib : sycl_libs) {
4181+
if (!devicelib_link_info[Lib.devicelib_option])
4182+
continue;
4183+
SmallString<128> LibName(LLCandidate);
4184+
llvm::sys::path::append(LibName, Lib.devicelib_name);
4185+
llvm::sys::path::replace_extension(LibName, LibSuffix);
4186+
if (llvm::sys::fs::exists(LibName)) {
4187+
++NumOfDeviceLibLinked;
4188+
Arg *InputArg = MakeInputArg(Args, C.getDriver().getOpts(),
4189+
Args.MakeArgString(LibName));
4190+
auto *SYCLDeviceLibsInputAction =
4191+
C.MakeAction<InputAction>(*InputArg, types::TY_Object);
4192+
auto *SYCLDeviceLibsUnbundleAction =
4193+
C.MakeAction<OffloadUnbundlingJobAction>(
4194+
SYCLDeviceLibsInputAction);
4195+
addDeviceDepences(SYCLDeviceLibsUnbundleAction);
4196+
DeviceLinkObjects.push_back(SYCLDeviceLibsUnbundleAction);
4197+
if (!LibLocSelected)
4198+
LibLocSelected = !LibLocSelected;
4199+
}
41914200
}
41924201
}
41934202
};

clang/lib/Driver/ToolChains/SYCL.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ using namespace clang::driver::tools;
2222
using namespace clang;
2323
using namespace llvm::opt;
2424

25+
SYCLInstallationDetector::SYCLInstallationDetector(const Driver &D)
26+
: D(D), InstallationCandidates() {
27+
InstallationCandidates.emplace_back(D.Dir + "/..");
28+
}
29+
30+
void SYCLInstallationDetector::getSYCLDeviceLibPath(
31+
llvm::SmallVector<llvm::SmallString<128>, 4> &DeviceLibPaths) const {
32+
for (const auto &IC : InstallationCandidates) {
33+
llvm::SmallString<128> InstallLibPath(IC.str());
34+
InstallLibPath.append("/lib");
35+
DeviceLibPaths.emplace_back(InstallLibPath);
36+
}
37+
38+
DeviceLibPaths.emplace_back(D.SysRoot + "/lib");
39+
}
40+
41+
void SYCLInstallationDetector::print(llvm::raw_ostream &OS) const {
42+
if (!InstallationCandidates.size())
43+
return;
44+
OS << "SYCL Installation Candidates: \n";
45+
for (const auto &IC : InstallationCandidates) {
46+
OS << IC << "\n";
47+
}
48+
}
49+
2550
const char *SYCL::Linker::constructLLVMSpirvCommand(
2651
Compilation &C, const JobAction &JA, const InputInfo &Output,
2752
StringRef OutputFilePrefix, bool ToBc, const char *InputFileName) const {
@@ -581,7 +606,7 @@ void SYCL::x86_64::BackendCompiler::ConstructJob(
581606

582607
SYCLToolChain::SYCLToolChain(const Driver &D, const llvm::Triple &Triple,
583608
const ToolChain &HostTC, const ArgList &Args)
584-
: ToolChain(D, Triple, Args), HostTC(HostTC) {
609+
: ToolChain(D, Triple, Args), HostTC(HostTC), SYCLInstallation(D) {
585610
// Lookup binaries into the driver directory, this is used to
586611
// discover the clang-offload-bundler executable.
587612
getProgramPaths().push_back(getDriver().Dir);

clang/lib/Driver/ToolChains/SYCL.h

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
namespace clang {
1616
namespace driver {
1717

18+
class SYCLInstallationDetector {
19+
public:
20+
SYCLInstallationDetector(const Driver &D);
21+
void getSYCLDeviceLibPath(
22+
llvm::SmallVector<llvm::SmallString<128>, 4> &DeviceLibPaths) const;
23+
void print(llvm::raw_ostream &OS) const;
24+
25+
private:
26+
const Driver &D;
27+
llvm::SmallVector<llvm::SmallString<128>, 4> InstallationCandidates;
28+
};
29+
1830
class Command;
1931

2032
namespace tools {
@@ -162,6 +174,7 @@ class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
162174

163175

164176
const ToolChain &HostTC;
177+
const SYCLInstallationDetector SYCLInstallation;
165178

166179
protected:
167180
Tool *buildBackendCompiler() const override;

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-cmath-fp64.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-cmath.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-complex-fp64.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-complex.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-crt.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-fallback-cassert.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-fallback-cmath-fp64.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-fallback-cmath.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-fallback-complex-fp64.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-fallback-complex.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-itt-compiler-wrappers.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-itt-stubs.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL-windows/lib/libsycl-itt-user-wrappers.obj

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-cmath-fp64.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-cmath.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-complex-fp64.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-complex.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-crt.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-fallback-cassert.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-fallback-cmath-fp64.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-fallback-cmath.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-fallback-complex-fp64.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-fallback-complex.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-itt-compiler-wrappers.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-itt-stubs.o

Whitespace-only changes.

clang/test/Driver/Inputs/SYCL/lib/libsycl-itt-user-wrappers.o

Whitespace-only changes.

clang/test/Driver/sycl-device-lib-win.cpp

+19-19
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
/// ###########################################################################
88

99
/// test behavior of device library default link
10-
// RUN: %clangxx -fsycl %s -### 2>&1 \
10+
// RUN: %clangxx -fsycl %s --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
1111
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT
12-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc -### 2>&1 \
12+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
1313
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT
14-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libm-fp32 -### 2>&1 \
14+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libm-fp32 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
1515
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT
16-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,libm-fp32 -### 2>&1 \
16+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,libm-fp32 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
1717
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT
1818
// SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-crt.obj" "-outputs={{.*}}libsycl-crt-{{.*}}.o" "-unbundle"
1919
// SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-complex.obj" "-outputs={{.*}}libsycl-complex-{{.*}}.o" "-unbundle"
@@ -28,15 +28,15 @@
2828

2929
/// ###########################################################################
3030
/// test behavior of device library link with libm-fp64
31-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libm-fp64 -### 2>&1 \
31+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libm-fp64 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
3232
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64
33-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,libm-fp64 -### 2>&1 \
33+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,libm-fp64 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
3434
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64
35-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=all -### 2>&1 \
35+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=all --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
3636
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64
37-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,libm-fp32,libm-fp64 -### 2>&1 \
37+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,libm-fp32,libm-fp64 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
3838
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64
39-
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,all -### 2>&1 \
39+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,all --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
4040
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64
4141
// SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-crt.obj" "-outputs={{.*}}libsycl-crt-{{.*}}.o" "-unbundle"
4242
// SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-complex.obj" "-outputs={{.*}}libsycl-complex-{{.*}}.o" "-unbundle"
@@ -52,7 +52,7 @@
5252
/// ###########################################################################
5353

5454
/// test behavior of -fno-sycl-device-lib=libc
55-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc -### 2>&1 \
55+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
5656
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC
5757
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-complex.obj" "-outputs={{.*}}libsycl-complex-{{.*}}.o" "-unbundle"
5858
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-complex-fp64.obj" "-outputs={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"
@@ -65,25 +65,25 @@
6565
/// ###########################################################################
6666

6767
/// test behavior of -fno-sycl-device-lib=libm-fp32,libm-fp64
68-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libm-fp32,libm-fp64 -### 2>&1 \
68+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libm-fp32,libm-fp64 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
6969
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBM
7070
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBM: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-crt.obj" "-outputs={{.*}}libsycl-crt-{{.*}}.o" "-unbundle"
7171
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBM-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-fallback-cassert.obj" "-outputs={{.*}}libsycl-fallback-cassert-{{.*}}.o" "-unbundle"
7272

7373
/// ###########################################################################
7474

7575
/// test behavior of disabling all device libraries
76-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,libm-fp32 -### 2>&1 \
76+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,libm-fp32 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
7777
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB
78-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=all -### 2>&1 \
78+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=all --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
7979
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB
80-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,all -### 2>&1 \
80+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,all --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
8181
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB
82-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libm-fp32,all -### 2>&1 \
82+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libm-fp32,all --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
8383
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB
84-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libm-fp64,all -### 2>&1 \
84+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libm-fp64,all --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
8585
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB
86-
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 -### 2>&1 \
86+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=libc,all,libm-fp64,libm-fp32 --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
8787
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB
8888
// SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB: {{.*}}clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown-sycldevice"
8989
// SYCL_DEVICE_LIB_UNBUNDLE_NO_DEVICE_LIB-NEXT: {{.*}}llvm-link{{.*}} {{.*}} "--suppress-warnings"
@@ -100,7 +100,7 @@
100100

101101
/// ###########################################################################
102102
/// test llvm-link behavior for linking device libraries
103-
// RUN: %clangxx -fsycl %s -### 2>&1 \
103+
// RUN: %clangxx -fsycl %s --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
104104
// RUN: | FileCheck %s -check-prefix=SYCL_LLVM_LINK_DEVICE_LIB
105105
// SYCL_LLVM_LINK_DEVICE_LIB: llvm-link{{.*}} "{{.*}}.bc" "-o" "{{.*}}.bc" "--suppress-warnings"
106106
// SYCL_LLVM_LINK_DEVICE_LIB-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs={{.*}}libsycl-crt.obj" "-outputs={{.*}}libsycl-crt-{{.*}}.o" "-unbundle"
@@ -117,7 +117,7 @@
117117

118118
/// ###########################################################################
119119
/// test llvm-link behavior for fno-sycl-device-lib
120-
// RUN: %clangxx -fsycl -fno-sycl-device-lib=all %s -### 2>&1 \
120+
// RUN: %clangxx -fsycl -fno-sycl-device-lib=all %s --sysroot=%S/Inputs/SYCL-windows -### 2>&1 \
121121
// RUN: | FileCheck %s -check-prefix=SYCL_LLVM_LINK_NO_DEVICE_LIB
122122
// SYCL_LLVM_LINK_NO_DEVICE_LIB: clang{{.*}} "-cc1" {{.*}} "-fsycl-is-device"
123123
// SYCL_LLVM_LINK_NO_DEVICE_LIB-NOT: llvm-link{{.*}} "-only-needed"

0 commit comments

Comments
 (0)