https://github.com/srividya-sundaram created https://github.com/llvm/llvm-project/pull/240674
-fsycl-link links and wraps the SYCL device code of the inputs into a host object through clang-linker-wrapper --sycl-device-link. The host linker is not run, and the default output name is the same as for a host link. It requires -fsycl and is not allowed with --offload-device-only or --offload-host-only. >From 12e50b10dfa7ecd643c4d57d5b62590b1363442f Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Mon, 9 Nov 2026 18:49:21 -0800 Subject: [PATCH 1/2] [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">; >From 430a66f1028ba4816252f82bc3fb3756e887d8d9 Mon Sep 17 00:00:00 2001 From: srividya sundaram <[email protected]> Date: Mon, 9 Nov 2026 18:51:41 -0800 Subject: [PATCH 2/2] [Clang][SYCL] Add -fsycl-link -fsycl-link links and wraps the SYCL device code of the inputs into a host object through clang-linker-wrapper --sycl-device-link. The host linker is not run; the object is used later in a host link. -fsycl-link requires -fsycl and is not allowed with --offload-device-only or --offload-host-only. The default output name is the same as for a host link. --- clang/docs/SYCLSupport.md | 26 ++++++++++ clang/include/clang/Options/Options.td | 3 ++ clang/lib/Driver/Driver.cpp | 17 +++++++ clang/lib/Driver/ToolChains/Clang.cpp | 5 ++ clang/test/Driver/sycl-link.cpp | 70 ++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 clang/test/Driver/sycl-link.cpp diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md index 9eabfd57e55f1c..7cba27bdafa196 100644 --- a/clang/docs/SYCLSupport.md +++ b/clang/docs/SYCLSupport.md @@ -106,3 +106,29 @@ space attributes for pointers: ```C++ //TODO: add support for __attribute__((opencl_global_host)) and __attribute__((opencl_global_device)). ``` + +## Separate device link + +`-fsycl-link` links the SYCL device code of its inputs and wraps the resulting +device image into a host object. The host linker is not run. The object is +then used in a later host link, which can be done by any host linker. + +```console +clang++ -fsycl -c a.cpp b.cpp +clang++ -fsycl -fsycl-link a.o b.o -o dev.o +clang++ main.o a.o b.o dev.o -L<libsycl dir> -lLLVMSYCL -o app +``` + +The host link does not use `-fsycl`. With `-fsycl`, the device code in `a.o` +and `b.o` would be linked and registered a second time. + +If the inputs contain no device code, the output is an empty object without +SYCL runtime registration. + +Device code in a static library is only extracted when the host link would +use the archive member. To device link a whole library, pass it with +`-Wl,--whole-archive libfoo.a -Wl,--no-whole-archive`. + +The output of `-fsycl-link` is not yet supported in a static library: nothing +in it is referenced by the host code, so the host linker does not pull it out +of the archive and the device images are never registered. diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 81f26bb4791416..818e48613f1281 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -7854,6 +7854,9 @@ def fsycl_device_only : Flag<["-"], "fsycl-device-only">, def fsycl_host_only : Flag<["-"], "fsycl-host-only">, Alias<offload_host_only>, HelpText<"Compile SYCL code for host only. Has no " "effect on non-SYCL compilations">; +def fsycl_link : Flag<["-"], "fsycl-link">, + HelpText<"Link and wrap SYCL device code into a host object without running " + "the host linker">; def sycl_link : Flag<["--"], "sycl-link">, Flags<[HelpHidden]>, HelpText<"Perform link through clang-sycl-linker via the target " "offloading toolchain.">; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index c6f0bb0325a440..9dfa41e8619ecd 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1132,6 +1132,23 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C, if (Active) Kinds.insert(Kind); + // -fsycl-link links SYCL device code into a host object, so it needs both + // the host and the device side of a SYCL compilation. + if (Arg *A = C.getInputArgs().getLastArgNoClaim(options::OPT_fsycl_link)) { + if (!IsSYCL) + Diag(clang::diag::err_drv_argument_only_allowed_with) + << A->getSpelling() << "-fsycl"; + else if (const Arg *OnlyArg = C.getInputArgs().getLastArgNoClaim( + options::OPT_offload_device_only, + options::OPT_offload_host_only)) { + // Report the spelling that was used, e.g. -fsycl-device-only. + if (OnlyArg->getAlias()) + OnlyArg = OnlyArg->getAlias(); + Diag(clang::diag::err_drv_argument_not_allowed_with) + << A->getSpelling() << OnlyArg->getSpelling(); + } + } + // We currently don't support any kind of mixed offloading. if (Kinds.size() > 1) { Diag(clang::diag::err_drv_mix_offload) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index a33b116eda7105..3c2d9c694a9eac 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -10140,6 +10140,11 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, for (auto Input : Inputs) CmdArgs.push_back(Input.getFilename()); } else { + // With -fsycl-link, the wrapper writes the wrapped SYCL device object to + // the output and does not run the host link command. + if (C.isOffloadingHostKind(Action::OFK_SYCL) && + Args.hasArg(options::OPT_fsycl_link)) + CmdArgs.push_back("--sycl-device-link"); for (const char *LinkArg : LinkCommand->getArguments()) CmdArgs.push_back(LinkArg); } diff --git a/clang/test/Driver/sycl-link.cpp b/clang/test/Driver/sycl-link.cpp new file mode 100644 index 00000000000000..8182f8007c7ab2 --- /dev/null +++ b/clang/test/Driver/sycl-link.cpp @@ -0,0 +1,70 @@ +/// Test -fsycl-link: the SYCL device code is linked and wrapped into a host +/// object by clang-linker-wrapper, without running the host linker. + +// RUN: touch %t.a.o %t.b.o + +/// clang-linker-wrapper is the only link job, and it gets --sycl-device-link. +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \ +// RUN: %t.a.o %t.b.o -o dev.o 2>&1 | FileCheck %s --check-prefix=LINK +// LINK: clang-linker-wrapper" +// LINK-SAME: "--sycl-device-link" +// LINK-SAME: "-o" "dev.o" + +// RUN: %clang -ccc-print-bindings --target=x86_64-unknown-linux-gnu -fsycl \ +// RUN: -fsycl-link %t.a.o %t.b.o -o dev.o 2>&1 \ +// RUN: | FileCheck %s --check-prefix=BINDINGS +// BINDINGS: "x86_64-unknown-linux-gnu" - "Offload::Linker", inputs: ["{{.*}}a.o", "{{.*}}b.o"], output: "dev.o" +// BINDINGS-NOT: {{.}} + +/// The default output name is the same as for a host link. +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \ +// RUN: %t.a.o 2>&1 | FileCheck %s --check-prefix=DEFAULT-NAME +// DEFAULT-NAME: clang-linker-wrapper"{{.*}} "--sycl-device-link"{{.*}} "-o" "a.out" + +/// Source inputs are compiled, then device linked. +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link %s \ +// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=SOURCE +// SOURCE: "-cc1"{{.*}} "-fsycl-is-device" +// SOURCE: "-cc1"{{.*}} "-fsycl-is-host" +// SOURCE: clang-linker-wrapper"{{.*}} "--sycl-device-link"{{.*}} "-o" "dev.o" + +// RUN: %clang -### --target=x86_64-pc-windows-msvc -fsycl -fsycl-link \ +// RUN: %t.a.o -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINDOWS +// WINDOWS: clang-linker-wrapper"{{.*}} "--sycl-device-link"{{.*}} "-out:dev.obj" +// RUN: %clang -ccc-print-bindings --target=x86_64-pc-windows-msvc -fsycl \ +// RUN: -fsycl-link %t.a.o -o dev.obj 2>&1 \ +// RUN: | FileCheck %s --check-prefix=WINDOWS-BINDINGS +// WINDOWS-BINDINGS: "x86_64-pc-windows-msvc{{.*}}" - "Offload::Linker", inputs: ["{{.*}}a.o"], output: "dev.obj" +// WINDOWS-BINDINGS-NOT: {{.}} + +/// Without -fsycl-link, the host link is run as usual. +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl %t.a.o 2>&1 \ +// RUN: | FileCheck %s --check-prefix=NO-SYCL-LINK +// NO-SYCL-LINK: clang-linker-wrapper" +// NO-SYCL-LINK-NOT: "--sycl-device-link" + +/// -fsycl-link requires -fsycl. +// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fsycl-link %t.a.o \ +// RUN: 2>&1 | FileCheck %s --check-prefix=NO-FSYCL +// NO-FSYCL: error: invalid argument '-fsycl-link' only allowed with '-fsycl' + +/// -fsycl-link needs both the host and the device compilation. +// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \ +// RUN: --offload-device-only %t.a.o 2>&1 \ +// RUN: | FileCheck %s --check-prefix=ONLY -DOPT=--offload-device-only +// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \ +// RUN: -fsycl-device-only %t.a.o 2>&1 \ +// RUN: | FileCheck %s --check-prefix=ONLY -DOPT=-fsycl-device-only +// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \ +// RUN: --offload-host-only %t.a.o 2>&1 \ +// RUN: | FileCheck %s --check-prefix=ONLY -DOPT=--offload-host-only +// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \ +// RUN: -fsycl-host-only %t.a.o 2>&1 \ +// RUN: | FileCheck %s --check-prefix=ONLY -DOPT=-fsycl-host-only +// ONLY: error: invalid argument '-fsycl-link' not allowed with '[[OPT]]' + +/// -fsycl-link is unused when there is no link step. +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link -c %s \ +// RUN: 2>&1 | FileCheck %s --check-prefix=UNUSED \ +// RUN: --implicit-check-not=--sycl-device-link +// UNUSED: warning: argument unused during compilation: '-fsycl-link' _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
