Skip to content

Commit

Permalink
Use emscripten_stack_set_limits in wasm_worker_initialize.S
Browse files Browse the repository at this point in the history
Followup to emscripten-core#22683.  This change avoids making __stack_base and
__stack_end into public/external symbols and avoids reimplementing
`emscripten_stack_set_limits`.
  • Loading branch information
sbc100 committed Oct 17, 2024
1 parent 814ec05 commit e481c5f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
3 changes: 0 additions & 3 deletions system/lib/compiler-rt/stack_limits.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
#endif

.globaltype __stack_pointer, PTR
.globl __stack_pointer

.section .globals,"",@

# TODO(sbc): It would be nice if these we initialized directly
# using PTR.const rather than using the `emscripten_stack_init`
.globaltype __stack_end, PTR
__stack_end:
.globl __stack_end
.globaltype __stack_base, PTR
.globl __stack_base
__stack_base:

.section .text,"",@
Expand Down
26 changes: 15 additions & 11 deletions system/lib/wasm_worker/wasm_worker_initialize.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.extern __stack_pointer
.extern __stack_base
.extern __stack_end

#ifdef __wasm64__
#define PTR i64
Expand All @@ -13,32 +11,37 @@
#endif

.globaltype __stack_pointer, PTR
.globaltype __stack_end, PTR
.globaltype __stack_base, PTR
.globaltype __tls_size, PTR, immutable
.functype emscripten_stack_set_limits (PTR /*base*/, PTR /*end*/) -> ()

.globl _emscripten_wasm_worker_initialize
_emscripten_wasm_worker_initialize:
.functype _emscripten_wasm_worker_initialize (PTR /*stackLowestAddress*/, i32 /*stackSize*/) -> ()
.local PTR, PTR

// __stack_end = stackLowestAddress + (__builtin_wasm_tls_size() + 15) & -16;
// stack_end = stackLowestAddress + (__builtin_wasm_tls_size() + 15) & -16;
local.get 0
.globaltype __tls_size, PTR, immutable
global.get __tls_size
PTR.add
PTR.const 0xf
PTR.add
PTR.const -0x10
PTR.and
global.set __stack_end
local.set 2

// __stack_base = stackLowestAddress + stackSize;
// stack_base = stackLowestAddress + stackSize;
local.get 0
local.get 1
#ifdef __wasm64__
i64.extend_i32_u
#endif
PTR.add
global.set __stack_base
local.set 3

local.get 3
local.get 2
// emscripten_stack_set_limits(stack_base, stack_end);
call emscripten_stack_set_limits

// TODO: We'd like to do this here to avoid JS side calls to __set_stack_limits.
// (or even better, we'd like to avoid duplicate versions of the stack variables)
Expand All @@ -48,6 +51,7 @@ _emscripten_wasm_worker_initialize:
// .functype __set_stack_limits (PTR, PTR) -> ()
// call __set_stack_limits


// __wasm_init_tls(stackLowestAddress);
local.get 0
.functype __wasm_init_tls (PTR) -> ()
Expand All @@ -57,8 +61,8 @@ _emscripten_wasm_worker_initialize:
// __stack_pointer initialized, and it destroys the value it was set to.
// So we must initialize __stack_pointer only *after* completing __wasm_init_tls:

// __stack_pointer = __stack_base;
global.get __stack_base
// __stack_pointer = stack_base;
local.get 3
global.set __stack_pointer

end_function
8 changes: 4 additions & 4 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"a.js.gz": 455,
"a.ww.js": 115,
"a.ww.js.gz": 127,
"a.wasm": 1894,
"a.wasm.gz": 1077,
"total": 3292,
"total_gz": 2043
"a.wasm": 1879,
"a.wasm.gz": 1066,
"total": 3277,
"total_gz": 2032
}

0 comments on commit e481c5f

Please sign in to comment.