Skip to content

Conversation

@jmmartinez
Copy link
Contributor

By convention the AS of the elements of llvm.compiler.used & llvm.used is 0. However, the AS of CGM.Int8PtrTy is not always 0.

This leaves some LLVM helpers (appendToUsed/appendToCompilerUsed/removeFromUsedLists) unusable.

This patch makes the AS of the elements of these variables to be 0.

This PR is related to #162660

@jmmartinez jmmartinez self-assigned this Oct 21, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Oct 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-clang-codegen

Author: Juan Manuel Martinez Caamaño (jmmartinez)

Changes

By convention the AS of the elements of llvm.compiler.used & llvm.used is 0. However, the AS of CGM.Int8PtrTy is not always 0.

This leaves some LLVM helpers (appendToUsed/appendToCompilerUsed/removeFromUsedLists) unusable.

This patch makes the AS of the elements of these variables to be 0.

This PR is related to #162660


Full diff: https://github.com/llvm/llvm-project/pull/164432.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+6-6)
  • (modified) clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c (+1-1)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 1085f45e0fc21..94e3c8bead385 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3281,18 +3281,18 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   if (List.empty())
     return;
 
+  llvm::PointerType *UnqualPtr =
+      llvm::PointerType::getUnqual(CGM.getLLVMContext());
+
   // Convert List to what ConstantArray needs.
   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);
+    UsedArray[i] = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+        cast<llvm::Constant>(&*List[i]), UnqualPtr);
   }
 
-  if (UsedArray.empty())
-    return;
-  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
+  llvm::ArrayType *ATy = llvm::ArrayType::get(UnqualPtr, UsedArray.size());
 
   auto *GV = new llvm::GlobalVariable(
       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
diff --git a/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c b/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c
index df7118859c764..8af9708a1bfb8 100644
--- a/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c
+++ b/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c
@@ -3,6 +3,6 @@
 
 // CHECK: @llvm.embedded.module = private addrspace(1) constant [0 x i8] zeroinitializer, section ".llvmbc", align 1
 // CHECK-NEXT: @llvm.cmdline = private addrspace(1) constant [{{[0-9]+}} x i8] c"{{.*}}", section ".llvmcmd", align 1
-// CHECK-NEXT: @llvm.compiler.used = appending addrspace(1) global [5 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @foo.managed to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @foo to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @__hip_cuid_ to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.embedded.module to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.cmdline to ptr addrspace(4))], section "llvm.metadata"
+// CHECK-NEXT: @llvm.compiler.used = appending addrspace(1) global [5 x ptr] [ptr addrspacecast (ptr addrspace(1) @foo.managed to ptr), ptr addrspacecast (ptr addrspace(1) @foo to ptr), ptr addrspacecast (ptr addrspace(1) @__hip_cuid_ to ptr), ptr addrspacecast (ptr addrspace(1) @llvm.embedded.module to ptr), ptr addrspacecast (ptr addrspace(1) @llvm.cmdline to ptr)], section "llvm.metadata"
 
 __attribute__((managed)) int foo = 42;

@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-clang

Author: Juan Manuel Martinez Caamaño (jmmartinez)

Changes

By convention the AS of the elements of llvm.compiler.used & llvm.used is 0. However, the AS of CGM.Int8PtrTy is not always 0.

This leaves some LLVM helpers (appendToUsed/appendToCompilerUsed/removeFromUsedLists) unusable.

This patch makes the AS of the elements of these variables to be 0.

This PR is related to #162660


Full diff: https://github.com/llvm/llvm-project/pull/164432.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+6-6)
  • (modified) clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c (+1-1)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 1085f45e0fc21..94e3c8bead385 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3281,18 +3281,18 @@ static void emitUsed(CodeGenModule &CGM, StringRef Name,
   if (List.empty())
     return;
 
+  llvm::PointerType *UnqualPtr =
+      llvm::PointerType::getUnqual(CGM.getLLVMContext());
+
   // Convert List to what ConstantArray needs.
   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);
+    UsedArray[i] = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+        cast<llvm::Constant>(&*List[i]), UnqualPtr);
   }
 
-  if (UsedArray.empty())
-    return;
-  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
+  llvm::ArrayType *ATy = llvm::ArrayType::get(UnqualPtr, UsedArray.size());
 
   auto *GV = new llvm::GlobalVariable(
       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
diff --git a/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c b/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c
index df7118859c764..8af9708a1bfb8 100644
--- a/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c
+++ b/clang/test/CodeGen/embed-bitcode-marker-with-nonzero-as.c
@@ -3,6 +3,6 @@
 
 // CHECK: @llvm.embedded.module = private addrspace(1) constant [0 x i8] zeroinitializer, section ".llvmbc", align 1
 // CHECK-NEXT: @llvm.cmdline = private addrspace(1) constant [{{[0-9]+}} x i8] c"{{.*}}", section ".llvmcmd", align 1
-// CHECK-NEXT: @llvm.compiler.used = appending addrspace(1) global [5 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @foo.managed to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @foo to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @__hip_cuid_ to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.embedded.module to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.cmdline to ptr addrspace(4))], section "llvm.metadata"
+// CHECK-NEXT: @llvm.compiler.used = appending addrspace(1) global [5 x ptr] [ptr addrspacecast (ptr addrspace(1) @foo.managed to ptr), ptr addrspacecast (ptr addrspace(1) @foo to ptr), ptr addrspacecast (ptr addrspace(1) @__hip_cuid_ to ptr), ptr addrspacecast (ptr addrspace(1) @llvm.embedded.module to ptr), ptr addrspacecast (ptr addrspace(1) @llvm.cmdline to ptr)], section "llvm.metadata"
 
 __attribute__((managed)) int foo = 42;

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a testcase that doesn't involve -fembed-bitcode?

Otherwise LGTM

@jmmartinez jmmartinez force-pushed the clang/llvm_compiler_used_as_0 branch from 889c8a1 to 184b601 Compare October 22, 2025 08:37
@jmmartinez
Copy link
Contributor Author

Can you add a testcase that doesn't involve -fembed-bitcode?

Otherwise LGTM

Thanks! I've added a test with a function and a global using __attribute__((__used__)).

@jmmartinez
Copy link
Contributor Author

@efriedma-quic is the test I've added ok ?

@jmmartinez
Copy link
Contributor Author

If there are no concerns about this patch, I'd like to land it.

@jmmartinez
Copy link
Contributor Author

If there are no complains I'll land this patch next week.

@arsenm arsenm changed the title [Clang] Make the AS of llvm.compiler.used & llvm.used elements addressspace(0) [Clang] Make the AS of llvm.compiler.used & llvm.used elements addrspace(0) Oct 31, 2025
@jmmartinez jmmartinez force-pushed the clang/llvm_compiler_used_as_0 branch from c6bf96d to 9b82500 Compare November 3, 2025 10:34
@jmmartinez jmmartinez merged commit bf2f577 into llvm:main Nov 3, 2025
10 checks passed
jmmartinez added a commit to ROCm/llvm-project that referenced this pull request Nov 3, 2025
…ace(0) (llvm#164432)

By convention the AS of the elements of `llvm.compiler.used` &
`llvm.used` is 0. However, the AS of `CGM.Int8PtrTy` is not always 0.

This leaves some LLVM helpers
(`appendToUsed/appendToCompilerUsed/removeFromUsedLists`) unusable.

This patch makes the AS of the elements of these variables to be 0.

This PR is related to llvm#162660
ronlieb added a commit to ROCm/llvm-project that referenced this pull request Nov 3, 2025
lamb-j added a commit that referenced this pull request Nov 3, 2025
…s addrspace(0)" (#166242)

Reverts #164432

Breaks Comgr tests with the following:


[2025-11-03T19:18:20.564Z] + clang -x hip --offload-arch=amdgcnspirv
-nogpulib -nogpuinc --no-gpu-bundle-output --offload-device-only -O3
/jenkins/workspace/compiler-psdb-amd-staging/repos/llvm-project/amd/comgr/test-lit/spirv-tests/spirv-to-reloc.hip
-o
/jenkins/workspace/compiler-psdb-amd-staging/repos/out/ubuntu-22.04/22.04/build/amd_comgr/test-lit/spirv-tests/Output/spirv-to-reloc.hip.tmp.spv
-fvisibility=hidden -fno-autolink -fexceptions -fcolor-diagnostics

[2025-11-03T19:18:20.564Z] InvalidModule: Invalid SPIR-V module: Casts
from private/local/global address space are allowed only to generic

[2025-11-03T19:18:20.564Z] 

[2025-11-03T19:18:20.564Z] <badref> = addrspacecast ptr addrspace(1)
@__hip_cuid_94fb83be5559070 to ptr

[2025-11-03T19:18:20.564Z] clang: error: amdgcn-link command failed with
exit code 10 (use -v to see invocation)
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Nov 3, 2025
…sed elements addrspace(0)" (#166242)

Reverts llvm/llvm-project#164432

Breaks Comgr tests with the following:

[2025-11-03T19:18:20.564Z] + clang -x hip --offload-arch=amdgcnspirv
-nogpulib -nogpuinc --no-gpu-bundle-output --offload-device-only -O3
/jenkins/workspace/compiler-psdb-amd-staging/repos/llvm-project/amd/comgr/test-lit/spirv-tests/spirv-to-reloc.hip
-o
/jenkins/workspace/compiler-psdb-amd-staging/repos/out/ubuntu-22.04/22.04/build/amd_comgr/test-lit/spirv-tests/Output/spirv-to-reloc.hip.tmp.spv
-fvisibility=hidden -fno-autolink -fexceptions -fcolor-diagnostics

[2025-11-03T19:18:20.564Z] InvalidModule: Invalid SPIR-V module: Casts
from private/local/global address space are allowed only to generic

[2025-11-03T19:18:20.564Z]

[2025-11-03T19:18:20.564Z] <badref> = addrspacecast ptr addrspace(1)
@__hip_cuid_94fb83be5559070 to ptr

[2025-11-03T19:18:20.564Z] clang: error: amdgcn-link command failed with
exit code 10 (use -v to see invocation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants