Skip to content

Commit

Permalink
Merge branch 'emscripten-core:main' into cw-audio-tweaks-2
Browse files Browse the repository at this point in the history
  • Loading branch information
cwoffenden authored Oct 17, 2024
2 parents 2efd0f5 + 814ec05 commit 00c79dd
Show file tree
Hide file tree
Showing 41 changed files with 328 additions and 383 deletions.
11 changes: 5 additions & 6 deletions src/library_glemu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ var LibraryGLEmulation = {
GLImmediate.rendererComponents[name] = 1;
#if ASSERTIONS
if (GLImmediate.enabledClientAttributes[name]) {
out("Warning: glTexCoord used after EnableClientState for TEXTURE_COORD_ARRAY for TEXTURE0. Disabling TEXTURE_COORD_ARRAY...");
warnOnce("Warning: glTexCoord used after EnableClientState for TEXTURE_COORD_ARRAY for TEXTURE0. Disabling TEXTURE_COORD_ARRAY...");
}
#endif
GLImmediate.enabledClientAttributes[name] = true;
Expand Down Expand Up @@ -2980,11 +2980,10 @@ var LibraryGLEmulation = {
var attr = attributes[i];
var srcStride = Math.max(attr.sizeBytes, attr.stride);
if ((srcStride & 3) == 0 && (attr.sizeBytes & 3) == 0) {
var size4 = attr.sizeBytes>>2;
var srcStride4 = Math.max(attr.sizeBytes, attr.stride)>>2;
for (var j = 0; j < count; j++) {
for (var k = 0; k < size4; k++) { // copy in chunks of 4 bytes, our alignment makes this possible
HEAP32[((start + attr.offset + bytes*j)>>2) + k] = HEAP32[(attr.pointer>>2) + j*srcStride4 + k];
for (var k = 0; k < attr.sizeBytes; k+=4) { // copy in chunks of 4 bytes, our alignment makes this possible
var val = {{{ makeGetValue('attr.pointer', 'j*srcStride + k', 'i32') }}};
{{{ makeSetValue('start + attr.offset', 'bytes*j + k', 'val', 'i32') }}};
}
}
} else {
Expand Down Expand Up @@ -3533,7 +3532,7 @@ var LibraryGLEmulation = {
if (!GLctx.currentElementArrayBufferBinding) {
assert(type == GLctx.UNSIGNED_SHORT); // We can only emulate buffers of this kind, for now
}
out("DrawElements doesn't actually prepareClientAttributes properly.");
warnOnce("DrawElements doesn't actually prepareClientAttributes properly.");
#endif
GLImmediate.prepareClientAttributes(count, false);
GLImmediate.mode = mode;
Expand Down
54 changes: 3 additions & 51 deletions system/lib/compiler-rt/stack_limits.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
#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 Expand Up @@ -82,57 +85,6 @@ emscripten_stack_get_free:
PTR.sub
end_function

#ifdef __EMSCRIPTEN_WASM_WORKERS__
# TODO: Relocate the following to its own file wasm_worker.S, but need to figure out how to reference
# __stack_base and __stack_end globals from a separate file as externs in order for that to work.
.globl _emscripten_wasm_worker_initialize
_emscripten_wasm_worker_initialize:
.functype _emscripten_wasm_worker_initialize (PTR /*stackLowestAddress*/, i32 /*stackSize*/) -> ()

// __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

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

// 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)
// See https://github.com/emscripten-core/emscripten/issues/16496
// global.get __stack_base
// global.get __stack_end
// .functype __set_stack_limits (PTR, PTR) -> ()
// call __set_stack_limits

// __wasm_init_tls(stackLowestAddress);
local.get 0
.functype __wasm_init_tls (PTR) -> ()
call __wasm_init_tls

// N.b. The function __wasm_init_tls above does not need
// __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
global.set __stack_pointer

end_function
#endif

# Add emscripten_stack_init to static ctors
.section .init_array.1,"",@
.p2align ALIGN
Expand Down
3 changes: 1 addition & 2 deletions system/lib/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
/* XXX Emscripten Tracing API. This defines away the code if tracing is disabled. */
#include <emscripten/trace.h>

#ifdef __EMSCRIPTEN_WASM_WORKERS__
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
#define USE_LOCKS 1
#endif

/* Make malloc() and free() threadsafe by securing the memory allocations with pthread mutexes. */
#if __EMSCRIPTEN_PTHREADS__
#define USE_LOCKS 1
#define USE_SPIN_LOCKS 0 // Ensure we use pthread_mutex_t.
#endif

Expand Down
21 changes: 9 additions & 12 deletions system/lib/libc/musl/src/errno/__errno_location.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#include <errno.h>
#include "pthread_impl.h"

#if __EMSCRIPTEN_PTHREADS__
// for pthreads, use the proper location on the thread info, so each
// thread has its own errno
int *__errno_location(void)
{
return &__pthread_self()->errno_val;
}
#else
// for single-threaded mode, avoid linking in pthreads support code
// just for this
static int __errno_storage = 0;
#if __EMSCRIPTEN__
// For emscripten we use TLS here instead of `__pthread_self`, so that in single
// threaded builds this gets lowered away to normal global variable.
static _Thread_local int __errno_storage = 0;
#endif

int *__errno_location(void)
{
#if __EMSCRIPTEN__
return &__errno_storage;
}
#else
return &__pthread_self()->errno_val;
#endif
}

weak_alias(__errno_location, ___errno_location);
64 changes: 64 additions & 0 deletions system/lib/wasm_worker/wasm_worker_initialize.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.extern __stack_pointer
.extern __stack_base
.extern __stack_end

#ifdef __wasm64__
#define PTR i64
#define ALIGN 3
#define PTRSTORE .int64
#else
#define PTR i32
#define ALIGN 2
#define PTRSTORE .int32
#endif

.globaltype __stack_pointer, PTR
.globaltype __stack_end, PTR
.globaltype __stack_base, PTR

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

// __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

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

// 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)
// See https://github.com/emscripten-core/emscripten/issues/16496
// global.get __stack_base
// global.get __stack_end
// .functype __set_stack_limits (PTR, PTR) -> ()
// call __set_stack_limits

// __wasm_init_tls(stackLowestAddress);
local.get 0
.functype __wasm_init_tls (PTR) -> ()
call __wasm_init_tls

// N.b. The function __wasm_init_tls above does not need
// __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
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": 1850,
"a.wasm.gz": 1050,
"total": 3248,
"total_gz": 2016
"a.wasm": 1894,
"a.wasm.gz": 1077,
"total": 3292,
"total_gz": 2043
}
2 changes: 2 additions & 0 deletions test/core/test_dlfcn_self.exports
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ __progname_full
__sig_actions
__sig_pending
__signgam
__stack_base
__stack_chk_guard
__stack_end
__threwValue
__timezone
__tzname
Expand Down
2 changes: 1 addition & 1 deletion test/core/test_em_asm_unicode.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hello world…
hello world…
2 changes: 1 addition & 1 deletion test/core/test_longjmp.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
first
result: 1 1
second
result: 2 -1
result: 2 -1
14 changes: 7 additions & 7 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3050,8 +3050,6 @@ def test_sdl2_gl_read(self):
# SDL, OpenGL, readPixels
self.btest_exit('test_sdl2_gl_read.c', args=['-sUSE_SDL=2'])

@no_4gb('https://github.com/libsdl-org/SDL/issues/9052')
@no_2gb('https://github.com/libsdl-org/SDL/issues/9052')
@requires_graphics_hardware
def test_sdl2_glmatrixmode_texture(self):
self.reftest('test_sdl2_glmatrixmode_texture.c', 'test_sdl2_glmatrixmode_texture.png',
Expand Down Expand Up @@ -3105,8 +3103,6 @@ def test_sdl2_fog_linear(self):
def test_sdl2_unwasteful(self):
self.btest_exit('test_sdl2_unwasteful.c', args=['-sUSE_SDL=2', '-O1'])

@no_2gb('https://github.com/libsdl-org/SDL/issues/9052')
@no_4gb('https://github.com/libsdl-org/SDL/issues/9052')
def test_sdl2_canvas_write(self):
self.btest_exit('test_sdl2_canvas_write.c', args=['-sUSE_SDL=2'])

Expand Down Expand Up @@ -5011,9 +5007,13 @@ def test_wasm_worker_futex_wait(self, args):

# Tests Wasm Worker thread stack setup
@also_with_minimal_runtime
def test_wasm_worker_thread_stack(self):
for mode in (0, 1, 2):
self.btest('wasm_worker/thread_stack.c', expected='0', args=['-sWASM_WORKERS', f'-sSTACK_OVERFLOW_CHECK={mode}'])
@parameterized({
'0': (0,),
'1': (1,),
'2': (2,),
})
def test_wasm_worker_thread_stack(self, mode):
self.btest('wasm_worker/thread_stack.c', expected='0', args=['-sWASM_WORKERS', f'-sSTACK_OVERFLOW_CHECK={mode}'])

# Tests emscripten_malloc_wasm_worker() and emscripten_current_thread_is_wasm_worker() functions
@also_with_minimal_runtime
Expand Down
9 changes: 3 additions & 6 deletions test/wasm_worker/c11__Thread_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

_Thread_local int __attribute__((aligned(64))) tls = 1;

void main_thread_func()
{
void main_thread_func() {
assert(!emscripten_current_thread_is_wasm_worker());
EM_ASM(out($0), tls);
#ifdef REPORT_RESULT
REPORT_RESULT(tls);
#endif
}

void worker_main()
{
void worker_main() {
assert(emscripten_current_thread_is_wasm_worker());
assert(((intptr_t)&tls % 64) == 0);
assert(tls != 42);
Expand All @@ -27,8 +25,7 @@ void worker_main()

char stack[1024];

int main()
{
int main() {
EM_ASM(out($0), tls);
assert(((intptr_t)&tls % 64) == 0);
assert(!emscripten_current_thread_is_wasm_worker());
Expand Down
9 changes: 3 additions & 6 deletions test/wasm_worker/cancel_all_wait_asyncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@ volatile int32_t addr = 1;

bool testSucceeded = 1;

void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
{
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
testSucceeded = 0;
assert(0); // We should not reach here
}

void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
{
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
#ifdef REPORT_RESULT
REPORT_RESULT(testSucceeded);
#endif
}

int main()
{
int main() {
emscripten_console_log("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));
Expand Down
9 changes: 3 additions & 6 deletions test/wasm_worker/cancel_all_wait_asyncs_at_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@ volatile int32_t addr = 1;

bool testSucceeded = 1;

void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
{
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
testSucceeded = 0;
assert(0); // We should not reach here
}

void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
{
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
#ifdef REPORT_RESULT
REPORT_RESULT(testSucceeded);
#endif
}

int main()
{
int main() {
emscripten_console_log("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));
Expand Down
Loading

0 comments on commit 00c79dd

Please sign in to comment.