[clang] [Darwin] Use macOS product version in ensureTargetInitialized (PR #206902)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 1 03:04:06 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-driver
Author: Paulius Velesko (pvelesko)
<details>
<summary>Changes</summary>
`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.
---
Full diff: https://github.com/llvm/llvm-project/pull/206902.diff
2 Files Affected:
- (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+12-1)
- (added) clang/test/Driver/darwin-hip-spirv-version.hip (+21)
``````````diff
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());
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/206902
More information about the cfe-commits
mailing list