Stream: rust-toolchain

Topic: Thread-local storage


view this post on Zulip Josh Triplett (Jun 16 2022 at 17:06):

@Dan Gohman In another thread you mentioned thread-local storage.

Thinking about that, on the one hand it would be possible for the code inside the sandbox to handle that completely itself if it allocates thread indexes and reserves memory for TLS. On the other hand, given support for multiple linear memories, it'd be fun if each thread could simply have its own small linear memory in addition to the main shared linear memory. Then it'd be straightforward for each thread to have a "TLS memory".

view this post on Zulip Dan Gohman (Jun 16 2022 at 17:07):

One complication with that other hand is that linear memories are not first-class. Each load and store has to decide which memory it's referencing in a static immediate field.

view this post on Zulip Dan Gohman (Jun 16 2022 at 17:08):

That would preclude taking the address of a thread-local variable and doing anything non-local with it.

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:08):

Ah, I forgot it had to be an immediate.

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:09):

(If we had a mechanism by which you could have a linear memory that is actually overlapping with the main shared linear memory, that could potentially help with that.)

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:09):

That aside, would something go horribly wrong if there were a way to have a first-class linear memory object?

view this post on Zulip Dan Gohman (Jun 16 2022 at 17:13):

Linear memories being non-first-class allows JITs to generate specialized code in several situations.

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:13):

Fair.

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:13):

I think we could do this without first-class linear memories though.

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:15):

Declare a linear memory that consists of a reference to a range of an existing linear memory, and when starting a thread have the ability to say "when this thread references memory N that's this new linear memory".

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:16):

Then if you want to reference TLS as a generic pointer you translate to the shared linear memory.

view this post on Zulip Josh Triplett (Jun 16 2022 at 17:20):

All that aside, in the absence of first-class linear memory, it might well be easier and more efficient to just give threads a base address in the main linear memory.

view this post on Zulip Dan Gohman (Jun 16 2022 at 18:55):

In the instance-per-thread model, this is done with wasm globals, which are per-instance and thus per-thread. In the multiple-threads-in-a-single-instance model, this is expected to be done by having a flag which indicates which globals are thread-local.

view this post on Zulip Dan Gohman (Jun 16 2022 at 18:56):

Thread-local wasm globals act much like "tls registers" in many platform ABIs. They're basically per-thread pointers.

view this post on Zulip Dan Gohman (Jun 16 2022 at 18:57):

That allows them to point to a thread's TLS data in a shared address space, which is a simpler model than threads having different base addresses in the address space.


Last updated: Nov 22 2024 at 17:03 UTC