[clang] [llvm] [WebAssembly] Implement %llvm.thread.pointer intrinsic. NFC (PR #117817)

Sam Clegg via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 26 17:14:53 PST 2024


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

>From 9578cf12c95d2511975b9ede6edb3bddc5f5da74 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
---
 clang/test/CodeGen/builtins-wasm.c            |  5 +++
 .../WebAssembly/WebAssemblyISelLowering.cpp   | 11 +++++++
 .../CodeGen/WebAssembly/thread_pointer.ll     | 32 +++++++++++++++++++
 3 files changed, 48 insertions(+)
 create mode 100644 llvm/test/CodeGen/WebAssembly/thread_pointer.ll

diff --git a/clang/test/CodeGen/builtins-wasm.c b/clang/test/CodeGen/builtins-wasm.c
index 7ee2ac5de3eac7..263cfd3ab4c691 100644
--- a/clang/test/CodeGen/builtins-wasm.c
+++ b/clang/test/CodeGen/builtins-wasm.c
@@ -740,3 +740,8 @@ __externref_t externref_null() {
   // WEBASSEMBLY: tail call ptr addrspace(10) @llvm.wasm.ref.null.extern()
   // WEBASSEMBLY-NEXT: ret
 }
+
+void *tp (void) {
+  return __builtin_thread_pointer ();
+  // WEBASSEMBLY: call {{.*}} @llvm.thread.pointer()
+}
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 cfe-commits mailing list