Skip to content

Update wasm2asm to generate "almost asm" if grow_memory is used #1340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2018

Conversation

krisselden
Copy link
Contributor

@krisselden krisselden commented Dec 19, 2017

Emits "almost asm" if grow_memory, current_memory or export memory is used.

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Do we need a flag here? Perhaps if memory growth is used, we can automatically do this and emit almost asm?

}

return {
memory: Object.create(Object.prototype, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this Object stuff needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because buffer needs to be a getter over the buffer var so we are exporting a living binding and Object.create supports an array of descriptors.

@krisselden
Copy link
Contributor Author

I will refactor to just automatically do it based on the presence of grow_memory and I'll fix the python linting rules.

I used Object.create() because it is how you create with descriptors and need the buffer to be a getter so it reflects the replacement when memory grows. I could use the new es2015 shorthand instead get buffer() { return buffer; } but not sure if the cashew ast supported that. I could also assign memory as a var, and just ensure memory.buffer = buffer is part of replacement, but this way I don't create the memory object unless it is exported.

@krisselden krisselden changed the title Add --allow-memory-growth to wasm2asm to generate "almost asm" js Update wasm2asm to generate "almost asm" if grow_memory is used Dec 28, 2017
@wycats
Copy link

wycats commented Dec 28, 2017

This would be great to have for Glimmer, where we're targeting wasm but still need asm.js output for older browsers (we still support IE11, for now).

src/wasm2asm.h Outdated
@@ -205,11 +205,14 @@ class Wasm2AsmBuilder {
// All our function tables have the same size TODO: optimize?
size_t tableSize;

bool almostASM;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializing this to false could be done here, in the definition.

src/wasm2asm.h Outdated
);
}
if (export_->kind == ExternalKind::Memory) {
almostASM = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should depend on the presence of a memory export - maybe I'm missing something?

I think it should happen of growth is used, as I see later in the patch, and also the memory has an initial and a max value; if the max is larger than the initial, then growth is possible (from outside - maybe we don't care about that?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think asm.js exports can be anything other than functions.

http://asmjs.org/spec/latest/#validateexport-s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is enforcing the maximum growth important for a first pass? The primary motivation is getting backward compatibility when targeting wasm first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean now, yeah, asm.js doesn't allow exporting the memory. However, I think we should just not export the memory - asm.js always imports (and never exports) the memory, no matter what wasm does, so that is always going to be different.

About enforcing the maximum growth, I agree, we can leave that for later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The export shims the WebAssembly.Memory interface and is also the only way to get the reassigned buffer post memory growth, the imported buffer is only the initial buffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks, then I agree we need to export it and disable "use asm".

However, can we not do this if memory growth is disabled? I think it may be common to have a non-memory growth wasm module that exports a memory, and it would be better to not export it so that we can keep "use asm" for speed. But, a downside of that is we do something different depending on memory growth. Perhaps we can issue a warning, and/or have an option to ignore a memory export?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting memory allows for memory growth externally, via the grow function even if the module itself doesn't have grow_memory instructions. I will log a message when "almost asm" is triggered with the reason why.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm excited for this!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this looks great! All ready to merge I think, except for the logging just mentioned, which I don't think I see yet?

If grow_memory, current_memory or export memory is
used then generate "almost asm" with memory growth
support.
@kripken
Copy link
Member

kripken commented Jan 5, 2018

Thanks, looks good. Before we merge this, please join the wasm w3c community group https://www.w3.org/community/webassembly/participants

@wycats
Copy link

wycats commented Jan 5, 2018

While I'm sure @krisselden is happy to fill out another form, I'm curious @kripken: is there a reason that contributions to binaryen require joining a community group?

@kripken
Copy link
Member

kripken commented Jan 5, 2018

I don't understand it myself, but it's what the WebAssembly group wanted, and this repo is under that org. It may have something to do with the w3c, @jfbastien knows all the details.

@dschuff
Copy link
Member

dschuff commented Jan 5, 2018

The CG provides the legal protections for all technical contributions to WebAssembly and its projects. Most notably patent protection: see https://www.w3.org/community/about/agreements/summary/ for an overview and https://www.w3.org/community/about/agreements/cla/ for details.

@wycats
Copy link

wycats commented Jan 5, 2018

Is there some reason that this tool needs to live in this org and be under the W3C IPR policy (as opposed to a standalone repository licensed under Apache2?)

Are any of the active contributors to this repository only active because of the fact that the repository is part of what WebAssembly umbrella and protected (specifically) by W3C IPR?

@jfbastien
Copy link
Member

@wycats sorry for missing this discussion. The title made me mark-as-read. Please consider forking this question to its own thread instead of tacking it onto this one.

To answer your question: binaryen was started early in WebAssembly's history when much of the design was in flux, and it definitely provided significant input to what will become the final WebAssembly standard. I was one of the 4 CG chairs at the time, representing Google, and advice from representative companies' standards folks + lawyers, and W3C folks, was that WebAssembly contributions which affect the design must be under the W3C umbrella and contributors who provide significant input must be CG members. Binaryen fit that description and was therefore under the WebAssembly organization.

Now that the design has settled more we could reconsider and put binaryen as well as other tools under their own organization. For example, the LLVM work now occurs directly in upstream LLVM. It remains true that significant contributions to the standard must come from CG members, but at this point tooling development doesn't influence the design as much as just get stuff working.

If you want to float this idea I recommend filing an issue here, and adding an agenda item for the next CG video call (today's is a bit late, but I'll soon post an agenda for the next meeting, at which point a PR for this can be created). CG video calls are open to all CG members, I suggest this path because it seems like the choice of un-CG-ifying binaryen should be put to CG members.

@krisselden
Copy link
Contributor Author

Microsoft already has signed a contribution agreement (as a member of the W3C), and we believe we are covered by that signed agreement. So can we submit without signing another agreement?

@kripken
Copy link
Member

kripken commented Feb 1, 2018

ping @jfbastien for that legal question.

@kripken
Copy link
Member

kripken commented Feb 3, 2018

Well, to my understanding that should be more than sufficient. I'll merge this in Monday unless someone objects.

Thanks for your work and your patience here @krisselden!

@kripken kripken merged commit a85be97 into WebAssembly:master Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants