https://github.com/pvelesko created 
https://github.com/llvm/llvm-project/pull/206902

`Darwin::ensureTargetInitialized()` recorded the raw triple OS version from 
`getOSVersion()`, which on macOS is the Darwin *kernel* version (e.g. 
`24.3.0`), not the macOS *product* version (`15.x`). The offload host job later 
calls `setTarget()` again from `AddDeploymentTarget()` with the product 
version; `setTarget()`'s reinit guard only short-circuits when the versions 
match, so the mismatch trips `assert(!TargetInitialized && "Target already 
initialized!")` and clang aborts on every `-x hip --offload=spirv64* 
--target=arm64-apple-darwin` compile.

Convert macOS kernel versions via `getMacOSXVersion()` so the lazy init records 
the same version `AddDeploymentTarget()` uses later.

>From 3973d6b66a239d6d5bee5f751dcb3fe1d3a0e3db Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Tue, 30 Jun 2026 12:59:25 +0300
Subject: [PATCH 1/2] [Darwin] Add regression test for HIP-SPIR-V offload
 version-init crash

HIP-SPIR-V offloading to a Darwin host whose triple carries a Darwin
kernel version (e.g. `--target=arm64-apple-darwin24.3.0`) crashes the
driver: ensureTargetInitialized() records the kernel version (24.x) via
setTarget(), then AddDeploymentTarget() calls setTarget() again with the
macOS product version (15.x); the version mismatch trips the
"Target already initialized!" assertion.

This test fails (clang aborts) on the current behavior and is fixed by
the following commit.
---
 .../test/Driver/darwin-hip-spirv-version.hip  | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 clang/test/Driver/darwin-hip-spirv-version.hip

diff --git a/clang/test/Driver/darwin-hip-spirv-version.hip 
b/clang/test/Driver/darwin-hip-spirv-version.hip
new file mode 100644
index 0000000000000..7e1ff20e7d662
--- /dev/null
+++ b/clang/test/Driver/darwin-hip-spirv-version.hip
@@ -0,0 +1,21 @@
+// Regression test: HIP-SPIR-V offloading to a Darwin host whose triple carries
+// a Darwin *kernel* version (e.g. darwin24.3.0) must not crash the driver.
+//
+// The HIP-SPIR-V offload path calls Darwin::ensureTargetInitialized() early.
+// It records the deployment version via setTarget(); the later host job calls
+// setTarget() again from AddDeploymentTarget(). setTarget()'s reinit guard 
only
+// short-circuits when the two versions match. ensureTargetInitialized() must
+// therefore record the converted macOS *product* version (15.x) rather than 
the
+// raw Darwin *kernel* version (24.x); otherwise the second call mismatches and
+// trips the "Target already initialized!" assertion.
+
+// REQUIRES: spirv-registered-target
+// UNSUPPORTED: system-windows, system-cygwin
+
+// RUN: %clang -### -x hip --offload=spirv64 --target=arm64-apple-darwin24.3.0 
\
+// RUN:   -nogpulib -nogpuinc %s 2>&1 | FileCheck %s
+
+// The Darwin host -cc1 job uses the converted macOS product version, and the
+// driver does not assert/crash.
+// CHECK: "-triple" "arm64-apple-macosx{{[0-9.]+}}"
+// CHECK-NOT: "-triple" "arm64-apple-darwin

>From 92194ebb223868acbcd1acb7e7df8f3d661f95cd Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Tue, 30 Jun 2026 12:59:37 +0300
Subject: [PATCH 2/2] [Darwin] Use macOS product version in
 ensureTargetInitialized

Darwin::ensureTargetInitialized() (the lazy target-init helper used by
the HIP-SPIR-V offload toolchain) passed setTarget() the raw triple OS
version from getOSVersion(). On macOS that is the Darwin *kernel* version
(e.g. 24.3.0), not the macOS *product* version (e.g. 15.0.0).

When clang later builds the offload host job, AddDeploymentTarget() calls
setTarget() again with the product version. setTarget()'s reinit guard
only short-circuits when the versions match; 24.3.0 != 15.0.0, so it
falls through to assert(!TargetInitialized) and clang aborts on every
'-x hip --offload=spirv64* --target=arm64-apple-darwin' compile.

Convert macOS kernel versions via getMacOSXVersion() so the lazy init
records the same version AddDeploymentTarget() later uses.
---
 clang/lib/Driver/ToolChains/Darwin.cpp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index 64bc96a900f30..de62a7c8ee9c2 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1210,7 +1210,18 @@ void Darwin::ensureTargetInitialized() const {
   else if (getTriple().isMacCatalystEnvironment())
     Environment = MacCatalyst;
 
-  VersionTuple OsVer = getTriple().getOSVersion();
+  VersionTuple OsVer;
+  if (Platform == MacOS) {
+    // Convert a Darwin kernel version (e.g. darwin24.3.0) to the macOS product
+    // version (e.g. 15.0.0). The raw getOSVersion() returns the kernel 
version,
+    // which mismatches the macOS version that AddDeploymentTarget() later
+    // passes to setTarget(), tripping its "Target already initialized!"
+    // assertion on the HIP-SPIR-V offload path.
+    if (!getTriple().getMacOSXVersion(OsVer))
+      return;
+  } else {
+    OsVer = getTriple().getOSVersion();
+  }
   setTarget(Platform, Environment, OsVer.getMajor(),
             OsVer.getMinor().value_or(0), OsVer.getSubminor().value_or(0),
             VersionTuple());

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

Reply via email to