xref: /llvm-project/llvm/test/CodeGen/SPIRV/structurizer/cf.while.break.ll (revision cba70550ccf55c6ad3daa621bb8caf3c4ca6cbd7)
1; RUN: llc -mtriple=spirv-unknown-vulkan-compute -O0 %s -o - | FileCheck %s
2; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan-compute %s -o - -filetype=obj | spirv-val %}
3
4;
5; int foo() { return true; }
6;
7; [numthreads(1, 1, 1)]
8; void main() {
9;   int val = 0;
10;   int i = 0;
11;
12;   while (i < 10) {
13;     val = i;
14;     if (val > 5) {
15;       break;
16;     }
17;
18;     if (val > 6) {
19;       break;
20;       break;       // No SPIR-V should be emitted for this statement.
21;       val++;       // No SPIR-V should be emitted for this statement.
22;       while(true); // No SPIR-V should be emitted for this statement.
23;       --i;         // No SPIR-V should be emitted for this statement.
24;     }
25;   }
26;
27;   ////////////////////////////////////////////////////////////////////////////////
28;   // Nested while loops with break statements                                   //
29;   // Each break statement should branch to the corresponding loop's break block //
30;   ////////////////////////////////////////////////////////////////////////////////
31;
32;   while (true) {
33;     i++;
34;     while(i<20) {
35;       val = i;
36;       {{break;}}
37;     }
38;     --i;
39;     break;
40;   }
41; }
42
43; CHECK: %[[#func_16:]] = OpFunction %[[#uint:]] DontInline %[[#]]
44; CHECK:    %[[#bb44:]] = OpLabel
45; CHECK:                  OpReturnValue %[[#]]
46; CHECK:                  OpFunctionEnd
47; CHECK: %[[#func_19:]] = OpFunction %[[#void:]] DontInline %[[#]]
48; CHECK:    %[[#bb45:]] = OpLabel
49; CHECK:                  OpBranch %[[#bb46:]]
50; CHECK:    %[[#bb46:]] = OpLabel
51; CHECK:                  OpLoopMerge %[[#bb47:]] %[[#bb48:]] None
52; CHECK:                  OpBranchConditional %[[#]] %[[#bb49:]] %[[#bb47:]]
53; CHECK:    %[[#bb49:]] = OpLabel
54; CHECK:                  OpBranchConditional %[[#]] %[[#bb47:]] %[[#bb50:]]
55; CHECK:    %[[#bb50:]] = OpLabel
56; CHECK:                  OpBranchConditional %[[#]] %[[#bb47:]] %[[#bb48:]]
57; CHECK:    %[[#bb48:]] = OpLabel
58; CHECK:                  OpBranch %[[#bb46:]]
59; CHECK:    %[[#bb47:]] = OpLabel
60; CHECK:                  OpSelectionMerge %[[#bb51:]] None
61; CHECK:                  OpSwitch %[[#]] %[[#bb51:]] 1 %[[#bb51:]] 2 %[[#bb51:]]
62; CHECK:    %[[#bb51:]] = OpLabel
63; CHECK:                  OpBranch %[[#bb52:]]
64; CHECK:    %[[#bb52:]] = OpLabel
65; CHECK:                  OpBranch %[[#bb53:]]
66; CHECK:    %[[#bb53:]] = OpLabel
67; CHECK:                  OpBranch %[[#bb54:]]
68; CHECK:    %[[#bb54:]] = OpLabel
69; CHECK:                  OpSelectionMerge %[[#bb55:]] None
70; CHECK:                  OpBranchConditional %[[#]] %[[#bb56:]] %[[#bb55:]]
71; CHECK:    %[[#bb56:]] = OpLabel
72; CHECK:                  OpBranch %[[#bb55:]]
73; CHECK:    %[[#bb55:]] = OpLabel
74; CHECK:                  OpBranch %[[#bb57:]]
75; CHECK:    %[[#bb57:]] = OpLabel
76; CHECK:                  OpReturn
77; CHECK:                  OpFunctionEnd
78; CHECK: %[[#func_40:]] = OpFunction %[[#void:]] None %[[#]]
79; CHECK:    %[[#bb58:]] = OpLabel
80; CHECK:                  OpReturn
81; CHECK:                  OpFunctionEnd
82
83target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1"
84target triple = "spirv-unknown-vulkan1.3-compute"
85
86; Function Attrs: convergent noinline norecurse nounwind optnone
87define spir_func noundef i32 @_Z3foov() #0 {
88entry:
89  %0 = call token @llvm.experimental.convergence.entry()
90  ret i32 1
91}
92
93; Function Attrs: convergent nocallback nofree nosync nounwind willreturn memory(none)
94declare token @llvm.experimental.convergence.entry() #1
95
96; Function Attrs: convergent noinline norecurse nounwind optnone
97define internal spir_func void @main() #0 {
98entry:
99  %0 = call token @llvm.experimental.convergence.entry()
100  %val = alloca i32, align 4
101  %i = alloca i32, align 4
102  store i32 0, ptr %val, align 4
103  store i32 0, ptr %i, align 4
104  br label %while.cond
105
106while.cond:                                       ; preds = %if.end4, %entry
107  %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
108  %2 = load i32, ptr %i, align 4
109  %cmp = icmp slt i32 %2, 10
110  br i1 %cmp, label %while.body, label %while.end
111
112while.body:                                       ; preds = %while.cond
113  %3 = load i32, ptr %i, align 4
114  store i32 %3, ptr %val, align 4
115  %4 = load i32, ptr %val, align 4
116  %cmp1 = icmp sgt i32 %4, 5
117  br i1 %cmp1, label %if.then, label %if.end
118
119if.then:                                          ; preds = %while.body
120  br label %while.end
121
122if.end:                                           ; preds = %while.body
123  %5 = load i32, ptr %val, align 4
124  %cmp2 = icmp sgt i32 %5, 6
125  br i1 %cmp2, label %if.then3, label %if.end4
126
127if.then3:                                         ; preds = %if.end
128  br label %while.end
129
130if.end4:                                          ; preds = %if.end
131  br label %while.cond
132
133while.end:                                        ; preds = %if.then3, %if.then, %while.cond
134  br label %while.cond5
135
136while.cond5:                                      ; preds = %while.end
137  %6 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
138  br label %while.body6
139
140while.body6:                                      ; preds = %while.cond5
141  %7 = load i32, ptr %i, align 4
142  %inc = add nsw i32 %7, 1
143  store i32 %inc, ptr %i, align 4
144  br label %while.cond7
145
146while.cond7:                                      ; preds = %while.body6
147  %8 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %6) ]
148  %9 = load i32, ptr %i, align 4
149  %cmp8 = icmp slt i32 %9, 20
150  br i1 %cmp8, label %while.body9, label %while.end10
151
152while.body9:                                      ; preds = %while.cond7
153  %10 = load i32, ptr %i, align 4
154  store i32 %10, ptr %val, align 4
155  br label %while.end10
156
157while.end10:                                      ; preds = %while.body9, %while.cond7
158  %11 = load i32, ptr %i, align 4
159  %dec = add nsw i32 %11, -1
160  store i32 %dec, ptr %i, align 4
161  br label %while.end11
162
163while.end11:                                      ; preds = %while.end10
164  ret void
165}
166
167; Function Attrs: convergent norecurse
168define void @main.1() #2 {
169entry:
170  call void @main()
171  ret void
172}
173
174; Function Attrs: convergent nocallback nofree nosync nounwind willreturn memory(none)
175declare token @llvm.experimental.convergence.loop() #1
176
177attributes #0 = { convergent noinline norecurse nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
178attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }
179attributes #2 = { convergent norecurse "frame-pointer"="all" "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
180
181!llvm.module.flags = !{!0, !1, !2}
182
183
184!0 = !{i32 1, !"wchar_size", i32 4}
185!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
186!2 = !{i32 7, !"frame-pointer", i32 2}
187
188
189