Repository navigation
Add a br_unless instruction. - #481
sunfishcode wants to merge 1 commit into
Conversation
|
I'm not sure I like this: compilers can have their own IR where this is easy but I don't think we necessarily want WebAssembly to have it. IMO the format isn't designed to support low-level tools: these have to understand the WebAssembly format already so having an internal representation and a writer isn't that onerous. Similarly, LLVM has comparisons for eq/ne/... and it has true/false comparisons. I see this change as analogous to adding true/false comparisons: useful for low-level tools, but IMO wrong abstraction level for WebAssembly's opcodes. WDYT? |
|
It seems that for consistency, if we have Since we do have the negations of the relational ops (both |
|
We don't have the negations of all the floating-point comparisons (currently). And since we don't have a bool type, the condition is an i32, which means the value will sometimes come from a It's also worth mentioning that this change closely echos constructs present in many ISAs, which commonly have both "branch on zero" and "branch on non-zero" instructions. They give compilers full flexibility to put blocks in whatever order they like (eg. to optimize for fall-through paths) without having to weigh the benefits against inserting new instructions. I wouldn't necessarily be opposed to adding an |
|
Fair points. But I feel that the motivation here could equally support adding the missing floating-point comparisons, plus adding But again, if In |
|
@kripken In my prototyping of this feature, |
|
Interesting. Is it easy to see which proportion of those would actually require a not? |
|
@kripken It may well help with the text source code to have separate opcodes here. If a canonicalization of the |
|
I got an in-person lgtm from @jfbastien for this proposal. |
|
Virtual lgtm as well. @sunfishcode showed me the different code that gets emitted by LLVM with/without this feature and it's much cleaner to have both. |
|
I did some measurements for this, on bananabread converted with binaryen. I looked at a random 20 out of 3,225 |
|
@kripken Any change of opinion here? |
|
I agree this has reasonable motivations. My only real concern here is consistency with the rest of the spec. Maybe the simplest solution to keep that consistency is, as discussed above, to add both |
|
IMO an "unless" instruction is not necessary, since one can simply express this with |
|
@titzer The Neither Just wanted to test the consensus on this one again - would like it from a source-code point of view too. |
|
@titzer Sorry, looking at the type issue I see it's not a problem and your proposal would work, at a savings of 2 bytes per instance. I thinks it's just requested as a convenience. |
|
@titzer: technically yes, however
|
|
a89d17b to
8c7eb8d
Compare
|
@titzer Following up on #554 (comment) and offline discussions, would you mind taking another look at this PR? |
|
With agreement on |
|
I won't fight strongly against |
|
I like |
|
Same rationale works for |
|
unless would only be useful in the 1-arm if case, since in the 2-arm if On Fri, Feb 26, 2016 at 12:15 PM, Alon Zakai [email protected]
|
|
Agreed, that reduces the usefulness a little, but 1-arm cases are over 50% in my measurements. So this would still be quite useful, in addition to maintaining consistency in the spec. |
|
I believe my results also support the utility of |
|
Just a thought. I think I read somewhere that (x86) when the branch target buffer is missing a cache entry for a conditional branch, it assumes the branch will not be taken and moreover the btb is left unmodified if the prediction is correct (that's a good thing, as it leaves more cache space for other branches). I've taken advantage of this when writing code like What I do is, I put the most likely branch first. But if my compiler swaps "if" and "else" branches, this optimization is defeated. Therefore if we only pick one, I'd rather have a i32.eqz than an "unless" instruction, unless there is a matching "unless_else" with two branches. |
|
The compiler is extremely likely to flip the order of those basic blocks. The compilers that I'm familiar with will forget and then rebuild the order of blocks based on some tried-and-true heuristics. -Filip
|
8c7eb8d to
04d8a18
Compare
|
Looking at the comments there does appear to be consensus to add br_unless, or no significant objections except requests to also add an |
|
I'm not opposed to it. -Filip
|
|
I strongly disagree, we should not split out
Furthermore, the parallel is not just conceptual, but also practical - binaryen for example has optimizations that can convert between I am in favor of adding both, since I think a reasonable argument has been made in this pull. |
|
I don't have a strong opinion about |
|
@sunfishcode If there is consensus to add both |
|
To be honest, I'm not fond of Can we perhaps wait until after we gained some experience with the MVP before we broaden the operator set in this direction? I don't think there is pressure to do it now. |
|
@rossberg-chromium They are just convenience operators, and not necessary, but there is evidence that they are both common patterns. What other 'special branch instructions that are directly supported by CPUs' do you suggest are in the same class of being so frequent? It seems only a small step and not the start of adding endless convenience control flow operators. There might be some other patterns to consider such as There is a chunk of code in the llvm backend to do this inversion as cleanly as possible, and I see a need for this when converting between There seems to otherwise be consensus. What results from the MVP 'experience' do you need to make a decision? Can we measure this for you now? |
This is the same as br_if except with an inverted condition.
|
This idea did not achieve consensus. |
This is the same as br_if, but with an inverted condition.
This is useful for low-level tools that wish to invert a branch condition in place, for example to rearrange basic blocks while being otherwise minimally invasive. Having both
br_ifandbr_unlessmeans that this is possible just by rewriting the opcode. If course, it is possible to invert a condition explicitly without this, although in many cases it requires inserting new AST nodes.