[clang] [llvm] [Clang][Object][SYCL][CUDA] Add --[no-]offload-embed-virtual-arch (PR #242407)
Srividya Sundaram via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 15:48:37 PST 2026
https://github.com/srividya-sundaram updated https://github.com/llvm/llvm-project/pull/242407
>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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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/51] [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;
>From a3fe58fec3c3aab1a17fc317354945b8492a28e3 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 5 Nov 2026 17:27:11 -0800
Subject: [PATCH 14/51] [Clang][SYCL] Check the metadata of merged
--embed-bitcode images
---
.../clang-linker-wrapper/linker-wrapper.c | 13 +++++++++++--
1 file changed, 11 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 0caabced866391..718aaa5fdbf8a9 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -152,8 +152,17 @@ __attribute__((visibility("protected"), used)) int x;
// 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
+// SYCL-MERGED-BC: OFFLOADING IMAGE [0]:
+// SYCL-MERGED-BC-NEXT: kind llvm ir
+// SYCL-MERGED-BC-NEXT: arch igca_40r
+// SYCL-MERGED-BC-NEXT: triple spirv64-unknown-unknown
+// SYCL-MERGED-BC-NEXT: producer sycl
+// SYCL-MERGED-BC: OFFLOADING IMAGE [1]:
+// SYCL-MERGED-BC-NEXT: kind llvm ir
+// SYCL-MERGED-BC-NEXT: arch igca_30r
+// SYCL-MERGED-BC-NEXT: triple spirv64-unknown-unknown
+// SYCL-MERGED-BC-NEXT: producer sycl
+// SYCL-MERGED-BC-NOT: OFFLOADING IMAGE
// With --wrapper-verbose --save-temps, the merged offload binary is wrapped.
// SYCL-MERGED-VERBOSE: llvm-offload-wrapper{{.*}} --kind=sycl {{.*}} a.out.sycl.merged.out{{$}}
>From f0582e792fc40a377e2a42e66b78dab4303ed4f5 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 10 Nov 2026 10:16:03 -0800
Subject: [PATCH 15/51] [Clang][SYCL] Address review: share the Intel GPU to
IGCA target mapping
Add getSYCLDeviceArch, used both for the device compiles and for
--should-extract, so that the two always ask for the same name.
---
clang/lib/Driver/Driver.cpp | 11 ++++-------
clang/lib/Driver/ToolChains/Clang.cpp | 4 ++--
clang/lib/Driver/ToolChains/SYCL.cpp | 6 ++++++
clang/lib/Driver/ToolChains/SYCL.h | 4 ++++
4 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 66a509b2ca4cc7..6660e9e45adc9d 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4157,14 +4157,11 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
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());
+ StringRef Target = toolchains::getSYCLDeviceArch(Arch);
Targets.insert(Target);
- C.addSYCLIGCADevice(&TC, Target, Arch);
+ // An Intel GPU maps to a different name, its IGCA target.
+ if (Target != Arch)
+ C.addSYCLIGCADevice(&TC, Target, Arch);
}
Archs = std::move(Targets);
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 8b3f977468d900..52a6dc2c7d54f0 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -18,6 +18,7 @@
#include "Hexagon.h"
#include "PS4CPU.h"
#include "ToolChains/Cuda.h"
+#include "ToolChains/SYCL.h"
#include "clang/Basic/CLWarnings.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/HeaderInclude.h"
@@ -9965,8 +9966,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
// 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());
+ Name = toolchains::getSYCLDeviceArch(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));
diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp
index c805f793215392..3d38f319e91c0c 100644
--- a/clang/lib/Driver/ToolChains/SYCL.cpp
+++ b/clang/lib/Driver/ToolChains/SYCL.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "SYCL.h"
#include "SPIRV.h"
+#include "clang/Basic/OffloadArch.h"
#include "clang/Driver/CommonArgs.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
@@ -108,6 +109,11 @@ static ArrayRef<options::ID> getUnsupportedOpts() {
return UnsupportedOpts;
}
+StringRef toolchains::getSYCLDeviceArch(StringRef Arch) {
+ OffloadArch ID = StringToOffloadArch(Arch);
+ return ID.isIntelGPU() ? OffloadArchToString(ID.intelGPUIGCATarget()) : Arch;
+}
+
SYCLToolChain::SYCLToolChain(const Driver &D, const llvm::Triple &Triple,
const ToolChain &HostTC, const ArgList &Args)
: ToolChain(D, Triple, Args), HostTC(HostTC),
diff --git a/clang/lib/Driver/ToolChains/SYCL.h b/clang/lib/Driver/ToolChains/SYCL.h
index d754544e30d971..135efb1d940c44 100644
--- a/clang/lib/Driver/ToolChains/SYCL.h
+++ b/clang/lib/Driver/ToolChains/SYCL.h
@@ -70,6 +70,10 @@ class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
SYCLInstallationDetector SYCLInstallation;
};
+/// \return the name SYCL device code for \p Arch is compiled for: the IGCA
+/// target of an Intel GPU, e.g. "igca_30r" for "xe-mtl-u", or \p Arch itself.
+StringRef getSYCLDeviceArch(StringRef Arch);
+
} // end namespace toolchains
} // end namespace driver
>From 3dc0fdd0f88ecb53ad4c5017323abf041669efdb Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 13 Nov 2026 09:35:12 -0800
Subject: [PATCH 16/51] [Clang][SYCL] Add --[no-]offload-embed-virtual-arch
With --no-offload-embed-virtual-arch, a SYCL device image compiled ahead of
time for Intel GPUs no longer keeps the SPIR-V. The driver records
no-embed-virtual-arch=1 in the device image, clang-linker-wrapper passes
--no-embed-virtual-arch to clang-sycl-linker when all device code of a link
group asks for it, and clang-sycl-linker passes -exclude_ir to ocloc and
records the setting on its output device image.
---
clang/docs/ClangLinkerWrapper.md | 5 ++-
clang/docs/ClangSYCLLinker.md | 5 +++
clang/include/clang/Driver/Compilation.h | 16 +++++++
clang/include/clang/Options/Options.td | 8 ++++
clang/lib/Driver/Driver.cpp | 2 +
clang/lib/Driver/ToolChains/Clang.cpp | 15 ++++++-
clang/test/Driver/sycl-embed-virtual-arch.cpp | 44 +++++++++++++++++++
.../clang-linker-wrapper/linker-wrapper.c | 17 +++++++
.../OffloadTools/clang-sycl-linker/basic.ll | 13 +++++-
.../ClangLinkerWrapper.cpp | 20 +++++++++
.../clang-sycl-linker/ClangSYCLLinker.cpp | 21 ++++++---
clang/tools/clang-sycl-linker/SYCLLinkOpts.td | 4 ++
12 files changed, 161 insertions(+), 9 deletions(-)
create mode 100644 clang/test/Driver/sycl-embed-virtual-arch.cpp
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index df2a946080e0c3..dd05931496577b 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -85,7 +85,10 @@ 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`.
+`clang-sycl-linker`. If all of that device code was compiled with
+`--no-offload-embed-virtual-arch`, the SPIR-V is dropped from the device image
+compiled ahead of time; if only some of it was, a warning is given and the
+SPIR-V is kept.
## Debugging
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index 6c156ca2540178..4b719ef2e4d941 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -38,6 +38,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.
+ --no-embed-virtual-arch Do not keep the SPIR-V in a device image compiled ahead of time for an Intel GPU.
--dry-run Print generated commands without running.
-help-hidden Display all available options
-help Display available options (--help-hidden for more)
@@ -117,6 +118,10 @@ in time if no `--device` is given:
clang-sycl-linker --triple spirv64 --arch igca_30r --device=xe-mtl-u --device=xe-mtl-h input.bc
```
+The SPIR-V is kept in the device image compiled ahead of time, so that it can
+be compiled just in time for other GPUs. With `--no-embed-virtual-arch`, it is
+dropped.
+
### 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 c37075dcb9ad57..50d4a3f9fe6560 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/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/Option.h"
@@ -104,6 +105,10 @@ class Compilation {
llvm::SmallVector<StringRef, 2>>
SYCLIGCADevices;
+ /// For SYCL, the --offload-arch values given as they are compiled, e.g.
+ /// igca_30r, rather than as an Intel GPU.
+ llvm::DenseSet<std::pair<const ToolChain *, StringRef>> SYCLDirectArchs;
+
/// Temporary files which should be removed on exit.
llvm::opt::ArgStringList TempFiles;
@@ -272,6 +277,17 @@ class Compilation {
ArrayRef<StringRef> getSYCLIGCADevices(const ToolChain *TC,
StringRef Target) const;
+ /// Record that \p Arch was given in --offload-arch as it is compiled, e.g.
+ /// igca_30r, rather than as an Intel GPU.
+ void addSYCLDirectArch(const ToolChain *TC, StringRef Arch) {
+ SYCLDirectArchs.insert({TC, Arch});
+ }
+
+ /// \return true if \p Arch was given in --offload-arch as it is compiled.
+ bool isSYCLDirectArch(const ToolChain *TC, StringRef Arch) const {
+ return SYCLDirectArchs.contains({TC, Arch});
+ }
+
/// addTempFile - Add a file to remove on exit, and returns its
/// argument.
const char *addTempFile(const char *Name) {
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index d9abc9b6e37d01..98a86b0d3c1d9b 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1287,6 +1287,14 @@ def no_offload_arch_EQ : CommaJoined<["--"], "no-offload-arch=">,
HelpText<"Remove CUDA/HIP offloading device architecture (e.g. sm_35, gfx906) from the list of devices to compile for. "
"'all' resets the list to its default value.">;
+defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
+ "offload-embed-virtual-arch",
+ PosFlag<SetTrue, [], [ClangOption], "Also embed the device code for the "
+ "virtual architecture when --offload-arch names a real architecture "
+ "(default for SYCL)">,
+ NegFlag<SetFalse, [], [ClangOption], "Do not embed the device code for the "
+ "virtual architecture when --offload-arch names a real architecture">,
+ BothFlags<[]>, ["--"]>;
def offload_new_driver : Flag<["--"], "offload-new-driver">,
Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
HelpText<"Deprecated; the new driver is always used for offloading compilation.">;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 6660e9e45adc9d..e8997837951c9d 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4162,6 +4162,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
// An Intel GPU maps to a different name, its IGCA target.
if (Target != Arch)
C.addSYCLIGCADevice(&TC, Target, Arch);
+ else
+ C.addSYCLDirectArch(&TC, Arch);
}
Archs = std::move(Targets);
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 52a6dc2c7d54f0..d6f58d364e7f0f 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9763,10 +9763,21 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
// 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))
+ if (OffloadAction->getOffloadingDeviceKind() == Action::OFK_SYCL) {
+ ArrayRef<StringRef> Devices = C.getSYCLIGCADevices(TC, Arch.ArchName);
+ for (StringRef Device : Devices)
Parts.emplace_back("code=" + Device.str());
+ // The SPIR-V can only be dropped when the image is compiled ahead of
+ // time, and is kept when the IGCA target is also named directly.
+ bool EmbedVirtualArch =
+ TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
+ options::OPT_no_offload_embed_virtual_arch, true);
+ if (!EmbedVirtualArch && !Devices.empty() &&
+ !C.isSYCLDirectArch(TC, Arch.ArchName))
+ Parts.emplace_back("no-embed-virtual-arch=1");
+ }
+
if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
for (StringRef Feature : FeatureArgs)
Parts.emplace_back("feature=" + Feature.str());
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
new file mode 100644
index 00000000000000..a32c556442f094
--- /dev/null
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -0,0 +1,44 @@
+/// Tests for --[no-]offload-embed-virtual-arch with SYCL.
+
+/// By default, the SPIR-V is kept.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=xe-mtl-u -c %s 2>&1 \
+// RUN: | FileCheck -check-prefix=EMBED %s
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch \
+// RUN: --offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefix=EMBED %s
+// EMBED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u"{{$}}
+
+/// --no-offload-embed-virtual-arch is recorded for a device image compiled
+/// ahead of time.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefix=NO-EMBED %s
+// NO-EMBED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u,no-embed-virtual-arch=1"{{$}}
+
+/// It has no effect for an IGCA target given directly, which is compiled just
+/// in time, also when one of its Intel GPUs is given as well.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=igca_30r --no-offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefix=DIRECT %s
+// DIRECT-NOT: warning:
+// DIRECT: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl"{{$}}
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=xe-mtl-u,igca_30r --no-offload-embed-virtual-arch \
+// RUN: -c %s 2>&1 | FileCheck -check-prefix=EMBED %s
+
+/// With -Xarch_, it applies only to the device compile for that IGCA target.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=xe-mtl-u,xe-bmg-g21 \
+// RUN: -Xarch_igca_40r --no-offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefix=XARCH %s
+// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u"
+// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_40r,kind=sycl,code=xe-bmg-g21,no-embed-virtual-arch=1"
+
+/// It has no effect without --offload-arch.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --no-offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefix=GENERIC %s
+// GENERIC-NOT: warning:
+// GENERIC: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=generic,kind=sycl"{{$}}
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 718aaa5fdbf8a9..ab126433636e98 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -125,6 +125,16 @@ __attribute__((visibility("protected"), used)) int x;
// 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
+// RUN: llvm-offload-binary -o %t.sycl-noembed.out \
+// RUN: --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,no-embed-virtual-arch=1
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-noembed.o -fembed-offload-object=%t.sycl-noembed.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld %t.sycl-noembed.o %t.sycl-noembed.o \
+// RUN: %t.sycl-generic.a -o a.out 2>&1 \
+// RUN: | FileCheck %s --check-prefix=SYCL-NO-EMBED
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld %t.sycl-noembed.o %t.sycl-u.o -o a.out 2>&1 \
+// RUN: | FileCheck %s --check-prefix=SYCL-SOME-NO-EMBED
// 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
@@ -169,6 +179,13 @@ __attribute__((visibility("protected"), used)) int x;
// The replayed extraction of an image without "code" does not also match one
// with "code" for the same architecture.
+// The SPIR-V is dropped only if all device images that list Intel GPUs ask
+// for it. A generic archive member does not count.
+// SYCL-NO-EMBED-NOT: warning:
+// SYCL-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --device=xe-mtl-u -Xlinker --no-embed-virtual-arch{{$}}
+// SYCL-SOME-NO-EMBED: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the SPIR-V is kept
+// SYCL-SOME-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --device=xe-mtl-u{{$}}
+
// 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 \
diff --git a/clang/test/OffloadTools/clang-sycl-linker/basic.ll b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
index 1095cd227f05b1..56b1e94326e66e 100644
--- a/clang/test/OffloadTools/clang-sycl-linker/basic.ll
+++ b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
@@ -134,12 +134,23 @@
; AOT-IGCA-NEXT: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: igca_30r
; AOT-IGCA-NOT: {{.+}}
;
+; Test that --no-embed-virtual-arch drops the SPIR-V from the ocloc output and
+; is recorded on the device image.
+; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --device=xe-mtl-u --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/aot-igca-noembed.out 2>&1 \
+; RUN: | FileCheck %s --check-prefix=AOT-IGCA-NO-EMBED
+; AOT-IGCA-NO-EMBED: "{{.*}}ocloc{{.*}}" {{.*}}-device xe-mtl-u -exclude_ir -output
+; AOT-IGCA-NO-EMBED-NEXT: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: igca_30r, no-embed-virtual-arch{{$}}
+;
+; Test that --no-embed-virtual-arch has no effect without --device.
+; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/jit-igca-noembed.out 2>&1 \
+; RUN: | FileCheck %s --check-prefix=JIT-IGCA
+;
; 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-NEXT: sycl-bundle: image kind: spv, triple: spirv64-unknown-unknown, arch: igca_30r{{$}}
; JIT-IGCA-NOT: {{.+}}
;
; Test AOT compilation for an Intel CPU.
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 02905065c40205..af42e3eadf23ce 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1103,6 +1103,26 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_device_EQ),
Args.MakeArgString(Device));
+ // Drop the SPIR-V from the ahead-of-time compiled image only if every image
+ // that lists Intel GPUs asks for it. Generic archive members list none.
+ if (Binary.getOffloadKind() == OFK_SYCL) {
+ auto HasKey = [](StringRef Key) {
+ return [=](const OffloadFile &File) {
+ return !File.getBinary()->getString(Key).empty();
+ };
+ };
+ size_t WithCode = llvm::count_if(Input, HasKey("code"));
+ size_t NoEmbed = llvm::count_if(Input, HasKey("no-embed-virtual-arch"));
+ if (WithCode && NoEmbed == WithCode)
+ DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_linker_arg_EQ),
+ "--no-embed-virtual-arch");
+ else if (NoEmbed)
+ WithColor::warning(errs(), LinkerExecutable)
+ << "not all device code for '" << Binary.getArch()
+ << "' was compiled with --no-offload-embed-virtual-arch; the "
+ "SPIR-V is kept\n";
+ }
+
// 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) {
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 314913a829e925..e9f82de36bbf66 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -722,6 +722,8 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
assert(!Devices.empty() && "a device must be specified for AOT compilation");
CmdArgs.push_back("-device");
CmdArgs.push_back(Devices);
+ if (Args.hasArg(OPT_no_embed_virtual_arch))
+ CmdArgs.push_back("-exclude_ir");
// getAllArgValues returns a temporary vector; retain it so the StringRefs
// remain valid through the executeCommands call below.
@@ -1007,6 +1009,9 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
bool IsAOTCompileNeeded =
OA.isIntelIGCA() ? Args.hasArg(OPT_device_EQ) : OA.isIntel();
+ // ocloc drops the SPIR-V for an Intel GPU; opencl-aot for a CPU keeps it.
+ bool NoEmbedVirtualArch = IsAOTCompileNeeded && !OA.isIntelCPU() &&
+ Args.hasArg(OPT_no_embed_virtual_arch);
StringRef OutputFileNameExt = ".spv";
@@ -1056,16 +1061,22 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
TheImage.StringData["arch"] =
Args.MakeArgString(Args.getLastArgValue(OPT_arch_EQ));
TheImage.StringData["symbols"] = SI.Symbols;
+ if (NoEmbedVirtualArch)
+ TheImage.StringData["no-embed-virtual-arch"] = "1";
TheImage.Image = std::move(*FileOrErr);
Images.emplace_back(std::move(TheImage));
}
if (Verbose) {
- for (const OffloadingImage &Image : Images)
- errs() << formatv(
- "sycl-bundle: image kind: {0}, triple: {1}, arch: {2}\n",
- getImageKindName(Image.TheImageKind),
- Image.StringData.lookup("triple"), Image.StringData.lookup("arch"));
+ for (const OffloadingImage &Image : Images) {
+ errs() << formatv("sycl-bundle: image kind: {0}, triple: {1}, arch: {2}",
+ getImageKindName(Image.TheImageKind),
+ Image.StringData.lookup("triple"),
+ Image.StringData.lookup("arch"));
+ if (Image.StringData.count("no-embed-virtual-arch"))
+ errs() << ", no-embed-virtual-arch";
+ errs() << "\n";
+ }
}
llvm::SmallString<0> Buffer = OffloadBinary::write(Images);
diff --git a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
index db81c494be0527..98b2acc993afaf 100644
--- a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
+++ b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
@@ -51,6 +51,10 @@ def device_EQ : Joined<["--", "-"], "device=">,
MetaVarName<"<device>">,
HelpText<"An Intel GPU to compile ahead of time for. Can be "
"repeated. Defaults to --arch for an Intel GPU">;
+def no_embed_virtual_arch : Flag<["--", "-"], "no-embed-virtual-arch">,
+ Flags<[LinkerOnlyOption]>,
+ HelpText<"Do not keep the SPIR-V in a device image "
+ "compiled ahead of time for an Intel GPU">;
def triple_EQ : Joined<["--", "-"], "triple=">,
Flags<[LinkerOnlyOption]>,
MetaVarName<"<triple>">,
>From 25a6b9d9af0b29bd1812541cfb6d337dfe456553 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 13 Nov 2026 12:17:29 -0800
Subject: [PATCH 17/51] [Clang][SYCL] Address review: rename --device to
--code, make "code" generic
Rename the --device option of clang-linker-wrapper and clang-sycl-linker to
--code, to match the "code" metadata, and describe it as a real device.
Filter replayed extractions on "code" for every offload kind. Move the merged
offload binary instead of copying it. Rename getSYCLDeviceArch to
getSYCLVirtualArch, clarify comments and docs, and add tests for an IGCA
target given together with one of its GPUs and for an unused -Xarch_.
---
clang/docs/ClangLinkerWrapper.md | 10 ++--
clang/docs/ClangSYCLLinker.md | 17 +++++--
clang/include/clang/Driver/Compilation.h | 5 +-
clang/lib/Driver/Driver.cpp | 3 +-
clang/lib/Driver/ToolChains/Clang.cpp | 7 +--
clang/lib/Driver/ToolChains/SYCL.cpp | 2 +-
clang/lib/Driver/ToolChains/SYCL.h | 7 +--
clang/test/Driver/sycl-igca-link.cpp | 6 +--
clang/test/Driver/sycl-intel-gpu-arch.cpp | 15 +++++-
.../linker-wrapper-verbose.c | 8 +--
.../clang-linker-wrapper/linker-wrapper.c | 16 +++---
.../OffloadTools/clang-sycl-linker/basic.ll | 6 +--
.../ClangLinkerWrapper.cpp | 51 ++++++++++---------
.../clang-linker-wrapper/LinkerWrapperOpts.td | 6 +--
.../clang-sycl-linker/ClangSYCLLinker.cpp | 11 ++--
clang/tools/clang-sycl-linker/SYCLLinkOpts.td | 12 +++--
16 files changed, 103 insertions(+), 79 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index df2a946080e0c3..598f89d15ad347 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -78,14 +78,14 @@ that it can be linked later.
## Matching
The linker wrapper will link extracted device code that is compatible with each
-other. Generally, this requires that the target triple and architecture match.
+other. Generally, this requires that the target triple, the architecture and
+the optional `code` list 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`.
+The `code` list is only needed when the architecture is a virtual one that
+several devices share, and it names the real devices to compile the device code
+for ahead of time.
## Debugging
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index 6c156ca2540178..a137227b99f042 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -37,7 +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.
+ --code=<code> Specify a real device to compile ahead of time for. Can be repeated. Without it, a real --arch is compiled ahead of time and a virtual one is kept for just-in-time compilation.
--dry-run Print generated commands without running.
-help-hidden Display all available options
-help Display available options (--help-hidden for more)
@@ -109,12 +109,19 @@ 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:
+### Specifying a virtual architecture and real devices
+
+`--arch` can name a virtual architecture, which several real devices share.
+`--code` then names the real devices to compile the device code for ahead of
+time.
+
+For example, for an IGCA target, such as `igca_30r`, the device image is
+compiled ahead of time for the Intel GPUs given with `--code`, with a single
+ocloc call, or, if no `--code` is given, kept as SPIR-V to be compiled just in
+time by the runtime:
```console
-clang-sycl-linker --triple spirv64 --arch igca_30r --device=xe-mtl-u --device=xe-mtl-h input.bc
+clang-sycl-linker --triple spirv64 --arch igca_30r --code=xe-mtl-u --code=xe-mtl-h input.bc
```
### Linking with Device Libraries
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index c37075dcb9ad57..5c52c608724cc1 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -98,8 +98,9 @@ class Compilation {
};
std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
- /// 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.
+ /// For SYCL, maps a device toolchain and an IGCA target, e.g. igca_30r, to
+ /// the Intel GPUs from --offload-arch that share that target, e.g. xe-mtl-h
+ /// and xe-mtl-u. Each list is sorted and has no duplicates.
llvm::DenseMap<std::pair<const ToolChain *, StringRef>,
llvm::SmallVector<StringRef, 2>>
SYCLIGCADevices;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 6660e9e45adc9d..bf1c7a5ee0a4f7 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4157,9 +4157,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
if (Kind == Action::OFK_SYCL) {
llvm::DenseSet<StringRef> Targets;
for (StringRef Arch : Archs) {
- StringRef Target = toolchains::getSYCLDeviceArch(Arch);
+ StringRef Target = toolchains::getSYCLVirtualArch(Arch);
Targets.insert(Target);
- // An Intel GPU maps to a different name, its IGCA target.
if (Target != Arch)
C.addSYCLIGCADevice(&TC, Target, Arch);
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 52a6dc2c7d54f0..98b37ec29a677d 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9761,8 +9761,9 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
"kind=" + Kind.str(),
};
- // 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".
+ // Add one "code=" per Intel GPU requested for this 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());
@@ -9966,7 +9967,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
// A SYCL device image for an Intel GPU is labeled with the GPU's IGCA
// target.
if (Kind == Action::OFK_SYCL)
- Name = toolchains::getSYCLDeviceArch(Name);
+ Name = toolchains::getSYCLVirtualArch(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));
diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp
index 3d38f319e91c0c..b79f7664816800 100644
--- a/clang/lib/Driver/ToolChains/SYCL.cpp
+++ b/clang/lib/Driver/ToolChains/SYCL.cpp
@@ -109,7 +109,7 @@ static ArrayRef<options::ID> getUnsupportedOpts() {
return UnsupportedOpts;
}
-StringRef toolchains::getSYCLDeviceArch(StringRef Arch) {
+StringRef toolchains::getSYCLVirtualArch(StringRef Arch) {
OffloadArch ID = StringToOffloadArch(Arch);
return ID.isIntelGPU() ? OffloadArchToString(ID.intelGPUIGCATarget()) : Arch;
}
diff --git a/clang/lib/Driver/ToolChains/SYCL.h b/clang/lib/Driver/ToolChains/SYCL.h
index 135efb1d940c44..13a2109b2c42ff 100644
--- a/clang/lib/Driver/ToolChains/SYCL.h
+++ b/clang/lib/Driver/ToolChains/SYCL.h
@@ -70,9 +70,10 @@ class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
SYCLInstallationDetector SYCLInstallation;
};
-/// \return the name SYCL device code for \p Arch is compiled for: the IGCA
-/// target of an Intel GPU, e.g. "igca_30r" for "xe-mtl-u", or \p Arch itself.
-StringRef getSYCLDeviceArch(StringRef Arch);
+/// \return the architecture to compile SYCL device code for, given \p Arch
+/// from --offload-arch: the IGCA target if \p Arch is an Intel GPU, e.g.
+/// "igca_30r" for "xe-mtl-u", and \p Arch itself otherwise.
+StringRef getSYCLVirtualArch(StringRef Arch);
} // end namespace toolchains
diff --git a/clang/test/Driver/sycl-igca-link.cpp b/clang/test/Driver/sycl-igca-link.cpp
index d46739f5ae4bf2..6501919c004d4a 100644
--- a/clang/test/Driver/sycl-igca-link.cpp
+++ b/clang/test/Driver/sycl-igca-link.cpp
@@ -18,7 +18,7 @@
// 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: clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-h -Xlinker --code=xe-mtl-u{{$}}
// SAME-DEVICES-NOT: --sycl-link
/// An object compiled for xe-mtl-u only has a different list of devices, so its
@@ -26,5 +26,5 @@
// 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{{$}}
+// DIFFERENT-DEVICES-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-u{{$}}
+// DIFFERENT-DEVICES-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-h -Xlinker --code=xe-mtl-u{{$}}
diff --git a/clang/test/Driver/sycl-intel-gpu-arch.cpp b/clang/test/Driver/sycl-intel-gpu-arch.cpp
index 5ec00a631b5a8c..8badd283f6ef3a 100644
--- a/clang/test/Driver/sycl-intel-gpu-arch.cpp
+++ b/clang/test/Driver/sycl-intel-gpu-arch.cpp
@@ -1,6 +1,6 @@
/// Tests for Intel GPU names in --offload-arch.
-/// An Intel GPU is compiled for its IGCA target, e.g. xe-bmg-g21 for igca_40r.
+/// An Intel GPU is compiled for its IGCA target, e.g. igca_40r for xe-bmg-g21.
// 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 \
@@ -27,6 +27,18 @@
// RUN: | FileCheck -check-prefixes=CHK-SORTED %s
// 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 together with one of its Intel GPUs gives one
+/// device compile, which lists that GPU.
+// RUN: %clang -ccc-print-phases --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=igca_30r,xe-mtl-u -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes=CHK-MIXED-PHASES %s
+// CHK-MIXED-PHASES: offload, "device-sycl (spirv64-unknown-unknown:igca_30r)"
+// CHK-MIXED-PHASES-NOT: device-sycl (spirv
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
+// RUN: --offload-arch=igca_30r,xe-mtl-u -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes=CHK-MIXED %s
+// CHK-MIXED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,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 \
@@ -39,6 +51,7 @@
// 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: warning: argument unused during compilation: '-Xarch_xe-pvc -DBAR'
// CHK-XARCH-NOT: "-D" "BAR"
// CHK-XARCH: "-cc1" "-triple" "spirv64-unknown-unknown"{{.*}} "-D" "FOO"
// CHK-XARCH-NOT: "-D" "BAR"
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 d822c4043c9332..7e6e75e30ef862 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
@@ -12,7 +12,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=OPENMP
-// OPENMP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,file={{.*}}.o --image=kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx90a,file={{.*}}.o
+// OPENMP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,file={{.*}}.o --image=kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx90a,code=,file={{.*}}.o
// OPENMP: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70
// OPENMP: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx90a
// OPENMP: llvm-offload-binary{{.*}} -o {{.*}}.offload --image=file={{.*}}.img,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70
@@ -41,7 +41,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=CUDA
-// CUDA: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_70,file={{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_52,file={{.*}}.o
+// CUDA: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,file={{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_52,code=,file={{.*}}.o
// CUDA: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70
// CUDA: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_52
// CUDA: fatbinary{{.*}}--create [[FB:.*]].fatbin {{.*}}--image3=kind=elf,sm=70{{.*}}--image3=kind=elf,sm=52
@@ -58,7 +58,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=HIP
-// HIP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx90a,file={{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx908,file={{.*}}.o
+// HIP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx90a,code=,file={{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx908,code=,file={{.*}}.o
// HIP: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx90a
// HIP: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx908
// HIP: clang-offload-bundler{{.*}}-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a,hip-amdgcn-amd-amdhsa--gfx908{{.*}}-output=[[FB:.*]].hipfb
@@ -91,4 +91,4 @@
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --wrapper-verbose --save-temps --dry-run \
// RUN: --should-extract=sm_70 --linker-path=/usr/bin/ld %t.a -o a.out 2>&1 | FileCheck %s --check-prefix=ARCHIVE
-// ARCHIVE: llvm-offload-binary{{.*}} {{.*}}.a --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,member={{.*}}.o,file={{.*}}.o
+// ARCHIVE: llvm-offload-binary{{.*}} {{.*}}.a --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,member={{.*}}.o,file={{.*}}.o
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 718aaa5fdbf8a9..50514d98113a5b 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -126,25 +126,25 @@ __attribute__((visibility("protected"), used)) int x;
// 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: clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --code=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{{$}}
+// SYCL-DIFFERENT-CODE-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-u{{$}}
+// SYCL-DIFFERENT-CODE-DAG: clang{{.*}} -march=igca_30r {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --code=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{{$}}
+// SYCL-SAVE-TEMPS-DAG: clang{{.*}} -o a.out.spirv64.igca_30r.xe-mtl-u.img {{.*}}--code=xe-mtl-u{{$}}
+// SYCL-SAVE-TEMPS-DAG: clang{{.*}} -o a.out.spirv64.igca_30r.xe-mtl-u.xe-mtl-h.img {{.*}}--code=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{{$}}
+// SYCL-GENERIC-ARCHIVE: clang{{.*}} -march=igca_30r {{.*}}igca_30r{{.*}}.o {{.*}}generic{{.*}}.o --sycl-link -Xlinker --code=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{{$}}
+// SYCL-EXTRACT-DAG: clang{{.*}} -march=igca_30r {{.*}}-igca_30r.xe-mtl-u.o --sycl-link -Xlinker --code=xe-mtl-u{{$}}
+// SYCL-EXTRACT-DAG: clang{{.*}} -march=igca_30r {{.*}}-igca_30r.xe-mtl-u.xe-mtl-h.o --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --code=xe-mtl-h{{$}}
// The device images of SYCL images that are linked separately end up in a
// single offload binary.
diff --git a/clang/test/OffloadTools/clang-sycl-linker/basic.ll b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
index 1095cd227f05b1..a7dac710891fd6 100644
--- a/clang/test/OffloadTools/clang-sycl-linker/basic.ll
+++ b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
@@ -125,8 +125,8 @@
; 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 \
+; --code list.
+; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --code=xe-mtl-u --code=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
@@ -134,7 +134,7 @@
; 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.
+; Test that an IGCA target without --code is kept as SPIR-V.
; 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
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 02905065c40205..a6a4efcc48c5f4 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -45,6 +45,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
+#include "llvm/Support/SmallVectorMemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/TargetSelect.h"
@@ -504,9 +505,9 @@ 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);
+ // Images with the same architecture but different "code" are linked
+ // separately, so the real devices are part of the name.
+ std::vector<std::string> Devices = Args.getAllArgValues(OPT_code_EQ);
for (const std::string &Device : Devices)
OutputFileBase += "." + Device;
auto TempFileOrErr = createOutputFile(ExecutableName + OutputFileBase, "img");
@@ -597,9 +598,9 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
if (ActiveOffloadKindMask & OFK_SYCL)
CmdArgs.push_back("--sycl-link");
- // Pass the devices on to clang-sycl-linker.
+ // Pass the real devices on to clang-sycl-linker.
for (const std::string &Device : Devices)
- CmdArgs.append({"-Xlinker", Args.MakeArgString("--device=" + Device)});
+ CmdArgs.append({"-Xlinker", Args.MakeArgString("--code=" + Device)});
for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
CmdArgs.append({"-Xlinker", Args.MakeArgString(Arg)});
@@ -937,8 +938,9 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
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.
+ // Images for different IGCA targets or with different "code" are linked
+ // separately, but the SYCL runtime registers one offload binary per wrapper,
+ // so merge their device images into one.
SmallVector<std::unique_ptr<OffloadBinary>> Binaries;
SmallVector<OffloadingImage> Merged;
for (const OffloadingImage &Image : Images) {
@@ -999,7 +1001,8 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
}
Identifier = *FileOrErr;
}
- Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(Data, Identifier));
+ Buffers.emplace_back(std::make_unique<SmallVectorMemoryBuffer>(
+ std::move(Data), Identifier, /*RequiresNullTerminator=*/false));
return std::move(Buffers);
}
@@ -1094,13 +1097,13 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_triple_EQ),
Args.MakeArgString(Input.front().getBinary()->getTriple()));
- // Get the Intel GPUs that a SYCL image lists in "code". clang-sycl-linker
+ // Get the real devices 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"), ','))
if (!Device.empty())
- DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_device_EQ),
+ DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
Args.MakeArgString(Device));
// If every input file is bitcode we have whole program visibility as we
@@ -1266,9 +1269,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))
+ if (LinkerArgs.hasArg(OPT_code_EQ))
TheImage.StringData["code"] = Args.MakeArgString(
- llvm::join(LinkerArgs.getAllArgValues(OPT_device_EQ), ","));
+ llvm::join(LinkerArgs.getAllArgValues(OPT_code_EQ), ","));
TheImage.Image = std::move(*FileOrErr);
Images[Kind].emplace_back(std::move(TheImage));
@@ -1427,16 +1430,14 @@ Error emitExtractCommands(
.str();
if (!Binary.getArch().empty())
ImageArg += (",arch=" + Binary.getArch()).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();
- }
+ // Filter on "code", including an empty one, so that an image without
+ // "code" does not also match one with it for the same architecture.
+ 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();
@@ -1550,9 +1551,9 @@ getDeviceInput(const ArgList &Args) {
B.getBinary()->getArch().count(':');
});
- // 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".
+ // An image can list real devices in "code", e.g. "code=xe-mtl-h,xe-mtl-u" for
+ // Intel GPUs. 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")};
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index d045b02428adfa..4e4aa7df8890ab 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -80,9 +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 code_EQ : Joined<["--"], "code=">,
+ Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<code>">,
+ HelpText<"A real device to compile an 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 314913a829e925..324e605a79b730 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -714,9 +714,9 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
CmdArgs.push_back("-output_no_suffix");
CmdArgs.push_back("-spirv_input");
- // 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), ",");
+ // Compile for the --code 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_code_EQ), ",");
if (Devices.empty())
Devices = Args.getLastArgValue(OPT_arch_EQ);
assert(!Devices.empty() && "a device must be specified for AOT compilation");
@@ -1002,11 +1002,10 @@ 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.
+ // An IGCA target is compiled ahead of time only for its --code list.
OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
bool IsAOTCompileNeeded =
- OA.isIntelIGCA() ? Args.hasArg(OPT_device_EQ) : OA.isIntel();
+ OA.isIntelIGCA() ? Args.hasArg(OPT_code_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 db81c494be0527..e2b0d66e4eb33e 100644
--- a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
+++ b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
@@ -46,11 +46,13 @@ 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 code_EQ : Joined<["--", "-"], "code=">,
+ Flags<[LinkerOnlyOption]>,
+ MetaVarName<"<code>">,
+ HelpText<"Specify a real device to compile ahead of time for. "
+ "Can be repeated. Without it, a real --arch is compiled "
+ "ahead of time and a virtual one is kept for "
+ "just-in-time compilation.">;
def triple_EQ : Joined<["--", "-"], "triple=">,
Flags<[LinkerOnlyOption]>,
MetaVarName<"<triple>">,
>From b0fda271560f8aa60c342caab8e0456ebb359d4b Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 13 Nov 2026 17:43:43 -0800
Subject: [PATCH 18/51] [Clang][SYCL] Address review: make the
clang-sycl-linker options list match --help
---
clang/docs/ClangSYCLLinker.md | 48 ++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 21 deletions(-)
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index a137227b99f042..607ab13c10d174 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -30,31 +30,37 @@ be passed down to downstream AOT compilation tools like 'ocloc' and 'opencl-aot'
```console
OVERVIEW: A utility that wraps around the SYCL device code linking process.
-This enables LLVM IR linking, post-linking and code generation for SPIR-V
-JIT and AOT targets.
+This enables LLVM IR linking, post-linking and code generation for SPIR-V JIT and AOT targets.
USAGE: clang-sycl-linker [options] <input bitcode files>
OPTIONS:
- --arch <value> Specify the name of the target architecture.
- --code=<code> Specify a real device to compile ahead of time for. Can be repeated. Without it, a real --arch is compiled ahead of time and a virtual one is kept for just-in-time compilation.
- --dry-run Print generated commands without running.
- -help-hidden Display all available options
- -help Display available options (--help-hidden for more)
- -L <dir> Add <dir> to the library search path
- -l <libname> Search for library <libname>
- --whole-archive Include all archive members in the link
- --no-whole-archive Only include archive members that resolve undefined symbols (default)
- -u <symbol> Force undefined symbol during linking
- --module-split-mode=<mode> Module split mode: 'translation_unit' (default), 'kernel', or 'link_unit'
- --ocloc-options=<value> Options passed to ocloc for Intel GPU AOT compilation
- --opencl-aot-options=<value> Options passed to opencl-aot for Intel CPU AOT compilation
- -o <path> Path to file to write output
- --save-temps Save intermediate results
- --triple <value> Specify the target triple.
- --version Display the version number and exit
- -v Print verbose information
- -spirv-dump-device-code=<dir> Directory to dump SPIR-V IR code into
+ --arch=<arch> The device architecture
+ --code=<code> Specify a real device to compile ahead of time for. Can be repeated. Without it, a real --arch is compiled ahead of time and a virtual one is kept for just-in-time compilation.
+ --dry-run Print generated commands without running.
+ -help-hidden Display all available options
+ -help Display available options (--help-hidden for more)
+ -L <dir> Add <dir> to the library search path
+ -l <libname> Search for library <libname>
+ --module-split-mode=<mode>
+ SYCL device code module split mode. Valid values:
+ - 'translation_unit' (default) emits one device image per translation unit that contains at least one entry point;
+ - 'kernel' emits one device image per kernel function;
+ - 'link_unit' emits one device image per linking unit.
+ --no-whole-archive Disable --whole-archive for the following inputs (default state)
+ --ocloc-options=<value> Options passed to ocloc for Intel GPU AOT compilation
+ --opencl-aot-options=<value>
+ Options passed to opencl-aot for Intel CPU AOT compilation
+ -o <path> Path to file to write output
+ --print-linked-module Print the linked module's IR for testing
+ --save-temps Save intermediate results
+ --spirv-dump-device-code=<value>
+ Path to the folder where the tool dumps SPIR-V device code. Other formats aren't dumped.
+ --triple=<triple> The device target triple
+ -u <symbol> Force undefined symbol during linking
+ --version Display the version number and exit
+ -v Print verbose information
+ --whole-archive Include all archive members in the link
```
## Library Linking
>From 91ea16b08ba52636be84aa1d7455b3a05db7e1b2 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 13 Nov 2026 18:22:48 -0800
Subject: [PATCH 19/51] [Clang][SYCL] Expect an empty code= filter for OpenACC
images too
---
.../OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 cbd232be0dc81a..0a5fcf208ba9ca 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
@@ -40,7 +40,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=OPENACC
-// OPENACC: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openacc,triple=nvptx64-nvidia-cuda,arch=sm_70,file={{.*}}.o
+// OPENACC: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openacc,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,file={{.*}}.o
// OPENACC: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70
// OPENACC: llvm-offload-binary{{.*}} -o {{.*}}.offload --image=file={{.*}}.img,kind=openacc,triple=nvptx64-nvidia-cuda,arch=sm_70
// OPENACC: llvm-offload-wrapper{{.*}} --kind=openacc --triple=x86_64-unknown-linux-gnu -o [[BC:.*]].bc {{.*}}.offload
>From 27c0ef01e12c5a7856bcca2782f7a49d7d672f14 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Mon, 16 Nov 2026 20:01:44 -0800
Subject: [PATCH 20/51] [Clang][SYCL] Address review: make "code" part of
OffloadFile::TargetID
Move the "code" check from clang-linker-wrapper into areTargetsEquivalent
and areTargetsCompatible. OffloadFile::TargetID is now a struct with Triple,
Arch and Code instead of a std::pair.
---
.../clang-linker-wrapper/linker-wrapper.c | 9 +++
.../ClangLinkerWrapper.cpp | 29 +++-----
llvm/include/llvm/Object/OffloadBinary.h | 67 +++++++++++++++----
llvm/lib/Object/OffloadBinary.cpp | 40 +++++++----
llvm/unittests/Object/OffloadingTest.cpp | 30 +++++++++
5 files changed, 129 insertions(+), 46 deletions(-)
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 1ca141c2a790a8..649aaff9fae6ad 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -92,6 +92,11 @@ __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: rm -f %t.sycl-u.a
+// RUN: llvm-ar rcs %t.sycl-u.a %t.sycl-u.o
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld %t.sycl-generic.o %t.sycl-u.a -o a.out 2>&1 \
+// RUN: | FileCheck %s --check-prefix=SYCL-CODE-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
@@ -140,6 +145,10 @@ __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 --code=xe-mtl-u{{$}}
+// An archive member that lists real devices is not linked into a generic group.
+// SYCL-CODE-ARCHIVE: clang{{.*}} --target=spirv64-unknown-unknown {{[^ ]*}}generic{{[^ ]*}}.o --sycl-link{{$}}
+// SYCL-CODE-ARCHIVE-NOT: --sycl-link
+
// 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
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index d3e6af29311809..f7ce333e4d9a51 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1557,25 +1557,16 @@ getDeviceInput(const ArgList &Args) {
B.getBinary()->getArch().count(':');
});
- // An image can list real devices in "code", e.g. "code=xe-mtl-h,xe-mtl-u" for
- // Intel GPUs. 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")};
- };
-
// Link all standard input files and update the list of symbols.
- MapVector<ImageID, SmallVector<OffloadFile, 0>> InputFiles;
+ MapVector<OffloadFile::TargetID, SmallVector<OffloadFile, 0>> InputFiles;
for (OffloadFile &Binary : ObjectFilesToExtract) {
if (!Binary.getBinary())
continue;
- ImageID Target = GetImageID(Binary);
- SmallVector<ImageID> CompatibleTargets;
+ OffloadFile::TargetID Target = Binary;
+ SmallVector<OffloadFile::TargetID> CompatibleTargets;
for (const auto &[ID, Input] : InputFiles)
- if (Target.second == ID.second &&
- object::areTargetsEquivalent(Target.first, ID.first))
+ if (object::areTargetsEquivalent(Target, ID))
CompatibleTargets.emplace_back(ID);
// Seed a new image when no existing target can provide for this input.
@@ -1602,20 +1593,16 @@ 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};
+ SmallVector<OffloadFile::TargetID> CompatibleTargets = {Binary};
for (const auto &[ID, Input] : InputFiles)
- if (Target != ID && (IsGeneric || Target.second == ID.second) &&
- object::areTargetsCompatible(Target.first, ID.first))
+ if (OffloadFile::TargetID(Binary) != ID &&
+ object::areTargetsCompatible(Binary, ID))
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.first.second))
+ if (!InputFiles.count(ID) && !ShouldExtract.contains(ID.Arch))
continue;
// If another target needs this binary it must be copied instead.
diff --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 79ed747ba4c2d6..2a569be8a81c9a 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -18,6 +18,8 @@
#define LLVM_OBJECT_OFFLOADBINARY_H
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
@@ -218,7 +220,26 @@ class OffloadBinary : public Binary {
/// A handle to a single OffloadBinary.
class OffloadFile {
public:
- using TargetID = std::pair<StringRef, StringRef>;
+ /// The target a device image is built for. Device images with the same
+ /// target are linked together.
+ struct TargetID {
+ /// The target triple, e.g. "spirv64-unknown-unknown".
+ StringRef Triple;
+ /// The architecture, e.g. "sm_70" or "igca_30r".
+ StringRef Arch;
+ /// The real devices to compile for ahead of time, e.g. "xe-mtl-h,xe-mtl-u".
+ /// Empty if the device image lists none.
+ StringRef Code;
+
+ TargetID() = default;
+ TargetID(StringRef Triple, StringRef Arch, StringRef Code = "")
+ : Triple(Triple), Arch(Arch), Code(Code) {}
+
+ bool operator==(const TargetID &Other) const {
+ return Triple == Other.Triple && Arch == Other.Arch && Code == Other.Code;
+ }
+ bool operator!=(const TargetID &Other) const { return !(*this == Other); }
+ };
OffloadFile() = default;
OffloadFile(std::unique_ptr<OffloadBinary> Binary)
@@ -237,10 +258,11 @@ class OffloadFile {
return OffloadFile(std::move((*NewBinaryOrErr)[0]));
}
- /// We use the Triple and Architecture pair to group linker inputs together.
- /// This conversion function lets us use these inputs in a hash-map.
+ /// We use the target to group device images together. This conversion
+ /// function lets us use them in a hash-map.
operator TargetID() const {
- return std::make_pair(getBinary()->getTriple(), getBinary()->getArch());
+ return {getBinary()->getTriple(), getBinary()->getArch(),
+ getBinary()->getString("code")};
}
private:
@@ -264,12 +286,22 @@ LLVM_ABI OffloadKind getOffloadKind(StringRef Name);
/// Convert an offload kind to its string representation.
LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
-/// Returns true if an image built for target \p Provided can provide the device
-/// code for a request for target \p Requested. This is directional: a
-/// feature-unspecified or generic image serves a more specific request (e.g. a
-/// generic static-archive member pulled into a specific device-image group),
-/// but not vice versa. For AMDGPU a target id is a string conforming to the
-/// following BNF syntax:
+/// Returns true if the device image \p Provided, e.g. a static archive member,
+/// can be linked into the group of device images \p Requested.
+///
+/// - A generic device image can be linked into any group.
+/// - Otherwise, both must list the same real devices in "code", and have the
+/// same architecture unless the group is generic.
+///
+/// Device image to add Group to link it into Result
+/// (Provided) (Requested)
+/// generic igca_30r, code=xe-mtl-u true
+/// igca_30r generic true
+/// igca_30r, code=xe-mtl-u igca_30r, code=xe-mtl-u true
+/// igca_30r, code=xe-mtl-u igca_30r, code=xe-mtl-h,xe-mtl-u false
+/// igca_30r, code=xe-mtl-u generic false
+///
+/// For AMDGPU a target id is a string conforming to the following BNF syntax:
///
/// target-id ::= '<arch> ( : <feature> ( '+' | '-' ) )*'
///
@@ -279,12 +311,23 @@ LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
LLVM_ABI bool areTargetsCompatible(const OffloadFile::TargetID &Provided,
const OffloadFile::TargetID &Requested);
-/// Returns true if \p LHS and \p RHS denote the same logical target, i.e. the
-/// same processor and features.
+/// Returns true if device images built for \p LHS and \p RHS are linked
+/// together: they have the same processor and features, and list the same
+/// real devices in "code".
LLVM_ABI bool areTargetsEquivalent(const OffloadFile::TargetID &LHS,
const OffloadFile::TargetID &RHS);
} // namespace object
+template <> struct DenseMapInfo<object::OffloadFile::TargetID> {
+ using TargetID = object::OffloadFile::TargetID;
+ static unsigned getHashValue(const TargetID &ID) {
+ return hash_combine(ID.Triple, ID.Arch, ID.Code);
+ }
+ static bool isEqual(const TargetID &LHS, const TargetID &RHS) {
+ return LHS == RHS;
+ }
+};
+
} // namespace llvm
#endif
diff --git a/llvm/lib/Object/OffloadBinary.cpp b/llvm/lib/Object/OffloadBinary.cpp
index 67d1ba8e3387aa..3ced4758ea6d2a 100644
--- a/llvm/lib/Object/OffloadBinary.cpp
+++ b/llvm/lib/Object/OffloadBinary.cpp
@@ -540,40 +540,54 @@ StringRef object::getImageKindName(ImageKind Kind) {
bool object::areTargetsEquivalent(const OffloadFile::TargetID &LHS,
const OffloadFile::TargetID &RHS) {
- llvm::Triple LHSTT(LHS.first);
- llvm::Triple RHSTT(RHS.first);
+ // Device images that list different real devices are linked separately.
+ if (LHS.Code != RHS.Code)
+ return false;
+
+ llvm::Triple LHSTT(LHS.Triple);
+ llvm::Triple RHSTT(RHS.Triple);
// Check for logical AMDGPU target-id equivalence.
if (LHSTT.isAMDGPU()) {
- AMDGPU::TargetID LHSID(LHSTT, LHS.second);
- AMDGPU::TargetID RHSID(RHSTT, RHS.second);
+ AMDGPU::TargetID LHSID(LHSTT, LHS.Arch);
+ AMDGPU::TargetID RHSID(RHSTT, RHS.Arch);
return LHSID.isEquivalent(RHSID);
}
// For other targets the triples must be compatible and the arch must match.
- return LHSTT.isCompatibleWith(RHSTT) && LHS.second == RHS.second;
+ return LHSTT.isCompatibleWith(RHSTT) && LHS.Arch == RHS.Arch;
}
bool object::areTargetsCompatible(const OffloadFile::TargetID &Provided,
const OffloadFile::TargetID &Requested) {
- llvm::Triple ProvidedTT(Provided.first);
- llvm::Triple RequestedTT(Requested.first);
+ llvm::Triple ProvidedTT(Provided.Triple);
+ llvm::Triple RequestedTT(Requested.Triple);
// The AMDGPU target requires target-id aware checks (base processor plus
// xnack/sramecc features).
if (ProvidedTT.isAMDGPU()) {
- AMDGPU::TargetID ProvidedID(ProvidedTT, Provided.second);
- AMDGPU::TargetID RequestedID(RequestedTT, Requested.second);
- return ProvidedID.providesFor(RequestedID);
+ AMDGPU::TargetID ProvidedID(ProvidedTT, Provided.Arch);
+ AMDGPU::TargetID RequestedID(RequestedTT, Requested.Arch);
+ return Provided.Code == Requested.Code &&
+ ProvidedID.providesFor(RequestedID);
}
// For other targets the triples must be compatible.
if (!ProvidedTT.isCompatibleWith(RequestedTT))
return false;
- // If the architecture is "generic" we assume it is always compatible.
- if (Provided.second == "generic" || Requested.second == "generic")
+ // A generic device image can be linked into any group.
+ if (Provided.Arch == "generic")
+ return true;
+
+ // Otherwise, device images that list different real devices are linked
+ // separately.
+ if (Provided.Code != Requested.Code)
+ return false;
+
+ // A generic group accepts a device image of any architecture.
+ if (Requested.Arch == "generic")
return true;
- return Provided.second == Requested.second;
+ return Provided.Arch == Requested.Arch;
}
diff --git a/llvm/unittests/Object/OffloadingTest.cpp b/llvm/unittests/Object/OffloadingTest.cpp
index 78959f98301359..54c4da5257cb04 100644
--- a/llvm/unittests/Object/OffloadingTest.cpp
+++ b/llvm/unittests/Object/OffloadingTest.cpp
@@ -357,3 +357,33 @@ TEST(OffloadingTest, checkCompressedRoundTrip) {
EXPECT_EQ(Files[0].getBinary()->getImage(), ImageContent);
EXPECT_EQ(Files[1].getBinary()->getImage(), "SecondImage");
}
+
+TEST(OffloadingTest, checkTargetsWithCode) {
+ using TargetID = OffloadFile::TargetID;
+ TargetID U{"spirv64-unknown-unknown", "igca_30r", "xe-mtl-u"};
+ TargetID HU{"spirv64-unknown-unknown", "igca_30r", "xe-mtl-h,xe-mtl-u"};
+ TargetID NoCode{"spirv64-unknown-unknown", "igca_30r", ""};
+ TargetID Generic{"spirv64-unknown-unknown", "generic", ""};
+
+ // A target given without "code" lists no real devices.
+ EXPECT_EQ(TargetID("spirv64-unknown-unknown", "igca_30r"), NoCode);
+
+ // Device images that list the same real devices are linked together.
+ EXPECT_TRUE(areTargetsEquivalent(U, U));
+ EXPECT_TRUE(areTargetsCompatible(U, U));
+
+ // Device images that list different real devices are linked separately.
+ EXPECT_FALSE(areTargetsEquivalent(U, HU));
+ EXPECT_FALSE(areTargetsEquivalent(U, NoCode));
+ EXPECT_FALSE(areTargetsCompatible(U, HU));
+ EXPECT_FALSE(areTargetsCompatible(NoCode, U));
+
+ // A generic device image can be linked into device images that list real
+ // devices, but not the other way round.
+ EXPECT_TRUE(areTargetsCompatible(Generic, U));
+ EXPECT_FALSE(areTargetsCompatible(U, Generic));
+
+ // A device image that lists no real devices can be linked into generic
+ // device images.
+ EXPECT_TRUE(areTargetsCompatible(NoCode, Generic));
+}
>From a6eb2dfa24e5b5f71d8e2d6d4471baa26b844217 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 18 Nov 2026 11:47:47 -0800
Subject: [PATCH 21/51] [Clang][SYCL] Fix the clang-sycl-linker examples in
ClangSYCLLinker.md
Use the --triple= and --arch= spellings that clang-sycl-linker accepts, and
pass -o, which it requires.
---
clang/docs/ClangSYCLLinker.md | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index 607ab13c10d174..e84d24fdfb55f6 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -70,14 +70,14 @@ using `llvm-ar` and linked using the `-l` option:
```console
llvm-ar rc libdevice.a func1.bc func2.bc func3.bc
-clang-sycl-linker input.bc -l device -L /path/to/libs
+clang-sycl-linker input.bc -l device -L /path/to/libs -o output.bin
```
The linker supports standard archive library search semantics:
- `-l <name>` searches for `lib<name>.a` in the directories specified by `-L`
- `-l :<exact-name>` searches for the exact filename in the `-L` paths
-- Absolute paths can be passed as positional arguments: `clang-sycl-linker input.bc /path/to/libdevice.a`
+- Absolute paths can be passed as positional arguments: `clang-sycl-linker input.bc /path/to/libdevice.a -o output.bin`
By default, archive linking is **lazy** - only archive members (individual `.bc` files)
that resolve undefined symbols are extracted and linked. This happens at file
@@ -90,14 +90,14 @@ To force extraction of all archive members regardless of symbol resolution, use
`--whole-archive`:
```console
-clang-sycl-linker input.bc --whole-archive -l device --no-whole-archive -l other
+clang-sycl-linker input.bc --whole-archive -l device --no-whole-archive -l other -o output.bin
```
The `-u <symbol>` option can be used to force a symbol to be undefined, which
can trigger extraction of archive members that define that symbol:
```console
-clang-sycl-linker input.bc -u my_init_function -l device
+clang-sycl-linker input.bc -u my_init_function -l device -o output.bin
```
## Examples
@@ -112,7 +112,7 @@ linked device images that are ready to be wrapped and linked with host code to
generate the final executable.
```console
-clang-sycl-linker --triple spirv64 --arch xe-bmg-g21 input.bc
+clang-sycl-linker --triple=spirv64 --arch=xe-bmg-g21 input.bc -o output.bin
```
### Specifying a virtual architecture and real devices
@@ -127,7 +127,7 @@ ocloc call, or, if no `--code` is given, kept as SPIR-V to be compiled just in
time by the runtime:
```console
-clang-sycl-linker --triple spirv64 --arch igca_30r --code=xe-mtl-u --code=xe-mtl-h input.bc
+clang-sycl-linker --triple=spirv64 --arch=igca_30r --code=xe-mtl-u --code=xe-mtl-h input.bc -o output.bin
```
### Linking with Device Libraries
@@ -140,12 +140,12 @@ llvm-ar rc libmath.a sin.bc cos.bc tan.bc
llvm-ar rc libutils.a helper1.bc helper2.bc
# Link with lazy loading (only needed members extracted)
-clang-sycl-linker --triple spirv64 kernel.bc -l math -l utils -L /path/to/libs -o kernel.spv
+clang-sycl-linker --triple=spirv64 kernel.bc -l math -l utils -L /path/to/libs -o kernel.spv
# Force all members to be included from libmath.a
-clang-sycl-linker --triple spirv64 kernel.bc --whole-archive -l math --no-whole-archive -l utils -L /path/to/libs -o kernel.spv
+clang-sycl-linker --triple=spirv64 kernel.bc --whole-archive -l math --no-whole-archive -l utils -L /path/to/libs -o kernel.spv
# Use exact archive filename or absolute path
-clang-sycl-linker --triple spirv64 kernel.bc -l :libmath.a -L /path/to/libs -o kernel.spv
-clang-sycl-linker --triple spirv64 kernel.bc /absolute/path/libmath.a -o kernel.spv
+clang-sycl-linker --triple=spirv64 kernel.bc -l :libmath.a -L /path/to/libs -o kernel.spv
+clang-sycl-linker --triple=spirv64 kernel.bc /absolute/path/libmath.a -o kernel.spv
```
>From 42ff393c481a69e15f7044c239d28a836eb674d2 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 18 Nov 2026 13:36:56 -0800
Subject: [PATCH 22/51] [Clang][SYCL] Clarify the --no-embed-virtual-arch help
text, test it for an Intel CPU
--no-embed-virtual-arch has no effect for an Intel CPU, which is compiled
with opencl-aot rather than ocloc.
---
clang/docs/ClangSYCLLinker.md | 2 +-
clang/test/OffloadTools/clang-sycl-linker/basic.ll | 6 ++++++
clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp | 2 ++
clang/tools/clang-sycl-linker/SYCLLinkOpts.td | 4 ++--
4 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index 3ff5df9f0563a8..1d4db4d7e790d0 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -47,7 +47,7 @@ OPTIONS:
- 'translation_unit' (default) emits one device image per translation unit that contains at least one entry point;
- 'kernel' emits one device image per kernel function;
- 'link_unit' emits one device image per linking unit.
- --no-embed-virtual-arch Do not keep the SPIR-V in a device image compiled ahead of time for --code
+ --no-embed-virtual-arch Drop the SPIR-V from a device image compiled ahead of time for an Intel GPU
--no-whole-archive Disable --whole-archive for the following inputs (default state)
--ocloc-options=<value> Options passed to ocloc for Intel GPU AOT compilation
--opencl-aot-options=<value>
diff --git a/clang/test/OffloadTools/clang-sycl-linker/basic.ll b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
index 6af509a7e4581c..17dd232e479ab2 100644
--- a/clang/test/OffloadTools/clang-sycl-linker/basic.ll
+++ b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
@@ -153,6 +153,12 @@
; JIT-IGCA-NEXT: sycl-bundle: image kind: spv, triple: spirv64-unknown-unknown, arch: igca_30r{{$}}
; JIT-IGCA-NOT: {{.+}}
;
+; Test that --no-embed-virtual-arch has no effect for an Intel CPU.
+; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=graniterapids --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/aot-cpu-noembed.out 2>&1 \
+; RUN: | FileCheck %s --check-prefix=AOT-INTEL-CPU-NO-EMBED
+; AOT-INTEL-CPU-NO-EMBED-NOT: -exclude_ir
+; AOT-INTEL-CPU-NO-EMBED: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: graniterapids{{$}}
+;
; 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-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 93bfa90a4b574d..483c68b5b54f6c 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -722,6 +722,8 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
assert(!Devices.empty() && "a device must be specified for AOT compilation");
CmdArgs.push_back("-device");
CmdArgs.push_back(Devices);
+ // Without -exclude_ir, ocloc keeps the SPIR-V in its output next to the
+ // native code.
if (Args.hasArg(OPT_no_embed_virtual_arch))
CmdArgs.push_back("-exclude_ir");
diff --git a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
index 81c97ee61950ed..1e451a4572b4fa 100644
--- a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
+++ b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
@@ -55,8 +55,8 @@ def code_EQ : Joined<["--", "-"], "code=">,
"just-in-time compilation.">;
def no_embed_virtual_arch : Flag<["--", "-"], "no-embed-virtual-arch">,
Flags<[LinkerOnlyOption]>,
- HelpText<"Do not keep the SPIR-V in a device image "
- "compiled ahead of time for --code">;
+ HelpText<"Drop the SPIR-V from a device image "
+ "compiled ahead of time for an Intel GPU">;
def triple_EQ : Joined<["--", "-"], "triple=">,
Flags<[LinkerOnlyOption]>,
MetaVarName<"<triple>">,
>From 96dd5404fa704ede68ad41bc709e98f864b470d3 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 18 Nov 2026 13:44:54 -0800
Subject: [PATCH 23/51] [Clang][SYCL] Add a release note for
--[no-]offload-embed-virtual-arch
---
clang/docs/ReleaseNotes.md | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e805697aaee655..0a150e4ccf9d01 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1695,6 +1695,12 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
`--offload-arch` and `--no-offload-arch` must now name an Intel GPU or CPU, an
IGCA target or `generic`. Any other value, e.g. a misspelled `xe-pcv`, is an
error rather than a device job for a device that does not exist.
+- Added `--[no-]offload-embed-virtual-arch`. With
+ `--no-offload-embed-virtual-arch`, a SYCL device image compiled ahead of time
+ for Intel GPUs does not keep the SPIR-V for just-in-time compilation. To
+ apply it to one IGCA target only, use `-Xarch_<IGCA target>`, e.g.
+ `-Xarch_igca_40r --no-offload-embed-virtual-arch`; it then applies to all
+ Intel GPUs of that IGCA target.
#### Improvements
>From 3f62fcb79c0a03ed9b342366e45a7ddcf333639f Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 18 Nov 2026 14:32:49 -0800
Subject: [PATCH 24/51] [Clang][SYCL] Explain --no-offload-embed-virtual-arch
with examples in ClangLinkerWrapper.md
---
clang/docs/ClangLinkerWrapper.md | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index c918c99a28aa31..5c3be39a7420cc 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -86,10 +86,26 @@ cause it be linked with any other device code with the same target triple.
The `code` list is only needed when the architecture is a virtual one that
several devices share, and it names the real devices to compile the device code
for ahead of time.
-If all device code with a `code` list was compiled with
-`--no-offload-embed-virtual-arch`, the device image compiled ahead of time does
-not keep the virtual architecture code; if only some of it was, a warning is
-given and it is kept.
+
+`--no-offload-embed-virtual-arch` drops the SPIR-V from device code compiled
+ahead of time. It only takes effect if every object that is linked for the same
+GPUs was compiled with it:
+
+- If all of them were, the SPIR-V is dropped.
+- If only some were, the SPIR-V is kept, and a warning says so.
+
+```console
+# SPIR-V dropped: a.o and b.o were both compiled with --no-offload-embed-virtual-arch.
+clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c a.cpp -o a.o
+clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c b.cpp -o b.o
+clang++ -fsycl --offload-arch=xe-mtl-u a.o b.o -o app
+
+# SPIR-V kept, with a warning: only a.o was.
+clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c a.cpp -o a.o
+clang++ -fsycl --offload-arch=xe-mtl-u -c b.cpp -o b.o
+clang++ -fsycl --offload-arch=xe-mtl-u a.o b.o -o app
+clang-linker-wrapper: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the SPIR-V is kept
+```
## Debugging
>From 8bd73d1b7db26caa410fb8b1563a7fc331fa6c52 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 18 Nov 2026 14:58:39 -0800
Subject: [PATCH 25/51] [Clang][SYCL] Simplify the comments for
--[no-]offload-embed-virtual-arch
---
clang/include/clang/Driver/Compilation.h | 10 +++++-----
clang/lib/Driver/ToolChains/Clang.cpp | 4 ++--
clang/test/Driver/sycl-embed-virtual-arch.cpp | 7 +++++--
.../OffloadTools/clang-linker-wrapper/linker-wrapper.c | 2 +-
.../tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 5 ++---
clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp | 3 ++-
6 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 615d18f3e50d56..9f23d08348c707 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -106,8 +106,8 @@ class Compilation {
llvm::SmallVector<StringRef, 2>>
SYCLIGCADevices;
- /// For SYCL, the --offload-arch values given directly, e.g. igca_30r, rather
- /// than mapped from an Intel GPU.
+ /// For SYCL, the --offload-arch values that are not Intel GPUs, e.g.
+ /// igca_30r. An IGCA target in this set keeps its SPIR-V.
llvm::DenseSet<std::pair<const ToolChain *, StringRef>> SYCLDirectArchs;
/// Temporary files which should be removed on exit.
@@ -278,13 +278,13 @@ class Compilation {
ArrayRef<StringRef> getSYCLIGCADevices(const ToolChain *TC,
StringRef Target) const;
- /// Record that \p Arch was given in --offload-arch directly, e.g. igca_30r,
- /// rather than mapped from an Intel GPU.
+ /// Record \p Arch as an --offload-arch value that is not an Intel GPU.
void addSYCLDirectArch(const ToolChain *TC, StringRef Arch) {
SYCLDirectArchs.insert({TC, Arch});
}
- /// \return true if \p Arch was given in --offload-arch directly.
+ /// \return true if \p Arch is an --offload-arch value that is not an Intel
+ /// GPU.
bool isSYCLDirectArch(const ToolChain *TC, StringRef Arch) const {
return SYCLDirectArchs.contains({TC, Arch});
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 3b3452b91749a7..42e9a4383ac91d 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9778,8 +9778,8 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
for (StringRef Device : Devices)
Parts.emplace_back("code=" + Device.str());
- // The SPIR-V can only be dropped from a device image compiled ahead of
- // time, and is kept if the IGCA target is also given directly.
+ // Drop the SPIR-V only from a device image compiled ahead of time, and
+ // keep it if its IGCA target is also in --offload-arch.
bool EmbedVirtualArch =
TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
options::OPT_no_offload_embed_virtual_arch, true);
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
index a32c556442f094..66d9f74cd958df 100644
--- a/clang/test/Driver/sycl-embed-virtual-arch.cpp
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -17,13 +17,16 @@
// RUN: | FileCheck -check-prefix=NO-EMBED %s
// NO-EMBED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u,no-embed-virtual-arch=1"{{$}}
-/// It has no effect for an IGCA target given directly, which is compiled just
-/// in time, also when one of its Intel GPUs is given as well.
+/// It has no effect for an IGCA target in --offload-arch, which is compiled
+/// just in time.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=igca_30r --no-offload-embed-virtual-arch -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=DIRECT %s
// DIRECT-NOT: warning:
// DIRECT: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl"{{$}}
+
+/// It still has no effect when one of the IGCA target's Intel GPUs is also
+/// given.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=xe-mtl-u,igca_30r --no-offload-embed-virtual-arch \
// RUN: -c %s 2>&1 | FileCheck -check-prefix=EMBED %s
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index f11fb163fdedeb..806c62d91d8228 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -188,7 +188,7 @@ __attribute__((visibility("protected"), used)) int x;
// The replayed extraction of an image without "code" does not also match one
// with "code" for the same architecture.
-// The SPIR-V is dropped only if all device images that list Intel GPUs ask
+// The SPIR-V is dropped only if all device images that list real devices ask
// for it. A generic archive member does not count.
// SYCL-NO-EMBED-NOT: warning:
// SYCL-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --no-embed-virtual-arch{{$}}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 7bbbc330dcf4c2..c01a2e138a94e7 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1112,9 +1112,8 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
Args.MakeArgString(Device));
- // Drop the SPIR-V from the device image compiled ahead of time only if every
- // device image that lists real devices in "code" asks for it. Generic
- // archive members list none.
+ // Drop the SPIR-V only if every device image that lists real devices in
+ // "code" asks for it. Generic archive members list none, so they don't count.
if (Binary.getOffloadKind() == OFK_SYCL) {
auto HasKey = [](StringRef Key) {
return [=](const OffloadFile &File) {
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 483c68b5b54f6c..3b9e6c143fc5ce 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -1010,7 +1010,8 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
bool IsAOTCompileNeeded =
OA.isIntelIGCA() ? Args.hasArg(OPT_code_EQ) : OA.isIntel();
- // ocloc drops the SPIR-V for an Intel GPU; opencl-aot for a CPU keeps it.
+ // Only ocloc, for an Intel GPU, can drop the SPIR-V; opencl-aot, for an
+ // Intel CPU, cannot.
bool NoEmbedVirtualArch = IsAOTCompileNeeded && !OA.isIntelCPU() &&
Args.hasArg(OPT_no_embed_virtual_arch);
>From 92c2b84e87263df72f6de942cfce7e67e5a598bb Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 18 Nov 2026 17:15:54 -0800
Subject: [PATCH 26/51] [Clang][SYCL] Address review: show --code=<device>,
simplify a comment
---
clang/docs/ClangSYCLLinker.md | 2 +-
clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td | 2 +-
clang/tools/clang-sycl-linker/SYCLLinkOpts.td | 2 +-
llvm/include/llvm/Object/OffloadBinary.h | 4 ++--
4 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index e84d24fdfb55f6..68f2b5dbd8cd25 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -36,7 +36,7 @@ USAGE: clang-sycl-linker [options] <input bitcode files>
OPTIONS:
--arch=<arch> The device architecture
- --code=<code> Specify a real device to compile ahead of time for. Can be repeated. Without it, a real --arch is compiled ahead of time and a virtual one is kept for just-in-time compilation.
+ --code=<device> Specify a real device to compile ahead of time for. Can be repeated. Without it, a real --arch is compiled ahead of time and a virtual one is kept for just-in-time compilation.
--dry-run Print generated commands without running.
-help-hidden Display all available options
-help Display available options (--help-hidden for more)
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index 4e4aa7df8890ab..2e01656d5a9811 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -81,7 +81,7 @@ def triple_EQ : Joined<["--"], "triple=">,
Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<triple>">,
HelpText<"The device target triple">;
def code_EQ : Joined<["--"], "code=">,
- Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<code>">,
+ Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<device>">,
HelpText<"A real device to compile an image ahead of time for">;
def whole_program : Flag<["--"], "whole-program">,
Flags<[DeviceOnlyOption, HelpHidden]>,
diff --git a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
index e2b0d66e4eb33e..faaf3fa12fd6cc 100644
--- a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
+++ b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
@@ -48,7 +48,7 @@ def arch_EQ : Joined<["--", "-"], "arch=">,
HelpText<"The device architecture">;
def code_EQ : Joined<["--", "-"], "code=">,
Flags<[LinkerOnlyOption]>,
- MetaVarName<"<code>">,
+ MetaVarName<"<device>">,
HelpText<"Specify a real device to compile ahead of time for. "
"Can be repeated. Without it, a real --arch is compiled "
"ahead of time and a virtual one is kept for "
diff --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 2a569be8a81c9a..1ed03c60be3cfe 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -290,8 +290,8 @@ LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
/// can be linked into the group of device images \p Requested.
///
/// - A generic device image can be linked into any group.
-/// - Otherwise, both must list the same real devices in "code", and have the
-/// same architecture unless the group is generic.
+/// - Otherwise, both must have identical "code" lists, and the same
+/// architecture unless the group is generic.
///
/// Device image to add Group to link it into Result
/// (Provided) (Requested)
>From 9db3349ad05cfba8f417df49daf6687fe0d3be2a Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 10:42:10 -0800
Subject: [PATCH 27/51] [Clang][SYCL] Address review: make the IGCA API
language-neutral
Avoid SYCL-specific names and places in the IGCA API where the code is not
SYCL-specific. In clang-linker-wrapper, handle "code" for every offload kind
and drop --embed-bitcode support in bundleSYCL.
---
clang/docs/ClangLinkerWrapper.md | 4 +-
clang/include/clang/Basic/OffloadArch.h | 5 ++
clang/include/clang/Driver/Compilation.h | 22 +++----
clang/lib/Basic/OffloadArch.cpp | 5 ++
clang/lib/Driver/Compilation.cpp | 14 ++---
clang/lib/Driver/Driver.cpp | 4 +-
clang/lib/Driver/ToolChains/Clang.cpp | 5 +-
clang/lib/Driver/ToolChains/SYCL.cpp | 6 --
clang/lib/Driver/ToolChains/SYCL.h | 5 --
clang/test/Driver/sycl-igca-link.cpp | 13 ++--
clang/test/Driver/sycl-intel-gpu-arch.cpp | 13 ++--
.../clang-linker-wrapper/linker-wrapper.c | 46 ++++++++-------
.../ClangLinkerWrapper.cpp | 59 ++++++-------------
.../clang-sycl-linker/ClangSYCLLinker.cpp | 2 +-
clang/unittests/Basic/OffloadArchTest.cpp | 12 ++++
llvm/include/llvm/Object/OffloadBinary.h | 4 +-
16 files changed, 107 insertions(+), 112 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index 598f89d15ad347..018ad82acfc1f0 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -81,7 +81,9 @@ The linker wrapper will link extracted device code that is compatible with each
other. Generally, this requires that the target triple, the architecture and
the optional `code` list 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.
+cause it to be linked with any other device code with the same target triple.
+The exception only goes one way: device code with a `code` list is not linked
+into `generic` device code.
The `code` list is only needed when the architecture is a virtual one that
several devices share, and it names the real devices to compile the device code
diff --git a/clang/include/clang/Basic/OffloadArch.h b/clang/include/clang/Basic/OffloadArch.h
index 84a25e5caf31ad..81b6d4e7f31642 100644
--- a/clang/include/clang/Basic/OffloadArch.h
+++ b/clang/include/clang/Basic/OffloadArch.h
@@ -131,6 +131,11 @@ const char *OffloadArchToVirtualArchString(OffloadArch A);
// string is not recognized.
OffloadArch StringToOffloadArch(llvm::StringRef S);
+/// \return the architecture that the device job for the --offload-arch value
+/// \p Arch is named after: the IGCA target if \p Arch is an Intel GPU, e.g.
+/// "igca_30r" for "xe-mtl-u", and \p Arch itself otherwise.
+llvm::StringRef getArchToCompileFor(llvm::StringRef Arch);
+
OffloadArch getSubArchOffloadArch(llvm::Triple::SubArchType SubArch);
llvm::Triple::SubArchType getOffloadArchSubArch(OffloadArch ID);
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 5c52c608724cc1..29082b533365e7 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -98,12 +98,12 @@ class Compilation {
};
std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
- /// For SYCL, maps a device toolchain and an IGCA target, e.g. igca_30r, to
- /// the Intel GPUs from --offload-arch that share that target, e.g. xe-mtl-h
- /// and xe-mtl-u. Each list is sorted and has no duplicates.
+ /// Maps a device toolchain and an IGCA target, e.g. igca_30r, to the Intel
+ /// GPUs from --offload-arch that share that target, e.g. xe-mtl-h and
+ /// xe-mtl-u. Each list is sorted and has no duplicates.
llvm::DenseMap<std::pair<const ToolChain *, StringRef>,
llvm::SmallVector<StringRef, 2>>
- SYCLIGCADevices;
+ IGCADevices;
/// Temporary files which should be removed on exit.
llvm::opt::ArgStringList TempFiles;
@@ -264,14 +264,14 @@ class Compilation {
getArgsForToolChain(const ToolChain *TC, BoundArch BA,
Action::OffloadKind DeviceOffloadKind);
- /// Add the Intel GPU \p Device to the GPUs of the IGCA target \p Target.
- void addSYCLIGCADevice(const ToolChain *TC, StringRef Target,
- StringRef Device);
+ /// Add the Intel GPU \p Device to the GPUs of the IGCA target \p Target for
+ /// the device toolchain \p TC.
+ void addIGCADevice(const ToolChain *TC, StringRef Target, StringRef Device);
- /// \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;
+ /// \return the Intel GPUs of the IGCA target \p Target for the device
+ /// toolchain \p TC, sorted, or an empty list if there are none.
+ ArrayRef<StringRef> getIGCADevices(const ToolChain *TC,
+ StringRef Target) const;
/// addTempFile - Add a file to remove on exit, and returns its
/// argument.
diff --git a/clang/lib/Basic/OffloadArch.cpp b/clang/lib/Basic/OffloadArch.cpp
index 96e1ff6887d344..629d918c30c179 100644
--- a/clang/lib/Basic/OffloadArch.cpp
+++ b/clang/lib/Basic/OffloadArch.cpp
@@ -111,6 +111,11 @@ OffloadArch StringToOffloadArch(llvm::StringRef S) {
return OffloadArch::getUnknown();
}
+llvm::StringRef getArchToCompileFor(llvm::StringRef Arch) {
+ OffloadArch ID = StringToOffloadArch(Arch);
+ return ID.isIntelGPU() ? OffloadArchToString(ID.intelGPUIGCATarget()) : Arch;
+}
+
OffloadArch getSubArchOffloadArch(llvm::Triple::SubArchType SubArch) {
llvm::AMDGPU::GPUKind AK = llvm::AMDGPU::getGPUKindFromSubArch(SubArch);
if (AK == llvm::AMDGPU::GK_NONE)
diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp
index 40aa5a91cfca10..8c633dd19c7999 100644
--- a/clang/lib/Driver/Compilation.cpp
+++ b/clang/lib/Driver/Compilation.cpp
@@ -121,18 +121,18 @@ 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}];
+void Compilation::addIGCADevice(const ToolChain *TC, StringRef Target,
+ StringRef Device) {
+ SmallVector<StringRef, 2> &Devices = IGCADevices[{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())
+ArrayRef<StringRef> Compilation::getIGCADevices(const ToolChain *TC,
+ StringRef Target) const {
+ auto It = IGCADevices.find({TC, Target});
+ if (It == IGCADevices.end())
return {};
return It->second;
}
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index bf1c7a5ee0a4f7..e52e248b61eb78 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4157,10 +4157,10 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
if (Kind == Action::OFK_SYCL) {
llvm::DenseSet<StringRef> Targets;
for (StringRef Arch : Archs) {
- StringRef Target = toolchains::getSYCLVirtualArch(Arch);
+ StringRef Target = getArchToCompileFor(Arch);
Targets.insert(Target);
if (Target != Arch)
- C.addSYCLIGCADevice(&TC, Target, Arch);
+ C.addIGCADevice(&TC, Target, Arch);
}
Archs = std::move(Targets);
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 1cccd8524c0e0d..b72906d0d121cc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -18,7 +18,6 @@
#include "Hexagon.h"
#include "PS4CPU.h"
#include "ToolChains/Cuda.h"
-#include "ToolChains/SYCL.h"
#include "clang/Basic/CLWarnings.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/HeaderInclude.h"
@@ -9774,7 +9773,7 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
// 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))
+ for (StringRef Device : C.getIGCADevices(TC, Arch.ArchName))
Parts.emplace_back("code=" + Device.str());
if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
@@ -9976,7 +9975,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
// A SYCL device image for an Intel GPU is labeled with the GPU's IGCA
// target.
if (Kind == Action::OFK_SYCL)
- Name = toolchains::getSYCLVirtualArch(Name);
+ Name = getArchToCompileFor(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));
diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp
index b79f7664816800..c805f793215392 100644
--- a/clang/lib/Driver/ToolChains/SYCL.cpp
+++ b/clang/lib/Driver/ToolChains/SYCL.cpp
@@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
#include "SYCL.h"
#include "SPIRV.h"
-#include "clang/Basic/OffloadArch.h"
#include "clang/Driver/CommonArgs.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
@@ -109,11 +108,6 @@ static ArrayRef<options::ID> getUnsupportedOpts() {
return UnsupportedOpts;
}
-StringRef toolchains::getSYCLVirtualArch(StringRef Arch) {
- OffloadArch ID = StringToOffloadArch(Arch);
- return ID.isIntelGPU() ? OffloadArchToString(ID.intelGPUIGCATarget()) : Arch;
-}
-
SYCLToolChain::SYCLToolChain(const Driver &D, const llvm::Triple &Triple,
const ToolChain &HostTC, const ArgList &Args)
: ToolChain(D, Triple, Args), HostTC(HostTC),
diff --git a/clang/lib/Driver/ToolChains/SYCL.h b/clang/lib/Driver/ToolChains/SYCL.h
index 13a2109b2c42ff..d754544e30d971 100644
--- a/clang/lib/Driver/ToolChains/SYCL.h
+++ b/clang/lib/Driver/ToolChains/SYCL.h
@@ -70,11 +70,6 @@ class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
SYCLInstallationDetector SYCLInstallation;
};
-/// \return the architecture to compile SYCL device code for, given \p Arch
-/// from --offload-arch: the IGCA target if \p Arch is an Intel GPU, e.g.
-/// "igca_30r" for "xe-mtl-u", and \p Arch itself otherwise.
-StringRef getSYCLVirtualArch(StringRef Arch);
-
} // end namespace toolchains
} // end namespace driver
diff --git a/clang/test/Driver/sycl-igca-link.cpp b/clang/test/Driver/sycl-igca-link.cpp
index 6501919c004d4a..03cd33cb2c3060 100644
--- a/clang/test/Driver/sycl-igca-link.cpp
+++ b/clang/test/Driver/sycl-igca-link.cpp
@@ -1,6 +1,6 @@
-/// 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.
+/// SYCL device code for Intel GPUs is compiled once per IGCA target. At link
+/// time, SYCL device code for the same IGCA target and devices is linked
+/// together, and the devices are passed on with --code.
// REQUIRES: x86-registered-target
// UNSUPPORTED: system-windows
@@ -12,9 +12,8 @@
// 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.
+/// xe-mtl-u and xe-mtl-h share igca_30r, so the SYCL device code of both
+/// objects is linked together, with both devices passed on with --code.
// 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
@@ -22,7 +21,7 @@
// 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.
+/// SYCL 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
diff --git a/clang/test/Driver/sycl-intel-gpu-arch.cpp b/clang/test/Driver/sycl-intel-gpu-arch.cpp
index 8badd283f6ef3a..768a96f907fc97 100644
--- a/clang/test/Driver/sycl-intel-gpu-arch.cpp
+++ b/clang/test/Driver/sycl-intel-gpu-arch.cpp
@@ -1,6 +1,7 @@
/// Tests for Intel GPU names in --offload-arch.
-/// An Intel GPU is compiled for its IGCA target, e.g. igca_40r for xe-bmg-g21.
+/// A device job is named after the IGCA target of an Intel GPU, e.g. igca_40r
+/// for xe-bmg-g21.
// 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 \
@@ -8,9 +9,8 @@
// 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 of one device give one device
-/// image for its IGCA target, which lists each name as given, to compile ahead
-/// of time for.
+/// A device name, an alias and a numeric name of the same device result in one
+/// device image for its IGCA target, with all names kept as given.
// 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 \
@@ -18,7 +18,7 @@
// 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.
+/// order have the same list.
// 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
@@ -39,8 +39,7 @@
// RUN: | FileCheck -check-prefixes=CHK-MIXED %s
// CHK-MIXED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u"{{$}}
-/// An IGCA target given directly lists no devices, so it is compiled just in
-/// time.
+/// An IGCA target given directly lists no devices.
// 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
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 649aaff9fae6ad..25551f90795ebb 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -66,7 +66,7 @@ __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
+// together, and the devices in the list are passed on with --code. 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
@@ -97,6 +97,13 @@ __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-generic.o %t.sycl-u.a -o a.out 2>&1 \
// RUN: | FileCheck %s --check-prefix=SYCL-CODE-ARCHIVE
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-u-member.o -fembed-offload-object=%t.sycl-u.out
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-uh-member.o -fembed-offload-object=%t.sycl-uh.out
+// RUN: rm -f %t.sycl-two-code.a
+// RUN: llvm-ar rcs %t.sycl-two-code.a %t.sycl-u-member.o %t.sycl-uh-member.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-uh-a.o %t.sycl-two-code.a \
+// RUN: -o a.out 2>&1 | FileCheck %s --check-prefix=SYCL-TWO-CODE-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
@@ -114,11 +121,6 @@ __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 \
@@ -130,6 +132,13 @@ __attribute__((visibility("protected"), used)) int x;
// 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
+// RUN: llvm-offload-binary -o %t.openmp-code.out \
+// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,code=a \
+// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,code=b
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.openmp-code.o -fembed-offload-object=%t.openmp-code.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld %t.openmp-code.o -o a.out 2>&1 \
+// RUN: | FileCheck %s --check-prefix=OPENMP-CODE
// SYCL-SAME-CODE: clang{{.*}} -march=igca_30r {{.*}}.o {{.*}}.o --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --code=xe-mtl-h{{$}}
// SYCL-SAME-CODE-NOT: --sycl-link
@@ -145,6 +154,11 @@ __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 --code=xe-mtl-u{{$}}
+// Archive members that list different devices are each linked only with the
+// SYCL images that list the same devices.
+// SYCL-TWO-CODE-ARCHIVE-DAG: clang{{.*}} -march=igca_30r {{[^ ]*}}sycl-u-spirv64{{[^ ]*}}.o {{[^ ]*}}sycl-u-member-spirv64{{[^ ]*}}.o --sycl-link -Xlinker --code=xe-mtl-u{{$}}
+// SYCL-TWO-CODE-ARCHIVE-DAG: clang{{.*}} -march=igca_30r {{[^ ]*}}sycl-uh-a-spirv64{{[^ ]*}}.o {{[^ ]*}}sycl-uh-member-spirv64{{[^ ]*}}.o --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --code=xe-mtl-h{{$}}
+
// An archive member that lists real devices is not linked into a generic group.
// SYCL-CODE-ARCHIVE: clang{{.*}} --target=spirv64-unknown-unknown {{[^ ]*}}generic{{[^ ]*}}.o --sycl-link{{$}}
// SYCL-CODE-ARCHIVE-NOT: --sycl-link
@@ -160,24 +174,16 @@ __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: OFFLOADING IMAGE [0]:
-// SYCL-MERGED-BC-NEXT: kind llvm ir
-// SYCL-MERGED-BC-NEXT: arch igca_40r
-// SYCL-MERGED-BC-NEXT: triple spirv64-unknown-unknown
-// SYCL-MERGED-BC-NEXT: producer sycl
-// SYCL-MERGED-BC: OFFLOADING IMAGE [1]:
-// SYCL-MERGED-BC-NEXT: kind llvm ir
-// SYCL-MERGED-BC-NEXT: arch igca_30r
-// SYCL-MERGED-BC-NEXT: triple spirv64-unknown-unknown
-// SYCL-MERGED-BC-NEXT: producer sycl
-// SYCL-MERGED-BC-NOT: OFFLOADING IMAGE
-
// With --wrapper-verbose --save-temps, the merged offload binary is wrapped.
// SYCL-MERGED-VERBOSE: llvm-offload-wrapper{{.*}} --kind=sycl {{.*}} a.out.sycl.merged.out{{$}}
+// Grouping by "code" does not depend on the offload kind: OpenMP images with
+// the same "arch" but different "code" are linked separately, without --code.
+// OPENMP-CODE-COUNT-2: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70 {{[^ ]*}}.o{{$}}
+// OPENMP-CODE-NOT: --target=nvptx64-nvidia-cuda
+
// The replayed extraction of an image without "code" does not also match one
-// with "code" for the same architecture.
+// with "code" for the same "arch".
// 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 \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index f7ce333e4d9a51..2e9c4d14db5c44 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -591,13 +591,12 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
// For linking device code with the SYCL offload kind, special handling is
// required. Passing --sycl-link to clang results in a call to
- // clang-sycl-linker.
- if (ActiveOffloadKindMask & OFK_SYCL)
+ // clang-sycl-linker, which also takes the real devices to compile for.
+ if (ActiveOffloadKindMask & OFK_SYCL) {
CmdArgs.push_back("--sycl-link");
-
- // Pass the real devices on to clang-sycl-linker.
- for (const std::string &Device : Devices)
- CmdArgs.append({"-Xlinker", Args.MakeArgString("--code=" + Device)});
+ for (const std::string &Device : Devices)
+ CmdArgs.append({"-Xlinker", Args.MakeArgString("--code=" + Device)});
+ }
for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
CmdArgs.append({"-Xlinker", Args.MakeArgString(Arg)});
@@ -662,9 +661,7 @@ 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.
+/// \return the name to extract \p Binary to.
std::string getExtractedFileName(const OffloadBinary &Binary) {
std::string Name =
(sys::path::stem(Binary.getMemoryBufferRef().getBufferIdentifier()) +
@@ -933,13 +930,12 @@ bundleOmpAcc(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()));
+ assert(!Images.empty() && "expected at least one linked image");
+ if (Images.size() == 1) {
+ const MemoryBuffer &Image = *Images.front().Image;
+ Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(
+ Image.getBuffer(), Image.getBufferIdentifier()));
return std::move(Buffers);
}
@@ -953,21 +949,6 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
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();
@@ -1097,20 +1078,18 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
// Set the subarchitecture and target triple for this compilation.
const OptTable &Tbl = getOptTable();
- StringRef Arch = Args.MakeArgString(Input.front().getBinary()->getArch());
+ const OffloadBinary &Binary = *Input.front().getBinary();
+ StringRef Arch = Args.MakeArgString(Binary.getArch());
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_arch_EQ),
Arch == "generic" ? "" : Arch);
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_triple_EQ),
- Args.MakeArgString(Input.front().getBinary()->getTriple()));
+ Args.MakeArgString(Binary.getTriple()));
- // Get the real devices 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"), ','))
- if (!Device.empty())
- DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
- Args.MakeArgString(Device));
+ // Get the real devices that an image lists in "code".
+ for (StringRef Device : llvm::split(Binary.getString("code"), ','))
+ if (!Device.empty())
+ DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
+ Args.MakeArgString(Device));
// If every input file is bitcode we have whole program visibility as we
// do only support static linking with bitcode.
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 324e605a79b730..735e3ecbdb37e3 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -715,7 +715,7 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
CmdArgs.push_back("-spirv_input");
// Compile for the --code list, e.g. "xe-mtl-u,xe-mtl-h" for an IGCA target,
- // or else for the Intel GPU in --arch.
+ // or otherwise for the Intel GPU in --arch.
std::string Devices = llvm::join(Args.getAllArgValues(OPT_code_EQ), ",");
if (Devices.empty())
Devices = Args.getLastArgValue(OPT_arch_EQ);
diff --git a/clang/unittests/Basic/OffloadArchTest.cpp b/clang/unittests/Basic/OffloadArchTest.cpp
index 2ed374e1d4a318..4c3c8c69b1410e 100644
--- a/clang/unittests/Basic/OffloadArchTest.cpp
+++ b/clang/unittests/Basic/OffloadArchTest.cpp
@@ -95,6 +95,18 @@ TEST(OffloadArchTest, IntelGPUIGCATarget) {
EXPECT_TRUE(parse("xe-pvc").intelGPUIGCATarget().isIntelIGCA());
}
+TEST(OffloadArchTest, ArchToCompileFor) {
+ // A device job for an Intel GPU is named after its IGCA target, and for
+ // anything else after the value itself.
+ EXPECT_EQ(getArchToCompileFor("xe-mtl-u"), "igca_30r");
+ EXPECT_EQ(getArchToCompileFor("xe-mtl-h"), "igca_30r");
+ EXPECT_EQ(getArchToCompileFor("igca_30r"), "igca_30r");
+ EXPECT_EQ(getArchToCompileFor("graniterapids"), "graniterapids");
+ EXPECT_EQ(getArchToCompileFor("sm_70"), "sm_70");
+ EXPECT_EQ(getArchToCompileFor("gfx90a:xnack+"), "gfx90a:xnack+");
+ EXPECT_EQ(getArchToCompileFor("generic"), "generic");
+}
+
TEST(OffloadArchTest, Defaults) {
EXPECT_STREQ(OffloadArchToString(OffloadArch::CudaDefault()), "sm_52");
EXPECT_STREQ(OffloadArchToString(OffloadArch::HIPDefault()), "gfx906");
diff --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 1ed03c60be3cfe..120ae753d124ec 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -312,8 +312,8 @@ LLVM_ABI bool areTargetsCompatible(const OffloadFile::TargetID &Provided,
const OffloadFile::TargetID &Requested);
/// Returns true if device images built for \p LHS and \p RHS are linked
-/// together: they have the same processor and features, and list the same
-/// real devices in "code".
+/// together: they have the same processor and features, and identical "code"
+/// lists.
LLVM_ABI bool areTargetsEquivalent(const OffloadFile::TargetID &LHS,
const OffloadFile::TargetID &RHS);
>From 52370a2c51c2447b6934e713815e3abf9299fca2 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:00:48 -0800
Subject: [PATCH 28/51] [Clang][SYCL] Address review: use
OffloadArchToVirtualArchString for Intel GPUs
---
clang/include/clang/Basic/OffloadArch.h | 5 -----
clang/lib/Basic/OffloadArch.cpp | 8 ++------
clang/lib/Driver/Driver.cpp | 6 +++++-
clang/lib/Driver/ToolChains/Clang.cpp | 3 ++-
clang/unittests/Basic/OffloadArchTest.cpp | 17 +++++++----------
5 files changed, 16 insertions(+), 23 deletions(-)
diff --git a/clang/include/clang/Basic/OffloadArch.h b/clang/include/clang/Basic/OffloadArch.h
index 81b6d4e7f31642..84a25e5caf31ad 100644
--- a/clang/include/clang/Basic/OffloadArch.h
+++ b/clang/include/clang/Basic/OffloadArch.h
@@ -131,11 +131,6 @@ const char *OffloadArchToVirtualArchString(OffloadArch A);
// string is not recognized.
OffloadArch StringToOffloadArch(llvm::StringRef S);
-/// \return the architecture that the device job for the --offload-arch value
-/// \p Arch is named after: the IGCA target if \p Arch is an Intel GPU, e.g.
-/// "igca_30r" for "xe-mtl-u", and \p Arch itself otherwise.
-llvm::StringRef getArchToCompileFor(llvm::StringRef Arch);
-
OffloadArch getSubArchOffloadArch(llvm::Triple::SubArchType SubArch);
llvm::Triple::SubArchType getOffloadArchSubArch(OffloadArch ID);
diff --git a/clang/lib/Basic/OffloadArch.cpp b/clang/lib/Basic/OffloadArch.cpp
index 629d918c30c179..77c2055ce02c94 100644
--- a/clang/lib/Basic/OffloadArch.cpp
+++ b/clang/lib/Basic/OffloadArch.cpp
@@ -76,9 +76,10 @@ const char *OffloadArchToVirtualArchString(OffloadArch A) {
return "unknown";
case OffloadArch::TargetArch::IntelIGCA:
return llvm::IntelGPU::getIGCATargetName(A.igcaTarget()).data();
+ case OffloadArch::TargetArch::IntelGPU:
+ return OffloadArchToString(A.intelGPUIGCATarget());
case OffloadArch::TargetArch::Unused:
case OffloadArch::TargetArch::IntelCPU:
- case OffloadArch::TargetArch::IntelGPU:
case OffloadArch::TargetArch::Generic:
return "";
}
@@ -111,11 +112,6 @@ OffloadArch StringToOffloadArch(llvm::StringRef S) {
return OffloadArch::getUnknown();
}
-llvm::StringRef getArchToCompileFor(llvm::StringRef Arch) {
- OffloadArch ID = StringToOffloadArch(Arch);
- return ID.isIntelGPU() ? OffloadArchToString(ID.intelGPUIGCATarget()) : Arch;
-}
-
OffloadArch getSubArchOffloadArch(llvm::Triple::SubArchType SubArch) {
llvm::AMDGPU::GPUKind AK = llvm::AMDGPU::getGPUKindFromSubArch(SubArch);
if (AK == llvm::AMDGPU::GK_NONE)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index e52e248b61eb78..0186498d7a0500 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4157,7 +4157,11 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
if (Kind == Action::OFK_SYCL) {
llvm::DenseSet<StringRef> Targets;
for (StringRef Arch : Archs) {
- StringRef Target = getArchToCompileFor(Arch);
+ // The device job for an Intel GPU is named after its IGCA target; other
+ // values are kept as given.
+ OffloadArch ID = StringToOffloadArch(Arch);
+ StringRef Target =
+ ID.isIntelGPU() ? OffloadArchToVirtualArchString(ID) : Arch;
Targets.insert(Target);
if (Target != Arch)
C.addIGCADevice(&TC, Target, Arch);
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index b72906d0d121cc..d69089a38e37bf 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9975,7 +9975,8 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
// A SYCL device image for an Intel GPU is labeled with the GPU's IGCA
// target.
if (Kind == Action::OFK_SYCL)
- Name = getArchToCompileFor(Name);
+ if (OffloadArch ID = StringToOffloadArch(Name); ID.isIntelGPU())
+ Name = OffloadArchToVirtualArchString(ID);
// Intel GPUs that share an IGCA target ask for it once.
if (ShouldExtract.insert(Name).second)
CmdArgs.emplace_back(Args.MakeArgString("--should-extract=" + Name));
diff --git a/clang/unittests/Basic/OffloadArchTest.cpp b/clang/unittests/Basic/OffloadArchTest.cpp
index 4c3c8c69b1410e..7360ef557b6f1c 100644
--- a/clang/unittests/Basic/OffloadArchTest.cpp
+++ b/clang/unittests/Basic/OffloadArchTest.cpp
@@ -95,16 +95,13 @@ TEST(OffloadArchTest, IntelGPUIGCATarget) {
EXPECT_TRUE(parse("xe-pvc").intelGPUIGCATarget().isIntelIGCA());
}
-TEST(OffloadArchTest, ArchToCompileFor) {
- // A device job for an Intel GPU is named after its IGCA target, and for
- // anything else after the value itself.
- EXPECT_EQ(getArchToCompileFor("xe-mtl-u"), "igca_30r");
- EXPECT_EQ(getArchToCompileFor("xe-mtl-h"), "igca_30r");
- EXPECT_EQ(getArchToCompileFor("igca_30r"), "igca_30r");
- EXPECT_EQ(getArchToCompileFor("graniterapids"), "graniterapids");
- EXPECT_EQ(getArchToCompileFor("sm_70"), "sm_70");
- EXPECT_EQ(getArchToCompileFor("gfx90a:xnack+"), "gfx90a:xnack+");
- EXPECT_EQ(getArchToCompileFor("generic"), "generic");
+TEST(OffloadArchTest, VirtualArch) {
+ // The virtual architecture of an Intel GPU is its IGCA target.
+ EXPECT_STREQ(OffloadArchToVirtualArchString(parse("xe-mtl-u")), "igca_30r");
+ EXPECT_STREQ(OffloadArchToVirtualArchString(parse("xe-mtl-h")), "igca_30r");
+ EXPECT_STREQ(OffloadArchToVirtualArchString(parse("xe-pvc")), "igca_20ca");
+ EXPECT_STREQ(OffloadArchToVirtualArchString(parse("igca_30r")), "igca_30r");
+ EXPECT_STREQ(OffloadArchToVirtualArchString(parse("sm_70")), "compute_70");
}
TEST(OffloadArchTest, Defaults) {
>From 8689f980a920375a0611b9ded43b61d972579e02 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:14:47 -0800
Subject: [PATCH 29/51] [Clang][SYCL] Address review: drop a SYCL-specific
comment
---
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 2e9c4d14db5c44..8ce7db604291d7 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1283,7 +1283,6 @@ 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)
>From 392cfadeb919c190aee8a83cf4e74211bc4bc813 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:20:41 -0800
Subject: [PATCH 30/51] [Clang][SYCL] Address review: check isIntelGPU when
recording IGCA devices
---
clang/lib/Driver/Driver.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 0186498d7a0500..57cae8c67189c8 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4163,7 +4163,7 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
StringRef Target =
ID.isIntelGPU() ? OffloadArchToVirtualArchString(ID) : Arch;
Targets.insert(Target);
- if (Target != Arch)
+ if (ID.isIntelGPU())
C.addIGCADevice(&TC, Target, Arch);
}
Archs = std::move(Targets);
>From 73ed8e33062e66a88b7763dc9fe710ec4f26095c Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:35:24 -0800
Subject: [PATCH 31/51] [Clang][SYCL] Address review: drop TargetID comments
---
llvm/include/llvm/Object/OffloadBinary.h | 3 ---
1 file changed, 3 deletions(-)
diff --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 120ae753d124ec..1b233bea4a6be3 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -220,10 +220,7 @@ class OffloadBinary : public Binary {
/// A handle to a single OffloadBinary.
class OffloadFile {
public:
- /// The target a device image is built for. Device images with the same
- /// target are linked together.
struct TargetID {
- /// The target triple, e.g. "spirv64-unknown-unknown".
StringRef Triple;
/// The architecture, e.g. "sm_70" or "igca_30r".
StringRef Arch;
>From e86bbf4cd23c450c2c97f33512df66b8c340a7c6 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:45:56 -0800
Subject: [PATCH 32/51] [Clang][SYCL] Address review: keep the original
target-matching docs
---
llvm/include/llvm/Object/OffloadBinary.h | 28 ++++++++----------------
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 1b233bea4a6be3..058432ae1a3c7e 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -283,22 +283,13 @@ LLVM_ABI OffloadKind getOffloadKind(StringRef Name);
/// Convert an offload kind to its string representation.
LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
-/// Returns true if the device image \p Provided, e.g. a static archive member,
-/// can be linked into the group of device images \p Requested.
-///
-/// - A generic device image can be linked into any group.
-/// - Otherwise, both must have identical "code" lists, and the same
-/// architecture unless the group is generic.
-///
-/// Device image to add Group to link it into Result
-/// (Provided) (Requested)
-/// generic igca_30r, code=xe-mtl-u true
-/// igca_30r generic true
-/// igca_30r, code=xe-mtl-u igca_30r, code=xe-mtl-u true
-/// igca_30r, code=xe-mtl-u igca_30r, code=xe-mtl-h,xe-mtl-u false
-/// igca_30r, code=xe-mtl-u generic false
-///
-/// For AMDGPU a target id is a string conforming to the following BNF syntax:
+/// Returns true if an image built for target \p Provided can provide the device
+/// code for a request for target \p Requested. This is directional: a
+/// feature-unspecified or generic image serves a more specific request (e.g. a
+/// generic static-archive member pulled into a specific device-image group),
+/// but not vice versa. Unless \p Provided is generic, both must also have
+/// identical "code" lists. For AMDGPU a target id is a string conforming to the
+/// following BNF syntax:
///
/// target-id ::= '<arch> ( : <feature> ( '+' | '-' ) )*'
///
@@ -308,9 +299,8 @@ LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
LLVM_ABI bool areTargetsCompatible(const OffloadFile::TargetID &Provided,
const OffloadFile::TargetID &Requested);
-/// Returns true if device images built for \p LHS and \p RHS are linked
-/// together: they have the same processor and features, and identical "code"
-/// lists.
+/// Returns true if \p LHS and \p RHS denote the same logical target, i.e. the
+/// same processor and features, and identical "code" lists.
LLVM_ABI bool areTargetsEquivalent(const OffloadFile::TargetID &LHS,
const OffloadFile::TargetID &RHS);
>From 85e00013102bf286c990b12580e92a60cd87723c Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:47:57 -0800
Subject: [PATCH 33/51] [Clang][SYCL] Address review: drop a comment in
areTargetsEquivalent
---
llvm/lib/Object/OffloadBinary.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/lib/Object/OffloadBinary.cpp b/llvm/lib/Object/OffloadBinary.cpp
index 3ced4758ea6d2a..98884415377fef 100644
--- a/llvm/lib/Object/OffloadBinary.cpp
+++ b/llvm/lib/Object/OffloadBinary.cpp
@@ -540,7 +540,6 @@ StringRef object::getImageKindName(ImageKind Kind) {
bool object::areTargetsEquivalent(const OffloadFile::TargetID &LHS,
const OffloadFile::TargetID &RHS) {
- // Device images that list different real devices are linked separately.
if (LHS.Code != RHS.Code)
return false;
>From 9b100dfedffc6acee4fb1338f24ef0f39c3593de Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 15:50:35 -0800
Subject: [PATCH 34/51] [Clang][SYCL] Drop a comment in areTargetsCompatible
---
llvm/lib/Object/OffloadBinary.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/llvm/lib/Object/OffloadBinary.cpp b/llvm/lib/Object/OffloadBinary.cpp
index 98884415377fef..1169894ec824e0 100644
--- a/llvm/lib/Object/OffloadBinary.cpp
+++ b/llvm/lib/Object/OffloadBinary.cpp
@@ -579,8 +579,6 @@ bool object::areTargetsCompatible(const OffloadFile::TargetID &Provided,
if (Provided.Arch == "generic")
return true;
- // Otherwise, device images that list different real devices are linked
- // separately.
if (Provided.Code != Requested.Code)
return false;
>From 0a7c628527c9219869dd03528541725e6afa7775 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 16:11:01 -0800
Subject: [PATCH 35/51] [Clang][SYCL] Address review: check "code" once in
areTargetsCompatible
---
llvm/lib/Object/OffloadBinary.cpp | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Object/OffloadBinary.cpp b/llvm/lib/Object/OffloadBinary.cpp
index 1169894ec824e0..254152fe24c60e 100644
--- a/llvm/lib/Object/OffloadBinary.cpp
+++ b/llvm/lib/Object/OffloadBinary.cpp
@@ -559,6 +559,10 @@ bool object::areTargetsEquivalent(const OffloadFile::TargetID &LHS,
bool object::areTargetsCompatible(const OffloadFile::TargetID &Provided,
const OffloadFile::TargetID &Requested) {
+ // Unless the image is generic, the "code" lists must be identical.
+ if (Provided.Arch != "generic" && Provided.Code != Requested.Code)
+ return false;
+
llvm::Triple ProvidedTT(Provided.Triple);
llvm::Triple RequestedTT(Requested.Triple);
@@ -567,23 +571,15 @@ bool object::areTargetsCompatible(const OffloadFile::TargetID &Provided,
if (ProvidedTT.isAMDGPU()) {
AMDGPU::TargetID ProvidedID(ProvidedTT, Provided.Arch);
AMDGPU::TargetID RequestedID(RequestedTT, Requested.Arch);
- return Provided.Code == Requested.Code &&
- ProvidedID.providesFor(RequestedID);
+ return ProvidedID.providesFor(RequestedID);
}
// For other targets the triples must be compatible.
if (!ProvidedTT.isCompatibleWith(RequestedTT))
return false;
- // A generic device image can be linked into any group.
- if (Provided.Arch == "generic")
- return true;
-
- if (Provided.Code != Requested.Code)
- return false;
-
- // A generic group accepts a device image of any architecture.
- if (Requested.Arch == "generic")
+ // If the architecture is "generic" we assume it is always compatible.
+ if (Provided.Arch == "generic" || Requested.Arch == "generic")
return true;
return Provided.Arch == Requested.Arch;
>From 06e279e8d3fe8bcf9605d8d73a6ce7fe24db50cd Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 16:14:26 -0800
Subject: [PATCH 36/51] [Clang][SYCL] Share the "code" split, simplify comments
Self code review findings.
---
.../ClangLinkerWrapper.cpp | 39 ++++++++++---------
.../clang-sycl-linker/ClangSYCLLinker.cpp | 3 +-
2 files changed, 23 insertions(+), 19 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index db9a9d4e7dd4e8..9b28234357b9eb 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -502,8 +502,8 @@ 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 "code" are linked
- // separately, so the real devices are part of the name.
+ // Add the real devices to the output name, so that device links for the
+ // same architecture don't overwrite each other's output file.
std::vector<std::string> Devices = Args.getAllArgValues(OPT_code_EQ);
for (const std::string &Device : Devices)
OutputFileBase += "." + Device;
@@ -661,15 +661,22 @@ Error containerizeRawImage(std::unique_ptr<MemoryBuffer> &Img, OffloadKind Kind,
return Error::success();
}
+/// \return the real devices that \p Binary lists in "code".
+SmallVector<StringRef> getCodeDevices(const OffloadBinary &Binary) {
+ SmallVector<StringRef> Devices;
+ Binary.getString("code").split(Devices, ',', /*MaxSplit=*/-1,
+ /*KeepEmpty=*/false);
+ return Devices;
+}
+
/// \return the name to extract \p Binary to.
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();
+ for (StringRef Device : getCodeDevices(Binary))
+ Name += ("." + Device).str();
return Name;
}
@@ -940,9 +947,8 @@ bundleSYCL(ArrayRef<OffloadingImage> Images) {
return std::move(Buffers);
}
- // Images for different IGCA targets or with different "code" are linked
- // separately, but the SYCL runtime registers one offload binary per wrapper,
- // so merge their device images into one.
+ // The SYCL runtime registers one offload binary per wrapper, so merge the
+ // device images of all device links into one.
SmallVector<std::unique_ptr<OffloadBinary>> Binaries;
SmallVector<OffloadingImage> Merged;
for (const OffloadingImage &Image : Images) {
@@ -1086,11 +1092,9 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_triple_EQ),
Args.MakeArgString(Binary.getTriple()));
- // Get the real devices that an image lists in "code".
- for (StringRef Device : llvm::split(Binary.getString("code"), ','))
- if (!Device.empty())
- DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
- Args.MakeArgString(Device));
+ for (StringRef Device : getCodeDevices(Binary))
+ DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
+ Args.MakeArgString(Device));
// If every input file is bitcode we have whole program visibility as we
// do only support static linking with bitcode.
@@ -1417,12 +1421,11 @@ Error emitExtractCommands(
ImageArg += (",arch=" + Binary.getArch()).str();
// Filter on "code", including an empty one, so that an image without
// "code" does not also match one with it for the same architecture.
- StringRef Code = Binary.getString("code");
- if (Code.empty())
+ SmallVector<StringRef> Devices = getCodeDevices(Binary);
+ if (Devices.empty())
ImageArg += ",code=";
- for (StringRef Device : llvm::split(Code, ','))
- if (!Device.empty())
- ImageArg += (",code=" + Device).str();
+ for (StringRef Device : Devices)
+ ImageArg += (",code=" + Device).str();
file_magic Magic;
if (!identify_magic(Source, Magic) && Magic == file_magic::archive)
ImageArg += (",member=" + sys::path::filename(Identifier)).str();
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 735e3ecbdb37e3..6b54f215f34c12 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -746,7 +746,8 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
/// into the AOT compilation step.
static Error runAOTCompile(StringRef InputFile, StringRef OutputFile,
const ArgList &Args) {
- OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
+ StringRef Arch = Args.getLastArgValue(OPT_arch_EQ);
+ OffloadArch OA = StringToOffloadArch(Arch);
if (OA.isIntelGPU() || OA.isIntelIGCA())
return runAOTCompileIntelGPU(InputFile, OutputFile, Args);
if (OA.isIntelCPU())
>From 69e9ae4425a30511cf85f95b11dbe62ad345ee73 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 20 Nov 2026 16:27:49 -0800
Subject: [PATCH 37/51] [Clang][SYCL] Simplify a comment on the device link
output name
Self code review findings.
---
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 9b28234357b9eb..157d820abcd928 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -502,8 +502,8 @@ 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();
- // Add the real devices to the output name, so that device links for the
- // same architecture don't overwrite each other's output file.
+ // Two device links can have the same architecture, e.g. igca_30r for
+ // xe-mtl-u and for xe-mtl-h, so add the real devices to the output name.
std::vector<std::string> Devices = Args.getAllArgValues(OPT_code_EQ);
for (const std::string &Device : Devices)
OutputFileBase += "." + Device;
>From b041a8d6b7b0aae3a1763fa43f3cd2e333d3fff4 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Mon, 23 Nov 2026 10:23:24 -0800
Subject: [PATCH 38/51] [Clang][SYCL] Address review: drop intelGPUIGCATarget,
keep "code" out of non-SYCL flows
---
clang/include/clang/Basic/OffloadArch.h | 3 ---
clang/lib/Basic/OffloadArch.cpp | 10 +++-------
clang/lib/Driver/Driver.cpp | 3 +++
clang/lib/Driver/ToolChains/Clang.cpp | 5 ++---
.../clang-linker-wrapper/linker-wrapper-verbose.c | 12 ++++++------
.../clang-linker-wrapper/ClangLinkerWrapper.cpp | 11 ++++++++---
clang/unittests/Basic/OffloadArchTest.cpp | 8 --------
7 files changed, 22 insertions(+), 30 deletions(-)
diff --git a/clang/include/clang/Basic/OffloadArch.h b/clang/include/clang/Basic/OffloadArch.h
index 84a25e5caf31ad..fbf51ffab67922 100644
--- a/clang/include/clang/Basic/OffloadArch.h
+++ b/clang/include/clang/Basic/OffloadArch.h
@@ -110,9 +110,6 @@ 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/lib/Basic/OffloadArch.cpp b/clang/lib/Basic/OffloadArch.cpp
index 77c2055ce02c94..b95ab8a6119f2a 100644
--- a/clang/lib/Basic/OffloadArch.cpp
+++ b/clang/lib/Basic/OffloadArch.cpp
@@ -35,12 +35,6 @@ 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:
@@ -77,7 +71,9 @@ const char *OffloadArchToVirtualArchString(OffloadArch A) {
case OffloadArch::TargetArch::IntelIGCA:
return llvm::IntelGPU::getIGCATargetName(A.igcaTarget()).data();
case OffloadArch::TargetArch::IntelGPU:
- return OffloadArchToString(A.intelGPUIGCATarget());
+ return llvm::IntelGPU::getIGCATargetName(
+ llvm::IntelGPU::getIGCATarget(A.intelGPUKind()))
+ .data();
case OffloadArch::TargetArch::Unused:
case OffloadArch::TargetArch::IntelCPU:
case OffloadArch::TargetArch::Generic:
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 40a3196daf0b54..a2d741f3b202ef 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4154,6 +4154,9 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
// 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.
+ // TODO: Do this for other offload kinds too. Their SPIR-V device link
+ // (spirv-link or llvm-lto) doesn't run ocloc for the "code" list yet; only
+ // the SYCL device link (clang-sycl-linker) does.
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 db9d2860fd29f5..ee095d2d210f9a 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9793,9 +9793,8 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
// Add one "code=" per Intel GPU requested for this 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.getIGCADevices(TC, Arch.ArchName))
- Parts.emplace_back("code=" + Device.str());
+ for (StringRef Device : C.getIGCADevices(TC, Arch.ArchName))
+ Parts.emplace_back("code=" + Device.str());
if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
for (StringRef Feature : FeatureArgs)
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 1903dae56aa13c..a2f19d998e49b3 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-verbose.c
@@ -12,7 +12,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=OPENMP
-// OPENMP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,file={{.*}}.o --image=kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx90a,code=,file={{.*}}.o
+// OPENMP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,file={{.*}}.o --image=kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx90a,file={{.*}}.o
// OPENMP: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70
// OPENMP: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx90a
// OPENMP: llvm-offload-binary{{.*}} -o {{.*}}.offload --image=file={{.*}}.img,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70
@@ -40,7 +40,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=OPENACC
-// OPENACC: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openacc,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,file={{.*}}.o
+// OPENACC: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=openacc,triple=nvptx64-nvidia-cuda,arch=sm_70,file={{.*}}.o
// OPENACC: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70
// OPENACC: llvm-offload-binary{{.*}} -o {{.*}}.offload --image=file={{.*}}.img,kind=openacc,triple=nvptx64-nvidia-cuda,arch=sm_70
// OPENACC: llvm-offload-wrapper{{.*}} --kind=openacc --triple=x86_64-unknown-linux-gnu -o [[BC:.*]].bc {{.*}}.offload
@@ -64,7 +64,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=CUDA
-// CUDA: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,file={{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_52,code=,file={{.*}}.o
+// CUDA: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_70,file={{.*}}.o --image=kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_52,file={{.*}}.o
// CUDA: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70
// CUDA: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_52
// CUDA: fatbinary{{.*}}--create [[FB:.*]].fatbin {{.*}}--image3=kind=elf,sm=70{{.*}}--image3=kind=elf,sm=52
@@ -81,7 +81,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=HIP
-// HIP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx90a,code=,file={{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx908,code=,file={{.*}}.o
+// HIP: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx90a,file={{.*}}.o --image=kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx908,file={{.*}}.o
// HIP: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx90a
// HIP: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx908
// HIP: clang-offload-bundler{{.*}}-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a,hip-amdgcn-amd-amdhsa--gfx908{{.*}}-output=[[FB:.*]].hipfb
@@ -98,7 +98,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,code=,file={{.*}}.o
+// SYCL: llvm-offload-binary{{.*}} {{.*}}.o --image=kind=sycl,triple=spirv64-unknown-unknown,arch=generic,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
@@ -122,4 +122,4 @@
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --wrapper-verbose --save-temps --dry-run \
// RUN: --should-extract=sm_70 --linker-path=/usr/bin/ld %t.a -o a.out 2>&1 | FileCheck %s --check-prefix=ARCHIVE
-// ARCHIVE: llvm-offload-binary{{.*}} {{.*}}.a --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,code=,member={{.*}}.o,file={{.*}}.o
+// ARCHIVE: llvm-offload-binary{{.*}} {{.*}}.a --image=kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70,member={{.*}}.o,file={{.*}}.o
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 157d820abcd928..5a66249efa59b0 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1401,6 +1401,13 @@ Error emitExtractCommands(
StringSaver Saver(Alloc);
MapVector<StringRef, SmallVector<StringRef>> Commands;
DenseSet<StringRef> Seen;
+ // Filter on an empty "code" only if some image has one, so that an image
+ // without "code" does not also match one with it for the same architecture.
+ bool AnyCode = llvm::any_of(InputsForTarget, [](const auto &Input) {
+ return llvm::any_of(Input, [](const OffloadFile &File) {
+ return !File.getBinary()->getString("code").empty();
+ });
+ });
for (const auto &Input : InputsForTarget) {
for (const OffloadFile &File : Input) {
const OffloadBinary &Binary = *File.getBinary();
@@ -1419,10 +1426,8 @@ Error emitExtractCommands(
.str();
if (!Binary.getArch().empty())
ImageArg += (",arch=" + Binary.getArch()).str();
- // Filter on "code", including an empty one, so that an image without
- // "code" does not also match one with it for the same architecture.
SmallVector<StringRef> Devices = getCodeDevices(Binary);
- if (Devices.empty())
+ if (Devices.empty() && AnyCode)
ImageArg += ",code=";
for (StringRef Device : Devices)
ImageArg += (",code=" + Device).str();
diff --git a/clang/unittests/Basic/OffloadArchTest.cpp b/clang/unittests/Basic/OffloadArchTest.cpp
index 7360ef557b6f1c..92e7fad7f10cb3 100644
--- a/clang/unittests/Basic/OffloadArchTest.cpp
+++ b/clang/unittests/Basic/OffloadArchTest.cpp
@@ -87,14 +87,6 @@ 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, VirtualArch) {
// The virtual architecture of an Intel GPU is its IGCA target.
EXPECT_STREQ(OffloadArchToVirtualArchString(parse("xe-mtl-u")), "igca_30r");
>From 999f378d308dbec8c361455a53b28190e971d249 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Mon, 23 Nov 2026 11:42:14 -0800
Subject: [PATCH 39/51] [Clang][SYCL] Address review: note "code" is
Intel-only, add a TODO
---
clang/docs/ClangLinkerWrapper.md | 3 +++
clang/lib/Driver/ToolChains/Clang.cpp | 1 +
2 files changed, 4 insertions(+)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index 018ad82acfc1f0..95067559609eb5 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -88,6 +88,9 @@ into `generic` device code.
The `code` list is only needed when the architecture is a virtual one that
several devices share, and it names the real devices to compile the device code
for ahead of time.
+Currently, only SYCL device code for Intel GPUs has a `code` list: the
+architecture is an IGCA target, e.g. `igca_30r`, and `code` lists its Intel
+GPUs, e.g. `xe-mtl-u`.
## Debugging
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index ee095d2d210f9a..dfddd99b555509 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9994,6 +9994,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
StringRef Name = TargetID ? StringRef(*TargetID) : Arg;
// A SYCL device image for an Intel GPU is labeled with the GPU's IGCA
// target.
+ // TODO: Drop the SYCL check along with the one in getOffloadArchs.
if (Kind == Action::OFK_SYCL)
if (OffloadArch ID = StringToOffloadArch(Name); ID.isIntelGPU())
Name = OffloadArchToVirtualArchString(ID);
>From d4f2327a317133de4c654d9d9ce925121e85d0aa Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 25 Nov 2026 15:00:34 -0800
Subject: [PATCH 40/51] [Clang][SYCL] Plainer names, comments and help text;
report the option as unused
Self code review findings.
---
clang/include/clang/Driver/Compilation.h | 22 +++++-----
clang/include/clang/Options/Options.td | 10 ++---
clang/lib/Driver/Driver.cpp | 4 +-
clang/lib/Driver/ToolChains/Clang.cpp | 17 ++++----
clang/test/Driver/sycl-embed-virtual-arch.cpp | 21 ++++++----
.../OffloadTools/clang-sycl-linker/basic.ll | 4 +-
.../ClangLinkerWrapper.cpp | 40 +++++++++++--------
.../clang-linker-wrapper/LinkerWrapperOpts.td | 3 ++
.../clang-sycl-linker/ClangSYCLLinker.cpp | 15 ++++---
9 files changed, 77 insertions(+), 59 deletions(-)
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 9874c53b362df9..c7a4504d7d799b 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -106,9 +106,9 @@ class Compilation {
llvm::SmallVector<StringRef, 2>>
IGCADevices;
- /// The --offload-arch values that are not Intel GPUs, e.g. igca_30r, for
- /// each device toolchain. An IGCA target in this set keeps its SPIR-V.
- llvm::DenseSet<std::pair<const ToolChain *, StringRef>> DirectArchs;
+ /// The IGCA targets given directly in --offload-arch, e.g. igca_30r, for each
+ /// device toolchain.
+ llvm::DenseSet<std::pair<const ToolChain *, StringRef>> RequestedIGCATargets;
/// Temporary files which should be removed on exit.
llvm::opt::ArgStringList TempFiles;
@@ -278,16 +278,16 @@ class Compilation {
ArrayRef<StringRef> getIGCADevices(const ToolChain *TC,
StringRef Target) const;
- /// Record \p Arch as an --offload-arch value that is not an Intel GPU for
- /// the device toolchain \p TC.
- void addDirectArch(const ToolChain *TC, StringRef Arch) {
- DirectArchs.insert({TC, Arch});
+ /// Remember that \p Target was given directly in --offload-arch for the
+ /// device toolchain \p TC.
+ void addRequestedIGCATarget(const ToolChain *TC, StringRef Target) {
+ RequestedIGCATargets.insert({TC, Target});
}
- /// \return true if \p Arch is an --offload-arch value that is not an Intel
- /// GPU for the device toolchain \p TC.
- bool isDirectArch(const ToolChain *TC, StringRef Arch) const {
- return DirectArchs.contains({TC, Arch});
+ /// \return true if \p Target was given directly in --offload-arch for the
+ /// device toolchain \p TC.
+ bool isRequestedIGCATarget(const ToolChain *TC, StringRef Target) const {
+ return RequestedIGCATargets.contains({TC, Target});
}
/// addTempFile - Add a file to remove on exit, and returns its
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index e36ae7a4b84c1e..8fc21e3135c6ac 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1286,11 +1286,11 @@ def no_offload_arch_EQ : CommaJoined<["--"], "no-offload-arch=">,
defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
"offload-embed-virtual-arch",
- PosFlag<SetTrue, [], [ClangOption], "Also embed the device code for the "
- "virtual architecture when --offload-arch names a real architecture "
- "(default for SYCL)">,
- NegFlag<SetFalse, [], [ClangOption], "Do not embed the device code for the "
- "virtual architecture when --offload-arch names a real architecture">,
+ PosFlag<SetTrue, [], [ClangOption], "Keep the SPIR-V in device code compiled "
+ "ahead of time for an Intel GPU, so it can still run on other GPUs "
+ "(default, SYCL only)">,
+ NegFlag<SetFalse, [], [ClangOption], "Drop the SPIR-V from device code "
+ "compiled ahead of time for an Intel GPU (SYCL only)">,
BothFlags<[]>, ["--"]>;
def offload_new_driver : Flag<["--"], "offload-new-driver">,
Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9381884ec5fc0f..1ff3d63c8c5278 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4174,8 +4174,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
Targets.insert(Target);
if (ID.isIntelGPU())
C.addIGCADevice(&TC, Target, Arch);
- else
- C.addDirectArch(&TC, Arch);
+ else if (ID.isIntelIGCA())
+ C.addRequestedIGCATarget(&TC, Arch);
}
Archs = std::move(Targets);
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index c12c2274d86334..401528b345c8a4 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9798,13 +9798,16 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
for (StringRef Device : Devices)
Parts.emplace_back("code=" + Device.str());
- // Drop the SPIR-V only from a device image compiled ahead of time, and
- // keep it if its IGCA target is also in --offload-arch.
- bool EmbedVirtualArch =
- TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
- options::OPT_no_offload_embed_virtual_arch, true);
- if (!EmbedVirtualArch && !Devices.empty() &&
- !C.isDirectArch(TC, Arch.ArchName))
+ // Ask to drop the SPIR-V only for device code that is compiled ahead of
+ // time. If the user also gave the IGCA target itself in --offload-arch,
+ // they want the SPIR-V, so keep it.
+ // Read the option only here, so that it is reported as unused when it
+ // can't do anything.
+ bool DropSPIRV =
+ !Devices.empty() &&
+ !TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
+ options::OPT_no_offload_embed_virtual_arch, true);
+ if (DropSPIRV && !C.isRequestedIGCATarget(TC, Arch.ArchName))
Parts.emplace_back("no-embed-virtual-arch=1");
if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
index 66d9f74cd958df..18a6fd5782d535 100644
--- a/clang/test/Driver/sycl-embed-virtual-arch.cpp
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -17,12 +17,14 @@
// RUN: | FileCheck -check-prefix=NO-EMBED %s
// NO-EMBED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u,no-embed-virtual-arch=1"{{$}}
-/// It has no effect for an IGCA target in --offload-arch, which is compiled
-/// just in time.
+/// Where the option can't do anything, it is reported as unused.
+// UNUSED: warning: argument unused during compilation: '--no-offload-embed-virtual-arch'
+
+/// It has no effect for an IGCA target given in --offload-arch, so it is
+/// reported as unused.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=igca_30r --no-offload-embed-virtual-arch -c %s 2>&1 \
-// RUN: | FileCheck -check-prefix=DIRECT %s
-// DIRECT-NOT: warning:
+// RUN: | FileCheck -check-prefixes=DIRECT,UNUSED %s
// DIRECT: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl"{{$}}
/// It still has no effect when one of the IGCA target's Intel GPUs is also
@@ -39,9 +41,14 @@
// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u"
// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_40r,kind=sycl,code=xe-bmg-g21,no-embed-virtual-arch=1"
-/// It has no effect without --offload-arch.
+/// It has no effect without --offload-arch, so it is reported as unused.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --no-offload-embed-virtual-arch -c %s 2>&1 \
-// RUN: | FileCheck -check-prefix=GENERIC %s
-// GENERIC-NOT: warning:
+// RUN: | FileCheck -check-prefixes=GENERIC,UNUSED %s
// GENERIC: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=generic,kind=sycl"{{$}}
+
+/// It is SYCL only, so it is reported as unused for OpenMP.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
+// RUN: --offload-arch=sm_70 -nogpulib --no-offload-embed-virtual-arch -c %s \
+// RUN: 2>&1 | FileCheck -check-prefix=UNUSED %s
+
diff --git a/clang/test/OffloadTools/clang-sycl-linker/basic.ll b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
index 17dd232e479ab2..fd7a6d9f24bebd 100644
--- a/clang/test/OffloadTools/clang-sycl-linker/basic.ll
+++ b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
@@ -134,8 +134,8 @@
; AOT-IGCA-NEXT: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: igca_30r
; AOT-IGCA-NOT: {{.+}}
;
-; Test that --no-embed-virtual-arch drops the SPIR-V from the ocloc output and
-; is recorded on the device image.
+; Test that --no-embed-virtual-arch passes -exclude_ir to ocloc and is
+; recorded on the device image.
; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --code=xe-mtl-u --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/aot-igca-noembed.out 2>&1 \
; RUN: | FileCheck %s --check-prefix=AOT-IGCA-NO-EMBED
; AOT-IGCA-NO-EMBED: "{{.*}}ocloc{{.*}}" {{.*}}-device xe-mtl-u -exclude_ir -output
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index ed1eb9010aa97f..7b48ddea05259d 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -125,6 +125,9 @@ static std::string CudaBinaryPath;
/// Mutex lock to protect writes to shared TempFiles in parallel.
static std::mutex TempFilesMutex;
+/// Device links run in parallel, so warnings from them take turns.
+static std::mutex WarningMutex;
+
/// Temporary files created by the linker wrapper.
static std::list<SmallString<128>> TempFiles;
@@ -597,6 +600,8 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
CmdArgs.push_back("--sycl-link");
for (const std::string &Device : Devices)
CmdArgs.append({"-Xlinker", Args.MakeArgString("--code=" + Device)});
+ if (Args.hasArg(OPT_no_embed_virtual_arch))
+ CmdArgs.append({"-Xlinker", "--no-embed-virtual-arch"});
}
for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
@@ -1098,23 +1103,24 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
Args.MakeArgString(Device));
// Drop the SPIR-V only if every device image that lists real devices in
- // "code" asks for it. Generic archive members list none, so they don't count.
- if (Binary.getOffloadKind() == OFK_SYCL) {
- auto HasKey = [](StringRef Key) {
- return [=](const OffloadFile &File) {
- return !File.getBinary()->getString(Key).empty();
- };
- };
- size_t WithCode = llvm::count_if(Input, HasKey("code"));
- size_t NoEmbed = llvm::count_if(Input, HasKey("no-embed-virtual-arch"));
- if (WithCode && NoEmbed == WithCode)
- DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_linker_arg_EQ),
- "--no-embed-virtual-arch");
- else if (NoEmbed)
- WithColor::warning(errs(), LinkerExecutable)
- << "not all device code for '" << Binary.getArch()
- << "' was compiled with --no-offload-embed-virtual-arch; the "
- "SPIR-V is kept\n";
+ // "code" asked for it. Generic archive members list no devices, so they
+ // don't get a say.
+ auto HasCode = [](const OffloadFile &File) {
+ return !File.getBinary()->getString("code").empty();
+ };
+ auto AsksToDrop = [](const OffloadFile &File) {
+ return !File.getBinary()->getString("no-embed-virtual-arch").empty();
+ };
+ size_t NumWithCode = llvm::count_if(Input, HasCode);
+ size_t NumAskingToDrop = llvm::count_if(Input, AsksToDrop);
+ if (NumWithCode > 0 && NumAskingToDrop == NumWithCode)
+ DAL.AddFlagArg(nullptr, Tbl.getOption(OPT_no_embed_virtual_arch));
+ else if (NumAskingToDrop > 0) {
+ std::scoped_lock<std::mutex> Lock(WarningMutex);
+ WithColor::warning(errs(), LinkerExecutable)
+ << "not all device code for '" << Binary.getArch()
+ << "' was compiled with --no-offload-embed-virtual-arch; the "
+ "SPIR-V is kept\n";
}
// If every input file is bitcode we have whole program visibility as we
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index 16478e5e0ca612..efa8a179724d43 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -83,6 +83,9 @@ def triple_EQ : Joined<["--"], "triple=">,
def code_EQ : Joined<["--"], "code=">,
Flags<[DeviceOnlyOption, HelpHidden]>, MetaVarName<"<device>">,
HelpText<"A real device to compile an image ahead of time for">;
+def no_embed_virtual_arch : Flag<["--"], "no-embed-virtual-arch">,
+ Flags<[DeviceOnlyOption, HelpHidden]>,
+ HelpText<"Drop the SPIR-V from device code compiled ahead of time">;
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 dc8d6ecfbc07a1..548259b1a75dd4 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -722,8 +722,7 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
assert(!Devices.empty() && "a device must be specified for AOT compilation");
CmdArgs.push_back("-device");
CmdArgs.push_back(Devices);
- // Without -exclude_ir, ocloc keeps the SPIR-V in its output next to the
- // native code.
+ // Unless it is given -exclude_ir, ocloc keeps the SPIR-V in its output.
if (Args.hasArg(OPT_no_embed_virtual_arch))
CmdArgs.push_back("-exclude_ir");
@@ -1011,10 +1010,10 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
bool IsAOTCompileNeeded =
OA.isIntelIGCA() ? Args.hasArg(OPT_code_EQ) : OA.isIntel();
- // Only ocloc, for an Intel GPU, can drop the SPIR-V; opencl-aot, for an
- // Intel CPU, cannot.
- bool NoEmbedVirtualArch = IsAOTCompileNeeded && !OA.isIntelCPU() &&
- Args.hasArg(OPT_no_embed_virtual_arch);
+ // Only ocloc, used for Intel GPUs, can drop the SPIR-V (see
+ // runAOTCompileIntelGPU). opencl-aot, used for an Intel CPU, can't.
+ bool DropSPIRV = IsAOTCompileNeeded && !OA.isIntelCPU() &&
+ Args.hasArg(OPT_no_embed_virtual_arch);
StringRef OutputFileNameExt = ".spv";
@@ -1064,7 +1063,7 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
TheImage.StringData["arch"] =
Args.MakeArgString(Args.getLastArgValue(OPT_arch_EQ));
TheImage.StringData["symbols"] = SI.Symbols;
- if (NoEmbedVirtualArch)
+ if (DropSPIRV)
TheImage.StringData["no-embed-virtual-arch"] = "1";
TheImage.Image = std::move(*FileOrErr);
Images.emplace_back(std::move(TheImage));
@@ -1076,7 +1075,7 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
getImageKindName(Image.TheImageKind),
Image.StringData.lookup("triple"),
Image.StringData.lookup("arch"));
- if (Image.StringData.count("no-embed-virtual-arch"))
+ if (Image.StringData.contains("no-embed-virtual-arch"))
errs() << ", no-embed-virtual-arch";
errs() << "\n";
}
>From 2b54c94e83bf65dff56d95f25f36c596d62c0e7d Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 25 Nov 2026 15:35:42 -0800
Subject: [PATCH 41/51] [Clang][SYCL] Merge two comments in OffloadPackager
Self code review findings.
---
clang/lib/Driver/ToolChains/Clang.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 401528b345c8a4..647892849065d1 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9799,10 +9799,9 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
Parts.emplace_back("code=" + Device.str());
// Ask to drop the SPIR-V only for device code that is compiled ahead of
- // time. If the user also gave the IGCA target itself in --offload-arch,
- // they want the SPIR-V, so keep it.
- // Read the option only here, so that it is reported as unused when it
- // can't do anything.
+ // time, and keep it if the user also gave the IGCA target itself in
+ // --offload-arch. The option is read only here, so clang reports it as
+ // unused where it can't do anything.
bool DropSPIRV =
!Devices.empty() &&
!TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
>From 605a5fb67d41a87b18f7b880df764a58d9c53f3d Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 1 Dec 2026 12:25:20 -0800
Subject: [PATCH 42/51] [Clang][SYCL] Update a driver comment and the docs
Self code review findings.
---
clang/docs/ClangLinkerWrapper.md | 2 ++
clang/lib/Driver/Driver.cpp | 4 ++--
clang/test/Driver/sycl-embed-virtual-arch.cpp | 1 -
3 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index 2fe858df451f19..e4531cb451bb80 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -99,6 +99,8 @@ GPUs was compiled with it:
- If all of them were, the SPIR-V is dropped.
- If only some were, the SPIR-V is kept, and a warning says so.
+Generic archive members don't count, since they aren't compiled for any GPU.
+
```console
# SPIR-V dropped: a.o and b.o were both compiled with --no-offload-embed-virtual-arch.
clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c a.cpp -o a.o
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 1ff3d63c8c5278..647a76d1dcd1de 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4158,8 +4158,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
// 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.
+ // xe-mtl-u and xe-mtl-h. Record the Intel GPUs to compile for ahead of time,
+ // and the IGCA targets given directly in --offload-arch.
// TODO: Do this for other offload kinds too. Their SPIR-V device link
// (spirv-link or llvm-lto) doesn't run ocloc for the "code" list yet; only
// the SYCL device link (clang-sycl-linker) does.
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
index 18a6fd5782d535..9e32278fc8e0a0 100644
--- a/clang/test/Driver/sycl-embed-virtual-arch.cpp
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -51,4 +51,3 @@
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
// RUN: --offload-arch=sm_70 -nogpulib --no-offload-embed-virtual-arch -c %s \
// RUN: 2>&1 | FileCheck -check-prefix=UNUSED %s
-
>From 1fd5f19c77504ffe7ac8dfee5f8b7eefc674de96 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Tue, 1 Dec 2026 17:54:22 -0800
Subject: [PATCH 43/51] [Clang][SYCL] Drop the no-embed-virtual-arch key from
clang-sycl-linker output
The SYCL runtime no longer reads it: specialization constants are always
native in --offload-arch mode. Note in the docs and the option help that a
program without the SPIR-V can't set specialization constants to non-default
values.
---
clang/docs/ClangLinkerWrapper.md | 5 +++--
clang/docs/ReleaseNotes.md | 3 ++-
clang/include/clang/Options/Options.td | 3 ++-
.../OffloadTools/clang-sycl-linker/basic.ll | 8 +++-----
.../clang-sycl-linker/ClangSYCLLinker.cpp | 20 +++++--------------
5 files changed, 15 insertions(+), 24 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index e4531cb451bb80..365bce60f9e346 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -93,8 +93,9 @@ architecture is an IGCA target, e.g. `igca_30r`, and `code` lists its Intel
GPUs, e.g. `xe-mtl-u`.
`--no-offload-embed-virtual-arch` drops the SPIR-V from device code compiled
-ahead of time. It only takes effect if every object that is linked for the same
-GPUs was compiled with it:
+ahead of time. Without the SPIR-V, a SYCL program can't set specialization
+constants to non-default values. The option only takes effect if every object
+that is linked for the same GPUs was compiled with it:
- If all of them were, the SPIR-V is dropped.
- If only some were, the SPIR-V is kept, and a warning says so.
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d54a357c8b8e9..02eb87fd82759d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1877,7 +1877,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
for Intel GPUs does not keep the SPIR-V for just-in-time compilation. To
apply it to one IGCA target only, use `-Xarch_<IGCA target>`, e.g.
`-Xarch_igca_40r --no-offload-embed-virtual-arch`; it then applies to all
- Intel GPUs of that IGCA target.
+ Intel GPUs of that IGCA target. Without the SPIR-V, a SYCL program can't set
+ specialization constants to non-default values.
#### Improvements
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 8fc21e3135c6ac..2f437dc016c83b 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1290,7 +1290,8 @@ defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
"ahead of time for an Intel GPU, so it can still run on other GPUs "
"(default, SYCL only)">,
NegFlag<SetFalse, [], [ClangOption], "Drop the SPIR-V from device code "
- "compiled ahead of time for an Intel GPU (SYCL only)">,
+ "compiled ahead of time for an Intel GPU (SYCL only). The program then "
+ "can't set specialization constants to non-default values">,
BothFlags<[]>, ["--"]>;
def offload_new_driver : Flag<["--"], "offload-new-driver">,
Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
diff --git a/clang/test/OffloadTools/clang-sycl-linker/basic.ll b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
index fd7a6d9f24bebd..68f54c43482ebb 100644
--- a/clang/test/OffloadTools/clang-sycl-linker/basic.ll
+++ b/clang/test/OffloadTools/clang-sycl-linker/basic.ll
@@ -134,12 +134,10 @@
; AOT-IGCA-NEXT: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: igca_30r
; AOT-IGCA-NOT: {{.+}}
;
-; Test that --no-embed-virtual-arch passes -exclude_ir to ocloc and is
-; recorded on the device image.
+; Test that --no-embed-virtual-arch passes -exclude_ir to ocloc.
; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --code=xe-mtl-u --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/aot-igca-noembed.out 2>&1 \
; RUN: | FileCheck %s --check-prefix=AOT-IGCA-NO-EMBED
; AOT-IGCA-NO-EMBED: "{{.*}}ocloc{{.*}}" {{.*}}-device xe-mtl-u -exclude_ir -output
-; AOT-IGCA-NO-EMBED-NEXT: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: igca_30r, no-embed-virtual-arch{{$}}
;
; Test that --no-embed-virtual-arch has no effect without --code.
; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=igca_30r --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/jit-igca-noembed.out 2>&1 \
@@ -150,14 +148,14 @@
; 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-NEXT: sycl-bundle: image kind: spv, triple: spirv64-unknown-unknown, arch: igca_30r
; JIT-IGCA-NOT: {{.+}}
;
; Test that --no-embed-virtual-arch has no effect for an Intel CPU.
; RUN: clang-sycl-linker --dry-run -v --module-split-mode=link_unit -arch=graniterapids --no-embed-virtual-arch %t/input1.bc %t/input2.bc -o %t/aot-cpu-noembed.out 2>&1 \
; RUN: | FileCheck %s --check-prefix=AOT-INTEL-CPU-NO-EMBED
; AOT-INTEL-CPU-NO-EMBED-NOT: -exclude_ir
-; AOT-INTEL-CPU-NO-EMBED: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: graniterapids{{$}}
+; AOT-INTEL-CPU-NO-EMBED: sycl-bundle: image kind: o, triple: spirv64-unknown-unknown, arch: graniterapids
;
; Test AOT compilation for an Intel CPU.
; Test that IMG_Object image kind is set for AOT compilation (Intel CPU).
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 548259b1a75dd4..4c2d96b5a866d2 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -1010,10 +1010,6 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
OffloadArch OA = StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ));
bool IsAOTCompileNeeded =
OA.isIntelIGCA() ? Args.hasArg(OPT_code_EQ) : OA.isIntel();
- // Only ocloc, used for Intel GPUs, can drop the SPIR-V (see
- // runAOTCompileIntelGPU). opencl-aot, used for an Intel CPU, can't.
- bool DropSPIRV = IsAOTCompileNeeded && !OA.isIntelCPU() &&
- Args.hasArg(OPT_no_embed_virtual_arch);
StringRef OutputFileNameExt = ".spv";
@@ -1063,22 +1059,16 @@ static Error runSYCLLink(ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs,
TheImage.StringData["arch"] =
Args.MakeArgString(Args.getLastArgValue(OPT_arch_EQ));
TheImage.StringData["symbols"] = SI.Symbols;
- if (DropSPIRV)
- TheImage.StringData["no-embed-virtual-arch"] = "1";
TheImage.Image = std::move(*FileOrErr);
Images.emplace_back(std::move(TheImage));
}
if (Verbose) {
- for (const OffloadingImage &Image : Images) {
- errs() << formatv("sycl-bundle: image kind: {0}, triple: {1}, arch: {2}",
- getImageKindName(Image.TheImageKind),
- Image.StringData.lookup("triple"),
- Image.StringData.lookup("arch"));
- if (Image.StringData.contains("no-embed-virtual-arch"))
- errs() << ", no-embed-virtual-arch";
- errs() << "\n";
- }
+ for (const OffloadingImage &Image : Images)
+ errs() << formatv(
+ "sycl-bundle: image kind: {0}, triple: {1}, arch: {2}\n",
+ getImageKindName(Image.TheImageKind),
+ Image.StringData.lookup("triple"), Image.StringData.lookup("arch"));
}
llvm::SmallString<0> Buffer = OffloadBinary::write(Images);
>From abc0e6c032fd17fe6816d57ec0648474e51bf39e Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 2 Dec 2026 11:44:31 -0800
Subject: [PATCH 44/51] [Clang][SYCL] Record embed-virtual-arch=0 and use
neutral wording
Use one positive key, embed-virtual-arch, instead of no-embed-virtual-arch=1,
so a later CUDA change can record embed-virtual-arch=1 with the same key.
Make the clang-linker-wrapper warning, its comment and the option help
neutral about the virtual architecture.
---
clang/docs/ClangLinkerWrapper.md | 2 +-
clang/include/clang/Options/Options.td | 13 +++++++------
clang/lib/Driver/ToolChains/Clang.cpp | 2 +-
clang/test/Driver/sycl-embed-virtual-arch.cpp | 4 ++--
.../clang-linker-wrapper/linker-wrapper.c | 4 ++--
.../clang-linker-wrapper/ClangLinkerWrapper.cpp | 11 ++++++-----
.../tools/clang-linker-wrapper/LinkerWrapperOpts.td | 3 ++-
7 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index 365bce60f9e346..a0fe8d48538176 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -112,7 +112,7 @@ clang++ -fsycl --offload-arch=xe-mtl-u a.o b.o -o app
clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c a.cpp -o a.o
clang++ -fsycl --offload-arch=xe-mtl-u -c b.cpp -o b.o
clang++ -fsycl --offload-arch=xe-mtl-u a.o b.o -o app
-clang-linker-wrapper: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the SPIR-V is kept
+clang-linker-wrapper: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the device code for the virtual architecture is kept
```
## Debugging
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 2f437dc016c83b..45d6245b725580 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1286,12 +1286,13 @@ def no_offload_arch_EQ : CommaJoined<["--"], "no-offload-arch=">,
defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
"offload-embed-virtual-arch",
- PosFlag<SetTrue, [], [ClangOption], "Keep the SPIR-V in device code compiled "
- "ahead of time for an Intel GPU, so it can still run on other GPUs "
- "(default, SYCL only)">,
- NegFlag<SetFalse, [], [ClangOption], "Drop the SPIR-V from device code "
- "compiled ahead of time for an Intel GPU (SYCL only). The program then "
- "can't set specialization constants to non-default values">,
+ PosFlag<SetTrue, [], [ClangOption], "Keep the device code for the virtual "
+ "architecture, e.g. SPIR-V, next to device code compiled ahead of time "
+ "for a real GPU, so it can still run on other GPUs (default, SYCL only)">,
+ NegFlag<SetFalse, [], [ClangOption], "Drop the device code for the virtual "
+ "architecture from device code compiled ahead of time for a real GPU "
+ "(SYCL only). A SYCL program then can't set specialization constants to "
+ "non-default values">,
BothFlags<[]>, ["--"]>;
def offload_new_driver : Flag<["--"], "offload-new-driver">,
Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 647892849065d1..e4875b8926df41 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9807,7 +9807,7 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
!TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
options::OPT_no_offload_embed_virtual_arch, true);
if (DropSPIRV && !C.isRequestedIGCATarget(TC, Arch.ArchName))
- Parts.emplace_back("no-embed-virtual-arch=1");
+ Parts.emplace_back("embed-virtual-arch=0");
if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
for (StringRef Feature : FeatureArgs)
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
index 9e32278fc8e0a0..ba08e8ef3ddda7 100644
--- a/clang/test/Driver/sycl-embed-virtual-arch.cpp
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -15,7 +15,7 @@
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=NO-EMBED %s
-// NO-EMBED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u,no-embed-virtual-arch=1"{{$}}
+// NO-EMBED: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u,embed-virtual-arch=0"{{$}}
/// Where the option can't do anything, it is reported as unused.
// UNUSED: warning: argument unused during compilation: '--no-offload-embed-virtual-arch'
@@ -39,7 +39,7 @@
// RUN: -Xarch_igca_40r --no-offload-embed-virtual-arch -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=XARCH %s
// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u"
-// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_40r,kind=sycl,code=xe-bmg-g21,no-embed-virtual-arch=1"
+// XARCH-DAG: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_40r,kind=sycl,code=xe-bmg-g21,embed-virtual-arch=0"
/// It has no effect without --offload-arch, so it is reported as unused.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 25b7c06f75a55b..582736e35a71a8 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -133,7 +133,7 @@ __attribute__((visibility("protected"), used)) int x;
// 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
// RUN: llvm-offload-binary -o %t.sycl-noembed.out \
-// RUN: --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,no-embed-virtual-arch=1
+// RUN: --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,embed-virtual-arch=0
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-noembed.o -fembed-offload-object=%t.sycl-noembed.out
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --linker-path=/usr/bin/ld %t.sycl-noembed.o %t.sycl-noembed.o \
@@ -196,7 +196,7 @@ __attribute__((visibility("protected"), used)) int x;
// for it. A generic archive member does not count.
// SYCL-NO-EMBED-NOT: warning:
// SYCL-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --no-embed-virtual-arch{{$}}
-// SYCL-SOME-NO-EMBED: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the SPIR-V is kept
+// SYCL-SOME-NO-EMBED: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the device code for the virtual architecture is kept
// SYCL-SOME-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u{{$}}
// The replayed extraction of an image without "code" does not also match one
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 7b48ddea05259d..fea706ec2574b2 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1102,14 +1102,15 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
Args.MakeArgString(Device));
- // Drop the SPIR-V only if every device image that lists real devices in
- // "code" asked for it. Generic archive members list no devices, so they
+ // Drop the device code for the virtual architecture only if every device
+ // image that lists real devices in "code" asked for it with
+ // "embed-virtual-arch=0". Generic archive members list no devices, so they
// don't get a say.
auto HasCode = [](const OffloadFile &File) {
return !File.getBinary()->getString("code").empty();
};
auto AsksToDrop = [](const OffloadFile &File) {
- return !File.getBinary()->getString("no-embed-virtual-arch").empty();
+ return File.getBinary()->getString("embed-virtual-arch") == "0";
};
size_t NumWithCode = llvm::count_if(Input, HasCode);
size_t NumAskingToDrop = llvm::count_if(Input, AsksToDrop);
@@ -1119,8 +1120,8 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
std::scoped_lock<std::mutex> Lock(WarningMutex);
WithColor::warning(errs(), LinkerExecutable)
<< "not all device code for '" << Binary.getArch()
- << "' was compiled with --no-offload-embed-virtual-arch; the "
- "SPIR-V is kept\n";
+ << "' was compiled with --no-offload-embed-virtual-arch; the device "
+ "code for the virtual architecture is kept\n";
}
// If every input file is bitcode we have whole program visibility as we
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index efa8a179724d43..b9bbd0b09dd089 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -85,7 +85,8 @@ def code_EQ : Joined<["--"], "code=">,
HelpText<"A real device to compile an image ahead of time for">;
def no_embed_virtual_arch : Flag<["--"], "no-embed-virtual-arch">,
Flags<[DeviceOnlyOption, HelpHidden]>,
- HelpText<"Drop the SPIR-V from device code compiled ahead of time">;
+ HelpText<"Drop the device code for the virtual architecture from device "
+ "code compiled ahead of time">;
def whole_program : Flag<["--"], "whole-program">,
Flags<[DeviceOnlyOption, HelpHidden]>,
HelpText<"LTO has visibility of all input files">;
>From 9fe1fd453554d903b66d03dd87a9b0956b08377a Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 2 Dec 2026 14:17:03 -0800
Subject: [PATCH 45/51] [Clang][SYCL] Address review: neutral
embed-virtual-arch handling and docs
Read --[no-]offload-embed-virtual-arch only where it can take effect.
In clang-linker-wrapper, ignore generic images, give a missing
embed-virtual-arch entry the default of the image's offload kind, and
name the link group in the warning. Make the option help and docs
language neutral.
---
clang/docs/ClangLinkerWrapper.md | 34 +++++--------
clang/docs/ClangSYCLLinker.md | 8 ++--
clang/docs/ReleaseNotes.md | 4 ++
clang/include/clang/Options/Options.td | 12 ++---
clang/lib/Driver/ToolChains/Clang.cpp | 14 +++---
clang/test/Driver/sycl-embed-virtual-arch.cpp | 9 ++--
.../clang-linker-wrapper/linker-wrapper.c | 6 +--
.../ClangLinkerWrapper.cpp | 48 +++++++++++--------
.../clang-linker-wrapper/LinkerWrapperOpts.td | 4 +-
clang/tools/clang-sycl-linker/SYCLLinkOpts.td | 5 +-
10 files changed, 74 insertions(+), 70 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index a0fe8d48538176..244d932bd4acf8 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -92,28 +92,18 @@ Currently, only SYCL device code for Intel GPUs has a `code` list: the
architecture is an IGCA target, e.g. `igca_30r`, and `code` lists its Intel
GPUs, e.g. `xe-mtl-u`.
-`--no-offload-embed-virtual-arch` drops the SPIR-V from device code compiled
-ahead of time. Without the SPIR-V, a SYCL program can't set specialization
-constants to non-default values. The option only takes effect if every object
-that is linked for the same GPUs was compiled with it:
-
-- If all of them were, the SPIR-V is dropped.
-- If only some were, the SPIR-V is kept, and a warning says so.
-
-Generic archive members don't count, since they aren't compiled for any GPU.
-
-```console
-# SPIR-V dropped: a.o and b.o were both compiled with --no-offload-embed-virtual-arch.
-clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c a.cpp -o a.o
-clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c b.cpp -o b.o
-clang++ -fsycl --offload-arch=xe-mtl-u a.o b.o -o app
-
-# SPIR-V kept, with a warning: only a.o was.
-clang++ -fsycl --offload-arch=xe-mtl-u --no-offload-embed-virtual-arch -c a.cpp -o a.o
-clang++ -fsycl --offload-arch=xe-mtl-u -c b.cpp -o b.o
-clang++ -fsycl --offload-arch=xe-mtl-u a.o b.o -o app
-clang-linker-wrapper: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the device code for the virtual architecture is kept
-```
+## Virtual Architecture Images
+
+The `embed-virtual-arch` string entry in an image's offload binary metadata
+records whether to embed the virtual architecture code (e.g. SPIR-V) alongside
+the code compiled ahead of time: `1` to embed it, `0` to drop it. A missing
+entry means the default for the image's offload kind.
+
+Within a link group, generic images (`arch=generic`, e.g. archive members) are
+ignored. Currently only SYCL acts on this entry: if all other images ask to drop
+the virtual architecture code, the linker wrapper passes
+`--no-embed-virtual-arch` to `clang-sycl-linker`. If only some of them do, it
+warns and does not pass the flag.
## Debugging
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index 754d1045392072..fcff76ae8dca27 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -47,7 +47,7 @@ OPTIONS:
- 'translation_unit' (default) emits one device image per translation unit that contains at least one entry point;
- 'kernel' emits one device image per kernel function;
- 'link_unit' emits one device image per linking unit.
- --no-embed-virtual-arch Drop the SPIR-V from a device image compiled ahead of time for an Intel GPU
+ --no-embed-virtual-arch Drop the virtual architecture code from a device image compiled ahead of time for an Intel GPU
--no-whole-archive Disable --whole-archive for the following inputs (default state)
--ocloc-options=<value> Options passed to ocloc for Intel GPU AOT compilation
--opencl-aot-options=<value>
@@ -131,9 +131,9 @@ time by the runtime:
clang-sycl-linker --triple=spirv64 --arch=igca_30r --code=xe-mtl-u --code=xe-mtl-h input.bc -o output.bin
```
-The SPIR-V is kept in the device image compiled ahead of time, so that it can
-be compiled just in time for other GPUs. With `--no-embed-virtual-arch`, it is
-dropped.
+ocloc also embeds the SPIR-V in its output, so that the device image can be
+compiled just in time for compatible GPUs not listed in `--code`;
+`--no-embed-virtual-arch` drops it (`ocloc -exclude_ir`).
### Linking with Device Libraries
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 02eb87fd82759d..42883482f9f1cd 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -409,6 +409,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### New Compiler Flags
+- Added `--[no-]offload-embed-virtual-arch`, which controls whether the virtual
+ architecture image is embedded alongside native code compiled for a real
+ architecture. It is on by default for SYCL.
+
- Added `-fpch-base-dir-is-cwd` to serialize precompiled header input paths
relative to the current working directory. Paths resolve against the
consumer's working directory when loading the PCH, allowing a build tree
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 45d6245b725580..c2c8a12d5e2c54 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1286,13 +1286,11 @@ def no_offload_arch_EQ : CommaJoined<["--"], "no-offload-arch=">,
defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
"offload-embed-virtual-arch",
- PosFlag<SetTrue, [], [ClangOption], "Keep the device code for the virtual "
- "architecture, e.g. SPIR-V, next to device code compiled ahead of time "
- "for a real GPU, so it can still run on other GPUs (default, SYCL only)">,
- NegFlag<SetFalse, [], [ClangOption], "Drop the device code for the virtual "
- "architecture from device code compiled ahead of time for a real GPU "
- "(SYCL only). A SYCL program then can't set specialization constants to "
- "non-default values">,
+ PosFlag<SetTrue, [], [ClangOption], "Embed the virtual architecture image "
+ "alongside native code compiled for a real architecture (default on for "
+ "SYCL)">,
+ NegFlag<SetFalse, [], [ClangOption], "Do not embed the virtual architecture "
+ "image alongside native code compiled for a real architecture">,
BothFlags<[]>, ["--"]>;
def offload_new_driver : Flag<["--"], "offload-new-driver">,
Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index e4875b8926df41..e1853e7a5cafe5 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9798,15 +9798,15 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
for (StringRef Device : Devices)
Parts.emplace_back("code=" + Device.str());
- // Ask to drop the SPIR-V only for device code that is compiled ahead of
- // time, and keep it if the user also gave the IGCA target itself in
- // --offload-arch. The option is read only here, so clang reports it as
- // unused where it can't do anything.
- bool DropSPIRV =
- !Devices.empty() &&
+ // Ask to drop the virtual architecture code only for device code that is
+ // compiled ahead of time, and keep it if the user also gave the IGCA
+ // target itself in --offload-arch. The option is read last, so clang
+ // reports it as unused where it can't do anything.
+ bool DropVirtualArch =
+ !Devices.empty() && !C.isRequestedIGCATarget(TC, Arch.ArchName) &&
!TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
options::OPT_no_offload_embed_virtual_arch, true);
- if (DropSPIRV && !C.isRequestedIGCATarget(TC, Arch.ArchName))
+ if (DropVirtualArch)
Parts.emplace_back("embed-virtual-arch=0");
if (TC->isUsingLTO(TCArgs, OffloadAction->getOffloadingDeviceKind()))
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
index ba08e8ef3ddda7..31f320b111d10b 100644
--- a/clang/test/Driver/sycl-embed-virtual-arch.cpp
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -1,6 +1,6 @@
/// Tests for --[no-]offload-embed-virtual-arch with SYCL.
-/// By default, the SPIR-V is kept.
+/// By default, no embed-virtual-arch entry is recorded.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=xe-mtl-u -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=EMBED %s
@@ -28,10 +28,11 @@
// DIRECT: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl"{{$}}
/// It still has no effect when one of the IGCA target's Intel GPUs is also
-/// given.
+/// given, so it is reported as unused.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=xe-mtl-u,igca_30r --no-offload-embed-virtual-arch \
-// RUN: -c %s 2>&1 | FileCheck -check-prefix=EMBED %s
+// RUN: -c %s 2>&1 | FileCheck -check-prefixes=UNUSED,BOTH %s
+// BOTH: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=igca_30r,kind=sycl,code=xe-mtl-u"{{$}}
/// With -Xarch_, it applies only to the device compile for that IGCA target.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
@@ -47,7 +48,7 @@
// RUN: | FileCheck -check-prefixes=GENERIC,UNUSED %s
// GENERIC: "--image=file={{[^"]*}},triple=spirv64-unknown-unknown,arch=generic,kind=sycl"{{$}}
-/// It is SYCL only, so it is reported as unused for OpenMP.
+/// It has no effect for OpenMP currently, so it is reported as unused.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
// RUN: --offload-arch=sm_70 -nogpulib --no-offload-embed-virtual-arch -c %s \
// RUN: 2>&1 | FileCheck -check-prefix=UNUSED %s
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 582736e35a71a8..5c386b9a51c272 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -192,11 +192,11 @@ __attribute__((visibility("protected"), used)) int x;
// OPENMP-CODE-COUNT-2: clang{{.*}} --target=nvptx64-nvidia-cuda -march=sm_70 {{[^ ]*}}.o{{$}}
// OPENMP-CODE-NOT: --target=nvptx64-nvidia-cuda
-// The SPIR-V is dropped only if all device images that list real devices ask
-// for it. A generic archive member does not count.
+// --no-embed-virtual-arch is passed only if all non-generic device images ask
+// for it. A generic archive member is ignored.
// SYCL-NO-EMBED-NOT: warning:
// SYCL-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --no-embed-virtual-arch{{$}}
-// SYCL-SOME-NO-EMBED: warning: not all device code for 'igca_30r' was compiled with --no-offload-embed-virtual-arch; the device code for the virtual architecture is kept
+// SYCL-SOME-NO-EMBED: warning: not all device images for 'igca_30r' with code 'xe-mtl-u' were compiled with --no-offload-embed-virtual-arch; keeping the virtual architecture code
// SYCL-SOME-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u{{$}}
// The replayed extraction of an image without "code" does not also match one
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index fea706ec2574b2..79eb9f368f7111 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -125,7 +125,7 @@ static std::string CudaBinaryPath;
/// Mutex lock to protect writes to shared TempFiles in parallel.
static std::mutex TempFilesMutex;
-/// Device links run in parallel, so warnings from them take turns.
+/// Serializes warnings emitted by device links that run in parallel.
static std::mutex WarningMutex;
/// Temporary files created by the linker wrapper.
@@ -675,6 +675,10 @@ SmallVector<StringRef> getCodeDevices(const OffloadBinary &Binary) {
return Devices;
}
+/// \return true if device code of offload kind \p Kind keeps the virtual
+/// architecture code by default when it is compiled ahead of time.
+bool embedsVirtualArchByDefault(OffloadKind Kind) { return Kind == OFK_SYCL; }
+
/// \return the name to extract \p Binary to.
std::string getExtractedFileName(const OffloadBinary &Binary) {
std::string Name =
@@ -1102,26 +1106,32 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
Args.MakeArgString(Device));
- // Drop the device code for the virtual architecture only if every device
- // image that lists real devices in "code" asked for it with
- // "embed-virtual-arch=0". Generic archive members list no devices, so they
- // don't get a say.
- auto HasCode = [](const OffloadFile &File) {
- return !File.getBinary()->getString("code").empty();
- };
- auto AsksToDrop = [](const OffloadFile &File) {
- return File.getBinary()->getString("embed-virtual-arch") == "0";
- };
- size_t NumWithCode = llvm::count_if(Input, HasCode);
- size_t NumAskingToDrop = llvm::count_if(Input, AsksToDrop);
- if (NumWithCode > 0 && NumAskingToDrop == NumWithCode)
+ // Pass --no-embed-virtual-arch only if there is at least one image other
+ // than generic ones (arch=generic, e.g. archive members), and all of them
+ // ask to drop the virtual architecture code. A missing "embed-virtual-arch"
+ // entry means the default for the image's offload kind.
+ size_t NumImages = 0;
+ size_t NumAskingToDrop = 0;
+ for (const OffloadFile &File : Input) {
+ const OffloadBinary &Image = *File.getBinary();
+ if (Image.getArch() == "generic")
+ continue;
+ ++NumImages;
+ StringRef Embed = Image.getString("embed-virtual-arch");
+ if (Embed.empty() ? !embedsVirtualArchByDefault(Image.getOffloadKind())
+ : Embed == "0")
+ ++NumAskingToDrop;
+ }
+ if (NumImages > 0 && NumAskingToDrop == NumImages) {
DAL.AddFlagArg(nullptr, Tbl.getOption(OPT_no_embed_virtual_arch));
- else if (NumAskingToDrop > 0) {
+ } else if (NumAskingToDrop > 0) {
std::scoped_lock<std::mutex> Lock(WarningMutex);
- WithColor::warning(errs(), LinkerExecutable)
- << "not all device code for '" << Binary.getArch()
- << "' was compiled with --no-offload-embed-virtual-arch; the device "
- "code for the virtual architecture is kept\n";
+ raw_ostream &Warning = WithColor::warning(errs(), LinkerExecutable);
+ Warning << "not all device images for '" << Binary.getArch() << "'";
+ if (StringRef Code = Binary.getString("code"); !Code.empty())
+ Warning << " with code '" << Code << "'";
+ Warning << " were compiled with --no-offload-embed-virtual-arch; keeping "
+ "the virtual architecture code\n";
}
// If every input file is bitcode we have whole program visibility as we
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index b9bbd0b09dd089..c9049b0a882e9c 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -85,8 +85,8 @@ def code_EQ : Joined<["--"], "code=">,
HelpText<"A real device to compile an image ahead of time for">;
def no_embed_virtual_arch : Flag<["--"], "no-embed-virtual-arch">,
Flags<[DeviceOnlyOption, HelpHidden]>,
- HelpText<"Drop the device code for the virtual architecture from device "
- "code compiled ahead of time">;
+ HelpText<"Drop the virtual architecture code from device code compiled "
+ "ahead of time">;
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/SYCLLinkOpts.td b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
index 072616f26683c1..4a0f67f451bffa 100644
--- a/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
+++ b/clang/tools/clang-sycl-linker/SYCLLinkOpts.td
@@ -55,8 +55,9 @@ def code_EQ : Joined<["--", "-"], "code=">,
"just-in-time compilation.">;
def no_embed_virtual_arch : Flag<["--", "-"], "no-embed-virtual-arch">,
Flags<[LinkerOnlyOption]>,
- HelpText<"Drop the SPIR-V from a device image "
- "compiled ahead of time for an Intel GPU">;
+ HelpText<"Drop the virtual architecture code "
+ "from a device image compiled ahead of "
+ "time for an Intel GPU">;
def triple_EQ : Joined<["--", "-"], "triple=">,
Flags<[LinkerOnlyOption]>,
MetaVarName<"<triple>">,
>From 261e4bee5136a3a0d71ce234c034ec5f9fde947a Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 2 Dec 2026 14:57:17 -0800
Subject: [PATCH 46/51] [Clang][CUDA] Make --[no-]offload-embed-virtual-arch
control PTX
For CUDA, --[no-]offload-embed-virtual-arch now works like
--[no-]cuda-include-ptx=all, and the last option that applies to a GPU wins.
PTX stays off by default, and --[no-]cuda-include-ptx= stays as the per-GPU
form.
---
clang/docs/ReleaseNotes.md | 5 ++++-
clang/include/clang/Options/Options.td | 2 +-
clang/lib/Driver/ToolChains/Cuda.cpp | 25 +++++++++++++++----------
clang/test/Driver/cuda-options.cu | 19 +++++++++++++++++++
4 files changed, 39 insertions(+), 12 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 42883482f9f1cd..40293df17cbd68 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -411,7 +411,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Added `--[no-]offload-embed-virtual-arch`, which controls whether the virtual
architecture image is embedded alongside native code compiled for a real
- architecture. It is on by default for SYCL.
+ architecture. It is on by default for SYCL. For CUDA, it is off by default
+ and works like `--[no-]cuda-include-ptx=all`, which stays as the per-GPU
+ form. As with `--cuda-include-ptx=`, PTX is only included without
+ `-fgpu-rdc`.
- Added `-fpch-base-dir-is-cwd` to serialize precompiled header input paths
relative to the current working directory. Paths resolve against the
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index c2c8a12d5e2c54..4e394ccdcf31ee 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1288,7 +1288,7 @@ defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
"offload-embed-virtual-arch",
PosFlag<SetTrue, [], [ClangOption], "Embed the virtual architecture image "
"alongside native code compiled for a real architecture (default on for "
- "SYCL)">,
+ "SYCL, off for CUDA). For CUDA, it works like --cuda-include-ptx=all">,
NegFlag<SetFalse, [], [ClangOption], "Do not embed the virtual architecture "
"image alongside native code compiled for a real architecture">,
BothFlags<[]>, ["--"]>;
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 57470c18db3930..8c1d3c15f5d7f2 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -532,20 +532,25 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
Exec, CmdArgs, Inputs, Output));
}
+/// \return true if PTX for \p InputArch should be included in the fat binary.
+/// --[no-]offload-embed-virtual-arch works like --[no-]cuda-include-ptx=all,
+/// and the last option that applies to \p InputArch wins. PTX is not included
+/// by default.
static bool shouldIncludePTX(const ArgList &Args, StringRef InputArch) {
- bool includePTX = false;
+ bool IncludePTX = false;
for (Arg *A : Args.filtered(options::OPT_cuda_include_ptx_EQ,
- options::OPT_no_cuda_include_ptx_EQ)) {
+ options::OPT_no_cuda_include_ptx_EQ,
+ options::OPT_offload_embed_virtual_arch,
+ options::OPT_no_offload_embed_virtual_arch)) {
A->claim();
- const StringRef ArchStr = A->getValue();
- if (A->getOption().matches(options::OPT_cuda_include_ptx_EQ) &&
- (ArchStr == "all" || ArchStr == InputArch))
- includePTX = true;
- else if (A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ) &&
- (ArchStr == "all" || ArchStr == InputArch))
- includePTX = false;
+ // --[no-]offload-embed-virtual-arch has no value and applies to all GPUs.
+ StringRef Arch = A->getNumValues() ? A->getValue() : "all";
+ if (Arch == "all" || Arch == InputArch)
+ IncludePTX =
+ A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
+ A->getOption().matches(options::OPT_offload_embed_virtual_arch);
}
- return includePTX;
+ return IncludePTX;
}
// All inputs to this linker must be from CudaDeviceActions, as we need to look
diff --git a/clang/test/Driver/cuda-options.cu b/clang/test/Driver/cuda-options.cu
index 704d87b34eb53b..58fd13effd2b24 100644
--- a/clang/test/Driver/cuda-options.cu
+++ b/clang/test/Driver/cuda-options.cu
@@ -186,6 +186,25 @@
// RUN: -c %s 2>&1 \
// RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM60,PTX-SM52 %s
+// f) --[no-]offload-embed-virtual-arch works like --[no-]cuda-include-ptx=all,
+// and the last option that applies to a GPU wins.
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpulib -nogpuinc \
+// RUN: --cuda-gpu-arch=sm_60 --cuda-gpu-arch=sm_52 \
+// RUN: --offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM60,PTX-SM52 %s
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpulib -nogpuinc \
+// RUN: --cuda-gpu-arch=sm_60 --cuda-gpu-arch=sm_52 \
+// RUN: --offload-embed-virtual-arch --no-cuda-include-ptx=sm_52 -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM60,NOPTX-SM52 %s
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpulib -nogpuinc \
+// RUN: --cuda-gpu-arch=sm_60 --cuda-gpu-arch=sm_52 \
+// RUN: --no-offload-embed-virtual-arch --cuda-include-ptx=sm_60 -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM60,NOPTX-SM52 %s
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpulib -nogpuinc \
+// RUN: --cuda-gpu-arch=sm_60 --cuda-gpu-arch=sm_52 \
+// RUN: --cuda-include-ptx=sm_60 --no-offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes FATBIN-COMMON,NOPTX-SM60,NOPTX-SM52 %s
+
// Verify -flto=thin -fwhole-program-vtables handling. This should result in
// both options being passed to the host compilation, with neither passed to
// the device compilation.
>From c246eabbdd45922e41dd3d1118af2433d3ce38ef Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Wed, 2 Dec 2026 15:30:33 -0800
Subject: [PATCH 47/51] [Clang][CUDA] Honor -Xarch_ for
--[no-]offload-embed-virtual-arch
Read the PTX options from each GPU's own arguments, so that
-Xarch_<arch> --[no-]offload-embed-virtual-arch applies to that GPU only.
---
clang/docs/ReleaseNotes.md | 6 +++---
clang/lib/Driver/ToolChains/Cuda.cpp | 6 +++++-
clang/test/Driver/cuda-options.cu | 11 +++++++++++
3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 40293df17cbd68..d8323213ad99fe 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -412,9 +412,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Added `--[no-]offload-embed-virtual-arch`, which controls whether the virtual
architecture image is embedded alongside native code compiled for a real
architecture. It is on by default for SYCL. For CUDA, it is off by default
- and works like `--[no-]cuda-include-ptx=all`, which stays as the per-GPU
- form. As with `--cuda-include-ptx=`, PTX is only included without
- `-fgpu-rdc`.
+ and works like `--[no-]cuda-include-ptx=all`; with `-Xarch_<arch>`, it
+ applies to one GPU, like `--[no-]cuda-include-ptx=<arch>`. As with
+ `--cuda-include-ptx=`, PTX is only included without `-fgpu-rdc`.
- Added `-fpch-base-dir-is-cwd` to serialize precompiled header input paths
relative to the current working directory. Paths resolve against the
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 8c1d3c15f5d7f2..1b518993be52f5 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -584,8 +584,12 @@ void NVPTX::FatBinary::ConstructJob(Compilation &C, const JobAction &JA,
assert(!GpuArch.empty() &&
"Device action expected to have associated a GPU architecture!");
+ // Read the PTX options from this GPU's own arguments, so that
+ // -Xarch_<arch> --[no-]offload-embed-virtual-arch applies to it only.
if (II.getType() == types::TY_PP_Asm &&
- !shouldIncludePTX(Args, GpuArch.ArchName))
+ !shouldIncludePTX(
+ C.getArgsForToolChain(&TC, GpuArch, A->getOffloadingDeviceKind()),
+ GpuArch.ArchName))
continue;
StringRef Kind = (II.getType() == types::TY_PP_Asm) ? "ptx" : "elf";
CmdArgs.push_back(Args.MakeArgString(
diff --git a/clang/test/Driver/cuda-options.cu b/clang/test/Driver/cuda-options.cu
index 58fd13effd2b24..ffa4e51b261de1 100644
--- a/clang/test/Driver/cuda-options.cu
+++ b/clang/test/Driver/cuda-options.cu
@@ -205,6 +205,17 @@
// RUN: --cuda-include-ptx=sm_60 --no-offload-embed-virtual-arch -c %s 2>&1 \
// RUN: | FileCheck -check-prefixes FATBIN-COMMON,NOPTX-SM60,NOPTX-SM52 %s
+// g) -Xarch_<arch> --[no-]offload-embed-virtual-arch applies to that GPU only.
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpulib -nogpuinc \
+// RUN: --cuda-gpu-arch=sm_60 --cuda-gpu-arch=sm_52 \
+// RUN: -Xarch_sm_60 --offload-embed-virtual-arch -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM60,NOPTX-SM52 %s
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpulib -nogpuinc \
+// RUN: --cuda-gpu-arch=sm_60 --cuda-gpu-arch=sm_52 \
+// RUN: --offload-embed-virtual-arch -Xarch_sm_52 --no-offload-embed-virtual-arch \
+// RUN: -c %s 2>&1 \
+// RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM60,NOPTX-SM52 %s
+
// Verify -flto=thin -fwhole-program-vtables handling. This should result in
// both options being passed to the host compilation, with neither passed to
// the device compilation.
>From 3269400eebb1ef8c0abdb16e2cc7bc1285b3dd82 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 3 Dec 2026 14:12:14 -0800
Subject: [PATCH 48/51] [Clang][SYCL] Address review: say "virtual architecture
code" in the docs
---
clang/docs/ClangSYCLLinker.md | 6 +++---
clang/docs/ReleaseNotes.md | 9 +++++----
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/clang/docs/ClangSYCLLinker.md b/clang/docs/ClangSYCLLinker.md
index fcff76ae8dca27..5fa37eff18cf2a 100644
--- a/clang/docs/ClangSYCLLinker.md
+++ b/clang/docs/ClangSYCLLinker.md
@@ -131,9 +131,9 @@ time by the runtime:
clang-sycl-linker --triple=spirv64 --arch=igca_30r --code=xe-mtl-u --code=xe-mtl-h input.bc -o output.bin
```
-ocloc also embeds the SPIR-V in its output, so that the device image can be
-compiled just in time for compatible GPUs not listed in `--code`;
-`--no-embed-virtual-arch` drops it (`ocloc -exclude_ir`).
+ocloc also embeds the virtual architecture code (SPIR-V) in its output, so that
+the device image can be compiled just in time for compatible GPUs not listed in
+`--code`; `--no-embed-virtual-arch` drops it (`ocloc -exclude_ir`).
### Linking with Device Libraries
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index d8323213ad99fe..9bf2404d20310e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1881,11 +1881,12 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
error rather than a device job for a device that does not exist.
- Added `--[no-]offload-embed-virtual-arch`. With
`--no-offload-embed-virtual-arch`, a SYCL device image compiled ahead of time
- for Intel GPUs does not keep the SPIR-V for just-in-time compilation. To
- apply it to one IGCA target only, use `-Xarch_<IGCA target>`, e.g.
+ for Intel GPUs does not keep the virtual architecture code (SPIR-V) for
+ just-in-time compilation. To apply it to one IGCA target only, use
+ `-Xarch_<IGCA target>`, e.g.
`-Xarch_igca_40r --no-offload-embed-virtual-arch`; it then applies to all
- Intel GPUs of that IGCA target. Without the SPIR-V, a SYCL program can't set
- specialization constants to non-default values.
+ Intel GPUs of that IGCA target. Without the virtual architecture code, a SYCL
+ program can't set specialization constants to non-default values.
#### Improvements
>From c752b7fe138f7ec2dcc0bbf2f179e307c5cd9290 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Thu, 3 Dec 2026 17:14:17 -0800
Subject: [PATCH 49/51] [Clang][SYCL] Address review: say "virtual architecture
code" in the help
---
clang/docs/ReleaseNotes.md | 2 +-
clang/include/clang/Options/Options.td | 4 ++--
clang/lib/Driver/ToolChains/Clang.cpp | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 9bf2404d20310e..28d27a885d1831 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -410,7 +410,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### New Compiler Flags
- Added `--[no-]offload-embed-virtual-arch`, which controls whether the virtual
- architecture image is embedded alongside native code compiled for a real
+ architecture code is embedded alongside native code compiled for a real
architecture. It is on by default for SYCL. For CUDA, it is off by default
and works like `--[no-]cuda-include-ptx=all`; with `-Xarch_<arch>`, it
applies to one GPU, like `--[no-]cuda-include-ptx=<arch>`. As with
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 4e394ccdcf31ee..0bd09aec08001d 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1286,11 +1286,11 @@ def no_offload_arch_EQ : CommaJoined<["--"], "no-offload-arch=">,
defm offload_embed_virtual_arch : BoolOptionWithoutMarshalling<"",
"offload-embed-virtual-arch",
- PosFlag<SetTrue, [], [ClangOption], "Embed the virtual architecture image "
+ PosFlag<SetTrue, [], [ClangOption], "Embed the virtual architecture code "
"alongside native code compiled for a real architecture (default on for "
"SYCL, off for CUDA). For CUDA, it works like --cuda-include-ptx=all">,
NegFlag<SetFalse, [], [ClangOption], "Do not embed the virtual architecture "
- "image alongside native code compiled for a real architecture">,
+ "code alongside native code compiled for a real architecture">,
BothFlags<[]>, ["--"]>;
def offload_new_driver : Flag<["--"], "offload-new-driver">,
Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index e1853e7a5cafe5..d0398acda7ab36 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9800,8 +9800,8 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
// Ask to drop the virtual architecture code only for device code that is
// compiled ahead of time, and keep it if the user also gave the IGCA
- // target itself in --offload-arch. The option is read last, so clang
- // reports it as unused where it can't do anything.
+ // target itself in --offload-arch. The option is read only after the
+ // other checks, so it is reported as unused where it has no effect.
bool DropVirtualArch =
!Devices.empty() && !C.isRequestedIGCATarget(TC, Arch.ArchName) &&
!TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
>From fe36f54360411ff7039aa8d3d80bc07f6fe51292 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 4 Dec 2026 12:08:33 -0800
Subject: [PATCH 50/51] [Clang][SYCL] Address review: clearer docs, names and
tests
---
clang/docs/ClangLinkerWrapper.md | 15 +++++-----
clang/docs/ReleaseNotes.md | 20 +++++++------
clang/include/clang/Driver/Compilation.h | 2 +-
clang/test/Driver/sycl-embed-virtual-arch.cpp | 4 ++-
.../clang-linker-wrapper/linker-wrapper.c | 6 +++-
.../ClangLinkerWrapper.cpp | 28 +++++++++++--------
.../clang-sycl-linker/ClangSYCLLinker.cpp | 3 +-
7 files changed, 47 insertions(+), 31 deletions(-)
diff --git a/clang/docs/ClangLinkerWrapper.md b/clang/docs/ClangLinkerWrapper.md
index 244d932bd4acf8..66d7f93c61eba0 100644
--- a/clang/docs/ClangLinkerWrapper.md
+++ b/clang/docs/ClangLinkerWrapper.md
@@ -97,13 +97,14 @@ GPUs, e.g. `xe-mtl-u`.
The `embed-virtual-arch` string entry in an image's offload binary metadata
records whether to embed the virtual architecture code (e.g. SPIR-V) alongside
the code compiled ahead of time: `1` to embed it, `0` to drop it. A missing
-entry means the default for the image's offload kind.
-
-Within a link group, generic images (`arch=generic`, e.g. archive members) are
-ignored. Currently only SYCL acts on this entry: if all other images ask to drop
-the virtual architecture code, the linker wrapper passes
-`--no-embed-virtual-arch` to `clang-sycl-linker`. If only some of them do, it
-warns and does not pass the flag.
+entry means the default for the image's offload kind. The driver writes the
+entry only when it differs from that default.
+
+Within a link group, generic images (`arch=generic`, e.g. from objects compiled
+without `--offload-arch`) are ignored. Currently only SYCL acts on this entry:
+if all other images ask to drop the virtual architecture code, the linker
+wrapper passes `--no-embed-virtual-arch` to `clang-sycl-linker`. If only some of
+them do, it warns and does not pass the flag.
## Debugging
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 28d27a885d1831..46297410e0d3ab 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -409,12 +409,13 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### New Compiler Flags
-- Added `--[no-]offload-embed-virtual-arch`, which controls whether the virtual
- architecture code is embedded alongside native code compiled for a real
- architecture. It is on by default for SYCL. For CUDA, it is off by default
- and works like `--[no-]cuda-include-ptx=all`; with `-Xarch_<arch>`, it
- applies to one GPU, like `--[no-]cuda-include-ptx=<arch>`. As with
- `--cuda-include-ptx=`, PTX is only included without `-fgpu-rdc`.
+- Added `--[no-]offload-embed-virtual-arch` for device offloading, which
+ controls whether the virtual architecture code is embedded alongside native
+ code compiled for a real architecture. It is on by default for SYCL. For
+ CUDA, it is off by default and works like `--[no-]cuda-include-ptx=all`, and
+ `-Xarch_<arch>` can be used to set it for one GPU, like
+ `--[no-]cuda-include-ptx=<arch>`. As with `--cuda-include-ptx=`, PTX is only
+ included without `-fgpu-rdc`.
- Added `-fpch-base-dir-is-cwd` to serialize precompiled header input paths
relative to the current working directory. Paths resolve against the
@@ -1580,6 +1581,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
#### CUDA Support
+- `--[no-]offload-embed-virtual-arch` now also controls PTX, like
+ `--[no-]cuda-include-ptx=all`.
+
- Fixed argument-dependent lookup for CUDA and HIP kernel launches, including
calls to kernels declared as hidden friends. (#GH45327)
@@ -1885,8 +1889,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
just-in-time compilation. To apply it to one IGCA target only, use
`-Xarch_<IGCA target>`, e.g.
`-Xarch_igca_40r --no-offload-embed-virtual-arch`; it then applies to all
- Intel GPUs of that IGCA target. Without the virtual architecture code, a SYCL
- program can't set specialization constants to non-default values.
+ Intel GPUs of that IGCA target. Don't use `--no-offload-embed-virtual-arch`
+ if the program sets specialization constants to non-default values.
#### Improvements
diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index c7a4504d7d799b..72e80cf01de71b 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -278,7 +278,7 @@ class Compilation {
ArrayRef<StringRef> getIGCADevices(const ToolChain *TC,
StringRef Target) const;
- /// Remember that \p Target was given directly in --offload-arch for the
+ /// Store that \p Target was given directly in --offload-arch for the
/// device toolchain \p TC.
void addRequestedIGCATarget(const ToolChain *TC, StringRef Target) {
RequestedIGCATargets.insert({TC, Target});
diff --git a/clang/test/Driver/sycl-embed-virtual-arch.cpp b/clang/test/Driver/sycl-embed-virtual-arch.cpp
index 31f320b111d10b..83507c1c6ac97f 100644
--- a/clang/test/Driver/sycl-embed-virtual-arch.cpp
+++ b/clang/test/Driver/sycl-embed-virtual-arch.cpp
@@ -1,6 +1,8 @@
/// Tests for --[no-]offload-embed-virtual-arch with SYCL.
-/// By default, no embed-virtual-arch entry is recorded.
+/// By default, or with --offload-embed-virtual-arch, no embed-virtual-arch
+/// entry is recorded, since keeping the virtual architecture code is the SYCL
+/// default.
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: --offload-arch=xe-mtl-u -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=EMBED %s
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
index 5c386b9a51c272..bc35fc298af453 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper.c
@@ -135,6 +135,8 @@ __attribute__((visibility("protected"), used)) int x;
// RUN: llvm-offload-binary -o %t.sycl-noembed.out \
// RUN: --image=file=%t.spirv.bc,kind=sycl,triple=spirv64-unknown-unknown,arch=igca_30r,code=xe-mtl-u,embed-virtual-arch=0
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.sycl-noembed.o -fembed-offload-object=%t.sycl-noembed.out
+// Two images that both ask to drop the virtual architecture code, plus a
+// generic archive member that is ignored.
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --linker-path=/usr/bin/ld %t.sycl-noembed.o %t.sycl-noembed.o \
// RUN: %t.sycl-generic.a -o a.out 2>&1 \
@@ -197,7 +199,9 @@ __attribute__((visibility("protected"), used)) int x;
// SYCL-NO-EMBED-NOT: warning:
// SYCL-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u -Xlinker --no-embed-virtual-arch{{$}}
// SYCL-SOME-NO-EMBED: warning: not all device images for 'igca_30r' with code 'xe-mtl-u' were compiled with --no-offload-embed-virtual-arch; keeping the virtual architecture code
-// SYCL-SOME-NO-EMBED: clang{{.*}} -march=igca_30r {{.*}} --sycl-link -Xlinker --code=xe-mtl-u{{$}}
+// SYCL-SOME-NO-EMBED: clang{{.*}} -march=igca_30r
+// SYCL-SOME-NO-EMBED-NOT: --no-embed-virtual-arch
+// SYCL-SOME-NO-EMBED-SAME: --sycl-link -Xlinker --code=xe-mtl-u{{$}}
// The replayed extraction of an image without "code" does not also match one
// with "code" for the same "arch".
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 79eb9f368f7111..4bbc48c457a319 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1106,25 +1106,29 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_code_EQ),
Args.MakeArgString(Device));
- // Pass --no-embed-virtual-arch only if there is at least one image other
- // than generic ones (arch=generic, e.g. archive members), and all of them
- // ask to drop the virtual architecture code. A missing "embed-virtual-arch"
- // entry means the default for the image's offload kind.
- size_t NumImages = 0;
- size_t NumAskingToDrop = 0;
+ // Pass --no-embed-virtual-arch only if there is at least one non-generic
+ // image (generic ones, arch=generic, come from objects compiled without
+ // --offload-arch), and all of them ask to drop the virtual architecture
+ // code. A missing "embed-virtual-arch" entry means the default for the
+ // image's offload kind.
+ size_t NumNonGenericImages = 0;
+ size_t NumNonGenericAskingToDrop = 0;
for (const OffloadFile &File : Input) {
const OffloadBinary &Image = *File.getBinary();
if (Image.getArch() == "generic")
continue;
- ++NumImages;
+ ++NumNonGenericImages;
StringRef Embed = Image.getString("embed-virtual-arch");
- if (Embed.empty() ? !embedsVirtualArchByDefault(Image.getOffloadKind())
- : Embed == "0")
- ++NumAskingToDrop;
+ bool KeepsVirtualArch =
+ Embed.empty() ? embedsVirtualArchByDefault(Image.getOffloadKind())
+ : Embed != "0";
+ if (!KeepsVirtualArch)
+ ++NumNonGenericAskingToDrop;
}
- if (NumImages > 0 && NumAskingToDrop == NumImages) {
+ if (NumNonGenericImages > 0 &&
+ NumNonGenericAskingToDrop == NumNonGenericImages) {
DAL.AddFlagArg(nullptr, Tbl.getOption(OPT_no_embed_virtual_arch));
- } else if (NumAskingToDrop > 0) {
+ } else if (NumNonGenericAskingToDrop > 0) {
std::scoped_lock<std::mutex> Lock(WarningMutex);
raw_ostream &Warning = WithColor::warning(errs(), LinkerExecutable);
Warning << "not all device images for '" << Binary.getArch() << "'";
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 4c2d96b5a866d2..6320f08bdddf7d 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -722,7 +722,8 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, StringRef OutputFile,
assert(!Devices.empty() && "a device must be specified for AOT compilation");
CmdArgs.push_back("-device");
CmdArgs.push_back(Devices);
- // Unless it is given -exclude_ir, ocloc keeps the SPIR-V in its output.
+ // Pass -exclude_ir to ocloc to drop the SPIR-V if --no-embed-virtual-arch is
+ // given.
if (Args.hasArg(OPT_no_embed_virtual_arch))
CmdArgs.push_back("-exclude_ir");
>From 4c05b727af0be145d96d0b191db1427f309d3d55 Mon Sep 17 00:00:00 2001
From: srividya sundaram <srividya.sundaram at intel.com>
Date: Fri, 4 Dec 2026 13:40:38 -0800
Subject: [PATCH 51/51] [Clang][Object] Address review: one place for the
embed-virtual-arch default
Move embedsVirtualArchByDefault() next to OffloadKind in OffloadBinary.h,
test it, and use it in the driver, for each device input's offload kind, and
in clang-linker-wrapper.
---
clang/docs/ReleaseNotes.md | 2 +-
clang/lib/Driver/ToolChains/Clang.cpp | 5 ++++-
clang/lib/Driver/ToolChains/Cuda.cpp | 21 +++++++++++--------
.../ClangLinkerWrapper.cpp | 4 ----
llvm/include/llvm/Object/OffloadBinary.h | 7 +++++++
llvm/unittests/Object/OffloadingTest.cpp | 8 +++++++
6 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 46297410e0d3ab..e3811db2790440 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1581,7 +1581,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
#### CUDA Support
-- `--[no-]offload-embed-virtual-arch` now also controls PTX, like
+- `--[no-]offload-embed-virtual-arch` now also controls PTX, behaving like
`--[no-]cuda-include-ptx=all`.
- Fixed argument-dependent lookup for CUDA and HIP kernel launches, including
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index d0398acda7ab36..7df826baf87af6 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -44,6 +44,7 @@
#include "llvm/Config/llvm-config.h"
#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBinary.h"
#include "llvm/Option/ArgList.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/Support/CodeGen.h"
@@ -9805,7 +9806,9 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
bool DropVirtualArch =
!Devices.empty() && !C.isRequestedIGCATarget(TC, Arch.ArchName) &&
!TCArgs.hasFlag(options::OPT_offload_embed_virtual_arch,
- options::OPT_no_offload_embed_virtual_arch, true);
+ options::OPT_no_offload_embed_virtual_arch,
+ llvm::object::embedsVirtualArchByDefault(
+ llvm::object::getOffloadKind(Kind)));
if (DropVirtualArch)
Parts.emplace_back("embed-virtual-arch=0");
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 1b518993be52f5..e7c71004e3dbb3 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -18,6 +18,7 @@
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Config/llvm-config.h" // for LLVM_HOST_TRIPLE
+#include "llvm/Object/OffloadBinary.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
@@ -532,12 +533,14 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
Exec, CmdArgs, Inputs, Output));
}
-/// \return true if PTX for \p InputArch should be included in the fat binary.
-/// --[no-]offload-embed-virtual-arch works like --[no-]cuda-include-ptx=all,
-/// and the last option that applies to \p InputArch wins. PTX is not included
-/// by default.
-static bool shouldIncludePTX(const ArgList &Args, StringRef InputArch) {
- bool IncludePTX = false;
+/// \return true if PTX for \p InputArch, compiled for offload kind \p Kind,
+/// should be included in the fat binary. --[no-]offload-embed-virtual-arch
+/// works like --[no-]cuda-include-ptx=all, and the last option that applies to
+/// \p InputArch wins.
+static bool shouldIncludePTX(const ArgList &Args, StringRef InputArch,
+ Action::OffloadKind Kind) {
+ bool IncludePTX = llvm::object::embedsVirtualArchByDefault(
+ llvm::object::getOffloadKind(Action::GetOffloadKindName(Kind)));
for (Arg *A : Args.filtered(options::OPT_cuda_include_ptx_EQ,
options::OPT_no_cuda_include_ptx_EQ,
options::OPT_offload_embed_virtual_arch,
@@ -586,10 +589,10 @@ void NVPTX::FatBinary::ConstructJob(Compilation &C, const JobAction &JA,
// Read the PTX options from this GPU's own arguments, so that
// -Xarch_<arch> --[no-]offload-embed-virtual-arch applies to it only.
+ Action::OffloadKind DeviceKind = A->getOffloadingDeviceKind();
if (II.getType() == types::TY_PP_Asm &&
- !shouldIncludePTX(
- C.getArgsForToolChain(&TC, GpuArch, A->getOffloadingDeviceKind()),
- GpuArch.ArchName))
+ !shouldIncludePTX(C.getArgsForToolChain(&TC, GpuArch, DeviceKind),
+ GpuArch.ArchName, DeviceKind))
continue;
StringRef Kind = (II.getType() == types::TY_PP_Asm) ? "ptx" : "elf";
CmdArgs.push_back(Args.MakeArgString(
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 4bbc48c457a319..63de6a3c0ecce9 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -675,10 +675,6 @@ SmallVector<StringRef> getCodeDevices(const OffloadBinary &Binary) {
return Devices;
}
-/// \return true if device code of offload kind \p Kind keeps the virtual
-/// architecture code by default when it is compiled ahead of time.
-bool embedsVirtualArchByDefault(OffloadKind Kind) { return Kind == OFK_SYCL; }
-
/// \return the name to extract \p Binary to.
std::string getExtractedFileName(const OffloadBinary &Binary) {
std::string Name =
diff --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 058432ae1a3c7e..dd8ffac5ae5117 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -283,6 +283,13 @@ LLVM_ABI OffloadKind getOffloadKind(StringRef Name);
/// Convert an offload kind to its string representation.
LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
+/// \return true if device code of offload kind \p Kind keeps the virtual
+/// architecture code by default. A missing "embed-virtual-arch" entry means
+/// this default.
+inline bool embedsVirtualArchByDefault(OffloadKind Kind) {
+ return Kind == OFK_SYCL;
+}
+
/// Returns true if an image built for target \p Provided can provide the device
/// code for a request for target \p Requested. This is directional: a
/// feature-unspecified or generic image serves a more specific request (e.g. a
diff --git a/llvm/unittests/Object/OffloadingTest.cpp b/llvm/unittests/Object/OffloadingTest.cpp
index 54c4da5257cb04..526e6237329925 100644
--- a/llvm/unittests/Object/OffloadingTest.cpp
+++ b/llvm/unittests/Object/OffloadingTest.cpp
@@ -387,3 +387,11 @@ TEST(OffloadingTest, checkTargetsWithCode) {
// device images.
EXPECT_TRUE(areTargetsCompatible(NoCode, Generic));
}
+
+TEST(OffloadingTest, checkEmbedsVirtualArchByDefault) {
+ // Only SYCL keeps the virtual architecture code by default.
+ EXPECT_TRUE(embedsVirtualArchByDefault(OFK_SYCL));
+ EXPECT_FALSE(embedsVirtualArchByDefault(OFK_Cuda));
+ EXPECT_FALSE(embedsVirtualArchByDefault(OFK_HIP));
+ EXPECT_FALSE(embedsVirtualArchByDefault(OFK_OpenMP));
+}
More information about the cfe-commits
mailing list