-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement branch hinting proposal support #12483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| ;;! target = "x86_64" | ||
| ;;! test = "optimize" | ||
|
|
||
| ;; Test that branch hints from the `metadata.code.branch_hint` custom section | ||
| ;; are used to mark cold blocks in the generated code. | ||
|
|
||
| (module | ||
| ;; Test br_if with hint that branch is unlikely (not taken). | ||
| ;; The branch target block should be marked cold. | ||
| (func $unlikely_branch (param i32) (result i32) | ||
| (block $target (result i32) | ||
| i32.const 0 ;; value to return if branch taken | ||
| local.get 0 ;; condition | ||
| (@metadata.code.branch_hint "\00") | ||
| br_if $target | ||
| ;; Fallthrough path (likely) | ||
| drop | ||
| i32.const 42 | ||
| ) | ||
| ) | ||
|
|
||
| ;; Test br_if with hint that branch is likely (taken). | ||
| ;; The fallthrough block should be marked cold. | ||
| (func $likely_branch (param i32) (result i32) | ||
| (block $target (result i32) | ||
| i32.const 0 ;; value to return if branch taken | ||
| local.get 0 ;; condition | ||
| (@metadata.code.branch_hint "\01") | ||
| br_if $target | ||
| ;; Fallthrough path (unlikely, should be cold) | ||
| drop | ||
| i32.const 42 | ||
| ) | ||
| ) | ||
| ) | ||
| ;; function u0:0(i64 vmctx, i64, i32) -> i32 tail { | ||
| ;; gv0 = vmctx | ||
| ;; gv1 = load.i64 notrap aligned readonly gv0+8 | ||
| ;; gv2 = load.i64 notrap aligned gv1+16 | ||
| ;; stack_limit = gv2 | ||
| ;; | ||
| ;; block0(v0: i64, v1: i64, v2: i32): | ||
| ;; @0043 v5 = iconst.i32 0 | ||
| ;; @0047 brif v2, block2(v5), block3 ; v5 = 0 | ||
| ;; | ||
| ;; block3: | ||
| ;; @004a v6 = iconst.i32 42 | ||
| ;; @004c jump block2(v6) ; v6 = 42 | ||
| ;; | ||
| ;; block2(v4: i32) cold: | ||
| ;; @004d jump block1(v4) | ||
| ;; | ||
| ;; block1(v3: i32): | ||
| ;; @004d return v3 | ||
| ;; } | ||
| ;; | ||
| ;; function u0:1(i64 vmctx, i64, i32) -> i32 tail { | ||
| ;; gv0 = vmctx | ||
| ;; gv1 = load.i64 notrap aligned readonly gv0+8 | ||
| ;; gv2 = load.i64 notrap aligned gv1+16 | ||
| ;; stack_limit = gv2 | ||
| ;; | ||
| ;; block0(v0: i64, v1: i64, v2: i32): | ||
| ;; @0052 v5 = iconst.i32 0 | ||
| ;; @0056 brif v2, block2(v5), block3 ; v5 = 0 | ||
| ;; | ||
| ;; block3 cold: | ||
| ;; @0059 v6 = iconst.i32 42 | ||
| ;; @005b jump block2(v6) ; v6 = 42 | ||
| ;; | ||
| ;; block2(v4: i32): | ||
| ;; @005c jump block1(v4) | ||
| ;; | ||
| ;; block1(v3: i32): | ||
| ;; @005c return v3 | ||
| ;; } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop I think might be good to optimize in terms of translation will only lookup branch hints (I think?) in increasing order of offsets. That means that currently this code is an$O(n^2)$ loop as-implemented. That could be optimized by using a binary search here, but I think this could go one step further and, ideally, store a reference to the raw buffer of input wasm data (in theory) here. For example this could be a
.peekable()iterator over the raw wasm itself. Looking up viaget_branch_hintwould advance the iterator if it's at the matching position.Regardless I think it'll be needed to remove the quadratic behavior here, and I think ideally it'd be via an iterator-like approach to avoid the logarithmic nature of a binary search.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you are right.
How about bc62088 for O(n) complexity?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What Alex is getting at, I think, is that the spec states that the hints are given in PC order. So why not traverse hints in order, taking them when they match as we visit increasing PC offsets? We should be able to that in O(n) time with O(1) space overhead, i.e., not building a HashMap at all.