https://github.com/ian-twilightcoder updated 
https://github.com/llvm/llvm-project/pull/171970

>From 5c5e79b90a9df70cc7d46f1e9319331791f1b17a Mon Sep 17 00:00:00 2001
From: Ian Anderson <[email protected]>
Date: Wed, 10 Dec 2025 13:58:52 -0800
Subject: [PATCH 1/3] [clang][driver][darwin] Base the system prefix on the
 SDK, not the target

The search path prefix is really a property of the SDK, and not the target 
triple. The target is just being used as a proxy for the SDK. That's 
problemmatic when the SDK being used doesn't match the target assumption, and 
the prefix should be taken from the SDK rather than hard coded.

The prefix is actually per target triple within an SDK, so make a map from 
target to prefix in DarwinSDKInfo. That requires making Triple support being 
used as a DenseMap key. From there, have the DarwinClang tool chain get the 
prefix from its SDKInfo.
---
 clang/include/clang/Basic/DarwinSDKInfo.h     | 13 ++++-
 clang/lib/Basic/DarwinSDKInfo.cpp             | 50 ++++++++++++++++++-
 clang/lib/Driver/ToolChains/Darwin.cpp        | 16 +++---
 clang/lib/Driver/ToolChains/Darwin.h          |  6 +++
 .../DriverKit21.0.1.sdk/SDKSettings.json      |  4 ++
 .../Inputs/DriverKit23.0.sdk/SDKSettings.json |  5 +-
 .../SDKSettings.json                          | 14 ++++++
 .../Inputs/MacOSX15.0.sdk/SDKSettings.json    |  6 ++-
 .../Inputs/MacOSX15.1.sdk/SDKSettings.json    |  6 ++-
 .../Inputs/WatchOS6.0.sdk/SDKSettings.json    |  5 +-
 .../Inputs/iPhoneOS13.0.sdk/SDKSettings.json  |  5 +-
 clang/test/Driver/driverkit-path.c            |  4 ++
 .../Inputs/MacOSX13.0.sdk/SDKSettings.json    | 19 ++++++-
 .../Inputs/AppleTVOS15.0.sdk/SDKSettings.json |  8 +++
 .../Inputs/MacOSX11.0.sdk/SDKSettings.json    | 17 ++++++-
 .../Inputs/WatchOS7.0.sdk/SDKSettings.json    |  8 +++
 .../Sema/Inputs/XROS.sdk/SDKSettings.json     |  3 ++
 clang/unittests/Basic/DarwinSDKInfoTest.cpp   | 32 ++++++++++++
 llvm/include/llvm/TargetParser/Triple.h       | 25 +++++++++-
 llvm/lib/TargetParser/Triple.cpp              |  2 +
 llvm/unittests/TargetParser/TripleTest.cpp    | 13 +++++
 21 files changed, 245 insertions(+), 16 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/DriverKit21.0.1.sdk/SDKSettings.json

diff --git a/clang/include/clang/Basic/DarwinSDKInfo.h 
b/clang/include/clang/Basic/DarwinSDKInfo.h
index bc122c7d21c72..41ecca4dc4c3a 100644
--- a/clang/include/clang/Basic/DarwinSDKInfo.h
+++ b/clang/include/clang/Basic/DarwinSDKInfo.h
@@ -144,18 +144,28 @@ class DarwinSDKInfo {
   DarwinSDKInfo(
       VersionTuple Version, VersionTuple MaximumDeploymentTarget,
       llvm::Triple::OSType OS,
+      llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes =
+          llvm::SmallDenseMap<llvm::Triple, std::string>(),
       llvm::DenseMap<OSEnvPair::StorageType,
                      std::optional<RelatedTargetVersionMapping>>
           VersionMappings =
               llvm::DenseMap<OSEnvPair::StorageType,
                              std::optional<RelatedTargetVersionMapping>>())
       : Version(Version), MaximumDeploymentTarget(MaximumDeploymentTarget),
-        OS(OS), VersionMappings(std::move(VersionMappings)) {}
+        OS(OS), SystemPrefixes(SystemPrefixes),
+        VersionMappings(std::move(VersionMappings)) {}
 
   const llvm::VersionTuple &getVersion() const { return Version; }
 
   const llvm::Triple::OSType &getOS() const { return OS; }
 
+  const StringRef getSystemPrefix(llvm::Triple Triple) const {
+    auto SystemPrefix = SystemPrefixes.find(Triple);
+    if (SystemPrefix == SystemPrefixes.end())
+      return StringRef();
+    return SystemPrefix->getSecond();
+  }
+
   // Returns the optional, target-specific version mapping that maps from one
   // target to another target.
   //
@@ -181,6 +191,7 @@ class DarwinSDKInfo {
   VersionTuple Version;
   VersionTuple MaximumDeploymentTarget;
   llvm::Triple::OSType OS;
+  llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes;
   // Need to wrap the value in an optional here as the value has to be default
   // constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
   // Optional as Optional is trying to copy it in emplace.
diff --git a/clang/lib/Basic/DarwinSDKInfo.cpp 
b/clang/lib/Basic/DarwinSDKInfo.cpp
index 6bcfb9d598377..a41dbe45f2230 100644
--- a/clang/lib/Basic/DarwinSDKInfo.cpp
+++ b/clang/lib/Basic/DarwinSDKInfo.cpp
@@ -85,6 +85,52 @@ static llvm::Triple::OSType parseOS(const llvm::json::Object 
&Obj) {
       .Default(llvm::Triple::UnknownOS);
 }
 
+static llvm::SmallDenseMap<llvm::Triple, std::string>
+parseSystemPrefixes(const llvm::json::Object &Obj, llvm::Triple::OSType SDKOS,
+                    VersionTuple Version) {
+  llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes;
+  auto SupportedTargets = Obj.getObject("SupportedTargets");
+  if (!SupportedTargets)
+    return SystemPrefixes;
+  for (auto SupportedTargetPair : *SupportedTargets) {
+    StringRef PlatformOrVariant = SupportedTargetPair.getFirst();
+    if ((PlatformOrVariant == "iosmac") && (Version < VersionTuple(99)))
+      // iosmac has an invalid SystemPrefix, skip it.
+      continue;
+
+    llvm::json::Object *SupportedTarget =
+        SupportedTargetPair.getSecond().getAsObject();
+    auto Archs = SupportedTarget->getArray("Archs");
+    auto Vendor = SupportedTarget->getString("LLVMTargetTripleVendor");
+    auto OS = SupportedTarget->getString("LLVMTargetTripleSys");
+    auto SystemPrefix = SupportedTarget->getString("SystemPrefix");
+    if (!SystemPrefix) {
+      // Older SDKs don't have SystemPrefix in SupportedTargets, manually add
+      // their prefixes.
+      if ((SDKOS == llvm::Triple::DriverKit) && (Version < VersionTuple(22, 
1)))
+        SystemPrefix = "/System/DriverKit";
+    }
+    if (!Archs || !Vendor || !OS || !SystemPrefix)
+      continue;
+
+    auto Environment =
+        SupportedTarget->getString("LLVMTargetTripleEnvironment");
+
+    for (auto Arch : *Archs) {
+      auto ArchString = Arch.getAsString();
+      if (!ArchString)
+        continue;
+      llvm::Triple Triple;
+      if (Environment)
+        Triple = llvm::Triple(*ArchString, *Vendor, *OS, *Environment);
+      else
+        Triple = llvm::Triple(*ArchString, *Vendor, *OS);
+      SystemPrefixes[Triple] = *SystemPrefix;
+    }
+  }
+  return SystemPrefixes;
+}
+
 static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
                                                  StringRef Key) {
   auto Value = Obj.getString(Key);
@@ -106,6 +152,8 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const 
llvm::json::Object *Obj) {
   if (!MaximumDeploymentVersion)
     return std::nullopt;
   llvm::Triple::OSType OS = parseOS(*Obj);
+  llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes =
+      parseSystemPrefixes(*Obj, OS, *Version);
   llvm::DenseMap<OSEnvPair::StorageType,
                  std::optional<RelatedTargetVersionMapping>>
       VersionMappings;
@@ -149,7 +197,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const 
llvm::json::Object *Obj) {
 
   return DarwinSDKInfo(std::move(*Version),
                        std::move(*MaximumDeploymentVersion), OS,
-                       std::move(VersionMappings));
+                       std::move(SystemPrefixes), std::move(VersionMappings));
 }
 
 Expected<std::optional<DarwinSDKInfo>>
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index fc3cd9030f71d..a6688df6f395e 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -566,8 +566,6 @@ static void renderRemarksOptions(const ArgList &Args, 
ArgStringList &CmdArgs,
   }
 }
 
-static void AppendPlatformPrefix(SmallString<128> &Path, const llvm::Triple 
&T);
-
 void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                   const InputInfo &Output,
                                   const InputInfoList &Inputs,
@@ -801,7 +799,7 @@ void darwin::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
       if (auto *Sysroot = Args.getLastArg(options::OPT_isysroot)) {
         auto AddSearchPath = [&](StringRef Flag, StringRef SearchPath) {
           SmallString<128> P(Sysroot->getValue());
-          AppendPlatformPrefix(P, Triple);
+          getMachOToolChain().AppendPlatformPrefix(P, Triple);
           llvm::sys::path::append(P, SearchPath);
           if (getToolChain().getVFS().exists(P)) {
             CmdArgs.push_back(Args.MakeArgString(Flag + P));
@@ -2609,9 +2607,15 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) 
const {
 // For certain platforms/environments almost all resources (e.g., headers) are
 // located in sub-directories, e.g., for DriverKit they live in
 // <SYSROOT>/System/DriverKit/usr/include (instead of <SYSROOT>/usr/include).
-static void AppendPlatformPrefix(SmallString<128> &Path,
-                                 const llvm::Triple &T) {
-  if (T.isDriverKit()) {
+void DarwinClang::AppendPlatformPrefix(SmallString<128> &Path,
+                                       const llvm::Triple &T) const {
+  if (SDKInfo) {
+    const StringRef SystemPrefix = SDKInfo->getSystemPrefix(T);
+    if (!SystemPrefix.empty())
+      llvm::sys::path::append(Path, SystemPrefix);
+  } else if (T.isDriverKit()) {
+    // The first version of DriverKit didn't have SDKSettings.json, manually 
add
+    // its prefix.
     llvm::sys::path::append(Path, "System", "DriverKit");
   }
 }
diff --git a/clang/lib/Driver/ToolChains/Darwin.h 
b/clang/lib/Driver/ToolChains/Darwin.h
index d1cfb6f4a5bf7..c4c52c6a6264b 100644
--- a/clang/lib/Driver/ToolChains/Darwin.h
+++ b/clang/lib/Driver/ToolChains/Darwin.h
@@ -197,6 +197,9 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
                                       llvm::opt::ArgStringList &CmdArgs) const 
{
   }
 
+  virtual void AppendPlatformPrefix(SmallString<128> &Path,
+                                    const llvm::Triple &T) const {}
+
   /// On some iOS platforms, kernel and kernel modules were built statically. 
Is
   /// this such a target?
   virtual bool isKernelStatic() const { return false; }
@@ -670,6 +673,9 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
   void AddLinkARCArgs(const llvm::opt::ArgList &Args,
                       llvm::opt::ArgStringList &CmdArgs) const override;
 
+  void AppendPlatformPrefix(SmallString<128> &Path,
+                            const llvm::Triple &T) const override;
+
   unsigned GetDefaultDwarfVersion() const override;
   // Until dtrace (via CTF) and LLDB can deal with distributed debug info,
   // Darwin defaults to standalone/full debug info.
diff --git a/clang/test/Driver/Inputs/DriverKit21.0.1.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/DriverKit21.0.1.sdk/SDKSettings.json
new file mode 100644
index 0000000000000..f1c42c93057a1
--- /dev/null
+++ b/clang/test/Driver/Inputs/DriverKit21.0.1.sdk/SDKSettings.json
@@ -0,0 +1,4 @@
+{"Version": "21.0.1", "CanonicalName": "driverkit21.0.1", 
"MaximumDeploymentTarget": "21.0.1.99",
+ "SupportedTargets": {
+   "driverkit": {"Archs": ["x86_64", "x86_64h", "arm64", "arm64e"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "driverkit", 
"LLVMTargetTripleEnvironment": ""}
+}}
diff --git a/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json
index edee441adb474..9ad5266c2a248 100644
--- a/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json
+++ b/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json
@@ -1 +1,4 @@
-{"Version":"23.0", "CanonicalName": "driverkit23.0", 
"MaximumDeploymentTarget": "23.0.99"}
+{"Version":"23.0", "CanonicalName": "driverkit23.0", 
"MaximumDeploymentTarget": "23.0.99",
+ "SupportedTargets": {
+   "driverkit": {"Archs": ["x86_64", "x86_64h", "arm64", "arm64e"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "driverkit", 
"LLVMTargetTripleEnvironment": "", "SystemPrefix": "\/System\/DriverKit"}
+}}
diff --git 
a/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json
index 7325cc45a2808..c3b46502069ba 100644
--- a/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json
+++ b/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json
@@ -2,6 +2,20 @@
   "Version":"10.15",
   "CanonicalName": "macosx10.15",
   "MaximumDeploymentTarget": "10.15.99",
+  "SupportedTargets": {
+      "macosx": {
+          "Archs": ["x86_64"],
+          "LLVMTargetTripleVendor": "apple",
+          "LLVMTargetTripleSys": "macosx",
+          "LLVMTargetTripleEnvironment": ""
+      },
+      "iosmac": {
+          "Archs": ["x86_64"],
+          "LLVMTargetTripleVendor": "apple",
+          "LLVMTargetTripleSys": "ios",
+          "LLVMTargetTripleEnvironment": "macabi"
+      }
+  },
   "VersionMap" : {
       "macOS_iOSMac" : {
           "10.15" : "13.1",
diff --git a/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json
index 81d5ee28a5a05..e7c691dd80307 100644
--- a/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json
+++ b/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json
@@ -1 +1,5 @@
-{"Version":"15.0", "CanonicalName": "macosx15.0", "MaximumDeploymentTarget": 
"15.0.99"}
+{"Version":"15.0", "CanonicalName": "macosx15.0", "MaximumDeploymentTarget": 
"15.0.99",
+ "SupportedTargets": {
+   "macosx": {"Archs": ["x86_64", "x86_64h", "arm64", "arm64e"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "macos", 
"LLVMTargetTripleEnvironment": "", "SystemPrefix": ""},
+   "iosmac": {"Archs": ["x86_64", "x86_64h", "arm64", "arm64e"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "ios", 
"LLVMTargetTripleEnvironment": "macabi", "SystemPrefix": "\/System\/iOSSupport"}
+}}
diff --git a/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json
index 956cbe4041b9a..c42e903255296 100644
--- a/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json
+++ b/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json
@@ -1 +1,5 @@
-{"Version":"15.1", "CanonicalName": "macosx15.1", "MaximumDeploymentTarget": 
"15.1.99"}
+{"Version":"15.1", "CanonicalName": "macosx15.1", "MaximumDeploymentTarget": 
"15.1.99",
+ "SupportedTargets": {
+   "macosx": {"Archs": ["x86_64", "x86_64h", "arm64", "arm64e"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "macos", 
"LLVMTargetTripleEnvironment": "", "SystemPrefix": ""},
+   "iosmac": {"Archs": ["x86_64", "x86_64h", "arm64", "arm64e"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "ios", 
"LLVMTargetTripleEnvironment": "macabi", "SystemPrefix": "\/System\/iOSSupport"}
+}}
diff --git a/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json
index 314fc22edf7eb..3e2f817ac1406 100644
--- a/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json
+++ b/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json
@@ -1 +1,4 @@
-{"Version":"6.0", "CanonicalName": "watchos6.0", "MaximumDeploymentTarget": 
"6.0.99"}
+{"Version":"6.0", "CanonicalName": "watchos6.0", "MaximumDeploymentTarget": 
"6.0.99",
+ "SupportedTargets": {
+   "watchos": {"Archs": ["armv7k", "arm64_32"], "LLVMTargetTripleVendor": 
"apple", "LLVMTargetTripleSys": "watchos", "LLVMTargetTripleEnvironment": ""}
+}}
diff --git a/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json 
b/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json
index c122cf47ce0f7..91cb1fa501b3b 100644
--- a/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json
+++ b/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json
@@ -1 +1,4 @@
-{"Version":"13.0", "CanonicalName": "iphoneos13.0", "MaximumDeploymentTarget": 
"13.0.99"}
+{"Version":"13.0", "CanonicalName": "iphoneos13.0", "MaximumDeploymentTarget": 
"13.0.99",
+ "SupportedTargets": {
+   "iphoneos": {"Archs": ["armv7", "armv7s", "arm64"], 
"LLVMTargetTripleVendor": "apple", "LLVMTargetTripleSys": "ios", 
"LLVMTargetTripleEnvironment": ""}
+}}
diff --git a/clang/test/Driver/driverkit-path.c 
b/clang/test/Driver/driverkit-path.c
index 32f0a6721ab77..bc96201753165 100644
--- a/clang/test/Driver/driverkit-path.c
+++ b/clang/test/Driver/driverkit-path.c
@@ -26,6 +26,10 @@ int main() { return 0; }
 
 // RUN: %clang %s -target x86_64-apple-driverkit19.0 -isysroot 
%S/Inputs/DriverKit19.0.sdk -x c++ -### 2>&1 \
 // RUN: | FileCheck %s -DSDKROOT=%S/Inputs/DriverKit19.0.sdk --check-prefix=INC
+// RUN: %clang %s -target x86_64-apple-driverkit21.0.1 -isysroot 
%S/Inputs/DriverKit21.0.1.sdk -x c++ -### 2>&1 \
+// RUN: | FileCheck %s -DSDKROOT=%S/Inputs/DriverKit21.0.1.sdk 
--check-prefix=INC
+// RUN: %clang %s -target x86_64-apple-driverkit23.0 -isysroot 
%S/Inputs/DriverKit23.0.sdk -x c++ -### 2>&1 \
+// RUN: | FileCheck %s -DSDKROOT=%S/Inputs/DriverKit23.0.sdk --check-prefix=INC
 //
 // INC: "-isysroot" "[[SDKROOT]]"
 // INC: "-internal-isystem" "[[SDKROOT]]/System/DriverKit/usr/local/include"
diff --git a/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json 
b/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json
index d08288ed1af10..563d17cfface9 100644
--- a/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json
+++ b/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json
@@ -3,7 +3,24 @@
   "Version": "13.0",
   "CanonicalName": "macosx13.0",
   "MaximumDeploymentTarget": "13.0.99",
-  "PropertyConditionFallbackNames": [], "VersionMap": {
+  "PropertyConditionFallbackNames": [],
+  "SupportedTargets": {
+    "macosx": {
+      "Archs": ["x86_64", "x86_64h", "arm64", "arm64e"],
+      "LLVMTargetTripleVendor": "apple",
+      "LLVMTargetTripleSys": "macos",
+      "LLVMTargetTripleEnvironment": "",
+      "SystemPrefix": ""
+    },
+    "iosmac": {
+      "Archs": ["x86_64", "x86_64h", "arm64", "arm64e"],
+      "LLVMTargetTripleVendor": "apple",
+      "LLVMTargetTripleSys": "ios",
+      "LLVMTargetTripleEnvironment": "macabi",
+      "SystemPrefix": "\/System\/iOSSupport"
+    }
+  },
+  "VersionMap": {
     "iOSMac_macOS": {
       "16.1": "13.0",
       "15.0": "12.0",
diff --git a/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json 
b/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json
index 6cd02f33471ed..1c74c27e48b96 100644
--- a/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json
+++ b/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json
@@ -4,6 +4,14 @@
   "CanonicalName": "appletvos15.0",
   "MaximumDeploymentTarget": "15.0.99",
   "PropertyConditionFallbackNames": [],
+  "SupportedTargets": {
+    "appletvos": {
+      "Archs": ["arm64e", "arm64"],
+      "LLVMTargetTripleVendor": "apple",
+      "LLVMTargetTripleSys": "tvos",
+      "LLVMTargetTripleEnvironment": ""
+    }
+  },
   "VersionMap": {
     "iOS_tvOS": {
       "10.0": "10.0",
diff --git a/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json 
b/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json
index e6220abfbe573..fbb8fa69edf5f 100644
--- a/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json
+++ b/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json
@@ -3,7 +3,22 @@
   "Version": "11.0",
   "CanonicalName": "macosx11.0",
   "MaximumDeploymentTarget": "11.0.99",
-  "PropertyConditionFallbackNames": [], "VersionMap": {
+  "PropertyConditionFallbackNames": [],
+  "SupportedTargets": {
+    "macosx": {
+      "Archs": ["x86_64", "x86_64h", "arm64", "arm64e"],
+      "LLVMTargetTripleVendor": "apple",
+      "LLVMTargetTripleSys": "macosx",
+      "LLVMTargetTripleEnvironment": ""
+    },
+    "iosmac": {
+      "Archs": ["x86_64", "x86_64h", "arm64", "arm64e"],
+      "LLVMTargetTripleVendor": "apple",
+      "LLVMTargetTripleSys": "ios",
+      "LLVMTargetTripleEnvironment": "macabi"
+    }
+  },
+  "VersionMap": {
     "iOSMac_macOS": {
       "13.2": "10.15.1",
       "13.4": "10.15.4",
diff --git a/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json 
b/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json
index 84914c105749a..4481ec7ddefd4 100644
--- a/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json
+++ b/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json
@@ -4,6 +4,14 @@
   "CanonicalName": "watchos7.0",
   "MaximumDeploymentTarget": "7.0.99",
   "PropertyConditionFallbackNames": [],
+  "SupportedTargets": {
+    "watchos": {
+      "Archs": ["arm64_32", "armv7k"],
+      "LLVMTargetTripleVendor": "apple",
+      "LLVMTargetTripleSys": "watchos",
+      "LLVMTargetTripleEnvironment": ""
+    }
+  },
   "VersionMap": {
     "iOS_watchOS": {
       "10.0": "3.0",
diff --git a/clang/test/Sema/Inputs/XROS.sdk/SDKSettings.json 
b/clang/test/Sema/Inputs/XROS.sdk/SDKSettings.json
index a56a7d61431b9..ccae54bcff48c 100644
--- a/clang/test/Sema/Inputs/XROS.sdk/SDKSettings.json
+++ b/clang/test/Sema/Inputs/XROS.sdk/SDKSettings.json
@@ -3,6 +3,9 @@
   "Version": "26.0",
   "CanonicalName": "xros26.0",
   "MaximumDeploymentTarget": "26.0.99",
+  "SupportedTargets": {
+    "xros": {"Archs": ["arm64e", "arm64"], "LLVMTargetTripleVendor": "apple", 
"LLVMTargetTripleSys": "xros", "LLVMTargetTripleEnvironment": "", 
"SystemPrefix": ""}
+  },
   "VersionMap": {
     "iOS_xrOS":{"15.0":"1.0", "16.0":"2.0", "19.0":"26.0", "26.0":"26.0"}
   }
diff --git a/clang/unittests/Basic/DarwinSDKInfoTest.cpp 
b/clang/unittests/Basic/DarwinSDKInfoTest.cpp
index 7214f3bc8e19f..b30ff042c28fd 100644
--- a/clang/unittests/Basic/DarwinSDKInfoTest.cpp
+++ b/clang/unittests/Basic/DarwinSDKInfoTest.cpp
@@ -78,6 +78,38 @@ TEST(DarwinSDKInfo, VersionMappingParseError) {
           .has_value());
 }
 
+TEST(DarwinSDKInfo, SystemPrefix) {
+  llvm::json::Object SDKSettings(
+      {{"Version", "26.0"}, {"MaximumDeploymentTarget", "26.0.99"}});
+  llvm::json::Object SupportedTargets;
+  llvm::json::Object MacOS({{"Archs", {"x86_64", "arm64"}},
+                            {"LLVMTargetTripleVendor", "apple"},
+                            {"LLVMTargetTripleSys", "macos"},
+                            {"LLVMTargetTripleEnvironment", ""},
+                            {"SystemPrefix", ""}});
+  llvm::json::Object MacCatalyst({{"Archs", {"x86_64", "arm64"}},
+                                  {"LLVMTargetTripleVendor", "apple"},
+                                  {"LLVMTargetTripleSys", "ios"},
+                                  {"LLVMTargetTripleEnvironment", "macabi"},
+                                  {"SystemPrefix", "/System/iOSSupport"}});
+  llvm::json::Object DriverKit({{"Archs", {"x86_64", "arm64"}},
+                                {"LLVMTargetTripleVendor", "apple"},
+                                {"LLVMTargetTripleSys", "driverkit"},
+                                {"LLVMTargetTripleEnvironment", ""},
+                                {"SystemPrefix", "/System/DriverKit"}});
+  SupportedTargets["macosx"] = std::move(MacOS);
+  SupportedTargets["iosmac"] = std::move(MacCatalyst);
+  SupportedTargets["driverkit"] = std::move(DriverKit);
+  SDKSettings["SupportedTargets"] = std::move(SupportedTargets);
+
+  auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&SDKSettings);
+  ASSERT_TRUE(SDKInfo);
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-macos26.0")), "");
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-ios26.0-macabi")), 
"");
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-driverkit")),
+            "/System/DriverKit");
+}
+
 TEST(DarwinSDKInfoTest, ParseAndTestMappingMacCatalyst) {
   llvm::json::Object Obj;
   Obj["Version"] = "11.0";
diff --git a/llvm/include/llvm/TargetParser/Triple.h 
b/llvm/include/llvm/TargetParser/Triple.h
index fee8f36c369f7..dddbe4ad3a048 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_TARGETPARSER_TRIPLE_H
 #define LLVM_TARGETPARSER_TRIPLE_H
 
+#include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/VersionTuple.h"
@@ -203,7 +204,8 @@ class Triple {
     OpenEmbedded,
     Intel,
     Meta,
-    LastVendorType = Meta
+    LastVendorType = Meta,
+    TombstoneVendor = LastVendorType + 1
   };
   enum OSType {
     UnknownOS,
@@ -1371,6 +1373,27 @@ class Triple {
   LLVM_ABI std::string computeDataLayout(StringRef ABIName = "") const;
 };
 
+inline hash_code hash_value(const Triple &Val) {
+  return hash_combine(Val.getArch(), Val.getSubArch(), Val.getVendor(),
+                      Val.getOS(), Val.getEnvironment(), 
Val.getObjectFormat());
+}
+
+template <> struct DenseMapInfo<Triple> {
+  static inline Triple getEmptyKey() { return Triple(); }
+
+  static inline Triple getTombstoneKey() {
+    Triple TombstoneKey;
+    TombstoneKey.setVendor(Triple::TombstoneVendor);
+    return TombstoneKey;
+  }
+
+  static unsigned getHashValue(const Triple &Val) { return hash_value(Val); }
+
+  static bool isEqual(const Triple &LHS, const Triple &RHS) {
+    return LHS == RHS;
+  }
+};
+
 } // End llvm namespace
 
 
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 2577b15b19992..8cd354fd1edbb 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -283,6 +283,8 @@ StringRef Triple::getVendorTypeName(VendorType Kind) {
   case SUSE: return "suse";
   case Meta:
     return "meta";
+  case TombstoneVendor:
+    return "tombstone";
   }
 
   llvm_unreachable("Invalid VendorType!");
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp 
b/llvm/unittests/TargetParser/TripleTest.cpp
index df8284d7be66a..96fd798e9f53a 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -3325,6 +3325,19 @@ TEST(TripleTest, isCompatibleWith) {
   }
 }
 
+TEST(TripleTest, equalsAndHash) {
+  EXPECT_EQ(Triple("arm64-apple-ios26.0"), Triple("arm64-apple-ios26.0"));
+  EXPECT_EQ(Triple("arm64-apple-ios26.0"), Triple("arm64-apple-ios26.1"));
+  EXPECT_NE(Triple("arm64-apple-ios26.0"), Triple("arm64-apple-macos26.0"));
+  EXPECT_NE(Triple("arm64-apple-ios26.0"),
+            Triple("arm64-apple-ios26.0-macabi"));
+
+  EXPECT_EQ(hash_value(Triple("arm64-apple-ios26.0")),
+            hash_value(Triple("arm64-apple-ios26.0")));
+  EXPECT_EQ(hash_value(Triple("arm64-apple-ios26.0")),
+            hash_value(Triple("arm64-apple-ios26.1")));
+}
+
 TEST(DataLayoutTest, UEFI) {
   Triple TT = Triple("x86_64-unknown-uefi");
 

>From cb1614b2ae487b01210308325debf27dbf13f98f Mon Sep 17 00:00:00 2001
From: Ian Anderson <[email protected]>
Date: Thu, 11 Dec 2025 11:17:40 -0800
Subject: [PATCH 2/3] Use unordered_map instead of SmallDenseMap

---
 clang/include/clang/Basic/DarwinSDKInfo.h  |  9 +++---
 clang/lib/Basic/DarwinSDKInfo.cpp          |  6 ++--
 llvm/include/llvm/TargetParser/Triple.h    | 33 +++++++---------------
 llvm/lib/TargetParser/Triple.cpp           |  2 --
 llvm/unittests/TargetParser/TripleTest.cpp |  9 +++---
 5 files changed, 23 insertions(+), 36 deletions(-)

diff --git a/clang/include/clang/Basic/DarwinSDKInfo.h 
b/clang/include/clang/Basic/DarwinSDKInfo.h
index 41ecca4dc4c3a..ffe7be0cefb4a 100644
--- a/clang/include/clang/Basic/DarwinSDKInfo.h
+++ b/clang/include/clang/Basic/DarwinSDKInfo.h
@@ -16,6 +16,7 @@
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/TargetParser/Triple.h"
 #include <optional>
+#include <unordered_map>
 
 namespace llvm {
 namespace json {
@@ -144,8 +145,8 @@ class DarwinSDKInfo {
   DarwinSDKInfo(
       VersionTuple Version, VersionTuple MaximumDeploymentTarget,
       llvm::Triple::OSType OS,
-      llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes =
-          llvm::SmallDenseMap<llvm::Triple, std::string>(),
+      std::unordered_map<llvm::Triple, std::string> SystemPrefixes =
+          std::unordered_map<llvm::Triple, std::string>(),
       llvm::DenseMap<OSEnvPair::StorageType,
                      std::optional<RelatedTargetVersionMapping>>
           VersionMappings =
@@ -163,7 +164,7 @@ class DarwinSDKInfo {
     auto SystemPrefix = SystemPrefixes.find(Triple);
     if (SystemPrefix == SystemPrefixes.end())
       return StringRef();
-    return SystemPrefix->getSecond();
+    return SystemPrefix->second;
   }
 
   // Returns the optional, target-specific version mapping that maps from one
@@ -191,7 +192,7 @@ class DarwinSDKInfo {
   VersionTuple Version;
   VersionTuple MaximumDeploymentTarget;
   llvm::Triple::OSType OS;
-  llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes;
+  std::unordered_map<llvm::Triple, std::string> SystemPrefixes;
   // Need to wrap the value in an optional here as the value has to be default
   // constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
   // Optional as Optional is trying to copy it in emplace.
diff --git a/clang/lib/Basic/DarwinSDKInfo.cpp 
b/clang/lib/Basic/DarwinSDKInfo.cpp
index a41dbe45f2230..2d2d0115d52c1 100644
--- a/clang/lib/Basic/DarwinSDKInfo.cpp
+++ b/clang/lib/Basic/DarwinSDKInfo.cpp
@@ -85,10 +85,10 @@ static llvm::Triple::OSType parseOS(const 
llvm::json::Object &Obj) {
       .Default(llvm::Triple::UnknownOS);
 }
 
-static llvm::SmallDenseMap<llvm::Triple, std::string>
+static std::unordered_map<llvm::Triple, std::string>
 parseSystemPrefixes(const llvm::json::Object &Obj, llvm::Triple::OSType SDKOS,
                     VersionTuple Version) {
-  llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes;
+  std::unordered_map<llvm::Triple, std::string> SystemPrefixes;
   auto SupportedTargets = Obj.getObject("SupportedTargets");
   if (!SupportedTargets)
     return SystemPrefixes;
@@ -152,7 +152,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const 
llvm::json::Object *Obj) {
   if (!MaximumDeploymentVersion)
     return std::nullopt;
   llvm::Triple::OSType OS = parseOS(*Obj);
-  llvm::SmallDenseMap<llvm::Triple, std::string> SystemPrefixes =
+  std::unordered_map<llvm::Triple, std::string> SystemPrefixes =
       parseSystemPrefixes(*Obj, OS, *Version);
   llvm::DenseMap<OSEnvPair::StorageType,
                  std::optional<RelatedTargetVersionMapping>>
diff --git a/llvm/include/llvm/TargetParser/Triple.h 
b/llvm/include/llvm/TargetParser/Triple.h
index dddbe4ad3a048..f648fcd39244d 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -9,10 +9,10 @@
 #ifndef LLVM_TARGETPARSER_TRIPLE_H
 #define LLVM_TARGETPARSER_TRIPLE_H
 
-#include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/VersionTuple.h"
+#include <functional>
 
 // Some system headers or GCC predefined macros conflict with identifiers in
 // this file.  Undefine them here.
@@ -204,8 +204,7 @@ class Triple {
     OpenEmbedded,
     Intel,
     Meta,
-    LastVendorType = Meta,
-    TombstoneVendor = LastVendorType + 1
+    LastVendorType = Meta
   };
   enum OSType {
     UnknownOS,
@@ -1373,28 +1372,16 @@ class Triple {
   LLVM_ABI std::string computeDataLayout(StringRef ABIName = "") const;
 };
 
-inline hash_code hash_value(const Triple &Val) {
-  return hash_combine(Val.getArch(), Val.getSubArch(), Val.getVendor(),
-                      Val.getOS(), Val.getEnvironment(), 
Val.getObjectFormat());
-}
-
-template <> struct DenseMapInfo<Triple> {
-  static inline Triple getEmptyKey() { return Triple(); }
-
-  static inline Triple getTombstoneKey() {
-    Triple TombstoneKey;
-    TombstoneKey.setVendor(Triple::TombstoneVendor);
-    return TombstoneKey;
-  }
-
-  static unsigned getHashValue(const Triple &Val) { return hash_value(Val); }
+} // End llvm namespace
 
-  static bool isEqual(const Triple &LHS, const Triple &RHS) {
-    return LHS == RHS;
+namespace std {
+template <> struct hash<llvm::Triple> {
+  size_t operator()(const llvm::Triple &Val) const {
+    return hash_combine(Val.getArch(), Val.getSubArch(), Val.getVendor(),
+                        Val.getOS(), Val.getEnvironment(),
+                        Val.getObjectFormat());
   }
 };
-
-} // End llvm namespace
-
+} // namespace std
 
 #endif
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 8cd354fd1edbb..2577b15b19992 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -283,8 +283,6 @@ StringRef Triple::getVendorTypeName(VendorType Kind) {
   case SUSE: return "suse";
   case Meta:
     return "meta";
-  case TombstoneVendor:
-    return "tombstone";
   }
 
   llvm_unreachable("Invalid VendorType!");
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp 
b/llvm/unittests/TargetParser/TripleTest.cpp
index 96fd798e9f53a..92c42a9dcf98a 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -3332,10 +3332,11 @@ TEST(TripleTest, equalsAndHash) {
   EXPECT_NE(Triple("arm64-apple-ios26.0"),
             Triple("arm64-apple-ios26.0-macabi"));
 
-  EXPECT_EQ(hash_value(Triple("arm64-apple-ios26.0")),
-            hash_value(Triple("arm64-apple-ios26.0")));
-  EXPECT_EQ(hash_value(Triple("arm64-apple-ios26.0")),
-            hash_value(Triple("arm64-apple-ios26.1")));
+  std::hash<Triple> Hasher;
+  EXPECT_EQ(Hasher(Triple("arm64-apple-ios26.0")),
+            Hasher(Triple("arm64-apple-ios26.0")));
+  EXPECT_EQ(Hasher(Triple("arm64-apple-ios26.0")),
+            Hasher(Triple("arm64-apple-ios26.1")));
 }
 
 TEST(DataLayoutTest, UEFI) {

>From 10a3021510e3f3b88caa289c7384ccaceb7429ba Mon Sep 17 00:00:00 2001
From: Ian Anderson <[email protected]>
Date: Wed, 17 Dec 2025 14:56:20 -0800
Subject: [PATCH 3/3] Make system prefix lookup more lenient

Don't consider arch/subarch when looking up a system prefix.
If a triple isn't found in the supported targets, fall back on the triple 
matching the SDK's platform.
---
 clang/include/clang/Basic/DarwinSDKInfo.h   |  51 ++++++--
 clang/lib/Basic/DarwinSDKInfo.cpp           | 132 +++++++++++---------
 clang/lib/Driver/ToolChains/Darwin.cpp      |  17 ++-
 clang/unittests/Basic/DarwinSDKInfoTest.cpp |  19 +--
 llvm/include/llvm/TargetParser/Triple.h     |  10 --
 llvm/unittests/TargetParser/TripleTest.cpp  |  14 ---
 6 files changed, 142 insertions(+), 101 deletions(-)

diff --git a/clang/include/clang/Basic/DarwinSDKInfo.h 
b/clang/include/clang/Basic/DarwinSDKInfo.h
index ffe7be0cefb4a..79f4340cee169 100644
--- a/clang/include/clang/Basic/DarwinSDKInfo.h
+++ b/clang/include/clang/Basic/DarwinSDKInfo.h
@@ -11,6 +11,7 @@
 
 #include "clang/Basic/LLVM.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/Hashing.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/VersionTuple.h"
 #include "llvm/Support/VirtualFileSystem.h"
@@ -142,28 +143,53 @@ class DarwinSDKInfo {
     llvm::DenseMap<VersionTuple, VersionTuple> Mapping;
   };
 
+  // Special versions of hash and equal_to to map triples to system prefixes
+  // ignoring the Arch/SubArch.
+  struct TripleHash {
+    std::size_t operator()(const llvm::Triple &Val) const noexcept {
+      return hash_combine(Val.getVendor(), Val.getOS(), Val.getEnvironment(),
+                          Val.getObjectFormat());
+    }
+  };
+  struct TripleEqualTo {
+    constexpr bool operator()(const llvm::Triple &LHS,
+                              const llvm::Triple &RHS) const {
+      return (LHS.getVendor() == RHS.getVendor()) &&
+             (LHS.getOS() == RHS.getOS()) &&
+             (LHS.getEnvironment() == RHS.getEnvironment()) &&
+             (LHS.getObjectFormat() == RHS.getObjectFormat());
+    }
+  };
+
   DarwinSDKInfo(
       VersionTuple Version, VersionTuple MaximumDeploymentTarget,
-      llvm::Triple::OSType OS,
-      std::unordered_map<llvm::Triple, std::string> SystemPrefixes =
-          std::unordered_map<llvm::Triple, std::string>(),
+      llvm::Triple Triple,
+      std::unordered_map<llvm::Triple, std::string, TripleHash, TripleEqualTo>
+          SystemPrefixes = std::unordered_map<llvm::Triple, std::string,
+                                              TripleHash, TripleEqualTo>(),
       llvm::DenseMap<OSEnvPair::StorageType,
                      std::optional<RelatedTargetVersionMapping>>
           VersionMappings =
               llvm::DenseMap<OSEnvPair::StorageType,
                              std::optional<RelatedTargetVersionMapping>>())
       : Version(Version), MaximumDeploymentTarget(MaximumDeploymentTarget),
-        OS(OS), SystemPrefixes(SystemPrefixes),
+        Triple(Triple), SystemPrefixes(SystemPrefixes),
         VersionMappings(std::move(VersionMappings)) {}
 
   const llvm::VersionTuple &getVersion() const { return Version; }
 
-  const llvm::Triple::OSType &getOS() const { return OS; }
-
-  const StringRef getSystemPrefix(llvm::Triple Triple) const {
-    auto SystemPrefix = SystemPrefixes.find(Triple);
-    if (SystemPrefix == SystemPrefixes.end())
-      return StringRef();
+  /// A representative triple for the SDK - Arch/SubArch are always
+  /// unknown/none.
+  const llvm::Triple &getTriple() const { return Triple; }
+
+  const StringRef getSystemPrefix(llvm::Triple TheTriple) const {
+    auto SystemPrefix = SystemPrefixes.find(TheTriple);
+    if (SystemPrefix == SystemPrefixes.end()) {
+      // Fall back on the canonical triple.
+      SystemPrefix = SystemPrefixes.find(Triple);
+      if (SystemPrefix == SystemPrefixes.end())
+        return StringRef();
+    }
     return SystemPrefix->second;
   }
 
@@ -191,8 +217,9 @@ class DarwinSDKInfo {
 private:
   VersionTuple Version;
   VersionTuple MaximumDeploymentTarget;
-  llvm::Triple::OSType OS;
-  std::unordered_map<llvm::Triple, std::string> SystemPrefixes;
+  llvm::Triple Triple;
+  std::unordered_map<llvm::Triple, std::string, TripleHash, TripleEqualTo>
+      SystemPrefixes;
   // Need to wrap the value in an optional here as the value has to be default
   // constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
   // Optional as Optional is trying to copy it in emplace.
diff --git a/clang/lib/Basic/DarwinSDKInfo.cpp 
b/clang/lib/Basic/DarwinSDKInfo.cpp
index 2d2d0115d52c1..df280aa6c38ca 100644
--- a/clang/lib/Basic/DarwinSDKInfo.cpp
+++ b/clang/lib/Basic/DarwinSDKInfo.cpp
@@ -63,72 +63,90 @@ DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(
       Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
 }
 
-static llvm::Triple::OSType parseOS(const llvm::json::Object &Obj) {
+static std::pair<llvm::Triple, std::unordered_map<llvm::Triple, std::string,
+                                                  DarwinSDKInfo::TripleHash,
+                                                  
DarwinSDKInfo::TripleEqualTo>>
+parseSupportedTargets(const llvm::json::Object &Obj, VersionTuple Version) {
   // The CanonicalName is the Xcode platform followed by a version, e.g.
-  // macosx16.0.
-  auto CanonicalName = Obj.getString("CanonicalName");
-  if (!CanonicalName)
-    return llvm::Triple::UnknownOS;
-  size_t VersionStart = CanonicalName->find_first_of("0123456789");
-  StringRef XcodePlatform = CanonicalName->slice(0, VersionStart);
-  return llvm::StringSwitch<llvm::Triple::OSType>(XcodePlatform)
-      .Case("macosx", llvm::Triple::MacOSX)
-      .Case("iphoneos", llvm::Triple::IOS)
-      .Case("iphonesimulator", llvm::Triple::IOS)
-      .Case("appletvos", llvm::Triple::TvOS)
-      .Case("appletvsimulator", llvm::Triple::TvOS)
-      .Case("watchos", llvm::Triple::WatchOS)
-      .Case("watchsimulator", llvm::Triple::WatchOS)
-      .Case("xros", llvm::Triple::XROS)
-      .Case("xrsimulator", llvm::Triple::XROS)
-      .Case("driverkit", llvm::Triple::DriverKit)
-      .Default(llvm::Triple::UnknownOS);
-}
+  // macosx15.0.
+  StringRef XcodePlatform;
+  if (auto CanonicalName = Obj.getString("CanonicalName")) {
+    size_t VersionStart = CanonicalName->find_first_of("0123456789");
+    XcodePlatform = CanonicalName->slice(0, VersionStart);
+  }
 
-static std::unordered_map<llvm::Triple, std::string>
-parseSystemPrefixes(const llvm::json::Object &Obj, llvm::Triple::OSType SDKOS,
-                    VersionTuple Version) {
-  std::unordered_map<llvm::Triple, std::string> SystemPrefixes;
+  std::unordered_map<llvm::Triple, std::string, DarwinSDKInfo::TripleHash,
+                     DarwinSDKInfo::TripleEqualTo>
+      SystemPrefixes;
   auto SupportedTargets = Obj.getObject("SupportedTargets");
-  if (!SupportedTargets)
-    return SystemPrefixes;
-  for (auto SupportedTargetPair : *SupportedTargets) {
-    StringRef PlatformOrVariant = SupportedTargetPair.getFirst();
-    if ((PlatformOrVariant == "iosmac") && (Version < VersionTuple(99)))
-      // iosmac has an invalid SystemPrefix, skip it.
-      continue;
+  if (!SupportedTargets) {
+    // For older SDKs that don't have SupportedTargets, infer the triple from
+    // the Xcode platform.
+    llvm::Triple InferredTriple;
+    InferredTriple.setVendor(llvm::Triple::Apple);
+    if (XcodePlatform == "macosx") {
+      InferredTriple.setOS(llvm::Triple::MacOSX);
+    } else if (XcodePlatform == "iphoneos") {
+      InferredTriple.setOS(llvm::Triple::IOS);
+    } else if (XcodePlatform == "iphonesimulator") {
+      InferredTriple.setOS(llvm::Triple::IOS);
+      InferredTriple.setEnvironment(llvm::Triple::Simulator);
+    } else if (XcodePlatform == "appletvos") {
+      InferredTriple.setOS(llvm::Triple::TvOS);
+    } else if (XcodePlatform == "appletvsimulator") {
+      InferredTriple.setOS(llvm::Triple::TvOS);
+      InferredTriple.setEnvironment(llvm::Triple::Simulator);
+    } else if (XcodePlatform == "watchos") {
+      InferredTriple.setOS(llvm::Triple::WatchOS);
+    } else if (XcodePlatform == "watchsimulator") {
+      InferredTriple.setOS(llvm::Triple::WatchOS);
+      InferredTriple.setEnvironment(llvm::Triple::Simulator);
+    } else if (XcodePlatform == "driverkit") {
+      InferredTriple.setOS(llvm::Triple::DriverKit);
+    }
+    return {InferredTriple, SystemPrefixes};
+  }
 
+  llvm::Triple PlatformTriple;
+  for (auto SupportedTargetPair : *SupportedTargets) {
     llvm::json::Object *SupportedTarget =
         SupportedTargetPair.getSecond().getAsObject();
-    auto Archs = SupportedTarget->getArray("Archs");
     auto Vendor = SupportedTarget->getString("LLVMTargetTripleVendor");
     auto OS = SupportedTarget->getString("LLVMTargetTripleSys");
-    auto SystemPrefix = SupportedTarget->getString("SystemPrefix");
-    if (!SystemPrefix) {
-      // Older SDKs don't have SystemPrefix in SupportedTargets, manually add
-      // their prefixes.
-      if ((SDKOS == llvm::Triple::DriverKit) && (Version < VersionTuple(22, 
1)))
-        SystemPrefix = "/System/DriverKit";
-    }
-    if (!Archs || !Vendor || !OS || !SystemPrefix)
+    if (!Vendor || !OS)
       continue;
 
+    StringRef Arch = llvm::Triple::getArchName(llvm::Triple::UnknownArch);
     auto Environment =
         SupportedTarget->getString("LLVMTargetTripleEnvironment");
+    llvm::Triple Triple;
+    if (Environment)
+      Triple = llvm::Triple(Arch, *Vendor, *OS, *Environment);
+    else
+      Triple = llvm::Triple(Arch, *Vendor, *OS);
 
-    for (auto Arch : *Archs) {
-      auto ArchString = Arch.getAsString();
-      if (!ArchString)
-        continue;
-      llvm::Triple Triple;
-      if (Environment)
-        Triple = llvm::Triple(*ArchString, *Vendor, *OS, *Environment);
-      else
-        Triple = llvm::Triple(*ArchString, *Vendor, *OS);
+    StringRef PlatformOrVariant = SupportedTargetPair.getFirst();
+    if (PlatformOrVariant == XcodePlatform)
+      PlatformTriple = Triple;
+
+    if ((PlatformOrVariant == "iosmac") && (Version < VersionTuple(99))) {
+      // iosmac has an invalid SystemPrefix, ignore it.
+      SystemPrefixes[Triple] = StringRef();
+    } else {
+      auto SystemPrefix = SupportedTarget->getString("SystemPrefix");
+      if (!SystemPrefix) {
+        // Older SDKs don't have SystemPrefix in SupportedTargets, manually add
+        // their prefixes.
+        if ((Triple.getOS() == llvm::Triple::DriverKit) &&
+            (Version < VersionTuple(22, 1)))
+          SystemPrefix = "/System/DriverKit";
+        else
+          SystemPrefix = StringRef();
+      }
       SystemPrefixes[Triple] = *SystemPrefix;
     }
   }
-  return SystemPrefixes;
+  return {PlatformTriple, SystemPrefixes};
 }
 
 static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
@@ -151,9 +169,10 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const 
llvm::json::Object *Obj) {
       getVersionKey(*Obj, "MaximumDeploymentTarget");
   if (!MaximumDeploymentVersion)
     return std::nullopt;
-  llvm::Triple::OSType OS = parseOS(*Obj);
-  std::unordered_map<llvm::Triple, std::string> SystemPrefixes =
-      parseSystemPrefixes(*Obj, OS, *Version);
+  std::pair<llvm::Triple, std::unordered_map<llvm::Triple, std::string,
+                                             DarwinSDKInfo::TripleHash,
+                                             DarwinSDKInfo::TripleEqualTo>>
+      TripleAndSystemPrefixes = parseSupportedTargets(*Obj, *Version);
   llvm::DenseMap<OSEnvPair::StorageType,
                  std::optional<RelatedTargetVersionMapping>>
       VersionMappings;
@@ -195,9 +214,10 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const 
llvm::json::Object *Obj) {
     }
   }
 
-  return DarwinSDKInfo(std::move(*Version),
-                       std::move(*MaximumDeploymentVersion), OS,
-                       std::move(SystemPrefixes), std::move(VersionMappings));
+  return DarwinSDKInfo(
+      std::move(*Version), std::move(*MaximumDeploymentVersion),
+      std::move(TripleAndSystemPrefixes.first),
+      std::move(TripleAndSystemPrefixes.second), std::move(VersionMappings));
 }
 
 Expected<std::optional<DarwinSDKInfo>>
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index a6688df6f395e..35047a2d4d18d 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1911,10 +1911,13 @@ struct DarwinPlatform {
   /// the platform from the SDKPath.
   DarwinSDKInfo inferSDKInfo() {
     assert(Kind == InferredFromSDK && "can infer SDK info only");
+    llvm::Triple InferredTriple;
+    InferredTriple.setVendor(llvm::Triple::Apple);
+    InferredTriple.setOS(getOSFromPlatform(Platform));
     return DarwinSDKInfo(getOSVersion(),
                          /*MaximumDeploymentTarget=*/
                          VersionTuple(getOSVersion().getMajor(), 0, 99),
-                         getOSFromPlatform(Platform));
+                         InferredTriple);
   }
 
 private:
@@ -3124,8 +3127,18 @@ sdkSupportsBuiltinModules(const 
std::optional<DarwinSDKInfo> &SDKInfo) {
     // the old behavior which is to not use builtin modules.
     return false;
 
+  llvm::Triple Triple = SDKInfo->getTriple();
+  if (!Triple.hasEnvironment() || Triple.isSimulatorEnvironment() ||
+      Triple.isMacCatalystEnvironment()) {
+    // Standard xnu/Mach/Darwin based environments
+    // depend on the SDK version.
+  } else {
+    // All other environments support builtin modules from the start.
+    return true;
+  }
+
   VersionTuple SDKVersion = SDKInfo->getVersion();
-  switch (SDKInfo->getOS()) {
+  switch (Triple.getOS()) {
   // Existing SDKs added support for builtin modules in the fall
   // 2024 major releases.
   case llvm::Triple::MacOSX:
diff --git a/clang/unittests/Basic/DarwinSDKInfoTest.cpp 
b/clang/unittests/Basic/DarwinSDKInfoTest.cpp
index b30ff042c28fd..f845b5687b13a 100644
--- a/clang/unittests/Basic/DarwinSDKInfoTest.cpp
+++ b/clang/unittests/Basic/DarwinSDKInfoTest.cpp
@@ -79,14 +79,15 @@ TEST(DarwinSDKInfo, VersionMappingParseError) {
 }
 
 TEST(DarwinSDKInfo, SystemPrefix) {
-  llvm::json::Object SDKSettings(
-      {{"Version", "26.0"}, {"MaximumDeploymentTarget", "26.0.99"}});
+  llvm::json::Object SDKSettings({{"CanonicalName", "macosx26.0"},
+                                  {"Version", "26.0"},
+                                  {"MaximumDeploymentTarget", "26.0.99"}});
   llvm::json::Object SupportedTargets;
   llvm::json::Object MacOS({{"Archs", {"x86_64", "arm64"}},
                             {"LLVMTargetTripleVendor", "apple"},
                             {"LLVMTargetTripleSys", "macos"},
                             {"LLVMTargetTripleEnvironment", ""},
-                            {"SystemPrefix", ""}});
+                            {"SystemPrefix", "/System/macOSSupport"}});
   llvm::json::Object MacCatalyst({{"Archs", {"x86_64", "arm64"}},
                                   {"LLVMTargetTripleVendor", "apple"},
                                   {"LLVMTargetTripleSys", "ios"},
@@ -96,7 +97,7 @@ TEST(DarwinSDKInfo, SystemPrefix) {
                                 {"LLVMTargetTripleVendor", "apple"},
                                 {"LLVMTargetTripleSys", "driverkit"},
                                 {"LLVMTargetTripleEnvironment", ""},
-                                {"SystemPrefix", "/System/DriverKit"}});
+                                {"SystemPrefix", ""}});
   SupportedTargets["macosx"] = std::move(MacOS);
   SupportedTargets["iosmac"] = std::move(MacCatalyst);
   SupportedTargets["driverkit"] = std::move(DriverKit);
@@ -104,10 +105,14 @@ TEST(DarwinSDKInfo, SystemPrefix) {
 
   auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&SDKSettings);
   ASSERT_TRUE(SDKInfo);
-  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-macos26.0")), "");
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-macos26.0")),
+            "/System/macOSSupport");
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("ppc-apple-macos26.0")),
+            "/System/macOSSupport");
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-ios26.0")),
+            "/System/macOSSupport");
   EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-ios26.0-macabi")), 
"");
-  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-driverkit")),
-            "/System/DriverKit");
+  EXPECT_EQ(SDKInfo->getSystemPrefix(Triple("arm64-apple-driverkit")), "");
 }
 
 TEST(DarwinSDKInfoTest, ParseAndTestMappingMacCatalyst) {
diff --git a/llvm/include/llvm/TargetParser/Triple.h 
b/llvm/include/llvm/TargetParser/Triple.h
index f648fcd39244d..fee8f36c369f7 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -12,7 +12,6 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/VersionTuple.h"
-#include <functional>
 
 // Some system headers or GCC predefined macros conflict with identifiers in
 // this file.  Undefine them here.
@@ -1374,14 +1373,5 @@ class Triple {
 
 } // End llvm namespace
 
-namespace std {
-template <> struct hash<llvm::Triple> {
-  size_t operator()(const llvm::Triple &Val) const {
-    return hash_combine(Val.getArch(), Val.getSubArch(), Val.getVendor(),
-                        Val.getOS(), Val.getEnvironment(),
-                        Val.getObjectFormat());
-  }
-};
-} // namespace std
 
 #endif
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp 
b/llvm/unittests/TargetParser/TripleTest.cpp
index 92c42a9dcf98a..df8284d7be66a 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -3325,20 +3325,6 @@ TEST(TripleTest, isCompatibleWith) {
   }
 }
 
-TEST(TripleTest, equalsAndHash) {
-  EXPECT_EQ(Triple("arm64-apple-ios26.0"), Triple("arm64-apple-ios26.0"));
-  EXPECT_EQ(Triple("arm64-apple-ios26.0"), Triple("arm64-apple-ios26.1"));
-  EXPECT_NE(Triple("arm64-apple-ios26.0"), Triple("arm64-apple-macos26.0"));
-  EXPECT_NE(Triple("arm64-apple-ios26.0"),
-            Triple("arm64-apple-ios26.0-macabi"));
-
-  std::hash<Triple> Hasher;
-  EXPECT_EQ(Hasher(Triple("arm64-apple-ios26.0")),
-            Hasher(Triple("arm64-apple-ios26.0")));
-  EXPECT_EQ(Hasher(Triple("arm64-apple-ios26.0")),
-            Hasher(Triple("arm64-apple-ios26.1")));
-}
-
 TEST(DataLayoutTest, UEFI) {
   Triple TT = Triple("x86_64-unknown-uefi");
 

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

Reply via email to