This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Check features before making SjLj vars thread-local
ClosedPublic

Authored by tlively on Sep 25 2020, 10:55 AM.

Details

Summary

1c5a3c4d3823 updated the variables inserted by Emscripten SjLj lowering to be
thread-local, depending on the CoalesceFeaturesAndStripAtomics pass to downgrade
them to normal globals if the target features did not support TLS. However, this
had the unintended side effect of preventing all non-TLS-supporting objects from
being linked into modules with shared memory, because stripping TLS marks an
object as thread-unsafe. This patch fixes the problem by only making the SjLj
lowering variables thread-local if the target machine supports TLS so that it
never introduces new usage of TLS that will be stripped. Since SjLj lowering
works on Modules instead of Functions, this required that the
WebAssemblyTargetMachine have its feature string updated to reflect the
coalesced features collected from all the functions so that a
WebAssemblySubtarget can be created without using any particular function.

Diff Detail

Event Timeline

tlively created this revision.Sep 25 2020, 10:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 25 2020, 10:55 AM
tlively requested review of this revision.Sep 25 2020, 10:55 AM
dschuff added inline comments.Sep 25 2020, 11:08 AM
llvm/include/llvm/Target/TargetMachine.h
114

where does the feature string usually get initialized? just during TM construction? Is there any particular risk in allowing it to be changed after init?

tlively added inline comments.Sep 25 2020, 11:14 AM
llvm/include/llvm/Target/TargetMachine.h
114

Yes, usually just during initialization. It comes from whatever -mattr=... options are provided on the command line, but doesn't actually represent the full set of enabled features because functions can introduce additional features.

I don't think there's much risk in changing the string after creation, especially since we were already changing the feature strings on each individual function and those feature strings seem to be more widely used.

tlively updated this revision to Diff 294373.Sep 25 2020, 11:23 AM
  • Restore proper pass order. It is important to run CoalesceFeaturesAndStripAtomics before SjLj lowering so that SjLj lowering reads the correctly updated feature string.

I don't understand the LLVM side enough to review, but running this locally it fixes all the issues we had earlier!

tlively added inline comments.Sep 25 2020, 11:24 AM
llvm/include/llvm/Target/TargetMachine.h
114

This did remind me to put the passes back in the proper order so that CoalesceLocalsAndStripAtomics runs first again.

dschuff accepted this revision.Sep 25 2020, 11:32 AM
dschuff added inline comments.
llvm/include/llvm/Target/TargetMachine.h
114

Sounds good. Especially given that the string already doesn't match the used features, it sounds like nobody is really using it after init.

This revision is now accepted and ready to land.Sep 25 2020, 11:32 AM
This revision was landed with ongoing or failed builds.Sep 25 2020, 11:45 AM
This revision was automatically updated to reflect the committed changes.