[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:42:21 PST 2024


https://github.com/sbc100 updated https://github.com/llvm/llvm-project/pull/117817

>From e2783e0f964883cc58ecdce2c982b41d847744cd 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     | 32 +++++++++++++++++++
 2 files changed, 43 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..88c4c6aeb30b0b
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/thread_pointer.ll
@@ -0,0 +1,32 @@
+; 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
+; 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