[llvm] [WebAssembly] Implement %llvm.thread.pointer intrinsic. NFC (PR #117817)
Sam Clegg via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 26 16:41:21 PST 2024
https://github.com/sbc100 created https://github.com/llvm/llvm-project/pull/117817
We can simply use the `__tls_base` global for this which is guaranteed to be non-zero and unique per thread.
Fixes: #117433
>From 8f29cb61d97dd972f32327da327366a4a9410643 Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Tue, 26 Nov 2024 16:38:00 -0800
Subject: [PATCH] [WebAssembly] Implement %llvm.thread.pointer intrinsic. NFC
We can simply use the `__tls_base` global for this which is guaranteed
to be non-zero and unique per thread.
Fixes: #117433
---
.../WebAssembly/WebAssemblyISelLowering.cpp | 11 ++++++++++
.../CodeGen/WebAssembly/thread_pointer.ll | 21 +++++++++++++++++++
2 files changed, 32 insertions(+)
create mode 100644 llvm/test/CodeGen/WebAssembly/thread_pointer.ll
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index 2d00889407ff48..94b49387b58f91 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -2089,6 +2089,17 @@ SDValue WebAssemblyTargetLowering::LowerIntrinsic(SDValue Op,
}
return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops);
}
+
+ case Intrinsic::thread_pointer: {
+ MVT PtrVT = getPointerTy(DAG.getDataLayout());
+ auto GlobalGet = PtrVT == MVT::i64 ? WebAssembly::GLOBAL_GET_I64
+ : WebAssembly::GLOBAL_GET_I32;
+ const char *TlsBase = MF.createExternalSymbolName("__tls_base");
+ return SDValue(
+ DAG.getMachineNode(GlobalGet, DL, PtrVT,
+ DAG.getTargetExternalSymbol(TlsBase, PtrVT)),
+ 0);
+ }
}
}
diff --git a/llvm/test/CodeGen/WebAssembly/thread_pointer.ll b/llvm/test/CodeGen/WebAssembly/thread_pointer.ll
new file mode 100644
index 00000000000000..cca739716fe887
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/thread_pointer.ll
@@ -0,0 +1,21 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=wasm32-unknown-unknown | FileCheck %s --check-prefix=wasm32
+; RUN: llc < %s -mtriple=wasm64-unknown-unknown | FileCheck %s --check-prefix=wasm64
+
+declare ptr @llvm.thread.pointer()
+
+define ptr @thread_pointer() nounwind {
+; wasm32-LABEL: thread_pointer:
+; wasm32: .functype thread_pointer () -> (i32)
+; wasm32-NEXT: # %bb.0:
+; wasm32-NEXT: global.get __tls_base
+; wasm32-NEXT: # fallthrough-return
+;
+; wasm64-LABEL: thread_pointer:
+; wasm64: .functype thread_pointer () -> (i64)
+; wasm64-NEXT: # %bb.0:
+; wasm64-NEXT: global.get __tls_base
+; wasm64-NEXT: # fallthrough-return
+ %1 = tail call ptr @llvm.thread.pointer()
+ ret ptr %1
+}
More information about the llvm-commits
mailing list