[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-18 Thread Nick Sarnie via cfe-commits

https://github.com/sarnex approved this pull request.

lgtm just a nit!

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-18 Thread Nick Sarnie via cfe-commits

https://github.com/sarnex edited 
https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-18 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa converted_to_draft 
https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-18 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/135683

>From a855d6dcff02209767582676012ef8a13897241f Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH 1/6] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 58 -
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 5 files changed, 134 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 082355e6c716f..51bb274770a96 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Nick Sarnie via cfe-commits


@@ -792,6 +805,7 @@ bundleLinkedOutput(ArrayRef Images, const 
ArgList &Args,
   llvm::TimeTraceScope TimeScope("Bundle linked output");
   switch (Kind) {
   case OFK_OpenMP:
+  case OFK_SYCL:
 return bundleOpenMP(Images);

sarnex wrote:

should we change this function name since it's not OpenMP only anymore?

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Kazu Hirata via cfe-commits

kazutakahirata wrote:

@sarnex I've landed 3133c956c57c5b952d3289323891dcdc252ea333 to fix a warning 
from this PR.  Thanks!

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

> @sarnex I've landed 
> [3133c95](https://github.com/llvm/llvm-project/commit/3133c956c57c5b952d3289323891dcdc252ea333)
>  to fix a warning from this PR. Thanks!

Thanks so much.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Nick Sarnie via cfe-commits

sarnex wrote:

Thanks!

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Nick Sarnie via cfe-commits

https://github.com/sarnex closed 
https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Arvind Sudarsanam via cfe-commits


@@ -792,6 +805,7 @@ bundleLinkedOutput(ArrayRef Images, const 
ArgList &Args,
   llvm::TimeTraceScope TimeScope("Bundle linked output");
   switch (Kind) {
   case OFK_OpenMP:
+  case OFK_SYCL:
 return bundleOpenMP(Images);

asudarsa wrote:

Yes. Please. Thanks


https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Nick Sarnie via cfe-commits


@@ -792,6 +805,7 @@ bundleLinkedOutput(ArrayRef Images, const 
ArgList &Args,
   llvm::TimeTraceScope TimeScope("Bundle linked output");
   switch (Kind) {
   case OFK_OpenMP:
+  case OFK_SYCL:
 return bundleOpenMP(Images);

sarnex wrote:

fine with me, is this ready for merge?

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Arvind Sudarsanam via cfe-commits


@@ -792,6 +805,7 @@ bundleLinkedOutput(ArrayRef Images, const 
ArgList &Args,
   llvm::TimeTraceScope TimeScope("Bundle linked output");
   switch (Kind) {
   case OFK_OpenMP:
+  case OFK_SYCL:
 return bundleOpenMP(Images);

asudarsa wrote:

Bundling logic is very simple. it just bufferizes the images and stores them 
back in a sequence. This can easily work for SYCL. Wrapping logic is the one 
which is more complicated.

I would prefer to keep the name as is and revisit this once offload wrapping 
logic has been added for SYCL. We may or may not keep the bundling logic the 
same. It is expected to work as is.

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Nick Sarnie via cfe-commits


@@ -792,6 +805,7 @@ bundleLinkedOutput(ArrayRef Images, const 
ArgList &Args,
   llvm::TimeTraceScope TimeScope("Bundle linked output");
   switch (Kind) {
   case OFK_OpenMP:
+  case OFK_SYCL:
 return bundleOpenMP(Images);

sarnex wrote:

yeah if SYCL using this bundling is only temporary it's fine as is imo

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-17 Thread Joseph Huber via cfe-commits


@@ -792,6 +805,7 @@ bundleLinkedOutput(ArrayRef Images, const 
ArgList &Args,
   llvm::TimeTraceScope TimeScope("Bundle linked output");
   switch (Kind) {
   case OFK_OpenMP:
+  case OFK_SYCL:
 return bundleOpenMP(Images);

jhuber6 wrote:

Could call it `offload/` but it emits the same calls that go to `libomptarget` 
when my assumption is that SYCL will want to use the plugin API @callumfare is 
working on.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/135683

>From 597fda606efbcf10304fed4adc5dd85da34b3de3 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH 1/5] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 58 -
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 5 files changed, 134 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 082355e6c716f..51bb274770a96 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Joseph Huber via cfe-commits

https://github.com/jhuber6 approved this pull request.


https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

I added a constraint that Images of SYCL offloading kind cannot be linked with 
images of other kind. This is a valid constraint in the current state of SYCL 
upstreaming effort. We will aim to remove this constraint soon.

This makes the clang-linker-wrapper code changes manageable.

Hope this is an agreeable resolution.

Thanks for driving me towards this.


https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa ready_for_review 
https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/135683

>From 597fda606efbcf10304fed4adc5dd85da34b3de3 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH 1/6] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 58 -
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 5 files changed, 134 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 082355e6c716f..51bb274770a96 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/135683

>From 597fda606efbcf10304fed4adc5dd85da34b3de3 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH 1/4] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 58 -
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 5 files changed, 134 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 082355e6c716f..51bb274770a96 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Marking this as a draft to attempt refactoring of clang-linker-wrapper changes 
based on offline discussions with @jhuber6 

Thanks much

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Joseph Huber via cfe-commits


@@ -168,10 +170,10 @@ Expected> getInput(const ArgList 
&Args) {
 /// are LLVM IR bitcode files.
 // TODO: Support SPIR-V IR files.
 Expected> getBitcodeModule(StringRef File,
-   LLVMContext &C) {
+   LLVMContext &Ctx) {

jhuber6 wrote:

Unrelated change?

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits


@@ -168,10 +170,10 @@ Expected> getInput(const ArgList 
&Args) {
 /// are LLVM IR bitcode files.
 // TODO: Support SPIR-V IR files.
 Expected> getBitcodeModule(StringRef File,
-   LLVMContext &C) {
+   LLVMContext &Ctx) {

asudarsa wrote:

Yes. I was changing C to Ctx in a 'related' code change and I decided to do it 
everywhere. Just for better readability. Hope its' ok.

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Joseph Huber via cfe-commits


@@ -937,13 +961,47 @@ Expected> linkAndWrapDeviceFiles(
   InputFiles.emplace_back(*FileNameOrErr);
 }
 
+if (HasSYCLOffloadKind) {
+  // Link the remaining device files using the device linker.
+  auto OutputOrErr = linkDevice(InputFiles, LinkerArgs, 
HasSYCLOffloadKind);
+  if (!OutputOrErr)
+return OutputOrErr.takeError();
+  // Output is a packaged object of device images. Unpackage the images and
+  // copy them to Images[Kind]
+  ErrorOr> BufferOrErr =
+  MemoryBuffer::getFileOrSTDIN(*OutputOrErr);
+  if (std::error_code EC = BufferOrErr.getError())
+return createFileError(*OutputOrErr, EC);
+
+  MemoryBufferRef Buffer = **BufferOrErr;
+  SmallVector Binaries;
+  if (Error Err = extractOffloadBinaries(Buffer, Binaries))
+return std::move(Err);
+  for (auto &OffloadFile : Binaries) {

jhuber6 wrote:

We really can't make this an if-else thing? It would be great to have it 
common, and now that we can just check for SYCL it should be easy to verify. 
Also this should probably make it clear that mixed SYCL / HIP or anything else 
does not work.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Joseph Huber via cfe-commits


@@ -988,6 +1038,11 @@ Expected> linkAndWrapDeviceFiles(
  A.StringData["arch"] > B.StringData["arch"] ||
  A.TheOffloadKind < B.TheOffloadKind;
 });
+if (Kind == OFK_SYCL) {
+  // TODO: Update once SYCL offload wrapping logic is available.

jhuber6 wrote:

I really need to make the offload wrapping a standalone tool so users can do it 
themselves. Normally I'd suggest landing that first, but since the tests for it 
rely on the linker wrapper, kind of hard. I guess it's fine to just let this 
error for now.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

https://github.com/llvm/llvm-project/pull/135809 has been merged. 
I have refactored this change on top of that. 
Kindly take a look whenever convenient.

Sincerely

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/135683

>From 597fda606efbcf10304fed4adc5dd85da34b3de3 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH 1/3] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 58 -
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 5 files changed, 134 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 082355e6c716f..51bb274770a96 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-15 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

Thanks so much for feedback so far. I have added a separate PR 
(https://github.com/llvm/llvm-project/pull/135809) for improving OffloadKind 
implementation. Kindly take a look.

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/135683

>From 226d96d4e66e0a1e9d4f42df413d11a6ece4fec9 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH 1/2] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 71 +++-
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/include/llvm/Object/OffloadBinary.h  |  1 +
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 6 files changed, 148 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 52d922abbcaec..7c09ce8c9e59d 100644
-

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-15 Thread Joseph Huber via cfe-commits


@@ -35,6 +35,7 @@ enum OffloadKind : uint16_t {
   OFK_OpenMP,
   OFK_Cuda,
   OFK_HIP,
+  OFK_SYCL,

jhuber6 wrote:

I think we should assign specific values for these and make them powers of two 
apart so we can use them like a bitfield. Clang does that already.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits


@@ -35,6 +35,7 @@ enum OffloadKind : uint16_t {
   OFK_OpenMP,
   OFK_Cuda,
   OFK_HIP,
+  OFK_SYCL,

asudarsa wrote:

As proposed by @jhuber6 earlier, I will submit a separate PR to make changes to 
offload kind representation.
And then update this PR after that gets merged.

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits


@@ -464,7 +464,8 @@ fatbinary(ArrayRef> 
InputFiles,
 } // namespace amdgcn
 
 namespace generic {
-Expected clang(ArrayRef InputFiles, const ArgList &Args) 
{
+Expected clang(ArrayRef InputFiles, const ArgList &Args,
+  bool HasSYCLOffloadKind = false) {

asudarsa wrote:

Ah..that's a great idea. let me do that. 

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits


@@ -35,6 +35,7 @@ enum OffloadKind : uint16_t {
   OFK_OpenMP,
   OFK_Cuda,
   OFK_HIP,
+  OFK_SYCL,

asudarsa wrote:

As mentioned earlier 
(https://github.com/llvm/llvm-project/pull/135683#discussion_r2043140354), it 
might be better to address this change in a subsequent PR. Hope that's 
agreeable.

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits


@@ -937,13 +961,47 @@ Expected> linkAndWrapDeviceFiles(
   InputFiles.emplace_back(*FileNameOrErr);
 }
 
+if (HasSYCLOffloadKind) {
+  // Link the remaining device files using the device linker.
+  auto OutputOrErr = linkDevice(InputFiles, LinkerArgs, 
HasSYCLOffloadKind);
+  if (!OutputOrErr)
+return OutputOrErr.takeError();
+  // Output is a packaged object of device images. Unpackage the images and
+  // copy them to Images[Kind]
+  ErrorOr> BufferOrErr =
+  MemoryBuffer::getFileOrSTDIN(*OutputOrErr);
+  if (std::error_code EC = BufferOrErr.getError())
+return createFileError(*OutputOrErr, EC);
+
+  MemoryBufferRef Buffer = **BufferOrErr;
+  SmallVector Binaries;
+  if (Error Err = extractOffloadBinaries(Buffer, Binaries))
+return std::move(Err);
+  for (auto &OffloadFile : Binaries) {

asudarsa wrote:

There are subtle changes here. In the other use site, we look at input 
filenames provided to clang-linker-wrapper and collect two lists of 
OffloadFiles - object files and archived files. In this new use site, we look 
at the output file returned by clang-sycl-linker and try to collect a list of 
OffloadingImages corresponding to the list of object files generated in 
clang-sycl-linker.
I tried merging the two use sites. it seems to make the code more complicated 
than necessary.
Please let me know if I am missing something here.

Thanks 

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Joseph Huber via cfe-commits


@@ -464,7 +464,8 @@ fatbinary(ArrayRef> 
InputFiles,
 } // namespace amdgcn
 
 namespace generic {
-Expected clang(ArrayRef InputFiles, const ArgList &Args) 
{
+Expected clang(ArrayRef InputFiles, const ArgList &Args,
+  bool HasSYCLOffloadKind = false) {

jhuber6 wrote:

Then precommit it.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits


@@ -464,7 +464,8 @@ fatbinary(ArrayRef> 
InputFiles,
 } // namespace amdgcn
 
 namespace generic {
-Expected clang(ArrayRef InputFiles, const ArgList &Args) 
{
+Expected clang(ArrayRef InputFiles, const ArgList &Args,
+  bool HasSYCLOffloadKind = false) {

asudarsa wrote:

I agree this change makes things better. Making OffloadKind enums to have 
power-of-two values and then using a bitfield here helps to avoid these flags 
and also helps to make the ActiveOffloadKinds support better. However, I would 
prefer to make these changes in a subsequent PR so that we can have a set of 
simpler commits. Please let me know if this is agreeable or you would prefer to 
make the change in this PR.

Thanks

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Joseph Huber via cfe-commits


@@ -464,7 +464,8 @@ fatbinary(ArrayRef> 
InputFiles,
 } // namespace amdgcn
 
 namespace generic {
-Expected clang(ArrayRef InputFiles, const ArgList &Args) 
{
+Expected clang(ArrayRef InputFiles, const ArgList &Args,
+  bool HasSYCLOffloadKind = false) {

jhuber6 wrote:

I think we should make this a bitfield and check if it contains SYCL.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Joseph Huber via cfe-commits


@@ -937,13 +961,47 @@ Expected> linkAndWrapDeviceFiles(
   InputFiles.emplace_back(*FileNameOrErr);
 }
 
+if (HasSYCLOffloadKind) {
+  // Link the remaining device files using the device linker.
+  auto OutputOrErr = linkDevice(InputFiles, LinkerArgs, 
HasSYCLOffloadKind);
+  if (!OutputOrErr)
+return OutputOrErr.takeError();
+  // Output is a packaged object of device images. Unpackage the images and
+  // copy them to Images[Kind]
+  ErrorOr> BufferOrErr =
+  MemoryBuffer::getFileOrSTDIN(*OutputOrErr);
+  if (std::error_code EC = BufferOrErr.getError())
+return createFileError(*OutputOrErr, EC);
+
+  MemoryBufferRef Buffer = **BufferOrErr;
+  SmallVector Binaries;
+  if (Error Err = extractOffloadBinaries(Buffer, Binaries))
+return std::move(Err);
+  for (auto &OffloadFile : Binaries) {

jhuber6 wrote:

Why can't this be made common with the other use? It looks directly copied.

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

There are a couple of TODO comments in this PR:
1. Add sycl post link functionality (including device code splitting) - This PR 
is under review here - https://github.com/llvm/llvm-project/pull/131347
2. Add SYCL offload wrapping logic - We are working on submitting this PR. 
There is interdependency between current PR and the offload wrapping effort. 
One option is to merge the two efforts. However, I am hoping to reduce the 
complexity of each PR. Hope, that is agreeable.

I expect these TODOs to be wrapped up very soon.

Thanks 

https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa created 
https://github.com/llvm/llvm-project/pull/135683

Device code linking happens inside clang-linker-wrapper. In the current 
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i], a. Gather all the offload kinds found 
inside those inputs in ActiveOffloadKinds b. Link all images inside 
Inputs[triple_i] by calling clang --target=triple_i  c. Create a copy of 
that linked image for each offload kind and add it to Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code 
splitting inside the 'clang --target=triple_i ' call and the output is now 
a 'packaged' file containing multiple device images. This deviation requires us 
to capture the OffloadKind during the linking stage and pass it along to the 
linking function (clang), so that clang can be called with a unique option 
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood 
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and 
get it working. We will eventually merge our approach with the community flow.

>From 226d96d4e66e0a1e9d4f42df413d11a6ece4fec9 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 14 Apr 2025 14:16:05 -0700
Subject: [PATCH] [SYCL] Add clang-linker-wrapper changes to call
 clang-sycl-linker for SYCL offloads

Device code linking happens inside clang-linker-wrapper. In the current
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices
Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i],
   a. Gather all the offload kinds found inside those inputs in
  ActiveOffloadKinds
   b. Link all images inside Inputs[triple_i] by calling
  clang --target=triple_i 
   c. Create a copy of that linked image for each offload kind and add it to
  Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code
splitting inside the 'clang --target=triple_i ' call and the output is now
a 'packaged' file containing multiple device images. This deviation requires us
to capture the OffloadKind during the linking stage and pass it along to the
linking function (clang), so that clang can be called with a unique option
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and
get it working. We will eventually merge our approach with the community flow.

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangOffloadPackager.rst   |  2 +
 clang/test/Driver/linker-wrapper.c| 10 +++
 .../ClangLinkerWrapper.cpp| 71 +++-
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 84 ++-
 llvm/include/llvm/Object/OffloadBinary.h  |  1 +
 llvm/lib/Object/OffloadBinary.cpp |  3 +
 6 files changed, 148 insertions(+), 23 deletions(-)

diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/135683
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Arvind Sudarsanam (asudarsa)


Changes

Device code linking happens inside clang-linker-wrapper. In the current 
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i], a. Gather all the offload kinds found 
inside those inputs in ActiveOffloadKinds b. Link all images inside 
Inputs[triple_i] by calling clang --target=triple_i  c. Create a copy of 
that linked image for each offload kind and add it to Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code 
splitting inside the 'clang --target=triple_i ' call and the output is now 
a 'packaged' file containing multiple device images. This deviation requires us 
to capture the OffloadKind during the linking stage and pass it along to the 
linking function (clang), so that clang can be called with a unique option 
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood 
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and 
get it working. We will eventually merge our approach with the community flow.

---
Full diff: https://github.com/llvm/llvm-project/pull/135683.diff


6 Files Affected:

- (modified) clang/docs/ClangOffloadPackager.rst (+2) 
- (modified) clang/test/Driver/linker-wrapper.c (+10) 
- (modified) clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp (+67-4) 
- (modified) clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp (+65-19) 
- (modified) llvm/include/llvm/Object/OffloadBinary.h (+1) 
- (modified) llvm/lib/Object/OffloadBinary.cpp (+3) 


``diff
diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 52d922abbcaec..7c09ce8c9e59d 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -464,7 +464,8 @@ fatbinary(ArrayRef> 
InputFiles,
 } // namespace amdgcn
 
 namespace generic 

[clang] [llvm] [SYCL] Add clang-linker-wrapper changes to call clang-sycl-linker for SYCL offloads (PR #135683)

2025-04-14 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-driver

Author: Arvind Sudarsanam (asudarsa)


Changes

Device code linking happens inside clang-linker-wrapper. In the current 
implementation, clang-linker-wrapper does the following:

1. Extracts device code. Input_1, Input_2,.
2. Group device code according to target devices Inputs[triple_1] = 
Inputs[triple_2] = 
3. For each group, i.e. Inputs[triple_i], a. Gather all the offload kinds found 
inside those inputs in ActiveOffloadKinds b. Link all images inside 
Inputs[triple_i] by calling clang --target=triple_i  c. Create a copy of 
that linked image for each offload kind and add it to Output[Kind] list.

In SYCL compilation flow, there is a deviation in Step 3b. We call device code 
splitting inside the 'clang --target=triple_i ' call and the output is now 
a 'packaged' file containing multiple device images. This deviation requires us 
to capture the OffloadKind during the linking stage and pass it along to the 
linking function (clang), so that clang can be called with a unique option 
'--sycl-link' that will help us to call 'clang-sycl-linker' under the hood 
(clang-sycl-linker will do SYCL specific linking).

Our current objective is to implement an end-to-end SYCL offloading flow and 
get it working. We will eventually merge our approach with the community flow.

---
Full diff: https://github.com/llvm/llvm-project/pull/135683.diff


6 Files Affected:

- (modified) clang/docs/ClangOffloadPackager.rst (+2) 
- (modified) clang/test/Driver/linker-wrapper.c (+10) 
- (modified) clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp (+67-4) 
- (modified) clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp (+65-19) 
- (modified) llvm/include/llvm/Object/OffloadBinary.h (+1) 
- (modified) llvm/lib/Object/OffloadBinary.cpp (+3) 


``diff
diff --git a/clang/docs/ClangOffloadPackager.rst 
b/clang/docs/ClangOffloadPackager.rst
index 2b985e260e302..481069b5e4235 100644
--- a/clang/docs/ClangOffloadPackager.rst
+++ b/clang/docs/ClangOffloadPackager.rst
@@ -112,6 +112,8 @@ the following values for the :ref:`offload 
kind` and the
 ++---+---+
 | OFK_HIP| 0x03  | The producer was HIP  |
 ++---+---+
+| OFK_SYCL   | 0x04  | The producer was SYCL |
+++---+---+
 
 The flags are used to signify certain conditions, such as the presence of
 debugging information or whether or not LTO was used. The string entry table is
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 0c77c2b34216a..921c9a11d2d32 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -2,6 +2,7 @@
 // REQUIRES: x86-registered-target
 // REQUIRES: nvptx-registered-target
 // REQUIRES: amdgpu-registered-target
+// REQUIRES: spirv-registered-target
 
 // An externally visible variable so static libraries extract.
 __attribute__((visibility("protected"), used)) int x;
@@ -9,6 +10,7 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
 // RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.nvptx.bc
 // RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+// RUN: %clang -cc1 %s -triple spirv64-unknown-unknown -emit-llvm-bc -o 
%t.spirv.bc
 
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   
--image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -49,6 +51,14 @@ __attribute__((visibility("protected"), used)) int x;
 
 // AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 
-flto {{.*}}-save-temps
 
+// RUN: clang-offload-packager -o %t.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.o 
-fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=SPIRV-LINK
+
+// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown 
{{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
+
 // RUN: clang-offload-packager -o %t.out \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 52d922abbcaec..7c09ce8c9e59d 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -464,7 +464,8 @@ fatbinary(ArrayRef> 
InputFiles,
 } // namespace amdgcn
 
 namespace g