-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[BPF] Use mul for certain div/mod operations #110712
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
Merged
Merged
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,57 @@ | ||
; RUN: llc -march=bpfel -mcpu=v1 < %s | FileCheck --check-prefix=CHECK-V1 %s | ||
; RUN: llc -march=bpfel -mcpu=v4 < %s | FileCheck --check-prefix=CHECK-V4 %s | ||
|
||
target triple = "bpf" | ||
|
||
; struct S { | ||
; int var[3]; | ||
; }; | ||
; int foo1 (struct S *a, struct S *b) | ||
; { | ||
; return a - b; | ||
; } | ||
define dso_local i32 @foo1(ptr noundef %a, ptr noundef %b) local_unnamed_addr { | ||
entry: | ||
%sub.ptr.lhs.cast = ptrtoint ptr %a to i64 | ||
%sub.ptr.rhs.cast = ptrtoint ptr %b to i64 | ||
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast | ||
%sub.ptr.div = sdiv exact i64 %sub.ptr.sub, 12 | ||
%conv = trunc i64 %sub.ptr.div to i32 | ||
ret i32 %conv | ||
} | ||
; CHECK-V1: r0 = r1 | ||
; CHECK-V1: r0 -= r2 | ||
; CHECK-V1: r0 s>>= 2 | ||
; CHECK-V1: r1 = -6148914691236517205 ll | ||
; CHECK-V1: r0 *= r1 | ||
; CHECK-V1: exit | ||
|
||
; CHECK-V4: r0 = r1 | ||
; CHECK-V4: r0 -= r2 | ||
; CHECK-V4: r0 >>= 2 | ||
; CHECK-V4: w0 *= -1431655765 | ||
; CHECK-V4: exit | ||
|
||
define dso_local noundef range(i32 -143165576, 143165577) i32 @foo2(i32 noundef %a) local_unnamed_addr { | ||
entry: | ||
%div = sdiv i32 %a, 15 | ||
ret i32 %div | ||
} | ||
; CHECK-V1-NOT: r[[#]] s/= 15 | ||
; CHECK-V4-NOT: w[[#]] s/= 15 | ||
|
||
define dso_local noundef range(i32 -14, 15) i32 @foo3(i32 noundef %a) local_unnamed_addr { | ||
entry: | ||
%rem = srem i32 %a, 15 | ||
ret i32 %rem | ||
} | ||
; CHECK-V1-NOT: r[[#]] s%= 15 | ||
; CHECK-V4-NOT: w[[#]] s%= 15 | ||
|
||
define dso_local i64 @foo4(i64 noundef %a) local_unnamed_addr { | ||
entry: | ||
%div = udiv exact i64 %a, 15 | ||
ret i64 %div | ||
} | ||
; CHECK-V1-NOT: r[[#]] /= 15 | ||
; CHECK-V4-NOT: w[[#]] /= 15 |
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
Oops, something went wrong.
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.
I think it's better to keep this "cheap" when cpuv4 is enabled.
E.g. as far as I understand for arm64 BPF backend division/modulo operations are translated as single instructions.
Thus, with the following patch on top allow-diff-for-cpuv4.txt:
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.
@eddyz87 From @4ast the point is that even a single divide is expensive than multiplication/shift/... etc. The number of insns is not a criteria here.
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.
No. Integer div is the slowest operation in pretty much every cpu architecture.
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.
Ok, I see both x86 and arm LLVM backends define this as expensive.
I somehow understood this as a knob whether to generate sdiv at all, while this is only an optimization knob.
However, the commit description says:
Do we also need to add some generic lowering for cpuv{1,2,3}?
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.
you mean to lower sdiv into udiv plus conditionals ?
It's doable, but let's do it in the separate patch and it's not a high priority imo.
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.
Yes
If we are sure that optimization pattern that fires in this case would cover all other uses, then yes, lower priority.