Adjust how TLS initialization works#820
Merged
Merged
Conversation
In reviewing various issues and TLS initialization I was personally left pretty confused. There were more hooks into TLS than I had anticipated and I found them to interact in confusing ways. Notably `__copy_tls` initializes another thread's TLS pointer but uses the `__wasm_init_tls` intrinsic which sets the current thread's TLS pointer as well. This means that there was previously a dance that was done to save/restore this as necessary. I additionally noticed that TLS initialization for WASIp3 is expected to be a bit more expensive than other platforms due to an intrinsic necessary to learn the current TID. This is something that I think would be worthwhile to take off the hot path. Finally, I felt that the assembly as part of task initialization was doing a bit too much relative to what could be done in C. Adding all these together, the changes here are: * For wasip3 coop threads, sync tasks use the "main thread" TLS setup in the `start` function. They no longer allocate their own block of TLS storage. * For wasip3 coop threads, async tasks now delegate to C to perform stack allocation. TLS is then stored at the top of the stack, and the stack top is returned to assembly to get configured. * The `__copy_tls` function is no longer used on wasip3 targets with coop threads. Task initialization after the changes above no longer used it, and spawned threads now initialize their TLS when they start. * The `__init_tp` function is now deferred as an internal detail of `__pthread_self`. This is effectively lazy initialization of `__pthread_self()`. The CRT startup objects thus no longer need this symbol for example. * When spawning a thread, assembly only initializes the stack pointer and lets C initialize TLS. * A workaround to consider `__wasi_coop_thread_start` used is removed since I believe it's not necessary as the symbol is referred to by function pointer to pass to an imported function. Throughout this I've attempted to improve comments here and there. A workaround for WebAssembly#819 is needed temporarily, too. Much of WebAssembly#810 is addressed, but the stack for async tasks is still leaked which'll need fixing.
Collaborator
Author
|
r? @TartanLlama |
TartanLlama
approved these changes
Jul 15, 2026
| // TODO(wasip3): should make this constant configurable. | ||
| size_t stack_size = get_stack_bounds().size; | ||
| if (stack_size == 0) | ||
| stack_size = 131072; |
Contributor
There was a problem hiding this comment.
Can we use __default_stacksize instead of the magic number?
Collaborator
Author
There was a problem hiding this comment.
I don't see that symbol anywhere in LLVM itself, and I wasn't actually sure that this was available. Does that work for you though?
Contributor
Collaborator
Author
There was a problem hiding this comment.
Oh wow and living in this file no less, thanks!
alexcrichton
enabled auto-merge (squash)
July 15, 2026 19:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In reviewing various issues and TLS initialization I was personally left pretty confused. There were more hooks into TLS than I had anticipated and I found them to interact in confusing ways. Notably
__copy_tlsinitializes another thread's TLS pointer but uses the__wasm_init_tlsintrinsic which sets the current thread's TLS pointer as well. This means that there was previously a dance that was done to save/restore this as necessary.I additionally noticed that TLS initialization for WASIp3 is expected to be a bit more expensive than other platforms due to an intrinsic necessary to learn the current TID. This is something that I think would be worthwhile to take off the hot path.
Finally, I felt that the assembly as part of task initialization was doing a bit too much relative to what could be done in C.
Adding all these together, the changes here are:
startfunction. They no longer allocate their own block of TLS storage.__copy_tlsfunction is no longer used on wasip3 targets with coop threads. Task initialization after the changes above no longer used it, and spawned threads now initialize their TLS when they start.__init_tpfunction is now deferred as an internal detail of__pthread_self. This is effectively lazy initialization of__pthread_self(). The CRT startup objects thus no longer need this symbol for example.__wasi_coop_thread_startused is removed since I believe it's not necessary as the symbol is referred to by function pointer to pass to an imported function.Throughout this I've attempted to improve comments here and there. A workaround for #819 is needed temporarily, too. Much of #810 is addressed, but the stack for async tasks is still leaked which'll need fixing.