[clang] [llvm] [Clang][SYCL] Compile device code once per IGCA target (PR #239010)

Srividya Sundaram via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 17:25:57 PST 2026


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

>From b001bba68a1b086bbe8ff091bbfec5476e3a2c48 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 4 Nov 2026 11:01:28 -0800
Subject: [PATCH 01/13] [TargetParser] Map an Intel GPU to its IGCA target

Add getIGCATarget, which returns the IGCA target of an Intel GPU from the IGCA columns of IntelGPUTargetParser.def, e.g. igca_30r for both xe-mtl-u and xe-mtl-h. The driver will use it to compile once per IGCA target.
---
 .../llvm/TargetParser/IntelGPUTargetParser.h      |  4 ++++
 llvm/lib/TargetParser/IntelGPUTargetParser.cpp    | 15 +++++++++++++++
 .../TargetParser/IntelGPUTargetParserTest.cpp     | 15 +++++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/llvm/include/llvm/TargetParser/IntelGPUTargetParser.h b/llvm/include/llvm/TargetParser/IntelGPUTargetParser.h
index 3e9f7ef82b41d4..4225ec24455ba2 100644
--- a/llvm/include/llvm/TargetParser/IntelGPUTargetParser.h
+++ b/llvm/include/llvm/TargetParser/IntelGPUTargetParser.h
@@ -176,6 +176,10 @@ LLVM_ABI IGCATarget parseIGCATarget(StringRef MaybeTarget);
 /// \p Target is invalid.
 LLVM_ABI StringRef getIGCATargetName(IGCATarget Target);
 
+/// \return the IGCA target of \p Kind, e.g. igca_20ca for xe-pvc, or an
+/// invalid IGCATarget for GK_NONE.
+LLVM_ABI IGCATarget getIGCATarget(GPUKind Kind);
+
 } // namespace IntelGPU
 } // namespace llvm
 
diff --git a/llvm/lib/TargetParser/IntelGPUTargetParser.cpp b/llvm/lib/TargetParser/IntelGPUTargetParser.cpp
index efc171de4d8db3..8d2206fb4fed10 100644
--- a/llvm/lib/TargetParser/IntelGPUTargetParser.cpp
+++ b/llvm/lib/TargetParser/IntelGPUTargetParser.cpp
@@ -197,3 +197,18 @@ StringRef llvm::IntelGPU::getIGCATargetName(IGCATarget T) {
       return E.Name;
   return "";
 }
+
+IGCATarget llvm::IntelGPU::getIGCATarget(GPUKind Kind) {
+  switch (Kind) {
+  case GK_NONE:
+    return IGCATarget::invalid();
+#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS)    \
+  case GK_##KIND:                                                              \
+    return IGCATarget(IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS);
+#define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS)           \
+  case GK_##KIND:                                                              \
+    return IGCATarget(IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS);
+#include "llvm/TargetParser/IntelGPUTargetParser.def"
+  }
+  llvm_unreachable("invalid Intel GPU kind");
+}
diff --git a/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp b/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp
index 40fef95203c74f..9eba07b1fb6e11 100644
--- a/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp
+++ b/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp
@@ -265,4 +265,19 @@ TEST(IntelGPUTargetParserTest, IGCATargetName) {
       IntelGPU::getIGCATargetName(IGCATarget(10, IGCAFeatureSet::Compute)), "");
 }
 
+TEST(IntelGPUTargetParserTest, KindIGCATargets) {
+  auto TargetName = [](IntelGPU::GPUKind Kind) {
+    return IntelGPU::getIGCATargetName(IntelGPU::getIGCATarget(Kind));
+  };
+  // Devices can share an IGCA target.
+  EXPECT_EQ(TargetName(IntelGPU::GK_XE_MTL_U), "igca_30r");
+  EXPECT_EQ(TargetName(IntelGPU::GK_XE_MTL_H), "igca_30r");
+  EXPECT_EQ(TargetName(IntelGPU::GK_XE_PVC), "igca_20ca");
+  EXPECT_EQ(TargetName(IntelGPU::GK_XE_CRI), "igca_60");
+  // A compatibility name and an alias have IGCA targets too.
+  EXPECT_EQ(TargetName(IntelGPU::GK_XE_DG2), "igca_15ra");
+  EXPECT_EQ(TargetName(IntelGPU::parseArch("xe-arl-u")), "igca_30r");
+  EXPECT_FALSE(IntelGPU::getIGCATarget(IntelGPU::GK_NONE).isValid());
+}
+
 } // namespace

>From ce7d28434a72fae00e4e9642f1845b263d3fd75f Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 4 Nov 2026 12:22:05 -0800
Subject: [PATCH 02/13] [TargetParser] Clarify a test comment

---
 llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp b/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp
index 9eba07b1fb6e11..07523e3849c879 100644
--- a/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp
+++ b/llvm/unittests/TargetParser/IntelGPUTargetParserTest.cpp
@@ -274,7 +274,8 @@ TEST(IntelGPUTargetParserTest, KindIGCATargets) {
   EXPECT_EQ(TargetName(IntelGPU::GK_XE_MTL_H), "igca_30r");
   EXPECT_EQ(TargetName(IntelGPU::GK_XE_PVC), "igca_20ca");
   EXPECT_EQ(TargetName(IntelGPU::GK_XE_CRI), "igca_60");
-  // A compatibility name and an alias have IGCA targets too.
+  // A compatibility name has an IGCA target too, and an alias has its
+  // device's.
   EXPECT_EQ(TargetName(IntelGPU::GK_XE_DG2), "igca_15ra");
   EXPECT_EQ(TargetName(IntelGPU::parseArch("xe-arl-u")), "igca_30r");
   EXPECT_FALSE(IntelGPU::getIGCATarget(IntelGPU::GK_NONE).isValid());

>From c153df6f4fcb73b3dc3ffbdd427c6686d70b592d Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 4 Nov 2026 15:06:25 -0800
Subject: [PATCH 03/13] [Clang][SYCL] Compile device code once per IGCA target

For SYCL, an Intel GPU in --offload-arch is now compiled for its IGCA target, once for all GPUs that share it, e.g. once for igca_30r for xe-mtl-u and xe-mtl-h. The device image records the GPUs as code=xe-mtl-h,xe-mtl-u. At link time, clang-linker-wrapper links images with the same IGCA target and GPUs together, and clang-sycl-linker compiles them ahead of time with a single ocloc -device xe-mtl-h,xe-mtl-u. An IGCA target named directly lists no GPUs and is compiled just in time.
---
 clang/include/clang/Basic/OffloadArch.h       |  3 +
 clang/include/clang/Driver/Compilation.h      | 19 ++++++
 clang/lib/Basic/OffloadArch.cpp               |  6 ++
 clang/lib/Driver/Compilation.cpp              | 16 +++++
 clang/lib/Driver/Driver.cpp                   | 18 ++++++
 clang/lib/Driver/ToolChains/Clang.cpp         | 16 ++++-
 clang/test/Driver/sycl-igca-link.cpp          | 30 +++++++++
 clang/test/Driver/sycl-intel-gpu-arch.cpp     | 62 +++++++++++++------
 clang/test/Driver/sycl-offload-jit.cpp        |  2 +-
 clang/test/Driver/sycl-system-arch.cpp        |  2 +-
 .../clang-linker-wrapper/linker-wrapper.c     | 31 ++++++++++
 .../OffloadTools/clang-sycl-linker/basic.ll   | 18 ++++++
 .../ClangLinkerWrapper.cpp                    | 44 ++++++++++---
 .../clang-linker-wrapper/LinkerWrapperOpts.td |  3 +
 .../clang-sycl-linker/ClangSYCLLinker.cpp     | 23 ++++---
 clang/tools/clang-sycl-linker/SYCLLinkOpts.td |  5 ++
 clang/unittests/Basic/OffloadArchTest.cpp     |  8 +++
 17 files changed, 264 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/Driver/sycl-igca-link.cpp

diff --git a/clang/include/clang/Basic/OffloadArch.h b/clang/include/clang/Basic/OffloadArch.h
index fbf51ffab67922..84a25e5caf31ad 100644
--- a/clang/include/clang/Basic/OffloadArch.h
+++ b/clang/include/clang/Basic/OffloadArch.h
@@ -110,6 +110,9 @@ class OffloadArch {
   }
   /// Which IGCA target this is; only valid when isIntelIGCA().
   llvm::IntelGPU::IGCATarget igcaTarget() const;
+  /// The IGCA target of this Intel GPU, e.g. igca_30r for xe-mtl-u; only valid
+  /// when isIntelGPU().
+  OffloadArch intelGPUIGCATarget() const;
 
   bool operator==(const OffloadArch &Other) const {
     return V == Other.V && Kind == Other.Kind;
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 825806b6cfe332..59df1af0fc70b3 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -16,6 +16,7 @@
 #include "clang/Driver/Util.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Option/Option.h"
 #include <cassert>
@@ -97,6 +98,13 @@ class Compilation {
   };
   std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
 
+  /// The Intel GPUs that each SYCL device compile for an IGCA target is
+  /// compiled ahead of time for, sorted, e.g. xe-mtl-h and xe-mtl-u for the
+  /// igca_30r compile.
+  std::map<std::pair<const ToolChain *, StringRef>,
+           llvm::SmallVector<StringRef, 2>>
+      SYCLIGCADevices;
+
   /// Temporary files which should be removed on exit.
   llvm::opt::ArgStringList TempFiles;
 
@@ -256,6 +264,17 @@ class Compilation {
   getArgsForToolChain(const ToolChain *TC, BoundArch BA,
                       Action::OffloadKind DeviceOffloadKind);
 
+  /// Record that the SYCL device compile for the IGCA target \p Target on
+  /// \p TC is compiled ahead of time for the Intel GPU \p Device.
+  void addSYCLIGCADevice(const ToolChain *TC, StringRef Target,
+                         StringRef Device);
+
+  /// \return the Intel GPUs that the SYCL device compile for the IGCA target
+  /// \p Target on \p TC is compiled ahead of time for, sorted, or an empty
+  /// list.
+  ArrayRef<StringRef> getSYCLIGCADevices(const ToolChain *TC,
+                                         StringRef Target) const;
+
   /// addTempFile - Add a file to remove on exit, and returns its
   /// argument.
   const char *addTempFile(const char *Name) {
diff --git a/clang/lib/Basic/OffloadArch.cpp b/clang/lib/Basic/OffloadArch.cpp
index cc33b6d67c09ca..96e1ff6887d344 100644
--- a/clang/lib/Basic/OffloadArch.cpp
+++ b/clang/lib/Basic/OffloadArch.cpp
@@ -35,6 +35,12 @@ llvm::IntelGPU::IGCATarget OffloadArch::igcaTarget() const {
   return llvm::IntelGPU::IGCATarget::unpack(Kind);
 }
 
+OffloadArch OffloadArch::intelGPUIGCATarget() const {
+  assert(isIntelGPU() &&
+         "cannot get IGCA target of a non-Intel-GPU OffloadArch");
+  return getIntelIGCA(llvm::IntelGPU::getIGCATarget(intelGPUKind()));
+}
+
 const char *OffloadArchToString(OffloadArch A) {
   switch (A.targetArch()) {
   case OffloadArch::TargetArch::Unused:
diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp
index 9b46b38811cb0b..40aa5a91cfca10 100644
--- a/clang/lib/Driver/Compilation.cpp
+++ b/clang/lib/Driver/Compilation.cpp
@@ -121,6 +121,22 @@ Compilation::getArgsForToolChain(const ToolChain *TC, BoundArch BA,
   return *Entry;
 }
 
+void Compilation::addSYCLIGCADevice(const ToolChain *TC, StringRef Target,
+                                    StringRef Device) {
+  SmallVector<StringRef, 2> &Devices = SYCLIGCADevices[{TC, Target}];
+  auto It = llvm::lower_bound(Devices, Device);
+  if (It == Devices.end() || *It != Device)
+    Devices.insert(It, Device);
+}
+
+ArrayRef<StringRef> Compilation::getSYCLIGCADevices(const ToolChain *TC,
+                                                    StringRef Target) const {
+  auto It = SYCLIGCADevices.find({TC, Target});
+  if (It == SYCLIGCADevices.end())
+    return {};
+  return It->second;
+}
+
 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
   // FIXME: Why are we trying to remove files that we have not created? For
   // example we should only try to remove a temporary assembly file if
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9d95f69f1bbfe1..fdd2dcec9a03f1 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4151,6 +4151,24 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
   Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
   Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
 
+  // For SYCL, compile once per IGCA target rather than once per Intel GPU,
+  // e.g. once for igca_30r for xe-mtl-u and xe-mtl-h, and record the GPUs so
+  // that the compile is also done ahead of time for them.
+  if (Kind == Action::OFK_SYCL) {
+    llvm::DenseSet<StringRef> Targets;
+    for (StringRef Arch : Archs) {
+      OffloadArch ID = StringToOffloadArch(Arch);
+      if (!ID.isIntelGPU()) {
+        Targets.insert(Arch);
+        continue;
+      }
+      StringRef Target = OffloadArchToString(ID.intelGPUIGCATarget());
+      Targets.insert(Target);
+      C.addSYCLIGCADevice(&TC, Target, Arch);
+    }
+    Archs = std::move(Targets);
+  }
+
   SmallVector<StringRef> Sorted(Archs.begin(), Archs.end());
   llvm::sort(Sorted);
 
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index d8819c952e5add..5ea1ca5888219b 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9759,6 +9759,13 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
         "kind=" + Kind.str(),
     };
 
+    // List the Intel GPUs that a SYCL device compile for an IGCA target is
+    // compiled ahead of time for, one "code=" each. llvm-offload-binary joins
+    // repeated keys, so the image gets "code=xe-mtl-h,xe-mtl-u".
+    if (OffloadAction->getOffloadingDeviceKind() == Action::OFK_SYCL)
+      for (StringRef Device : C.getSYCLIGCADevices(TC, Arch.ArchName))
+        Parts.emplace_back("code=" + Device.str());
+
     if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
       for (StringRef Feature : FeatureArgs)
         Parts.emplace_back("feature=" + Feature.str());
@@ -9953,8 +9960,13 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
       for (StringRef Arg : ToolChainArgs.getAllArgValues(OPT_offload_arch_EQ)) {
         std::optional<std::string> TargetID =
             getCanonicalTargetID(TC->getTriple(), Arg);
-        CmdArgs.emplace_back(Args.MakeArgString(
-            "--should-extract=" + (TargetID ? StringRef(*TargetID) : Arg)));
+        StringRef Name = TargetID ? StringRef(*TargetID) : Arg;
+        // A SYCL device image for an Intel GPU is labeled with the GPU's IGCA
+        // target.
+        if (Kind == Action::OFK_SYCL)
+          if (OffloadArch ID = StringToOffloadArch(Arg); ID.isIntelGPU())
+            Name = OffloadArchToString(ID.intelGPUIGCATarget());
+        CmdArgs.emplace_back(Args.MakeArgString("--should-extract=" + Name));
       }
 
       // If this is OpenMP the device linker will need `-lompdevice`.
diff --git a/clang/test/Driver/sycl-igca-link.cpp b/clang/test/Driver/sycl-igca-link.cpp
new file mode 100644
index 00000000000000..d46739f5ae4bf2
--- /dev/null
+++ b/clang/test/Driver/sycl-igca-link.cpp
@@ -0,0 +1,30 @@
+/// Device code for Intel GPUs is compiled once per IGCA target. At link time,
+/// device code for the same IGCA target and devices is linked together, and
+/// compiled ahead of time for those devices.
+
+// REQUIRES: x86-registered-target
+// UNSUPPORTED: system-windows
+
+// RUN: %clang -fsycl --target=x86_64-unknown-linux-gnu \
+// RUN:   --offload-arch=xe-mtl-u,xe-mtl-h -c %s -o %t-uh-a.o
+// RUN: %clang -fsycl --target=x86_64-unknown-linux-gnu \
+// RUN:   --offload-arch=xe-mtl-h,xe-mtl-u -c %s -o %t-uh-b.o
+// RUN: %clang -fsycl --target=x86_64-unknown-linux-gnu \
+// RUN:   --offload-arch=xe-mtl-u -c %s -o %t-u.o
+
+/// xe-mtl-u and xe-mtl-h share igca_30r, so both objects have one device image
+/// for igca_30r with the same devices, and their device code is linked together
+/// for both devices.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t-uh-a.o %t-uh-b.o -o a.out 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SAME-DEVICES
+// SAME-DEVICES:     clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-h -Xlinker --device=xe-mtl-u{{$}}
+// SAME-DEVICES-NOT: --sycl-link
+
+/// An object compiled for xe-mtl-u only has a different list of devices, so its
+/// device code is linked separately.
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t-u.o %t-uh-a.o -o a.out 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=DIFFERENT-DEVICES
+// DIFFERENT-DEVICES-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u{{$}}
+// DIFFERENT-DEVICES-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-h -Xlinker --device=xe-mtl-u{{$}}
diff --git a/clang/test/Driver/sycl-intel-gpu-arch.cpp b/clang/test/Driver/sycl-intel-gpu-arch.cpp
index 686570c91e0ea8..0aa5b0b7bd87a8 100644
--- a/clang/test/Driver/sycl-intel-gpu-arch.cpp
+++ b/clang/test/Driver/sycl-intel-gpu-arch.cpp
@@ -1,21 +1,46 @@
 /// Tests for Intel GPU names in --offload-arch.
 
-/// An Intel GPU name names a device job.
+/// An Intel GPU is compiled for its IGCA target, e.g. xe-bmg-g21 for igca_40r.
 // RUN: %clang -ccc-print-phases --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-targets=spirv64-unknown-unknown --offload-arch=xe-bmg-g21 \
 // RUN:   --offload-arch=xe-dg2 -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-SYCL %s
-// CHK-SYCL-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-bmg-g21)"
-// CHK-SYCL-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-dg2)"
+// CHK-SYCL-DAG: offload, "device-sycl (spirv64-unknown-unknown:igca_40r)"
+// CHK-SYCL-DAG: offload, "device-sycl (spirv64-unknown-unknown:igca_15ra)"
 
-/// A device name, an alias and a numeric name are kept as given.
-// RUN: %clang -ccc-print-phases --target=x86_64-unknown-linux-gnu -fsycl \
+/// A device name, an alias and a numeric name of one device give one device
+/// image for its IGCA target, which lists each name as given, to compile ahead
+/// of time for.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-targets=spirv64-unknown-unknown --offload-arch=xe-mtl-u \
 // RUN:   --offload-arch=xe-arl-u --offload-arch=xe_12.70.0 -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-AS-GIVEN %s
-// CHK-AS-GIVEN-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-mtl-u)"
-// CHK-AS-GIVEN-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-arl-u)"
-// CHK-AS-GIVEN-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe_12.70.0)"
+// CHK-AS-GIVEN: llvm-offload-binary{{.*}} "--image=file={{.*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-arl-u,code=xe-mtl-u,code=xe_12.70.0"
+// CHK-AS-GIVEN-NOT: --image=
+
+/// The devices are sorted, so that objects compiled for the same devices in any
+/// order have the same list and are linked together.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN:   --offload-arch=xe-mtl-h,xe-mtl-u -c %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHK-SORTED %s
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN:   --offload-arch=xe-mtl-u,xe-mtl-h -c %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHK-SORTED %s
+// CHK-SORTED: "--image=file={{.*}},arch=igca_30r,kind=sycl,code=xe-mtl-h,code=xe-mtl-u"
+
+/// An IGCA target given directly lists no devices, so it is compiled just in
+/// time.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN:   --offload-arch=igca_30r -c %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHK-IGCA-ONLY %s
+// CHK-IGCA-ONLY: "--image=file={{.*}},arch=igca_30r,kind=sycl"
+
+/// Static libraries are searched for the IGCA target of an Intel GPU.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN:   --offload-arch=xe-mtl-u,xe-mtl-h %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHK-EXTRACT %s
+// CHK-EXTRACT: clang-linker-wrapper{{.*}} "--should-extract=igca_30r"
+// CHK-EXTRACT-NOT: --should-extract=xe-
 
 /// Without --offload-targets, an Intel device (by name or numeric name), an IGCA
 /// target or "generic" picks SPIR-V of the host's width.
@@ -26,24 +51,21 @@
 // CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:generic)"
 // CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:graniterapids)"
 // CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:igca_20ca)"
-// CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-bmg-g21)"
-// CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-pvc)"
-// CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe_12.60.7)"
+// CHK-NO-TARGETS-DAG: offload, "device-sycl (spirv64-unknown-unknown:igca_40r)"
 // RUN: %clang -ccc-print-phases --target=i386-unknown-linux-gnu -fsycl \
 // RUN:   --offload-arch=xe-pvc --offload-arch=igca_20ca -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-NO-TARGETS-32 %s
-// CHK-NO-TARGETS-32-DAG: offload, "device-sycl (spirv32-unknown-unknown:xe-pvc)"
-// CHK-NO-TARGETS-32-DAG: offload, "device-sycl (spirv32-unknown-unknown:igca_20ca)"
-/// Names are kept as given, so two aliases of one device make two device jobs,
-/// and --no-offload-arch removes a name.
-// RUN: %clang -ccc-print-phases --target=x86_64-unknown-linux-gnu -fsycl \
+// CHK-NO-TARGETS-32: offload, "device-sycl (spirv32-unknown-unknown:igca_20ca)"
+// CHK-NO-TARGETS-32-NOT: device-sycl (spirv
+
+/// Two names of one device give one device image that lists both, and
+/// --no-offload-arch removes a name.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-arch=xe-mtl-u --offload-arch=xe-arl-u --offload-arch=xe-pvc \
 // RUN:   --no-offload-arch=xe-pvc -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-NO-TARGETS-AS-GIVEN %s
-// CHK-NO-TARGETS-AS-GIVEN-NOT: xe-pvc
-// CHK-NO-TARGETS-AS-GIVEN-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-mtl-u)"
-// CHK-NO-TARGETS-AS-GIVEN-DAG: offload, "device-sycl (spirv64-unknown-unknown:xe-arl-u)"
-// CHK-NO-TARGETS-AS-GIVEN-NOT: xe-pvc
+// CHK-NO-TARGETS-AS-GIVEN: "--image=file={{.*}},arch=igca_30r,kind=sycl,code=xe-arl-u,code=xe-mtl-u"
+// CHK-NO-TARGETS-AS-GIVEN-NOT: {{igca_20ca|xe-pvc}}
 
 /// As for CUDA and HIP, -foffload-via-llvm gives the inferred target the LLVM
 /// environment.
diff --git a/clang/test/Driver/sycl-offload-jit.cpp b/clang/test/Driver/sycl-offload-jit.cpp
index 40b6a9ede9ed85..a3908c5df884b9 100644
--- a/clang/test/Driver/sycl-offload-jit.cpp
+++ b/clang/test/Driver/sycl-offload-jit.cpp
@@ -91,7 +91,7 @@
 // RUN:   --offload-targets=spirv64-unknown-unknown --offload-arch=generic --offload-arch=xe-bmg-g21 \
 // RUN:   -c %s 2>&1 | FileCheck -check-prefixes=CHK-PHASES-NORDC-ARCHS %s
 // CHK-PHASES-NORDC-ARCHS: 7: offload, "device-sycl (spirv64-unknown-unknown:generic)" {6}, ir
-// CHK-PHASES-NORDC-ARCHS: 12: offload, "device-sycl (spirv64-unknown-unknown:xe-bmg-g21)" {11}, ir
+// CHK-PHASES-NORDC-ARCHS: 12: offload, "device-sycl (spirv64-unknown-unknown:igca_40r)" {11}, ir
 // CHK-PHASES-NORDC-ARCHS-NEXT: 13: llvm-offload-binary, {7, 12}, image, (device-sycl)
 // CHK-PHASES-NORDC-ARCHS-NEXT: 14: clang-linker-wrapper, {13}, sycl-fatbin, (device-sycl)
 
diff --git a/clang/test/Driver/sycl-system-arch.cpp b/clang/test/Driver/sycl-system-arch.cpp
index b5c86ab760f04f..0323182e486f8b 100644
--- a/clang/test/Driver/sycl-system-arch.cpp
+++ b/clang/test/Driver/sycl-system-arch.cpp
@@ -14,7 +14,7 @@
 // RUN:   --offload-arch=native --offload-arch-tool=%t/offload_arch_sm_70_gfx906_gfx90a_xe_pvc \
 // RUN:   -c %s 2>&1 | FileCheck %s
 // CHECK-NOT: "device-sycl ({{amdgpu|nvptx}}
-// CHECK: offload, "device-sycl (spirv64-unknown-unknown:xe-pvc)"
+// CHECK: offload, "device-sycl (spirv64-unknown-unknown:igca_20ca)"
 // CHECK-NOT: "device-sycl ({{amdgpu|nvptx}}
 
 /// Without an Intel GPU, SYCL reports that no GPU was found, as HIP and CUDA
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index b092052cabb9e0..4db98ef3fe0beb 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -65,6 +65,37 @@ __attribute__((visibility("protected"), used)) int x;
 
 // SPIRV-LINK: clang{{.*}} -o {{.*}}.img -dumpdir a.out.spirv64.foo.img. --target=spirv64-unknown-unknown -march=foo {{.*}}.o --sycl-link{{$}}
 
+// SYCL images with the same IGCA target and the same "code" list are linked
+// together and compiled ahead of time for the devices in the list. Images with
+// a different "code" list are linked separately.
+// RUN: llvm-offload-binary -o %t.sycl-u.out \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u
+// RUN: llvm-offload-binary -o %t.sycl-uh.out \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,code=xe-mtl-h
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-u.o -fembed-offload-object=%t.sycl-u.out
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-uh-a.o -fembed-offload-object=%t.sycl-uh.out
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-uh-b.o -fembed-offload-object=%t.sycl-uh.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.sycl-uh-a.o %t.sycl-uh-b.o -o a.out 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SYCL-SAME-CODE
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.sycl-u.o %t.sycl-uh-a.o -o a.out 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SYCL-DIFFERENT-CODE
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --save-temps --linker-path=/usr/bin/ld %t.sycl-u.o %t.sycl-uh-a.o \
+// RUN:   -o a.out 2>&1 | FileCheck %s --check-prefix=SYCL-SAVE-TEMPS
+
+// SYCL-SAME-CODE:     clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
+// SYCL-SAME-CODE-NOT: --sycl-link
+
+// SYCL-DIFFERENT-CODE-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u{{$}}
+// SYCL-DIFFERENT-CODE-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
+
+// The devices are part of the output name, so the two images do not overwrite
+// each other with --save-temps.
+// SYCL-SAVE-TEMPS-DAG: clang{{.*}} -o a.out.spirv64.igca_30r.xe-mtl-u.img {{.*}}--device=xe-mtl-u{{$}}
+// SYCL-SAVE-TEMPS-DAG: clang{{.*}} -o a.out.spirv64.igca_30r.xe-mtl-u.xe-mtl-h.img {{.*}}--device=xe-mtl-h{{$}}
+
 // RUN: llvm-offload-binary -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/test/OffloadTools/clang-sycl-linker/basic.ll b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
index 8e0523c1ea2665..1095cd227f05b1 100644
--- a/clang/test/OffloadTools/clang-sycl-linker/basic.ll
+++ b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
@@ -124,6 +124,24 @@
 ; RUN:   | FileCheck %s --check-prefix=AOT-INTEL-OCLOC-OPTIONS
 ; AOT-INTEL-OCLOC-OPTIONS: "{{.*}}ocloc{{.*}}" {{.*}}-device xe-bmg-g21 -a -b -c d "-e -f"
 ;
+; Test that an IGCA target is compiled ahead of time with one ocloc call for its
+; --device list.
+; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --device=xe-mtl-u --device=xe-mtl-h %t/input1.bc %t/input2.bc -o %t/aot-igca.out 2>&1 \
+; RUN:   | FileCheck %s --check-prefix=AOT-IGCA
+; AOT-IGCA:      link: inputs: {{.*}}.bc, {{.*}}.bc output: [[LLVMLINKOUT:.*]].bc
+; AOT-IGCA-NEXT: LLVM backend: input: [[LLVMLINKOUT]].bc, output: [[SPVSTEM:.*]].spv
+; AOT-IGCA-NEXT: "{{.*}}ocloc{{.*}}" {{.*}}-device xe-mtl-u,xe-mtl-h {{.*}}-output [[SPVSTEM]].out -file [[SPVSTEM]].spv
+; AOT-IGCA-NEXT: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: igca_30r
+; AOT-IGCA-NOT:  {{.+}}
+;
+; Test that an IGCA target without --device is compiled just in time.
+; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r %t/input1.bc %t/input2.bc -o %t/jit-igca.out 2>&1 \
+; RUN:   | FileCheck %s --check-prefix=JIT-IGCA
+; JIT-IGCA:      link: inputs: {{.*}}.bc, {{.*}}.bc output: [[LLVMLINKOUT:.*]].bc
+; JIT-IGCA-NEXT: LLVM backend: input: [[LLVMLINKOUT]].bc, output: {{.*}}.spv
+; JIT-IGCA-NEXT: sycl-bundle: image kind: spv, triple: spirv64-unknown-unknown, arch: igca_30r
+; JIT-IGCA-NOT:  {{.+}}
+;
 ; Test AOT compilation for an Intel CPU.
 ; Test that IMG_Object image kind is set for AOT compilation (Intel CPU).
 ; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=graniterapids %t/input1.bc %t/input2.bc -o %t/aot-cpu.out 2>&1 \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 9f3976949d3f6b..8b8a4fcf7df793 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -504,6 +504,11 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
   // input filename already has the device and architecture.
   std::string OutputFileBase =
       "." + Triple.getArchName().str() + "." + Arch.str();
+  // Images with the same architecture but different devices are linked
+  // separately, so the devices are part of the name.
+  std::vector<std::string> Devices = Args.getAllArgValues(OPT_device_EQ);
+  for (const std::string &Device : Devices)
+    OutputFileBase += "." + Device;
   auto TempFileOrErr = createOutputFile(ExecutableName + OutputFileBase, "img");
   if (!TempFileOrErr)
     return TempFileOrErr.takeError();
@@ -592,6 +597,10 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
   if (ActiveOffloadKindMask & OFK_SYCL)
     CmdArgs.push_back("--sycl-link");
 
+  // Pass the devices to compile ahead of time for on to clang-sycl-linker.
+  for (const std::string &Device : Devices)
+    CmdArgs.append({"-Xlinker", Args.MakeArgString("--device=" + Device)});
+
   for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
     CmdArgs.append({"-Xlinker", Args.MakeArgString(Arg)});
   for (StringRef Arg : Args.getAllArgValues(OPT_compiler_arg_EQ))
@@ -1012,6 +1021,15 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
   DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_triple_EQ),
                    Args.MakeArgString(Input.front().getBinary()->getTriple()));
 
+  // Record the Intel GPUs that a SYCL image lists in "code", which
+  // clang-sycl-linker compiles the image ahead of time for.
+  const OffloadBinary &Binary = *Input.front().getBinary();
+  if (Binary.getOffloadKind() == OFK_SYCL)
+    for (StringRef Device : llvm::split(Binary.getString("code"), ','))
+      if (!Device.empty())
+        DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_device_EQ),
+                         Args.MakeArgString(Device));
+
   // If every input file is bitcode we have whole program visibility as we
   // do only support static linking with bitcode.
   auto ContainsBitcode = [](const OffloadFile &F) {
@@ -1444,16 +1462,25 @@ getDeviceInput(const ArgList &Args) {
                              B.getBinary()->getArch().count(':');
                     });
 
+  // A SYCL image can also list the Intel GPUs to compile it ahead of time for,
+  // e.g. "code=xe-mtl-u,xe-mtl-h". Images with different lists are linked
+  // separately, so the key of an image is its target and its "code" value.
+  using ImageID = std::pair<OffloadFile::TargetID, StringRef>;
+  auto GetImageID = [](const OffloadFile &Binary) -> ImageID {
+    return {Binary, Binary.getBinary()->getString("code")};
+  };
+
   // Link all standard input files and update the list of symbols.
-  MapVector<OffloadFile::TargetID, SmallVector<OffloadFile, 0>> InputFiles;
+  MapVector<ImageID, SmallVector<OffloadFile, 0>> InputFiles;
   for (OffloadFile &Binary : ObjectFilesToExtract) {
     if (!Binary.getBinary())
       continue;
 
-    OffloadFile::TargetID Target = Binary;
-    SmallVector<OffloadFile::TargetID> CompatibleTargets;
+    ImageID Target = GetImageID(Binary);
+    SmallVector<ImageID> CompatibleTargets;
     for (const auto &[ID, Input] : InputFiles)
-      if (object::areTargetsEquivalent(Target, ID))
+      if (Target.second == ID.second &&
+          object::areTargetsEquivalent(Target.first, ID.first))
         CompatibleTargets.emplace_back(ID);
 
     // Seed a new image when no existing target can provide for this input.
@@ -1480,16 +1507,17 @@ getDeviceInput(const ArgList &Args) {
     if (!Binary.getBinary())
       continue;
 
-    SmallVector<OffloadFile::TargetID> CompatibleTargets = {Binary};
+    ImageID Target = GetImageID(Binary);
+    SmallVector<ImageID> CompatibleTargets = {Target};
     for (const auto &[ID, Input] : InputFiles)
-      if (OffloadFile::TargetID(Binary) != ID &&
-          object::areTargetsCompatible(Binary, ID))
+      if (Target != ID && Target.second == ID.second &&
+          object::areTargetsCompatible(Target.first, ID.first))
         CompatibleTargets.emplace_back(ID);
 
     for (const auto &[Index, ID] : llvm::enumerate(CompatibleTargets)) {
       // Only extract if we have an object matching this target or it
       // was specifically requested.
-      if (!InputFiles.count(ID) && !ShouldExtract.contains(ID.second))
+      if (!InputFiles.count(ID) && !ShouldExtract.contains(ID.first.second))
         continue;
 
       // If another target needs this binary it must be copied instead.
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index fc48eac2637eeb..d045b02428adfa 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -80,6 +80,9 @@ def arch_EQ : Joined<["--"], "arch=">,
 def triple_EQ : Joined<["--"], "triple=">,
   Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<triple>">,
   HelpText<"The device target triple">;
+def device_EQ : Joined<["--"], "device=">,
+  Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<device>">,
+  HelpText<"An Intel GPU to compile a SYCL image ahead of time for">;
 def whole_program : Flag<["--"], "whole-program">,
   Flags<[DeviceOnlyOption, HelpHidden]>,
   HelpText<"LTO has visibility of all input files">;
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 8b179f4c37e23b..314913a829e925 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -714,10 +714,14 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
   CmdArgs.push_back("-output_no_suffix");
   CmdArgs.push_back("-spirv_input");
 
-  StringRef Arch(Args.getLastArgValue(OPT_arch_EQ));
-  assert(!Arch.empty() && "Arch must be specified for AOT compilation");
+  // Compile for the --device list, e.g. "xe-mtl-u,xe-mtl-h" for an IGCA
+  // target, or else for the Intel GPU in --arch.
+  std::string Devices = llvm::join(Args.getAllArgValues(OPT_device_EQ), ",");
+  if (Devices.empty())
+    Devices = Args.getLastArgValue(OPT_arch_EQ);
+  assert(!Devices.empty() && "a device must be specified for AOT compilation");
   CmdArgs.push_back("-device");
-  CmdArgs.push_back(Arch);
+  CmdArgs.push_back(Devices);
 
   // getAllArgValues returns a temporary vector; retain it so the StringRefs
   // remain valid through the executeCommands call below.
@@ -742,15 +746,11 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
 /// into the AOT compilation step.
 static Error runAOTCompile(StringRef InputFile, StringRef OutputFile,
                            const ArgList &Args) {
-  StringRef Arch = Args.getLastArgValue(OPT_arch_EQ);
-  OffloadArch OA = StringToOffloadArch(Arch);
-  if (OA.isIntelGPU())
+  OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
+  if (OA.isIntelGPU() || OA.isIntelIGCA())
     return runAOTCompileIntelGPU(InputFile, OutputFile, Args);
   if (OA.isIntelCPU())
     return runAOTCompileIntelCPU(InputFile, OutputFile, Args);
-  if (OA.isIntelIGCA())
-    return createStringError("AOT compilation for IGCA target '" + Arch +
-                             "' is not yet implemented");
 
   llvm_unreachable("runAOTCompile dispatched on unsupported arch");
 }
@@ -1002,8 +1002,11 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
     SplitModules = std::move(*SplitModulesOrErr);
   }
 
+  // An IGCA target is compiled ahead of time only for its --device list, and
+  // otherwise just in time.
+  OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
   bool IsAOTCompileNeeded =
-      StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ)).isIntel();
+      OA.isIntelIGCA() ? Args.hasArg(OPT_device_EQ) : OA.isIntel();
 
   StringRef OutputFileNameExt = ".spv";
 
diff --git a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
index 965ab6b5e4e65d..db81c494be0527 100644
--- a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
+++ b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
@@ -46,6 +46,11 @@ def arch_EQ : Joined<["--", "-"], "arch=">,
               Flags<[LinkerOnlyOption]>,
               MetaVarName<"<arch>">,
               HelpText<"The device architecture">;
+def device_EQ : Joined<["--", "-"], "device=">,
+                Flags<[LinkerOnlyOption]>,
+                MetaVarName<"<device>">,
+                HelpText<"An Intel GPU to compile ahead of time for. Can be "
+                         "repeated. Defaults to --arch for an Intel GPU">;
 def triple_EQ : Joined<["--", "-"], "triple=">,
                 Flags<[LinkerOnlyOption]>,
                 MetaVarName<"<triple>">,
diff --git a/clang/unittests/Basic/OffloadArchTest.cpp b/clang/unittests/Basic/OffloadArchTest.cpp
index 4ebee22fb2294d..2ed374e1d4a318 100644
--- a/clang/unittests/Basic/OffloadArchTest.cpp
+++ b/clang/unittests/Basic/OffloadArchTest.cpp
@@ -87,6 +87,14 @@ TEST(OffloadArchTest, IntelGPUOtherSpellings) {
   EXPECT_TRUE(parse("bmg_g21").isUnknown());
 }
 
+TEST(OffloadArchTest, IntelGPUIGCATarget) {
+  // Intel GPUs can share an IGCA target.
+  EXPECT_EQ(parse("xe-mtl-u").intelGPUIGCATarget(), parse("igca_30r"));
+  EXPECT_EQ(parse("xe-mtl-h").intelGPUIGCATarget(), parse("igca_30r"));
+  EXPECT_EQ(parse("xe-pvc").intelGPUIGCATarget(), parse("igca_20ca"));
+  EXPECT_TRUE(parse("xe-pvc").intelGPUIGCATarget().isIntelIGCA());
+}
+
 TEST(OffloadArchTest, Defaults) {
   EXPECT_STREQ(OffloadArchToString(OffloadArch::CudaDefault()), "sm_52");
   EXPECT_STREQ(OffloadArchToString(OffloadArch::HIPDefault()), "gfx906");

>From dc3a0fc233372a625f6dda73b4e0f4dfb22891ae Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 4 Nov 2026 15:50:42 -0800
Subject: [PATCH 04/13] [Clang][SYCL] Address review: use DenseMap, document
 --device and code

---
 clang/docs/ClangLinkerWrapper.md         | 5 +++++
 clang/docs/ClangSYCLLinker.md            | 9 +++++++++
 clang/include/clang/Driver/Compilation.h | 4 ++--
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index 7245d3cead6ad9..df2a946080e0c3 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -82,6 +82,11 @@ other. Generally, this requires that the target triple and architecture match.
 An exception is made when the architecture is listed as `generic`, which will
 cause it be linked with any other device code with the same target triple.
 
+SYCL device code for an IGCA target can also list the Intel GPUs to compile it
+ahead of time for, as `code=xe-mtl-h,xe-mtl-u`. It is only linked with device
+code that lists the same GPUs, and the GPUs are passed on to
+`clang-sycl-linker`.
+
 ## Debugging
 
 The linker wrapper performs a lot of steps internally, such as input matching,
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index c527afa8b41f6d..6c156ca2540178 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -37,6 +37,7 @@ USAGE: clang-sycl-linker [options] <input bitcode files>
 
 OPTIONS:
   --arch <value>                Specify the name of the target architecture.
+  --device=<value>              An Intel GPU to compile ahead of time for. Can be repeated.
   --dry-run                     Print generated commands without running.
   -help-hidden                  Display all available options
   -help                         Display available options (--help-hidden for more)
@@ -108,6 +109,14 @@ generate the final executable.
 clang-sycl-linker --triple spirv64 --arch xe-bmg-g21 input.bc
 ```
 
+For an IGCA target, such as `igca_30r`, the device image is compiled ahead of
+time for the Intel GPUs given with `--device`, with a single ocloc call, or just
+in time if no `--device` is given:
+
+```console
+clang-sycl-linker --triple spirv64 --arch igca_30r --device=xe-mtl-u --device=xe-mtl-h input.bc
+```
+
 ### Linking with Device Libraries
 
 To link device bitcode libraries, first package them into archive files:
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 59df1af0fc70b3..d7e7deade6f7c0 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -101,8 +101,8 @@ class Compilation {
   /// The Intel GPUs that each SYCL device compile for an IGCA target is
   /// compiled ahead of time for, sorted, e.g. xe-mtl-h and xe-mtl-u for the
   /// igca_30r compile.
-  std::map<std::pair<const ToolChain *, StringRef>,
-           llvm::SmallVector<StringRef, 2>>
+  llvm::DenseMap<std::pair<const ToolChain *, StringRef>,
+                 llvm::SmallVector<StringRef, 2>>
       SYCLIGCADevices;
 
   /// Temporary files which should be removed on exit.

>From f638c9c6220fe077279d7bdcd05297f95b08d608 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 4 Nov 2026 17:44:53 -0800
Subject: [PATCH 05/13] [Clang][SYCL] Address review: test -Xarch_ with an IGCA
 target, clarify a comment

---
 clang/include/clang/Driver/Compilation.h  | 5 ++---
 clang/test/Driver/sycl-intel-gpu-arch.cpp | 9 +++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index d7e7deade6f7c0..53d204e9fa150e 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -98,9 +98,8 @@ class Compilation {
   };
   std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
 
-  /// The Intel GPUs that each SYCL device compile for an IGCA target is
-  /// compiled ahead of time for, sorted, e.g. xe-mtl-h and xe-mtl-u for the
-  /// igca_30r compile.
+  /// For each SYCL device compile for an IGCA target, the Intel GPUs to compile
+  /// it ahead of time for, sorted, e.g. xe-mtl-h and xe-mtl-u for igca_30r.
   llvm::DenseMap<std::pair<const ToolChain *, StringRef>,
                  llvm::SmallVector<StringRef, 2>>
       SYCLIGCADevices;
diff --git a/clang/test/Driver/sycl-intel-gpu-arch.cpp b/clang/test/Driver/sycl-intel-gpu-arch.cpp
index 0aa5b0b7bd87a8..5f1346b55101af 100644
--- a/clang/test/Driver/sycl-intel-gpu-arch.cpp
+++ b/clang/test/Driver/sycl-intel-gpu-arch.cpp
@@ -35,6 +35,15 @@
 // RUN:   | FileCheck -check-prefixes=CHK-IGCA-ONLY %s
 // CHK-IGCA-ONLY: "--image=file={{.*}},arch=igca_30r,kind=sycl"
 
+/// -Xarch_ names the IGCA target of the device compile, not an Intel GPU, as
+/// one device compile can be for several Intel GPUs.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN:   --offload-arch=xe-pvc -Xarch_igca_20ca -DFOO -Xarch_xe-pvc -DBAR \
+// RUN:   -c %s 2>&1 | FileCheck -check-prefixes=CHK-XARCH %s
+// CHK-XARCH-NOT: "-D" "BAR"
+// CHK-XARCH: "-cc1" "-triple" "spirv64-unknown-unknown"{{.*}} "-D" "FOO"
+// CHK-XARCH-NOT: "-D" "BAR"
+
 /// Static libraries are searched for the IGCA target of an Intel GPU.
 // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-arch=xe-mtl-u,xe-mtl-h %s 2>&1 \

>From 734708e523cc4c0e59f5e3cc213302fb8b455bdc Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 12:44:02 -0800
Subject: [PATCH 06/13] [Clang][SYCL] Simplify the Compilation comments

---
 clang/include/clang/Driver/Compilation.h | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 53d204e9fa150e..c37075dcb9ad57 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -98,8 +98,8 @@ class Compilation {
   };
   std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
 
-  /// For each SYCL device compile for an IGCA target, the Intel GPUs to compile
-  /// it ahead of time for, sorted, e.g. xe-mtl-h and xe-mtl-u for igca_30r.
+  /// For SYCL, the Intel GPUs given in --offload-arch for each IGCA target,
+  /// e.g. xe-mtl-h and xe-mtl-u for igca_30r. Each list is sorted.
   llvm::DenseMap<std::pair<const ToolChain *, StringRef>,
                  llvm::SmallVector<StringRef, 2>>
       SYCLIGCADevices;
@@ -263,14 +263,12 @@ class Compilation {
   getArgsForToolChain(const ToolChain *TC, BoundArch BA,
                       Action::OffloadKind DeviceOffloadKind);
 
-  /// Record that the SYCL device compile for the IGCA target \p Target on
-  /// \p TC is compiled ahead of time for the Intel GPU \p Device.
+  /// Add the Intel GPU \p Device to the GPUs of the IGCA target \p Target.
   void addSYCLIGCADevice(const ToolChain *TC, StringRef Target,
                          StringRef Device);
 
-  /// \return the Intel GPUs that the SYCL device compile for the IGCA target
-  /// \p Target on \p TC is compiled ahead of time for, sorted, or an empty
-  /// list.
+  /// \return the Intel GPUs of the IGCA target \p Target, sorted, or an empty
+  /// list if there are none.
   ArrayRef<StringRef> getSYCLIGCADevices(const ToolChain *TC,
                                          StringRef Target) const;
 

>From 8d704def322a9484218c028078a030a9bbee9115 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 13:00:48 -0800
Subject: [PATCH 07/13] [Clang][SYCL] Keep linking generic archive members into
 images with devices

A generic device image from a static library is linked into any compatible image, as documented in ClangLinkerWrapper.md. Comparing "code" in the archive loop stopped it from being linked into an image that lists Intel GPUs, so exempt generic members from that comparison.
---
 .../clang-linker-wrapper/linker-wrapper.c             | 11 +++++++++++
 .../tools/clang-linker-wrapper/ClangLinkerWrapper.cpp |  5 ++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 4db98ef3fe0beb..8f8646bb39648c 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -84,6 +84,14 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
 // RUN:   --save-temps --linker-path=/usr/bin/ld %t.sycl-u.o %t.sycl-uh-a.o \
 // RUN:   -o a.out 2>&1 | FileCheck %s --check-prefix=SYCL-SAVE-TEMPS
+// RUN: llvm-offload-binary -o %t.sycl-generic.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-generic.o -fembed-offload-object=%t.sycl-generic.out
+// RUN: rm -f %t.sycl-generic.a
+// RUN: llvm-ar rcs %t.sycl-generic.a %t.sycl-generic.o
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --linker-path=/usr/bin/ld %t.sycl-u.o %t.sycl-generic.a -o a.out 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SYCL-GENERIC-ARCHIVE
 
 // SYCL-SAME-CODE:     clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
 // SYCL-SAME-CODE-NOT: --sycl-link
@@ -96,6 +104,9 @@ __attribute__((visibility("protected"), used)) int x;
 // SYCL-SAVE-TEMPS-DAG: clang{{.*}} -o a.out.spirv64.igca_30r.xe-mtl-u.img {{.*}}--device=xe-mtl-u{{$}}
 // SYCL-SAVE-TEMPS-DAG: clang{{.*}} -o a.out.spirv64.igca_30r.xe-mtl-u.xe-mtl-h.img {{.*}}--device=xe-mtl-h{{$}}
 
+// A generic archive member is still linked into an image that lists devices.
+// SYCL-GENERIC-ARCHIVE: clang{{.*}} -march=igca_30r {{.*}}igca_30r{{.*}}.o {{.*}}generic{{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u{{$}}
+
 // RUN: llvm-offload-binary -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 8b8a4fcf7df793..d69117b4cf3429 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1507,10 +1507,13 @@ getDeviceInput(const ArgList &Args) {
     if (!Binary.getBinary())
       continue;
 
+    // A generic member can still be linked into any compatible image; other
+    // members only into images with the same "code".
     ImageID Target = GetImageID(Binary);
+    bool IsGeneric = Target.first.second == "generic";
     SmallVector<ImageID> CompatibleTargets = {Target};
     for (const auto &[ID, Input] : InputFiles)
-      if (Target != ID && Target.second == ID.second &&
+      if (Target != ID && (IsGeneric || Target.second == ID.second) &&
           object::areTargetsCompatible(Target.first, ID.first))
         CompatibleTargets.emplace_back(ID);
 

>From 05ef4b5ced58b3132ff2272f841cbc2ceccefebc Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 13:00:49 -0800
Subject: [PATCH 08/13] [Clang][SYCL] Simplify comments

---
 clang/lib/Driver/Driver.cpp                          |  6 +++---
 clang/lib/Driver/ToolChains/Clang.cpp                |  5 ++---
 .../clang-linker-wrapper/ClangLinkerWrapper.cpp      | 12 ++++++------
 3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index fdd2dcec9a03f1..66a509b2ca4cc7 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4151,9 +4151,9 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
   Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
   Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
 
-  // For SYCL, compile once per IGCA target rather than once per Intel GPU,
-  // e.g. once for igca_30r for xe-mtl-u and xe-mtl-h, and record the GPUs so
-  // that the compile is also done ahead of time for them.
+  // For SYCL, compile once per IGCA target, e.g. once for igca_30r for both
+  // xe-mtl-u and xe-mtl-h, and record its Intel GPUs for ahead-of-time
+  // compilation.
   if (Kind == Action::OFK_SYCL) {
     llvm::DenseSet<StringRef> Targets;
     for (StringRef Arch : Archs) {
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 5ea1ca5888219b..baf65ab4d9687f 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9759,9 +9759,8 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
         "kind=" + Kind.str(),
     };
 
-    // List the Intel GPUs that a SYCL device compile for an IGCA target is
-    // compiled ahead of time for, one "code=" each. llvm-offload-binary joins
-    // repeated keys, so the image gets "code=xe-mtl-h,xe-mtl-u".
+    // Add one "code=" per Intel GPU of the IGCA target. llvm-offload-binary
+    // joins them, so the image gets "code=xe-mtl-h,xe-mtl-u".
     if (OffloadAction->getOffloadingDeviceKind() == Action::OFK_SYCL)
       for (StringRef Device : C.getSYCLIGCADevices(TC, Arch.ArchName))
         Parts.emplace_back("code=" + Device.str());
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index d69117b4cf3429..6210db9840cfc5 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -597,7 +597,7 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
   if (ActiveOffloadKindMask & OFK_SYCL)
     CmdArgs.push_back("--sycl-link");
 
-  // Pass the devices to compile ahead of time for on to clang-sycl-linker.
+  // Pass the devices on to clang-sycl-linker.
   for (const std::string &Device : Devices)
     CmdArgs.append({"-Xlinker", Args.MakeArgString("--device=" + Device)});
 
@@ -1021,8 +1021,8 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
   DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_triple_EQ),
                    Args.MakeArgString(Input.front().getBinary()->getTriple()));
 
-  // Record the Intel GPUs that a SYCL image lists in "code", which
-  // clang-sycl-linker compiles the image ahead of time for.
+  // Get the Intel GPUs that a SYCL image lists in "code". clang-sycl-linker
+  // compiles the image ahead of time for them.
   const OffloadBinary &Binary = *Input.front().getBinary();
   if (Binary.getOffloadKind() == OFK_SYCL)
     for (StringRef Device : llvm::split(Binary.getString("code"), ','))
@@ -1462,9 +1462,9 @@ getDeviceInput(const ArgList &Args) {
                              B.getBinary()->getArch().count(':');
                     });
 
-  // A SYCL image can also list the Intel GPUs to compile it ahead of time for,
-  // e.g. "code=xe-mtl-u,xe-mtl-h". Images with different lists are linked
-  // separately, so the key of an image is its target and its "code" value.
+  // A SYCL image can list Intel GPUs in "code", e.g. "code=xe-mtl-h,xe-mtl-u".
+  // Images with different lists are linked separately, so the key of an image
+  // is its target and its "code".
   using ImageID = std::pair<OffloadFile::TargetID, StringRef>;
   auto GetImageID = [](const OffloadFile &Binary) -> ImageID {
     return {Binary, Binary.getBinary()->getString("code")};

>From 64749c4afdc155f629bb7dfa612250d2e2038ed9 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 13:31:47 -0800
Subject: [PATCH 09/13] [Clang][SYCL] Address review: keep every SYCL image,
 unique extracted names

bundleSYCL returned one offload binary per linked image, but only the first was wrapped, so images linked separately (different IGCA targets or different code) were silently dropped, and --emit-fatbin-only failed. Merge the device images of all of them into one offload binary, which the SYCL runtime registers. With --save-temps, images that differ only in code were extracted to the same file; add the Intel GPUs in code to the extracted file name and to the replayed extraction commands.
---
 .../clang-linker-wrapper/linker-wrapper.c     | 28 ++++++++
 .../ClangLinkerWrapper.cpp                    | 72 +++++++++++++++----
 2 files changed, 85 insertions(+), 15 deletions(-)

diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 8f8646bb39648c..b29c8c12dfc0aa 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -92,6 +92,23 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
 // RUN:   --linker-path=/usr/bin/ld %t.sycl-u.o %t.sycl-generic.a -o a.out 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=SYCL-GENERIC-ARCHIVE
+// RUN: llvm-offload-binary -o %t.sycl-two.out \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,code=xe-mtl-h
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-two.o -fembed-offload-object=%t.sycl-two.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --save-temps --wrapper-verbose --linker-path=/usr/bin/ld %t.sycl-two.o \
+// RUN:   -o a.out 2>&1 | FileCheck %s --check-prefix=SYCL-EXTRACT
+// RUN: llvm-offload-binary -o %t.sycl-30r.out \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r
+// RUN: llvm-offload-binary -o %t.sycl-40r.out \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_40r
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-30r.o -fembed-offload-object=%t.sycl-30r.out
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-40r.o -fembed-offload-object=%t.sycl-40r.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu \
+// RUN:   --emit-fatbin-only --linker-path=/usr/bin/ld %t.sycl-30r.o %t.sycl-40r.o \
+// RUN:   -o %t.sycl.fatbin
+// RUN: llvm-objdump --offloading %t.sycl.fatbin | FileCheck %s --check-prefix=SYCL-MERGED
 
 // SYCL-SAME-CODE:     clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
 // SYCL-SAME-CODE-NOT: --sycl-link
@@ -107,6 +124,17 @@ __attribute__((visibility("protected"), used)) int x;
 // A generic archive member is still linked into an image that lists devices.
 // SYCL-GENERIC-ARCHIVE: clang{{.*}} -march=igca_30r {{.*}}igca_30r{{.*}}.o {{.*}}generic{{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u{{$}}
 
+// Images that only differ in "code" are extracted to different files, also
+// when replaying the extraction.
+// SYCL-EXTRACT-DAG: llvm-offload-binary{{.*}} --image=kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,file={{.*}}-igca_30r.xe-mtl-u.o --image=kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,code=xe-mtl-h,file={{.*}}-igca_30r.xe-mtl-u.xe-mtl-h.o
+// SYCL-EXTRACT-DAG: clang{{.*}} -march=igca_30r {{.*}}-igca_30r.xe-mtl-u.o --sycl-link -Xlinker --device=xe-mtl-u{{$}}
+// SYCL-EXTRACT-DAG: clang{{.*}} -march=igca_30r {{.*}}-igca_30r.xe-mtl-u.xe-mtl-h.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
+
+// The device images of SYCL images that are linked separately end up in a
+// single offload binary.
+// SYCL-MERGED-DAG: arch            igca_30r
+// SYCL-MERGED-DAG: arch            igca_40r
+
 // RUN: llvm-offload-binary -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 6210db9840cfc5..8c9b059db5d524 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -664,15 +664,24 @@ Error containerizeRawImage(std::unique_ptr<MemoryBuffer> &Img, OffloadKind Kind,
   return Error::success();
 }
 
+/// \return the name to extract \p Binary to. The Intel GPUs in "code" are part
+/// of the name, e.g. "-igca_30r.xe-mtl-h.xe-mtl-u", so that images that only
+/// differ in "code" do not overwrite each other.
+std::string getExtractedFileName(const OffloadBinary &Binary) {
+  std::string Name =
+      (sys::path::stem(Binary.getMemoryBufferRef().getBufferIdentifier()) +
+       "-" + Binary.getTriple() + "-" + Binary.getArch())
+          .str();
+  for (StringRef Device : llvm::split(Binary.getString("code"), ','))
+    if (!Device.empty())
+      Name += ("." + Device).str();
+  return Name;
+}
+
 Expected<StringRef> writeOffloadFile(const OffloadFile &File) {
   const OffloadBinary &Binary = *File.getBinary();
 
-  StringRef Prefix =
-      sys::path::stem(Binary.getMemoryBufferRef().getBufferIdentifier());
-  SmallString<128> Filename;
-  (Prefix + "-" + Binary.getTriple() + "-" + Binary.getArch())
-      .toVector(Filename);
-  auto TempFileOrErr = createOutputFile(Filename, "o");
+  auto TempFileOrErr = createOutputFile(getExtractedFileName(Binary), "o");
   if (!TempFileOrErr)
     return TempFileOrErr.takeError();
 
@@ -918,15 +927,48 @@ bundleOpenMP(ArrayRef<OffloadingImage> Images) {
 
 Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
 bundleSYCL(ArrayRef<OffloadingImage> Images) {
+  // clang-sycl-linker packs its outputs into one offload binary, which is
+  // passed to the offload wrapper as is.
   SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
+  if (Images.size() <= 1) {
+    for (const OffloadingImage &Image : Images)
+      Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(
+          Image.Image->getBuffer(), Image.Image->getBufferIdentifier()));
+    return std::move(Buffers);
+  }
+
+  // Images with different "code" are linked separately, but the SYCL runtime
+  // registers a single offload binary, so merge their device images into one.
+  SmallVector<std::unique_ptr<OffloadBinary>> Binaries;
   for (const OffloadingImage &Image : Images) {
-    // clang-sycl-linker packs outputs into one binary blob. Therefore, it is
-    // passed to Offload Wrapper as is.
-    StringRef S(Image.Image->getBufferStart(), Image.Image->getBufferSize());
-    Buffers.emplace_back(
-        MemoryBuffer::getMemBufferCopy(S, Image.Image->getBufferIdentifier()));
+    // A dry run has no linked output to read.
+    if (Image.Image->getBufferSize() == 0)
+      continue;
+    auto BinariesOrErr = OffloadBinary::create(Image.Image->getMemBufferRef());
+    if (!BinariesOrErr)
+      return BinariesOrErr.takeError();
+    for (std::unique_ptr<OffloadBinary> &Binary : *BinariesOrErr)
+      Binaries.push_back(std::move(Binary));
+  }
+
+  SmallVector<OffloadingImage> Merged;
+  for (const std::unique_ptr<OffloadBinary> &Binary : Binaries) {
+    OffloadingImage Image;
+    Image.TheImageKind = Binary->getImageKind();
+    Image.TheOffloadKind = Binary->getOffloadKind();
+    Image.Flags = Binary->getFlags();
+    for (const auto &[Key, Value] : Binary->strings())
+      Image.StringData[Key] = Value;
+    Image.Image = MemoryBuffer::getMemBuffer(Binary->getImage(), "",
+                                             /*RequiresNullTerminator=*/false);
+    Merged.push_back(std::move(Image));
   }
 
+  SmallString<0> Data;
+  if (!Merged.empty())
+    Data = OffloadBinary::write(Merged);
+  Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(
+      Data, Images.front().Image->getBufferIdentifier()));
   return std::move(Buffers);
 }
 
@@ -1336,10 +1378,7 @@ Error emitExtractCommands(
       if (Source.empty())
         Source = Identifier;
 
-      auto OutputOrErr =
-          createOutputFile(sys::path::stem(Identifier) + "-" +
-                               Binary.getTriple() + "-" + Binary.getArch(),
-                           "o");
+      auto OutputOrErr = createOutputFile(getExtractedFileName(Binary), "o");
       if (!OutputOrErr)
         return OutputOrErr.takeError();
 
@@ -1349,6 +1388,9 @@ Error emitExtractCommands(
               .str();
       if (!Binary.getArch().empty())
         ImageArg += (",arch=" + Binary.getArch()).str();
+      for (StringRef Device : llvm::split(Binary.getString("code"), ','))
+        if (!Device.empty())
+          ImageArg += (",code=" + Device).str();
       file_magic Magic;
       if (!identify_magic(Source, Magic) && Magic == file_magic::archive)
         ImageArg += (",member=" + sys::path::filename(Identifier)).str();

>From 872a7d5986eed2f273ff7d36ce4a7885c43b94eb Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 14:51:07 -0800
Subject: [PATCH 10/13] [Clang][SYCL] Ask once for an IGCA target in
 --should-extract, tighten tests

Intel GPUs that share an IGCA target gave the same --should-extract value once each. Also check the whole llvm-offload-binary command in the tests that expect a single device image, as {{.*}} could match across another image.
---
 clang/lib/Driver/ToolChains/Clang.cpp     |  6 +++++-
 clang/test/Driver/sycl-intel-gpu-arch.cpp | 18 +++++++++---------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index baf65ab4d9687f..8b3f977468d900 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -39,6 +39,7 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Frontend/Debug/Options.h"
@@ -9956,6 +9957,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
       // If the user explicitly requested it via `--offload-arch` we should
       // extract it from any static libraries if present. Device images are
       // labeled with the canonical target ID, so request that.
+      llvm::StringSet<> ShouldExtract;
       for (StringRef Arg : ToolChainArgs.getAllArgValues(OPT_offload_arch_EQ)) {
         std::optional<std::string> TargetID =
             getCanonicalTargetID(TC->getTriple(), Arg);
@@ -9965,7 +9967,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
         if (Kind == Action::OFK_SYCL)
           if (OffloadArch ID = StringToOffloadArch(Arg); ID.isIntelGPU())
             Name = OffloadArchToString(ID.intelGPUIGCATarget());
-        CmdArgs.emplace_back(Args.MakeArgString("--should-extract=" + Name));
+        // Intel GPUs that share an IGCA target ask for it once.
+        if (ShouldExtract.insert(Name).second)
+          CmdArgs.emplace_back(Args.MakeArgString("--should-extract=" + Name));
       }
 
       // If this is OpenMP the device linker will need `-lompdevice`.
diff --git a/clang/test/Driver/sycl-intel-gpu-arch.cpp b/clang/test/Driver/sycl-intel-gpu-arch.cpp
index 5f1346b55101af..5ec00a631b5a8c 100644
--- a/clang/test/Driver/sycl-intel-gpu-arch.cpp
+++ b/clang/test/Driver/sycl-intel-gpu-arch.cpp
@@ -15,8 +15,7 @@
 // RUN:   --offload-targets=spirv64-unknown-unknown --offload-arch=xe-mtl-u \
 // RUN:   --offload-arch=xe-arl-u --offload-arch=xe_12.70.0 -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-AS-GIVEN %s
-// CHK-AS-GIVEN: llvm-offload-binary{{.*}} "--image=file={{.*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-arl-u,code=xe-mtl-u,code=xe_12.70.0"
-// CHK-AS-GIVEN-NOT: --image=
+// CHK-AS-GIVEN: llvm-offload-binary" "-o" "{{[^"]*}}" "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-arl-u,code=xe-mtl-u,code=xe_12.70.0"{{$}}
 
 /// The devices are sorted, so that objects compiled for the same devices in any
 /// order have the same list and are linked together.
@@ -26,14 +25,14 @@
 // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-arch=xe-mtl-u,xe-mtl-h -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-SORTED %s
-// CHK-SORTED: "--image=file={{.*}},arch=igca_30r,kind=sycl,code=xe-mtl-h,code=xe-mtl-u"
+// CHK-SORTED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-h,code=xe-mtl-u"
 
 /// An IGCA target given directly lists no devices, so it is compiled just in
 /// time.
 // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-arch=igca_30r -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-IGCA-ONLY %s
-// CHK-IGCA-ONLY: "--image=file={{.*}},arch=igca_30r,kind=sycl"
+// CHK-IGCA-ONLY: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl"
 
 /// -Xarch_ names the IGCA target of the device compile, not an Intel GPU, as
 /// one device compile can be for several Intel GPUs.
@@ -44,12 +43,14 @@
 // CHK-XARCH: "-cc1" "-triple" "spirv64-unknown-unknown"{{.*}} "-D" "FOO"
 // CHK-XARCH-NOT: "-D" "BAR"
 
-/// Static libraries are searched for the IGCA target of an Intel GPU.
+/// Static libraries are searched for the IGCA target of an Intel GPU, once for
+/// Intel GPUs that share it.
 // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
 // RUN:   --offload-arch=xe-mtl-u,xe-mtl-h %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-EXTRACT %s
-// CHK-EXTRACT: clang-linker-wrapper{{.*}} "--should-extract=igca_30r"
-// CHK-EXTRACT-NOT: --should-extract=xe-
+// CHK-EXTRACT: clang-linker-wrapper"
+// CHK-EXTRACT-SAME: "--should-extract=igca_30r"
+// CHK-EXTRACT-NOT: --should-extract=
 
 /// Without --offload-targets, an Intel device (by name or numeric name), an IGCA
 /// target or "generic" picks SPIR-V of the host's width.
@@ -73,8 +74,7 @@
 // RUN:   --offload-arch=xe-mtl-u --offload-arch=xe-arl-u --offload-arch=xe-pvc \
 // RUN:   --no-offload-arch=xe-pvc -c %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=CHK-NO-TARGETS-AS-GIVEN %s
-// CHK-NO-TARGETS-AS-GIVEN: "--image=file={{.*}},arch=igca_30r,kind=sycl,code=xe-arl-u,code=xe-mtl-u"
-// CHK-NO-TARGETS-AS-GIVEN-NOT: {{igca_20ca|xe-pvc}}
+// CHK-NO-TARGETS-AS-GIVEN: llvm-offload-binary" "-o" "{{[^"]*}}" "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-arl-u,code=xe-mtl-u"{{$}}
 
 /// As for CUDA and HIP, -foffload-via-llvm gives the inferred target the LLVM
 /// environment.

>From b814d390b8ece40bee6b45df9dcc35c932896ea7 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 15:44:08 -0800
Subject: [PATCH 11/13] [Clang][SYCL] Address review: wrap the merged file,
 merge in a fixed order

With --wrapper-verbose --save-temps, llvm-offload-wrapper read the first linked image from disk instead of the merged offload binary; write the merged offload binary to its own file and use that. Also sort SYCL images with the same architecture by their code, so images linked in parallel are merged in the same order every time.
---
 .../clang-linker-wrapper/linker-wrapper.c     |  7 ++++++
 .../ClangLinkerWrapper.cpp                    | 24 +++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index b29c8c12dfc0aa..a22d098b69db8d 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -109,6 +109,10 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN:   --emit-fatbin-only --linker-path=/usr/bin/ld %t.sycl-30r.o %t.sycl-40r.o \
 // RUN:   -o %t.sycl.fatbin
 // RUN: llvm-objdump --offloading %t.sycl.fatbin | FileCheck %s --check-prefix=SYCL-MERGED
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --wrapper-verbose --save-temps --linker-path=/usr/bin/ld \
+// RUN:   %t.sycl-30r.o %t.sycl-40r.o -o a.out 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SYCL-MERGED-VERBOSE
 
 // SYCL-SAME-CODE:     clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
 // SYCL-SAME-CODE-NOT: --sycl-link
@@ -135,6 +139,9 @@ __attribute__((visibility("protected"), used)) int x;
 // SYCL-MERGED-DAG: arch            igca_30r
 // SYCL-MERGED-DAG: arch            igca_40r
 
+// With --wrapper-verbose --save-temps, the merged offload binary is wrapped.
+// SYCL-MERGED-VERBOSE: llvm-offload-wrapper{{.*}} --kind=sycl {{.*}} a.out.sycl.merged.out{{$}}
+
 // RUN: llvm-offload-binary -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 8c9b059db5d524..adde04865586bd 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -967,8 +967,20 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
   SmallString<0> Data;
   if (!Merged.empty())
     Data = OffloadBinary::write(Merged);
-  Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(
-      Data, Images.front().Image->getBufferIdentifier()));
+
+  // Write the merged offload binary to its own file, which is what
+  // --wrapper-verbose --save-temps passes to llvm-offload-wrapper.
+  auto FileOrErr = createOutputFile(ExecutableName + ".sycl.merged", "out");
+  if (!FileOrErr)
+    return FileOrErr.takeError();
+  if (!DryRun) {
+    int FD = -1;
+    if (std::error_code EC = sys::fs::openFileForWrite(*FileOrErr, FD))
+      return errorCodeToError(EC);
+    llvm::raw_fd_ostream OS(FD, true);
+    OS << Data;
+  }
+  Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(Data, *FileOrErr));
   return std::move(Buffers);
 }
 
@@ -1235,6 +1247,9 @@ linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
           Args.MakeArgString(LinkerArgs.getLastArgValue(OPT_triple_EQ));
       TheImage.StringData["arch"] =
           Args.MakeArgString(LinkerArgs.getLastArgValue(OPT_arch_EQ));
+      if (LinkerArgs.hasArg(OPT_device_EQ))
+        TheImage.StringData["code"] = Args.MakeArgString(
+            llvm::join(LinkerArgs.getAllArgValues(OPT_device_EQ), ","));
       TheImage.Image = std::move(*FileOrErr);
 
       Images[Kind].emplace_back(std::move(TheImage));
@@ -1261,6 +1276,11 @@ linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
         return TripleA > TripleB;
       if (ArchA != ArchB)
         return ArchA > ArchB;
+      // SYCL images with the same architecture differ in "code".
+      StringRef CodeA = A.StringData.lookup("code");
+      StringRef CodeB = B.StringData.lookup("code");
+      if (CodeA != CodeB)
+        return CodeA > CodeB;
       return A.TheOffloadKind < B.TheOffloadKind;
     });
     auto BundledImagesOrErr = bundleLinkedOutput(Input, Args, WrapperKind);

>From 1e162138434f12908b4c8c27cb41c67f085f10f3 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 16:33:07 -0800
Subject: [PATCH 12/13] [Clang][SYCL] Address review: write the merged file
 only with --save-temps, filter replays on empty code

The merged SYCL offload binary is only read from disk with --save-temps, so write it only then. Replayed extractions of SYCL images now also filter on an empty code, so an image without code does not also match one with code for the same architecture.
---
 .../linker-wrapper-verbose.c                  |  2 +-
 .../clang-linker-wrapper/linker-wrapper.c     | 11 +++++
 .../ClangLinkerWrapper.cpp                    | 41 ++++++++++++-------
 3 files changed, 38 insertions(+), 16 deletions(-)

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 7da80fc9ee41f9..d822c4043c9332 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
@@ -75,7 +75,7 @@
 // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --wrapper-verbose --save-temps --dry-run \
 // RUN:   --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=SYCL
 
-// SYCL: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=sycl,triple=spirv64-unknown-unknown,arch=generic,file={{.*}}.o
+// SYCL: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=sycl,triple=spirv64-unknown-unknown,arch=generic,code=,file={{.*}}.o
 // SYCL: clang{{.*}} --target=spirv64-unknown-unknown {{.*}} --sycl-link{{$}}
 // 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
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index a22d098b69db8d..ef949d8f2f83e1 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -113,6 +113,13 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN:   --wrapper-verbose --save-temps --linker-path=/usr/bin/ld \
 // RUN:   %t.sycl-30r.o %t.sycl-40r.o -o a.out 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=SYCL-MERGED-VERBOSE
+// RUN: llvm-offload-binary -o %t.sycl-mix.out \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r \
+// RUN:   --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-mix.o -fembed-offload-object=%t.sycl-mix.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN:   --save-temps --wrapper-verbose --linker-path=/usr/bin/ld %t.sycl-mix.o \
+// RUN:   -o a.out 2>&1 | FileCheck %s --check-prefix=SYCL-REPLAY
 
 // SYCL-SAME-CODE:     clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --device=xe-mtl-h{{$}}
 // SYCL-SAME-CODE-NOT: --sycl-link
@@ -142,6 +149,10 @@ __attribute__((visibility("protected"), used)) int x;
 // With --wrapper-verbose --save-temps, the merged offload binary is wrapped.
 // SYCL-MERGED-VERBOSE: llvm-offload-wrapper{{.*}} --kind=sycl {{.*}} a.out.sycl.merged.out{{$}}
 
+// The replayed extraction of an image without "code" does not also match one
+// with "code" for the same architecture.
+// SYCL-REPLAY: llvm-offload-binary{{.*}} --image=kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=,file={{[^ ]*}}-igca_30r.o --image=kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,file={{[^ ]*}}-igca_30r.xe-mtl-u.o
+
 // RUN: llvm-offload-binary -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 adde04865586bd..9439d4bb712d8c 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -968,19 +968,23 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
   if (!Merged.empty())
     Data = OffloadBinary::write(Merged);
 
-  // Write the merged offload binary to its own file, which is what
-  // --wrapper-verbose --save-temps passes to llvm-offload-wrapper.
-  auto FileOrErr = createOutputFile(ExecutableName + ".sycl.merged", "out");
-  if (!FileOrErr)
-    return FileOrErr.takeError();
-  if (!DryRun) {
-    int FD = -1;
-    if (std::error_code EC = sys::fs::openFileForWrite(*FileOrErr, FD))
-      return errorCodeToError(EC);
-    llvm::raw_fd_ostream OS(FD, true);
-    OS << Data;
+  // With --save-temps, also write the merged offload binary to its own file,
+  // which --wrapper-verbose passes to llvm-offload-wrapper.
+  StringRef Identifier = Images.front().Image->getBufferIdentifier();
+  if (SaveTemps) {
+    auto FileOrErr = createOutputFile(ExecutableName + ".sycl.merged", "out");
+    if (!FileOrErr)
+      return FileOrErr.takeError();
+    if (!DryRun) {
+      int FD = -1;
+      if (std::error_code EC = sys::fs::openFileForWrite(*FileOrErr, FD))
+        return errorCodeToError(EC);
+      llvm::raw_fd_ostream OS(FD, true);
+      OS << Data;
+    }
+    Identifier = *FileOrErr;
   }
-  Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(Data, *FileOrErr));
+  Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(Data, Identifier));
   return std::move(Buffers);
 }
 
@@ -1408,9 +1412,16 @@ Error emitExtractCommands(
               .str();
       if (!Binary.getArch().empty())
         ImageArg += (",arch=" + Binary.getArch()).str();
-      for (StringRef Device : llvm::split(Binary.getString("code"), ','))
-        if (!Device.empty())
-          ImageArg += (",code=" + Device).str();
+      // Filter SYCL images on "code", including an empty one, so that an
+      // image without "code" does not also match one with it.
+      if (Binary.getOffloadKind() == OFK_SYCL) {
+        StringRef Code = Binary.getString("code");
+        if (Code.empty())
+          ImageArg += ",code=";
+        for (StringRef Device : llvm::split(Code, ','))
+          if (!Device.empty())
+            ImageArg += (",code=" + Device).str();
+      }
       file_magic Magic;
       if (!identify_magic(Source, Magic) && Magic == file_magic::archive)
         ImageArg += (",member=" + sys::path::filename(Identifier)).str();

>From 1de4ee860793ddff4d06c0b109b07453e5bf8e79 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 17:25:40 -0800
Subject: [PATCH 13/13] [Clang][SYCL] Address review: merge --embed-bitcode
 images as device images

With --embed-bitcode, each linked SYCL image is the bitcode itself rather
than an offload binary, so add it to the merged offload binary as one
device image instead of parsing it.
---
 .../clang-linker-wrapper/linker-wrapper.c     |  9 ++++
 .../ClangLinkerWrapper.cpp                    | 43 +++++++++++++------
 2 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index ef949d8f2f83e1..0caabced866391 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -109,6 +109,11 @@ __attribute__((visibility("protected"), used)) int x;
 // RUN:   --emit-fatbin-only --linker-path=/usr/bin/ld %t.sycl-30r.o %t.sycl-40r.o \
 // RUN:   -o %t.sycl.fatbin
 // RUN: llvm-objdump --offloading %t.sycl.fatbin | FileCheck %s --check-prefix=SYCL-MERGED
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu \
+// RUN:   --embed-bitcode --emit-fatbin-only --linker-path=/usr/bin/ld \
+// RUN:   %t.sycl-30r.o %t.sycl-40r.o -o %t.sycl-bc.fatbin
+// RUN: llvm-objdump --offloading %t.sycl-bc.fatbin \
+// RUN:   | FileCheck %s --check-prefix=SYCL-MERGED-BC
 // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
 // RUN:   --wrapper-verbose --save-temps --linker-path=/usr/bin/ld \
 // RUN:   %t.sycl-30r.o %t.sycl-40r.o -o a.out 2>&1 \
@@ -146,6 +151,10 @@ __attribute__((visibility("protected"), used)) int x;
 // SYCL-MERGED-DAG: arch            igca_30r
 // SYCL-MERGED-DAG: arch            igca_40r
 
+// With --embed-bitcode, each linked bitcode file is one device image.
+// SYCL-MERGED-BC-COUNT-2: kind            llvm ir
+// SYCL-MERGED-BC-NOT:     kind
+
 // With --wrapper-verbose --save-temps, the merged offload binary is wrapped.
 // SYCL-MERGED-VERBOSE: llvm-offload-wrapper{{.*}} --kind=sycl {{.*}} a.out.sycl.merged.out{{$}}
 
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 9439d4bb712d8c..02905065c40205 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -940,28 +940,43 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
   // Images with different "code" are linked separately, but the SYCL runtime
   // registers a single offload binary, so merge their device images into one.
   SmallVector<std::unique_ptr<OffloadBinary>> Binaries;
+  SmallVector<OffloadingImage> Merged;
   for (const OffloadingImage &Image : Images) {
     // A dry run has no linked output to read.
     if (Image.Image->getBufferSize() == 0)
       continue;
+
+    // With --embed-bitcode, the image is the linked bitcode itself rather than
+    // an offload binary, so it is one device image.
+    if (identify_magic(Image.Image->getBuffer()) !=
+        file_magic::offload_binary) {
+      OffloadingImage Copy;
+      Copy.TheImageKind = Image.TheImageKind;
+      Copy.TheOffloadKind = Image.TheOffloadKind;
+      Copy.Flags = Image.Flags;
+      Copy.StringData = Image.StringData;
+      Copy.Image = MemoryBuffer::getMemBuffer(Image.Image->getMemBufferRef(),
+                                              /*RequiresNullTerminator=*/false);
+      Merged.push_back(std::move(Copy));
+      continue;
+    }
+
     auto BinariesOrErr = OffloadBinary::create(Image.Image->getMemBufferRef());
     if (!BinariesOrErr)
       return BinariesOrErr.takeError();
-    for (std::unique_ptr<OffloadBinary> &Binary : *BinariesOrErr)
+    for (std::unique_ptr<OffloadBinary> &Binary : *BinariesOrErr) {
+      OffloadingImage Entry;
+      Entry.TheImageKind = Binary->getImageKind();
+      Entry.TheOffloadKind = Binary->getOffloadKind();
+      Entry.Flags = Binary->getFlags();
+      for (const auto &[Key, Value] : Binary->strings())
+        Entry.StringData[Key] = Value;
+      Entry.Image =
+          MemoryBuffer::getMemBuffer(Binary->getImage(), "",
+                                     /*RequiresNullTerminator=*/false);
+      Merged.push_back(std::move(Entry));
       Binaries.push_back(std::move(Binary));
-  }
-
-  SmallVector<OffloadingImage> Merged;
-  for (const std::unique_ptr<OffloadBinary> &Binary : Binaries) {
-    OffloadingImage Image;
-    Image.TheImageKind = Binary->getImageKind();
-    Image.TheOffloadKind = Binary->getOffloadKind();
-    Image.Flags = Binary->getFlags();
-    for (const auto &[Key, Value] : Binary->strings())
-      Image.StringData[Key] = Value;
-    Image.Image = MemoryBuffer::getMemBuffer(Binary->getImage(), "",
-                                             /*RequiresNullTerminator=*/false);
-    Merged.push_back(std::move(Image));
+    }
   }
 
   SmallString<0> Data;



More information about the cfe-commits mailing list