-
Notifications
You must be signed in to change notification settings - Fork 74
Allow immutable fields in struct.new_default_with_rtt #174
Comments
Yes, this can be helpful for my Java implementation. It can be reduce a lot of code. |
… on instance initialization to allow the assignment of the vtable. struct.new_default_with_rtt does not allow to initialize immutable fields to values other than null. In our model the vtable (or type information block) needs to be immutable to satisfy the wasm typing model. There is a proposal to allow struct.new_default_with_rtt receive the values for all the immutable fields: WebAssembly/gc#174 PiperOrigin-RevId: 354404824
If code size is the primary concern here, we might want to consider explicit inlining here, such as @kripken's suggestion in WebAssembly/design#1375 (comment). The reason is that inlining also addresses a related classic problem in modules at separate compilation. If you consider a Java class in some separately compiled module, it cannot inline the initialization code for its super classes (including |
I see how this can be useful, though my gut reaction would be to consider this a post-MVP addition, since it is a local optimisation that doesn't add crucial functionality. And it is not immediately obvious how to derive the selection of non-defaulted fields (all immutables plus all non-defaults? or something more customisable? either way, it would be a rather complex instruction). |
In my Java compiler it would only relevant for the vtable currently which is an int32. Instead a simple:
I need the follow code:
This are 3 extra operation and an extra local for every struct allocation and the vtable is not immutable. I have also evaluate to push first all default values. But with a medium of 10 fields per structs the overhead is much larger. |
Right, but keep in mind that Wasm is an assembly language, so this is to be expected. Wasm code mostly mirrors what native code would have to do as well. We have so far resisted adding "macro instructions" to Wasm, unless they have common counterparts in actual hardware or cannot be avoided for other reasons. (Arguably, the existing |
@Horcrux7 In the MVP doc, it suggests that v-tables be implemented using a struct, but you're using a numeric index. Could you give some background as to why? |
There are multiple reasons why I use an i32 currently.
But I will evaluate the performance of a vtable with a struct later. It will be interesting. |
@rossberg Wasm is extremely limit comparing to a assembly language. For example In assembly I can fill allocated memory of a struct with zero bytes. |
That all makes sense. My concern is that using an |
@manoskouk, do you think this is still worth discussing for inclusion in the MVP or should we close this issue? We can also add it as a post-MVP feature idea in the post-MVP doc. |
We can definitely postpone this to post-MVP. |
Even defaultable types sometimes want to be initialized to a non-zero value For example, the following statement in C#: class CustomDefault
{
public int Field = 42;
} So can we declare the default typed value when |
@oovm, your compiler will have to generate that initialisation code, like any regular compiler targeting hardware. Defaults are not intended as a convenience mechanism to replace such code. The only reason they exist is to guarantee a well-defined semantics for Wasm (since it isn't always possible to check initialisation efficiently). |
I understand, so is it possible to have instructions with semantics similar to the following: i32.const 1 ;; overrride defaultable
table.idx funcref ;; fill undefaultable
struct.new_default_with_field $type $field1 $field2 ;; fill other unindexed defaultable types Under If I use |
Well, sort of, by using Also, I'm not sure what you mean by readonly semantics. The Wasm data representation used by a compiler does not need to reflect read/write constraints of the source language (if you were to use linear memory, everything would be mutable as well). The only reason Wasm has immutable fields at all is that they permit more subtyping. But unless the source language you're compiling allows covariant subtyping on these fields, your compiler can simply define them as mutable internally. |
It would be useful to be able to initialize structs with default fields even if there are immutable fields. In that case, the immutable fields would need to be provided as arguments to
struct.new_default_with_rtt
.As a use case, consider a Java object: it will always have an immutable field pointing to the virtual method table, but it also needs to have its fields initialized to their default value at allocation (as is dictated by Java semantics).
The text was updated successfully, but these errors were encountered: