This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Add new `export_name` clang attribute for controlling wasm export names
ClosedPublic

Authored by sbc100 on Nov 20 2019, 5:36 PM.

Details

Summary

This is equivalent to the existing import_name and import_module
attributes which control the import names in the final wasm binary
produced by lld.

This maps the existing

This attribute currently requires a string rather than using the
symbol name for a couple of reasons:

  1. Avoid confusion with static and dynamic linking which is based on symbol name. Exporting a function from a wasm module using this directive is orthogonal to both static and dynamic linking.
  2. Avoids name mangling.

Diff Detail

Event Timeline

sbc100 created this revision.Nov 20 2019, 5:36 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptNov 20 2019, 5:36 PM

First stab at getting this attribute plumbed all the way through.

Do you think that setting this attribute should also prevent GC?

I could split this up into clang, llvm, and lld parts if it makes reviewing simpler.

It appears this doesn't handle exporting an imported function yet, which is fine for now, but it would be good to issue a warning, because wasm itself is capable of representing this:

void aaa(void) __attribute__((import_module("imp"), import_name("foo"), export_name("bar")));
lld/wasm/Writer.cpp
523

For wasm exports, it's valid to have empty strings. In fact, I may even have a usecase which wants an empty-string export. It'd be good to use Optional<> for export names, rather than special-casing the empty string.

(wasm-ld does often special-case the empty string in symbol names, but wasm export strings aren't ordinary symbol names, so it'd be good to follow wasm's rules for them.)

Do you think that setting this attribute should also prevent GC?

That sounds right to me.

I could split this up into clang, llvm, and lld parts if it makes reviewing simpler.

For me, it's easier to have it all together in one.

In this patch, export_name has a required string operand, which makes sense to me. I wonder if it also makes sense to change wasm-ld's --export command-line option to take a pair of strings, a symbol and a name to export it as.

sbc100 updated this revision to Diff 231618.Dec 1 2019, 10:40 AM
sbc100 marked an inline comment as done.
  • Add support for asm parsing

It appears this doesn't handle exporting an imported function yet, which is fine for now, but it would be good to issue a warning, because wasm itself is capable of representing this:

void aaa(void) __attribute__((import_module("imp"), import_name("foo"), export_name("bar")));

Yes, although this is supported at the bitcode level I think the internal APIs would need some refactoring to support this in BinaryFormat and lld. Thankfully I don't think the actual binary format would need to change.

lld/wasm/Writer.cpp
523

This is a fairly widespread problem within the current codebase that applies to both import and export names. If its OK with you I'll do this as a followup and at tests for empty string imports and exports. I imagine it will require fairly widespread by simple changes.

sbc100 updated this revision to Diff 231620.Dec 1 2019, 10:58 AM
  • rebase
sbc100 added a comment.Dec 6 2019, 5:32 PM

Ping .. I think this is good to go now.

dschuff accepted this revision.Dec 9 2019, 5:05 PM
dschuff added inline comments.
lld/test/wasm/export-name.ll
21

does this test need to verify that the memory and _start are exported? seems like just a check for bar would be enough.

This revision is now accepted and ready to land.Dec 9 2019, 5:05 PM
sbc100 marked an inline comment as done.Dec 9 2019, 5:08 PM
sbc100 added inline comments.
lld/test/wasm/export-name.ll
21

The worry that if I just look for Name: bar it might appear in some other section too mabye?

dschuff added inline comments.Dec 9 2019, 5:29 PM
lld/test/wasm/export-name.ll
21

You could do CHECK-LABEL: Exports and then CHECK: - Name: bar and then CHECK-LABEL on the next section?

This revision was automatically updated to reflect the committed changes.
sbc100 marked an inline comment as done.