1 ; RUN: opt %s -analyze -divergence -use-gpu-divergence-analysis | FileCheck %s
3 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
4 target triple = "nvptx64-nvidia-cuda"
6 ; return (n < 0 ? a + threadIdx.x : b + threadIdx.x)
7 define i32 @no_diverge(i32 %n, i32 %a, i32 %b) {
8 ; CHECK-LABEL: Printing analysis 'Legacy Divergence Analysis' for function 'no_diverge'
10 %tid = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
11 %cond = icmp slt i32 %n, 0
12 br i1 %cond, label %then, label %else ; uniform
13 ; CHECK-NOT: DIVERGENT: br i1 %cond,
15 %a1 = add i32 %a, %tid
18 %b2 = add i32 %b, %tid
21 %c = phi i32 [ %a1, %then ], [ %b2, %else ]
26 ; if (threadIdx.x < 5) // divergent: data dependent
28 ; return c; // c is divergent: sync dependent
29 define i32 @sync(i32 %a, i32 %b) {
30 ; CHECK-LABEL: Printing analysis 'Legacy Divergence Analysis' for function 'sync'
32 %tid = call i32 @llvm.nvvm.read.ptx.sreg.tid.y()
33 %cond = icmp slt i32 %tid, 5
34 br i1 %cond, label %bb2, label %bb3
35 ; CHECK: DIVERGENT: br i1 %cond,
39 %c = phi i32 [ %a, %bb1 ], [ %b, %bb2 ] ; sync dependent on tid
40 ; CHECK: DIVERGENT: %c =
45 ; if (threadIdx.x >= 5) { // divergent
46 ; c = (n < 0 ? a : b); // c here is uniform because n is uniform
48 ; // c here is divergent because it is sync dependent on threadIdx.x >= 5
50 define i32 @mixed(i32 %n, i32 %a, i32 %b) {
51 ; CHECK-LABEL: Printing analysis 'Legacy Divergence Analysis' for function 'mixed'
53 %tid = call i32 @llvm.nvvm.read.ptx.sreg.tid.z()
54 %cond = icmp slt i32 %tid, 5
55 br i1 %cond, label %bb6, label %bb2
56 ; CHECK: DIVERGENT: br i1 %cond,
58 %cond2 = icmp slt i32 %n, 0
59 br i1 %cond2, label %bb4, label %bb3
65 %c = phi i32 [ %a, %bb3 ], [ %b, %bb4 ]
66 ; CHECK-NOT: DIVERGENT: %c =
69 %c2 = phi i32 [ 0, %bb1], [ %c, %bb5 ]
70 ; CHECK: DIVERGENT: %c2 =
74 ; We conservatively treats all parameters of a __device__ function as divergent.
75 define i32 @device(i32 %n, i32 %a, i32 %b) {
76 ; CHECK-LABEL: Printing analysis 'Legacy Divergence Analysis' for function 'device'
77 ; CHECK: DIVERGENT: i32 %n
78 ; CHECK: DIVERGENT: i32 %a
79 ; CHECK: DIVERGENT: i32 %b
81 %cond = icmp slt i32 %n, 0
82 br i1 %cond, label %then, label %else
83 ; CHECK: DIVERGENT: br i1 %cond,
89 %c = phi i32 [ %a, %then ], [ %b, %else ]
95 ; i++; // i here is uniform
96 ; } while (i < laneid);
97 ; return i == 10 ? 0 : 1; // i here is divergent
99 ; The i defined in the loop is used outside.
101 ; CHECK-LABEL: Printing analysis 'Legacy Divergence Analysis' for function 'loop'
103 %laneid = call i32 @llvm.nvvm.read.ptx.sreg.laneid()
106 %i = phi i32 [ 0, %entry ], [ %i1, %loop ]
107 ; CHECK-NOT: DIVERGENT: %i =
109 %exit_cond = icmp sge i32 %i1, %laneid
110 br i1 %exit_cond, label %loop_exit, label %loop
112 %cond = icmp eq i32 %i, 10
113 br i1 %cond, label %then, label %else
114 ; CHECK: DIVERGENT: br i1 %cond,
121 ; Same as @loop, but the loop is in the LCSSA form.
122 define i32 @lcssa() {
123 ; CHECK-LABEL: Printing analysis 'Legacy Divergence Analysis' for function 'lcssa'
125 %tid = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
128 %i = phi i32 [ 0, %entry ], [ %i1, %loop ]
129 ; CHECK-NOT: DIVERGENT: %i =
131 %exit_cond = icmp sge i32 %i1, %tid
132 br i1 %exit_cond, label %loop_exit, label %loop
134 %i.lcssa = phi i32 [ %i, %loop ]
135 ; CHECK: DIVERGENT: %i.lcssa =
136 %cond = icmp eq i32 %i.lcssa, 10
137 br i1 %cond, label %then, label %else
138 ; CHECK: DIVERGENT: br i1 %cond,
145 ; Verifies sync-dependence is computed correctly in the absense of loops.
146 define i32 @sync_no_loop(i32 %arg) {
149 %tid = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
150 %1 = icmp sge i32 %tid, 10
151 br i1 %1, label %bb1, label %bb2
161 ; CHECK-NOT: DIVERGENT: %2
165 declare i32 @llvm.nvvm.read.ptx.sreg.tid.x()
166 declare i32 @llvm.nvvm.read.ptx.sreg.tid.y()
167 declare i32 @llvm.nvvm.read.ptx.sreg.tid.z()
168 declare i32 @llvm.nvvm.read.ptx.sreg.laneid()
170 !nvvm.annotations = !{!0, !1, !2, !3, !4}
171 !0 = !{i32 (i32, i32, i32)* @no_diverge, !"kernel", i32 1}
172 !1 = !{i32 (i32, i32)* @sync, !"kernel", i32 1}
173 !2 = !{i32 (i32, i32, i32)* @mixed, !"kernel", i32 1}
174 !3 = !{i32 ()* @loop, !"kernel", i32 1}
175 !4 = !{i32 (i32)* @sync_no_loop, !"kernel", i32 1}