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

Semantics for non-mutable globals #1466

Closed
kg opened this issue Dec 7, 2022 · 5 comments
Closed

Semantics for non-mutable globals #1466

kg opened this issue Dec 7, 2022 · 5 comments

Comments

@kg
Copy link
Contributor

kg commented Dec 7, 2022

From reading over the spec, it's not clear to me whether runtimes would be permitted to turn 'global.get' on a readonly global into an 'i32.const'. It doesn't seem like v8 applies this optimization either, so importing readonly globals as a way to approximate relocations/reconfiguration seems to come with a performance tax. Is this something the spec is meant to allow, or is it intended that a readonly global can change?

@rossberg
Copy link
Member

rossberg commented Dec 7, 2022

The spec only defines observational behaviour, an engine can do whatever pleases it, as long as it does not change that behaviour.

The reason why engines cannot generally replace global.get with const for imports is the compilation/instantiation model: it simply doesn't know the value at compilation time, it's a parameter only supplied later by instantiations (and it can vary across instantiations). So yes, there's a cost to parameterisation.

@kg
Copy link
Contributor Author

kg commented Dec 7, 2022

Right, but is the spec intended to specify that it shouldn't be possible to observe changes to the value? It seems to only specify that the wasm module is prohibited from modifying it. Whether or not the value is allowed to be changed by the host/engine would change which optimizations are valid around readonly globals.

@rossberg
Copy link
Member

rossberg commented Dec 7, 2022

The spec imposes constraints on what the host can do:

  • The host is expected to only interact with Wasm and its store through the embedder interface.
  • In particular, the host must use a valid store for all operations.
  • A host function call can only modify the store in certain ways. Concretely, the store must remain valid and it must be an extension of the existing store. The latter definition forbids mutation of immutable globals.

Of course, when a host bypasses the embedder interface and messes with the store directly, then all bets about transition consistency are off. In particular, there is nothing preventing it from using a valid store that it just made up, i.e., one that is not an extension of a previous store. Strictly speaking, it could "mutate" immutable globals between calls into Wasm that way. But of course, the spec cannot say anything about implementations not following the spec. :)

@kg
Copy link
Contributor Author

kg commented Dec 7, 2022

  • A host function call can only modify the store in certain ways. Concretely, the store must remain valid and it must be an extension of the existing store. The latter definition forbids mutation of immutable globals.

Thanks, that's the bit I was missing about the definition of a valid store! This at least means it would be possible to ask individual vendors for these optimizations, even if it turns out they can't provide them for other reasons.

@sunfishcode
Copy link
Member

The question here seems answered; please file new issues if there are any further questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants