[clang] [Clang][SYCL] Add -fsycl-link (PR #240674)
Srividya Sundaram via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 13:01:02 PST 2026
https://github.com/srividya-sundaram updated https://github.com/llvm/llvm-project/pull/240674
>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 01/33] [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 0000000000000..2601dbcf39d95
--- /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 1024d5e6e1a6c..c083c694705de 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 fc48eac2637ee..b54e2b0fd24ec 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 <srividya.sundaram at intel.com>
Date: Mon, 9 Nov 2026 18:51:41 -0800
Subject: [PATCH 02/33] [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 9eabfd57e55f1..7cba27bdafa19 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 81f26bb479141..818e48613f128 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 c6f0bb0325a44..9dfa41e8619ec 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 a33b116eda710..3c2d9c694a9ea 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 0000000000000..8182f8007c7ab
--- /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'
>From c8407af98a29062f56bee0f8aeccf769be6cefbe Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 07:32:22 -0800
Subject: [PATCH 03/33] [clang-linker-wrapper][SYCL] Address review: embed
finalized images, check override kinds and missing inputs
With --sycl-device-link the device image is already linked, so it is now
embedded in .sycl_fatbin instead of .llvm.offloading. A later wrapped
host link no longer extracts it and links it again.
Also check the kind of --override-image inputs, and report missing input
files, since no host linker runs to report them.
---
.../linker-wrapper-sycl-device-link.c | 25 ++++++++++++
.../ClangLinkerWrapper.cpp | 39 ++++++++++++++-----
2 files changed, 54 insertions(+), 10 deletions(-)
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
index 2601dbcf39d95..cbedb6021a19c 100644
--- 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
@@ -30,8 +30,19 @@
// 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
+// RUN: llvm-readobj --sections %t.dev.o | FileCheck %s --check-prefix=SECTIONS
// WRAPPED-DAG: .sycl_offloading.binary
// WRAPPED-DAG: U __sycl_register_lib
+// SECTIONS-NOT: Name: .llvm.offloading
+// SECTIONS: Name: .sycl_fatbin
+// SECTIONS-NOT: Name: .llvm.offloading
+
+// The device image is already linked, so a later wrapped host link does not
+// link it again.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld %t.dev.o -o a.out 2>&1 \
+// RUN: | FileCheck %s --check-prefix=RELINK --implicit-check-not=--sycl-link
+// RELINK: "/usr/bin/ld" {{.*}}dev.o -o a.out
// 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 \
@@ -54,8 +65,22 @@
// 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
+// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --override-image=openmp=%t.elf.o \
+// RUN: --linker-path=/usr/bin/ld %t.elf.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'
+// Without a host link, a missing input must be diagnosed here.
+// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o %t.missing.o \
+// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
+// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld --whole-archive \
+// RUN: %t.missing.o --no-whole-archive -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
+// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
+
// 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
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index c083c694705de..d3e0eec39e723 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -787,8 +787,15 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
const ArgList &Args, OffloadKind Kind) {
llvm::TimeTraceScope TimeScope("Wrap bundled images");
+ // With --sycl-device-link, the wrapped images are already linked. They must
+ // not be found and linked again if the output goes through another wrapped
+ // host link, so they are embedded as finalized images.
+ bool IsFinalizedImage = Kind == OFK_SYCL && Args.hasArg(OPT_sycl_device_link);
+
// We use the discrete tools if we are in verbose mode with '--save-temps'.
- if (Verbose && SaveTemps && !Args.hasArg(OPT_print_wrapped_module))
+ // llvm-offload-wrapper cannot embed finalized images.
+ if (Verbose && SaveTemps && !Args.hasArg(OPT_print_wrapped_module) &&
+ !IsFinalizedImage)
return wrapDeviceImagesVerbose(Buffers, Args, Kind);
SmallVector<ArrayRef<char>, 4> BuffersToWrap;
@@ -824,8 +831,8 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
case OFK_SYCL: {
// TODO: fill these options once the Driver supports them.
offloading::SYCLJITOptions Options;
- if (Error Err =
- offloading::wrapSYCLBinaries(M, BuffersToWrap.front(), Options))
+ if (Error Err = offloading::wrapSYCLBinaries(M, BuffersToWrap.front(),
+ Options, IsFinalizedImage))
return std::move(Err);
break;
}
@@ -1446,8 +1453,16 @@ getDeviceInput(const ArgList &Args) {
if (!Filename && Arg->getOption().matches(OPT_library))
return createStringError("unable to find library -l%s", Arg->getValue());
- if (!Filename || !sys::fs::exists(*Filename) ||
- sys::fs::is_directory(*Filename))
+ if (!Filename || !sys::fs::exists(*Filename)) {
+ // With --sycl-device-link there is no host link to report missing
+ // inputs.
+ if (Args.hasArg(OPT_sycl_device_link))
+ return createFileError(
+ Arg->getValue(),
+ std::make_error_code(std::errc::no_such_file_or_directory));
+ continue;
+ }
+ if (sys::fs::is_directory(*Filename))
continue;
// Unlike `--whole-archive`, `/wholearchive:` applies to a single library.
@@ -1658,13 +1673,17 @@ int main(int Argc, char **Argv) {
if (EmitFatbinOnly)
reportError(createStringError(
"--sycl-device-link cannot be used with --emit-fatbin-only"));
+ auto CheckKind = [](OffloadKind Kind) {
+ if (Kind != OFK_SYCL)
+ reportError(createStringError(
+ "--sycl-device-link requires SYCL device code, got '%s'",
+ getOffloadKindName(Kind).str().c_str()));
+ };
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()));
+ CheckKind(File.getBinary()->getOffloadKind());
+ for (StringRef Override : Args.getAllArgValues(OPT_override_image))
+ CheckKind(getOffloadKind(Override.split('=').first));
}
// Link and process the device images. The function may emit a direct fat
>From f25480b438cc7ce481cac69991daab4e7d522962 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 07:34:46 -0800
Subject: [PATCH 04/33] [Clang][SYCL] Address review: pass only link inputs
with -fsycl-link, allow --offload-host-device
With -fsycl-link no host link runs, so clang-linker-wrapper now gets the
link inputs and library paths instead of the host link command with its
startup files and default libraries. This also lets the wrapper report
missing inputs without tripping over host linker options.
--offload-host-device after --offload-device-only or --offload-host-only
selects both sides, so it is no longer rejected.
---
clang/lib/Driver/Driver.cpp | 13 ++++++++-----
clang/lib/Driver/ToolChains/Clang.cpp | 16 +++++++++++-----
clang/test/Driver/sycl-link.cpp | 21 ++++++++++++++++-----
3 files changed, 35 insertions(+), 15 deletions(-)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9dfa41e8619ec..b320ef3494a61 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1138,14 +1138,17 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
if (!IsSYCL)
Diag(clang::diag::err_drv_argument_only_allowed_with)
<< A->getSpelling() << "-fsycl";
- else if (const Arg *OnlyArg = C.getInputArgs().getLastArgNoClaim(
+ else if (const Arg *ModeArg = C.getInputArgs().getLastArgNoClaim(
options::OPT_offload_device_only,
- options::OPT_offload_host_only)) {
+ options::OPT_offload_host_only,
+ options::OPT_offload_host_device);
+ ModeArg &&
+ !ModeArg->getOption().matches(options::OPT_offload_host_device)) {
// Report the spelling that was used, e.g. -fsycl-device-only.
- if (OnlyArg->getAlias())
- OnlyArg = OnlyArg->getAlias();
+ if (ModeArg->getAlias())
+ ModeArg = ModeArg->getAlias();
Diag(clang::diag::err_drv_argument_not_allowed_with)
- << A->getSpelling() << OnlyArg->getSpelling();
+ << A->getSpelling() << ModeArg->getSpelling();
}
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 3c2d9c694a9ea..f68ac073e9b19 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -10139,12 +10139,18 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.append({"-o", Output.getFilename()});
for (auto Input : Inputs)
CmdArgs.push_back(Input.getFilename());
- } else {
+ } else if (C.isOffloadingHostKind(Action::OFK_SYCL) &&
+ Args.hasArg(options::OPT_fsycl_link)) {
// 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");
+ // the output and no host link runs. Pass only the link inputs and library
+ // paths, not the host link command with its startup files and default
+ // libraries.
+ CmdArgs.push_back("--sycl-device-link");
+ CmdArgs.append({"-o", Output.getFilename()});
+ Args.AddAllArgs(CmdArgs, options::OPT_L);
+ getToolChain().AddFilePathLibArgs(Args, CmdArgs);
+ AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
+ } else {
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
index 8182f8007c7ab..d6c1458ddba73 100644
--- a/clang/test/Driver/sycl-link.cpp
+++ b/clang/test/Driver/sycl-link.cpp
@@ -3,12 +3,16 @@
// RUN: touch %t.a.o %t.b.o
-/// clang-linker-wrapper is the only link job, and it gets --sycl-device-link.
+/// clang-linker-wrapper is the only link job, and it gets --sycl-device-link
+/// with the link inputs instead of the host link command.
// 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
+// RUN: %t.a.o -L/foo -Wl,--whole-archive -lbar -Wl,--no-whole-archive \
+// RUN: %t.b.o -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=crt1.o \
+// RUN: --implicit-check-not='"-lc"'
// LINK: clang-linker-wrapper"
-// LINK-SAME: "--sycl-device-link"
-// LINK-SAME: "-o" "dev.o"
+// LINK-SAME: "--sycl-device-link" "-o" "dev.o" "-L/foo"
+// LINK-SAME: "{{.*}}a.o" "--whole-archive" "-lbar" "--no-whole-archive" "{{.*}}b.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 \
@@ -30,7 +34,7 @@
// 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"
+// WINDOWS: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o" "dev.obj" "{{.*}}a.o"{{$}}
// 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
@@ -63,6 +67,13 @@
// RUN: | FileCheck %s --check-prefix=ONLY -DOPT=-fsycl-host-only
// ONLY: error: invalid argument '-fsycl-link' not allowed with '[[OPT]]'
+/// The last of the mode options selects the mode.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \
+// RUN: --offload-device-only --offload-host-device %t.a.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=HOST-DEVICE
+// HOST-DEVICE-NOT: error:
+// HOST-DEVICE: clang-linker-wrapper"{{.*}} "--sycl-device-link"
+
/// -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 \
>From 6ac6264902bbd02317845c9f9eb215a6588c88ef Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 07:39:48 -0800
Subject: [PATCH 05/33] [clang-linker-wrapper][SYCL] Address review: keep host
linker option values, drop copy print
A missing input is not reported when it follows an option the wrapper
does not know, since it may be the value of a host linker option such as
'-z now'.
Also stop printing a copy command that is not run, assert that there is
at most one wrapped object, and test the -v --save-temps path.
---
.../linker-wrapper-sycl-device-link.c | 28 +++++++++----
.../ClangLinkerWrapper.cpp | 39 +++++++++++--------
2 files changed, 42 insertions(+), 25 deletions(-)
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
index cbedb6021a19c..56b70fd165d94 100644
--- 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
@@ -15,8 +15,7 @@
// 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{{$}}
+// LINK: "{{.*}}clang{{(.exe)?}}" {{.*}}--target=spirv64-unknown-unknown {{.*}}sycl-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
// Archive members are only extracted for the host link, so use
// --whole-archive to device link a static library.
@@ -31,8 +30,8 @@
// 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
// RUN: llvm-readobj --sections %t.dev.o | FileCheck %s --check-prefix=SECTIONS
-// WRAPPED-DAG: .sycl_offloading.binary
-// WRAPPED-DAG: U __sycl_register_lib
+// WRAPPED-DAG: {{^[0-9a-f]+}} r .sycl_offloading.binary{{$}}
+// WRAPPED-DAG: {{^ +}}U __sycl_register_lib{{$}}
// SECTIONS-NOT: Name: .llvm.offloading
// SECTIONS: Name: .sycl_fatbin
// SECTIONS-NOT: Name: .llvm.offloading
@@ -44,10 +43,17 @@
// RUN: | FileCheck %s --check-prefix=RELINK --implicit-check-not=--sycl-link
// RELINK: "/usr/bin/ld" {{.*}}dev.o -o a.out
-// 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{{$}}
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o -o %t.dev.obj
+// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
+// WIN: Format: COFF-x86-64
+
+// With -v and --save-temps, the wrapping is still done in-process, because the
+// image must be embedded as finalized.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run -v \
+// RUN: --save-temps --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o \
+// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=LINK \
+// RUN: --implicit-check-not=/usr/bin/ld --implicit-check-not=llvm-offload-wrapper
// No device code: an empty host object without SYCL runtime registration is
// written to the output.
@@ -81,6 +87,12 @@
// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
+// A value of a host linker option is not reported as a missing input.
+// 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 -z now \
+// RUN: -Map out.map -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
+
// 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
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index d3e0eec39e723..bb7dcc77535ee 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -36,6 +36,7 @@
#include "llvm/Option/Option.h"
#include "llvm/Remarks/HotnessThresholdParser.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Errc.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
@@ -793,7 +794,7 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
bool IsFinalizedImage = Kind == OFK_SYCL && Args.hasArg(OPT_sycl_device_link);
// We use the discrete tools if we are in verbose mode with '--save-temps'.
- // llvm-offload-wrapper cannot embed finalized images.
+ // llvm-offload-wrapper has no option to embed finalized images.
if (Verbose && SaveTemps && !Args.hasArg(OPT_print_wrapped_module) &&
!IsFinalizedImage)
return wrapDeviceImagesVerbose(Buffers, Args, Kind);
@@ -1104,17 +1105,15 @@ Error handleOverrideImages(
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());
+ // All inputs are SYCL device code, which is wrapped into a single object.
+ assert(WrappedFiles.size() <= 1 && "expected one wrapped SYCL object");
- 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.
+ Triple HostTriple(Args.getLastArgValue(OPT_host_triple_EQ,
+ sys::getDefaultTargetTriple()));
LLVMContext Context;
Module M("offload.wrapper.module", Context);
M.setTargetTriple(HostTriple);
@@ -1127,9 +1126,6 @@ Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
File = WrappedFiles.front();
}
- if (Verbose || DryRun)
- printCommands(
- {HostTriple.isOSWindows() ? "copy" : "cp", File, ExecutableName});
if (DryRun)
return Error::success();
@@ -1435,6 +1431,16 @@ getDeviceInput(const ArgList &Args) {
SmallVector<OffloadFile> ObjectFilesToExtract;
SmallVector<OffloadFile> ArchiveFilesToExtract;
DenseMap<StringRef, StringRef> SourceForImage;
+
+ // With --sycl-device-link no host link runs to report missing inputs, so
+ // report them here. An input right after an option that is not known here
+ // may be the value of a host linker option (e.g. '-z now'), so it is skipped
+ // as in a host link.
+ bool ReportMissingInputs = Args.hasArg(OPT_sycl_device_link);
+ DenseSet<unsigned> UnknownOptionIndices;
+ for (const opt::Arg *Arg : Args.filtered(OPT_UNKNOWN))
+ UnknownOptionIndices.insert(Arg->getIndex());
+
for (const opt::Arg *Arg :
Args.filtered(OPT_INPUT, OPT_library, OPT_wholearchive_file,
OPT_whole_archive, OPT_no_whole_archive)) {
@@ -1454,12 +1460,11 @@ getDeviceInput(const ArgList &Args) {
return createStringError("unable to find library -l%s", Arg->getValue());
if (!Filename || !sys::fs::exists(*Filename)) {
- // With --sycl-device-link there is no host link to report missing
- // inputs.
- if (Args.hasArg(OPT_sycl_device_link))
+ if (ReportMissingInputs &&
+ !UnknownOptionIndices.contains(Arg->getIndex() - 1))
return createFileError(
- Arg->getValue(),
- std::make_error_code(std::errc::no_such_file_or_directory));
+ Filename ? *Filename : Arg->getValue(),
+ make_error_code(errc::no_such_file_or_directory));
continue;
}
if (sys::fs::is_directory(*Filename))
@@ -1676,8 +1681,8 @@ int main(int Argc, char **Argv) {
auto CheckKind = [](OffloadKind Kind) {
if (Kind != OFK_SYCL)
reportError(createStringError(
- "--sycl-device-link requires SYCL device code, got '%s'",
- getOffloadKindName(Kind).str().c_str()));
+ "--sycl-device-link requires SYCL device code, got '" +
+ getOffloadKindName(Kind) + "'"));
};
for (const SmallVector<OffloadFile> &Files : *DeviceInputFiles)
for (const OffloadFile &File : Files)
>From 1225028580bf8e20e463f0873e9a4a36d6ac70c7 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 07:41:36 -0800
Subject: [PATCH 06/33] [Clang][SYCL] Address review: forward the sysroot with
-fsycl-link, clarify help text
clang-linker-wrapper uses the sysroot to find -l and -L= inputs, so pass
it with -fsycl-link as the host link does. Also say in the help text and
docs how the output is used, and note the Windows library name.
---
clang/docs/SYCLSupport.md | 2 ++
clang/include/clang/Options/Options.td | 4 ++--
clang/lib/Driver/ToolChains/Clang.cpp | 9 ++++++---
clang/test/Driver/sycl-link.cpp | 13 ++++++++-----
4 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index 7cba27bdafa19..d613617bd6f20 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -119,6 +119,8 @@ 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
```
+On Windows, the host link uses `LLVMSYCL.lib` instead of `-lLLVMSYCL`.
+
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.
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 818e48613f128..823e8cc7cf7cd 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -7855,8 +7855,8 @@ 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">;
+ HelpText<"Link SYCL device code and wrap it into a host object for a later "
+ "host link, 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/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index f68ac073e9b19..6c1be753f1223 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -10142,11 +10142,14 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
} else if (C.isOffloadingHostKind(Action::OFK_SYCL) &&
Args.hasArg(options::OPT_fsycl_link)) {
// With -fsycl-link, the wrapper writes the wrapped SYCL device object to
- // the output and no host link runs. Pass only the link inputs and library
- // paths, not the host link command with its startup files and default
- // libraries.
+ // the output and no host link runs. Pass only what is needed to find the
+ // device code: the sysroot, library paths and link inputs. Other host link
+ // options, startup files and default libraries are not used.
CmdArgs.push_back("--sycl-device-link");
CmdArgs.append({"-o", Output.getFilename()});
+ const Driver &D = getToolChain().getDriver();
+ if (!D.SysRoot.empty())
+ CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Args.AddAllArgs(CmdArgs, options::OPT_L);
getToolChain().AddFilePathLibArgs(Args, CmdArgs);
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
diff --git a/clang/test/Driver/sycl-link.cpp b/clang/test/Driver/sycl-link.cpp
index d6c1458ddba73..9d58314f9f907 100644
--- a/clang/test/Driver/sycl-link.cpp
+++ b/clang/test/Driver/sycl-link.cpp
@@ -4,15 +4,18 @@
// RUN: touch %t.a.o %t.b.o
/// clang-linker-wrapper is the only link job, and it gets --sycl-device-link
-/// with the link inputs instead of the host link command.
+/// with the sysroot, library paths and link inputs instead of the host link
+/// command.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \
-// RUN: %t.a.o -L/foo -Wl,--whole-archive -lbar -Wl,--no-whole-archive \
-// RUN: %t.b.o -o dev.o 2>&1 \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree %t.a.o -L/foo \
+// RUN: -Wl,--whole-archive -lbar -Wl,--no-whole-archive %t.b.o -o dev.o 2>&1 \
// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=crt1.o \
// RUN: --implicit-check-not='"-lc"'
// LINK: clang-linker-wrapper"
-// LINK-SAME: "--sycl-device-link" "-o" "dev.o" "-L/foo"
-// LINK-SAME: "{{.*}}a.o" "--whole-archive" "-lbar" "--no-whole-archive" "{{.*}}b.o"{{$}}
+// LINK-SAME: "--sycl-device-link" "-o" "dev.o"
+// LINK-SAME: "--sysroot={{[^"]*}}basic_linux_tree" "-L/foo"
+// LINK-SAME: "-L{{[^"]*}}basic_linux_tree{{[^"]*}}{{[/\\]+}}usr{{[/\\]+}}lib"
+// LINK-SAME: "{{[^"]*}}a.o" "--whole-archive" "-lbar" "--no-whole-archive" "{{[^"]*}}b.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 \
>From 72c11a33bbeee7d13a20dba47757ebba4e0cf19e Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 07:43:08 -0800
Subject: [PATCH 07/33] [clang-linker-wrapper][SYCL] Note which missing inputs
are not reported
---
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index bb7dcc77535ee..34bf137f5322d 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1435,7 +1435,8 @@ getDeviceInput(const ArgList &Args) {
// With --sycl-device-link no host link runs to report missing inputs, so
// report them here. An input right after an option that is not known here
// may be the value of a host linker option (e.g. '-z now'), so it is skipped
- // as in a host link.
+ // as in a host link. Without the host linker's options, a file after an
+ // unknown flag option is skipped the same way.
bool ReportMissingInputs = Args.hasArg(OPT_sycl_device_link);
DenseSet<unsigned> UnknownOptionIndices;
for (const opt::Arg *Arg : Args.filtered(OPT_UNKNOWN))
>From dbf5d3d5f64f9138b87dce20d24a2ab3e75a7d3c Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:05:01 -0800
Subject: [PATCH 08/33] [clang-linker-wrapper][SYCL] Address review: report
inputs after '--', allow COFF options
The '--' separator is not an option, so a missing input after it is
reported. COFF linker options such as /nologo are parsed as inputs, so
they are not reported as missing files. Also use --wrapper-verbose in the
test so it reaches the verbose wrapping path.
---
.../linker-wrapper-sycl-device-link.c | 14 ++++++++++----
.../clang-linker-wrapper/ClangLinkerWrapper.cpp | 15 ++++++++++++---
2 files changed, 22 insertions(+), 7 deletions(-)
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
index 56b70fd165d94..f1c05f5aca9e4 100644
--- 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
@@ -43,14 +43,17 @@
// RUN: | FileCheck %s --check-prefix=RELINK --implicit-check-not=--sycl-link
// RELINK: "/usr/bin/ld" {{.*}}dev.o -o a.out
+// COFF linker options are not reported as missing inputs.
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o -o %t.dev.obj
+// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
+// RUN: /IGNORE:4078 -o %t.dev.obj
// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
// WIN: Format: COFF-x86-64
-// With -v and --save-temps, the wrapping is still done in-process, because the
-// image must be embedded as finalized.
-// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run -v \
+// With --wrapper-verbose and --save-temps, the wrapping is still done
+// in-process, because the image must be embedded as finalized.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --wrapper-verbose \
// RUN: --save-temps --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o \
// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=LINK \
// RUN: --implicit-check-not=/usr/bin/ld --implicit-check-not=llvm-offload-wrapper
@@ -85,6 +88,9 @@
// RUN: --sycl-device-link --linker-path=/usr/bin/ld --whole-archive \
// RUN: %t.missing.o --no-whole-archive -o dev.o 2>&1 \
// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
+// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --sycl-device-link --linker-path=/usr/bin/ld -o dev.o -- %t.missing.o \
+// RUN: 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
// A value of a host linker option is not reported as a missing input.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 34bf137f5322d..3254f00053bc7 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1436,11 +1436,19 @@ getDeviceInput(const ArgList &Args) {
// report them here. An input right after an option that is not known here
// may be the value of a host linker option (e.g. '-z now'), so it is skipped
// as in a host link. Without the host linker's options, a file after an
- // unknown flag option is skipped the same way.
+ // unknown flag option is skipped the same way. The '--' separator is not an
+ // option.
bool ReportMissingInputs = Args.hasArg(OPT_sycl_device_link);
DenseSet<unsigned> UnknownOptionIndices;
for (const opt::Arg *Arg : Args.filtered(OPT_UNKNOWN))
- UnknownOptionIndices.insert(Arg->getIndex());
+ if (Arg->getSpelling() != "--")
+ UnknownOptionIndices.insert(Arg->getIndex());
+ // COFF linker options such as '/nologo' are parsed as inputs. Unlike a path,
+ // they have no further path separator.
+ auto IsCOFFLinkerOption = [&](StringRef Input) {
+ return HostTriple.isOSWindows() && Input.starts_with("/") &&
+ Input.find_first_of("/\\", 1) == StringRef::npos;
+ };
for (const opt::Arg *Arg :
Args.filtered(OPT_INPUT, OPT_library, OPT_wholearchive_file,
@@ -1462,7 +1470,8 @@ getDeviceInput(const ArgList &Args) {
if (!Filename || !sys::fs::exists(*Filename)) {
if (ReportMissingInputs &&
- !UnknownOptionIndices.contains(Arg->getIndex() - 1))
+ !UnknownOptionIndices.contains(Arg->getIndex() - 1) &&
+ !IsCOFFLinkerOption(Arg->getValue()))
return createFileError(
Filename ? *Filename : Arg->getValue(),
make_error_code(errc::no_such_file_or_directory));
>From 246491f7527e02b0db9157d71e7e0a8276525a7b Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:07:20 -0800
Subject: [PATCH 09/33] [clang-linker-wrapper][SYCL] Address review: allow COFF
options with path values
COFF options such as /LIBPATH:C:\dir have path separators in the value,
so only the option name before ':' is checked.
---
.../linker-wrapper-sycl-device-link.c | 5 ++++-
.../tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 11 +++++++----
2 files changed, 11 insertions(+), 5 deletions(-)
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
index f1c05f5aca9e4..51c2f721a03c9 100644
--- 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
@@ -46,7 +46,7 @@
// COFF linker options are not reported as missing inputs.
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
-// RUN: /IGNORE:4078 -o %t.dev.obj
+// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
// WIN: Format: COFF-x86-64
@@ -91,6 +91,9 @@
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --sycl-device-link --linker-path=/usr/bin/ld -o dev.o -- %t.missing.o \
// RUN: 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
+// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o %t.missing.o \
+// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
// A value of a host linker option is not reported as a missing input.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 3254f00053bc7..4ccdf7aaa1f34 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1443,11 +1443,14 @@ getDeviceInput(const ArgList &Args) {
for (const opt::Arg *Arg : Args.filtered(OPT_UNKNOWN))
if (Arg->getSpelling() != "--")
UnknownOptionIndices.insert(Arg->getIndex());
- // COFF linker options such as '/nologo' are parsed as inputs. Unlike a path,
- // they have no further path separator.
+ // COFF linker options such as '/nologo' or '/LIBPATH:C:\dir' are parsed as
+ // inputs. Unlike a path, the option name before any ':' has no further path
+ // separator.
auto IsCOFFLinkerOption = [&](StringRef Input) {
- return HostTriple.isOSWindows() && Input.starts_with("/") &&
- Input.find_first_of("/\\", 1) == StringRef::npos;
+ if (!HostTriple.isOSWindows() || !Input.consume_front("/"))
+ return false;
+ StringRef Name = Input.split(':').first;
+ return !Name.empty() && Name.find_first_of("/\\") == StringRef::npos;
};
for (const opt::Arg *Arg :
>From 50c64e493e41fb69a4887916bc3605df28adab20 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:31:51 -0800
Subject: [PATCH 10/33] [Clang][SYCL] Address review: use -libpath: for MSVC,
pass clang-cl /link values
For MSVC targets clang-linker-wrapper finds named inputs only in -libpath:
paths, so render -L and toolchain paths that way, as the MSVC link job
does. Also pass clang-cl /link values, and say in the docs that archive
device code is extracted for the device targets of the other inputs.
---
clang/docs/SYCLSupport.md | 7 ++++---
clang/lib/Driver/ToolChains/Clang.cpp | 15 +++++++++++++--
clang/test/Driver/sycl-link.cpp | 13 +++++++++++++
3 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index d613617bd6f20..4363305b242f4 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -127,9 +127,10 @@ 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`.
+Device code in a static library is only extracted for device targets that
+the other inputs already have. If only static libraries provide device code,
+pass them with `-Wl,--whole-archive libfoo.a -Wl,--no-whole-archive`, or with
+`-Wl,/wholearchive:foo.lib` on Windows.
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
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 6c1be753f1223..d472407063936 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -10150,9 +10150,20 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
const Driver &D = getToolChain().getDriver();
if (!D.SysRoot.empty())
CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
- Args.AddAllArgs(CmdArgs, options::OPT_L);
- getToolChain().AddFilePathLibArgs(Args, CmdArgs);
+ // MSVC style links search for named inputs only in -libpath: paths.
+ bool IsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
+ auto AddLibraryPath = [&](StringRef Path) {
+ if (!Path.empty())
+ CmdArgs.push_back(
+ Args.MakeArgString((IsMSVC ? "-libpath:" : "-L") + Path));
+ };
+ for (const std::string &Path : Args.getAllArgValues(options::OPT_L))
+ AddLibraryPath(Path);
+ for (const std::string &Path : getToolChain().getFilePaths())
+ AddLibraryPath(Path);
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
+ // Inputs passed with clang-cl /link.
+ Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
} else {
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
index 9d58314f9f907..bb90cce277c9a 100644
--- a/clang/test/Driver/sycl-link.cpp
+++ b/clang/test/Driver/sycl-link.cpp
@@ -44,6 +44,19 @@
// WINDOWS-BINDINGS: "x86_64-pc-windows-msvc{{.*}}" - "Offload::Linker", inputs: ["{{.*}}a.o"], output: "dev.obj"
// WINDOWS-BINDINGS-NOT: {{.}}
+/// MSVC style links search for named inputs in -libpath: paths, and clang-cl
+/// /link values are passed on.
+// RUN: %clang -### --target=x86_64-pc-windows-msvc -fsycl -fsycl-link \
+// RUN: %t.a.o -L/foo -Wl,/wholearchive:bar.lib -o dev.obj 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WINDOWS-LIBPATH
+// WINDOWS-LIBPATH: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o" "dev.obj"
+// WINDOWS-LIBPATH-SAME: "-libpath:/foo" "{{[^"]*}}a.o" "/wholearchive:bar.lib"{{$}}
+// RUN: %clang_cl -### --target=x86_64-pc-windows-msvc -fsycl -fsycl-link \
+// RUN: %t.a.o /link /wholearchive:bar.lib /libpath:dir 2>&1 \
+// RUN: | FileCheck %s --check-prefix=CL-LINK
+// CL-LINK: clang-linker-wrapper"{{.*}} "--sycl-device-link"
+// CL-LINK-SAME: "{{[^"]*}}a.o" "/wholearchive:bar.lib" "/libpath:dir"{{$}}
+
/// 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
>From 4750b3618a8bf9ab2e046ea8292c7ae811b5bd03 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:31:52 -0800
Subject: [PATCH 11/33] [clang-linker-wrapper][SYCL] Address review: fix
archive extraction comment in test
---
.../clang-linker-wrapper/linker-wrapper-sycl-device-link.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
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
index 51c2f721a03c9..64767b5144013 100644
--- 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
@@ -17,8 +17,8 @@
// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
// LINK: "{{.*}}clang{{(.exe)?}}" {{.*}}--target=spirv64-unknown-unknown {{.*}}sycl-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
-// Archive members are only extracted for the host link, so use
-// --whole-archive to device link a static library.
+// Archive device code is only extracted for device targets that other inputs
+// have. Here no other input has device code, so --whole-archive is needed.
// 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 \
>From a59ddb98a98a701cc873dcd82af2c495f1bca6c6 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:36:10 -0800
Subject: [PATCH 12/33] [clang-linker-wrapper][SYCL] Address review: match COFF
options in any case, search LIB
link.exe options are case-insensitive, so /LIBPATH:, /WHOLEARCHIVE: and
/WHOLEARCHIVE are now recognized in any case. link.exe also searches the
LIB environment variable, so do the same for MSVC hosts. Without this,
--sycl-device-link reported libraries found this way as missing, or
skipped their device code.
---
.../linker-wrapper-sycl-device-link.c | 16 +++++
.../ClangLinkerWrapper.cpp | 65 ++++++++++++++++---
2 files changed, 73 insertions(+), 8 deletions(-)
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
index 64767b5144013..0d02c1b268547 100644
--- 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
@@ -50,6 +50,22 @@
// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
// WIN: Format: COFF-x86-64
+// link.exe options are case-insensitive, and link.exe also searches LIB.
+// RUN: %clang -cc1 %s -triple x86_64-pc-windows-msvc -emit-obj -o %t.win.obj \
+// RUN: -fembed-offload-object=%t.sycl.out
+// RUN: rm -rf %t.libdir && mkdir %t.libdir
+// RUN: llvm-ar rcs %t.libdir/foo.lib %t.win.obj
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe /LIBPATH:%t.libdir \
+// RUN: /WHOLEARCHIVE:foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
+// RUN: env LIB=%t.libdir clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --dry-run --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE \
+// RUN: foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
+// WINLIB: --target=spirv64-unknown-unknown {{.*}}win-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
+// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
+// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=nope.lib
+
// With --wrapper-verbose and --save-temps, the wrapping is still done
// in-process, because the image must be embedded as finalized.
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 4ccdf7aaa1f34..695582522bfb3 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -44,6 +44,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
@@ -1413,6 +1414,18 @@ getDeviceInput(const ArgList &Args) {
const llvm::Triple HostTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
+ // link.exe options are case-insensitive, but the COFF options in the option
+ // table only match lower case. Returns the value of option \p Name if
+ // \p Input is that option in any case.
+ auto GetCOFFOptionValue = [&](StringRef Input,
+ StringRef Name) -> std::optional<StringRef> {
+ if (!HostTriple.isOSWindows() ||
+ !(Input.consume_front("/") || Input.consume_front("-")) ||
+ !Input.consume_front_insensitive(Name))
+ return std::nullopt;
+ return Input;
+ };
+
StringRef Root = Args.getLastArgValue(OPT_sysroot_EQ);
SmallVector<StringRef> LibraryPaths;
for (const opt::Arg *Arg : Args.filtered(OPT_library_path, OPT_libpath))
@@ -1422,12 +1435,37 @@ getDeviceInput(const ArgList &Args) {
SmallVector<StringRef> InputPaths;
for (const opt::Arg *Arg : Args.filtered(OPT_libpath))
InputPaths.push_back(Arg->getValue());
+ for (const opt::Arg *Arg : Args.filtered(OPT_INPUT)) {
+ if (std::optional<StringRef> Path =
+ GetCOFFOptionValue(Arg->getValue(), "libpath:")) {
+ LibraryPaths.push_back(*Path);
+ InputPaths.push_back(*Path);
+ }
+ }
+
+ // link.exe also searches the paths in the LIB environment variable.
+ std::optional<std::string> LibEnv;
+ if (HostTriple.isWindowsMSVCEnvironment())
+ LibEnv = sys::Process::GetEnv("LIB");
+ if (LibEnv) {
+ SmallVector<StringRef> EnvPaths;
+ StringRef(*LibEnv).split(EnvPaths, ';', /*MaxSplit=*/-1,
+ /*KeepEmpty=*/false);
+ LibraryPaths.append(EnvPaths);
+ InputPaths.append(EnvPaths);
+ }
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
// Try to extract device code from the linker input files.
- bool WholeArchive = Args.hasArg(OPT_wholearchive_flag);
+ bool WholeArchive =
+ Args.hasArg(OPT_wholearchive_flag) ||
+ llvm::any_of(Args.filtered(OPT_INPUT), [&](const opt::Arg *Arg) {
+ std::optional<StringRef> Rest =
+ GetCOFFOptionValue(Arg->getValue(), "wholearchive");
+ return Rest && Rest->empty();
+ });
SmallVector<OffloadFile> ObjectFilesToExtract;
SmallVector<OffloadFile> ArchiveFilesToExtract;
DenseMap<StringRef, StringRef> SourceForImage;
@@ -1462,11 +1500,23 @@ getDeviceInput(const ArgList &Args) {
continue;
}
+ StringRef Input = Arg->getValue();
+ bool IsWholeArchiveFile = Arg->getOption().matches(OPT_wholearchive_file);
+ if (Arg->getOption().matches(OPT_INPUT)) {
+ // Upper-case spellings of the COFF options in the option table.
+ std::optional<StringRef> Rest = GetCOFFOptionValue(Input, "wholearchive");
+ if (Rest && Rest->consume_front(":")) {
+ Input = *Rest;
+ IsWholeArchiveFile = true;
+ } else if (Rest || GetCOFFOptionValue(Input, "libpath:")) {
+ continue;
+ }
+ }
+
std::optional<std::string> Filename =
Arg->getOption().matches(OPT_library)
- ? searchLibrary(Arg->getValue(), Root, LibraryPaths,
- HostTriple.isOSWindows())
- : searchInput(Arg->getValue(), Root, InputPaths);
+ ? searchLibrary(Input, Root, LibraryPaths, HostTriple.isOSWindows())
+ : searchInput(Input, Root, InputPaths);
if (!Filename && Arg->getOption().matches(OPT_library))
return createStringError("unable to find library -l%s", Arg->getValue());
@@ -1474,9 +1524,9 @@ getDeviceInput(const ArgList &Args) {
if (!Filename || !sys::fs::exists(*Filename)) {
if (ReportMissingInputs &&
!UnknownOptionIndices.contains(Arg->getIndex() - 1) &&
- !IsCOFFLinkerOption(Arg->getValue()))
+ !IsCOFFLinkerOption(Input))
return createFileError(
- Filename ? *Filename : Arg->getValue(),
+ Filename ? *Filename : Input,
make_error_code(errc::no_such_file_or_directory));
continue;
}
@@ -1484,8 +1534,7 @@ getDeviceInput(const ArgList &Args) {
continue;
// Unlike `--whole-archive`, `/wholearchive:` applies to a single library.
- bool ExtractWholeArchive =
- WholeArchive || Arg->getOption().matches(OPT_wholearchive_file);
+ bool ExtractWholeArchive = WholeArchive || IsWholeArchiveFile;
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFileOrSTDIN(*Filename);
>From 245bceff11d425bc3555ce8dedd7044a7973d6ea Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:36:53 -0800
Subject: [PATCH 13/33] [Clang][SYCL] Address review: clarify forwarded
options, default name, tests
Say that -Wl and -Xlinker values are forwarded with -fsycl-link, note
the default output name in the docs, and tighten the Windows test.
---
clang/docs/SYCLSupport.md | 3 ++-
clang/lib/Driver/ToolChains/Clang.cpp | 5 +++--
clang/test/Driver/sycl-link.cpp | 5 ++++-
3 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index 4363305b242f4..855cdcee117df 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -119,7 +119,8 @@ 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
```
-On Windows, the host link uses `LLVMSYCL.lib` instead of `-lLLVMSYCL`.
+On Windows, the host link uses `LLVMSYCL.lib` instead of `-lLLVMSYCL`. Without
+`-o`, the output is named as for a host link, e.g. `a.out` or `a.exe`.
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.
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index d472407063936..499859bff4006 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -10143,8 +10143,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
Args.hasArg(options::OPT_fsycl_link)) {
// With -fsycl-link, the wrapper writes the wrapped SYCL device object to
// the output and no host link runs. Pass only what is needed to find the
- // device code: the sysroot, library paths and link inputs. Other host link
- // options, startup files and default libraries are not used.
+ // device code: the sysroot, library paths and link inputs, which include
+ // -Wl and -Xlinker values. Startup files and default libraries are not
+ // added.
CmdArgs.push_back("--sycl-device-link");
CmdArgs.append({"-o", Output.getFilename()});
const Driver &D = getToolChain().getDriver();
diff --git a/clang/test/Driver/sycl-link.cpp b/clang/test/Driver/sycl-link.cpp
index bb90cce277c9a..1d5f11ffe5c21 100644
--- a/clang/test/Driver/sycl-link.cpp
+++ b/clang/test/Driver/sycl-link.cpp
@@ -37,7 +37,7 @@
// 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" "-o" "dev.obj" "{{.*}}a.o"{{$}}
+// WINDOWS: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o" "dev.obj" "{{[^"]*}}a.o"{{$}}
// 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
@@ -84,6 +84,9 @@
// ONLY: error: invalid argument '-fsycl-link' not allowed with '[[OPT]]'
/// The last of the mode options selects the mode.
+// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \
+// RUN: -fsycl-device-only -fsycl-host-only %t.a.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=ONLY -DOPT=-fsycl-host-only
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-link \
// RUN: --offload-device-only --offload-host-device %t.a.o 2>&1 \
// RUN: | FileCheck %s --check-prefix=HOST-DEVICE
>From ef619c32692134b93189d97b0f74903432fe54d3 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 09:39:48 -0800
Subject: [PATCH 14/33] [clang-linker-wrapper][SYCL] Address review: search
COFF paths in order, drop unused '-' match
Collect -L, -libpath: and /LIBPATH: paths in one pass so they are
searched in command line order, as link.exe does. '-' spellings in other
cases are parsed as options, not inputs, so only '/' spellings are matched.
---
.../ClangLinkerWrapper.cpp | 35 +++++++++++--------
1 file changed, 20 insertions(+), 15 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 695582522bfb3..272cdd69a4e91 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1415,32 +1415,37 @@ getDeviceInput(const ArgList &Args) {
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
// link.exe options are case-insensitive, but the COFF options in the option
- // table only match lower case. Returns the value of option \p Name if
- // \p Input is that option in any case.
+ // table only match lower case. A '/' spelling in another case, e.g.
+ // '/LIBPATH:', is parsed as an input. Returns the value of option \p Name if
+ // \p Input is such a spelling of it.
auto GetCOFFOptionValue = [&](StringRef Input,
StringRef Name) -> std::optional<StringRef> {
- if (!HostTriple.isOSWindows() ||
- !(Input.consume_front("/") || Input.consume_front("-")) ||
+ if (!HostTriple.isOSWindows() || !Input.consume_front("/") ||
!Input.consume_front_insensitive(Name))
return std::nullopt;
return Input;
};
+ // Libraries are searched in -L and -libpath: paths. Only `link.exe` style
+ // linkers also search -libpath: paths for other inputs. Paths are searched
+ // in command line order.
StringRef Root = Args.getLastArgValue(OPT_sysroot_EQ);
SmallVector<StringRef> LibraryPaths;
- for (const opt::Arg *Arg : Args.filtered(OPT_library_path, OPT_libpath))
- LibraryPaths.push_back(Arg->getValue());
-
- // Only `link.exe` style linkers search for their input files.
SmallVector<StringRef> InputPaths;
- for (const opt::Arg *Arg : Args.filtered(OPT_libpath))
- InputPaths.push_back(Arg->getValue());
- for (const opt::Arg *Arg : Args.filtered(OPT_INPUT)) {
- if (std::optional<StringRef> Path =
- GetCOFFOptionValue(Arg->getValue(), "libpath:")) {
- LibraryPaths.push_back(*Path);
- InputPaths.push_back(*Path);
+ for (const opt::Arg *Arg :
+ Args.filtered(OPT_library_path, OPT_libpath, OPT_INPUT)) {
+ if (Arg->getOption().matches(OPT_library_path)) {
+ LibraryPaths.push_back(Arg->getValue());
+ continue;
}
+ std::optional<StringRef> Path =
+ Arg->getOption().matches(OPT_libpath)
+ ? std::optional<StringRef>(Arg->getValue())
+ : GetCOFFOptionValue(Arg->getValue(), "libpath:");
+ if (!Path)
+ continue;
+ LibraryPaths.push_back(*Path);
+ InputPaths.push_back(*Path);
}
// link.exe also searches the paths in the LIB environment variable.
>From 7e21190b33aeb5b5b2cbc5f61251223c0a5a4ffa Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:24:44 -0800
Subject: [PATCH 15/33] [clang-linker-wrapper][SYCL] Address review: keep
inputs that start like /WHOLEARCHIVE
Only a bare /WHOLEARCHIVE is the flag. A path such as
/wholearchive-dir/a.obj, possible when targeting Windows from a POSIX
host, is an input and was skipped.
---
.../clang-linker-wrapper/linker-wrapper-sycl-device-link.c | 5 +++++
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 5 ++++-
2 files changed, 9 insertions(+), 1 deletion(-)
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
index 0d02c1b268547..5315036756fa5 100644
--- 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
@@ -65,6 +65,11 @@
// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=nope.lib
+// A path that starts like a COFF option is still an input.
+// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe /wholearchive-dir/missing.obj \
+// RUN: -o dev.obj 2>&1 \
+// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=/wholearchive-dir/missing.obj
// With --wrapper-verbose and --save-temps, the wrapping is still done
// in-process, because the image must be embedded as finalized.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 272cdd69a4e91..fe670e5765590 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1513,7 +1513,10 @@ getDeviceInput(const ArgList &Args) {
if (Rest && Rest->consume_front(":")) {
Input = *Rest;
IsWholeArchiveFile = true;
- } else if (Rest || GetCOFFOptionValue(Input, "libpath:")) {
+ } else if ((Rest && Rest->empty()) ||
+ GetCOFFOptionValue(Input, "libpath:")) {
+ // '/WHOLEARCHIVE' or '/LIBPATH:', both handled above. Anything else,
+ // e.g. '/wholearchive-dir/a.obj', is an input path.
continue;
}
}
>From 357326be7cc7f67a871c44a9e92afa68aadb3a49 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:25:54 -0800
Subject: [PATCH 16/33] [clang-linker-wrapper][SYCL] Note the limit of the COFF
option check
---
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index fe670e5765590..fe6d2987c8218 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1488,7 +1488,8 @@ getDeviceInput(const ArgList &Args) {
UnknownOptionIndices.insert(Arg->getIndex());
// COFF linker options such as '/nologo' or '/LIBPATH:C:\dir' are parsed as
// inputs. Unlike a path, the option name before any ':' has no further path
- // separator.
+ // separator. A missing root-level path such as '/a.obj' looks the same, so it
+ // is not reported.
auto IsCOFFLinkerOption = [&](StringRef Input) {
if (!HostTriple.isOSWindows() || !Input.consume_front("/"))
return false;
>From bfafd2f945913df038e9b121ac3dc3390b53071b Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:40:25 -0800
Subject: [PATCH 17/33] [clang-linker-wrapper] Address review: accept /OUT: in
any case
link.exe options are case-insensitive, but only a lower-case /out: set
the output name. With /OUT: and no -o, the output was written to a.exe.
---
.../linker-wrapper-sycl-device-link.c | 5 +++++
.../clang-linker-wrapper/ClangLinkerWrapper.cpp | 15 +++++++++++++--
2 files changed, 18 insertions(+), 2 deletions(-)
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
index 5315036756fa5..ab2dea71d5f88 100644
--- 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
@@ -48,6 +48,11 @@
// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
+// link.exe options are case-insensitive, so /OUT: names the output too.
+// RUN: rm -f %t.out.obj
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /OUT:%t.out.obj
+// RUN: llvm-readobj --file-header %t.out.obj | FileCheck %s --check-prefix=WIN
// WIN: Format: COFF-x86-64
// link.exe options are case-insensitive, and link.exe also searches LIB.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index fe6d2987c8218..b60b7d0b43c7f 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1698,10 +1698,21 @@ int main(int Argc, char **Argv) {
llvm::Triple Triple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
+ // link.exe options are case-insensitive, but the option table only matches
+ // '/out:' in lower case. Another spelling, e.g. '/OUT:', is parsed as an
+ // input.
+ StringRef COFFOutput;
+ for (const opt::Arg *Arg : Args.filtered(OPT_out, OPT_INPUT)) {
+ StringRef Value = Arg->getValue();
+ if (Arg->getOption().matches(OPT_out) ||
+ (Triple.isOSWindows() && Value.consume_front("/") &&
+ Value.consume_front_insensitive("out:")))
+ COFFOutput = Value;
+ }
if (Args.hasArg(OPT_o))
ExecutableName = Args.getLastArgValue(OPT_o, "a.out");
- else if (Args.hasArg(OPT_out))
- ExecutableName = Args.getLastArgValue(OPT_out, "a.exe");
+ else if (!COFFOutput.empty())
+ ExecutableName = COFFOutput;
else
ExecutableName = Triple.isOSWindows() ? "a.exe" : "a.out";
>From e049de23df305f13bded195b408f6786f4e47155 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:43:49 -0800
Subject: [PATCH 18/33] [clang-linker-wrapper] Address review: pass the wrapped
image after /OUT: in any case
The wrapped device image is added to the host link after the output
option, but only a lower-case /out: was recognized. With /OUT: the image
was not linked. Share the case-insensitive COFF option match between
main, runLinker and getDeviceInput.
---
.../linker-wrapper-sycl-device-link.c | 5 ++
.../ClangLinkerWrapper.cpp | 49 ++++++++++++-------
2 files changed, 35 insertions(+), 19 deletions(-)
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
index ab2dea71d5f88..ecc899a18922d 100644
--- 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
@@ -66,6 +66,11 @@
// RUN: env LIB=%t.libdir clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
// RUN: --dry-run --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE \
// RUN: foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
+// In a host link, the wrapped image follows /OUT: in any case.
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=link.exe %t.win.obj /OUT:app.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WIN-LINK
+// WIN-LINK: "link.exe" {{.*}}win.obj /OUT:app.exe {{.*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o{{$}}
// WINLIB: --target=spirv64-unknown-unknown {{.*}}win-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index b60b7d0b43c7f..951e15b13d8d3 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -358,6 +358,25 @@ Error relocateOffloadSection(const ArgList &Args, StringRef Output) {
return Error::success();
}
+/// link.exe options are case-insensitive, but the COFF options in the option
+/// table only match lower case, so a '/' spelling in another case, e.g.
+/// '/OUT:', is parsed as an input. Returns the value of option \p Name if
+/// \p Input is such a spelling of it and the host is Windows.
+std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
+ const Triple &HostTriple) {
+ if (!HostTriple.isOSWindows() || !Input.consume_front("/") ||
+ !Input.consume_front_insensitive(Name))
+ return std::nullopt;
+ return Input;
+}
+
+/// Returns true if \p Arg names the output file.
+bool isOutputArg(const opt::Arg *Arg, const Triple &HostTriple) {
+ return Arg->getOption().matches(OPT_o) || Arg->getOption().matches(OPT_out) ||
+ (Arg->getOption().matches(OPT_INPUT) &&
+ getCOFFOptionValue(Arg->getValue(), "out:", HostTriple));
+}
+
/// Runs the wrapped linker job with the newly created input.
Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
llvm::TimeTraceScope TimeScope("Execute host linker");
@@ -367,6 +386,8 @@ Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
StringRef LinkerPath = Args.getLastArgValue(OPT_linker_path_EQ);
if (LinkerPath.empty())
return createStringError("linker path missing, must pass 'linker-path'");
+ const Triple HostTriple(
+ Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
ArgStringList NewLinkerArgs;
for (const opt::Arg *Arg : Args) {
// Do not forward arguments only intended for the linker wrapper.
@@ -374,7 +395,7 @@ Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
continue;
Arg->render(Args, NewLinkerArgs);
- if (Arg->getOption().matches(OPT_o) || Arg->getOption().matches(OPT_out))
+ if (isOutputArg(Arg, HostTriple))
llvm::transform(Files, std::back_inserter(NewLinkerArgs),
[&](StringRef A) { return Args.MakeArgString(A); });
}
@@ -1414,16 +1435,8 @@ getDeviceInput(const ArgList &Args) {
const llvm::Triple HostTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
- // link.exe options are case-insensitive, but the COFF options in the option
- // table only match lower case. A '/' spelling in another case, e.g.
- // '/LIBPATH:', is parsed as an input. Returns the value of option \p Name if
- // \p Input is such a spelling of it.
- auto GetCOFFOptionValue = [&](StringRef Input,
- StringRef Name) -> std::optional<StringRef> {
- if (!HostTriple.isOSWindows() || !Input.consume_front("/") ||
- !Input.consume_front_insensitive(Name))
- return std::nullopt;
- return Input;
+ auto GetCOFFOptionValue = [&](StringRef Input, StringRef Name) {
+ return getCOFFOptionValue(Input, Name, HostTriple);
};
// Libraries are searched in -L and -libpath: paths. Only `link.exe` style
@@ -1698,16 +1711,14 @@ int main(int Argc, char **Argv) {
llvm::Triple Triple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
- // link.exe options are case-insensitive, but the option table only matches
- // '/out:' in lower case. Another spelling, e.g. '/OUT:', is parsed as an
- // input.
+ // The last '/out:' in any case names the output if there is no -o.
StringRef COFFOutput;
for (const opt::Arg *Arg : Args.filtered(OPT_out, OPT_INPUT)) {
- StringRef Value = Arg->getValue();
- if (Arg->getOption().matches(OPT_out) ||
- (Triple.isOSWindows() && Value.consume_front("/") &&
- Value.consume_front_insensitive("out:")))
- COFFOutput = Value;
+ if (Arg->getOption().matches(OPT_out))
+ COFFOutput = Arg->getValue();
+ else if (std::optional<StringRef> Value =
+ getCOFFOptionValue(Arg->getValue(), "out:", Triple))
+ COFFOutput = *Value;
}
if (Args.hasArg(OPT_o))
ExecutableName = Args.getLastArgValue(OPT_o, "a.out");
>From f5178eab09554d253423182577d40a8c72568f21 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:46:07 -0800
Subject: [PATCH 19/33] [clang-linker-wrapper] Address review: add the wrapped
image to the host link once
The image was added after every output option, so -o x.exe /OUT:x.exe
or -o with /out: passed the same object twice.
---
.../clang-linker-wrapper/linker-wrapper-sycl-device-link.c | 5 +++++
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 6 +++++-
2 files changed, 10 insertions(+), 1 deletion(-)
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
index ecc899a18922d..02b92de7dbe35 100644
--- 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
@@ -71,6 +71,11 @@
// RUN: --linker-path=link.exe %t.win.obj /OUT:app.exe 2>&1 \
// RUN: | FileCheck %s --check-prefix=WIN-LINK
// WIN-LINK: "link.exe" {{.*}}win.obj /OUT:app.exe {{.*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o{{$}}
+// The image is added once, even if the output is named more than once.
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=link.exe %t.win.obj -o app.exe /OUT:app.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WIN-ONCE
+// WIN-ONCE: "link.exe" {{.*}}win.obj -o app.exe {{[^ ]*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o /OUT:app.exe{{$}}
// WINLIB: --target=spirv64-unknown-unknown {{.*}}win-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 951e15b13d8d3..20a50ff9c6b7f 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -389,15 +389,19 @@ Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
const Triple HostTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
ArgStringList NewLinkerArgs;
+ bool AddedFiles = false;
for (const opt::Arg *Arg : Args) {
// Do not forward arguments only intended for the linker wrapper.
if (Arg->getOption().hasFlag(WrapperOnlyOption))
continue;
Arg->render(Args, NewLinkerArgs);
- if (isOutputArg(Arg, HostTriple))
+ // Add the image once, even if the output is named more than once.
+ if (!AddedFiles && isOutputArg(Arg, HostTriple)) {
llvm::transform(Files, std::back_inserter(NewLinkerArgs),
[&](StringRef A) { return Args.MakeArgString(A); });
+ AddedFiles = true;
+ }
}
SmallVector<StringRef> LinkerArgs({LinkerPath});
>From a7b08d88080cfc8e8229b70c47ad08203307f5ff Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:53:21 -0800
Subject: [PATCH 20/33] [clang-linker-wrapper] Address review: use the last
output option on Windows hosts
link.exe uses the last of its output options, so clang-cl '/link /out:x'
names the output in a host link. The wrapper preferred -o over /out:,
so with --sycl-device-link the output was written to the -o name instead.
On Windows hosts the last of -o and /out: in any case now wins.
---
.../linker-wrapper-sycl-device-link.c | 7 +++++
.../ClangLinkerWrapper.cpp | 28 +++++++++++--------
2 files changed, 23 insertions(+), 12 deletions(-)
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
index 02b92de7dbe35..a9a1e7c238057 100644
--- 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
@@ -53,6 +53,13 @@
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /OUT:%t.out.obj
// RUN: llvm-readobj --file-header %t.out.obj | FileCheck %s --check-prefix=WIN
+// As with link.exe, the last of -o and /out: names the output, e.g. for
+// clang-cl '/link /out:'.
+// RUN: rm -f %t.last.obj
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --sycl-device-link --linker-path=link.exe -o %t.first.exe %t.sycl.o \
+// RUN: /Out:%t.last.obj
+// RUN: llvm-readobj --file-header %t.last.obj | FileCheck %s --check-prefix=WIN
// WIN: Format: COFF-x86-64
// link.exe options are case-insensitive, and link.exe also searches LIB.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 20a50ff9c6b7f..c8cf1f34840f7 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1715,19 +1715,23 @@ int main(int Argc, char **Argv) {
llvm::Triple Triple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
- // The last '/out:' in any case names the output if there is no -o.
- StringRef COFFOutput;
- for (const opt::Arg *Arg : Args.filtered(OPT_out, OPT_INPUT)) {
- if (Arg->getOption().matches(OPT_out))
- COFFOutput = Arg->getValue();
- else if (std::optional<StringRef> Value =
- getCOFFOptionValue(Arg->getValue(), "out:", Triple))
- COFFOutput = *Value;
+ StringRef Output;
+ if (Triple.isOSWindows()) {
+ // As with link.exe, the last of -o and '/out:' in any case names the
+ // output, e.g. for clang-cl '/link /out:'.
+ for (const opt::Arg *Arg : Args.filtered(OPT_o, OPT_out, OPT_INPUT)) {
+ if (!Arg->getOption().matches(OPT_INPUT))
+ Output = Arg->getValue();
+ else if (std::optional<StringRef> Value =
+ getCOFFOptionValue(Arg->getValue(), "out:", Triple))
+ Output = *Value;
+ }
+ } else {
+ Output = Args.hasArg(OPT_o) ? Args.getLastArgValue(OPT_o)
+ : Args.getLastArgValue(OPT_out);
}
- if (Args.hasArg(OPT_o))
- ExecutableName = Args.getLastArgValue(OPT_o, "a.out");
- else if (!COFFOutput.empty())
- ExecutableName = COFFOutput;
+ if (!Output.empty())
+ ExecutableName = Output;
else
ExecutableName = Triple.isOSWindows() ? "a.exe" : "a.out";
>From 2fe4658b1c65b917956f87c023a45d889d582f88 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 12:54:09 -0800
Subject: [PATCH 21/33] [Clang][SYCL] Address review: complete the doc example,
test /link /out:
The example linked main.o without creating it. Also test that a clang-cl
/link /out: comes after -o, so it names the output as in a host link.
---
clang/docs/SYCLSupport.md | 4 ++++
clang/test/Driver/sycl-link.cpp | 9 +++++----
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index 855cdcee117df..ba9a9a7ee8a1f 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -116,9 +116,13 @@ 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++ -c main.cpp
clang++ main.o a.o b.o dev.o -L<libsycl dir> -lLLVMSYCL -o app
```
+Here `main.cpp` has no SYCL device code. Device code that is not in the
+inputs of `-fsycl-link` is not in `dev.o`.
+
On Windows, the host link uses `LLVMSYCL.lib` instead of `-lLLVMSYCL`. Without
`-o`, the output is named as for a host link, e.g. `a.out` or `a.exe`.
diff --git a/clang/test/Driver/sycl-link.cpp b/clang/test/Driver/sycl-link.cpp
index 1d5f11ffe5c21..c7f4fbdc28a76 100644
--- a/clang/test/Driver/sycl-link.cpp
+++ b/clang/test/Driver/sycl-link.cpp
@@ -45,17 +45,18 @@
// WINDOWS-BINDINGS-NOT: {{.}}
/// MSVC style links search for named inputs in -libpath: paths, and clang-cl
-/// /link values are passed on.
+/// /link values are passed on after -o, so a /link /out: names the output as in
+/// a host link.
// RUN: %clang -### --target=x86_64-pc-windows-msvc -fsycl -fsycl-link \
// RUN: %t.a.o -L/foo -Wl,/wholearchive:bar.lib -o dev.obj 2>&1 \
// RUN: | FileCheck %s --check-prefix=WINDOWS-LIBPATH
// WINDOWS-LIBPATH: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o" "dev.obj"
// WINDOWS-LIBPATH-SAME: "-libpath:/foo" "{{[^"]*}}a.o" "/wholearchive:bar.lib"{{$}}
// RUN: %clang_cl -### --target=x86_64-pc-windows-msvc -fsycl -fsycl-link \
-// RUN: %t.a.o /link /wholearchive:bar.lib /libpath:dir 2>&1 \
+// RUN: %t.a.o /link /wholearchive:bar.lib /libpath:dir /out:dev.obj 2>&1 \
// RUN: | FileCheck %s --check-prefix=CL-LINK
-// CL-LINK: clang-linker-wrapper"{{.*}} "--sycl-device-link"
-// CL-LINK-SAME: "{{[^"]*}}a.o" "/wholearchive:bar.lib" "/libpath:dir"{{$}}
+// CL-LINK: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o"
+// CL-LINK-SAME: "{{[^"]*}}a.o" "/wholearchive:bar.lib" "/libpath:dir" "/out:dev.obj"{{$}}
/// Without -fsycl-link, the host link is run as usual.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl %t.a.o 2>&1 \
>From ae192090be65ac030d6870bea4fe8752d8b7490f Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 18:06:04 -0800
Subject: [PATCH 22/33] [clang-linker-wrapper][SYCL] Clarify names, comments
and test layout
Rename variables and lambdas to say what they hold or check, simplify
the comments, call getCOFFOptionValue directly instead of through a
same-named lambda, and group the test into sections. No functional
change.
---
.../linker-wrapper-sycl-device-link.c | 176 ++++++++++--------
.../ClangLinkerWrapper.cpp | 93 ++++-----
2 files changed, 148 insertions(+), 121 deletions(-)
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
index a9a1e7c238057..4bbe3617d2d80 100644
--- 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
@@ -4,19 +4,32 @@
// REQUIRES: x86-registered-target
// REQUIRES: spirv-registered-target
+// Inputs: a host object without device code (%t.elf.o), and host objects with
+// SYCL device code for Linux (%t.sycl.o) and Windows (%t.win.obj).
// 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 -cc1 %s -triple x86_64-pc-windows-msvc -emit-obj -o %t.win.obj \
+// RUN: -fembed-offload-object=%t.sycl.out
+
+//===----------------------------------------------------------------------===//
+// The device code is linked, and the host linker is not run.
+//===----------------------------------------------------------------------===//
// 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 {{.*}}sycl-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
+// A value of a host linker option is not taken as an input.
+// 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 -z now \
+// RUN: -Map out.map -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
+
// Archive device code is only extracted for device targets that other inputs
// have. Here no other input has device code, so --whole-archive is needed.
// RUN: rm -f %t.sycl.a && llvm-ar rcs %t.sycl.a %t.sycl.o
@@ -25,7 +38,20 @@
// 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.
+// With --wrapper-verbose and --save-temps, the wrapping is still done
+// in-process, because llvm-offload-wrapper cannot embed finalized images.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --wrapper-verbose --save-temps --sycl-device-link \
+// RUN: --linker-path=/usr/bin/ld %t.sycl.o -o dev.o 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK \
+// RUN: --implicit-check-not=/usr/bin/ld --implicit-check-not=llvm-offload-wrapper
+
+//===----------------------------------------------------------------------===//
+// The output.
+//===----------------------------------------------------------------------===//
+
+// The output registers the device image with the SYCL runtime. The image is in
+// .sycl_fatbin, not .llvm.offloading, because it is already linked.
// 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
@@ -36,73 +62,14 @@
// SECTIONS: Name: .sycl_fatbin
// SECTIONS-NOT: Name: .llvm.offloading
-// The device image is already linked, so a later wrapped host link does not
-// link it again.
+// So a later wrapped host link does not link the image again.
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --linker-path=/usr/bin/ld %t.dev.o -o a.out 2>&1 \
// RUN: | FileCheck %s --check-prefix=RELINK --implicit-check-not=--sycl-link
// RELINK: "/usr/bin/ld" {{.*}}dev.o -o a.out
-// COFF linker options are not reported as missing inputs.
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
-// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
-// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
-// link.exe options are case-insensitive, so /OUT: names the output too.
-// RUN: rm -f %t.out.obj
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /OUT:%t.out.obj
-// RUN: llvm-readobj --file-header %t.out.obj | FileCheck %s --check-prefix=WIN
-// As with link.exe, the last of -o and /out: names the output, e.g. for
-// clang-cl '/link /out:'.
-// RUN: rm -f %t.last.obj
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --sycl-device-link --linker-path=link.exe -o %t.first.exe %t.sycl.o \
-// RUN: /Out:%t.last.obj
-// RUN: llvm-readobj --file-header %t.last.obj | FileCheck %s --check-prefix=WIN
-// WIN: Format: COFF-x86-64
-
-// link.exe options are case-insensitive, and link.exe also searches LIB.
-// RUN: %clang -cc1 %s -triple x86_64-pc-windows-msvc -emit-obj -o %t.win.obj \
-// RUN: -fembed-offload-object=%t.sycl.out
-// RUN: rm -rf %t.libdir && mkdir %t.libdir
-// RUN: llvm-ar rcs %t.libdir/foo.lib %t.win.obj
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --sycl-device-link --linker-path=link.exe /LIBPATH:%t.libdir \
-// RUN: /WHOLEARCHIVE:foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
-// RUN: env LIB=%t.libdir clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --dry-run --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE \
-// RUN: foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
-// In a host link, the wrapped image follows /OUT: in any case.
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --linker-path=link.exe %t.win.obj /OUT:app.exe 2>&1 \
-// RUN: | FileCheck %s --check-prefix=WIN-LINK
-// WIN-LINK: "link.exe" {{.*}}win.obj /OUT:app.exe {{.*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o{{$}}
-// The image is added once, even if the output is named more than once.
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --linker-path=link.exe %t.win.obj -o app.exe /OUT:app.exe 2>&1 \
-// RUN: | FileCheck %s --check-prefix=WIN-ONCE
-// WIN-ONCE: "link.exe" {{.*}}win.obj -o app.exe {{[^ ]*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o /OUT:app.exe{{$}}
-// WINLIB: --target=spirv64-unknown-unknown {{.*}}win-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
-// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
-// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=nope.lib
-// A path that starts like a COFF option is still an input.
-// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --sycl-device-link --linker-path=link.exe /wholearchive-dir/missing.obj \
-// RUN: -o dev.obj 2>&1 \
-// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=/wholearchive-dir/missing.obj
-
-// With --wrapper-verbose and --save-temps, the wrapping is still done
-// in-process, because the image must be embedded as finalized.
-// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
-// RUN: --wrapper-verbose \
-// RUN: --save-temps --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o \
-// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=LINK \
-// RUN: --implicit-check-not=/usr/bin/ld --implicit-check-not=llvm-offload-wrapper
-
-// No device code: an empty host object without SYCL runtime registration is
-// written to the output.
+// Without device code, the output is an empty host object without SYCL runtime
+// registration.
// 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
@@ -110,6 +77,16 @@
// RUN: llvm-nm %t.empty.o | count 0
// EMPTY: Format: elf64-x86-64
+//===----------------------------------------------------------------------===//
+// Errors.
+//===----------------------------------------------------------------------===//
+
+// 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
+
+// Device code of another offload kind, also through --override-image.
// 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 \
@@ -123,7 +100,8 @@
// RUN: | FileCheck %s --check-prefix=NOT-SYCL
// NOT-SYCL: error: --sycl-device-link requires SYCL device code, got 'openmp'
-// Without a host link, a missing input must be diagnosed here.
+// No host linker runs, so a missing input is reported here: a plain input, one
+// inside --whole-archive, and one after '--'.
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o %t.missing.o \
// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
@@ -134,18 +112,64 @@
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --sycl-device-link --linker-path=/usr/bin/ld -o dev.o -- %t.missing.o \
// RUN: 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
+// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
+
+//===----------------------------------------------------------------------===//
+// Windows hosts: link.exe options are case-insensitive, and link.exe searches
+// the LIB environment variable.
+//===----------------------------------------------------------------------===//
+
+// link.exe options are not reported as missing inputs. The -o after
+// '/OUT:C:/dir/a.exe' names the output, because the last output option wins.
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
+// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
+// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
+
+// /OUT: names the output in any case. The last of -o and /out: wins, e.g. for
+// clang-cl '/link /out:'.
+// RUN: rm -f %t.out.obj %t.last.obj
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /OUT:%t.out.obj
+// RUN: llvm-readobj --file-header %t.out.obj | FileCheck %s --check-prefix=WIN
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --sycl-device-link --linker-path=link.exe -o %t.first.exe %t.sycl.o \
+// RUN: /Out:%t.last.obj
+// RUN: llvm-readobj --file-header %t.last.obj | FileCheck %s --check-prefix=WIN
+// WIN: Format: COFF-x86-64
+
+// Libraries are found through /LIBPATH: and LIB, and /WHOLEARCHIVE: and
+// /WHOLEARCHIVE extract their device code.
+// RUN: rm -rf %t.libdir && mkdir %t.libdir
+// RUN: llvm-ar rcs %t.libdir/foo.lib %t.win.obj
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe /LIBPATH:%t.libdir \
+// RUN: /WHOLEARCHIVE:foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
+// RUN: env LIB=%t.libdir clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: --dry-run --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE \
+// RUN: foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
+// WINLIB: --target=spirv64-unknown-unknown {{.*}}win-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
+
+// Missing inputs are reported: a /WHOLEARCHIVE: library, a path that starts
+// like a link.exe option, and a plain input.
+// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
+// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=nope.lib
+// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe /wholearchive-dir/missing.obj \
+// RUN: -o dev.obj 2>&1 \
+// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=/wholearchive-dir/missing.obj
// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o %t.missing.o \
// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
-// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
-// A value of a host linker option is not reported as a missing input.
-// 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 -z now \
-// RUN: -Map out.map -o dev.o 2>&1 \
-// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
-
-// 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
+// In a host link without --sycl-device-link, the wrapped device object follows
+// /OUT: in any case, and is added once even if the output is named twice.
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=link.exe %t.win.obj /OUT:app.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WIN-LINK
+// WIN-LINK: "link.exe" {{.*}}win.obj /OUT:app.exe {{.*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o{{$}}
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=link.exe %t.win.obj -o app.exe /OUT:app.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WIN-ONCE
+// WIN-ONCE: "link.exe" {{.*}}win.obj -o app.exe {{[^ ]*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o /OUT:app.exe{{$}}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index c8cf1f34840f7..ee9975e700dd0 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -358,10 +358,10 @@ Error relocateOffloadSection(const ArgList &Args, StringRef Output) {
return Error::success();
}
-/// link.exe options are case-insensitive, but the COFF options in the option
-/// table only match lower case, so a '/' spelling in another case, e.g.
-/// '/OUT:', is parsed as an input. Returns the value of option \p Name if
-/// \p Input is such a spelling of it and the host is Windows.
+/// Returns the value of the link.exe option \p Name if \p Input is that option
+/// with a '/' prefix in any case, e.g. "dir" for "/LIBPATH:dir" and "libpath:".
+/// link.exe options are case-insensitive, but the option table only matches
+/// lower case, so other spellings are parsed as inputs. Windows hosts only.
std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
const Triple &HostTriple) {
if (!HostTriple.isOSWindows() || !Input.consume_front("/") ||
@@ -370,7 +370,7 @@ std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
return Input;
}
-/// Returns true if \p Arg names the output file.
+/// Returns true if \p Arg names the output file, e.g. -o, /out: or /OUT:.
bool isOutputArg(const opt::Arg *Arg, const Triple &HostTriple) {
return Arg->getOption().matches(OPT_o) || Arg->getOption().matches(OPT_out) ||
(Arg->getOption().matches(OPT_INPUT) &&
@@ -389,18 +389,19 @@ Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
const Triple HostTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
ArgStringList NewLinkerArgs;
- bool AddedFiles = false;
+ bool AddedWrappedFiles = false;
for (const opt::Arg *Arg : Args) {
// Do not forward arguments only intended for the linker wrapper.
if (Arg->getOption().hasFlag(WrapperOnlyOption))
continue;
Arg->render(Args, NewLinkerArgs);
- // Add the image once, even if the output is named more than once.
- if (!AddedFiles && isOutputArg(Arg, HostTriple)) {
+ // Add the wrapped device objects after the first output option only, even
+ // if the output is named more than once.
+ if (!AddedWrappedFiles && isOutputArg(Arg, HostTriple)) {
llvm::transform(Files, std::back_inserter(NewLinkerArgs),
[&](StringRef A) { return Args.MakeArgString(A); });
- AddedFiles = true;
+ AddedWrappedFiles = true;
}
}
@@ -814,9 +815,9 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
const ArgList &Args, OffloadKind Kind) {
llvm::TimeTraceScope TimeScope("Wrap bundled images");
- // With --sycl-device-link, the wrapped images are already linked. They must
- // not be found and linked again if the output goes through another wrapped
- // host link, so they are embedded as finalized images.
+ // With --sycl-device-link the images are already linked. Embed them as
+ // finalized images, so that a later host link through this tool does not
+ // link them again.
bool IsFinalizedImage = Kind == OFK_SYCL && Args.hasArg(OPT_sycl_device_link);
// We use the discrete tools if we are in verbose mode with '--save-temps'.
@@ -1134,7 +1135,7 @@ Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
// All inputs are SYCL device code, which is wrapped into a single object.
assert(WrappedFiles.size() <= 1 && "expected one wrapped SYCL object");
- StringRef File;
+ StringRef WrappedFile;
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.
@@ -1147,15 +1148,15 @@ Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
auto FileOrErr = compileModule(LibraryOptions, M, OFK_SYCL);
if (!FileOrErr)
return FileOrErr.takeError();
- File = *FileOrErr;
+ WrappedFile = *FileOrErr;
} else {
- File = WrappedFiles.front();
+ WrappedFile = WrappedFiles.front();
}
if (DryRun)
return Error::success();
- if (std::error_code EC = sys::fs::copy_file(File, ExecutableName))
+ if (std::error_code EC = sys::fs::copy_file(WrappedFile, ExecutableName))
return createFileError(ExecutableName, EC);
return Error::success();
}
@@ -1439,13 +1440,9 @@ getDeviceInput(const ArgList &Args) {
const llvm::Triple HostTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
- auto GetCOFFOptionValue = [&](StringRef Input, StringRef Name) {
- return getCOFFOptionValue(Input, Name, HostTriple);
- };
-
- // Libraries are searched in -L and -libpath: paths. Only `link.exe` style
- // linkers also search -libpath: paths for other inputs. Paths are searched
- // in command line order.
+ // Collect the search paths in command line order. -L paths are used for -l
+ // libraries. link.exe style -libpath: paths are used for both libraries and
+ // other inputs.
StringRef Root = Args.getLastArgValue(OPT_sysroot_EQ);
SmallVector<StringRef> LibraryPaths;
SmallVector<StringRef> InputPaths;
@@ -1458,7 +1455,7 @@ getDeviceInput(const ArgList &Args) {
std::optional<StringRef> Path =
Arg->getOption().matches(OPT_libpath)
? std::optional<StringRef>(Arg->getValue())
- : GetCOFFOptionValue(Arg->getValue(), "libpath:");
+ : getCOFFOptionValue(Arg->getValue(), "libpath:", HostTriple);
if (!Path)
continue;
LibraryPaths.push_back(*Path);
@@ -1480,33 +1477,38 @@ getDeviceInput(const ArgList &Args) {
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
- // Try to extract device code from the linker input files.
+ // Try to extract device code from the linker input files. A bare
+ // '/WHOLEARCHIVE' extracts all archives, like --whole-archive.
bool WholeArchive =
Args.hasArg(OPT_wholearchive_flag) ||
llvm::any_of(Args.filtered(OPT_INPUT), [&](const opt::Arg *Arg) {
std::optional<StringRef> Rest =
- GetCOFFOptionValue(Arg->getValue(), "wholearchive");
+ getCOFFOptionValue(Arg->getValue(), "wholearchive", HostTriple);
return Rest && Rest->empty();
});
SmallVector<OffloadFile> ObjectFilesToExtract;
SmallVector<OffloadFile> ArchiveFilesToExtract;
DenseMap<StringRef, StringRef> SourceForImage;
- // With --sycl-device-link no host link runs to report missing inputs, so
- // report them here. An input right after an option that is not known here
- // may be the value of a host linker option (e.g. '-z now'), so it is skipped
- // as in a host link. Without the host linker's options, a file after an
- // unknown flag option is skipped the same way. The '--' separator is not an
- // option.
+ // With --sycl-device-link no host linker runs, so report missing inputs here.
+ // This tool does not know every host linker option, so a missing input is
+ // not reported if it may not be a file:
+ // - it follows an unknown option and may be its value, e.g. 'now' in
+ // '-z now'. A file after an unknown option without a value is also skipped.
+ // - it is a link.exe option, e.g. '/nologo'.
bool ReportMissingInputs = Args.hasArg(OPT_sycl_device_link);
DenseSet<unsigned> UnknownOptionIndices;
for (const opt::Arg *Arg : Args.filtered(OPT_UNKNOWN))
- if (Arg->getSpelling() != "--")
+ if (Arg->getSpelling() != "--") // '--' ends the options and has no value.
UnknownOptionIndices.insert(Arg->getIndex());
- // COFF linker options such as '/nologo' or '/LIBPATH:C:\dir' are parsed as
- // inputs. Unlike a path, the option name before any ':' has no further path
- // separator. A missing root-level path such as '/a.obj' looks the same, so it
- // is not reported.
+ auto FollowsUnknownOption = [&](const opt::Arg *Arg) {
+ return Arg->getIndex() > 0 &&
+ UnknownOptionIndices.contains(Arg->getIndex() - 1);
+ };
+ // link.exe options such as '/nologo' or '/LIBPATH:C:\dir' are parsed as
+ // inputs. Unlike a path, the option name before any ':' has no '/' or '\'.
+ // A missing root-level path such as '/a.obj' looks the same, so it is not
+ // reported.
auto IsCOFFLinkerOption = [&](StringRef Input) {
if (!HostTriple.isOSWindows() || !Input.consume_front("/"))
return false;
@@ -1526,13 +1528,15 @@ getDeviceInput(const ArgList &Args) {
StringRef Input = Arg->getValue();
bool IsWholeArchiveFile = Arg->getOption().matches(OPT_wholearchive_file);
if (Arg->getOption().matches(OPT_INPUT)) {
- // Upper-case spellings of the COFF options in the option table.
- std::optional<StringRef> Rest = GetCOFFOptionValue(Input, "wholearchive");
+ // '/WHOLEARCHIVE:file', '/WHOLEARCHIVE' and '/LIBPATH:dir' in a case
+ // other than the option table's lower case.
+ std::optional<StringRef> Rest =
+ getCOFFOptionValue(Input, "wholearchive", HostTriple);
if (Rest && Rest->consume_front(":")) {
Input = *Rest;
IsWholeArchiveFile = true;
} else if ((Rest && Rest->empty()) ||
- GetCOFFOptionValue(Input, "libpath:")) {
+ getCOFFOptionValue(Input, "libpath:", HostTriple)) {
// '/WHOLEARCHIVE' or '/LIBPATH:', both handled above. Anything else,
// e.g. '/wholearchive-dir/a.obj', is an input path.
continue;
@@ -1548,8 +1552,7 @@ getDeviceInput(const ArgList &Args) {
return createStringError("unable to find library -l%s", Arg->getValue());
if (!Filename || !sys::fs::exists(*Filename)) {
- if (ReportMissingInputs &&
- !UnknownOptionIndices.contains(Arg->getIndex() - 1) &&
+ if (ReportMissingInputs && !FollowsUnknownOption(Arg) &&
!IsCOFFLinkerOption(Input))
return createFileError(
Filename ? *Filename : Input,
@@ -1779,7 +1782,7 @@ int main(int Argc, char **Argv) {
if (EmitFatbinOnly)
reportError(createStringError(
"--sycl-device-link cannot be used with --emit-fatbin-only"));
- auto CheckKind = [](OffloadKind Kind) {
+ auto CheckIsSYCL = [](OffloadKind Kind) {
if (Kind != OFK_SYCL)
reportError(createStringError(
"--sycl-device-link requires SYCL device code, got '" +
@@ -1787,9 +1790,9 @@ int main(int Argc, char **Argv) {
};
for (const SmallVector<OffloadFile> &Files : *DeviceInputFiles)
for (const OffloadFile &File : Files)
- CheckKind(File.getBinary()->getOffloadKind());
+ CheckIsSYCL(File.getBinary()->getOffloadKind());
for (StringRef Override : Args.getAllArgValues(OPT_override_image))
- CheckKind(getOffloadKind(Override.split('=').first));
+ CheckIsSYCL(getOffloadKind(Override.split('=').first));
}
// Link and process the device images. The function may emit a direct fat
>From e9dd1e7ddcc03951f8bc26ab5659ee041c23f818 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 18:09:12 -0800
Subject: [PATCH 23/33] [clang-linker-wrapper][SYCL] Share the link.exe option
checks
Move the /WHOLEARCHIVE and link.exe option checks into helpers next to
getCOFFOptionValue, reuse isOutputArg for the output name, and fix
comment wording. No functional change.
---
.../ClangLinkerWrapper.cpp | 79 +++++++++++--------
1 file changed, 44 insertions(+), 35 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index ee9975e700dd0..98e81738dce44 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -359,9 +359,10 @@ Error relocateOffloadSection(const ArgList &Args, StringRef Output) {
}
/// Returns the value of the link.exe option \p Name if \p Input is that option
-/// with a '/' prefix in any case, e.g. "dir" for "/LIBPATH:dir" and "libpath:".
-/// link.exe options are case-insensitive, but the option table only matches
-/// lower case, so other spellings are parsed as inputs. Windows hosts only.
+/// with a '/' prefix in any case, e.g. returns "dir" for Input "/LIBPATH:dir"
+/// and Name "libpath:". link.exe options are case-insensitive, but the option
+/// table only matches lower case, so other spellings are parsed as inputs.
+/// Windows hosts only.
std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
const Triple &HostTriple) {
if (!HostTriple.isOSWindows() || !Input.consume_front("/") ||
@@ -370,6 +371,25 @@ std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
return Input;
}
+/// Returns true if \p Input is the link.exe flag '/WHOLEARCHIVE' in any case,
+/// which extracts all archives like --whole-archive.
+bool isCOFFWholeArchiveFlag(StringRef Input, const Triple &HostTriple) {
+ std::optional<StringRef> Rest =
+ getCOFFOptionValue(Input, "wholearchive", HostTriple);
+ return Rest && Rest->empty();
+}
+
+/// Returns true if \p Input looks like a link.exe option, e.g. '/nologo' or
+/// '/LIBPATH:C:\dir'. Unlike a path, the option name before any ':' has no '/'
+/// or '\'. A root-level path such as '/a.obj' looks the same. Windows hosts
+/// only.
+bool isCOFFLinkerOption(StringRef Input, const Triple &HostTriple) {
+ if (!HostTriple.isOSWindows() || !Input.consume_front("/"))
+ return false;
+ StringRef Name = Input.split(':').first;
+ return !Name.empty() && Name.find_first_of("/\\") == StringRef::npos;
+}
+
/// Returns true if \p Arg names the output file, e.g. -o, /out: or /OUT:.
bool isOutputArg(const opt::Arg *Arg, const Triple &HostTriple) {
return Arg->getOption().matches(OPT_o) || Arg->getOption().matches(OPT_out) ||
@@ -1132,8 +1152,9 @@ Error handleOverrideImages(
Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
ArrayRef<StringRef> WrappedFiles,
const ArgList &Args) {
- // All inputs are SYCL device code, which is wrapped into a single object.
- assert(WrappedFiles.size() <= 1 && "expected one wrapped SYCL object");
+ // All SYCL device code is wrapped into at most one object.
+ assert(WrappedFiles.size() <= 1 &&
+ "expected at most one wrapped SYCL object");
StringRef WrappedFile;
if (WrappedFiles.empty()) {
@@ -1441,8 +1462,8 @@ getDeviceInput(const ArgList &Args) {
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
// Collect the search paths in command line order. -L paths are used for -l
- // libraries. link.exe style -libpath: paths are used for both libraries and
- // other inputs.
+ // libraries. link.exe /libpath: paths, in any case, are used for both
+ // libraries and other inputs.
StringRef Root = Args.getLastArgValue(OPT_sysroot_EQ);
SmallVector<StringRef> LibraryPaths;
SmallVector<StringRef> InputPaths;
@@ -1482,9 +1503,7 @@ getDeviceInput(const ArgList &Args) {
bool WholeArchive =
Args.hasArg(OPT_wholearchive_flag) ||
llvm::any_of(Args.filtered(OPT_INPUT), [&](const opt::Arg *Arg) {
- std::optional<StringRef> Rest =
- getCOFFOptionValue(Arg->getValue(), "wholearchive", HostTriple);
- return Rest && Rest->empty();
+ return isCOFFWholeArchiveFlag(Arg->getValue(), HostTriple);
});
SmallVector<OffloadFile> ObjectFilesToExtract;
SmallVector<OffloadFile> ArchiveFilesToExtract;
@@ -1505,16 +1524,6 @@ getDeviceInput(const ArgList &Args) {
return Arg->getIndex() > 0 &&
UnknownOptionIndices.contains(Arg->getIndex() - 1);
};
- // link.exe options such as '/nologo' or '/LIBPATH:C:\dir' are parsed as
- // inputs. Unlike a path, the option name before any ':' has no '/' or '\'.
- // A missing root-level path such as '/a.obj' looks the same, so it is not
- // reported.
- auto IsCOFFLinkerOption = [&](StringRef Input) {
- if (!HostTriple.isOSWindows() || !Input.consume_front("/"))
- return false;
- StringRef Name = Input.split(':').first;
- return !Name.empty() && Name.find_first_of("/\\") == StringRef::npos;
- };
for (const opt::Arg *Arg :
Args.filtered(OPT_INPUT, OPT_library, OPT_wholearchive_file,
@@ -1528,17 +1537,16 @@ getDeviceInput(const ArgList &Args) {
StringRef Input = Arg->getValue();
bool IsWholeArchiveFile = Arg->getOption().matches(OPT_wholearchive_file);
if (Arg->getOption().matches(OPT_INPUT)) {
- // '/WHOLEARCHIVE:file', '/WHOLEARCHIVE' and '/LIBPATH:dir' in a case
- // other than the option table's lower case.
- std::optional<StringRef> Rest =
- getCOFFOptionValue(Input, "wholearchive", HostTriple);
- if (Rest && Rest->consume_front(":")) {
- Input = *Rest;
+ // '/WHOLEARCHIVE:file' in a case other than the option table's lower
+ // case is a library to extract whole. '/WHOLEARCHIVE' and '/LIBPATH:dir'
+ // were handled above. Anything else, e.g. '/wholearchive-dir/a.obj', is
+ // an input path.
+ if (std::optional<StringRef> File =
+ getCOFFOptionValue(Input, "wholearchive:", HostTriple)) {
+ Input = *File;
IsWholeArchiveFile = true;
- } else if ((Rest && Rest->empty()) ||
+ } else if (isCOFFWholeArchiveFlag(Input, HostTriple) ||
getCOFFOptionValue(Input, "libpath:", HostTriple)) {
- // '/WHOLEARCHIVE' or '/LIBPATH:', both handled above. Anything else,
- // e.g. '/wholearchive-dir/a.obj', is an input path.
continue;
}
}
@@ -1553,7 +1561,7 @@ getDeviceInput(const ArgList &Args) {
if (!Filename || !sys::fs::exists(*Filename)) {
if (ReportMissingInputs && !FollowsUnknownOption(Arg) &&
- !IsCOFFLinkerOption(Input))
+ !isCOFFLinkerOption(Input, HostTriple))
return createFileError(
Filename ? *Filename : Input,
make_error_code(errc::no_such_file_or_directory));
@@ -1723,11 +1731,12 @@ int main(int Argc, char **Argv) {
// As with link.exe, the last of -o and '/out:' in any case names the
// output, e.g. for clang-cl '/link /out:'.
for (const opt::Arg *Arg : Args.filtered(OPT_o, OPT_out, OPT_INPUT)) {
- if (!Arg->getOption().matches(OPT_INPUT))
- Output = Arg->getValue();
- else if (std::optional<StringRef> Value =
- getCOFFOptionValue(Arg->getValue(), "out:", Triple))
- Output = *Value;
+ if (!isOutputArg(Arg, Triple))
+ continue;
+ // An '/OUT:file' input holds the whole option, -o and /out: the value.
+ Output = Arg->getOption().matches(OPT_INPUT)
+ ? *getCOFFOptionValue(Arg->getValue(), "out:", Triple)
+ : StringRef(Arg->getValue());
}
} else {
Output = Args.hasArg(OPT_o) ? Args.getLastArgValue(OPT_o)
>From 8e4dc7e1b7f1eb7fee5878d41b8fc7f9c6dcaa43 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 18:35:24 -0800
Subject: [PATCH 24/33] [clang-linker-wrapper][SYCL] Explain the SYCL kind
checks
---
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 98e81738dce44..3617ef8b97d36 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1797,9 +1797,14 @@ int main(int Argc, char **Argv) {
"--sycl-device-link requires SYCL device code, got '" +
getOffloadKindName(Kind) + "'"));
};
+ // Only SYCL device code can be wrapped into a SYCL object.
+ // Device code extracted from the inputs.
for (const SmallVector<OffloadFile> &Files : *DeviceInputFiles)
for (const OffloadFile &File : Files)
CheckIsSYCL(File.getBinary()->getOffloadKind());
+ // Device images given with --override-image=<kind>=<file>. With this
+ // option nothing is extracted from the inputs, so the loop above checks
+ // nothing and these images must be checked here.
for (StringRef Override : Args.getAllArgValues(OPT_override_image))
CheckIsSYCL(getOffloadKind(Override.split('=').first));
}
>From 42ddd220ff9582c0270b9f991ac202ed8a3a5baa Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 18:42:48 -0800
Subject: [PATCH 25/33] [clang-linker-wrapper][SYCL] Explain how the output
name is chosen on Windows
---
.../tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 3617ef8b97d36..8fb20997d8ed5 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1728,12 +1728,16 @@ int main(int Argc, char **Argv) {
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
StringRef Output;
if (Triple.isOSWindows()) {
- // As with link.exe, the last of -o and '/out:' in any case names the
- // output, e.g. for clang-cl '/link /out:'.
+ // On Windows the output can be named with -o, /out: or /OUT:. If several
+ // are given, the last one wins, as in link.exe. For example, clang-cl
+ // passes -o first and then the user's '/link /out:x.obj', so the output is
+ // x.obj.
for (const opt::Arg *Arg : Args.filtered(OPT_o, OPT_out, OPT_INPUT)) {
if (!isOutputArg(Arg, Triple))
continue;
- // An '/OUT:file' input holds the whole option, -o and /out: the value.
+ // The parser does not know '/OUT:', so for '/OUT:x.obj' the value is the
+ // whole text. Strip '/OUT:' to get the file name. For -o and /out: the
+ // value is already the file name.
Output = Arg->getOption().matches(OPT_INPUT)
? *getCOFFOptionValue(Arg->getValue(), "out:", Triple)
: StringRef(Arg->getValue());
>From e9daad9d52d066bce667afcecdf405e3b6c3c5c2 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 19:20:36 -0800
Subject: [PATCH 26/33] [clang-linker-wrapper][SYCL] Explain the missing input
check
---
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 8fb20997d8ed5..859ae93dfbd73 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1559,6 +1559,11 @@ getDeviceInput(const ArgList &Args) {
if (!Filename && Arg->getOption().matches(OPT_library))
return createStringError("unable to find library -l%s", Arg->getValue());
+ // The input was not found. With --sycl-device-link no host linker runs to
+ // report it, so report it here. Do not report words that may not be
+ // files: the value of an unknown option, e.g. 'now' in '-z now', or a
+ // link.exe option such as '/nologo'. Those are ignored, as in a normal
+ // link.
if (!Filename || !sys::fs::exists(*Filename)) {
if (ReportMissingInputs && !FollowsUnknownOption(Arg) &&
!isCOFFLinkerOption(Input, HostTriple))
@@ -1567,6 +1572,7 @@ getDeviceInput(const ArgList &Args) {
make_error_code(errc::no_such_file_or_directory));
continue;
}
+ // A directory has no device code.
if (sys::fs::is_directory(*Filename))
continue;
>From 55ecc68bf13ef053693816dbc06abec58038e48b Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 19:30:50 -0800
Subject: [PATCH 27/33] [clang-linker-wrapper][SYCL] Name clang-linker-wrapper
in comments, remove repeated comment
Say clang-linker-wrapper instead of 'this tool' or 'here', and keep the
explanation of the missing input check in one place.
---
.../ClangLinkerWrapper.cpp | 26 +++++++++----------
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 859ae93dfbd73..dc3607421e34e 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -836,8 +836,8 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
llvm::TimeTraceScope TimeScope("Wrap bundled images");
// With --sycl-device-link the images are already linked. Embed them as
- // finalized images, so that a later host link through this tool does not
- // link them again.
+ // finalized images, so that a later host link through clang-linker-wrapper
+ // does not link them again.
bool IsFinalizedImage = Kind == OFK_SYCL && Args.hasArg(OPT_sycl_device_link);
// We use the discrete tools if we are in verbose mode with '--save-temps'.
@@ -1509,13 +1509,10 @@ getDeviceInput(const ArgList &Args) {
SmallVector<OffloadFile> ArchiveFilesToExtract;
DenseMap<StringRef, StringRef> SourceForImage;
- // With --sycl-device-link no host linker runs, so report missing inputs here.
- // This tool does not know every host linker option, so a missing input is
- // not reported if it may not be a file:
- // - it follows an unknown option and may be its value, e.g. 'now' in
- // '-z now'. A file after an unknown option without a value is also skipped.
- // - it is a link.exe option, e.g. '/nologo'.
bool ReportMissingInputs = Args.hasArg(OPT_sycl_device_link);
+ // Positions of the options that clang-linker-wrapper does not know. A word
+ // right after one may be its value, e.g. 'now' in '-z now'. This also hides
+ // a missing file after an unknown option that takes no value.
DenseSet<unsigned> UnknownOptionIndices;
for (const opt::Arg *Arg : Args.filtered(OPT_UNKNOWN))
if (Arg->getSpelling() != "--") // '--' ends the options and has no value.
@@ -1538,8 +1535,9 @@ getDeviceInput(const ArgList &Args) {
bool IsWholeArchiveFile = Arg->getOption().matches(OPT_wholearchive_file);
if (Arg->getOption().matches(OPT_INPUT)) {
// '/WHOLEARCHIVE:file' in a case other than the option table's lower
- // case is a library to extract whole. '/WHOLEARCHIVE' and '/LIBPATH:dir'
- // were handled above. Anything else, e.g. '/wholearchive-dir/a.obj', is
+ // case is a library to extract whole. '/WHOLEARCHIVE' was handled when
+ // setting WholeArchive and '/LIBPATH:dir' when collecting the search
+ // paths, so skip them. Anything else, e.g. '/wholearchive-dir/a.obj', is
// an input path.
if (std::optional<StringRef> File =
getCOFFOptionValue(Input, "wholearchive:", HostTriple)) {
@@ -1560,10 +1558,10 @@ getDeviceInput(const ArgList &Args) {
return createStringError("unable to find library -l%s", Arg->getValue());
// The input was not found. With --sycl-device-link no host linker runs to
- // report it, so report it here. Do not report words that may not be
- // files: the value of an unknown option, e.g. 'now' in '-z now', or a
- // link.exe option such as '/nologo'. Those are ignored, as in a normal
- // link.
+ // report it, so clang-linker-wrapper reports it. Do not report words that
+ // may not be files: the value of an unknown option, e.g. 'now' in
+ // '-z now', or a link.exe option such as '/nologo'. Those are ignored, as
+ // in a normal link.
if (!Filename || !sys::fs::exists(*Filename)) {
if (ReportMissingInputs && !FollowsUnknownOption(Arg) &&
!isCOFFLinkerOption(Input, HostTriple))
>From 355885c1365d44df2166864bac2bd740512fad48 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 19:56:31 -0800
Subject: [PATCH 28/33] [clang-linker-wrapper][SYCL] Explain WholeArchive and
the link.exe options in the input loop
---
.../ClangLinkerWrapper.cpp | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index dc3607421e34e..75a8570c658ce 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1498,8 +1498,12 @@ getDeviceInput(const ArgList &Args) {
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
- // Try to extract device code from the linker input files. A bare
- // '/WHOLEARCHIVE' extracts all archives, like --whole-archive.
+ // Try to extract device code from the input files: the objects and static
+ // libraries on the host link command line.
+ // WholeArchive: take the device code from every member of every static
+ // library, not only from the members that match the other inputs' device
+ // targets. link.exe '/WHOLEARCHIVE', without a file name and in any case,
+ // turns it on, like --whole-archive.
bool WholeArchive =
Args.hasArg(OPT_wholearchive_flag) ||
llvm::any_of(Args.filtered(OPT_INPUT), [&](const opt::Arg *Arg) {
@@ -1534,11 +1538,12 @@ getDeviceInput(const ArgList &Args) {
StringRef Input = Arg->getValue();
bool IsWholeArchiveFile = Arg->getOption().matches(OPT_wholearchive_file);
if (Arg->getOption().matches(OPT_INPUT)) {
- // '/WHOLEARCHIVE:file' in a case other than the option table's lower
- // case is a library to extract whole. '/WHOLEARCHIVE' was handled when
- // setting WholeArchive and '/LIBPATH:dir' when collecting the search
- // paths, so skip them. Anything else, e.g. '/wholearchive-dir/a.obj', is
- // an input path.
+ // The parser does not know upper-case link.exe options, so they reach
+ // this loop as inputs:
+ // - '/WHOLEARCHIVE:foo.lib': take all device code from foo.lib.
+ // - '/WHOLEARCHIVE' and '/LIBPATH:dir': not files. They were used when
+ // setting WholeArchive and collecting the search paths, so skip them.
+ // Anything else, e.g. '/wholearchive-dir/a.obj', is a file.
if (std::optional<StringRef> File =
getCOFFOptionValue(Input, "wholearchive:", HostTriple)) {
Input = *File;
>From 857a270701464dd3d38ac4ee1544ab49b757f14b Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 12 Nov 2026 11:55:34 -0800
Subject: [PATCH 29/33] [clang-linker-wrapper][SYCL] Clarify the remaining
comments
---
.../ClangLinkerWrapper.cpp | 54 +++++++++----------
1 file changed, 27 insertions(+), 27 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 75a8570c658ce..abd028524b8ac 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -358,11 +358,10 @@ Error relocateOffloadSection(const ArgList &Args, StringRef Output) {
return Error::success();
}
-/// Returns the value of the link.exe option \p Name if \p Input is that option
-/// with a '/' prefix in any case, e.g. returns "dir" for Input "/LIBPATH:dir"
-/// and Name "libpath:". link.exe options are case-insensitive, but the option
-/// table only matches lower case, so other spellings are parsed as inputs.
-/// Windows hosts only.
+/// link.exe options are case-insensitive, but the option table only knows
+/// them in lower case, so for example '/LIBPATH:dir' is parsed as an input.
+/// If \p Input is the link.exe option \p Name in any case, returns its value,
+/// e.g. "dir" for Input "/LIBPATH:dir" and Name "libpath:". Windows hosts only.
std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
const Triple &HostTriple) {
if (!HostTriple.isOSWindows() || !Input.consume_front("/") ||
@@ -371,18 +370,17 @@ std::optional<StringRef> getCOFFOptionValue(StringRef Input, StringRef Name,
return Input;
}
-/// Returns true if \p Input is the link.exe flag '/WHOLEARCHIVE' in any case,
-/// which extracts all archives like --whole-archive.
+/// Returns true if \p Input is '/WHOLEARCHIVE' without a file name, in any
+/// case. link.exe then takes every member of every static library.
bool isCOFFWholeArchiveFlag(StringRef Input, const Triple &HostTriple) {
std::optional<StringRef> Rest =
getCOFFOptionValue(Input, "wholearchive", HostTriple);
return Rest && Rest->empty();
}
-/// Returns true if \p Input looks like a link.exe option, e.g. '/nologo' or
-/// '/LIBPATH:C:\dir'. Unlike a path, the option name before any ':' has no '/'
-/// or '\'. A root-level path such as '/a.obj' looks the same. Windows hosts
-/// only.
+/// Returns true if \p Input looks like a link.exe option, such as '/nologo' or
+/// '/LIBPATH:C:\dir', rather than a file path. A root-level path such as
+/// '/a.obj' also looks like an option. Windows hosts only.
bool isCOFFLinkerOption(StringRef Input, const Triple &HostTriple) {
if (!HostTriple.isOSWindows() || !Input.consume_front("/"))
return false;
@@ -416,8 +414,8 @@ Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
continue;
Arg->render(Args, NewLinkerArgs);
- // Add the wrapped device objects after the first output option only, even
- // if the output is named more than once.
+ // Add the wrapped device objects only once, after the first output option,
+ // even if the output is named more than once.
if (!AddedWrappedFiles && isOutputArg(Arg, HostTriple)) {
llvm::transform(Files, std::back_inserter(NewLinkerArgs),
[&](StringRef A) { return Args.MakeArgString(A); });
@@ -835,13 +833,14 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
const ArgList &Args, OffloadKind Kind) {
llvm::TimeTraceScope TimeScope("Wrap bundled images");
- // With --sycl-device-link the images are already linked. Embed them as
- // finalized images, so that a later host link through clang-linker-wrapper
- // does not link them again.
+ // With --sycl-device-link the device images are already linked. Mark them as
+ // finalized, so that a later host link through clang-linker-wrapper does not
+ // try to link them again.
bool IsFinalizedImage = Kind == OFK_SYCL && Args.hasArg(OPT_sycl_device_link);
// We use the discrete tools if we are in verbose mode with '--save-temps'.
- // llvm-offload-wrapper has no option to embed finalized images.
+ // llvm-offload-wrapper cannot mark images as finalized, so it is not used for
+ // --sycl-device-link.
if (Verbose && SaveTemps && !Args.hasArg(OPT_print_wrapped_module) &&
!IsFinalizedImage)
return wrapDeviceImagesVerbose(Buffers, Args, Kind);
@@ -1147,8 +1146,8 @@ 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.
+/// Writes the output of --sycl-device-link: the wrapped SYCL device object,
+/// to be used later in a host link. The host linker is not run.
Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
ArrayRef<StringRef> WrappedFiles,
const ArgList &Args) {
@@ -1158,8 +1157,8 @@ Error emitSYCLDeviceLinkOutput(const cl::LibraryOptionsContext &LibraryOptions,
StringRef WrappedFile;
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.
+ // No device code was linked. Write an empty host object without SYCL
+ // runtime registration, so the output file still exists for the host link.
Triple HostTriple(Args.getLastArgValue(OPT_host_triple_EQ,
sys::getDefaultTargetTriple()));
LLVMContext Context;
@@ -1461,9 +1460,9 @@ getDeviceInput(const ArgList &Args) {
const llvm::Triple HostTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
- // Collect the search paths in command line order. -L paths are used for -l
- // libraries. link.exe /libpath: paths, in any case, are used for both
- // libraries and other inputs.
+ // The directories that the host linker searches for libraries and, with
+ // link.exe, for other inputs. They are kept in command line order, which is
+ // the order the host linker searches them in.
StringRef Root = Args.getLastArgValue(OPT_sysroot_EQ);
SmallVector<StringRef> LibraryPaths;
SmallVector<StringRef> InputPaths;
@@ -1483,7 +1482,8 @@ getDeviceInput(const ArgList &Args) {
InputPaths.push_back(*Path);
}
- // link.exe also searches the paths in the LIB environment variable.
+ // link.exe also searches the directories listed in the LIB environment
+ // variable.
std::optional<std::string> LibEnv;
if (HostTriple.isWindowsMSVCEnvironment())
LibEnv = sys::Process::GetEnv("LIB");
@@ -1797,8 +1797,8 @@ 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.
+ // With --sycl-device-link, write the wrapped device object to the output
+ // instead of running the host linker.
bool SYCLDeviceLink = Args.hasArg(OPT_sycl_device_link);
if (SYCLDeviceLink) {
if (EmitFatbinOnly)
>From 8a3d04f3caae00e655a15fb9243b249d504ff89f Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 12 Nov 2026 11:56:03 -0800
Subject: [PATCH 30/33] [clang-linker-wrapper][SYCL] Clarify the test comments
---
.../linker-wrapper-sycl-device-link.c | 29 +++++++++++--------
1 file changed, 17 insertions(+), 12 deletions(-)
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
index 4bbe3617d2d80..668eee384ad3a 100644
--- 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
@@ -24,22 +24,24 @@
// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
// LINK: "{{.*}}clang{{(.exe)?}}" {{.*}}--target=spirv64-unknown-unknown {{.*}}sycl-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
-// A value of a host linker option is not taken as an input.
+// 'now' in '-z now' and 'out.map' in '-Map out.map' are values of host linker
+// options, not missing inputs.
// 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 -z now \
// RUN: -Map out.map -o dev.o 2>&1 \
// RUN: | FileCheck %s --check-prefix=LINK --implicit-check-not=/usr/bin/ld
-// Archive device code is only extracted for device targets that other inputs
-// have. Here no other input has device code, so --whole-archive is needed.
+// Device code in a static library is only extracted for device targets that
+// the other inputs have. No other input has device code here, so
+// --whole-archive is needed.
// 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
-// With --wrapper-verbose and --save-temps, the wrapping is still done
-// in-process, because llvm-offload-wrapper cannot embed finalized images.
+// With --wrapper-verbose and --save-temps, llvm-offload-wrapper is still not
+// used, because it cannot mark images as finalized.
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --wrapper-verbose --save-temps --sycl-device-link \
// RUN: --linker-path=/usr/bin/ld %t.sycl.o -o dev.o 2>&1 \
@@ -47,7 +49,7 @@
// RUN: --implicit-check-not=/usr/bin/ld --implicit-check-not=llvm-offload-wrapper
//===----------------------------------------------------------------------===//
-// The output.
+// The output file.
//===----------------------------------------------------------------------===//
// The output registers the device image with the SYCL runtime. The image is in
@@ -62,7 +64,8 @@
// SECTIONS: Name: .sycl_fatbin
// SECTIONS-NOT: Name: .llvm.offloading
-// So a later wrapped host link does not link the image again.
+// Because the image is finalized, a later host link through
+// clang-linker-wrapper does not link it again.
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --linker-path=/usr/bin/ld %t.dev.o -o a.out 2>&1 \
// RUN: | FileCheck %s --check-prefix=RELINK --implicit-check-not=--sycl-link
@@ -86,7 +89,8 @@
// 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
-// Device code of another offload kind, also through --override-image.
+// OpenMP device code is an error, both in an input and given with
+// --override-image.
// 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 \
@@ -100,8 +104,8 @@
// RUN: | FileCheck %s --check-prefix=NOT-SYCL
// NOT-SYCL: error: --sycl-device-link requires SYCL device code, got 'openmp'
-// No host linker runs, so a missing input is reported here: a plain input, one
-// inside --whole-archive, and one after '--'.
+// No host linker runs, so clang-linker-wrapper reports missing inputs: a plain
+// input, one inside --whole-archive, and one after '--'.
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o %t.missing.o \
// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
@@ -119,8 +123,9 @@
// the LIB environment variable.
//===----------------------------------------------------------------------===//
-// link.exe options are not reported as missing inputs. The -o after
-// '/OUT:C:/dir/a.exe' names the output, because the last output option wins.
+// link.exe options such as /nologo and /LIBPATH:C:\dir are not reported as
+// missing inputs. The -o names the output, because it comes after /OUT: and
+// the last output option wins.
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
>From be21f8f7181db8c56d47b8b567972decfc357e4d Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 12 Nov 2026 12:46:01 -0800
Subject: [PATCH 31/33] [clang-linker-wrapper][SYCL] Remove repeated test cases
Drop RUN lines that test the same code path as another one, and merge
the two Windows host link checks into one.
---
.../linker-wrapper-sycl-device-link.c | 30 +++++--------------
1 file changed, 8 insertions(+), 22 deletions(-)
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
index 668eee384ad3a..d3bfd477591a3 100644
--- 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
@@ -105,15 +105,11 @@
// NOT-SYCL: error: --sycl-device-link requires SYCL device code, got 'openmp'
// No host linker runs, so clang-linker-wrapper reports missing inputs: a plain
-// input, one inside --whole-archive, and one after '--'.
+// input, and one after '--'.
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --sycl-device-link --linker-path=/usr/bin/ld %t.sycl.o %t.missing.o \
// RUN: -o dev.o 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
-// RUN: --sycl-device-link --linker-path=/usr/bin/ld --whole-archive \
-// RUN: %t.missing.o --no-whole-archive -o dev.o 2>&1 \
-// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
-// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --sycl-device-link --linker-path=/usr/bin/ld -o dev.o -- %t.missing.o \
// RUN: 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
// MISSING: error: '[[FILE]]': {{[Nn]}}o such file or directory
@@ -131,12 +127,9 @@
// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
-// /OUT: names the output in any case. The last of -o and /out: wins, e.g. for
-// clang-cl '/link /out:'.
-// RUN: rm -f %t.out.obj %t.last.obj
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /OUT:%t.out.obj
-// RUN: llvm-readobj --file-header %t.out.obj | FileCheck %s --check-prefix=WIN
+// /out: names the output in any case, and the last of -o and /out: wins, e.g.
+// for clang-cl '/link /out:'.
+// RUN: rm -f %t.last.obj
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
// RUN: --sycl-device-link --linker-path=link.exe -o %t.first.exe %t.sycl.o \
// RUN: /Out:%t.last.obj
@@ -155,8 +148,8 @@
// RUN: foo.lib -o dev.obj 2>&1 | FileCheck %s --check-prefix=WINLIB
// WINLIB: --target=spirv64-unknown-unknown {{.*}}win-spirv64-unknown-unknown-generic{{.*}}.o --sycl-link{{$}}
-// Missing inputs are reported: a /WHOLEARCHIVE: library, a path that starts
-// like a link.exe option, and a plain input.
+// Missing inputs are reported: a /WHOLEARCHIVE: library, and a path that starts
+// like a link.exe option.
// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
// RUN: --sycl-device-link --linker-path=link.exe /WHOLEARCHIVE:nope.lib \
// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=nope.lib
@@ -164,17 +157,10 @@
// RUN: --sycl-device-link --linker-path=link.exe /wholearchive-dir/missing.obj \
// RUN: -o dev.obj 2>&1 \
// RUN: | FileCheck %s --check-prefix=MISSING -DFILE=/wholearchive-dir/missing.obj
-// RUN: not clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o %t.missing.o \
-// RUN: -o dev.obj 2>&1 | FileCheck %s --check-prefix=MISSING -DFILE=%t.missing.o
// In a host link without --sycl-device-link, the wrapped device object follows
// /OUT: in any case, and is added once even if the output is named twice.
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --linker-path=link.exe %t.win.obj /OUT:app.exe 2>&1 \
+// RUN: --linker-path=link.exe %t.win.obj /OUT:app.exe -o app.exe 2>&1 \
// RUN: | FileCheck %s --check-prefix=WIN-LINK
-// WIN-LINK: "link.exe" {{.*}}win.obj /OUT:app.exe {{.*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o{{$}}
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
-// RUN: --linker-path=link.exe %t.win.obj -o app.exe /OUT:app.exe 2>&1 \
-// RUN: | FileCheck %s --check-prefix=WIN-ONCE
-// WIN-ONCE: "link.exe" {{.*}}win.obj -o app.exe {{[^ ]*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o /OUT:app.exe{{$}}
+// WIN-LINK: "link.exe" {{.*}}win.obj /OUT:app.exe {{[^ ]*}}app.exe.sycl.image.wrapper{{[^ ]*}}.o -o app.exe{{$}}
>From 0c2ebaecceb6c854179a0b6a6d8238f4a548e173 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 12 Nov 2026 12:54:45 -0800
Subject: [PATCH 32/33] [clang-linker-wrapper][SYCL] Check the Windows output
name in a dry run
The output name is in the printed device link command, so a real link
and llvm-readobj are not needed to check it.
---
.../linker-wrapper-sycl-device-link.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
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
index d3bfd477591a3..fd4f167aa59b2 100644
--- 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
@@ -122,19 +122,17 @@
// link.exe options such as /nologo and /LIBPATH:C:\dir are not reported as
// missing inputs. The -o names the output, because it comes after /OUT: and
// the last output option wins.
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
// RUN: --sycl-device-link --linker-path=link.exe %t.sycl.o /nologo \
-// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o %t.dev.obj
-// RUN: llvm-readobj --file-header %t.dev.obj | FileCheck %s --check-prefix=WIN
+// RUN: /IGNORE:4078 '/LIBPATH:C:\dir' /OUT:C:/dir/a.exe -o dev.obj 2>&1 \
+// RUN: | FileCheck %s --check-prefix=WIN-NAME -DNAME=dev.obj
// /out: names the output in any case, and the last of -o and /out: wins, e.g.
// for clang-cl '/link /out:'.
-// RUN: rm -f %t.last.obj
-// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc \
-// RUN: --sycl-device-link --linker-path=link.exe -o %t.first.exe %t.sycl.o \
-// RUN: /Out:%t.last.obj
-// RUN: llvm-readobj --file-header %t.last.obj | FileCheck %s --check-prefix=WIN
-// WIN: Format: COFF-x86-64
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --sycl-device-link --linker-path=link.exe -o first.exe %t.sycl.o \
+// RUN: /Out:last.obj 2>&1 | FileCheck %s --check-prefix=WIN-NAME -DNAME=last.obj
+// WIN-NAME: -dumpdir [[NAME]].spirv64.
// Libraries are found through /LIBPATH: and LIB, and /WHOLEARCHIVE: and
// /WHOLEARCHIVE extract their device code.
>From ddc28b966aaf40cdfe6d7fd625d82820761ef136 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 12 Nov 2026 13:00:21 -0800
Subject: [PATCH 33/33] [Clang][SYCL] Fix the clang-cl test on macOS
clang-cl reads a path under /Users as the /U option, so on macOS the
input was dropped and no link step was run. Give the input with /Tp, as
other clang-cl tests do.
---
clang/test/Driver/sycl-link.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/test/Driver/sycl-link.cpp b/clang/test/Driver/sycl-link.cpp
index c7f4fbdc28a76..0d3718f2f56fa 100644
--- a/clang/test/Driver/sycl-link.cpp
+++ b/clang/test/Driver/sycl-link.cpp
@@ -52,11 +52,13 @@
// RUN: | FileCheck %s --check-prefix=WINDOWS-LIBPATH
// WINDOWS-LIBPATH: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o" "dev.obj"
// WINDOWS-LIBPATH-SAME: "-libpath:/foo" "{{[^"]*}}a.o" "/wholearchive:bar.lib"{{$}}
+// The input is given with /Tp, because clang-cl would read a path under /Users
+// (common on macOS) as the /U option.
// RUN: %clang_cl -### --target=x86_64-pc-windows-msvc -fsycl -fsycl-link \
-// RUN: %t.a.o /link /wholearchive:bar.lib /libpath:dir /out:dev.obj 2>&1 \
+// RUN: /Tp%s /link /wholearchive:bar.lib /libpath:dir /out:dev.obj 2>&1 \
// RUN: | FileCheck %s --check-prefix=CL-LINK
// CL-LINK: clang-linker-wrapper"{{.*}} "--sycl-device-link" "-o"
-// CL-LINK-SAME: "{{[^"]*}}a.o" "/wholearchive:bar.lib" "/libpath:dir" "/out:dev.obj"{{$}}
+// CL-LINK-SAME: "{{[^"]*}}.obj" "/wholearchive:bar.lib" "/libpath:dir" "/out:dev.obj"{{$}}
/// Without -fsycl-link, the host link is run as usual.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl %t.a.o 2>&1 \
More information about the cfe-commits
mailing list