[clang] df98779 - [Clang][CodeGen] Handle TLS variables for non-zero Global ASes (#238159)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 04:48:35 PST 2026
Author: Alex Voicu
Date: 2026-11-04T12:48:30Z
New Revision: df987794f9d7788e4cd06bf582a323b3c5ad7ad1
URL: https://github.com/llvm/llvm-project/commit/df987794f9d7788e4cd06bf582a323b3c5ad7ad1
DIFF: https://github.com/llvm/llvm-project/commit/df987794f9d7788e4cd06bf582a323b3c5ad7ad1.diff
LOG: [Clang][CodeGen] Handle TLS variables for non-zero Global ASes (#238159)
For targets that use a non-default AS for globals we sometimes insert a cast to the default AS in order to compose with AS agnostic languages such as e.g. HIP. This breaks the invariants around TLS access via the `threadlocal.address` intrinsic, which expects its argument to be the global itself, and not an AS-cast. Currently the breakage is almost not observable as GPU targets, which would expose it, do not support TLS, however:
- `hipstdpar` directly triggers it;
- in the future the lack of TLS might be revisited.
Overall, the composition of a non-default Global AS and TLS should work. This PR tries to do that by unwrapping the AS-cast, passing the casted pointer into `CreateThreadLocalAddress` and then casting the result. An alternative solution I looked at, but which would seem to go against the intended use case of the intrinsic / interface, sinks the handling into `CreateThreadLocalAddres`'s implementation.
Added:
Modified:
clang/lib/CodeGen/CGDecl.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/test/CodeGenCXX/threadlocal_address.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 8862f57301412..b425392ebbe41 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -310,7 +310,7 @@ llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
// Make sure the result is of the correct type.
LangAS ExpectedAS = Ty.getAddressSpace();
llvm::Constant *Addr = GV;
- if (AS != ExpectedAS) {
+ if (AS != ExpectedAS && D.getTLSKind() == VarDecl::TLS_None) {
Addr = performAddrSpaceCast(
GV,
llvm::PointerType::get(getLLVMContext(),
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index ab13a6ab9446f..e59f7811c89c1 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -6130,7 +6130,8 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
}
// Make sure the result is of the correct type.
- if (Entry->getType()->getAddressSpace() != TargetAS)
+ if (Entry->getType()->getAddressSpace() != TargetAS &&
+ (!D || D->getTLSKind() == VarDecl::TLS_None))
return llvm::ConstantExpr::getAddrSpaceCast(
Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
@@ -6275,7 +6276,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
D ? D->getType().getAddressSpace()
: (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
- if (DAddrSpace != ExpectedAS)
+ if (DAddrSpace != ExpectedAS && (!D || D->getTLSKind() == VarDecl::TLS_None))
return performAddrSpaceCast(
GV, llvm::PointerType::get(getLLVMContext(), TargetAS));
diff --git a/clang/test/CodeGenCXX/threadlocal_address.cpp b/clang/test/CodeGenCXX/threadlocal_address.cpp
index 0ae58ab550029..445a75538a64e 100644
--- a/clang/test/CodeGenCXX/threadlocal_address.cpp
+++ b/clang/test/CodeGenCXX/threadlocal_address.cpp
@@ -1,54 +1,160 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals all --version 6
// Test that the use of thread local variables would be wrapped by @llvm.threadlocal.address intrinsics.
// RUN: %clang_cc1 -std=c++11 -emit-llvm -triple x86_64 -o - %s -disable-llvm-passes | FileCheck %s
+// RUN: %clang_cc1 -std=c++11 -emit-llvm -triple amdgpu-amd-amdhsa -o - %s -disable-llvm-passes | FileCheck --check-prefix=NON-ZERO-GLOBAL-AS %s
// RUN: %clang_cc1 -std=c++11 -emit-llvm -triple aarch64 -o - -O1 %s | FileCheck %s -check-prefix=CHECK-O1
+
thread_local int i;
+//.
+// CHECK: @i = thread_local global i32 0, align 4
+// CHECK: @_ZZ1fvE1j = internal thread_local global i32 0, align 4
+// CHECK: @_ZZ1hvE1k = internal thread_local global i32 0, align 4
+//.
+// NON-ZERO-GLOBAL-AS: @i = thread_local addrspace(1) global i32 0, align 4
+// NON-ZERO-GLOBAL-AS: @_ZZ1fvE1j = internal thread_local addrspace(1) global i32 0, align 4
+// NON-ZERO-GLOBAL-AS: @_ZZ1hvE1k = internal thread_local addrspace(1) global i32 0, align 4
+//.
+// CHECK-O1: @i = thread_local global i32 0, align 4
+// CHECK-O1: @_ZZ1fvE1j = internal thread_local unnamed_addr global i32 0, align 4
+// CHECK-O1: @_ZZ1hvE1k = internal thread_local global i32 0, align 4
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_Z1gv(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[TMP0:%.*]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
+// CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
+// CHECK-NEXT: store i32 [[INC]], ptr [[TMP0]], align 4
+// CHECK-NEXT: [[TMP2:%.*]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
+// CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
+// CHECK-NEXT: ret i32 [[TMP3]]
+//
+// NON-ZERO-GLOBAL-AS-LABEL: define dso_local noundef i32 @_Z1gv(
+// NON-ZERO-GLOBAL-AS-SAME: ) #[[ATTR0:[0-9]+]] {
+// NON-ZERO-GLOBAL-AS-NEXT: [[ENTRY:.*:]]
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP0:%.*]] = call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @i)
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP1:%.*]] = load i32, ptr addrspace(1) [[TMP0]], align 4
+// NON-ZERO-GLOBAL-AS-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
+// NON-ZERO-GLOBAL-AS-NEXT: store i32 [[INC]], ptr addrspace(1) [[TMP0]], align 4
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP2:%.*]] = call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @i)
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP3:%.*]] = load i32, ptr addrspace(1) [[TMP2]], align 4
+// NON-ZERO-GLOBAL-AS-NEXT: ret i32 [[TMP3]]
+//
+// CHECK-O1-LABEL: define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z1gv(
+// CHECK-O1-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// CHECK-O1-NEXT: [[ENTRY:.*:]]
+// CHECK-O1-NEXT: [[TMP0:%.*]] = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
+// CHECK-O1-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[INT_TBAA9:![0-9]+]]
+// CHECK-O1-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
+// CHECK-O1-NEXT: store i32 [[INC]], ptr [[TMP0]], align 4, !tbaa [[INT_TBAA9]]
+// CHECK-O1-NEXT: ret i32 [[INC]]
+//
int g() {
i++;
return i;
}
-// CHECK: @i = {{.*}}thread_local global i32 0
-// CHECK: @_ZZ1fvE1j = internal thread_local global i32 0
-//
-// CHECK: @_Z1gv()
-// CHECK-NEXT: entry
-// CHECK-NEXT: %[[IA:.+]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
-// CHECK-NEXT: %[[VA:.+]] = load i32, ptr %[[IA]]
-// CHECK-NEXT: %[[INC:.+]] = add nsw i32 %[[VA]], 1
-// CHECK-NEXT: store i32 %[[INC]], ptr %[[IA]], align 4
-// CHECK-NEXT: %[[IA2:.+]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
-// CHECK-NEXT: %[[RET:.+]] = load i32, ptr %[[IA2]], align 4
-// CHECK-NEXT: ret i32 %[[RET]]
-//
-// CHECK: declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #[[ATTR_NUM:.+]]
-//
-// CHECK-O1-LABEL: @_Z1gv
-// CHECK-O1-NEXT: entry:
-// CHECK-O1-NEXT: %[[I_ADDR:.+]] = {{.*}}call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
-// CHECK-O1-NEXT: %[[VAL:.+]] = load i32, ptr %[[I_ADDR]]
-// CHECK-O1-NEXT: %[[INC:.+]] = add nsw i32 %[[VAL]], 1
-// CHECK-O1-NEXT: store i32 %[[INC]], ptr %[[I_ADDR]]
-// CHECK-O1-NEXT: ret i32 %[[INC]]
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z1fv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[TMP0:%.*]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1fvE1j)
+// CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
+// CHECK-NEXT: store i32 [[INC]], ptr [[TMP0]], align 4
+// CHECK-NEXT: [[TMP2:%.*]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1fvE1j)
+// CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
+// CHECK-NEXT: ret i32 [[TMP3]]
+//
+// NON-ZERO-GLOBAL-AS-LABEL: define dso_local noundef i32 @_Z1fv(
+// NON-ZERO-GLOBAL-AS-SAME: ) #[[ATTR0]] {
+// NON-ZERO-GLOBAL-AS-NEXT: [[ENTRY:.*:]]
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP0:%.*]] = call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @_ZZ1fvE1j)
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP1:%.*]] = load i32, ptr addrspace(1) [[TMP0]], align 4
+// NON-ZERO-GLOBAL-AS-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
+// NON-ZERO-GLOBAL-AS-NEXT: store i32 [[INC]], ptr addrspace(1) [[TMP0]], align 4
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP2:%.*]] = call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @_ZZ1fvE1j)
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP3:%.*]] = load i32, ptr addrspace(1) [[TMP2]], align 4
+// NON-ZERO-GLOBAL-AS-NEXT: ret i32 [[TMP3]]
+//
+// CHECK-O1-LABEL: define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z1fv(
+// CHECK-O1-SAME: ) local_unnamed_addr #[[ATTR0]] {
+// CHECK-O1-NEXT: [[ENTRY:.*:]]
+// CHECK-O1-NEXT: [[TMP0:%.*]] = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1fvE1j)
+// CHECK-O1-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[INT_TBAA9]]
+// CHECK-O1-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
+// CHECK-O1-NEXT: store i32 [[INC]], ptr [[TMP0]], align 4, !tbaa [[INT_TBAA9]]
+// CHECK-O1-NEXT: ret i32 [[INC]]
+//
int f() {
thread_local int j = 0;
j++;
return j;
}
-// CHECK: @_Z1fv()
-// CHECK-NEXT: entry
-// CHECK-NEXT: %[[JA:.+]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1fvE1j)
-// CHECK-NEXT: %[[VA:.+]] = load i32, ptr %[[JA]]
-// CHECK-NEXT: %[[INC:.+]] = add nsw i32 %[[VA]], 1
-// CHECK-NEXT: store i32 %[[INC]], ptr %[[JA]], align 4
-// CHECK-NEXT: %[[JA2:.+]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1fvE1j)
-// CHECK-NEXT: %[[RET:.+]] = load i32, ptr %[[JA2]], align 4
-// CHECK-NEXT: ret i32 %[[RET]]
-//
-// CHECK-O1-LABEL: @_Z1fv
-// CHECK-O1-NEXT: entry:
-// CHECK-O1-NEXT: %[[J_ADDR:.+]] = {{.*}}call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1fvE1j)
-// CHECK-O1-NEXT: %[[VAL:.+]] = load i32, ptr %[[J_ADDR]]
-// CHECK-O1-NEXT: %[[INC:.+]] = add nsw i32 %[[VAL]], 1
-// CHECK-O1-NEXT: store i32 %[[INC]], ptr %[[J_ADDR]]
-// CHECK-O1-NEXT: ret i32 %[[INC]]
-//
-// CHECK: attributes #[[ATTR_NUM]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+
+extern int foo(int *, int *);
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z1hv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[TMP0:%.*]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
+// CHECK-NEXT: [[TMP1:%.*]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1hvE1k)
+// CHECK-NEXT: [[CALL:%.*]] = call noundef i32 @_Z3fooPiS_(ptr noundef [[TMP0]], ptr noundef [[TMP1]])
+// CHECK-NEXT: ret i32 [[CALL]]
+//
+// NON-ZERO-GLOBAL-AS-LABEL: define dso_local noundef i32 @_Z1hv(
+// NON-ZERO-GLOBAL-AS-SAME: ) #[[ATTR0]] {
+// NON-ZERO-GLOBAL-AS-NEXT: [[ENTRY:.*:]]
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP0:%.*]] = call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @i)
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP1:%.*]] = call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @_ZZ1hvE1k)
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP2:%.*]] = addrspacecast ptr addrspace(1) [[TMP0]] to ptr
+// NON-ZERO-GLOBAL-AS-NEXT: [[TMP3:%.*]] = addrspacecast ptr addrspace(1) [[TMP1]] to ptr
+// NON-ZERO-GLOBAL-AS-NEXT: [[CALL:%.*]] = call noundef i32 @_Z3fooPiS_(ptr noundef [[TMP2]], ptr noundef [[TMP3]]) #[[ATTR4:[0-9]+]]
+// NON-ZERO-GLOBAL-AS-NEXT: ret i32 [[CALL]]
+//
+// CHECK-O1-LABEL: define dso_local noundef i32 @_Z1hv(
+// CHECK-O1-SAME: ) local_unnamed_addr #[[ATTR2:[0-9]+]] {
+// CHECK-O1-NEXT: [[ENTRY:.*:]]
+// CHECK-O1-NEXT: [[TMP0:%.*]] = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @i)
+// CHECK-O1-NEXT: [[TMP1:%.*]] = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZ1hvE1k)
+// CHECK-O1-NEXT: [[CALL:%.*]] = tail call noundef i32 @_Z3fooPiS_(ptr noundef nonnull [[TMP0]], ptr noundef nonnull [[TMP1]]) #[[ATTR5:[0-9]+]]
+// CHECK-O1-NEXT: ret i32 [[CALL]]
+//
+int h() {
+ thread_local int k = 0;
+ return foo(&i, &k);
+}
+//.
+// CHECK: attributes #[[ATTR0]] = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+// CHECK: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+// CHECK: attributes #[[ATTR2:[0-9]+]] = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+// CHECK: attributes #[[ATTR3:[0-9]+]] = { noinline nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+//.
+// NON-ZERO-GLOBAL-AS: attributes #[[ATTR0]] = { convergent mustprogress noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// NON-ZERO-GLOBAL-AS: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+// NON-ZERO-GLOBAL-AS: attributes #[[ATTR2:[0-9]+]] = { convergent "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// NON-ZERO-GLOBAL-AS: attributes #[[ATTR3:[0-9]+]] = { convergent noinline nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// NON-ZERO-GLOBAL-AS: attributes #[[ATTR4]] = { convergent }
+//.
+// CHECK-O1: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none, target_mem: none) "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// CHECK-O1: attributes #[[ATTR1:[0-9]+]] = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+// CHECK-O1: attributes #[[ATTR2]] = { mustprogress nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// CHECK-O1: attributes #[[ATTR3:[0-9]+]] = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// CHECK-O1: attributes #[[ATTR4:[0-9]+]] = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// CHECK-O1: attributes #[[ATTR5]] = { nounwind }
+//.
+// CHECK: [[META0:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
+//.
+// NON-ZERO-GLOBAL-AS: [[META0:![0-9]+]] = !{i32 1, !"amdhsa_code_object_version", i32 600}
+// NON-ZERO-GLOBAL-AS: [[META1:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
+//.
+// CHECK-O1: [[META0:![0-9]+]] = !{i32 1, !"ptrauth-elf-got", i32 0}
+// CHECK-O1: [[META1:![0-9]+]] = !{i32 1, !"ptrauth-init-fini", i32 0}
+// CHECK-O1: [[META2:![0-9]+]] = !{i32 1, !"ptrauth-init-fini-address-discrimination", i32 0}
+// CHECK-O1: [[META3:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
+// CHECK-O1: [[META4:![0-9]+]] = !{[[META5:![0-9]+]], [[META6:![0-9]+]], i64 0}
+// CHECK-O1: [[META5]] = !{!"__libc_errno", [[META6]], i64 0}
+// CHECK-O1: [[META6]] = !{!"int", [[META7:![0-9]+]], i64 0}
+// CHECK-O1: [[META7]] = !{!"omnipotent char", [[META8:![0-9]+]], i64 0}
+// CHECK-O1: [[META8]] = !{!"Simple C++ TBAA"}
+// CHECK-O1: [[INT_TBAA9]] = !{[[META6]], [[META6]], i64 0}
+//.
More information about the cfe-commits
mailing list