Skip to content
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

Flesh out emscripten metadata #8519

Merged
merged 3 commits into from
May 2, 2019

Conversation

rianhunter
Copy link
Contributor

@rianhunter rianhunter commented Apr 30, 2019

DYNAMICTOP_PTR and tempDoublePtr are no longer guaranteed to be
computable from STATIC_BUMP. Also GLOBAL_BASE is not always 1024.
Pass these through emscripten metadata for third-party runtimes
that consume Emscripten-generated WASM binaries.

DYNAMICTOP_PTR and tempDoublePtr are no longer deterministically
computable from STATIC_BUMP. Also GLOBAL_BASE is not always 1024.
Pass these through emscripten metadata for third-party runtimes
that consume Emscripten-generated WASM binaries.
tools/shared.py Outdated
# NB: more data can be appended here as long as you increase
# the EMSCRIPTEN_METADATA_MINOR

b''
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this 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.

Just for symmetry purposes with the preceding lines (each ending with +). Fine to take out at the small expense of potentially incurring unnecessary line diffs in future edits of this function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah I see. How about using contents = b''.join(... , then you can use trailing commas on each line?

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 could do that but then I'd have to change more lines of code :) (see this comment for past comment on keeping code style consistent: #7815 (comment)) I have no problem doing it if you really think it's worth it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, but if you are going to leave the multiline + but in that case I would drop the extra b''.

Also, is there a reason why echo the lines above is separated by an empty line? This is odd, especially since its single expression.

tools/shared.py Outdated
m = re.search(r"DYNAMIC_BASE\s+=\s+(\d+)", js)
dynamic_base = int(m.group(1))
m = re.search(r"DYNAMICTOP_PTR\s+=\s+(\d+)", js)
dynamictop_ptr = int(m.group(1))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems strangely convoluted to read back in the file we just generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would require more extensive changes to force the JS compiler to use a value for DYNAMICTOP_PTR that would be chosen by Python. Right now it's automatically allocated by makeStaticAlloc(). I thought the method of parsing the JS was acceptable since it was done in earlier incarnations of this function.

tools/shared.py Outdated

WebAssembly.lebify(dynamictop_ptr) +

WebAssembly.lebify(tempdouble_ptr) +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make more sense to export these things as globals from the wasm file?

If we are going to make this an ABI can we document to exact meaning on each of these things?

Do all these make sense with both fastcomp and the llvm backend?

Copy link
Contributor Author

@rianhunter rianhunter Apr 30, 2019

Choose a reason for hiding this comment

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

The reason they are in a different metadata section is that they must be used to properly parameterize the Emscripten runtime before the WASM can be successfully instantiated. (There is a circular dependence).

Medium term this will be documented exactly. I still don't consider it stable enough to document, which is partly the reason it isn't emitted automatically and requires "-s EMIT_EMSCRIPTEN_METADATA=1" to opt in. There is some initial documentation for developers here https://github.com/emscripten-core/emscripten/wiki/WebAssembly-Standalone#jswasm-abi

At least as far as emulating the JS runtime, tempDoublePtr, DYNAMICTOP_PTR, and DYNAMIC_BASE makes sense with the LLVM backend. It wouldn't be possible for the host to implement sbrk() and copyTempDouble() without those parameters. global_base does not seem to be needed by LLVM generated WASM files (in asmjs/binaryen it's imported as __memory_base). I haven't yet successfully run LLVM generated WASM binaries but to be honest I anticipate more changes at that time.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm OK with landing this now, as long as we can iterate on this, change it, and hopefully simplify it in the future. I wouldn't want to be locked in the ad-hoc conventions we currently have without a more formal spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1, Major version on this metadata section is still 0, so it's still explicitly not supported and not guaranteed to be in any specific format.

@@ -2979,8 +2979,20 @@ def delebify(buf, offset):

@staticmethod
def add_emscripten_metadata(js_file, wasm_file):
mem_size = Settings.STATIC_BUMP
WASM_PAGE_SIZE = 65536
Copy link
Collaborator

Choose a reason for hiding this comment

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

so STATIC_BUMP is no longer 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.

STATIC_BUMP was being used to compute DYNAMICTOP_PTR and tempDoublePtr but since those are now not derived from STATIC_BUMP it's no longer necessary to store that.

mem_size = Settings.STATIC_BUMP
WASM_PAGE_SIZE = 65536

mem_size = Settings.TOTAL_MEMORY // WASM_PAGE_SIZE
Copy link
Collaborator

@sbc100 sbc100 Apr 30, 2019

Choose a reason for hiding this comment

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

Doesn't // do the wrong thing here and round down? I guess TOTAL_MEMORY is already guaranteed to be a multiple so probably fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the intent is to make sure mem_size is an integer. I could have done int(TOTAL_MEMORY / WASM_PAGE_SIZE) but this seemed better.

@sbc100 sbc100 merged commit e7d3e0f into emscripten-core:incoming May 2, 2019
@rianhunter rianhunter deleted the better_em_md branch May 2, 2019 22:45
VirtualTim pushed a commit to VirtualTim/emscripten that referenced this pull request May 21, 2019
DYNAMICTOP_PTR and tempDoublePtr are no longer deterministically
computable from STATIC_BUMP. Also GLOBAL_BASE is not always 1024.
Pass these through emscripten metadata for third-party runtimes
that consume Emscripten-generated WASM binaries.
VirtualTim added a commit to VirtualTim/emscripten that referenced this pull request May 23, 2019
VirtualTim added a commit to VirtualTim/emscripten that referenced this pull request May 23, 2019
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
DYNAMICTOP_PTR and tempDoublePtr are no longer deterministically
computable from STATIC_BUMP. Also GLOBAL_BASE is not always 1024.
Pass these through emscripten metadata for third-party runtimes
that consume Emscripten-generated WASM binaries.
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.

2 participants