[clang] [llvm] [clang][CodeGen] Global constructors/destructors are globals (PR #93914)

Alex Voicu via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 3 07:35:41 PDT 2024


https://github.com/AlexVlx updated https://github.com/llvm/llvm-project/pull/93914

>From 34b9646a38c6bdd0419dd9d2eb1bc847c4d16596 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Tue, 28 May 2024 20:09:15 +0100
Subject: [PATCH 1/9] Fix `emitUsed` to place `used` globals in the Global AS.
 Update tests accordingly.

---
 clang/lib/CodeGen/CodeGenModule.cpp                   | 11 ++++++-----
 clang/test/CodeGen/2005-12-04-AttributeUsed.c         |  2 ++
 clang/test/CodeGen/attr-retain.c                      |  4 +++-
 clang/test/CodeGenCUDA/llvm-used.cu                   |  7 +++++--
 clang/test/CodeGenCXX/attr-retain.cpp                 |  2 ++
 ...static-member-variable-explicit-specialization.cpp |  6 +++++-
 .../test/OpenMP/amdgcn_target_global_constructor.cpp  |  4 ++--
 7 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index e4774a587707a..30a18b1c5e7a8 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2047,9 +2047,9 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
   llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
       TheModule.getDataLayout().getProgramAddressSpace());
 
-  // Get the type of a ctor entry, { i32, void ()*, i8* }.
+  // Get the type of a ctor entry, { i32, program void ()*, global i8* }.
   llvm::StructType *CtorStructTy = llvm::StructType::get(
-      Int32Ty, CtorPFTy, VoidPtrTy);
+      Int32Ty, CtorPFTy, GlobalsInt8PtrTy);
 
   // Construct the constructor and destructor arrays.
   ConstantInitBuilder builder(*this);
@@ -2061,7 +2061,7 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
     if (I.AssociatedData)
       ctor.add(I.AssociatedData);
     else
-      ctor.addNullPointer(VoidPtrTy);
+      ctor.addNullPointer(GlobalsInt8PtrTy);
     ctor.finishAndAddTo(ctors);
   }
 
@@ -2928,12 +2928,13 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   for (unsigned i = 0, e = List.size(); i != e; ++i) {
     UsedArray[i] =
         llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
-            cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
+            cast<llvm::Constant>(&*List[i]), CGM.GlobalsInt8PtrTy);
   }
 
   if (UsedArray.empty())
     return;
-  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
+  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.GlobalsInt8PtrTy,
+                                              UsedArray.size());
 
   auto *GV = new llvm::GlobalVariable(
       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
diff --git a/clang/test/CodeGen/2005-12-04-AttributeUsed.c b/clang/test/CodeGen/2005-12-04-AttributeUsed.c
index bd232831fe1a6..c5089d9bda2c6 100644
--- a/clang/test/CodeGen/2005-12-04-AttributeUsed.c
+++ b/clang/test/CodeGen/2005-12-04-AttributeUsed.c
@@ -1,6 +1,8 @@
 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm -o - | FileCheck %s --check-prefix=GLOBALAS
 
 // CHECK: @llvm.used = appending global [2 x ptr] [ptr @foo, ptr @X], section "llvm.metadata"
+// GLOBALAS: @llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(1)] [ptr addrspace(1) addrspacecast (ptr @foo to ptr addrspace(1)), ptr addrspace(1) @X], section "llvm.metadata"
 int X __attribute__((used));
 int Y;
 
diff --git a/clang/test/CodeGen/attr-retain.c b/clang/test/CodeGen/attr-retain.c
index fc41be76f9d0a..e86f6ae8ed06c 100644
--- a/clang/test/CodeGen/attr-retain.c
+++ b/clang/test/CodeGen/attr-retain.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -emit-llvm -triple x86_64 %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -triple amdgcn-amd-amdhsa %s -o - | FileCheck %s --check-prefix=GLOBALAS
 
 /// Set !retain regardless of the target. The backend will lower !retain to
 /// SHF_GNU_RETAIN on ELF and ignore the metadata for other binary formats.
@@ -11,7 +12,8 @@
 
 // CHECK:      @llvm.used = appending global [8 x ptr] [ptr @c0, ptr @foo.l0, ptr @f0, ptr @f2, ptr @g0, ptr @g1, ptr @g3, ptr @g4], section "llvm.metadata"
 // CHECK:      @llvm.compiler.used = appending global [3 x ptr] [ptr @f2, ptr @g3, ptr @g4], section "llvm.metadata"
-
+// GLOBALAS:   @llvm.used = appending addrspace(1) global [8 x ptr addrspace(1)] [ptr addrspace(1) addrspacecast (ptr addrspace(4) @c0 to ptr addrspace(1)), ptr addrspace(1) @foo.l0, ptr addrspace(1) addrspacecast (ptr @f0 to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr @f2 to ptr addrspace(1)), ptr addrspace(1) @g0, ptr addrspace(1) @g1, ptr addrspace(1) @g3, ptr addrspace(1) @g4], section "llvm.metadata"
+// GLOBALAS:   @llvm.compiler.used = appending addrspace(1) global [3 x ptr addrspace(1)] [ptr addrspace(1) addrspacecast (ptr @f2 to ptr addrspace(1)), ptr addrspace(1) @g3, ptr addrspace(1) @g4], section "llvm.metadata"
 const int c0 __attribute__((retain)) = 42;
 
 void foo(void) {
diff --git a/clang/test/CodeGenCUDA/llvm-used.cu b/clang/test/CodeGenCUDA/llvm-used.cu
index c39111dd48036..7782048b52836 100644
--- a/clang/test/CodeGenCUDA/llvm-used.cu
+++ b/clang/test/CodeGenCUDA/llvm-used.cu
@@ -1,8 +1,11 @@
 // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx64-unknown-unknown | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple amdgcn-amd-amdhsa | FileCheck %s --check-prefix=NOASCAST
 
 
-// Make sure we emit the proper addrspacecast for llvm.used.  PR22383 exposed an
-// issue where we were generating a bitcast instead of an addrspacecast.
+// Make sure we emit the proper addrspacecast for llvm.used iff necessary.
+// PR22383 exposed an issue where we were generating a bitcast instead of an
+// addrspacecast.
 
 // CHECK: @llvm.compiler.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @a to ptr)], section "llvm.metadata"
+// NOASCAST: @llvm.compiler.used = appending addrspace(1) global [1 x ptr addrspace(1)] [ptr addrspace(1) @a], section "llvm.metadata"
 __attribute__((device)) __attribute__((__used__)) int a[] = {};
diff --git a/clang/test/CodeGenCXX/attr-retain.cpp b/clang/test/CodeGenCXX/attr-retain.cpp
index 3a56576d81632..73c649ae972bf 100644
--- a/clang/test/CodeGenCXX/attr-retain.cpp
+++ b/clang/test/CodeGenCXX/attr-retain.cpp
@@ -1,6 +1,8 @@
 // RUN: %clang_cc1 -emit-llvm -triple x86_64 -Werror %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -triple amdgcn-amd-amdhsa -Werror %s -o - | FileCheck %s --check-prefix=GLOBALAS
 
 // CHECK:      @llvm.used = appending global [7 x ptr]
+// GLOBALAS:   @llvm.used = appending addrspace(1) global [7 x ptr addrspace(1)]
 // CHECK-SAME:   @_ZN2X0C2Ev
 // CHECK-SAME:   @_ZN2X0C1Ev
 // CHECK-SAME:   @_ZN2X0D2Ev
diff --git a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
index 46c4c4d391769..04d29cf1cea51 100644
--- a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
+++ b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
@@ -1,6 +1,8 @@
 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s
 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s
 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -fdeclspec -DSELECTANY -o - | FileCheck --check-prefix=ELF-SELECTANY %s
+// RUN: %clang_cc1 %s -std=c++1y -triple=amdgcn-amd-amdhsa -emit-llvm -o - | FileCheck --check-prefix=GLOBALAS-ELF %s
+// RUN: %clang_cc1 %s -std=c++1y -triple=amdgcn-amd-amdhsa -emit-llvm -fdeclspec -DSELECTANY -o - | FileCheck --check-prefix=GLOBALAS-ELF-SELECTANY %s
 
 #ifdef SELECTANY
 struct S {
@@ -12,6 +14,7 @@ int f();
 
 // ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
 // ELF-SELECTANY: @llvm.used = appending global [1 x ptr] [ptr @selectany]
+// GLOBALAS-ELF-SELECTANY: @llvm.used = appending addrspace(1) global [1 x ptr addrspace(1)] [ptr addrspace(1) @selectany]
 int __declspec(selectany) selectany = f();
 
 #else
@@ -73,12 +76,13 @@ template<> int A<bool>::a = 10;
 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered22:[^,]*]], ptr null },
 
 // ELF:  { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr @_ZN4Fib2ILi5EE1aE },
-// MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr null }, 
+// MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr null },
 
 // ALL:  { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp, ptr null }]
 
 /// llvm.used ensures SHT_INIT_ARRAY in a section group cannot be GCed.
 // ELF: @llvm.used = appending global [14 x ptr] [ptr @_ZN1AIsE1aE, ptr @_Z1xIsE, ptr @_ZN2ns1aIiE1iE, ptr @_ZN2ns1b1iIiEE, ptr @_ZN1AIvE1aE, ptr @_Z1xIcE, ptr @_ZN3FibILi5EE1aE, ptr @_ZN3FibILi3EE1aE, ptr @_ZN3FibILi2EE1aE, ptr @_ZN3FibILi4EE1aE, ptr @_ZN4Fib2ILi5EE1aE, ptr @_ZN4Fib2ILi4EE1aE, ptr @_ZN4Fib2ILi2EE1aE, ptr @_ZN4Fib2ILi3EE1aE]
+// GLOBALAS-ELF: @llvm.used = appending addrspace(1) global [14 x ptr addrspace(1)] [ptr addrspace(1) @_ZN1AIsE1aE, ptr addrspace(1) @_Z1xIsE, ptr addrspace(1) @_ZN2ns1aIiE1iE, ptr addrspace(1) @_ZN2ns1b1iIiEE, ptr addrspace(1) @_ZN1AIvE1aE, ptr addrspace(1) @_Z1xIcE, ptr addrspace(1) @_ZN3FibILi5EE1aE, ptr addrspace(1) @_ZN3FibILi3EE1aE, ptr addrspace(1) @_ZN3FibILi2EE1aE, ptr addrspace(1) @_ZN3FibILi4EE1aE, ptr addrspace(1) @_ZN4Fib2ILi5EE1aE, ptr addrspace(1) @_ZN4Fib2ILi4EE1aE, ptr addrspace(1) @_ZN4Fib2ILi2EE1aE, ptr addrspace(1) @_ZN4Fib2ILi3EE1aE], section "llvm.metadata"
 
 template int A<short>::a;  // Unordered
 int b = foo();
diff --git a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
index 80a5067b357a2..cc9dd56615dc6 100644
--- a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
+++ b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
@@ -21,8 +21,8 @@ S A;
 
 #endif
 //.
-// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_amdgcn_target_global_constructor.cpp, ptr null }]
-// CHECK: @llvm.global_dtors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__dtor_A, ptr null }]
+// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @_GLOBAL__sub_I_amdgcn_target_global_constructor.cpp, ptr addrspace(1) null }]
+// CHECK: @llvm.global_dtors = appending global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @__dtor_A, ptr addrspace(1) null }]
 //.
 // CHECK-LABEL: define {{[^@]+}}@__cxx_global_var_init
 // CHECK-SAME: () #[[ATTR0:[0-9]+]] {

>From 90cd705b63cbdf1939b61b07a11d94abbcdea3ec Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Tue, 28 May 2024 20:30:00 +0100
Subject: [PATCH 2/9] Fix formatting.

---
 clang/lib/CodeGen/CodeGenModule.cpp | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 30a18b1c5e7a8..397c611b3ea33 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2048,8 +2048,8 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
       TheModule.getDataLayout().getProgramAddressSpace());
 
   // Get the type of a ctor entry, { i32, program void ()*, global i8* }.
-  llvm::StructType *CtorStructTy = llvm::StructType::get(
-      Int32Ty, CtorPFTy, GlobalsInt8PtrTy);
+  llvm::StructType *CtorStructTy =
+      llvm::StructType::get(Int32Ty, CtorPFTy, GlobalsInt8PtrTy);
 
   // Construct the constructor and destructor arrays.
   ConstantInitBuilder builder(*this);
@@ -2068,7 +2068,7 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
   auto list =
     ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
                                 /*constant*/ false,
-                                llvm::GlobalValue::AppendingLinkage);
+                                llvm::GlobalValue::AppendingLinkage, );
 
   // The LTO linker doesn't seem to like it when we set an alignment
   // on appending variables.  Take it off as a workaround.
@@ -2926,15 +2926,14 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   SmallVector<llvm::Constant*, 8> UsedArray;
   UsedArray.resize(List.size());
   for (unsigned i = 0, e = List.size(); i != e; ++i) {
-    UsedArray[i] =
-        llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
-            cast<llvm::Constant>(&*List[i]), CGM.GlobalsInt8PtrTy);
+    UsedArray[i] = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+        cast<llvm::Constant>(&*List[i]), CGM.GlobalsInt8PtrTy);
   }
 
   if (UsedArray.empty())
     return;
-  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.GlobalsInt8PtrTy,
-                                              UsedArray.size());
+  llvm::ArrayType *ATy =
+      llvm::ArrayType::get(CGM.GlobalsInt8PtrTy, UsedArray.size());
 
   auto *GV = new llvm::GlobalVariable(
       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,

>From 780f541ed427ad847ec80dd5dd6cc602ef9a01da Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Tue, 28 May 2024 20:44:53 +0100
Subject: [PATCH 3/9] Delete accidental noise.

---
 clang/lib/CodeGen/CodeGenModule.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 397c611b3ea33..4346ea7b1b6ad 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2068,7 +2068,7 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
   auto list =
     ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
                                 /*constant*/ false,
-                                llvm::GlobalValue::AppendingLinkage, );
+                                llvm::GlobalValue::AppendingLinkage);
 
   // The LTO linker doesn't seem to like it when we set an alignment
   // on appending variables.  Take it off as a workaround.

>From e5b604f2b839705d951cd3739210571c70653970 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Thu, 30 May 2024 15:28:42 +0100
Subject: [PATCH 4/9] Remove `used`/`compiler.used` related changes.

---
 clang/lib/CodeGen/CodeGenModule.cpp                        | 5 ++---
 clang/test/CodeGen/2005-12-04-AttributeUsed.c              | 2 --
 clang/test/CodeGen/asan-constructor.c                      | 1 +
 clang/test/CodeGen/attr-retain.c                           | 3 ---
 clang/test/CodeGenCUDA/llvm-used.cu                        | 7 ++-----
 clang/test/CodeGenCXX/attr-retain.cpp                      | 2 --
 .../static-member-variable-explicit-specialization.cpp     | 4 ++--
 7 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 473a8a251a58f..fc08932d9ae7a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2927,13 +2927,12 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   UsedArray.resize(List.size());
   for (unsigned i = 0, e = List.size(); i != e; ++i) {
     UsedArray[i] = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
-        cast<llvm::Constant>(&*List[i]), CGM.GlobalsInt8PtrTy);
+        cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
   }
 
   if (UsedArray.empty())
     return;
-  llvm::ArrayType *ATy =
-      llvm::ArrayType::get(CGM.GlobalsInt8PtrTy, UsedArray.size());
+  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
 
   auto *GV = new llvm::GlobalVariable(
       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
diff --git a/clang/test/CodeGen/2005-12-04-AttributeUsed.c b/clang/test/CodeGen/2005-12-04-AttributeUsed.c
index c5089d9bda2c6..bd232831fe1a6 100644
--- a/clang/test/CodeGen/2005-12-04-AttributeUsed.c
+++ b/clang/test/CodeGen/2005-12-04-AttributeUsed.c
@@ -1,8 +1,6 @@
 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
-// RUN: %clang_cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm -o - | FileCheck %s --check-prefix=GLOBALAS
 
 // CHECK: @llvm.used = appending global [2 x ptr] [ptr @foo, ptr @X], section "llvm.metadata"
-// GLOBALAS: @llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(1)] [ptr addrspace(1) addrspacecast (ptr @foo to ptr addrspace(1)), ptr addrspace(1) @X], section "llvm.metadata"
 int X __attribute__((used));
 int Y;
 
diff --git a/clang/test/CodeGen/asan-constructor.c b/clang/test/CodeGen/asan-constructor.c
index 762bf0778565d..280c550a75fb3 100644
--- a/clang/test/CodeGen/asan-constructor.c
+++ b/clang/test/CodeGen/asan-constructor.c
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - %s | FileCheck %s
 // RUN: %clang_cc1 -triple wasm32-unknown-emscripten -fsanitize=address -emit-llvm -o - %s | FileCheck %s --check-prefix=EMSCRIPTEN
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fsanitize=address -emit-llvm -o - %s | FileCheck %s
 
 // CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr null }]
 // EMSCRIPTEN: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 50, ptr @asan.module_ctor, ptr null }]
diff --git a/clang/test/CodeGen/attr-retain.c b/clang/test/CodeGen/attr-retain.c
index e86f6ae8ed06c..6a9869c00392d 100644
--- a/clang/test/CodeGen/attr-retain.c
+++ b/clang/test/CodeGen/attr-retain.c
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -emit-llvm -triple x86_64 %s -o - | FileCheck %s
-// RUN: %clang_cc1 -emit-llvm -triple amdgcn-amd-amdhsa %s -o - | FileCheck %s --check-prefix=GLOBALAS
 
 /// Set !retain regardless of the target. The backend will lower !retain to
 /// SHF_GNU_RETAIN on ELF and ignore the metadata for other binary formats.
@@ -12,8 +11,6 @@
 
 // CHECK:      @llvm.used = appending global [8 x ptr] [ptr @c0, ptr @foo.l0, ptr @f0, ptr @f2, ptr @g0, ptr @g1, ptr @g3, ptr @g4], section "llvm.metadata"
 // CHECK:      @llvm.compiler.used = appending global [3 x ptr] [ptr @f2, ptr @g3, ptr @g4], section "llvm.metadata"
-// GLOBALAS:   @llvm.used = appending addrspace(1) global [8 x ptr addrspace(1)] [ptr addrspace(1) addrspacecast (ptr addrspace(4) @c0 to ptr addrspace(1)), ptr addrspace(1) @foo.l0, ptr addrspace(1) addrspacecast (ptr @f0 to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr @f2 to ptr addrspace(1)), ptr addrspace(1) @g0, ptr addrspace(1) @g1, ptr addrspace(1) @g3, ptr addrspace(1) @g4], section "llvm.metadata"
-// GLOBALAS:   @llvm.compiler.used = appending addrspace(1) global [3 x ptr addrspace(1)] [ptr addrspace(1) addrspacecast (ptr @f2 to ptr addrspace(1)), ptr addrspace(1) @g3, ptr addrspace(1) @g4], section "llvm.metadata"
 const int c0 __attribute__((retain)) = 42;
 
 void foo(void) {
diff --git a/clang/test/CodeGenCUDA/llvm-used.cu b/clang/test/CodeGenCUDA/llvm-used.cu
index 7782048b52836..9f22dabde3c2d 100644
--- a/clang/test/CodeGenCUDA/llvm-used.cu
+++ b/clang/test/CodeGenCUDA/llvm-used.cu
@@ -1,11 +1,8 @@
 // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx64-unknown-unknown | FileCheck %s
-// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple amdgcn-amd-amdhsa | FileCheck %s --check-prefix=NOASCAST
 
 
-// Make sure we emit the proper addrspacecast for llvm.used iff necessary.
-// PR22383 exposed an issue where we were generating a bitcast instead of an
-// addrspacecast.
+// Make sure we emit the proper addrspacecast for llvm.used. PR22383 exposed an
+// issue where we were generating a bitcast instead of an addrspacecast.
 
 // CHECK: @llvm.compiler.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @a to ptr)], section "llvm.metadata"
-// NOASCAST: @llvm.compiler.used = appending addrspace(1) global [1 x ptr addrspace(1)] [ptr addrspace(1) @a], section "llvm.metadata"
 __attribute__((device)) __attribute__((__used__)) int a[] = {};
diff --git a/clang/test/CodeGenCXX/attr-retain.cpp b/clang/test/CodeGenCXX/attr-retain.cpp
index 73c649ae972bf..3a56576d81632 100644
--- a/clang/test/CodeGenCXX/attr-retain.cpp
+++ b/clang/test/CodeGenCXX/attr-retain.cpp
@@ -1,8 +1,6 @@
 // RUN: %clang_cc1 -emit-llvm -triple x86_64 -Werror %s -o - | FileCheck %s
-// RUN: %clang_cc1 -emit-llvm -triple amdgcn-amd-amdhsa -Werror %s -o - | FileCheck %s --check-prefix=GLOBALAS
 
 // CHECK:      @llvm.used = appending global [7 x ptr]
-// GLOBALAS:   @llvm.used = appending addrspace(1) global [7 x ptr addrspace(1)]
 // CHECK-SAME:   @_ZN2X0C2Ev
 // CHECK-SAME:   @_ZN2X0C1Ev
 // CHECK-SAME:   @_ZN2X0D2Ev
diff --git a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
index 04d29cf1cea51..839e528bf2a9f 100644
--- a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
+++ b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
@@ -14,7 +14,7 @@ int f();
 
 // ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
 // ELF-SELECTANY: @llvm.used = appending global [1 x ptr] [ptr @selectany]
-// GLOBALAS-ELF-SELECTANY: @llvm.used = appending addrspace(1) global [1 x ptr addrspace(1)] [ptr addrspace(1) @selectany]
+// GLOBALAS-ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
 int __declspec(selectany) selectany = f();
 
 #else
@@ -33,6 +33,7 @@ template<> int A<char>::a;
 template<> int A<bool>::a = 10;
 
 // ALL: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }]
+// GLOBALAS-ELF: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }]
 
 // ELF:  [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr @_ZN1AIsE1aE },
 // MACHO: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr null },
@@ -82,7 +83,6 @@ template<> int A<bool>::a = 10;
 
 /// llvm.used ensures SHT_INIT_ARRAY in a section group cannot be GCed.
 // ELF: @llvm.used = appending global [14 x ptr] [ptr @_ZN1AIsE1aE, ptr @_Z1xIsE, ptr @_ZN2ns1aIiE1iE, ptr @_ZN2ns1b1iIiEE, ptr @_ZN1AIvE1aE, ptr @_Z1xIcE, ptr @_ZN3FibILi5EE1aE, ptr @_ZN3FibILi3EE1aE, ptr @_ZN3FibILi2EE1aE, ptr @_ZN3FibILi4EE1aE, ptr @_ZN4Fib2ILi5EE1aE, ptr @_ZN4Fib2ILi4EE1aE, ptr @_ZN4Fib2ILi2EE1aE, ptr @_ZN4Fib2ILi3EE1aE]
-// GLOBALAS-ELF: @llvm.used = appending addrspace(1) global [14 x ptr addrspace(1)] [ptr addrspace(1) @_ZN1AIsE1aE, ptr addrspace(1) @_Z1xIsE, ptr addrspace(1) @_ZN2ns1aIiE1iE, ptr addrspace(1) @_ZN2ns1b1iIiEE, ptr addrspace(1) @_ZN1AIvE1aE, ptr addrspace(1) @_Z1xIcE, ptr addrspace(1) @_ZN3FibILi5EE1aE, ptr addrspace(1) @_ZN3FibILi3EE1aE, ptr addrspace(1) @_ZN3FibILi2EE1aE, ptr addrspace(1) @_ZN3FibILi4EE1aE, ptr addrspace(1) @_ZN4Fib2ILi5EE1aE, ptr addrspace(1) @_ZN4Fib2ILi4EE1aE, ptr addrspace(1) @_ZN4Fib2ILi2EE1aE, ptr addrspace(1) @_ZN4Fib2ILi3EE1aE], section "llvm.metadata"
 
 template int A<short>::a;  // Unordered
 int b = foo();

>From 2aa32c55c1cd16c7d1213fb8a47b3ee61a534408 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Thu, 30 May 2024 15:30:10 +0100
Subject: [PATCH 5/9] Revert unrelated formatting change.

---
 clang/lib/CodeGen/CodeGenModule.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index fc08932d9ae7a..a22f1eb2b8065 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2926,8 +2926,8 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   SmallVector<llvm::Constant*, 8> UsedArray;
   UsedArray.resize(List.size());
   for (unsigned i = 0, e = List.size(); i != e; ++i) {
-    UsedArray[i] = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
-        cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
+      llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+          cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
   }
 
   if (UsedArray.empty())

>From 7985d43a97251b28ae092f1a90bbf5a726836096 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Thu, 30 May 2024 15:38:21 +0100
Subject: [PATCH 6/9] Fix typo.

---
 clang/lib/CodeGen/CodeGenModule.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index a22f1eb2b8065..c45c5cab9f48c 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2926,8 +2926,9 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   SmallVector<llvm::Constant*, 8> UsedArray;
   UsedArray.resize(List.size());
   for (unsigned i = 0, e = List.size(); i != e; ++i) {
-      llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
-          cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
+    UsedArray[i] =
+        llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+            cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
   }
 
   if (UsedArray.empty())

>From 17e41275180ab8f5f1dec7d872e53efa0a40c8eb Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Fri, 31 May 2024 02:59:41 +0100
Subject: [PATCH 7/9] Fix the AS for the `global_*tors` arrays.

---
 clang/lib/CodeGen/CodeGenModule.cpp                 |  3 ++-
 clang/test/CodeGen/asan-constructor.c               |  3 ++-
 .../cxx0x-initializer-stdinitializerlist.cpp        |  3 ++-
 ...atic-member-variable-explicit-specialization.cpp |  4 ++--
 .../OpenMP/amdgcn_target_global_constructor.cpp     |  4 ++--
 llvm/lib/Transforms/Utils/ModuleUtils.cpp           | 13 ++++++-------
 .../llvm-reduce/remove-ifunc-program-addrspace.ll   |  4 ++--
 7 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index c45c5cab9f48c..b59c5c52783e9 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2068,7 +2068,8 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
   auto list =
     ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
                                 /*constant*/ false,
-                                llvm::GlobalValue::AppendingLinkage);
+                                llvm::GlobalValue::AppendingLinkage,
+                                GlobalsInt8PtrTy->getAddressSpace());
 
   // The LTO linker doesn't seem to like it when we set an alignment
   // on appending variables.  Take it off as a workaround.
diff --git a/clang/test/CodeGen/asan-constructor.c b/clang/test/CodeGen/asan-constructor.c
index 280c550a75fb3..41212d45a5c63 100644
--- a/clang/test/CodeGen/asan-constructor.c
+++ b/clang/test/CodeGen/asan-constructor.c
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - %s | FileCheck %s
 // RUN: %clang_cc1 -triple wasm32-unknown-emscripten -fsanitize=address -emit-llvm -o - %s | FileCheck %s --check-prefix=EMSCRIPTEN
-// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fsanitize=address -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fsanitize=address -emit-llvm -o - %s | FileCheck %s --check-prefix=GLOBALAS
 
 // CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr null }]
 // EMSCRIPTEN: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 50, ptr @asan.module_ctor, ptr null }]
+// GLOBALAS: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 1, ptr @asan.module_ctor, ptr addrspace(1) null }]
diff --git a/clang/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp b/clang/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp
index aa2f078a5fb0c..2e22192e8b388 100644
--- a/clang/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp
+++ b/clang/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp
@@ -90,7 +90,8 @@ std::initializer_list<int> thread_local x = {1, 2, 3, 4};
 // AMDGCN: @[[REFTMP1:.*]] = private addrspace(4) constant [2 x i32] [i32 42, i32 43], align 4
 // AMDGCN: @[[REFTMP2:.*]] = private addrspace(4) constant [3 x %{{.*}}] [%{{.*}} { i32 1 }, %{{.*}} { i32 2 }, %{{.*}} { i32 3 }], align 4
 
-// CHECK: appending global
+// X86: appending global
+// AMDGCN: appending addrspace(1) global
 
 // thread_local initializer:
 // X86-LABEL: define internal void @__cxx_global_var_init
diff --git a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
index 839e528bf2a9f..58055130cd708 100644
--- a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
+++ b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
@@ -14,7 +14,7 @@ int f();
 
 // ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
 // ELF-SELECTANY: @llvm.used = appending global [1 x ptr] [ptr @selectany]
-// GLOBALAS-ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
+// GLOBALAS-ELF-SELECTANY: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @__cxx_global_var_init, ptr addrspace(1) @selectany }]
 int __declspec(selectany) selectany = f();
 
 #else
@@ -33,7 +33,7 @@ template<> int A<char>::a;
 template<> int A<bool>::a = 10;
 
 // ALL: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }]
-// GLOBALAS-ELF: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }]
+// GLOBALAS-ELF: @llvm.global_ctors = appending addrspace(1) global [16 x { i32, ptr, ptr addrspace(1) }]
 
 // ELF:  [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr @_ZN1AIsE1aE },
 // MACHO: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr null },
diff --git a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
index cc9dd56615dc6..fe414b59588c0 100644
--- a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
+++ b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
@@ -21,8 +21,8 @@ S A;
 
 #endif
 //.
-// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @_GLOBAL__sub_I_amdgcn_target_global_constructor.cpp, ptr addrspace(1) null }]
-// CHECK: @llvm.global_dtors = appending global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @__dtor_A, ptr addrspace(1) null }]
+// CHECK: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @_GLOBAL__sub_I_amdgcn_target_global_constructor.cpp, ptr addrspace(1) null }]
+// CHECK: @llvm.global_dtors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @__dtor_A, ptr addrspace(1) null }]
 //.
 // CHECK-LABEL: define {{[^@]+}}@__cxx_global_var_init
 // CHECK-SAME: () #[[ATTR0:[0-9]+]] {
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index 122279160cc7e..7151cc869bc95 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -30,6 +30,8 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
                                 int Priority, Constant *Data) {
   IRBuilder<> IRB(M.getContext());
   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
+  PointerType *GlobalsInt8PtrTy = PointerType::get(
+      M.getContext(), M.getDataLayout().getDefaultGlobalsAddressSpace());
 
   // Get the current set of static global constructors and add the new ctor
   // to the list.
@@ -47,15 +49,15 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
   } else {
     EltTy = StructType::get(IRB.getInt32Ty(),
                             PointerType::get(FnTy, F->getAddressSpace()),
-                            IRB.getPtrTy());
+                            GlobalsInt8PtrTy);
   }
 
   // Build a 3 field global_ctor entry.  We don't take a comdat key.
   Constant *CSVals[3];
   CSVals[0] = IRB.getInt32(Priority);
   CSVals[1] = F;
-  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getPtrTy())
-                   : Constant::getNullValue(IRB.getPtrTy());
+  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, GlobalsInt8PtrTy)
+                   : Constant::getNullValue(GlobalsInt8PtrTy);
   Constant *RuntimeCtorInit =
       ConstantStruct::get(EltTy, ArrayRef(CSVals, EltTy->getNumElements()));
 
@@ -437,12 +439,9 @@ bool llvm::lowerGlobalIFuncUsersAsGlobalCtor(
 
   InitBuilder.CreateRetVoid();
 
-  PointerType *ConstantDataTy = PointerType::get(Ctx, 0);
-
   // TODO: Is this the right priority? Probably should be before any other
   // constructors?
   const int Priority = 10;
-  appendToGlobalCtors(M, NewCtor, Priority,
-                      ConstantPointerNull::get(ConstantDataTy));
+  appendToGlobalCtors(M, NewCtor, Priority, nullptr);
   return UnhandledUsers;
 }
diff --git a/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll b/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll
index e275d61764b21..7ddab0b6a815e 100644
--- a/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll
+++ b/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll
@@ -15,8 +15,8 @@ define void @existing_ctor() addrspace(1) {
 
 ; CHECK-FINAL: [[TABLE:@[0-9]+]] = internal addrspace(2) global [6 x ptr addrspace(1)] poison, align 8
 
-; CHECK-FINAL: @llvm.global_ctors = appending addrspace(2) global [2 x { i32, ptr addrspace(1), ptr }] [{ i32, ptr addrspace(1), ptr } { i32 0, ptr addrspace(1) @existing_ctor, ptr null }, { i32, ptr addrspace(1), ptr } { i32 10, ptr addrspace(1) [[TABLE_CTOR:@[0-9]+]], ptr null }]
- at llvm.global_ctors = appending global [1 x { i32, ptr addrspace(1), ptr }] [{ i32, ptr addrspace(1), ptr } { i32 0, ptr addrspace(1) @existing_ctor, ptr null }]
+; CHECK-FINAL: @llvm.global_ctors = appending addrspace(2) global [2 x { i32, ptr addrspace(1), ptr addrspace(2) }] [{ i32, ptr addrspace(1), ptr addrspace(2) } { i32 0, ptr addrspace(1) @existing_ctor, ptr addrspace(2) null }, { i32, ptr addrspace(1), ptr addrspace(2) } { i32 10, ptr addrspace(1) [[TABLE_CTOR:@[0-9]+]], ptr addrspace(2) null }]
+ at llvm.global_ctors = appending global [1 x { i32, ptr addrspace(1), ptr addrspace(2) }] [{ i32, ptr addrspace(1), ptr addrspace(2) } { i32 0, ptr addrspace(1) @existing_ctor, ptr addrspace(2) null }]
 
 
 

>From d3136cdbf1a71dcb8fe7236e007d2fb9a8a1575a Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Fri, 31 May 2024 03:26:31 +0100
Subject: [PATCH 8/9] Fix formatting.

---
 clang/lib/CodeGen/CodeGenModule.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index b59c5c52783e9..c12a2f4719c5e 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2065,11 +2065,10 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
     ctor.finishAndAddTo(ctors);
   }
 
-  auto list =
-    ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
-                                /*constant*/ false,
-                                llvm::GlobalValue::AppendingLinkage,
-                                GlobalsInt8PtrTy->getAddressSpace());
+  auto list = ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
+                                          /*constant*/ false,
+                                          llvm::GlobalValue::AppendingLinkage,
+                                          GlobalsInt8PtrTy->getAddressSpace());
 
   // The LTO linker doesn't seem to like it when we set an alignment
   // on appending variables.  Take it off as a workaround.

>From 1f42f8264f8f7b0a394e2362f8f59da55eb37637 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 3 Jun 2024 15:35:24 +0100
Subject: [PATCH 9/9] Switch to unconditional Unqual use.

---
 clang/lib/CodeGen/CodeGenModule.cpp           | 17 +++++--
 clang/test/CodeGen/asan-constructor.c         |  2 +-
 clang/test/CodeGen/attr-retain.c              |  1 +
 clang/test/CodeGenCUDA/llvm-used.cu           |  2 +-
 ...ember-variable-explicit-specialization.cpp |  4 +-
 .../amdgcn_target_global_constructor.cpp      |  4 +-
 llvm/docs/LangRef.rst                         | 48 +++++++++++--------
 llvm/lib/Transforms/Utils/ModuleUtils.cpp     | 11 +++--
 .../remove-ifunc-program-addrspace.ll         |  4 +-
 9 files changed, 54 insertions(+), 39 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index c12a2f4719c5e..34f32af968b12 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2046,10 +2046,16 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
   llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
   llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
       TheModule.getDataLayout().getProgramAddressSpace());
-
-  // Get the type of a ctor entry, { i32, program void ()*, global i8* }.
+  llvm::PointerType *AssocDataPtrTy =
+      llvm::PointerType::getUnqual(getLLVMContext());
+
+  // Get the type of a ctor entry, { i32, program void ()*, i8* }.
+  // Note that we unconditionally emit an unqualified pointer to the associated
+  // data - this is intentional as this is a fake global, serving only as a
+  // lifetime extension hook; this must match the type we use in the llvm.used
+  // and llvm.compiler.used arrays.
   llvm::StructType *CtorStructTy =
-      llvm::StructType::get(Int32Ty, CtorPFTy, GlobalsInt8PtrTy);
+      llvm::StructType::get(Int32Ty, CtorPFTy, AssocDataPtrTy);
 
   // Construct the constructor and destructor arrays.
   ConstantInitBuilder builder(*this);
@@ -2059,9 +2065,10 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
     ctor.addInt(Int32Ty, I.Priority);
     ctor.add(I.Initializer);
     if (I.AssociatedData)
-      ctor.add(I.AssociatedData);
+      ctor.add(llvm::ConstantExpr::getPointerCast(I.AssociatedData,
+                                                  AssocDataPtrTy));
     else
-      ctor.addNullPointer(GlobalsInt8PtrTy);
+      ctor.addNullPointer(AssocDataPtrTy);
     ctor.finishAndAddTo(ctors);
   }
 
diff --git a/clang/test/CodeGen/asan-constructor.c b/clang/test/CodeGen/asan-constructor.c
index 41212d45a5c63..85bd59a4c75b2 100644
--- a/clang/test/CodeGen/asan-constructor.c
+++ b/clang/test/CodeGen/asan-constructor.c
@@ -4,4 +4,4 @@
 
 // CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr null }]
 // EMSCRIPTEN: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 50, ptr @asan.module_ctor, ptr null }]
-// GLOBALAS: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 1, ptr @asan.module_ctor, ptr addrspace(1) null }]
+// GLOBALAS: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr null }]
diff --git a/clang/test/CodeGen/attr-retain.c b/clang/test/CodeGen/attr-retain.c
index 6a9869c00392d..fc41be76f9d0a 100644
--- a/clang/test/CodeGen/attr-retain.c
+++ b/clang/test/CodeGen/attr-retain.c
@@ -11,6 +11,7 @@
 
 // CHECK:      @llvm.used = appending global [8 x ptr] [ptr @c0, ptr @foo.l0, ptr @f0, ptr @f2, ptr @g0, ptr @g1, ptr @g3, ptr @g4], section "llvm.metadata"
 // CHECK:      @llvm.compiler.used = appending global [3 x ptr] [ptr @f2, ptr @g3, ptr @g4], section "llvm.metadata"
+
 const int c0 __attribute__((retain)) = 42;
 
 void foo(void) {
diff --git a/clang/test/CodeGenCUDA/llvm-used.cu b/clang/test/CodeGenCUDA/llvm-used.cu
index 9f22dabde3c2d..c39111dd48036 100644
--- a/clang/test/CodeGenCUDA/llvm-used.cu
+++ b/clang/test/CodeGenCUDA/llvm-used.cu
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx64-unknown-unknown | FileCheck %s
 
 
-// Make sure we emit the proper addrspacecast for llvm.used. PR22383 exposed an
+// Make sure we emit the proper addrspacecast for llvm.used.  PR22383 exposed an
 // issue where we were generating a bitcast instead of an addrspacecast.
 
 // CHECK: @llvm.compiler.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @a to ptr)], section "llvm.metadata"
diff --git a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
index 58055130cd708..2753aaf9f5134 100644
--- a/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
+++ b/clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
@@ -14,7 +14,7 @@ int f();
 
 // ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
 // ELF-SELECTANY: @llvm.used = appending global [1 x ptr] [ptr @selectany]
-// GLOBALAS-ELF-SELECTANY: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @__cxx_global_var_init, ptr addrspace(1) @selectany }]
+// GLOBALAS-ELF-SELECTANY: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr addrspacecast (ptr addrspace(1) @selectany to ptr) }]
 int __declspec(selectany) selectany = f();
 
 #else
@@ -33,7 +33,7 @@ template<> int A<char>::a;
 template<> int A<bool>::a = 10;
 
 // ALL: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }]
-// GLOBALAS-ELF: @llvm.global_ctors = appending addrspace(1) global [16 x { i32, ptr, ptr addrspace(1) }]
+// GLOBALAS-ELF: @llvm.global_ctors = appending addrspace(1) global [16 x { i32, ptr, ptr }]
 
 // ELF:  [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr @_ZN1AIsE1aE },
 // MACHO: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr null },
diff --git a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
index fe414b59588c0..5d4dec6848970 100644
--- a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
+++ b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp
@@ -21,8 +21,8 @@ S A;
 
 #endif
 //.
-// CHECK: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @_GLOBAL__sub_I_amdgcn_target_global_constructor.cpp, ptr addrspace(1) null }]
-// CHECK: @llvm.global_dtors = appending addrspace(1) global [1 x { i32, ptr, ptr addrspace(1) }] [{ i32, ptr, ptr addrspace(1) } { i32 65535, ptr @__dtor_A, ptr addrspace(1) null }]
+// CHECK: @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_amdgcn_target_global_constructor.cpp, ptr null }]
+// CHECK: @llvm.global_dtors = appending addrspace(1) global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__dtor_A, ptr null }]
 //.
 // CHECK-LABEL: define {{[^@]+}}@__cxx_global_var_init
 // CHECK-SAME: () #[[ATTR0:[0-9]+]] {
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index c58f7f7140e47..816f5a7af0de0 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -354,7 +354,7 @@ added in the future:
     not be used lightly but only for specific situations such as an
     alternative to the *register pinning* performance technique often
     used when implementing functional programming languages. At the
-    moment only X86, AArch64, and RISCV support this convention. The 
+    moment only X86, AArch64, and RISCV support this convention. The
     following limitations exist:
 
     -  On *X86-32* only up to 4 bit type parameters are supported. No
@@ -685,10 +685,10 @@ implementation defined, the optimizer can't do the latter.  The former is
 challenging as many commonly expected properties, such as
 ``ptrtoint(v)-ptrtoint(v) == 0``, don't hold for non-integral types.
 Similar restrictions apply to intrinsics that might examine the pointer bits,
-such as :ref:`llvm.ptrmask<int_ptrmask>`. 
+such as :ref:`llvm.ptrmask<int_ptrmask>`.
 
 The alignment information provided by the frontend for a non-integral pointer
-(typically using attributes or metadata) must be valid for every possible 
+(typically using attributes or metadata) must be valid for every possible
 representation of the pointer.
 
 .. _globalvars:
@@ -1649,10 +1649,10 @@ Currently, only the following parameter attributes are defined:
     -  Both ``a`` and ``b`` are constants.
     -  The range is allowed to wrap.
     -  The range should not represent the full or empty set. That is, ``a!=b``.
-    
-    This attribute may only be applied to parameters or return values with integer 
+
+    This attribute may only be applied to parameters or return values with integer
     or vector of integer types.
-    
+
     For vector-typed parameters, the range is applied element-wise.
 
 .. _gc:
@@ -8699,7 +8699,10 @@ functions with the same priority is not defined.
 
 If the third field is non-null, and points to a global variable
 or function, the initializer function will only run if the associated
-data from the current module is not discarded.
+data from the current module is not discarded. An unqualified pointer is used
+unconditionally to underline the ephemeral nature of this field - it is
+essentially a lifetime hook meant for internal compiler usage, just like the
+elements of the ``@llvm.used`` and ``@llvm.compiler.used`` arrays.
 On ELF the referenced global variable or function must be in a comdat.
 
 .. _llvmglobaldtors:
@@ -8720,7 +8723,10 @@ order of functions with the same priority is not defined.
 
 If the third field is non-null, and points to a global variable
 or function, the destructor function will only run if the associated
-data from the current module is not discarded.
+data from the current module is not discarded. An unqualified pointer is used
+unconditionally to underline the ephemeral nature of this field - it is
+essentially a lifetime hook meant for internal compiler usage, just like the
+elements of the ``@llvm.used`` and ``@llvm.compiler.used`` arrays.
 On ELF the referenced global variable or function must be in a comdat.
 
 Instruction Reference
@@ -14254,7 +14260,7 @@ Arguments:
 """"""""""
 The first 4 arguments are similar to ``llvm.instrprof.increment``. The indexing
 is specific to callsites, meaning callsites are indexed from 0, independent from
-the indexes used by the other intrinsics (such as 
+the indexes used by the other intrinsics (such as
 ``llvm.instrprof.increment[.step]``).
 
 The last argument is the called value of the callsite this intrinsic precedes.
@@ -14268,7 +14274,7 @@ a buffer LLVM can use to perform counter increments (i.e. the lowering of
 ``llvm.instrprof.increment[.step]``. The address range following the counter
 buffer, ``<num-counters>`` x ``sizeof(ptr)`` - sized, is expected to contain
 pointers to contexts of functions called from this function ("subcontexts").
-LLVM does not dereference into that memory region, just calculates GEPs. 
+LLVM does not dereference into that memory region, just calculates GEPs.
 
 The lowering of ``llvm.instrprof.callsite`` consists of:
 
@@ -14888,8 +14894,8 @@ integer bit width or any vector of integer elements.
 Overview:
 """""""""
 
-Return ``-1`` if ``%a`` is signed less than ``%b``, ``0`` if they are equal, and 
-``1`` if ``%a`` is signed greater than ``%b``. Vector intrinsics operate on a per-element basis. 
+Return ``-1`` if ``%a`` is signed less than ``%b``, ``0`` if they are equal, and
+``1`` if ``%a`` is signed greater than ``%b``. Vector intrinsics operate on a per-element basis.
 
 Arguments:
 """"""""""
@@ -14917,8 +14923,8 @@ integer bit width or any vector of integer elements.
 Overview:
 """""""""
 
-Return ``-1`` if ``%a`` is unsigned less than ``%b``, ``0`` if they are equal, and 
-``1`` if ``%a`` is unsigned greater than ``%b``. Vector intrinsics operate on a per-element basis. 
+Return ``-1`` if ``%a`` is unsigned less than ``%b``, ``0`` if they are equal, and
+``1`` if ``%a`` is unsigned greater than ``%b``. Vector intrinsics operate on a per-element basis.
 
 Arguments:
 """"""""""
@@ -20980,9 +20986,9 @@ Semantics:
 """"""""""
 
 The '``llvm.vp.minimum``' intrinsic performs floating-point minimum (:ref:`minimum <i_minimum>`)
-of the first and second vector operand on each enabled lane, the result being 
+of the first and second vector operand on each enabled lane, the result being
 NaN if either operand is a NaN. -0.0 is considered to be less than +0.0 for this
-intrinsic. The result on disabled lanes is a :ref:`poison value <poisonvalues>`. 
+intrinsic. The result on disabled lanes is a :ref:`poison value <poisonvalues>`.
 The operation is performed in the default floating-point environment.
 
 Examples:
@@ -21030,9 +21036,9 @@ Semantics:
 """"""""""
 
 The '``llvm.vp.maximum``' intrinsic performs floating-point maximum (:ref:`maximum <i_maximum>`)
-of the first and second vector operand on each enabled lane, the result being 
+of the first and second vector operand on each enabled lane, the result being
 NaN if either operand is a NaN. -0.0 is considered to be less than +0.0 for this
-intrinsic. The result on disabled lanes is a :ref:`poison value <poisonvalues>`. 
+intrinsic. The result on disabled lanes is a :ref:`poison value <poisonvalues>`.
 The operation is performed in the default floating-point environment.
 
 Examples:
@@ -28309,7 +28315,7 @@ Semantics:
 """"""""""
 
 The intrinsic ``@llvm.allow.ubsan.check()`` returns either ``true`` or
-``false``, depending on compiler options. 
+``false``, depending on compiler options.
 
 For each evaluation of a call to this intrinsic, the program must be valid and
 correct both if it returns ``true`` and if it returns ``false``.
@@ -28368,13 +28374,13 @@ Semantics:
 """"""""""
 
 The intrinsic ``@llvm.allow.runtime.check()`` returns either ``true`` or
-``false``, depending on compiler options. 
+``false``, depending on compiler options.
 
 For each evaluation of a call to this intrinsic, the program must be valid and
 correct both if it returns ``true`` and if it returns ``false``.
 
 When used in a branch condition, it allows us to choose between
-two alternative correct solutions for the same problem. 
+two alternative correct solutions for the same problem.
 
 If the intrinsic is evaluated as ``true``, program should execute a guarded
 check. If the intrinsic is evaluated as ``false``, the program should avoid any
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index 7151cc869bc95..f17d3edbf0e81 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -30,8 +30,9 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
                                 int Priority, Constant *Data) {
   IRBuilder<> IRB(M.getContext());
   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
-  PointerType *GlobalsInt8PtrTy = PointerType::get(
-      M.getContext(), M.getDataLayout().getDefaultGlobalsAddressSpace());
+  // The pointer to associated data MUST be emitted as an unqualified ptr, see
+  // the comment in CodeGenModule::EmitCtorList().
+  PointerType *AssocDataPtrTy = PointerType::getUnqual(M.getContext());
 
   // Get the current set of static global constructors and add the new ctor
   // to the list.
@@ -49,15 +50,15 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
   } else {
     EltTy = StructType::get(IRB.getInt32Ty(),
                             PointerType::get(FnTy, F->getAddressSpace()),
-                            GlobalsInt8PtrTy);
+                            AssocDataPtrTy);
   }
 
   // Build a 3 field global_ctor entry.  We don't take a comdat key.
   Constant *CSVals[3];
   CSVals[0] = IRB.getInt32(Priority);
   CSVals[1] = F;
-  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, GlobalsInt8PtrTy)
-                   : Constant::getNullValue(GlobalsInt8PtrTy);
+  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, AssocDataPtrTy)
+                   : Constant::getNullValue(AssocDataPtrTy);
   Constant *RuntimeCtorInit =
       ConstantStruct::get(EltTy, ArrayRef(CSVals, EltTy->getNumElements()));
 
diff --git a/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll b/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll
index 7ddab0b6a815e..9573c55030242 100644
--- a/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll
+++ b/llvm/test/tools/llvm-reduce/remove-ifunc-program-addrspace.ll
@@ -15,8 +15,8 @@ define void @existing_ctor() addrspace(1) {
 
 ; CHECK-FINAL: [[TABLE:@[0-9]+]] = internal addrspace(2) global [6 x ptr addrspace(1)] poison, align 8
 
-; CHECK-FINAL: @llvm.global_ctors = appending addrspace(2) global [2 x { i32, ptr addrspace(1), ptr addrspace(2) }] [{ i32, ptr addrspace(1), ptr addrspace(2) } { i32 0, ptr addrspace(1) @existing_ctor, ptr addrspace(2) null }, { i32, ptr addrspace(1), ptr addrspace(2) } { i32 10, ptr addrspace(1) [[TABLE_CTOR:@[0-9]+]], ptr addrspace(2) null }]
- at llvm.global_ctors = appending global [1 x { i32, ptr addrspace(1), ptr addrspace(2) }] [{ i32, ptr addrspace(1), ptr addrspace(2) } { i32 0, ptr addrspace(1) @existing_ctor, ptr addrspace(2) null }]
+; CHECK-FINAL: @llvm.global_ctors = appending addrspace(2) global [2 x { i32, ptr addrspace(1), ptr }] [{ i32, ptr addrspace(1), ptr } { i32 0, ptr addrspace(1) @existing_ctor, ptr null }, { i32, ptr addrspace(1), ptr } { i32 10, ptr addrspace(1) [[TABLE_CTOR:@[0-9]+]], ptr null }]
+ at llvm.global_ctors = appending addrspace(2) global [1 x { i32, ptr addrspace(1), ptr }] [{ i32, ptr addrspace(1), ptr } { i32 0, ptr addrspace(1) @existing_ctor, ptr null }]
 
 
 



More information about the cfe-commits mailing list