https://github.com/srividya-sundaram updated 
https://github.com/llvm/llvm-project/pull/242098

>From 46f3189c37c80a3d6074c5d767d88c491ee5bba1 Mon Sep 17 00:00:00 2001
From: srividya sundaram <[email protected]>
Date: Thu, 12 Nov 2026 15:21:29 -0800
Subject: [PATCH 1/5] [clang-linker-wrapper][SYCL] Support relocatable linking

With -r, clang-linker-wrapper links and registers the device code, merges
the inputs into one relocatable object, and removes the .llvm.offloading
section so that a later link does not link the device code again. For
SYCL this failed: the wrapped device image was itself put in
.llvm.offloading, which the registration code refers to, so the section
could not be removed.

With -r, mark the SYCL device image as finalized, which puts it in
.sycl_fatbin, the section the SYCL no-RDC compile path already uses.
OpenMP uses .llvm.offloading.relocatable instead, but a later link still
extracts and links that section, and the SYCL image is already linked.
llvm-offload-wrapper --relocatable does the same for SYCL, so that the
--wrapper-verbose --save-temps path gives the same result.
---
 clang/docs/SYCLSupport.md                        | 16 ++++++++++++++++
 .../clang-linker-wrapper/linker-wrapper-image.c  |  9 ++++++---
 .../linker-wrapper-verbose.c                     |  8 ++++++++
 .../clang-linker-wrapper/ClangLinkerWrapper.cpp  |  9 ++++++---
 .../llvm-offload-wrapper/offload-wrapper.ll      |  5 +++++
 .../llvm-offload-wrapper.cpp                     |  9 +++++++--
 6 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index 9eabfd57e55f1..216ba611c8e8b 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -106,3 +106,19 @@ space attributes for pointers:
 ```C++
 //TODO: add support for __attribute__((opencl_global_host)) and 
__attribute__((opencl_global_device)).
 ```
+
+## Relocatable linking
+
+`-r` links the SYCL device code of its inputs and registers it with the SYCL
+runtime, then merges the inputs into one relocatable object. The object can be
+linked later by any host linker, or put into a static library.
+
+```console
+clang++ -fsycl -c a.cpp b.cpp
+clang++ -fsycl -r a.o b.o -o ab.o
+clang++ -c main.cpp
+clang++ main.o ab.o -L<libsycl dir> -lLLVMSYCL -o app
+```
+
+Without `-fsycl`, use `--offload-link -r`. Relocatable linking is not
+supported for COFF targets. See {doc}`OffloadingDesign` for details.
diff --git 
a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c 
b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
index bb72fefd7bf12..57070dd2982b0 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
@@ -319,11 +319,14 @@
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o \
 // RUN:   -fembed-offload-object=%t.out
 // RUN: clang-linker-wrapper --print-wrapped-module --dry-run 
--host-triple=x86_64-unknown-linux-gnu \
-// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefixes=SYCL
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefixes=SYCL,SYCL-NOREL
 // RUN: clang-linker-wrapper --print-wrapped-module --dry-run 
--host-triple=x86_64-unknown-linux-gnu -r \
-// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefixes=SYCL
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefixes=SYCL,SYCL-REL
 
-//      SYCL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".llvm.offloading"
+// With -r the device image is already linked, so it is not put in
+// .llvm.offloading, where a later link would find it and link it again.
+// SYCL-NOREL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".llvm.offloading"
+//   SYCL-REL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".sycl_fatbin"
 
 //      SYCL: define internal void @sycl.descriptor_reg() section 
".text.startup" {
 // SYCL-NEXT: entry:
diff --git 
a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c 
b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
index 40349a291f616..a2f19d998e49b 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
@@ -103,6 +103,14 @@
 // SYCL: llvm-offload-wrapper{{.*}} --kind=sycl 
--triple=x86_64-unknown-linux-gnu -o [[BC:.*]].bc {{.*}}.img
 // SYCL: clang{{.*}} --no-default-config --target=x86_64-unknown-linux-gnu -c 
-fPIC -o {{.*}}.sycl.image.wrapper{{.*}}.o [[BC]].bc
 
+//
+// The '--relocatable' flag is forwarded to the wrapper tool for SYCL.
+//
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu 
--wrapper-verbose --save-temps --dry-run \
+// RUN:   --linker-path=/usr/bin/ld -r %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=RELOCATABLE-SYCL
+
+// RELOCATABLE-SYCL: llvm-offload-wrapper{{.*}} --kind=sycl 
--triple=x86_64-unknown-linux-gnu -o {{.*}}.bc --relocatable {{.*}}.img
+
 //
 // Images pulled from a static archive are extracted from the archive path and
 // singled out by their member name so the replayed command is reproducible.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 035cb75999144..ee2a79cdd8a8b 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -743,7 +743,7 @@ 
wrapDeviceImagesVerbose(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
       "-o",
       *BitcodeOrErr,
   };
-  if ((Kind == OFK_OpenMP || Kind == OFK_OpenACC) &&
+  if ((Kind == OFK_OpenMP || Kind == OFK_OpenACC || Kind == OFK_SYCL) &&
       Args.hasArg(OPT_relocatable))
     WrapperArgs.push_back("--relocatable");
   for (const auto &Buffer : Buffers)
@@ -829,8 +829,11 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> 
Buffers,
   case OFK_SYCL: {
     // TODO: fill these options once the Driver supports them.
     offloading::SYCLJITOptions Options;
-    if (Error Err =
-            offloading::wrapSYCLBinaries(M, BuffersToWrap.front(), Options))
+    // With -r the device image is already linked. Mark it as finalized, so 
that
+    // a later link through clang-linker-wrapper does not link it again.
+    if (Error Err = offloading::wrapSYCLBinaries(
+            M, BuffersToWrap.front(), Options,
+            /*IsFinalizedImage=*/Args.hasArg(OPT_relocatable)))
       return std::move(Err);
     break;
   }
diff --git a/llvm/test/tools/llvm-offload-wrapper/offload-wrapper.ll 
b/llvm/test/tools/llvm-offload-wrapper/offload-wrapper.ll
index c8542fcbf9106..05c1fa38f2050 100644
--- a/llvm/test/tools/llvm-offload-wrapper/offload-wrapper.ll
+++ b/llvm/test/tools/llvm-offload-wrapper/offload-wrapper.ll
@@ -145,6 +145,11 @@
 
 ; OMP-RELOCATABLE: @.llvm_offloading.device_image = internal unnamed_addr 
constant [{{[0-9]+}} x i8] c"{{.*}}", section ".llvm.offloading.relocatable", 
align 8
 
+; RUN: llvm-offload-wrapper --triple=x86_64-unknown-linux-gnu -kind=sycl 
--relocatable %s -o %t.bc
+; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefix=SYCL-RELOCATABLE
+
+; SYCL-RELOCATABLE: @.sycl_offloading.binary = internal unnamed_addr constant 
[{{[0-9]+}} x i8] c"{{.*}}", section ".sycl_fatbin"
+
 ; RUN: llvm-offload-wrapper --triple=x86_64-unknown-linux-gnu -kind=openacc %s 
-o %t.bc
 ; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefix=ACC
 
diff --git a/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp 
b/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp
index adc68dc01fa85..0462a46983786 100644
--- a/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp
+++ b/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp
@@ -42,7 +42,8 @@ static cl::opt<object::OffloadKind> Kind(
 
 static cl::opt<bool> Relocatable(
     "relocatable",
-    cl::desc("Wrap for a relocatable offloading application (OpenMP only)"),
+    cl::desc("Wrap for a relocatable offloading application (OpenMP, OpenACC "
+             "and SYCL)"),
     cl::cat(OffloadWrapeprCategory));
 
 static cl::opt<std::string> OutputFile("o", cl::desc("Write output to 
<file>."),
@@ -94,7 +95,11 @@ static Error wrapImages(ArrayRef<ArrayRef<char>> 
BuffersToWrap) {
       return Err;
     break;
   case llvm::object::OFK_SYCL:
-    if (Error Err = offloading::wrapSYCLBinaries(M, BuffersToWrap.front()))
+    // A relocatable link has already linked the device image. Mark it as
+    // finalized, so that a later link does not link it again.
+    if (Error Err = offloading::wrapSYCLBinaries(
+            M, BuffersToWrap.front(), offloading::SYCLJITOptions(),
+            /*IsFinalizedImage=*/Relocatable))
       return Err;
     break;
   default:

>From fb3d51502699501ebc732c0fe7554e888151307d Mon Sep 17 00:00:00 2001
From: srividya sundaram <[email protected]>
Date: Thu, 12 Nov 2026 15:33:25 -0800
Subject: [PATCH 2/5] [clang-linker-wrapper][SYCL] Clarify comments and docs

---
 clang/docs/SYCLSupport.md                                 | 8 ++++----
 .../clang-linker-wrapper/linker-wrapper-image.c           | 5 +++--
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp   | 4 +++-
 llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp  | 4 ++--
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index 216ba611c8e8b..e8b8b34f677f0 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -109,9 +109,9 @@ space attributes for pointers:
 
 ## Relocatable linking
 
-`-r` links the SYCL device code of its inputs and registers it with the SYCL
-runtime, then merges the inputs into one relocatable object. The object can be
-linked later by any host linker, or put into a static library.
+`-r` links the SYCL device code of its inputs, adds the code that registers it
+with the SYCL runtime, and merges the inputs into one relocatable object. The
+object can be linked later by any host linker, or put into a static library.
 
 ```console
 clang++ -fsycl -c a.cpp b.cpp
@@ -121,4 +121,4 @@ clang++ main.o ab.o -L<libsycl dir> -lLLVMSYCL -o app
 ```
 
 Without `-fsycl`, use `--offload-link -r`. Relocatable linking is not
-supported for COFF targets. See {doc}`OffloadingDesign` for details.
+supported for Windows (COFF) targets. See {doc}`OffloadingDesign` for details.
diff --git 
a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c 
b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
index 57070dd2982b0..4230b1c5a0368 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
@@ -323,8 +323,9 @@
 // RUN: clang-linker-wrapper --print-wrapped-module --dry-run 
--host-triple=x86_64-unknown-linux-gnu -r \
 // RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefixes=SYCL,SYCL-REL
 
-// With -r the device image is already linked, so it is not put in
-// .llvm.offloading, where a later link would find it and link it again.
+// With -r the device image goes in .sycl_fatbin instead of .llvm.offloading:
+// -r removes .llvm.offloading from the output, and the image is already
+// linked, so a later link must not link it again.
 // SYCL-NOREL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".llvm.offloading"
 //   SYCL-REL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".sycl_fatbin"
 
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index ee2a79cdd8a8b..2332d89796b4b 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -829,7 +829,9 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> 
Buffers,
   case OFK_SYCL: {
     // TODO: fill these options once the Driver supports them.
     offloading::SYCLJITOptions Options;
-    // With -r the device image is already linked. Mark it as finalized, so 
that
+    // With -r, .llvm.offloading is removed from the output after the link,
+    // and the device image is already linked. A finalized image goes in
+    // .sycl_fatbin instead, so the registration code can still refer to it and
     // a later link through clang-linker-wrapper does not link it again.
     if (Error Err = offloading::wrapSYCLBinaries(
             M, BuffersToWrap.front(), Options,
diff --git a/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp 
b/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp
index 0462a46983786..e346984b3e386 100644
--- a/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp
+++ b/llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp
@@ -95,8 +95,8 @@ static Error wrapImages(ArrayRef<ArrayRef<char>> 
BuffersToWrap) {
       return Err;
     break;
   case llvm::object::OFK_SYCL:
-    // A relocatable link has already linked the device image. Mark it as
-    // finalized, so that a later link does not link it again.
+    // As clang-linker-wrapper does for -r, put the already linked device image
+    // in .sycl_fatbin instead of .llvm.offloading.
     if (Error Err = offloading::wrapSYCLBinaries(
             M, BuffersToWrap.front(), offloading::SYCLJITOptions(),
             /*IsFinalizedImage=*/Relocatable))

>From cb96b3290fed26b598b852ccb496207994602eaa Mon Sep 17 00:00:00 2001
From: srividya sundaram <[email protected]>
Date: Fri, 13 Nov 2026 12:20:15 -0800
Subject: [PATCH 3/5] [clang-linker-wrapper][SYCL] Simplify the -r comment

---
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index ba1db324b2f23..713e8fd5f6858 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -826,10 +826,8 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> 
Buffers,
   case OFK_SYCL: {
     // TODO: fill these options once the Driver supports them.
     offloading::SYCLJITOptions Options;
-    // With -r, .llvm.offloading is removed from the output after the link,
-    // and the device image is already linked. A finalized image goes in
-    // .sycl_fatbin instead, so the registration code can still refer to it and
-    // a later link through clang-linker-wrapper does not link it again.
+    // -r removes .llvm.offloading from the output, so put the device image in
+    // .sycl_fatbin instead.
     if (Error Err = offloading::wrapSYCLBinaries(
             M, BuffersToWrap.front(), Options,
             /*IsFinalizedImage=*/Args.hasArg(OPT_relocatable)))

>From 0c06625e92eac501f84ee71032f29a0e7f897299 Mon Sep 17 00:00:00 2001
From: srividya sundaram <[email protected]>
Date: Fri, 13 Nov 2026 12:39:58 -0800
Subject: [PATCH 4/5] [clang-linker-wrapper][SYCL] Name the -o file in the -r
 comment

---
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 713e8fd5f6858..2dabcdec0e0a7 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -826,7 +826,7 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> 
Buffers,
   case OFK_SYCL: {
     // TODO: fill these options once the Driver supports them.
     offloading::SYCLJITOptions Options;
-    // -r removes .llvm.offloading from the output, so put the device image in
+    // -r removes .llvm.offloading from the -o file, so put the device image in
     // .sycl_fatbin instead.
     if (Error Err = offloading::wrapSYCLBinaries(
             M, BuffersToWrap.front(), Options,

>From 768454dadbfec53cd6827c41bf349cdad5d0feaa Mon Sep 17 00:00:00 2001
From: srividya sundaram <[email protected]>
Date: Fri, 13 Nov 2026 12:47:39 -0800
Subject: [PATCH 5/5] [clang-linker-wrapper][SYCL] Explain why the image moves
 to .sycl_fatbin

---
 clang/docs/SYCLSupport.md                                  | 7 ++++---
 .../clang-linker-wrapper/linker-wrapper-image.c            | 5 ++---
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp    | 4 ++--
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index e8b8b34f677f0..3adf827d5a576 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -109,9 +109,10 @@ space attributes for pointers:
 
 ## Relocatable linking
 
-`-r` links the SYCL device code of its inputs, adds the code that registers it
-with the SYCL runtime, and merges the inputs into one relocatable object. The
-object can be linked later by any host linker, or put into a static library.
+With `-r`, clang links the SYCL device code of its inputs and merges the inputs
+into one object file. That object file also contains the code that registers 
the
+device code with the SYCL runtime, so any host linker can link it later, also
+from a static library.
 
 ```console
 clang++ -fsycl -c a.cpp b.cpp
diff --git 
a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c 
b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
index 4230b1c5a0368..7d445a359389d 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-image.c
@@ -323,9 +323,8 @@
 // RUN: clang-linker-wrapper --print-wrapped-module --dry-run 
--host-triple=x86_64-unknown-linux-gnu -r \
 // RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefixes=SYCL,SYCL-REL
 
-// With -r the device image goes in .sycl_fatbin instead of .llvm.offloading:
-// -r removes .llvm.offloading from the output, and the image is already
-// linked, so a later link must not link it again.
+// -r removes .llvm.offloading from the -o file, but the registration code
+// refers to the device image, so the image goes in .sycl_fatbin instead.
 // SYCL-NOREL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".llvm.offloading"
 //   SYCL-REL: @.sycl_offloading.binary = internal unnamed_addr constant [0 x 
i8] zeroinitializer, section ".sycl_fatbin"
 
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 2dabcdec0e0a7..0867b112016a0 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -826,8 +826,8 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> 
Buffers,
   case OFK_SYCL: {
     // TODO: fill these options once the Driver supports them.
     offloading::SYCLJITOptions Options;
-    // -r removes .llvm.offloading from the -o file, so put the device image in
-    // .sycl_fatbin instead.
+    // -r removes .llvm.offloading from the -o file, but the registration code
+    // refers to the device image, so put the image in .sycl_fatbin instead.
     if (Error Err = offloading::wrapSYCLBinaries(
             M, BuffersToWrap.front(), Options,
             /*IsFinalizedImage=*/Args.hasArg(OPT_relocatable)))

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to