forked from rust-lang/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 3
BPF: extend JSET optimisation to cover select IR pattern #2
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
Open
0xull
wants to merge
1
commit into
blueshift-gg:upstream-gallery-21
Choose a base branch
from
0xull:jset-select-pattern-optimisation
base: upstream-gallery-21
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| ; RUN: llc < %s -mtriple=bpf -mcpu=v2 -verify-machineinstrs | FileCheck %s | ||
| ; | ||
| ; Test that the JSET peephole optimisation fires for the select IR pattern. | ||
| ; When both branch outcomes are compile-time constants, LLVM emits select | ||
| ; instead of br. This should fold AND + JNE into JSET, eliminating the AND. | ||
| ; | ||
| ; Source: | ||
| ; uint64_t select_jset(uint64_t x, uint64_t m) { | ||
| ; return (x & m) == 0 ? 64 : 32; | ||
| ; } | ||
| ; | ||
| ; uint32_t select_jset_32_zero(uint32_t x, uint32_t m) { | ||
| ; return (x & m) == 0 ? 64 : 32; | ||
| ; } | ||
|
|
||
| ; Function Attrs: norecurse nounwind readnone | ||
| define i64 @select_jset(i64 %0, i64 %1) { | ||
clairechingching marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| %3 = and i64 %1, %0 | ||
| %4 = icmp eq i64 %3, 0 | ||
| %5 = select i1 %4, i64 64, i64 32 | ||
| ; CHECK-LABEL: select_jset: | ||
| ; CHECK: r0 = 32 | ||
| ; CHECK-NEXT: if r{{[0-9]+}} & r{{[0-9]+}} goto [[LABEL:LBB0_[0-9]+]] | ||
| ; CHECK: r0 = 64 | ||
| ; CHECK-NEXT: [[LABEL]]: | ||
| ; CHECK-NEXT: exit | ||
| ; CHECK-NOT: &= | ||
| ret i64 %5 | ||
| } | ||
|
|
||
| ; Function Attrs: norecurse nounwind readnone | ||
| define i32 @select_jset_32_zero(i32 %0, i32 %1) { | ||
| %3 = and i32 %1, %0 | ||
| %4 = icmp eq i32 %3, 0 | ||
| %5 = select i1 %4, i32 64, i32 32 | ||
| ; CHECK-LABEL: select_jset_32_zero: | ||
| ; CHECK: r{{[0-9]+}} &= r{{[0-9]+}} | ||
| ; CHECK-NEXT: r{{[0-9]+}} <<= 32 | ||
| ; CHECK-NEXT: r{{[0-9]+}} >>= 32 | ||
| ; CHECK-NEXT: r0 = 64 | ||
| ; CHECK-NEXT: if r{{[0-9]+}} == 0 goto [[LABEL32:LBB[0-9]+_[0-9]+]] | ||
| ; CHECK: r0 = 32 | ||
| ; CHECK-NEXT: [[LABEL32]]: | ||
| ; CHECK-NEXT: exit | ||
| ; CHECK-NOT: if r{{[0-9]+}} & r{{[0-9]+}} goto | ||
| ret i32 %5 | ||
| } | ||
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,30 @@ | ||
| # RUN: llc -o - %s -mtriple=bpf -run-pass=bpf-mi-pemit-peephole | FileCheck %s | ||
| # | ||
| # Verify that the fold does not fire when an intervening instruction clobbers | ||
| # the AND's source register. The AND and JNE must survive unchanged. | ||
| # | ||
| --- | | ||
| define i64 @select_jset_clobber(i64 %0, i64 %1) { ret i64 0 } | ||
| ... | ||
| --- | ||
| name: select_jset_clobber | ||
| tracksRegLiveness: true | ||
| body: | | ||
| bb.0: | ||
| successors: %bb.1, %bb.2 | ||
| liveins: $r1, $r2 | ||
| $r2 = AND_rr $r2, $r1 | ||
| $r1 = MOV_ri 0 | ||
| $r0 = MOV_ri 32 | ||
| JNE_ri killed $r2, 0, %bb.2 | ||
| bb.1: | ||
| successors: %bb.2 | ||
| $r0 = MOV_ri 64 | ||
| bb.2: | ||
| liveins: $r0 | ||
| RET implicit $r0 | ||
| # CHECK-LABEL: name: select_jset_clobber | ||
| # CHECK: $r2 = AND_rr $r2, $r1 | ||
| # CHECK: $r1 = MOV_ri 0 | ||
| # CHECK: JNE_ri killed $r2, 0, %bb.2 | ||
| # CHECK-NOT: JSET_rr |
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,27 @@ | ||
| # RUN: llc -o - %s -mtriple=bpf -run-pass=bpf-mi-pemit-peephole | FileCheck %s | ||
| # | ||
| # Verify that AND_ri + JNE_ri folds into JSET_ri when the mask is an immediate. | ||
| # | ||
| --- | | ||
| define i64 @select_jset_ri(i64 %0) { ret i64 0 } | ||
| ... | ||
| --- | ||
| name: select_jset_ri | ||
| tracksRegLiveness: true | ||
| body: | | ||
| bb.0: | ||
| successors: %bb.1, %bb.2 | ||
| liveins: $r1 | ||
| $r1 = AND_ri killed $r1, 255 | ||
| $r0 = MOV_ri 32 | ||
| JNE_ri killed $r1, 0, %bb.2 | ||
| bb.1: | ||
| successors: %bb.2 | ||
| $r0 = MOV_ri 64 | ||
| bb.2: | ||
| liveins: $r0 | ||
| RET implicit $r0 | ||
| # CHECK-LABEL: name: select_jset_ri | ||
| # CHECK: $r0 = MOV_ri 32 | ||
| # CHECK-NEXT: JSET_ri $r1, 255, %bb.2 | ||
| # CHECK-NOT: AND_ri |
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,28 @@ | ||
| # RUN: llc -o - %s -mtriple=bpf -run-pass=bpf-mi-pemit-peephole | FileCheck %s | ||
| # | ||
| # Verify that AND_rr + JNE_ri folds into JSET_rr for the select IR path. | ||
| # The AND should be eliminated and JSET emitted in its place. | ||
| # | ||
| --- | | ||
| define i64 @select_jset(i64 %0, i64 %1) { ret i64 0 } | ||
| ... | ||
| --- | ||
| name: select_jset | ||
| tracksRegLiveness: true | ||
| body: | | ||
| bb.0: | ||
| successors: %bb.1, %bb.2 | ||
| liveins: $r1, $r2 | ||
| $r2 = AND_rr killed $r2, killed $r1 | ||
| $r0 = MOV_ri 32 | ||
| JNE_ri killed $r2, 0, %bb.2 | ||
| bb.1: | ||
| successors: %bb.2 | ||
| $r0 = MOV_ri 64 | ||
| bb.2: | ||
| liveins: $r0 | ||
| RET implicit $r0 | ||
| # CHECK-LABEL: name: select_jset | ||
| # CHECK: $r0 = MOV_ri 32 | ||
| # CHECK-NEXT: JSET_rr $r2, $r1, %bb.2 | ||
| # CHECK-NOT: AND_rr |
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.
Uh oh!
There was an error while loading. Please reload this page.