aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll
blob: 9de6652d48cbf45c35c3e65e9fac495ce5ea867b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
; RUN: opt < %s -wasm-lower-em-ehsjlj -wasm-enable-eh -wasm-enable-sjlj -S | FileCheck %s
; RUN: llc < %s -wasm-enable-eh -wasm-enable-sjlj -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs

target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"

%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
%struct.Temp = type { i8 }
@_ZL3buf = internal global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16

; void test() {
;   int jmpval = setjmp(buf);
;   if (jmpval != 0)
;     return;
;   try {
;     foo();
;   } catch (...) {
;   }
; }
define void @setjmp_and_try() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-LABEL: @setjmp_and_try
entry:
  %call = call i32 @setjmp(ptr @_ZL3buf) #0
  %cmp = icmp ne i32 %call, 0
  br i1 %cmp, label %return, label %if.end

if.end:                                           ; preds = %entry
  invoke void @foo()
  to label %return unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %if.end
  %0 = catchswitch within none [label %catch.start] unwind to caller
; CHECK:       catch.dispatch:
; CHECK-NEXT:    catchswitch within none [label %catch.start] unwind label %catch.dispatch.longjmp

catch.start:                                      ; preds = %catch.dispatch
  %1 = catchpad within %0 [ptr null]
  %2 = call ptr @llvm.wasm.get.exception(token %1)
  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
  %4 = call ptr @__cxa_begin_catch(ptr %2) #2 [ "funclet"(token %1) ]
  call void @__cxa_end_catch() [ "funclet"(token %1) ]
  catchret from %1 to label %return
; CHECK:       catch.start:
; CHECK:         [[T0:%.*]] = catchpad within {{.*}} [ptr null]
; CHECK:         invoke void @__cxa_end_catch() [ "funclet"(token [[T0]]) ]
; CHECK-NEXT:    to label %.noexc unwind label %catch.dispatch.longjmp

; CHECK:       .noexc:
; CHECK-NEXT:     catchret from [[T0]] to label {{.*}}

return:                                           ; preds = %catch.start, %if.end, %entry
  ret void

; CHECK:       catch.dispatch.longjmp:
; CHECK-NEXT:    catchswitch within none [label %catch.longjmp] unwind to caller
}

; void setjmp_within_try() {
;   try {
;     foo();
;     int jmpval = setjmp(buf);
;     if (jmpval != 0)
;       return;
;     foo();
;   } catch (...) {
;   }
; }
define void @setjmp_within_try() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-LABEL: @setjmp_within_try
entry:
  %jmpval = alloca i32, align 4
  %exn.slot = alloca ptr, align 4
  invoke void @foo()
  to label %invoke.cont unwind label %catch.dispatch

invoke.cont:                                      ; preds = %entry
  %call = invoke i32 @setjmp(ptr @_ZL3buf) #0
  to label %invoke.cont1 unwind label %catch.dispatch

invoke.cont1:                                     ; preds = %invoke.cont
  store i32 %call, ptr %jmpval, align 4
  %0 = load i32, ptr %jmpval, align 4
  %cmp = icmp ne i32 %0, 0
  br i1 %cmp, label %if.then, label %if.end

if.then:                                          ; preds = %invoke.cont1
  br label %try.cont

if.end:                                           ; preds = %invoke.cont1
  invoke void @foo()
  to label %invoke.cont2 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %if.end, %invoke.cont, %entry
  %1 = catchswitch within none [label %catch.start] unwind to caller

; CHECK:       catch.dispatch:
; CHECK:         catchswitch within none [label %catch.start] unwind label %catch.dispatch.longjmp
catch.start:                                      ; preds = %catch.dispatch
  %2 = catchpad within %1 [ptr null]
  %3 = call ptr @llvm.wasm.get.exception(token %2)
  store ptr %3, ptr %exn.slot, align 4
  %4 = call i32 @llvm.wasm.get.ehselector(token %2)
  br label %catch

catch:                                            ; preds = %catch.start
  %exn = load ptr, ptr %exn.slot, align 4
  %5 = call ptr @__cxa_begin_catch(ptr %exn) #2 [ "funclet"(token %2) ]
  call void @__cxa_end_catch() [ "funclet"(token %2) ]
  catchret from %2 to label %catchret.dest
; CHECK: catch:                                            ; preds = %catch.start
; CHECK-NEXT:   %exn = load ptr, ptr %exn.slot, align 4
; CHECK-NEXT:   %5 = call ptr @__cxa_begin_catch(ptr %exn) #3 [ "funclet"(token %2) ]
; CHECK-NEXT:   invoke void @__cxa_end_catch() [ "funclet"(token %2) ]
; CHECK-NEXT:           to label %.noexc unwind label %catch.dispatch.longjmp

catchret.dest:                                    ; preds = %catch
  br label %try.cont

try.cont:                                         ; preds = %invoke.cont2, %catchret.dest, %if.then
  ret void

invoke.cont2:                                     ; preds = %if.end
  br label %try.cont

; CHECK:       catch.dispatch.longjmp:
; CHECK-NEXT:    catchswitch within none [label %catch.longjmp] unwind to caller
}

; void setjmp_and_nested_try() {
;   int jmpval = setjmp(buf);
;   if (jmpval != 0)
;     return;
;   try {
;     foo();
;     try {
;       foo();
;     } catch (...) {
;       foo();
;     }
;   } catch (...) {
;   }
; }
define void @setjmp_and_nested_try() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-LABEL: @setjmp_and_nested_try
entry:
  %call = call i32 @setjmp(ptr @_ZL3buf) #0
  %cmp = icmp ne i32 %call, 0
  br i1 %cmp, label %try.cont10, label %if.end

if.end:                                           ; preds = %entry
  invoke void @foo()
  to label %invoke.cont unwind label %catch.dispatch5

invoke.cont:                                      ; preds = %if.end
  invoke void @foo()
  to label %try.cont10 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %invoke.cont
  %0 = catchswitch within none [label %catch.start] unwind label %catch.dispatch5

catch.start:                                      ; preds = %catch.dispatch
  %1 = catchpad within %0 [ptr null]
  %2 = call ptr @llvm.wasm.get.exception(token %1)
  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
  %4 = call ptr @__cxa_begin_catch(ptr %2) #2 [ "funclet"(token %1) ]
  invoke void @foo() [ "funclet"(token %1) ]
  to label %invoke.cont2 unwind label %ehcleanup

invoke.cont2:                                     ; preds = %catch.start
  invoke void @__cxa_end_catch() [ "funclet"(token %1) ]
  to label %invoke.cont3 unwind label %catch.dispatch5

invoke.cont3:                                     ; preds = %invoke.cont2
  catchret from %1 to label %try.cont10

ehcleanup:                                        ; preds = %catch.start
  %5 = cleanuppad within %1 []
  invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
  to label %invoke.cont4 unwind label %terminate
; CHECK:       ehcleanup:
; CHECK-NEXT:    [[T0:%.*]] = cleanuppad within {{.*}} []
; CHECK-NEXT:    invoke void @__cxa_end_catch() [ "funclet"(token [[T0]]) ]
; CHECK-NEXT:    to label %invoke.cont4 unwind label %terminate

invoke.cont4:                                     ; preds = %ehcleanup
  cleanupret from %5 unwind label %catch.dispatch5
; CHECK:       invoke.cont4:
; CHECK-NEXT:    cleanupret from [[T0]] unwind label %catch.dispatch5

catch.dispatch5:                                  ; preds = %invoke.cont4, %invoke.cont2, %catch.dispatch, %if.end
  %6 = catchswitch within none [label %catch.start6] unwind to caller
; CHECK:       catch.dispatch5:
; CHECK-NEXT:    catchswitch within none [label %catch.start6] unwind label %catch.dispatch.longjmp

catch.start6:                                     ; preds = %catch.dispatch5
  %7 = catchpad within %6 [ptr null]
  %8 = call ptr @llvm.wasm.get.exception(token %7)
  %9 = call i32 @llvm.wasm.get.ehselector(token %7)
  %10 = call ptr @__cxa_begin_catch(ptr %8) #2 [ "funclet"(token %7) ]
  call void @__cxa_end_catch() [ "funclet"(token %7) ]
  catchret from %7 to label %try.cont10
; CHECK:       catch.start6:
; CHECK-NEXT:    [[T1:%.*]] = catchpad within {{.*}} [ptr null]
; CHECK-NEXT:    call ptr @llvm.wasm.get.exception(token [[T1]])
; CHECK-NEXT:    call i32 @llvm.wasm.get.ehselector(token [[T1]])
; CHECK-NEXT:    call ptr @__cxa_begin_catch(ptr {{.*}}) {{.*}} [ "funclet"(token [[T1]]) ]
; CHECK:         invoke void @__cxa_end_catch() [ "funclet"(token [[T1]]) ]
; CHECK-NEXT:    to label %.noexc unwind label %catch.dispatch.longjmp

; CHECK:       .noexc:
; CHECK-NEXT:    catchret from [[T1]] to label {{.*}}

try.cont10:                                       ; preds = %catch.start6, %invoke.cont3, %invoke.cont, %entry
  ret void

terminate:                                        ; preds = %ehcleanup
  %11 = cleanuppad within %5 []
  call void @terminate() #3 [ "funclet"(token %11) ]
  unreachable
; CHECK:       terminate:
; CHECK-NEXT:    [[T2:%.*]] = cleanuppad within [[T0]] []
; Note that this call unwinds not to %catch.dispatch.longjmp but to
; %catch.dispatch5. This call is enclosed in the cleanuppad above, but there is
; no matching catchret, which has the unwind destination. So this checks this
; cleanuppad's parent, which is in 'ehcleanup', and unwinds to its unwind
; destination, %catch.dispatch5.
; This call was originally '_ZSt9terminatev', which is the mangled name for
; 'std::terminate'. But we listed that as "cannot longjmp", we changed
; the name of the function in this test to show the case in which a call has to
; change to an invoke whose unwind destination is determined by its parent
; chain.
; CHECK-NEXT:    invoke void @terminate() {{.*}} [ "funclet"(token [[T2]]) ]
; CHECK-NEXT:    to label %[[NOEXC:.*]] unwind label %catch.dispatch5

; CHECK:       [[NOEXC]]:
; CHECK-NEXT:    unreachable

; CHECK:       catch.dispatch.longjmp:
; CHECK-NEXT:    catchswitch within none [label %catch.longjmp] unwind to caller
}

; void @cleanuppad_no_parent {
;   jmp_buf buf;
;   Temp t;
;   setjmp(buf);
; }
define void @cleanuppad_no_parent() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-LABEL: @cleanuppad_no_parent
entry:
  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
  %t = alloca %struct.Temp, align 1
  %call = invoke i32 @setjmp(ptr noundef %buf) #0
          to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  %call1 = call noundef ptr @_ZN4TempD2Ev(ptr noundef %t) #2
  ret void

ehcleanup:                                        ; preds = %entry
  %0 = cleanuppad within none []
  ; After SjLj transformation, this will be converted to an invoke that
  ; eventually unwinds to %catch.dispatch.longjmp. But in case a call has a
  ; "funclet" attribute, we should unwind to the funclet's unwind destination
  ; first to preserve the scoping structure. But this call's parent is %0
  ; (cleanuppad), whose parent is 'none', so we should unwind directly to
  ; %catch.dispatch.longjmp.
  %call2 = call noundef ptr @_ZN4TempD2Ev(ptr noundef %t) #2 [ "funclet"(token %0) ]
; CHECK: %call11 = invoke {{.*}} ptr @_ZN4TempD2Ev(ptr
; CHECK-NEXT:    to label {{.*}} unwind label %catch.dispatch.longjmp
  cleanupret from %0 unwind to caller
}

; This case was adapted from @cleanuppad_no_parent by removing allocas and
; destructor calls, to generate a situation that there's only 'invoke @setjmp'
; and no other longjmpable calls.
define i32 @setjmp_only() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-LABEL: @setjmp_only
entry:
  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
  %call = invoke i32 @setjmp(ptr noundef %buf) #0
          to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  ret i32 %call
; CHECK: invoke.cont:
; The remaining setjmp call is converted to constant 0, because setjmp returns 0
; when called directly.
; CHECK:   ret i32 0

ehcleanup:                                        ; preds = %entry
  %0 = cleanuppad within none []
  cleanupret from %0 unwind to caller
}

declare void @foo()
; Function Attrs: nounwind
declare ptr @_ZN4TempD2Ev(ptr %this) #2
; Function Attrs: returns_twice
declare i32 @setjmp(ptr) #0
; Function Attrs: noreturn
declare void @longjmp(ptr, i32) #1
declare i32 @__gxx_wasm_personality_v0(...)
; Function Attrs: nounwind
declare ptr @llvm.wasm.get.exception(token) #2
; Function Attrs: nounwind
declare i32 @llvm.wasm.get.ehselector(token) #2
declare ptr @__cxa_begin_catch(ptr)
declare void @__cxa_end_catch()
declare void @terminate()

attributes #0 = { returns_twice }
attributes #1 = { noreturn }
attributes #2 = { nounwind }
attributes #3 = { noreturn nounwind }