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

This PR adds support for HIP on macOS: Mach-O section naming, Darwin host 
toolchain initialization guards, and HIPSPV behavior when Darwin is the host.

This has been verified using chipStar on MacOS via the PoCL OpenCL 
implementation. 

>From 2ddf80b4034353ab3e883b7e230ac06ab143c2a9 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Sun, 1 Mar 2026 10:59:45 +0200
Subject: [PATCH 1/3] CGCUDANV, HIPUtility: Use Mach-O segment,section format
 for HIP on macOS

---
 clang/lib/CodeGen/CGCUDANV.cpp             | 10 +++++++---
 clang/lib/Driver/ToolChains/HIPUtility.cpp |  7 +++++--
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index e04da90b3cbf6..f08040d1d3d15 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -817,10 +817,14 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() 
{
   llvm::Constant *FatBinStr;
   unsigned FatMagic;
   if (IsHIP) {
-    FatbinConstantName = ".hip_fatbin";
-    FatbinSectionName = ".hipFatBinSegment";
+    // On macOS (Mach-O), section names must be in "segment,section" format.
+    FatbinConstantName =
+        CGM.getTriple().isMacOSX() ? "__HIP,__hip_fatbin" : ".hip_fatbin";
+    FatbinSectionName =
+        CGM.getTriple().isMacOSX() ? "__HIP,__fatbin" : ".hipFatBinSegment";
 
-    ModuleIDSectionName = "__hip_module_id";
+    ModuleIDSectionName =
+        CGM.getTriple().isMacOSX() ? "__HIP,__module_id" : "__hip_module_id";
     ModuleIDPrefix = "__hip_";
 
     if (CudaGpuBinary) {
diff --git a/clang/lib/Driver/ToolChains/HIPUtility.cpp 
b/clang/lib/Driver/ToolChains/HIPUtility.cpp
index 1fcb36cc3a390..c1ca3d5df2a7e 100644
--- a/clang/lib/Driver/ToolChains/HIPUtility.cpp
+++ b/clang/lib/Driver/ToolChains/HIPUtility.cpp
@@ -430,9 +430,12 @@ void HIP::constructGenerateObjFileFromHIPFatBinary(
   }
   if (FoundPrimaryHipFatbinSymbol) {
     // Define the first fatbin symbol
-    if (HostTriple.isWindowsMSVCEnvironment())
+    if (HostTriple.isWindowsMSVCEnvironment()) {
       ObjStream << "  .section .hip_fatbin,\"dw\"\n";
-    else {
+    } else if (HostTriple.isMacOSX()) {
+      // Mach-O requires "segment,section" format
+      ObjStream << "  .section __HIP,__hip_fatbin\n";
+    } else {
       ObjStream << "  .protected " << PrimaryHipFatbinSymbol << "\n";
       ObjStream << "  .type " << PrimaryHipFatbinSymbol << ",@object\n";
       ObjStream << "  .section .hip_fatbin,\"a\",@progbits\n";

>From ff90ccb5c6ff3cc4b1f2b31195afc0a93c226a51 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Sun, 1 Mar 2026 10:59:49 +0200
Subject: [PATCH 2/3] Darwin: Guard HIP/CUDA offloading paths against
 uninitialized target

---
 clang/lib/Driver/ToolChains/Darwin.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index aec1ad7d2f155..88351fb606ea0 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1244,6 +1244,11 @@ void DarwinClang::addClangWarningOptions(ArgStringList 
&CC1Args) const {
   CC1Args.push_back("-Werror=undef-prefix");
 
   // For modern targets, promote certain warnings to errors.
+  // Guard against uninitialized target (e.g. when Darwin is used as host
+  // toolchain for HIP/CUDA offloading where the target platform may not
+  // have been fully set up). See Driver.cpp BuildJobsForAction FIXME.
+  if (!isTargetInitialized())
+    return;
   if (isTargetWatchOSBased() || getTriple().isArch64Bit()) {
     // Always enable -Wdeprecated-objc-isa-usage and promote it
     // to an error.
@@ -3899,6 +3904,10 @@ void Darwin::addStartObjectFileArgs(const ArgList &Args,
 }
 
 void Darwin::CheckObjCARC() const {
+  // Guard against uninitialized target (e.g. when Darwin is used as host
+  // toolchain for HIP/CUDA offloading). See Driver.cpp BuildJobsForAction 
FIXME.
+  if (!isTargetInitialized())
+    return;
   if (isTargetIOSBased() || isTargetWatchOSBased() || isTargetXROS() ||
       (isTargetMacOSBased() && !isMacosxVersionLT(10, 6)))
     return;
@@ -3918,6 +3927,11 @@ SanitizerMask Darwin::getSupportedSanitizers() const {
   Res |= SanitizerKind::FuzzerNoLink;
   Res |= SanitizerKind::ObjCCast;
 
+  // Guard against uninitialized target (e.g. when Darwin is used as host
+  // toolchain for HIP/CUDA offloading). Return base sanitizers only.
+  // See Driver.cpp BuildJobsForAction FIXME.
+  if (!isTargetInitialized())
+    return Res;
   // Prior to 10.9, macOS shipped a version of the C++ standard library without
   // C++11 support. The same is true of iOS prior to version 5. These OS'es are
   // incompatible with -fsanitize=vptr.

>From 1cb477bc3dc65e86a7cbfb87501dd3d4ed6c7605 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Sun, 1 Mar 2026 10:59:56 +0200
Subject: [PATCH 3/3] HIPSPV: Do not delegate addClangTargetOptions to host on
 macOS

Host Darwin flags like -faligned-alloc-unavailable break SPIR-V device
compilation; SPIR-V device code does not share host stdlib limitations.
---
 clang/lib/Driver/ToolChains/HIPSPV.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/HIPSPV.cpp 
b/clang/lib/Driver/ToolChains/HIPSPV.cpp
index 8bdb7ab042b2b..c9be378eb5b32 100644
--- a/clang/lib/Driver/ToolChains/HIPSPV.cpp
+++ b/clang/lib/Driver/ToolChains/HIPSPV.cpp
@@ -159,7 +159,11 @@ void HIPSPVToolChain::addClangTargetOptions(
     return;
   }
 
-  HostTC->addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
+  // NOTE: Unlike other HIP toolchains, we do NOT delegate to
+  // HostTC->addClangTargetOptions() here. On macOS (Darwin), the host 
toolchain
+  // adds flags like -faligned-alloc-unavailable that are specific to macOS
+  // libc++ and break SPIR-V device compilation. SPIR-V device code doesn't
+  // have the same stdlib limitations as the host.
 
   assert(DeviceOffloadingKind == Action::OFK_HIP &&
          "Only HIP offloading kinds are supported for GPUs.");

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

Reply via email to