Skip to content

Commit 1c5a3c4

Browse files
committed
[WebAssembly] Make SjLj lowering globals thread-local
Emscripten's longjump and exception mechanism depends on two global variables, `__THREW__` and `__threwValue`, which are changed to be defined as thread-local in emscripten-core/emscripten#12056. This patch updates the corresponding code in the WebAssembly backend to properly declare these globals as thread-local as well. Differential Revision: https://reviews.llvm.org/D88262
1 parent a079f61 commit 1c5a3c4

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,12 @@ static bool canThrow(const Value *V) {
315315
// which will generate an import and asssumes that it will exist at link time.
316316
static GlobalVariable *getGlobalVariableI32(Module &M, IRBuilder<> &IRB,
317317
const char *Name) {
318-
319-
auto *GV =
320-
dyn_cast<GlobalVariable>(M.getOrInsertGlobal(Name, IRB.getInt32Ty()));
318+
auto Int32Ty = IRB.getInt32Ty();
319+
auto *GV = dyn_cast<GlobalVariable>(M.getOrInsertGlobal(Name, Int32Ty, [&]() {
320+
return new GlobalVariable(M, Int32Ty, false,
321+
GlobalVariable::ExternalLinkage, nullptr, Name,
322+
nullptr, GlobalValue::LocalExecTLSModel);
323+
}));
321324
if (!GV)
322325
report_fatal_error(Twine("unable to create global: ") + Name);
323326

llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,6 @@ FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
348348
//===----------------------------------------------------------------------===//
349349

350350
void WebAssemblyPassConfig::addIRPasses() {
351-
// Runs LowerAtomicPass if necessary
352-
addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
353-
354-
// This is a no-op if atomics are not used in the module
355-
addPass(createAtomicExpandPass());
356-
357351
// Add signatures to prototype-less function declarations
358352
addPass(createWebAssemblyAddMissingPrototypes());
359353

@@ -389,6 +383,12 @@ void WebAssemblyPassConfig::addIRPasses() {
389383
// Expand indirectbr instructions to switches.
390384
addPass(createIndirectBrExpandPass());
391385

386+
// Lower atomics and TLS if necessary
387+
addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
388+
389+
// This is a no-op if atomics are not used in the module
390+
addPass(createAtomicExpandPass());
391+
392392
TargetPassConfig::addIRPasses();
393393
}
394394

llvm/test/CodeGen/WebAssembly/lower-em-exceptions.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ target triple = "wasm32-unknown-unknown"
55

66
@_ZTIi = external constant i8*
77
@_ZTIc = external constant i8*
8-
; CHECK-DAG: __THREW__ = external global i32
9-
; CHECK-DAG: __threwValue = external global i32
8+
; CHECK-DAG: __THREW__ = external thread_local(localexec) global i32
9+
; CHECK-DAG: __threwValue = external thread_local(localexec) global i32
1010

1111
; Test invoke instruction with clauses (try-catch block)
1212
define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {

llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ target triple = "wasm32-unknown-unknown"
66
%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
77

88
@global_var = global i32 0, align 4
9-
; CHECK-DAG: __THREW__ = external global i32
10-
; CHECK-DAG: __threwValue = external global i32
9+
; CHECK-DAG: __THREW__ = external thread_local(localexec) global i32
10+
; CHECK-DAG: __threwValue = external thread_local(localexec) global i32
1111

1212
; Test a simple setjmp - longjmp sequence
1313
define void @setjmp_longjmp() {

0 commit comments

Comments
 (0)