Author: Nick Sarnie
Date: 2026-03-16T14:09:38Z
New Revision: 50a5462b2c09962d6c10b8b9122c87557402396e

URL: 
https://github.com/llvm/llvm-project/commit/50a5462b2c09962d6c10b8b9122c87557402396e
DIFF: 
https://github.com/llvm/llvm-project/commit/50a5462b2c09962d6c10b8b9122c87557402396e.diff

LOG: [CodeGen] Fix C++ global dtor for non-zero program AS targets (#186484)

In codegen for C++ global destructors, we pass a pointer to the
destructor to be called at program exit as the first arg to the
`__cxa_atexit` function.

If the target's default program AS and default AS are not equal, we need
to emit an addrspacecast from the program AS to the generic AS (which is
used as the argument type for the first arg of `__cxa_atexit`) in the
function call.

---------

Signed-off-by: Nick Sarnie <[email protected]>

Added: 
    clang/test/CodeGenSPIRV/global-dtor.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/CodeGen/ItaniumCXXABI.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cb5d1c235951f..6f89cc9a30603 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -352,6 +352,7 @@ Bug Fixes to C++ Support
 - Fixed a crash when `explicit(bool)` is used with an incomplete enumeration. 
(#GH183887)
 - Fixed a crash on ``typeid`` of incomplete local types during template 
instantiation. (#GH63242), (#GH176397)
 - Fixed a crash when an immediate-invoked ``consteval`` lambda is used as an 
invalid initializer. (#GH185270)
+- Fixed an assertion failure when using a global destructor with a target with 
a non-default program address space. (#GH186484)
 
 - Inherited constructors in ``dllexport`` classes are now exported for 
ABI-compatible cases, matching 
   MSVC behavior. Constructors with variadic arguments or callee-cleanup 
parameters are not yet supported 

diff  --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp 
b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 8a06051a1c730..6d21db0e26f5e 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -2979,8 +2979,12 @@ static void emitGlobalDtorWithCXAAtExit(CodeGenFunction 
&CGF,
       /*IsVariadic=*/false, /*IsCXXMethod=*/false));
   QualType fnType =
       Context.getFunctionType(Context.VoidTy, {Context.VoidPtrTy}, EPI);
-  llvm::Constant *dtorCallee = cast<llvm::Constant>(dtor.getCallee());
-  dtorCallee = CGF.CGM.getFunctionPointer(dtorCallee, fnType);
+  llvm::Value *dtorCallee = dtor.getCallee();
+  dtorCallee =
+      CGF.CGM.getFunctionPointer(cast<llvm::Constant>(dtorCallee), fnType);
+
+  if (dtorCallee->getType()->getPointerAddressSpace() != AddrAS)
+    dtorCallee = CGF.performAddrSpaceCast(dtorCallee, AddrPtrTy);
 
   if (!addr)
     // addr is null when we are trying to register a dtor annotated with

diff  --git a/clang/test/CodeGenSPIRV/global-dtor.cpp 
b/clang/test/CodeGenSPIRV/global-dtor.cpp
new file mode 100644
index 0000000000000..da3b1e333a80e
--- /dev/null
+++ b/clang/test/CodeGenSPIRV/global-dtor.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple spirv64-intel %s -emit-llvm -o - | FileCheck %s
+
+// CHECK: all spir_func addrspace(9) i32 @__cxa_atexit(ptr addrspace(4) 
addrspacecast (ptr addrspace(9) @{{.*}} to ptr addrspace(4)),
+struct S {
+  ~S() {}
+};
+S s;


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

Reply via email to