[PATCH] D119803: [WebAssembly] Make __wasm_lpad_context thread-local

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 14 18:31:12 PST 2022


aheejin created this revision.
aheejin added a reviewer: sbc100.
Herald added subscribers: wingo, ecnelises, sunfish, hiraditya, jgravelle-google, dschuff.
aheejin requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This makes `__wasm_lpad_context`, a struct that is used as a
communication channel between compiler-generated code and personality
function in libunwind, thread local. The library code will be changed to
thread local in the emscripten side.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119803

Files:
  llvm/lib/CodeGen/WasmEHPrepare.cpp
  llvm/test/CodeGen/WebAssembly/wasmehprepare.ll


Index: llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
+++ llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
@@ -1,9 +1,11 @@
-; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
+; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s --check-prefixes=CHECK,NO-TLS
+; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S --mattr=+atomics,+bulk-memory | FileCheck %s --check-prefixes=CHECK,TLS
 
 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
 target triple = "wasm32-unknown-unknown"
 
-; CHECK: @__wasm_lpad_context = external global { i32, i8*, i32 }
+; NO-TLS: @__wasm_lpad_context = external global { i32, i8*, i32 }
+; TLS: @__wasm_lpad_context = external thread_local global { i32, i8*, i32 }
 
 @_ZTIi = external constant i8*
 %struct.Temp = type { i8 }
Index: llvm/lib/CodeGen/WasmEHPrepare.cpp
===================================================================
--- llvm/lib/CodeGen/WasmEHPrepare.cpp
+++ llvm/lib/CodeGen/WasmEHPrepare.cpp
@@ -212,9 +212,25 @@
 
   assert(F.hasPersonalityFn() && "Personality function not found");
 
-  // __wasm_lpad_context global variable
+  // __wasm_lpad_context global variable.
+  // If the target supports TLS, make this thread-local. We can't just
+  // unconditionally make it thread-local and depend on
+  // CoalesceFeaturesAndStripAtomics to downgrade it, because stripping TLS has
+  // the side effect of disallowing the object from being linked into a
+  // shared-memory module, which we don't want to be responsible for.
   LPadContextGV = cast<GlobalVariable>(
       M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy));
+  Attribute FSAttr = F.getFnAttribute("target-features");
+  bool EnableTLS = false;
+  if (FSAttr.isValid()) {
+    StringRef FS = FSAttr.getValueAsString();
+    if (FS.contains("+atomics") && FS.contains("+bulk-memory"))
+      EnableTLS = true;
+  }
+  auto TLS = EnableTLS ? GlobalValue::GeneralDynamicTLSModel
+                       : GlobalValue::NotThreadLocal;
+  LPadContextGV->setThreadLocalMode(TLS);
+
   LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
                                           "lpad_index_gep");
   LSDAField =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119803.408683.patch
Type: text/x-patch
Size: 2374 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220215/94a0e6bb/attachment.bin>


More information about the llvm-commits mailing list