You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add type to getglobal/setglobal/getlocal/setlocal opcodes #311
In the spec repo's reference implementation, these opcodes don't specify which type of local or global it is accessing. That matches what's described by AstSemantics, but every other typed operation includes the type in the opcode, so it would be nice to eliminate this one exception.
I'm not sure what problem that would solve. Every local has exactly one type so there is already a map from local index -> type. Why bake that into the opcode too? Either way, the validator has to maintain this map and still perform checks at usage sites.
Besides, this will result in an explosion of opcodes when we add more local types, since structs and function pointers will allow an unbounded number of new local types.
That seems like it also applies to eq and any other operations that are valid on the new local types. Just == and !=, I think, but maybe I'm overlooking some others.
It doesn't change functionality, it just makes things more uniform. You still need to maintain a map still to verify type, but it separates the verification into two parts:
does this expression evaluate to the type demanded by its context?
does the variable identified by this get/set have the expected type?
These are the only opcodes in the text format that need to look at their operands to determine whether they match the type demanded by the context. As far as I've gotten in my implementation, the result of every other expression can be determined solely from their opcode. I think that's the right choice for the text format, because it localizes type errors to exactly where they are occur.
The binary encoding goes the other direction and makes the interpretation of opcodes entirely dependent on the type demanded by the context. I also think that's the right choice for the binary encoding, since we don't need to worry about encoding ill-typed programs. That means the binary encoding already has a type-specific encoding of these operations; this suggestion is purely about the text format syntax.
You can see an example of the type-specific binary encoding in the polyfill prototype encoding (12). The exception there is setlocal/setglobal statements that derive the type from the index, but binary WebAssembly no longer has statements so it will have to use the type-specific expression opcodes for these operations.
Either way, the validator has to maintain this map and still perform checks at usage sites.
In this case there is no map, right? getlocal.i32 0 would get you offset 0 from the int32 local space. And getlocal.f32 0 would read from the float32 local space. So you would only need to do a bound check to ensure your access is < local_count.type.
The explosion of opcodes when we add more types is definitely a good point worth considering, and if we are going to take the axe to this concept, it seems there are a lot of other opcodes to consider too.
@kg That seems like it also applies to eq and any other operations that are valid on the new local types. Just == and !=, I think, but maybe I'm overlooking some others.
The comparison operations are a bit different because they always yield an I32 (or bool if that change is made). For comparisons the text format opcode includes the type of the operands, but the binary format may want to use a single opcode for each comparison operation, and also save the operand type.
@titzer Besides, this will result in an explosion of opcodes when we add more local types, since structs and function pointers will allow an unbounded number of new local types.
@MikeHolman The explosion of opcodes when we add more types is definitely a good point worth considering, and if we are going to take the axe to this concept, it seems there are a lot of other opcodes to consider too.
I think it makes sense to keep type-specific opcodes in the text format, but for the binary encoding it might be desirable to use "kind"-specific opcodes instead. e.g. getlocal.float, callIndirect.function, or lit.struct instead of getlocal.f64, callIndirect.f64->i32, or lit.myStruct. That would increase the scope for encoding ill-typed code, but it would still be easy to validate.
There exist two different classes of operators: type-specific ones (that
have type-specific semantics), and parametric ones (those that operate
uniformly on, and independent of, any type). I don't see a reason to
type-annotate the latter kind, neither in text nor in the binary format,
given that the type does not affect the operation.
/Andreas
On 25 August 2015 at 18:01, Andrew Scheidecker [email protected]
wrote:
The comparison operations are a bit different because they always yield an
I32 (or bool if that change is made). For comparisons the text format
opcode includes the type of the operands, but the binary format may want to
use a single opcode for each comparison operation, and also save the
operand type.
—
Reply to this email directly or view it on GitHub #311 (comment).
I don't see a reason to
type-annotate the latter kind, neither in text nor in the binary format,
given that the type does not affect the operation.
At least as described, the binary format encodes an opcode that is dependent on the type demanded of its result. Encoding parametric operations differently doesn't seem worth making an exception.
I see now that the text format also has call opcodes that omit the expected result type. Other than get, set, and call, are there any other parametric operations?
I see now that the text format also has call opcodes that omit the
expected result type. Other than get, set, and call, are there any other
parametric operations?
Yes. Considering that the S-expr format defines an expression language
right now: all of the rest. ;)
I see now that the text format also has call opcodes that omit the
expected result type. Other than get, set, and call, are there any other
parametric operations?
—
Reply to this email directly or view it on GitHub #311 (comment).
FWIW, despite proposing type-specific opcodes initially, I'm now on the fence (there is probably no size win with module local index spaces; maybe there is a tiny decode speed win b/c no need to validate result type) and I'm mostly waiting to see how they play out once we have realistic tests/prototypes, along with all the other binary encoding design questions.
FWIW v8-native-prototype has a single decode loop/switch, so type-specific
opcode spaces would probably require one more table indirection (i.e. load
bytecode, index into current type-specific table to retrieve globally
unique opcode, switch).
On Tue, Aug 25, 2015 at 6:53 PM, Luke Wagner [email protected]
wrote:
But for example somewhere you have to do a type check to see when you do add.i32 that both operands are int32, right? With typed opcode spaces, your operands are by definition int32 so you don't need that check. And that check is at least another indirection + (branch predicted) jump.
Yes, you have to check the type for getlocal/setlocal, but that's for those
bytecodes, not for every single bytecode.
On Tue, Aug 25, 2015 at 7:28 PM, Michael Holman [email protected]
wrote:
But for example somewhere you have to do a type check to see when you do
add.i32 that both operands are int32, right? With typed opcode spaces, your
operands are by definition int32 so you don't need that check. And that
check is at least another indirection + (branch predicted) jump.
—
Reply to this email directly or view it on GitHub #311 (comment).
From the opcode distribution I've heard Luke bring up, getlocal/setlocal may as well be the only bytecode (they make up something like 30-40% of ASTs IIRC). But why don't you need a type check for other bytecodes? Surely when you do add.i32, you need to make sure your child nodes produced i32?
On Tue, Aug 25, 2015 at 8:00 PM, Michael Holman [email protected]
wrote:
From the opcode distribution I've heard Luke bring up, getlocal/setlocal
may as well be the only bytecode (they make up something like 30-40% of
ASTs IIRC). But why don't you need a type check for other bytecodes? Surely
when you do add.i32, you need to make sure your child nodes produced i32?
Yep. The result type is part of the expression stack that is maintained
during the iterative decoding. That stack also contains the compiler IR
nodes that represent exactly what has been built so far during decoding. If
you're not building a graph and just validating, the stack can contain just
local types for checking.
—
Reply to this email directly or view it on GitHub #311 (comment).
With these definitions this would be encoded as 0110001
Note that this sort of encoding has the convenient effect of making it such that it impossible to encode an AST that will fail type checks, presumably resulting in faster validation.
For example, you couldn't encode something like this:
It would still encode to 0110001. Of course depending on layout of opcode spaces it is still possible that it would encode to some invalid AST e.g. having wrong number of child nodes, but it could not ever fail type checks, at least as far as I can tell.
There is a wider philosophical issue here underlying this question. One view is that a single opcode space makes a bytecode's meaning context-independent; we can assign meaning to the expression rooted at any particular bytecode independent of the type context in which it's used. That might be useful to find more trees to de-duplicate in layer 1.
But I think the more important question is how do we view encoding mistakes? E.g. with a single opcode space an encoding mistake or bug is more likely to produce a validation error (because types don't match) rather than decoding to valid but unintended code.
The other design pressure is opcode space extensibility. In the limit, if we define a format that gives meaning to every sequence of bits, there are no decode errors and no validation needs to be done. By the same reasoning then there is no space for extensibility, since every sequence of bits already has a meaning.
But I think the more important question is how do we view encoding mistakes?
Considering that wasm will generally be machine produced (especially binary encoding), defending against mistakes should not be a real consideration. Just as the javascript spec doesn't concern itself with bad codegen bugs from js runtimes, we shouldn't concern ourselves with bad codegen from wasm producers (e.g. LLVM).
The other design pressure is opcode space extensibility.
Maybe I misunderstand, but I don't see why this would be an issue. Assuming module local index spaces, each sequence of bits only has meaning within its module, so we can continue to add new types, etc. without any problems.
On Tue, Aug 25, 2015 at 9:20 PM, Michael Holman [email protected]
wrote:
But I think the more important question is how do we view encoding
mistakes?
Considering that wasm will generally be machine produced (especially
binary encoding), defending against mistakes should not be a real
consideration. Just as the javascript spec doesn't concern itself with bad
codegen bugs from js runtimes, we shouldn't concern ourselves with bad
codegen from wasm producers (e.g. LLVM).
Like I said, philosophy. WASM operates in the internet universe where we
should be by default mistrustful and should validate all inputs. I think
it's highly likely that once binary formats hit the airwaves that many
producers are going to try to produce the bits themselves without
round-tripping through s-expressions and a tool that encodes the bits for
them, especially in a JIT scenario. Debugging a misinterpreted bitstream is
harder than one that misvalidates.
The other design pressure is opcode space extensibility.
Maybe I misunderstand, but I don't see why this would be an issue.
Assuming module local index spaces, each sequence of bits only has meaning
within its module, so we can continue to add new types, etc. without any
problems.
—
Reply to this email directly or view it on GitHub #311 (comment).
Sure, it's a philosophical difference, type validation is a matter of convenience for wasm producers, and my opinion is that isn't important concern for us. There is no one who is going to hand encode wasm binary code. So whatever tool is converting your input (whether wasm text, c++, whatever) to binary should have assertions and tests to make sure that they produce correct wasm, and these types of encoding bugs could easily be caught by such testing. The tools outputting wasm binary will probably want to do type checking of their own and give user-friendly error messages if they detect an incongruity, but that does not have to be our domain.
Activity
titzer commentedon Aug 25, 2015
I'm not sure what problem that would solve. Every local has exactly one type so there is already a map from local index -> type. Why bake that into the opcode too? Either way, the validator has to maintain this map and still perform checks at usage sites.
Besides, this will result in an explosion of opcodes when we add more local types, since structs and function pointers will allow an unbounded number of new local types.
kg commentedon Aug 25, 2015
That seems like it also applies to eq and any other operations that are valid on the new local types. Just == and !=, I think, but maybe I'm overlooking some others.
AndrewScheidecker commentedon Aug 25, 2015
It doesn't change functionality, it just makes things more uniform. You still need to maintain a map still to verify type, but it separates the verification into two parts:
These are the only opcodes in the text format that need to look at their operands to determine whether they match the type demanded by the context. As far as I've gotten in my implementation, the result of every other expression can be determined solely from their opcode. I think that's the right choice for the text format, because it localizes type errors to exactly where they are occur.
The binary encoding goes the other direction and makes the interpretation of opcodes entirely dependent on the type demanded by the context. I also think that's the right choice for the binary encoding, since we don't need to worry about encoding ill-typed programs. That means the binary encoding already has a type-specific encoding of these operations; this suggestion is purely about the text format syntax.
You can see an example of the type-specific binary encoding in the polyfill prototype encoding (1 2). The exception there is setlocal/setglobal statements that derive the type from the index, but binary WebAssembly no longer has statements so it will have to use the type-specific expression opcodes for these operations.
MikeHolman commentedon Aug 25, 2015
In this case there is no map, right?
getlocal.i32 0would get you offset 0 from the int32 local space. Andgetlocal.f32 0would read from the float32 local space. So you would only need to do a bound check to ensure your access is < local_count.type.The explosion of opcodes when we add more types is definitely a good point worth considering, and if we are going to take the axe to this concept, it seems there are a lot of other opcodes to consider too.
AndrewScheidecker commentedon Aug 25, 2015
The comparison operations are a bit different because they always yield an I32 (or bool if that change is made). For comparisons the text format opcode includes the type of the operands, but the binary format may want to use a single opcode for each comparison operation, and also save the operand type.
AndrewScheidecker commentedon Aug 25, 2015
I think it makes sense to keep type-specific opcodes in the text format, but for the binary encoding it might be desirable to use "kind"-specific opcodes instead. e.g. getlocal.float, callIndirect.function, or lit.struct instead of getlocal.f64, callIndirect.f64->i32, or lit.myStruct. That would increase the scope for encoding ill-typed code, but it would still be easy to validate.
rossberg commentedon Aug 25, 2015
There exist two different classes of operators: type-specific ones (that
have type-specific semantics), and parametric ones (those that operate
uniformly on, and independent of, any type). I don't see a reason to
type-annotate the latter kind, neither in text nor in the binary format,
given that the type does not affect the operation.
/Andreas
On 25 August 2015 at 18:01, Andrew Scheidecker [email protected]
wrote:
AndrewScheidecker commentedon Aug 25, 2015
At least as described, the binary format encodes an opcode that is dependent on the type demanded of its result. Encoding parametric operations differently doesn't seem worth making an exception.
I see now that the text format also has call opcodes that omit the expected result type. Other than get, set, and call, are there any other parametric operations?
rossberg commentedon Aug 25, 2015
On 25 August 2015 at 18:24, Andrew Scheidecker [email protected]
wrote:
/Andreas
titzer commentedon Aug 25, 2015
Luke's prototype packer used type-specific opcodes, yes. But
v8-native-prototype does not, e.g., and there isn't a consensus on that
issue.
On Tue, Aug 25, 2015 at 6:25 PM, Andrew Scheidecker <
[email protected]> wrote:
lukewagner commentedon Aug 25, 2015
FWIW, despite proposing type-specific opcodes initially, I'm now on the fence (there is probably no size win with module local index spaces; maybe there is a tiny decode speed win b/c no need to validate result type) and I'm mostly waiting to see how they play out once we have realistic tests/prototypes, along with all the other binary encoding design questions.
titzer commentedon Aug 25, 2015
FWIW v8-native-prototype has a single decode loop/switch, so type-specific
opcode spaces would probably require one more table indirection (i.e. load
bytecode, index into current type-specific table to retrieve globally
unique opcode, switch).
On Tue, Aug 25, 2015 at 6:53 PM, Luke Wagner [email protected]
wrote:
MikeHolman commentedon Aug 25, 2015
But for example somewhere you have to do a type check to see when you do add.i32 that both operands are int32, right? With typed opcode spaces, your operands are by definition int32 so you don't need that check. And that check is at least another indirection + (branch predicted) jump.
titzer commentedon Aug 25, 2015
Yes, you have to check the type for getlocal/setlocal, but that's for those
bytecodes, not for every single bytecode.
On Tue, Aug 25, 2015 at 7:28 PM, Michael Holman [email protected]
wrote:
MikeHolman commentedon Aug 25, 2015
From the opcode distribution I've heard Luke bring up, getlocal/setlocal may as well be the only bytecode (they make up something like 30-40% of ASTs IIRC). But why don't you need a type check for other bytecodes? Surely when you do add.i32, you need to make sure your child nodes produced i32?
titzer commentedon Aug 25, 2015
On Tue, Aug 25, 2015 at 8:00 PM, Michael Holman [email protected]
wrote:
MikeHolman commentedon Aug 25, 2015
From my understanding, with the typed opcodes you don't need that validation. Take the following AST:
void:
0,
setlocal.i32i32:
0,
getlocal1,
addf32:
0,
getlocalWith these definitions this would be encoded as
0110001Note that this sort of encoding has the convenient effect of making it such that it impossible to encode an AST that will fail type checks, presumably resulting in faster validation.
For example, you couldn't encode something like this:
It would still encode to
0110001. Of course depending on layout of opcode spaces it is still possible that it would encode to some invalid AST e.g. having wrong number of child nodes, but it could not ever fail type checks, at least as far as I can tell.titzer commentedon Aug 25, 2015
There is a wider philosophical issue here underlying this question. One view is that a single opcode space makes a bytecode's meaning context-independent; we can assign meaning to the expression rooted at any particular bytecode independent of the type context in which it's used. That might be useful to find more trees to de-duplicate in layer 1.
But I think the more important question is how do we view encoding mistakes? E.g. with a single opcode space an encoding mistake or bug is more likely to produce a validation error (because types don't match) rather than decoding to valid but unintended code.
The other design pressure is opcode space extensibility. In the limit, if we define a format that gives meaning to every sequence of bits, there are no decode errors and no validation needs to be done. By the same reasoning then there is no space for extensibility, since every sequence of bits already has a meaning.
MikeHolman commentedon Aug 25, 2015
Considering that wasm will generally be machine produced (especially binary encoding), defending against mistakes should not be a real consideration. Just as the javascript spec doesn't concern itself with bad codegen bugs from js runtimes, we shouldn't concern ourselves with bad codegen from wasm producers (e.g. LLVM).
Maybe I misunderstand, but I don't see why this would be an issue. Assuming module local index spaces, each sequence of bits only has meaning within its module, so we can continue to add new types, etc. without any problems.
titzer commentedon Aug 25, 2015
On Tue, Aug 25, 2015 at 9:20 PM, Michael Holman [email protected]
wrote:
MikeHolman commentedon Aug 25, 2015
Sure, it's a philosophical difference, type validation is a matter of convenience for wasm producers, and my opinion is that isn't important concern for us. There is no one who is going to hand encode wasm binary code. So whatever tool is converting your input (whether wasm text, c++, whatever) to binary should have assertions and tests to make sure that they produce correct wasm, and these types of encoding bugs could easily be caught by such testing. The tools outputting wasm binary will probably want to do type checking of their own and give user-friendly error messages if they detect an incongruity, but that does not have to be our domain.
lukewagner commentedon Oct 22, 2015
Closing: see more recent discussion and refinement in #408.