Skip to content

Commit 778502b

Browse files
committed
[clang][IR] Overload @llvm.thread.pointer to support non-AS0 targets
Thraed-local globals live, by default, in the default globals address space, which may not be 0, so we need to overload @llvm.thread.pointer to support other address spaces, and use the default globals address space in Clang.
1 parent acdb0c1 commit 778502b

File tree

21 files changed

+59
-48
lines changed

21 files changed

+59
-48
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5905,8 +5905,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
59055905
case Builtin::BI__builtin_thread_pointer: {
59065906
if (!getContext().getTargetInfo().isTLSSupported())
59075907
CGM.ErrorUnsupported(E, "__builtin_thread_pointer");
5908-
// Fall through - it's already mapped to the intrinsic by ClangBuiltin.
5909-
break;
5908+
5909+
return RValue::get(Builder.CreateIntrinsic(llvm::Intrinsic::thread_pointer,
5910+
{GlobalsInt8PtrTy}, {}));
59105911
}
59115912
case Builtin::BI__builtin_os_log_format:
59125913
return emitBuiltinOSLogFormat(*E);

clang/test/CodeGen/builtins-arm64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void f0(void *a, void *b) {
1010

1111
void *tp (void) {
1212
return __builtin_thread_pointer ();
13-
// CHECK-LINUX: call {{.*}} @llvm.thread.pointer()
13+
// CHECK-LINUX: call {{.*}} @llvm.thread.pointer.p0()
1414
}
1515

1616
// CHECK: call {{.*}} @llvm.bitreverse.i32(i32 %a)

clang/test/CodeGen/builtins-wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,5 +743,5 @@ __externref_t externref_null() {
743743

744744
void *tp (void) {
745745
return __builtin_thread_pointer ();
746-
// WEBASSEMBLY: call {{.*}} @llvm.thread.pointer()
746+
// WEBASSEMBLY: call {{.*}} @llvm.thread.pointer.p0()
747747
}

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ def int_stackrestore : DefaultAttrsIntrinsic<[], [llvm_anyptr_ty]>,
882882

883883
def int_get_dynamic_area_offset : DefaultAttrsIntrinsic<[llvm_anyint_ty]>;
884884

885-
def int_thread_pointer : DefaultAttrsIntrinsic<[llvm_ptr_ty], [], [IntrNoMem]>,
885+
def int_thread_pointer : DefaultAttrsIntrinsic<[llvm_anyptr_ty], [], [IntrNoMem]>,
886886
ClangBuiltin<"__builtin_thread_pointer">;
887887

888888
// IntrInaccessibleMemOrArgMemOnly is a little more pessimistic than strictly

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@ static bool upgradeArmOrAarch64IntrinsicFunction(bool IsArm, Function *F,
645645

646646
if (Name == "thread.pointer") {
647647
// '(arm|aarch64).thread.pointer'.
648-
NewFn = Intrinsic::getOrInsertDeclaration(F->getParent(),
649-
Intrinsic::thread_pointer);
648+
NewFn = Intrinsic::getOrInsertDeclaration(
649+
F->getParent(), Intrinsic::thread_pointer, F->getReturnType());
650650
return true;
651651
}
652652

@@ -1419,6 +1419,14 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
14191419
}
14201420
break;
14211421

1422+
case 't':
1423+
if (Name == "thread.pointer") {
1424+
NewFn = Intrinsic::getDeclaration(
1425+
F->getParent(), Intrinsic::thread_pointer, F->getReturnType());
1426+
return true;
1427+
}
1428+
break;
1429+
14221430
case 'v': {
14231431
if (Name == "var.annotation" && F->arg_size() == 4) {
14241432
rename(F);

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28166,8 +28166,8 @@ bool AArch64TargetLowering::shouldNormalizeToSelectSequence(LLVMContext &,
2816628166

2816728167
static Value *UseTlsOffset(IRBuilderBase &IRB, unsigned Offset) {
2816828168
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
28169-
Function *ThreadPointerFunc =
28170-
Intrinsic::getOrInsertDeclaration(M, Intrinsic::thread_pointer);
28169+
Function *ThreadPointerFunc = Intrinsic::getOrInsertDeclaration(
28170+
M, Intrinsic::thread_pointer, IRB.getPtrTy());
2817128171
return IRB.CreatePointerCast(
2817228172
IRB.CreateConstGEP1_32(IRB.getInt8Ty(), IRB.CreateCall(ThreadPointerFunc),
2817328173
Offset),

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22916,8 +22916,8 @@ bool RISCVTargetLowering::preferScalarizeSplat(SDNode *N) const {
2291622916

2291722917
static Value *useTpOffset(IRBuilderBase &IRB, unsigned Offset) {
2291822918
Module *M = IRB.GetInsertBlock()->getModule();
22919-
Function *ThreadPointerFunc =
22920-
Intrinsic::getOrInsertDeclaration(M, Intrinsic::thread_pointer);
22919+
Function *ThreadPointerFunc = Intrinsic::getOrInsertDeclaration(
22920+
M, Intrinsic::thread_pointer, IRB.getPtrTy());
2292122921
return IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
2292222922
IRB.CreateCall(ThreadPointerFunc), Offset);
2292322923
}

llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ Value *getAndroidSlotPtr(IRBuilder<> &IRB, int Slot) {
290290
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
291291
// Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
292292
// in Bionic's libc/private/bionic_tls.h.
293-
Function *ThreadPointerFunc =
294-
Intrinsic::getOrInsertDeclaration(M, Intrinsic::thread_pointer);
293+
Function *ThreadPointerFunc = Intrinsic::getOrInsertDeclaration(
294+
M, Intrinsic::thread_pointer,
295+
IRB.getPtrTy(M->getDataLayout().getDefaultGlobalsAddressSpace()));
295296
return IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
296297
IRB.CreateCall(ThreadPointerFunc), 8 * Slot);
297298
}

llvm/test/Assembler/autoupgrade-thread-pointer.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ declare ptr @llvm.arm.thread.pointer()
66

77
define ptr @test1() {
88
; CHECK-LABEL: define ptr @test1()
9-
; CHECK: call ptr @llvm.thread.pointer()
9+
; CHECK: call ptr @llvm.thread.pointer.p0()
1010
%1 = call ptr @llvm.aarch64.thread.pointer()
1111
ret ptr %1
1212
}
1313

1414
define ptr @test2() {
1515
; CHECK-LABEL: define ptr @test2()
16-
; CHECK: call ptr @llvm.thread.pointer()
16+
; CHECK: call ptr @llvm.thread.pointer.p0()
1717
%1 = call ptr @llvm.arm.thread.pointer()
1818
ret ptr %1
1919
}

llvm/test/CodeGen/AArch64/stack-tagging-prologue.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ entry:
2323

2424
; INSTR-LABEL: define void @OneVar(
2525
; INSTR: [[BASE:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
26-
; INSTR: [[TLS:%.*]] = call ptr @llvm.thread.pointer()
26+
; INSTR: [[TLS:%.*]] = call ptr @llvm.thread.pointer.p0()
2727
; INSTR: [[TLS_SLOT:%.*]] = getelementptr i8, ptr [[TLS]], i32 -24
2828
; INSTR: [[TLS_VALUE:%.*]] = load i64, ptr [[TLS_SLOT]], align 8
2929
; INSTR: [[FP:%.*]] = call ptr @llvm.frameaddress.p0(i32 0)

0 commit comments

Comments
 (0)