Lower @returnAddress to a constant 0 in Emscripten release builds

Emscripten currently implements `emscripten_return_address()` by calling
out into JavaScript and parsing a stack trace, which introduces
significant overhead that we would prefer to avoid in release builds.

This is especially problematic for allocators because the generic parts
of `std.mem.Allocator` make frequent use of `@returnAddress`, even
though very few allocator implementations even observe the return
address, which makes allocators nigh unusable for performance-critical
applications like games if the compiler is unable to devirtualize the
allocator calls.
This commit is contained in:
Carl Åstholm
2025-03-23 13:12:45 +01:00
committed by Andrew Kelley
parent 9f235a105b
commit f45f9649e3
3 changed files with 11 additions and 4 deletions

View File

@@ -183,9 +183,11 @@ pub const sys_can_stack_trace = switch (builtin.cpu.arch) {
// `@returnAddress()` in LLVM 10 gives
// "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address".
// On Emscripten, Zig only supports `@returnAddress()` in debug builds
// because Emscripten's implementation is very slow.
.wasm32,
.wasm64,
=> native_os == .emscripten,
=> native_os == .emscripten and builtin.mode == .Debug,
// `@returnAddress()` is unsupported in LLVM 13.
.bpfel,