https://github.com/srividya-sundaram created 
https://github.com/llvm/llvm-project/pull/232148

Request canonical architectures in --should-extract.

The linker wrapper extracts device code from a static library only when a 
--should-extract value matches the image's arch label exactly. The label is the 
canonical architecture, but --should-extract was built from the raw 
--offload-arch values. So `--offload-arch=gfx908:xnack+:sramecc-` labeled the 
image `gfx908:sramecc-:xnack+` but requested `gfx908:xnack+:sramecc-`, and an 
architecture removed with --no-offload-arch was still requested. The values now 
come from getOffloadArchs, which canonicalizes and applies --no-offload-arch.


>From 8bf280443889135fd10afb41cb247a3856d25c5e Mon Sep 17 00:00:00 2001
From: srividya sundaram <[email protected]>
Date: Wed, 14 Oct 2026 17:58:02 -0700
Subject: [PATCH] [Clang][Offload] Request canonical architectures in
 --should-extract

The linker wrapper extracts device code from a static library only when a
--should-extract value matches the image's arch label exactly. The label is
the canonical architecture, but --should-extract was built from the raw
--offload-arch values, so --offload-arch=gfx908:xnack+:sramecc- labeled the
image gfx908:sramecc-:xnack+ yet requested gfx908:xnack+:sramecc-, and an
architecture removed with --no-offload-arch was still requested. Take the
values from getOffloadArchs, which canonicalizes and applies
--no-offload-arch.

Assisted-by: Claude Opus 5.5
---
 clang/lib/Driver/ToolChains/Clang.cpp      | 11 ++++++++---
 clang/test/Driver/offload-should-extract.c | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Driver/offload-should-extract.c

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index eb7bd3ad1ad5e..a67f6a761d0e6 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9909,9 +9909,14 @@ 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.
-      for (StringRef Arg : ToolChainArgs.getAllArgValues(OPT_offload_arch_EQ))
-        CmdArgs.emplace_back(Args.MakeArgString("--should-extract=" + Arg));
+      // extract it from any static libraries if present. Use the canonical
+      // name, which device images are labeled with.
+      if (ToolChainArgs.hasArg(OPT_offload_arch_EQ))
+        for (const BoundArch &BA :
+             C.getDriver().getOffloadArchs(C, ToolChainArgs, Kind, *TC))
+          if (!BA.ArchName.empty())
+            CmdArgs.emplace_back(
+                Args.MakeArgString("--should-extract=" + BA.ArchName));
 
       // If this is OpenMP the device linker will need `-lompdevice`.
       if (Kind == Action::OFK_OpenMP && !Args.hasArg(OPT_no_offloadlib) &&
diff --git a/clang/test/Driver/offload-should-extract.c 
b/clang/test/Driver/offload-should-extract.c
new file mode 100644
index 0000000000000..f610466e9a562
--- /dev/null
+++ b/clang/test/Driver/offload-should-extract.c
@@ -0,0 +1,17 @@
+// The linker wrapper extracts device code from static libraries by the
+// architecture its images are labeled with, so request the canonical name.
+
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -x hip -nogpulib 
-nogpuinc \
+// RUN:   --offload-arch=gfx908:xnack+:sramecc- %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CANONICAL
+// CANONICAL: 
"--image=file={{.*}},triple={{[^,]*}},arch=gfx908:sramecc-:xnack+,kind=hip"
+// CANONICAL: clang-linker-wrapper{{.*}} 
"--should-extract=gfx908:sramecc-:xnack+"
+
+/// An architecture removed with --no-offload-arch is not requested.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -x hip -nogpulib 
-nogpuinc \
+// RUN:   --offload-arch=gfx906 --offload-arch=gfx90a --no-offload-arch=gfx906 
%s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=REMOVED
+// REMOVED: clang-linker-wrapper
+// REMOVED-NOT: "--should-extract=gfx906"
+// REMOVED-SAME: "--should-extract=gfx90a"
+// REMOVED-NOT: "--should-extract=gfx906"

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

Reply via email to