[PATCH] D109722: [lld][WebAssembly] Relax limitations on multithreaded instantiation

Thomas Lively via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 13 13:58:55 PDT 2021


tlively created this revision.
tlively added reviewers: sbc100, dschuff.
Herald added subscribers: wingo, ecnelises, sunfish, jgravelle-google.
tlively requested review of this revision.
Herald added subscribers: llvm-commits, aheejin.
Herald added a project: LLVM.

For multithreaded modules (i.e. modules with a shared memory), lld injects a
synthetic Wasm start function that is automatically called during instantiation
to initialize memory from passive data segments. Even though the module will be
instantiated separately on each thread, memory initialization should happen only
once. Furthermore, memory initialization should be finished by the time each
thread finishes instantiation. Since multiple threads may be instantiating their
modules at the same time, the synthetic function must synchronize them.

The current synchronization tries to atomically increment a flag from 0 to 1 in
memory then enters one of two cases. First, if the increment was successful, the
current thread is responsible for initializing memory. It does so, increments
the flag to 2 to signify that memory has been initialized, then notifies all
threads waiting on the flag. Otherwise, the thread atomically waits on the flag
with an expected value of 1 until memory has been initialized. Either the
initializer thread finishes initializing memory (i.e. sets the flag to 2) first
and the waiter threads do not end up blocking, or the waiter threads succesfully
start waiting before memory is initialized so they will be woken by the
initializer thread once it has finished.

One complication with this scheme is that there are various contexts on the Web,
most notably on the main browser thread, that cannot successfully execute a
wait. Executing a wait in these contexts causes a trap, and in this case would
cause instantiation to fail. The embedder must therefore ensure that these
contexts win the race and become responsible for initializing memory, since that
is the only code path that does not execute a wait.

Unfortunately, since only one thread can win the race and initialize memory,
this scheme makes it impossible to have multiple threads in contexts that cannot
wait. For example, it is not currently possible to instantiate the module on
both the main browser thread as well as in an AudioWorklet. To loosen this
restriction, this commit inserts an extra check so that the wait will not be
executed at all when memory has already been initialized, i.e. when the flag
value is 2. After this change, the module can be instantiated on threads in
non-waiting contexts as long as the embedder can guarantee either that the
thread will win the race and initialize memory (as before) or that memory has
already been initialized when instantiation begins. Threads in contexts that can
wait can continue racing to initialize memory.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109722

Files:
  lld/test/wasm/data-segments.ll
  lld/wasm/Writer.cpp
  llvm/include/llvm/BinaryFormat/Wasm.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109722.372339.patch
Type: text/x-patch
Size: 7625 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210913/727f3cf1/attachment.bin>


More information about the llvm-commits mailing list