WIP: Support for mutable global imports/exports#1644
WIP: Support for mutable global imports/exports#1644jayphelps wants to merge 2 commits intoWebAssembly:masterfrom
Conversation
| ExternalKind kind; | ||
| Name functionType; // for Function imports | ||
| Type globalType; // for Global imports | ||
| bool mutable_; // for Global imports |
There was a problem hiding this comment.
This feels dirty but seemed like a simple solution without deciding to completely rethink about imports/globals are represented
There was a problem hiding this comment.
Seems reasonable to me. Might be nice to eventually think about a union or other cleaner approach, but Imports are not that common, so I'm not worried about it.
kripken
left a comment
There was a problem hiding this comment.
Thanks! Good start, everything looks correct to me aside from the one comment below on reading of SetGlobals.
| return; | ||
| } | ||
| throwError("bad set_global"); | ||
| curr->finalize(); |
There was a problem hiding this comment.
I may be missing something, but I don't think the code added to this function is needed - the type of SetGlobal is always none, as it does not return a value.
There was a problem hiding this comment.
You're not missing something, I was! You are correct and it actually explains why some codegen around the tests were adding in a drop. Great catch.
|
Otherwise, we have a Other things we'll need here eventually:
|
|
Oh, and about tests: For this feature, we might want to add the spec tests, but we haven't in a while and I'm not sure it's worth the effort. Otherwise, can add |
jayphelps
left a comment
There was a problem hiding this comment.
Added a test, and some fixes that the test revealed, however it also caused some other tests to change to mutable and it's not clear why. I'll continue digging but if you happen to know immediately why lmk.
| o << "(mut " << printType(curr->globalType) << ")"; | ||
| } else { | ||
| o << printType(curr->globalType) << ' '; | ||
| o << printType(curr->globalType); |
There was a problem hiding this comment.
The test revealed that I had incorrectly added spaces here
| im->kind = ExternalKind::Global; | ||
| im->globalType = type; | ||
| if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col); | ||
| im->mutable_ = mutable_; |
There was a problem hiding this comment.
testing revealed I missed adding the support to this file.
| (import "env" "table" (table 0 0 anyfunc)) | ||
| (import "env" "memoryBase" (global $memoryBase i32)) | ||
| (import "env" "tableBase" (global $tableBase i32)) | ||
| (import "env" "memoryBase" (global $memoryBase (mut i32))) |
There was a problem hiding this comment.
@kripken It's not clear why all these changed to mutable. Perhaps this is revealing a bug I introduced?
There was a problem hiding this comment.
Oh! Is it because those asmjs globals were actually mutable but previously we couldn’t represent that?
There was a problem hiding this comment.
Interesting - I think that's a coincidence actually, but a good one here as it pointed out something useful :) What I think is going on is that we forgot an initial value for bool mutable_; (unlike the other fields near it, it doesn't have a default value) - we should set that to bool mutable_ = false;.
It's also a good question what we should do for asm.js imports. Yeah, in asm.js they are mutable, and to work around that in wasm we created a mutable global with the initial value of the import, then we just use that global from then on. In theory we could simplify that to use a mutable import, however, I think it would be more complicated than it would be worth since we do want the old form supported too (and also a mutable import may have perf overhead).
|
Fixed |
You could use |
|
@binji thanks for the tip--I really do appreciate it. Unfortunately wat-desugar strips comments and also removes some extraneous code that are part of the test (like deeply nested blocks). But lmk if I'm misunderstanding! |
|
Yes, it will remove comments (it'd be nice if the wabt parser kept them around for tools like this). It shouldn't remove blocks though; if you have a repro case I'd like to fix that. But yeah, you can't just run it directly, but you could use it to help fold code rather than doing it by hand. |
turns out I was looking at the git diff incorrectly. What actually happened is that it put the export outside instead of inline--my eye saw the export "deep" and thought the code was removed. Sorry about that! |
Oh, there should be an option for that, but it looks like it's only in |
Ah yes, I was worried about that. It might not be worth the effort - I think it would be reasonable instead to just get the relevant mutable global tests directly. Specifically, we can replace the git submodule with just checked-in files (since we've diverged from upstream anyhow) of the current state, then add that one file (assuming it's in a new file). |
|
Do you still want to land this? I'd would like to see mutable global support in binaryen. |
This picks up from #1644 and indeed borrows the test case from there.
This picks up from #1644 and indeed borrows the test case from there.
This picks up from #1644 and indeed borrows the test case from there.
|
I think this should be covered now by #1785 and |
|
Thanks @sbc100! I had ran into a blocker with the spec test suite but if it passed for you it must have since been resolved. Woot woot! |
Wanted to open the conversation around adding support for mutable global imports/exports so I made what I think are most of the necessary changes--I'm probably missing some parts in one of the tools.
I'm also ran into some issues with check.py and auto_update_tests.py on MacOS but we can discuss those in #1185
No worries if this PR isn't merged, I mostly did the work to unblock myself for now 🌮