Skip to content

Commit 39b5367

Browse files
committedMar 20, 2018
[WebAssembly] Strip threadlocal attribute from globals in single thread mode
The default thread model for wasm is single, and in this mode thread-local global variables can be lowered identically to non-thread-local variables. Differential Revision: https://reviews.llvm.org/D44703 llvm-svn: 328049
1 parent e28ff4d commit 39b5367

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
 

‎llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
126126
}
127127

128128
namespace {
129+
class StripThreadLocal final : public ModulePass {
130+
// The default thread model for wasm is single, where thread-local variables
131+
// are identical to regular globals and should be treated the same. So this
132+
// pass just converts all GlobalVariables to NotThreadLocal
133+
static char ID;
134+
135+
public:
136+
StripThreadLocal() : ModulePass(ID) {}
137+
bool runOnModule(Module &M) override {
138+
for (auto &GV : M.globals())
139+
GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal);
140+
return true;
141+
}
142+
};
143+
char StripThreadLocal::ID = 0;
144+
129145
/// WebAssembly Code Generator Pass Configuration Options.
130146
class WebAssemblyPassConfig final : public TargetPassConfig {
131147
public:
@@ -166,13 +182,15 @@ FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
166182
//===----------------------------------------------------------------------===//
167183

168184
void WebAssemblyPassConfig::addIRPasses() {
169-
if (TM->Options.ThreadModel == ThreadModel::Single)
185+
if (TM->Options.ThreadModel == ThreadModel::Single) {
170186
// In "single" mode, atomics get lowered to non-atomics.
171187
addPass(createLowerAtomicPass());
172-
else
188+
addPass(new StripThreadLocal());
189+
} else {
173190
// Expand some atomic operations. WebAssemblyTargetLowering has hooks which
174191
// control specifically what gets lowered.
175192
addPass(createAtomicExpandPass());
193+
}
176194

177195
// Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
178196
addPass(createWebAssemblyLowerGlobalDtors());

‎llvm/test/CodeGen/WebAssembly/tls.ll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -disable-wasm-explicit-locals -thread-model=single | FileCheck --check-prefix=SINGLE %s
2+
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
3+
target triple = "wasm32-unknown-unknown-wasm"
4+
5+
; SINGLE-LABEL: address_of_tls:
6+
define i32 @address_of_tls() {
7+
; SINGLE: i32.const $push0=, tls
8+
; SINGLE-NEXT: return $pop0
9+
ret i32 ptrtoint(i32* @tls to i32)
10+
}
11+
12+
; SINGLE: .type tls,@object
13+
; SINGLE-NEXT: .section .bss.tls,"",@
14+
; SINGLE-NEXT: .p2align 2
15+
; SINGLE-NEXT: tls:
16+
; SINGLE-NEXT: .int32 0
17+
@tls = internal thread_local global i32 0

0 commit comments

Comments
 (0)
Please sign in to comment.