[clang] f90e121 - [Darwin] Use macOS product version in ensureTargetInitialized (#206902)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 21 05:39:16 PDT 2026
Author: Paulius Velesko
Date: 2026-07-21T07:39:12-05:00
New Revision: f90e121dde60bea7e2988cf277ca4a484bce33b9
URL: https://github.com/llvm/llvm-project/commit/f90e121dde60bea7e2988cf277ca4a484bce33b9
DIFF: https://github.com/llvm/llvm-project/commit/f90e121dde60bea7e2988cf277ca4a484bce33b9.diff
LOG: [Darwin] Use macOS product version in ensureTargetInitialized (#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.
Added:
clang/test/Driver/darwin-hip-spirv-version.hip
Modified:
clang/lib/Driver/ToolChains/Darwin.cpp
clang/lib/Driver/ToolChains/Darwin.h
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index a729fb4fc1a4f..d3de04fc5155e 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1210,10 +1210,23 @@ void Darwin::ensureTargetInitialized() const {
else if (getTriple().isMacCatalystEnvironment())
Environment = MacCatalyst;
- VersionTuple OsVer = getTriple().getOSVersion();
+ VersionTuple OsVer;
+ if (Platform == MacOS) {
+ // Record the macOS product version (e.g. macosx15), not the Darwin kernel
+ // version (e.g. darwin24.3): version checks against the lazily-recorded
+ // target must behave as if AddDeploymentTarget() had computed it.
+ 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());
+ // The version above is a guess from the triple alone; AddDeploymentTarget()
+ // may later derive a
diff erent deployment target from flags, environment
+ // variables, or the SDK, and overwrite this initialization.
+ TargetInitializedLazily = true;
}
AppleMachO::~AppleMachO() {}
diff --git a/clang/lib/Driver/ToolChains/Darwin.h b/clang/lib/Driver/ToolChains/Darwin.h
index 97520a89d8042..c41bb6c2eead5 100644
--- a/clang/lib/Driver/ToolChains/Darwin.h
+++ b/clang/lib/Driver/ToolChains/Darwin.h
@@ -355,6 +355,11 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public AppleMachO {
// the argument translation business.
mutable bool TargetInitialized;
+ /// Whether the target was lazily initialized from the triple by
+ /// ensureTargetInitialized() rather than by AddDeploymentTarget(). Such a
+ /// target is a best-effort guess that setTarget() may overwrite.
+ mutable bool TargetInitializedLazily = false;
+
// TODO: Are these useful? Can we use Triple::OSType/EnvironmentType instead?
enum DarwinPlatformKind {
MacOS,
@@ -447,10 +452,17 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public AppleMachO {
if (TargetInitialized && TargetPlatform == Platform &&
TargetEnvironment == Environment &&
(Environment == MacCatalyst ? OSTargetVersion : TargetVersion) ==
- VersionTuple(Major, Minor, Micro))
+ VersionTuple(Major, Minor, Micro)) {
+ TargetInitializedLazily = false;
return;
+ }
- assert(!TargetInitialized && "Target already initialized!");
+ // A lazily-initialized target (see ensureTargetInitialized()) is a
+ // best-effort guess from the triple alone; the authoritative
+ // initialization from AddDeploymentTarget() may overwrite it.
+ assert((!TargetInitialized || TargetInitializedLazily) &&
+ "Target already initialized!");
+ TargetInitializedLazily = false;
TargetInitialized = true;
TargetPlatform = Platform;
TargetEnvironment = Environment;
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..754c7a1d079d6
--- /dev/null
+++ b/clang/test/Driver/darwin-hip-spirv-version.hip
@@ -0,0 +1,33 @@
+// The HIP-SPIR-V offload path lazily initializes the Darwin host target from
+// the triple (ensureTargetInitialized) before AddDeploymentTarget() computes
+// the authoritative deployment target; a disagreement between the two used to
+// trip setTarget()'s "Target already initialized!" assertion.
+
+// REQUIRES: spirv-registered-target
+// UNSUPPORTED: system-windows, system-cygwin
+
+// A Darwin *kernel* version in the triple (darwin24.3.0) is recorded as the
+// macOS *product* version (macosx15) and the driver does not crash.
+// RUN: %clang -### -x hip --offload=spirv64 --target=arm64-apple-darwin24.3.0 \
+// RUN: -nogpulib -nogpuinc %s 2>&1 | FileCheck %s
+// CHECK: "-triple" "arm64-apple-macosx15{{[0-9.]*}}"
+// CHECK-NOT: "-triple" "arm64-apple-darwin
+
+// A deployment target from the environment overrides the lazily-recorded
+// triple version instead of asserting.
+// RUN: env MACOSX_DEPLOYMENT_TARGET=14.0 %clang -### -x hip \
+// RUN: --offload=spirv64 --target=arm64-apple-darwin24.3.0 \
+// RUN: -nogpulib -nogpuinc %s 2>&1 | FileCheck --check-prefix=DEPLOY %s
+// DEPLOY: "-triple" "arm64-apple-macosx14.0.0"
+
+// Same for -mmacosx-version-min.
+// RUN: %clang -### -x hip --offload=spirv64 --target=arm64-apple-darwin24.3.0 \
+// RUN: -mmacosx-version-min=14.0 -nogpulib -nogpuinc %s 2>&1 \
+// RUN: | FileCheck --check-prefix=VERMIN %s
+// VERMIN: "-triple" "arm64-apple-macosx14.0.0"
+
+// An unversioned triple must not crash either; the version is inferred (and
+// on a macOS host may
diff er from the lazily-recorded default).
+// RUN: %clang -### -x hip --offload=spirv64 --target=arm64-apple-darwin \
+// RUN: -nogpulib -nogpuinc %s 2>&1 | FileCheck --check-prefix=NOVER %s
+// NOVER: "-triple" "arm64-apple-macosx{{[0-9.]+}}"
More information about the cfe-commits
mailing list