-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[llvm][SPIRV] Expose fast popcnt
support for SPIR-V targets
#109845
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 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
eca4879
Advertise fast `popcnt` support for SPIR-V targets.
AlexVlx 82a9f72
Remove incorrect assertion.
AlexVlx 25e9b3c
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx 04779ea
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx 8597c63
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx f68b396
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx fb29456
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx 75e39fa
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx db154ee
SPIR-V doesn't handle integer bitwidths greater than 64.
AlexVlx ec6167f
Add opt->llc->SPIR-V test.
AlexVlx 4f6da8b
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx 5476fcd
Add missing whitespace.
AlexVlx f0112ff
No need to duplicate the same test under both AMDGPU and SPIRV.
AlexVlx 57c75e9
Remove test as per review feedback.
AlexVlx f486701
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx 178bc50
Merge branch 'main' of https://github.com/llvm/llvm-project into popc…
AlexVlx 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
if not "SPIRV" in config.root.targets: | ||
config.unsupported = True |
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,128 @@ | ||
; RUN: opt -passes=loop-idiom -mtriple=spirv32-- -S < %s | FileCheck %s | ||
; RUN: opt -passes=loop-idiom -mtriple=spirv64-- -S < %s | FileCheck %s | ||
|
||
; Mostly copied from x86 version. | ||
|
||
;To recognize this pattern: | ||
;int popcount(unsigned long long a) { | ||
; int c = 0; | ||
; while (a) { | ||
; c++; | ||
; a &= a - 1; | ||
; } | ||
; return c; | ||
;} | ||
; | ||
|
||
; CHECK-LABEL: @popcount_i64 | ||
; CHECK: entry | ||
; CHECK: llvm.ctpop.i64 | ||
; CHECK: ret | ||
define i32 @popcount_i64(i64 %a) nounwind uwtable readnone ssp { | ||
entry: | ||
%tobool3 = icmp eq i64 %a, 0 | ||
br i1 %tobool3, label %while.end, label %while.body | ||
|
||
while.body: ; preds = %entry, %while.body | ||
%c.05 = phi i32 [ %inc, %while.body ], [ 0, %entry ] | ||
%a.addr.04 = phi i64 [ %and, %while.body ], [ %a, %entry ] | ||
%inc = add nsw i32 %c.05, 1 | ||
%sub = add i64 %a.addr.04, -1 | ||
%and = and i64 %sub, %a.addr.04 | ||
%tobool = icmp eq i64 %and, 0 | ||
br i1 %tobool, label %while.end, label %while.body | ||
|
||
while.end: ; preds = %while.body, %entry | ||
%c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] | ||
ret i32 %c.0.lcssa | ||
} | ||
|
||
; CHECK-LABEL: @popcount_i32 | ||
; CHECK: entry | ||
; CHECK: llvm.ctpop.i32 | ||
; CHECK: ret | ||
define i32 @popcount_i32(i32 %a) nounwind uwtable readnone ssp { | ||
entry: | ||
%tobool3 = icmp eq i32 %a, 0 | ||
br i1 %tobool3, label %while.end, label %while.body | ||
|
||
while.body: ; preds = %entry, %while.body | ||
%c.05 = phi i32 [ %inc, %while.body ], [ 0, %entry ] | ||
%a.addr.04 = phi i32 [ %and, %while.body ], [ %a, %entry ] | ||
%inc = add nsw i32 %c.05, 1 | ||
%sub = add i32 %a.addr.04, -1 | ||
%and = and i32 %sub, %a.addr.04 | ||
%tobool = icmp eq i32 %and, 0 | ||
br i1 %tobool, label %while.end, label %while.body | ||
|
||
while.end: ; preds = %while.body, %entry | ||
%c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] | ||
ret i32 %c.0.lcssa | ||
} | ||
|
||
; CHECK-LABEL: @popcount_i128 | ||
; CHECK: entry | ||
; CHECK: llvm.ctpop.i128 | ||
; CHECK: ret | ||
define i32 @popcount_i128(i128 %a) nounwind uwtable readnone ssp { | ||
entry: | ||
%tobool3 = icmp eq i128 %a, 0 | ||
br i1 %tobool3, label %while.end, label %while.body | ||
|
||
while.body: ; preds = %entry, %while.body | ||
%c.05 = phi i32 [ %inc, %while.body ], [ 0, %entry ] | ||
%a.addr.04 = phi i128 [ %and, %while.body ], [ %a, %entry ] | ||
%inc = add nsw i32 %c.05, 1 | ||
%sub = add i128 %a.addr.04, -1 | ||
%and = and i128 %sub, %a.addr.04 | ||
%tobool = icmp eq i128 %and, 0 | ||
br i1 %tobool, label %while.end, label %while.body | ||
|
||
while.end: ; preds = %while.body, %entry | ||
%c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] | ||
ret i32 %c.0.lcssa | ||
} | ||
|
||
; To recognize this pattern: | ||
;int popcount(unsigned long long a, int mydata1, int mydata2) { | ||
; int c = 0; | ||
; while (a) { | ||
; c++; | ||
; a &= a - 1; | ||
; mydata1 *= c; | ||
; mydata2 *= (int)a; | ||
; } | ||
; return c + mydata1 + mydata2; | ||
;} | ||
|
||
; CHECK-LABEL: @popcount2 | ||
; CHECK: entry | ||
; CHECK: llvm.ctpop.i64 | ||
; CHECK: ret | ||
define i32 @popcount2(i64 %a, i32 %mydata1, i32 %mydata2) nounwind uwtable readnone ssp { | ||
entry: | ||
%tobool9 = icmp eq i64 %a, 0 | ||
br i1 %tobool9, label %while.end, label %while.body | ||
|
||
while.body: ; preds = %entry, %while.body | ||
%c.013 = phi i32 [ %inc, %while.body ], [ 0, %entry ] | ||
%mydata2.addr.012 = phi i32 [ %mul1, %while.body ], [ %mydata2, %entry ] | ||
%mydata1.addr.011 = phi i32 [ %mul, %while.body ], [ %mydata1, %entry ] | ||
%a.addr.010 = phi i64 [ %and, %while.body ], [ %a, %entry ] | ||
%inc = add nsw i32 %c.013, 1 | ||
%sub = add i64 %a.addr.010, -1 | ||
%and = and i64 %sub, %a.addr.010 | ||
%mul = mul nsw i32 %inc, %mydata1.addr.011 | ||
%conv = trunc i64 %and to i32 | ||
%mul1 = mul nsw i32 %conv, %mydata2.addr.012 | ||
%tobool = icmp eq i64 %and, 0 | ||
br i1 %tobool, label %while.end, label %while.body | ||
|
||
while.end: ; preds = %while.body, %entry | ||
%c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] | ||
%mydata2.addr.0.lcssa = phi i32 [ %mydata2, %entry ], [ %mul1, %while.body ] | ||
%mydata1.addr.0.lcssa = phi i32 [ %mydata1, %entry ], [ %mul, %while.body ] | ||
%add = add i32 %mydata2.addr.0.lcssa, %mydata1.addr.0.lcssa | ||
%add2 = add i32 %add, %c.0.lcssa | ||
ret i32 %add2 | ||
} |
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.
TTI probably should have a better default checking if the operation is legal. It's also not great that TargetLowering also has isCtpopFast