Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Branch comparison folding and order optimizations #629

Merged
merged 13 commits into from
Apr 17, 2019

Conversation

tyler
Copy link
Member

@tyler tyler commented Dec 21, 2018

Fixes #606.

This PR add two optimizations to simple_preopt.rs. The first is to fold code where we compare against zero, then use an explicit conditional branch. For instance:

v1 = icmp_imm v0, 0
brz v1, ebb1

becomes

brnz v0, ebb1

For x86-64 this doesn't actually make much difference, as it compiles to a test rather than a cmp, which should be very near equivalent. But it does save a byte on a very common operation, so hey.

The second is reordering branches at the end of ebbs to encourage fallthroughs. As pointed out in #606, it's common for wasm code to be compiled like this:

    brz v0, ebb1
    jump ebb2
ebb1:
    ...
ebb2:
    ...

Which ends up being compiled as a conditional jump followed by a jump. However, we can switch those instructions by flipping the destinations and inverting the conditional jump's condition, like so:

    brnz v0, ebb2
    fallthrough ebb1
ebb1:
    ...
ebb2:
    ...

And when we do that we eliminate the latter jump entirely.

@tyler tyler changed the title Branch comparison folding and order optimizations [WIP] Branch comparison folding and order optimizations Dec 21, 2018
Copy link
Member

@sunfishcode sunfishcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is on the right track!

Long term, I think this is additional motivation for Cranelift developing more convenient APIs for pattern-matching, as it's a fair amount of work to dig out all the required bits and test all the needed conditions manually like this :-).

@tyler tyler changed the title [WIP] Branch comparison folding and order optimizations Branch comparison folding and order optimizations Jan 3, 2019
@tyler tyler force-pushed the tyler/branch-opts branch from c8cc35e to 2b129f3 Compare January 3, 2019 22:03
Cargo.toml Outdated
@@ -14,7 +14,7 @@ name = "clif-util"
path = "src/clif-util.rs"

[dependencies]
cfg-if = "0.1"
cfg-if = "0.1.6"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to have been underspecified. With v0.1.4 locally, I was getting errors about missing macros. Presumably due to lack of this commit: rust-lang/cfg-if@67e01e5

@tyler
Copy link
Member Author

tyler commented Jan 4, 2019

I think this is ready for another round of reviews.

@tyler tyler force-pushed the tyler/branch-opts branch from 2b129f3 to 6d5ff80 Compare January 8, 2019 01:32
Copy link
Member

@sunfishcode sunfishcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, overall this looks good!

@bnjbvr bnjbvr self-assigned this Mar 22, 2019
@bnjbvr
Copy link
Member

bnjbvr commented Apr 8, 2019

Hi @tyler ! Any idea when/if you'd have time to address the review comments and rebase this PR here? Or can we help you moving this forward by taking over the remaining changes?

@bnjbvr bnjbvr removed their assignment Apr 8, 2019
@tyler
Copy link
Member Author

tyler commented Apr 10, 2019

hey @bnjbvr, yeah I can get back to this soon. I'll try to get to that this afternoon.

@tyler
Copy link
Member Author

tyler commented Apr 10, 2019

(If I don't, feel free to take it over. :))

@tyler tyler force-pushed the tyler/branch-opts branch from 6d5ff80 to b26d781 Compare April 10, 2019 19:54
@tyler tyler self-assigned this Apr 10, 2019
@tyler
Copy link
Member Author

tyler commented Apr 11, 2019

@sunfishcode or @bnjbvr, do you want to take another look before merge? I think I addressed all the things.

@bnjbvr
Copy link
Member

bnjbvr commented Apr 12, 2019

Thanks @tyler! I'll try to take a look at this early next week.

Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for doing this and adding tests! I've got a few suggestions which could make the code easier to follow, let me know if they make sense to you.

@bjorn3
Copy link
Contributor

bjorn3 commented Apr 15, 2019

I believe you meant other instead of order in the pr title :)

@tyler
Copy link
Member Author

tyler commented Apr 16, 2019

I believe you meant other instead of order in the pr title :)

Surprisingly, I didn't. I was referring to re-ordering branches. :)

@bnjbvr
Copy link
Member

bnjbvr commented Apr 17, 2019

Thanks, looks great!

@bnjbvr bnjbvr merged commit d43cfc6 into bytecodealliance:master Apr 17, 2019
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Apr 24, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512

--HG--
extra : rebase_source : f0d765a1cb1e2f7872037c18b9951077a08ae4b7
extra : histedit_source : 1a1dd95618e166705f7165c045f3b5af12f96d5b
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Apr 24, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512

--HG--
extra : moz-landing-system : lando
mykmelez pushed a commit to mykmelez/gecko that referenced this pull request Apr 25, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512
mykmelez pushed a commit to mykmelez/gecko that referenced this pull request Apr 25, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this pull request Oct 4, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512

UltraBlame original commit: 5acf68edbf599cafcf131d5fa2eb5e70c67279b8
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request Oct 4, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512

UltraBlame original commit: 5acf68edbf599cafcf131d5fa2eb5e70c67279b8
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this pull request Oct 4, 2019
…f1; r=lth

The one optimization introduced by this is the rearrangement of branches to
prefer fallthrough whenever possible, as well as folding branches when
comparing against 0. See also
bytecodealliance/cranelift#629 for details.

Differential Revision: https://phabricator.services.mozilla.com/D28512

UltraBlame original commit: 5acf68edbf599cafcf131d5fa2eb5e70c67279b8
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize branches
4 participants