Skip to content

Commit 307c74e

Browse files
committed
Address review comments
Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent 679436a commit 307c74e

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

clang/docs/ClangSYCLLinker.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ be passed down to downstream tools like 'llvm-link', 'llvm-spirv', etc.
5656
-help-hidden Display all available options
5757
-help Display available options (--help-hidden for more)
5858
--library-path=<dir> Set the library path for SYCL device libraries
59+
--device-libs=<value> A comma separated list of device libraries that are linked during the device link
5960
-o <path> Path to file to write output
6061
--save-temps Save intermediate results
6162
--triple <value> Specify the target triple.
@@ -64,6 +65,7 @@ be passed down to downstream tools like 'llvm-link', 'llvm-spirv', etc.
6465
-spirv-dump-device-code=<dir> Directory to dump SPIR-V IR code into
6566
-is-windows-msvc-env Specify if we are compiling under windows environment
6667
-llvm-spirv-options=<value> Pass options to llvm-spirv tool
68+
--llvm-spirv-path=<dir> Set the system llvm-spirv path
6769
6870
Example
6971
=======

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//
33
// Test a simple case without arguments
44
// RUN: %clangxx -fsycl -emit-llvm -c %s -o %t.bc
5-
// RUN: clang-sycl-linker --dry-run -triple spirv64 %t.bc --library-path=%S/Inputs -o a.spv 2>&1 \
5+
// RUN: echo ' ' > %T/lib1.bc
6+
// RUN: echo ' ' > %T/lib2.bc
7+
// RUN: clang-sycl-linker --dry-run -triple spirv64 %t.bc --library-path=%T --device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
68
// RUN: | FileCheck %s --check-prefix=CMDS
79
// CMDS: "{{.*}}llvm-link{{.*}}" {{.*}}.bc -o [[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
8-
// CMDS-NEXT: "{{.*}}llvm-link{{.*}}" -only-needed [[FIRSTLLVMLINKOUT]].bc {{.*}}libsycl-crt.bc {{.*}}libsycl-complex.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
10+
// CMDS-NEXT: "{{.*}}llvm-link{{.*}}" -only-needed [[FIRSTLLVMLINKOUT]].bc {{.*}}lib1.bc {{.*}}lib2.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
911
// CMDS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[SECONDLLVMLINKOUT]].bc

clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,6 @@ Expected<std::string> findProgram(const ArgList &Args, StringRef Name,
156156
return *Path;
157157
}
158158

159-
std::optional<std::string> findFile(StringRef Dir, StringRef Root,
160-
const Twine &Name) {
161-
SmallString<128> Path;
162-
if (Dir.starts_with("="))
163-
sys::path::append(Path, Root, Dir.substr(1), Name);
164-
else
165-
sys::path::append(Path, Dir, Name);
166-
167-
if (sys::fs::exists(Path))
168-
return static_cast<std::string>(Path);
169-
return std::nullopt;
170-
}
171-
172159
void printCommands(ArrayRef<StringRef> CmdArgs) {
173160
if (CmdArgs.empty())
174161
return;
@@ -238,43 +225,31 @@ Expected<StringRef> linkDeviceInputFiles(ArrayRef<std::string> InputFiles,
238225
return *OutFileOrErr;
239226
}
240227

241-
const SmallVector<std::string> SYCLDeviceLibNames = {
242-
"libsycl-crt.bc",
243-
"libsycl-complex.bc",
244-
"libsycl-complex-fp64.bc",
245-
"libsycl-cmath.bc",
246-
"libsycl-cmath-fp64.bc",
247-
"libsycl-imf.bc",
248-
"libsycl-imf-fp64.bc",
249-
"libsycl-imf-bf16.bc",
250-
"libsycl-fallback-cassert.bc",
251-
"libsycl-fallback-cstring.bc",
252-
"libsycl-fallback-complex.bc",
253-
"libsycl-fallback-complex-fp64.bc",
254-
"libsycl-fallback-cmath.bc",
255-
"libsycl-fallback-cmath-fp64.bc",
256-
"libsycl-fallback-imf.bc",
257-
"libsycl-fallback-imf-fp64.bc",
258-
"libsycl-fallback-imf-bf16.bc",
259-
"libsycl-fallback-bfloat16.bc",
260-
"libsycl-native-bfloat16.bc",
261-
"libsycl-itt-user-wrappers.bc",
262-
"libsycl-itt-compiler-wrappers.bc",
263-
"libsycl-itt-stubs.bc",
264-
"libsycl-sanitizer.bc"};
265-
266-
Expected<SmallVector<std::string>> getSYCLDeviceLibFiles(const ArgList &Args) {
228+
// This utility function is used to gather all SYCL device library files that
229+
// will be linked with input device files.
230+
// The list of files and its location are passed from driver.
231+
Expected<SmallVector<std::string>> getSYCLDeviceLibs(const ArgList &Args) {
267232
SmallVector<std::string> DeviceLibFiles;
268233
StringRef LibraryPath;
269234
if (Arg *A = Args.getLastArg(OPT_library_path_EQ))
270235
LibraryPath = A->getValue();
271236
if (LibraryPath.empty())
272237
return DeviceLibFiles;
273-
for (auto &DeviceLibName : SYCLDeviceLibNames) {
274-
std::optional<std::string> Filename =
275-
findFile(LibraryPath, /*Root=*/"", DeviceLibName);
276-
if (Filename)
277-
DeviceLibFiles.push_back(*Filename);
238+
if (Arg *A = Args.getLastArg(OPT_device_libs_EQ)) {
239+
if (A->getValues().size() == 0)
240+
return createStringError(
241+
inconvertibleErrorCode(),
242+
"Number of device library files cannot be zero.");
243+
for (StringRef Val : A->getValues()) {
244+
SmallString<128> LibName(LibraryPath);
245+
llvm::sys::path::append(LibName, Val);
246+
if (llvm::sys::fs::exists(LibName))
247+
DeviceLibFiles.push_back(std::string(LibName));
248+
else
249+
return createStringError(inconvertibleErrorCode(),
250+
std::string(LibName) +
251+
" SYCL device library file is not found.");
252+
}
278253
}
279254
return DeviceLibFiles;
280255
}
@@ -288,7 +263,7 @@ static Expected<StringRef> linkDeviceLibFiles(StringRef InputFile,
288263
const ArgList &Args) {
289264
llvm::TimeTraceScope TimeScope("LinkDeviceLibraryFiles");
290265

291-
auto SYCLDeviceLibFiles = getSYCLDeviceLibFiles(Args);
266+
auto SYCLDeviceLibFiles = getSYCLDeviceLibs(Args);
292267
if (!SYCLDeviceLibFiles)
293268
return SYCLDeviceLibFiles.takeError();
294269
if ((*SYCLDeviceLibFiles).empty())
@@ -472,8 +447,7 @@ int main(int argc, char **argv) {
472447

473448
if (Args.hasArg(OPT_help) || Args.hasArg(OPT_help_hidden)) {
474449
Tbl.printHelp(
475-
outs(),
476-
"clang-sycl-linker [options] <options to sycl link steps>",
450+
outs(), "clang-sycl-linker [options] <options to sycl link steps>",
477451
"A utility that wraps around several steps required to link SYCL "
478452
"device files.\n"
479453
"This enables LLVM IR linking, post-linking and code generation for "

clang/tools/clang-sycl-linker/SYCLLinkOpts.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def output : Separate<["--"], "output-file">, Alias<o>, Flags<[HelpHidden]>,
2020
def library_path_EQ : Joined<["--", "-"], "library-path=">,
2121
Flags<[HelpHidden]>, HelpText<"Add <dir> to the library search path">;
2222

23+
def device_libs_EQ : CommaJoined<["--", "-"], "device-libs=">,
24+
Flags<[WrapperOnlyOption]>,
25+
HelpText<"A comma separated list of device libraries that are linked during the device link.">;
26+
2327
def triple : Joined<["--"], "triple">,
2428
HelpText<"The device target triple">;
2529
def arch : Separate<["--", "-"], "arch">,
@@ -41,6 +45,10 @@ def spirv_dump_device_code_EQ : Joined<["--", "-"], "spirv-dump-device-code=">,
4145
def is_windows_msvc_env : Flag<["--", "-"], "is-windows-msvc-env">,
4246
Flags<[WrapperOnlyOption, HelpHidden]>;
4347

48+
def llvm_spirv_path_EQ : Joined<["--"], "llvm-spirv-path=">,
49+
Flags<[WrapperOnlyOption]>, MetaVarName<"<dir>">,
50+
HelpText<"Set the system llvm-spirv path">;
51+
4452
// Options to pass to llvm-spirv tool
4553
def llvm_spirv_options_EQ : Joined<["--", "-"], "llvm-spirv-options=">,
4654
Flags<[WrapperOnlyOption]>,

0 commit comments

Comments
 (0)