Skip to content

Commit 5d46263

Browse files
committed
[AMDGPU] Enable divergence-driven 'ctpop' selection
This change adds the patterns and divergence predicates for the ctpop (bitcount) nodes to make them selected according to the divergence. Reviewed By: foad Differential Revision: https://reviews.llvm.org/D116284
1 parent 40c7243 commit 5d46263

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

llvm/lib/Target/AMDGPU/SIInstructions.td

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ def : GCNPat <
10111011
}
10121012

10131013
def : GCNPat <
1014-
(i32 (ctpop i32:$popcnt)),
1014+
(i32 (DivergentUnaryFrag<ctpop> i32:$popcnt)),
10151015
(V_BCNT_U32_B32_e64 VSrc_b32:$popcnt, (i32 0))
10161016
>;
10171017

@@ -1020,6 +1020,14 @@ def : GCNPat <
10201020
(V_BCNT_U32_B32_e64 $popcnt, $val)
10211021
>;
10221022

1023+
def : GCNPat <
1024+
(i64 (DivergentUnaryFrag<ctpop> i64:$src)),
1025+
(REG_SEQUENCE VReg_64,
1026+
(V_BCNT_U32_B32_e64 (i32 (EXTRACT_SUBREG i64:$src, sub1)),
1027+
(i32 (V_BCNT_U32_B32_e64 (i32 (EXTRACT_SUBREG i64:$src, sub0)), (i32 0)))), sub0,
1028+
(i32 (V_MOV_B32_e32 (i32 0))), sub1)
1029+
>;
1030+
10231031
/********** ============================================ **********/
10241032
/********** Extraction, Insertion, Building and Casting **********/
10251033
/********** ============================================ **********/

llvm/lib/Target/AMDGPU/SOPInstructions.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ let Defs = [SCC] in {
246246
def S_BCNT0_I32_B32 : SOP1_32 <"s_bcnt0_i32_b32">;
247247
def S_BCNT0_I32_B64 : SOP1_32_64 <"s_bcnt0_i32_b64">;
248248
def S_BCNT1_I32_B32 : SOP1_32 <"s_bcnt1_i32_b32",
249-
[(set i32:$sdst, (ctpop i32:$src0))]
249+
[(set i32:$sdst, (UniformUnaryFrag<ctpop> i32:$src0))]
250250
>;
251251
def S_BCNT1_I32_B64 : SOP1_32_64 <"s_bcnt1_i32_b64",
252-
[(set i32:$sdst, (ctpop i64:$src0))]
252+
[(set i32:$sdst, (UniformUnaryFrag<ctpop> i64:$src0))]
253253
>;
254254
} // End Defs = [SCC]
255255

@@ -1371,7 +1371,7 @@ def : GCNPat <
13711371
>;
13721372

13731373
def : GCNPat <
1374-
(i64 (ctpop i64:$src)),
1374+
(i64 (UniformUnaryFrag<ctpop> i64:$src)),
13751375
(i64 (REG_SEQUENCE SReg_64,
13761376
(i32 (COPY_TO_REGCLASS (S_BCNT1_I32_B64 $src), SReg_32)), sub0,
13771377
(S_MOV_B32 (i32 0)), sub1))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; RUN: llc -march=amdgcn -stop-after=amdgpu-isel < %s | FileCheck -check-prefix=GCN %s
2+
3+
; GCN-LABEL: name: s_ctpop_i32
4+
; GCN: S_BCNT1_I32_B32
5+
define amdgpu_kernel void @s_ctpop_i32(i32 addrspace(1)* noalias %out, i32 %val) nounwind {
6+
%ctpop = call i32 @llvm.ctpop.i32(i32 %val) nounwind readnone
7+
store i32 %ctpop, i32 addrspace(1)* %out, align 4
8+
ret void
9+
}
10+
11+
; GCN-LABEL: name: s_ctpop_i64
12+
; GCN: %[[BCNT:[0-9]+]]:sreg_32 = S_BCNT1_I32_B64
13+
; GCN: %[[SREG1:[0-9]+]]:sreg_32 = COPY %[[BCNT]]
14+
; GCN: %[[SREG2:[0-9]+]]:sreg_32 = S_MOV_B32 0
15+
; GCN: REG_SEQUENCE killed %[[SREG1]], %subreg.sub0, killed %[[SREG2]], %subreg.sub1
16+
define amdgpu_kernel void @s_ctpop_i64(i32 addrspace(1)* noalias %out, i64 %val) nounwind {
17+
%ctpop = call i64 @llvm.ctpop.i64(i64 %val) nounwind readnone
18+
%truncctpop = trunc i64 %ctpop to i32
19+
store i32 %truncctpop, i32 addrspace(1)* %out, align 4
20+
ret void
21+
}
22+
23+
; GCN-LABEL: name: v_ctpop_i32
24+
; GCN: V_BCNT_U32_B32_e64
25+
define amdgpu_kernel void @v_ctpop_i32(i32 addrspace(1)* noalias %out, i32 addrspace(1)* noalias %in) nounwind {
26+
%tid = call i32 @llvm.amdgcn.workitem.id.x()
27+
%in.gep = getelementptr i32, i32 addrspace(1)* %in, i32 %tid
28+
%val = load i32, i32 addrspace(1)* %in.gep, align 4
29+
%ctpop = call i32 @llvm.ctpop.i32(i32 %val) nounwind readnone
30+
store i32 %ctpop, i32 addrspace(1)* %out, align 4
31+
ret void
32+
}
33+
34+
; GCN-LABEL: name: v_ctpop_i64
35+
; GCN: %[[BCNT1:[0-9]+]]:vgpr_32 = V_BCNT_U32_B32_e64 killed %{{[0-9]+}}, 0, implicit $exec
36+
; GCN: %[[BCNT2:[0-9]+]]:vgpr_32 = V_BCNT_U32_B32_e64 killed %{{[0-9]+}}, killed %[[BCNT1]], implicit $exec
37+
; GCN: %[[VGPR1:[0-9]+]]:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
38+
; GCN: REG_SEQUENCE killed %[[BCNT2]], %subreg.sub0, killed %[[VGPR1]], %subreg.sub1
39+
define amdgpu_kernel void @v_ctpop_i64(i32 addrspace(1)* noalias %out, i64 addrspace(1)* noalias %in) nounwind {
40+
%tid = call i32 @llvm.amdgcn.workitem.id.x()
41+
%in.gep = getelementptr i64, i64 addrspace(1)* %in, i32 %tid
42+
%val = load i64, i64 addrspace(1)* %in.gep, align 8
43+
%ctpop = call i64 @llvm.ctpop.i64(i64 %val) nounwind readnone
44+
%truncctpop = trunc i64 %ctpop to i32
45+
store i32 %truncctpop, i32 addrspace(1)* %out, align 4
46+
ret void
47+
}
48+
49+
declare i64 @llvm.ctpop.i64(i64) nounwind readnone
50+
51+
declare i32 @llvm.ctpop.i32(i32) nounwind readnone
52+
53+
declare i32 @llvm.amdgcn.workitem.id.x() nounwind readnone

0 commit comments

Comments
 (0)