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
WebAssembly has several operators that imply that any code following them until the next end or else operator will never be executed: ret, br, br_table, unreachable.
I propose that these operators act as implicit end/else operators. This would imply that they may only occur at the end of a control flow block. The end operator would still be useful as a special-case br operator that targets a fixed depth of 0.
This also makes it clear that else can be eliminated, and that an if operator just needs to be followed by two terminator instructions.
I believe this simplifies code generation, since you don't need to worry about the stack having the right type going into an unreachable end operator. I believe it also simplifies the spec and validation, since it completely avoids the question of how to handle code between these operators and the next end.
Would this mean that end == br 0 except that the branch currently unwinds the values stack?
Yes, though with the block signature change, they could be the same: end/br0 could take just the results specified by the block signature from the current stack frame and implicitly drop the rest.
Perhaps if could be defined as effectively block<0>, , br_unless, and if-else could require an explicit block around this.
Yes, since the general if can be implemented with blocks and br_if, it might make sense for if to be a shortcut for compressing the common case. However, I think an if with an else block is still justified for compactness. Here's what the byte stream would look like if you needed to use a block for the else:
block
signature
<cond>
if
<then>
br
1
<else>
br0
Vs with an if_else_0 that still has an else, but an implicit () signature:
<cond>
if_else_0
<then>
br0
<else>
br0
I think given the frequency of if, the compactness of the last if_else_0 case is worthwhile.
This proposal makes sense to me. Along with block signatures, this proposal would give us a concrete stack state at every instruction, with no special states or modes. Even if special states or modes are simple today, they require have the potential to interact with future features, and they require extra testing.
One detail: removing else would make it ambiguous whether an if ... end has an "else" arm. A simple way to fix this would be to introduce an if_else opcode, so the two sequences would be:
if ...end`
if_else ... end ... end
(and any of the ends could be any one of the terminators). This approach also informs consumers up front whether an "else" arm is present, which one could imagine being useful in some circumstances.
A notable feature of this proposal is that it eliminates the awkward zone between an unconditional branch and the subsequent end while still permitting unreachable code. A producer that knows it may emit unreachable code can start a new block before emitting an unconditional branch, and with block signatures, this creates unreachable code but retains a concrete type sequence on the stack.
I've been rewriting WAVM to support the binary format + stack machine semantics, and am currently targeting a hypothetical version that has both #765 and this implicit terminator proposal. I'm happy to say that eliminates quite a bit of complexity in the validator since, as @sunfishcode pointed out, it eliminates the possibility of code where the stack is in an unknown state. It moves a little of that complexity to assembling a WAST file, but overall I'm happy with it.
In my prototype, end and br both implicitly discard superfluous elements on the stack. That's an arbitrary choice though, and the implementation could work either way. But end and br do differ in the case of loop:
loop : i32
<condition>
if_else : i32
br 1
;; else (implied by the preceding br)
i32.const 1
br 0
end
There isn't a good way to replace the final end with a br; getting rid of end would mean that you have to wrap a loop in a block to break out of it. loop pushes a single entry on the control stack, but that entry effectively has two branch targets: one that is only used by end, and one that is used by the various br operators. It might be possible to eliminate this distinction, but I haven't thought of a good way yet.
I was under the impression it was going to change to one label, but it looks like it hasn't been merged yet (#710), so perhaps it will keep both labels.
If the branch target stack isn't 1:1 with the control structure stack (e.g. the terminating operator for the loop control structure will implicitly pop two branch targets), then it does seem like end can be replaced with br in all cases.
Perhaps two signatures too, unless the common case is a void result. A loop is an operator that could really benefit from consuming values from the stack at the head of the loop, so could make use of a signature with arguments and results. The exit block signature will be different in general.
I agree that it would be nice, but it needs to be exposed through WAST for it to be useful, so my test just uses signatures with no parameters and an inferred return type.
I converted WAVM to do what I believe the current standard is (unreachable code is allowed between an unconditional branch and the end of the current control structure, and consumers should ignore it), but I recreated the changes proposed in this issue as a single commit for easy diffing: WAVM/WAVM@6d20818
Part of the change is to use distinct opcodes for if with and without an else branch, so you don't need the else opcode to disambiguate the start of the else branch from then end of an else-less if. These changes made my translation to LLVM IR about 5% faster, which I hypothesize is mostly because it no longer needs to speculatively create a basic block for the else branch of an else-less if.
The zipped size savings were about .5%; that's comparing against a WASM that already didn't have any unreachable code that would be disallowed by this change, so it only comes from two changes: the end opcode can be saved when a control structure ends in an unconditional branch, and the else-less if opcode doesn't need a result signature (it cannot yield a result).
An alternative thought: target index 0 goes to the next instruction. In the case of br_table, this would allow code following this instruction to be used as a target, saving a block layer and its corresponding 3 bytes.
@qwertie Assuming that AngryBots13.wasm is a real-world example, target-0-is-next-instruction would save 4194* bytes, but eliminating the default case would only save 1398* as this particular file has no default 0, necessitating the immediate next instruction to be a br. A clever compiler could re-order the blocks so that the default comes first, but this may not be easy or even possible if it's in the middle of a fall-through series**.
All of the options discussed in this issue will save bytes, though, so I think if the binary format is not yet frozen, one of these approaches should be implemented.
(*) Branches targeting what used to be level 127 but is now 128 will grow by 1 byte; I'm not accounting for this currently, but such deep block stacks should be uncommon.
(**) My WASM reader isn't currently sophisticated enough to assess this possibility.
I have no data in front of me, but thinking about C switches, in the majority of them, default: is preceded by a break, which allows the optimization you mentioned.
I believe omitting br_table's default label would be simpler, both in terms of describing br_table in the spec and in terms of implementation.
Edit: I just realized I don't know what you meant by "this file has no default 0" so I did not actually understand what you said. But I'm Canadian, so, sorry.
@qwertie AngryBots13.wasm has no instances of br_table where default is 0, so removing the default means the very next instruction after the br_table will be br, which requires two bytes. So, the savings of removing the default isn't that great for this file without the optimization I mentioned.
Activity
AndrewScheidecker commentedon Aug 30, 2016
Yes, though with the block signature change, they could be the same:
end/br0could take just the results specified by the block signature from the current stack frame and implicitly drop the rest.Yes, since the general
ifcan be implemented with blocks andbr_if, it might make sense forifto be a shortcut for compressing the common case. However, I think anifwith an else block is still justified for compactness. Here's what the byte stream would look like if you needed to use a block for the else:Vs with an
if_else_0that still has an else, but an implicit()signature:I think given the frequency of
if, the compactness of the lastif_else_0case is worthwhile.sunfishcode commentedon Sep 1, 2016
This proposal makes sense to me. Along with block signatures, this proposal would give us a concrete stack state at every instruction, with no special states or modes. Even if special states or modes are simple today, they require have the potential to interact with future features, and they require extra testing.
One detail: removing
elsewould make it ambiguous whether anif ... endhas an "else" arm. A simple way to fix this would be to introduce anif_elseopcode, so the two sequences would be:if ...end`if_else ... end ... end(and any of the
ends could be any one of the terminators). This approach also informs consumers up front whether an "else" arm is present, which one could imagine being useful in some circumstances.sunfishcode commentedon Sep 1, 2016
A notable feature of this proposal is that it eliminates the awkward zone between an unconditional branch and the subsequent end while still permitting unreachable code. A producer that knows it may emit unreachable code can start a new
blockbefore emitting an unconditional branch, and with block signatures, this creates unreachable code but retains a concrete type sequence on the stack.AndrewScheidecker commentedon Sep 5, 2016
Yes, it you add explicit signature to the blocks. e.g.:
I've been rewriting WAVM to support the binary format + stack machine semantics, and am currently targeting a hypothetical version that has both #765 and this implicit terminator proposal. I'm happy to say that eliminates quite a bit of complexity in the validator since, as @sunfishcode pointed out, it eliminates the possibility of code where the stack is in an unknown state. It moves a little of that complexity to assembling a WAST file, but overall I'm happy with it.
AndrewScheidecker commentedon Sep 5, 2016
In my prototype,
endandbrboth implicitly discard superfluous elements on the stack. That's an arbitrary choice though, and the implementation could work either way. Butendandbrdo differ in the case ofloop:There isn't a good way to replace the final
endwith abr; getting rid ofendwould mean that you have to wrap aloopin a block to break out of it.looppushes a single entry on the control stack, but that entry effectively has two branch targets: one that is only used byend, and one that is used by the variousbroperators. It might be possible to eliminate this distinction, but I haven't thought of a good way yet.AndrewScheidecker commentedon Sep 6, 2016
I was under the impression it was going to change to one label, but it looks like it hasn't been merged yet (#710), so perhaps it will keep both labels.
If the branch target stack isn't 1:1 with the control structure stack (e.g. the terminating operator for the loop control structure will implicitly pop two branch targets), then it does seem like
endcan be replaced withbrin all cases.I agree that it would be nice, but it needs to be exposed through WAST for it to be useful, so my test just uses signatures with no parameters and an inferred return type.
AndrewScheidecker commentedon Sep 19, 2016
I converted WAVM to do what I believe the current standard is (unreachable code is allowed between an unconditional branch and the
endof the current control structure, and consumers should ignore it), but I recreated the changes proposed in this issue as a single commit for easy diffing: WAVM/WAVM@6d20818Part of the change is to use distinct opcodes for
ifwith and without an else branch, so you don't need theelseopcode to disambiguate the start of the else branch from then end of an else-lessif. These changes made my translation to LLVM IR about 5% faster, which I hypothesize is mostly because it no longer needs to speculatively create a basic block for the else branch of an else-lessif.The zipped size savings were about .5%; that's comparing against a WASM that already didn't have any unreachable code that would be disallowed by this change, so it only comes from two changes: the
endopcode can be saved when a control structure ends in an unconditional branch, and the else-lessifopcode doesn't need a result signature (it cannot yield a result).RyanLamansky commentedon Jan 13, 2017
An alternative thought: target index 0 goes to the next instruction. In the case of
br_table, this would allow code following this instruction to be used as a target, saving a block layer and its corresponding 3 bytes.qwertie commentedon Jan 13, 2017
@Kardax hmm, to me it makes more sense to eliminate the default case, so
br_tablewould be conditional on whether the index is in range.RyanLamansky commentedon Jan 14, 2017
@qwertie Assuming that AngryBots13.wasm is a real-world example, target-0-is-next-instruction would save 4194* bytes, but eliminating the default case would only save 1398* as this particular file has no default 0, necessitating the immediate next instruction to be a
br. A clever compiler could re-order the blocks so that the default comes first, but this may not be easy or even possible if it's in the middle of a fall-through series**.All of the options discussed in this issue will save bytes, though, so I think if the binary format is not yet frozen, one of these approaches should be implemented.
(*) Branches targeting what used to be level 127 but is now 128 will grow by 1 byte; I'm not accounting for this currently, but such deep block stacks should be uncommon.
(**) My WASM reader isn't currently sophisticated enough to assess this possibility.
qwertie commentedon Jan 14, 2017
I have no data in front of me, but thinking about C switches, in the majority of them,
default:is preceded by abreak, which allows the optimization you mentioned.I believe omitting
br_table's default label would be simpler, both in terms of describingbr_tablein the spec and in terms of implementation.Edit: I just realized I don't know what you meant by "this file has no default 0" so I did not actually understand what you said. But I'm Canadian, so, sorry.
RyanLamansky commentedon Jan 14, 2017
@qwertie AngryBots13.wasm has no instances of
br_tablewhere default is 0, so removing the default means the very next instruction after thebr_tablewill bebr, which requires two bytes. So, the savings of removing the default isn't that great for this file without the optimization I mentioned.pizlonator commentedon Feb 6, 2017
It's too late for this.