Skip to content

Commit 82a7028

Browse files
committed
[clang][IR] Overload @llvm.thread.pointer to support non-AS0 targets
1 parent 91d8f6e commit 82a7028

File tree

20 files changed

+74
-43
lines changed

20 files changed

+74
-43
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5726,8 +5726,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
57265726
case Builtin::BI__builtin_thread_pointer: {
57275727
if (!getContext().getTargetInfo().isTLSSupported())
57285728
CGM.ErrorUnsupported(E, "__builtin_thread_pointer");
5729-
// Fall through - it's already mapped to the intrinsic by ClangBuiltin.
5730-
break;
5729+
5730+
return RValue::get(Builder.CreateIntrinsic(llvm::Intrinsic::thread_pointer,
5731+
{GlobalsInt8PtrTy}, {}));
57315732
}
57325733
case Builtin::BI__builtin_os_log_format:
57335734
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)

llvm/include/llvm/IR/Intrinsics.td

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

861861
def int_get_dynamic_area_offset : DefaultAttrsIntrinsic<[llvm_anyint_ty]>;
862862

863-
def int_thread_pointer : DefaultAttrsIntrinsic<[llvm_ptr_ty], [], [IntrNoMem]>,
863+
def int_thread_pointer : DefaultAttrsIntrinsic<[llvm_anyptr_ty], [], [IntrNoMem]>,
864864
ClangBuiltin<"__builtin_thread_pointer">;
865865

866866
// IntrInaccessibleMemOrArgMemOnly is a little more pessimistic than strictly

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
785785
return true;
786786
}
787787
if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") {
788-
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer);
788+
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer,
789+
F->getReturnType());
789790
return true;
790791
}
791792
if (Name.startswith("arm.neon.vqadds.")) {
@@ -1333,6 +1334,14 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
13331334
}
13341335
break;
13351336

1337+
case 't':
1338+
if (Name == "thread.pointer") {
1339+
NewFn = Intrinsic::getDeclaration(
1340+
F->getParent(), Intrinsic::thread_pointer, F->getReturnType());
1341+
return true;
1342+
}
1343+
break;
1344+
13361345
case 'v': {
13371346
auto *ArgTy = F->arg_empty() ? nullptr : F->arg_begin()->getType();
13381347
if (Name == "va_start") {

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24401,7 +24401,8 @@ bool AArch64TargetLowering::shouldNormalizeToSelectSequence(LLVMContext &,
2440124401
static Value *UseTlsOffset(IRBuilderBase &IRB, unsigned Offset) {
2440224402
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
2440324403
Function *ThreadPointerFunc =
24404-
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
24404+
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer,
24405+
IRB.getInt8PtrTy());
2440524406
return IRB.CreatePointerCast(
2440624407
IRB.CreateConstGEP1_32(IRB.getInt8Ty(), IRB.CreateCall(ThreadPointerFunc),
2440724408
Offset),

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17854,12 +17854,14 @@ bool RISCVTargetLowering::preferScalarizeSplat(SDNode *N) const {
1785417854

1785517855
static Value *useTpOffset(IRBuilderBase &IRB, unsigned Offset) {
1785617856
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
17857+
unsigned AS = M->getDataLayout().getGlobalsAddressSpace();
1785717858
Function *ThreadPointerFunc =
17858-
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
17859+
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer,
17860+
IRB.getInt8PtrTy(AS));
1785917861
return IRB.CreatePointerCast(
1786017862
IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
1786117863
IRB.CreateCall(ThreadPointerFunc), Offset),
17862-
IRB.getInt8PtrTy()->getPointerTo(0));
17864+
IRB.getInt8PtrTy()->getPointerTo(AS));
1786317865
}
1786417866

1786517867
Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,8 @@ Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
11821182
// Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
11831183
// in Bionic's libc/private/bionic_tls.h.
11841184
Function *ThreadPointerFunc =
1185-
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
1185+
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer,
1186+
IRB.getInt8PtrTy());
11861187
Value *SlotPtr = IRB.CreatePointerCast(
11871188
IRB.CreateConstGEP1_32(Int8Ty, IRB.CreateCall(ThreadPointerFunc), 0x30),
11881189
Ty->getPointerTo(0));

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: 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: 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
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: %riscv32_cheri_purecap_llc < %s | FileCheck %s
3+
; RUN: %riscv64_cheri_purecap_llc < %s | FileCheck %s
4+
5+
declare ptr addrspace(200) @llvm.thread.pointer.p200()
6+
7+
define ptr addrspace(200) @thread_pointer() nounwind {
8+
; CHECK-LABEL: thread_pointer:
9+
; CHECK: # %bb.0:
10+
; CHECK-NEXT: cmove ca0, ctp
11+
; CHECK-NEXT: ret
12+
%1 = tail call ptr addrspace(200) @llvm.thread.pointer.p200()
13+
ret ptr addrspace(200) %1
14+
}

llvm/test/Instrumentation/HWAddressSanitizer/alloca-array.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare void @use(ptr, ptr)
99
define void @test_alloca() sanitize_hwaddress {
1010
; CHECK-LABEL: define void @test_alloca
1111
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
12-
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @llvm.thread.pointer()
12+
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @llvm.thread.pointer.p0()
1313
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[TMP1]], i32 48
1414
; CHECK-NEXT: [[TMP3:%.*]] = load i64, ptr [[TMP2]], align 8
1515
; CHECK-NEXT: [[TMP4:%.*]] = ashr i64 [[TMP3]], 3

0 commit comments

Comments
 (0)