This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Disallow 'shared-mem' rather than 'atomics'
ClosedPublic

Authored by tlively on May 6 2020, 7:46 PM.

Details

Summary

The WebAssembly backend automatically lowers atomic operations and TLS
to nonatomic operations and non-TLS data when either are present and
the atomics or bulk-memory features are not present, respectively. The
resulting object is no longer thread-safe, so the linker has to be
told not to allow it to be linked into a module with shared
memory. This was previously done by disallowing the 'atomics' feature,
which prevented any objct with its atomic operations or TLS removed
from being linked with any object containing atomics or TLS, and
therefore preventing it from being linked into a module with shared
memory since shared memory requires atomics.

However, as of https://github.com/WebAssembly/threads/issues/144, the
validation rules are relaxed to allow atomic operations to validate
with unshared memories, which makes it perfectly safe to link an
object with stripped atomics and TLS with another object that still
contains TLS and atomics as long as the resulting module has an
unshared memory. To allow this kind of link, this patch disallows a
pseudo-feature 'shared-mem' rather than 'atomics' to communicate to
the linker that the object is not thread-safe. This means that the
'atomics' feature is available to accurately reflect whether or not an
object has atomics enabled.

As a drive-by tweak, this change also requires that bulk-memory be
enabled in addition to atomics in order to use shared memory. This is
because initializing shared memories requires bulk-memory operations.

Diff Detail

Event Timeline

tlively created this revision.May 6 2020, 7:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2020, 7:46 PM
sbc100 accepted this revision.May 6 2020, 8:06 PM
sbc100 added inline comments.
lld/test/wasm/shared-memory-no-atomics.yaml
60

This sentence sounds a little redundant to me? The part about shared memory not being allowed. Maybe its just me ..

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
265

Why not add this to WebAssemblyFeatureKV?

This revision is now accepted and ready to land.May 6 2020, 8:06 PM

This was previously done by disallowing the 'atomics' feature, but that could lead to an awkward situation in which an object was marked as disallowing atomics when it in fact used atomics itself. Once atomic operations are allowed in modules with unshared memory, this would result in modules containing atomics but not having the 'atomics' feature in their target-features sections.

Is this possible now? When we disallow atomics now, haven't we already stripped all atomic instructions by then, or we didn't have atomics in the first place? How can a module contain atomics and disallow it at the same time?

tlively marked 2 inline comments as done.May 7 2020, 11:29 AM

This was previously done by disallowing the 'atomics' feature, but that could lead to an awkward situation in which an object was marked as disallowing atomics when it in fact used atomics itself. Once atomic operations are allowed in modules with unshared memory, this would result in modules containing atomics but not having the 'atomics' feature in their target-features sections.

Is this possible now? When we disallow atomics now, haven't we already stripped all atomic instructions by then, or we didn't have atomics in the first place? How can a module contain atomics and disallow it at the same time?

Consider compiling a module that contains TLS and has 'atomics' enabled but not 'bulk-memory'. The module's TLS and atomic operations will be lowered away because TLS requires bulk-memory. Note that intrinsics like @llvm.wasm.notify and friends are not currently lowered away, which is how you can end up with a module that has its atomics stripped but still contains atomics. But we could fix that by lowering away the notify and wait intrinsics, and then you'd be right that this is impossible.

I suppose the simpler and more important reason for this change is that it is perfectly safe to link an object with atomics with an object with stripped atomics as long as the resulting module does not use a shared memory, but this is disallowed when the stripped object has '-atomics'. I'll update the message to explain that instead.

lld/test/wasm/shared-memory-no-atomics.yaml
60

Sure, I'll remove everything after the comma and change the beginning to use the literal flag name.

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
265

WebAssemblyFeatureKV is generated from the TableGen definitions of the WebAssembly subtarget features. I didn't want to go so far as to add a real feature for this, though, since users should not be manually enabling or disabling it.

tlively edited the summary of this revision. (Show Details)May 7 2020, 11:45 AM
tlively updated this revision to Diff 262713.May 7 2020, 11:51 AM
tlively edited the summary of this revision. (Show Details)
  • Update error message
aheejin accepted this revision.May 7 2020, 7:33 PM
This revision was automatically updated to reflect the committed changes.