[clang] [clang-linker-wrapper][SYCL] Add --sycl-device-link (PR #240673)
Srividya Sundaram via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 19:10:43 PST 2026
https://github.com/srividya-sundaram created https://github.com/llvm/llvm-project/pull/240673
With --sycl-device-link, clang-linker-wrapper links and wraps the SYCL device code, then copies the wrapped host object to the output instead of running the host linker. The object can be linked later by any host linker. If there is no device code, an empty host object without SYCL runtime registration is written.
This is the wrapper part of -fsycl-link support.
>From 12e50b10dfa7ecd643c4d57d5b62590b1363442f Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Mon, 9 Nov 2026 18:49:21 -0800
Subject: [PATCH] [clang-linker-wrapper][SYCL] Add --sycl-device-link
With --sycl-device-link, clang-linker-wrapper links and wraps the SYCL
device code, then copies the wrapped host object to the output instead
of running the host linker. The object can be linked later by any host
linker. If there is no device code, an empty host object without SYCL
runtime registration is written.
This is the wrapper part of -fsycl-link support.
---
.../linker-wrapper-sycl-device-link.c | 62 ++++++++++++++++++
.../ClangLinkerWrapper.cpp | 63 ++++++++++++++++++-
.../clang-linker-wrapper/LinkerWrapperOpts.td | 6 ++
3 files changed, 129 insertions(+), 2 deletions(-)
create mode 100644 clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-sycl-device-link.c
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-sycl-device-link.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-sycl-device-link.c
new file mode 100644
index 00000000000000..2601dbcf39d95c
--- /dev/null
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-sycl-device-link.c
@@ -0,0 +1,62 @@
+// Test --sycl-device-link: SYCL device code is linked and wrapped into a host
+// object, which is copied to the output. The host linker is not run.
+
+// REQUIRES: x86-registered-target
+// REQUIRES: spirv-registered-target
+
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o %t.spirv.bc
+
+// RUN: llvm-offload-binary -o %t.sycl.out \
+// RUN: --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=generic
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl.o \
+// RUN: -fembed-offload-object=%t.sycl.out
+
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
+// LINK: "{{.*}}clang{{(.exe)?}}" {{.*}}--target=spirv64-unknown-unknown {{.*}}.o --sycl-link{{$}}
+// LINK-NEXT: "cp" {{.*}}dev.o.sycl.image.wrapper{{.*}}.o dev.o{{$}}
+
+// Archive members are only extracted for the host link, so use
+// --whole-archive to device link a static library.
+// RUN: rm -f %t.sycl.a && llvm-ar rcs %t.sycl.a %t.sycl.o
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld --whole-archive %t.sycl.a \
+// RUN: --no-whole-archive -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
+
+// The output is the wrapped device object with SYCL runtime registration.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o -o %t.dev.o
+// RUN: llvm-nm %t.dev.o | FileCheck %s --check-prefix=WRAPPED
+// WRAPPED-DAG: .sycl_offloading.binary
+// WRAPPED-DAG: U __sycl_register_lib
+
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o -o dev.obj 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WIN --implicit-check-not=/usr/bin/ld
+// WIN: "copy" {{.*}}dev.obj.sycl.image.wrapper{{.*}}.o dev.obj{{$}}
+
+// No device code: an empty host object without SYCL runtime registration is
+// written to the output.
+// RUN: rm -f %t.empty.o
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.elf.o -o %t.empty.o
+// RUN: llvm-readobj --file-header %t.empty.o | FileCheck %s --check-prefix=EMPTY
+// RUN: llvm-nm %t.empty.o | count 0
+// EMPTY: Format: elf64-x86-64
+
+// RUN: llvm-offload-binary -o %t.openmp.out \
+// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.openmp.o \
+// RUN: -fembed-offload-object=%t.openmp.out
+// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.openmp.o -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NOT-SYCL
+// NOT-SYCL: error: --sycl-device-link requires SYCL device code, got 'openmp'
+
+// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --emit-fatbin-only --linker-path=/usr/bin/ld \
+// RUN: %t.sycl.o -o dev.o 2>&1 | FileCheck %s --check-prefix=FATBIN
+// FATBIN: error: --sycl-device-link cannot be used with --emit-fatbin-only
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 1024d5e6e1a6c8..c083c694705de5 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1092,6 +1092,45 @@ Error handleOverrideImages(
return Error::success();
}
+/// Writes the result of --sycl-device-link to the output file. The wrapped SYCL
+/// device object is the final output, so the host linker is not run.
+Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
+ ArrayRef<StringRef> WrappedFiles,
+ const ArgList &Args) {
+ if (WrappedFiles.size() > 1)
+ return createStringError("expected a single wrapped SYCL device object, "
+ "got %zu",
+ WrappedFiles.size());
+
+ Triple HostTriple(
+ Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
+ StringRef File;
+ if (WrappedFiles.empty()) {
+ // No device code was linked. Emit an empty host object with no runtime
+ // registration, so the output can still be used in the host link.
+ LLVMContext Context;
+ Module M("offload.wrapper.module", Context);
+ M.setTargetTriple(HostTriple);
+ M.setDataLayout(HostTriple.computeDataLayout());
+ auto FileOrErr = compileModule(LibraryOptions, M, OFK_SYCL);
+ if (!FileOrErr)
+ return FileOrErr.takeError();
+ File = *FileOrErr;
+ } else {
+ File = WrappedFiles.front();
+ }
+
+ if (Verbose || DryRun)
+ printCommands(
+ {HostTriple.isOSWindows() ? "copy" : "cp", File, ExecutableName});
+ if (DryRun)
+ return Error::success();
+
+ if (std::error_code EC = sys::fs::copy_file(File, ExecutableName))
+ return createFileError(ExecutableName, EC);
+ return Error::success();
+}
+
/// Transforms all the extracted offloading input files into an image that can
/// be registered by the runtime. If NeedsWrapping is false, writes bundled
/// output directly without wrapping or host linking.
@@ -1612,6 +1651,22 @@ int main(int Argc, char **Argv) {
// linking.
bool EmitFatbinOnly = Args.hasArg(OPT_emit_fatbin_only);
+ // With --sycl-device-link, the wrapped device object is the output and the
+ // host linker is not run.
+ bool SYCLDeviceLink = Args.hasArg(OPT_sycl_device_link);
+ if (SYCLDeviceLink) {
+ if (EmitFatbinOnly)
+ reportError(createStringError(
+ "--sycl-device-link cannot be used with --emit-fatbin-only"));
+ for (const SmallVector<OffloadFile> &Files : *DeviceInputFiles)
+ for (const OffloadFile &File : Files)
+ if (OffloadKind Kind = File.getBinary()->getOffloadKind();
+ Kind != OFK_SYCL)
+ reportError(createStringError(
+ "--sycl-device-link requires SYCL device code, got '%s'",
+ getOffloadKindName(Kind).str().c_str()));
+ }
+
// Link and process the device images. The function may emit a direct fat
// binary if --emit-fatbin-only is specified.
auto FilesOrErr = linkAndWrapDeviceFiles(LibraryOptions, *DeviceInputFiles,
@@ -1619,8 +1674,12 @@ int main(int Argc, char **Argv) {
if (!FilesOrErr)
reportError(FilesOrErr.takeError());
- // Run the host linking job with the rendered arguments.
- if (!EmitFatbinOnly) {
+ if (SYCLDeviceLink) {
+ if (Error Err =
+ emitSYCLDeviceLinkOutput(LibraryOptions, *FilesOrErr, Args))
+ reportError(std::move(Err));
+ } else if (!EmitFatbinOnly) {
+ // Run the host linking job with the rendered arguments.
if (Error Err = runLinker(*FilesOrErr, Args))
reportError(std::move(Err));
}
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index fc48eac2637eeb..b54e2b0fd24ec5 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -69,6 +69,12 @@ def emit_fatbin_only
Flags<[WrapperOnlyOption]>,
HelpText<"Emit fat binary directly without wrapping or host linking">;
+def sycl_device_link
+ : Flag<["--"], "sycl-device-link">,
+ Flags<[WrapperOnlyOption]>,
+ HelpText<"Link and wrap SYCL device code into a host object without "
+ "running the host linker">;
+
def no_canonical_prefixes : Flag<["--"], "no-canonical-prefixes">,
Flags<[WrapperOnlyOption]>,
HelpText<"Do not resolve symbolic links, turn relative paths into absolute ones, or do anything else to identify the executable">;
More information about the cfe-commits
mailing list