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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
; RUN: llc < %s -asm-verbose=false -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck --implicit-check-not=ehgcr -allow-deprecated-dag-overlap %s
; RUN: llc < %s -asm-verbose=false -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs -O0
; RUN: llc < %s -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling
; RUN: llc < %s -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -filetype=obj
; RUN: llc < %s -mtriple=wasm64-unknown-unknown -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck --implicit-check-not=ehgcr -allow-deprecated-dag-overlap %s --check-prefix=WASM64
target triple = "wasm32-unknown-unknown"
%struct.Temp = type { i8 }
@_ZTIi = external dso_local constant ptr
; CHECK: .tagtype __cpp_exception i32
; CHECK-LABEL: throw:
; CHECK: throw __cpp_exception
; CHECK-NOT: unreachable
define void @throw(ptr %p) {
call void @llvm.wasm.throw(i32 0, ptr %p)
ret void
}
; Simple test with a try-catch
;
; void foo();
; void catch() {
; try {
; foo();
; } catch (int) {
; }
; }
; CHECK-LABEL: catch:
; WASM64-LABEL: catch:
; CHECK: global.get __stack_pointer
; CHECK: local.set 0
; CHECK: block
; CHECK: block () -> (i32, exnref)
; CHECK: try_table (catch_ref __cpp_exception 0)
; WASM64: block () -> (i64, exnref)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
; CHECK: unreachable
; CHECK: end_block
; CHECK: local.set 2
; CHECK: local.get 0
; CHECK: global.set __stack_pointer
; CHECK: i32.store __wasm_lpad_context
; CHECK: call _Unwind_CallPersonality
; CHECK: block
; CHECK: br_if 0
; CHECK: call __cxa_begin_catch
; CHECK: call __cxa_end_catch
; CHECK: br 1
; CHECK: end_block
; CHECK: local.get 2
; CHECK: throw_ref
; CHECK: end_block
define void @catch() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
catch.start: ; preds = %catch.dispatch
%1 = catchpad within %0 [ptr @_ZTIi]
%2 = call ptr @llvm.wasm.get.exception(token %1)
%3 = call i32 @llvm.wasm.get.ehselector(token %1)
%4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
%matches = icmp eq i32 %3, %4
br i1 %matches, label %catch, label %rethrow
catch: ; preds = %catch.start
%5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
rethrow: ; preds = %catch.start
call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
unreachable
try.cont: ; preds = %catch, %entry
ret void
}
; Destructor (cleanup) test
;
; void foo();
; struct Temp {
; ~Temp() {}
; };
; void cleanup() {
; Temp t;
; foo();
; }
; CHECK-LABEL: cleanup:
; CHECK: block
; CHECK: block exnref
; CHECK: try_table (catch_all_ref 0)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
; CHECK: end_block
; CHECK: local.set 1
; CHECK: global.set __stack_pointer
; CHECK: call _ZN4TempD2Ev
; CHECK: local.get 1
; CHECK: throw_ref
; CHECK: end_block
; CHECK: call _ZN4TempD2Ev
define void @cleanup() personality ptr @__gxx_wasm_personality_v0 {
entry:
%t = alloca %struct.Temp, align 1
invoke void @foo()
to label %invoke.cont unwind label %ehcleanup
invoke.cont: ; preds = %entry
%call = call ptr @_ZN4TempD2Ev(ptr %t)
ret void
ehcleanup: ; preds = %entry
%0 = cleanuppad within none []
%call1 = call ptr @_ZN4TempD2Ev(ptr %t) [ "funclet"(token %0) ]
cleanupret from %0 unwind to caller
}
; Calling a function that may throw within a 'catch (...)' generates a
; terminatepad, because __cxa_end_catch() also can throw within 'catch (...)'.
;
; void foo();
; void terminatepad() {
; try {
; foo();
; } catch (...) {
; foo();
; }
; }
; CHECK-LABEL: terminatepad
; WASM64-LABEL: terminatepad
; CHECK: block
; CHECK: block i32
; WASM64: block i64
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
; CHECK: end_block
; CHECK: call __cxa_begin_catch
; CHECK: block
; CHECK: block exnref
; CHECK: try_table (catch_all_ref 0)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
; CHECK: end_block
; CHECK: local.set 2
; CHECK: block
; CHECK: block
; CHECK: try_table (catch_all 0)
; CHECK: call __cxa_end_catch
; CHECK: br 2
; CHECK: end_try_table
; CHECK: end_block
; CHECK: call _ZSt9terminatev
; CHECK: unreachable
; CHECK: end_block
; CHECK: local.get 2
; CHECK: throw_ref
; CHECK: end_block
; CHECK: call __cxa_end_catch
; CHECK: end_block
define void @terminatepad() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
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) [ "funclet"(token %1) ]
invoke void @foo() [ "funclet"(token %1) ]
to label %invoke.cont1 unwind label %ehcleanup
invoke.cont1: ; preds = %catch.start
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
try.cont: ; preds = %invoke.cont1, %entry
ret void
ehcleanup: ; preds = %catch.start
%5 = cleanuppad within %1 []
invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
to label %invoke.cont2 unwind label %terminate
invoke.cont2: ; preds = %ehcleanup
cleanupret from %5 unwind to caller
terminate: ; preds = %ehcleanup
%6 = cleanuppad within %5 []
call void @_ZSt9terminatev() #2 [ "funclet"(token %6) ]
unreachable
}
; Tests prologues and epilogues are not generated within EH scopes.
; They should not be treated as funclets; BBs starting with a catch instruction
; should not have a prologue, and BBs ending with a catchret/cleanupret should
; not have an epilogue. This is separate from __stack_pointer restoring
; instructions after a catch instruction.
;
; void bar(int) noexcept;
; void no_prolog_epilog_in_ehpad() {
; int stack_var = 0;
; bar(stack_var);
; try {
; foo();
; } catch (int) {
; foo();
; }
; }
; CHECK-LABEL: no_prolog_epilog_in_ehpad
; CHECK: call bar
; CHECK: block
; CHECK: block () -> (i32, exnref)
; CHECK: try_table (catch_ref __cpp_exception 0)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
; CHECK: end_block
; CHECK: local.set 2
; CHECK-NOT: global.get __stack_pointer
; CHECK: global.set __stack_pointer
; CHECK: block
; CHECK: block
; CHECK: br_if 0
; CHECK: call __cxa_begin_catch
; CHECK: block exnref
; CHECK: try_table (catch_all_ref 0)
; CHECK: call foo
; CHECK: br 3
; CHECK: end_try_table
; CHECK: end_block
; CHECK: local.set 2
; CHECK-NOT: global.get __stack_pointer
; CHECK: global.set __stack_pointer
; CHECK: call __cxa_end_catch
; CHECK: local.get 2
; CHECK: throw_ref
; CHECK-NOT: global.set __stack_pointer
; CHECK: end_block
; CHECK: local.get 2
; CHECK: throw_ref
; CHECK: end_block
; CHECK-NOT: global.set __stack_pointer
; CHECK: call __cxa_end_catch
; CHECK: end_block
define void @no_prolog_epilog_in_ehpad() personality ptr @__gxx_wasm_personality_v0 {
entry:
%stack_var = alloca i32, align 4
call void @bar(ptr %stack_var)
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
catch.start: ; preds = %catch.dispatch
%1 = catchpad within %0 [ptr @_ZTIi]
%2 = call ptr @llvm.wasm.get.exception(token %1)
%3 = call i32 @llvm.wasm.get.ehselector(token %1)
%4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
%matches = icmp eq i32 %3, %4
br i1 %matches, label %catch, label %rethrow
catch: ; preds = %catch.start
%5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
%6 = load float, ptr %5, align 4
invoke void @foo() [ "funclet"(token %1) ]
to label %invoke.cont1 unwind label %ehcleanup
invoke.cont1: ; preds = %catch
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
rethrow: ; preds = %catch.start
call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
unreachable
try.cont: ; preds = %invoke.cont1, %entry
ret void
ehcleanup: ; preds = %catch
%7 = cleanuppad within %1 []
call void @__cxa_end_catch() [ "funclet"(token %7) ]
cleanupret from %7 unwind to caller
}
; When a function does not have stack-allocated objects, it does not need to
; store SP back to __stack_pointer global at the epilog.
;
; void foo();
; void no_sp_writeback() {
; try {
; foo();
; } catch (...) {
; }
; }
; CHECK-LABEL: no_sp_writeback
; CHECK: block
; CHECK: block i32
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
; CHECK: end_block
; CHECK: call __cxa_begin_catch
; CHECK: call __cxa_end_catch
; CHECK: end_block
; CHECK-NOT: global.set __stack_pointer
; CHECK: end_function
define void @no_sp_writeback() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
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) [ "funclet"(token %1) ]
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
try.cont: ; preds = %catch.start, %entry
ret void
}
; When the result of @llvm.wasm.get.exception is not used. This is created to
; fix a bug in LateEHPrepare and this should not crash.
define void @get_exception_wo_use() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
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)
catchret from %1 to label %try.cont
try.cont: ; preds = %catch.start, %entry
ret void
}
; Tests a case when a cleanup region (cleanuppad ~ clanupret) contains another
; catchpad
define void @complex_cleanup_region() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %invoke.cont unwind label %ehcleanup
invoke.cont: ; preds = %entry
ret void
ehcleanup: ; preds = %entry
%0 = cleanuppad within none []
invoke void @foo() [ "funclet"(token %0) ]
to label %ehcleanupret unwind label %catch.dispatch
catch.dispatch: ; preds = %ehcleanup
%1 = catchswitch within %0 [label %catch.start] unwind label %ehcleanup.1
catch.start: ; preds = %catch.dispatch
%2 = catchpad within %1 [ptr null]
%3 = call ptr @llvm.wasm.get.exception(token %2)
%4 = call i32 @llvm.wasm.get.ehselector(token %2)
catchret from %2 to label %ehcleanupret
ehcleanup.1: ; preds = %catch.dispatch
%5 = cleanuppad within %0 []
unreachable
ehcleanupret: ; preds = %catch.start, %ehcleanup
cleanupret from %0 unwind to caller
}
; Regression test for the bug that 'rethrow' was not treated correctly as a
; terminator in isel.
define void @rethrow_terminator() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind label %ehcleanup
catch.start: ; preds = %catch.dispatch
%1 = catchpad within %0 [ptr @_ZTIi]
%2 = call ptr @llvm.wasm.get.exception(token %1)
%3 = call i32 @llvm.wasm.get.ehselector(token %1)
%4 = call i32 @llvm.eh.typeid.for.p0(ptr @_ZTIi)
%matches = icmp eq i32 %3, %4
br i1 %matches, label %catch, label %rethrow
catch: ; preds = %catch.start
%5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
%6 = load i32, ptr %5, align 4
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
rethrow: ; preds = %catch.start
invoke void @llvm.wasm.rethrow() #1 [ "funclet"(token %1) ]
to label %unreachable unwind label %ehcleanup
try.cont: ; preds = %entry, %catch
ret void
ehcleanup: ; preds = %rethrow, %catch.dispatch
; 'rethrow' BB is this BB's predecessor, and its
; 'invoke void @llvm.wasm.rethrow()' is lowered down to a 'RETHROW' in Wasm
; MIR. And this 'phi' creates 'CONST_I32' instruction in the predecessor
; 'rethrow' BB. If 'RETHROW' is not treated correctly as a terminator, it can
; create a BB like
; bb.3.rethrow:
; RETHROW 0
; %0 = CONST_I32 20
; BR ...
%tmp = phi i32 [ 10, %catch.dispatch ], [ 20, %rethrow ]
%7 = cleanuppad within none []
call void @take_i32(i32 %tmp) [ "funclet"(token %7) ]
cleanupret from %7 unwind to caller
unreachable: ; preds = %rethrow
unreachable
}
; The bitcode below is generated when the code below is compiled and
; Temp::~Temp() is inlined into inlined_cleanupret():
;
; void inlined_cleanupret() {
; try {
; Temp t;
; throw 2;
; } catch (...)
; }
;
; Temp::~Temp() {
; try {
; throw 1;
; } catch (...) {
; }
; }
;
; ~Temp() generates cleanupret, which is lowered to a 'throw_ref' later. That
; throw_ref's argument should correctly target the top-level cleanuppad
; (catch_all_ref). This is a regression test for the bug where we did not
; compute throw_ref's argument correctly.
; CHECK-LABEL: inlined_cleanupret:
; CHECK: block exnref
; CHECK: block
; CHECK: block exnref
; CHECK: try_table (catch_all_ref 0)
; CHECK: call __cxa_throw
; CHECK: end_try_table
; CHECK: end_block
; try_table (catch_all_ref 0)'s caught exception is stored in local 2
; CHECK: local.set 2
; CHECK: block
; CHECK: try_table (catch_all 0)
; CHECK: block
; CHECK: block i32
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: call __cxa_throw
; CHECK: end_try_table
; CHECK: end_block
; CHECK: call __cxa_end_catch
; CHECK: block i32
; CHECK: try_table (catch_all_ref 5)
; CHECK: try_table (catch __cpp_exception 1)
; Note that the throw_ref below targets the top-level catch_all_ref (local 2)
; CHECK: local.get 2
; CHECK: throw_ref
; CHECK: end_try_table
; CHECK: end_try_table
; CHECK: end_block
; CHECK: try_table (catch_all_ref 4)
; CHECK: call __cxa_end_catch
; CHECK: end_try_table
; CHECK: return
; CHECK: end_block
; CHECK: end_try_table
; CHECK: end_block
; CHECK: call _ZSt9terminatev
; CHECK: end_block
; CHECK: end_block
; CHECK: throw_ref
define void @inlined_cleanupret() personality ptr @__gxx_wasm_personality_v0 {
entry:
%exception = tail call ptr @__cxa_allocate_exception(i32 4)
store i32 2, ptr %exception, align 16
invoke void @__cxa_throw(ptr nonnull %exception, ptr nonnull @_ZTIi, ptr null)
to label %unreachable unwind label %ehcleanup
ehcleanup: ; preds = %entry
%0 = cleanuppad within none []
%exception.i = call ptr @__cxa_allocate_exception(i32 4) [ "funclet"(token %0) ]
store i32 1, ptr %exception.i, align 16
invoke void @__cxa_throw(ptr nonnull %exception.i, ptr nonnull @_ZTIi, ptr null) [ "funclet"(token %0) ]
to label %unreachable unwind label %catch.dispatch.i
catch.dispatch.i: ; preds = %ehcleanup
%1 = catchswitch within %0 [label %catch.start.i] unwind label %terminate.i
catch.start.i: ; preds = %catch.dispatch.i
%2 = catchpad within %1 [ptr null]
%3 = tail call ptr @llvm.wasm.get.exception(token %2)
%4 = tail call i32 @llvm.wasm.get.ehselector(token %2)
%5 = call ptr @__cxa_begin_catch(ptr %3) [ "funclet"(token %2) ]
invoke void @__cxa_end_catch() [ "funclet"(token %2) ]
to label %invoke.cont.i unwind label %terminate.i
invoke.cont.i: ; preds = %catch.start.i
catchret from %2 to label %_ZN4TempD2Ev.exit
terminate.i: ; preds = %catch.start.i, %catch.dispatch.i
%6 = cleanuppad within %0 []
call void @_ZSt9terminatev() #2 [ "funclet"(token %6) ]
unreachable
_ZN4TempD2Ev.exit: ; preds = %invoke.cont.i
cleanupret from %0 unwind label %catch.dispatch
catch.dispatch: ; preds = %_ZN4TempD2Ev.exit
%7 = catchswitch within none [label %catch.start] unwind to caller
catch.start: ; preds = %catch.dispatch
%8 = catchpad within %7 [ptr null]
%9 = tail call ptr @llvm.wasm.get.exception(token %8)
%10 = tail call i32 @llvm.wasm.get.ehselector(token %8)
%11 = call ptr @__cxa_begin_catch(ptr %9) #8 [ "funclet"(token %8) ]
call void @__cxa_end_catch() [ "funclet"(token %8) ]
catchret from %8 to label %try.cont
try.cont: ; preds = %catch.start
ret void
unreachable: ; preds = %entry
unreachable
}
; This tests whether llvm.wasm.throw intrinsic can invoked and iseled correctly.
; CHECK-LABEL: invoke_throw:
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: local.get 0
; CHECK: throw __cpp_exception
; CHECK: end_try_table
define void @invoke_throw(ptr %p) personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @llvm.wasm.throw(i32 0, ptr %p)
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
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) #4 [ "funclet"(token %1) ]
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
try.cont: ; preds = %catch, %entry
ret void
}
; Regression test for a bug where, when an invoke unwinds to a catchswitch, the
; catchswitch's unwind destination was not included in the invoke's unwind
; destination when there was no direct link from catch.start to there.
; CHECK-LABEL: unwind_destinations:
; CHECK: block
; CHECK: block
; CHECK: try_table (catch_all 0)
; CHECK: block i32
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: call foo
; CHECK: end_try_table
; CHECK: unreachable
; CHECK: end_block
; CHECK: call _ZSt9terminatev
; CHECK: unreachable
; CHECK: end_try_table
; CHECK: unreachable
; CHECK: end_block
; Note the below is "terminate" BB and should not be DCE'd
; CHECK: call _ZSt9terminatev
; CHECK: unreachable
; CHECK: end_block
define void @unwind_destinations() personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind label %terminate
catch.start: ; preds = %catch.dispatch
%1 = catchpad within %0 [ptr null]
%2 = call ptr @llvm.wasm.get.exception(token %1)
%3 = call ptr @__cxa_begin_catch(ptr %2) #5 [ "funclet"(token %1) ]
call void @_ZSt9terminatev() #2 [ "funclet"(token %1) ]
unreachable
; Even if there is no link from catch.start to this terminate BB, when there is
; an exception that catch.start does not catch (e.g. a foreign exception), it
; should end up here, so this BB should NOT be DCE'ed
terminate: ; preds = %catch.dispatch
%4 = cleanuppad within none []
call void @_ZSt9terminatev() #2 [ "funclet"(token %4) ]
unreachable
try.cont: ; preds = %entry
ret void
}
declare void @foo()
declare void @bar(ptr)
declare void @take_i32(i32)
declare i32 @__gxx_wasm_personality_v0(...)
; Function Attrs: noreturn
declare void @llvm.wasm.throw(i32, ptr) #1
; Function Attrs: nounwind
declare ptr @llvm.wasm.get.exception(token) #0
; Function Attrs: nounwind
declare i32 @llvm.wasm.get.ehselector(token) #0
; Function Attrs: noreturn
declare void @llvm.wasm.rethrow() #1
; Function Attrs: nounwind
declare i32 @llvm.eh.typeid.for(ptr) #0
; Function Attrs: nounwind
declare ptr @__cxa_allocate_exception(i32) #0
declare ptr @__cxa_begin_catch(ptr)
declare void @__cxa_end_catch()
; Function Attrs: noreturn
declare void @__cxa_throw(ptr, ptr, ptr) #1
declare void @_ZSt9terminatev()
declare ptr @_ZN4TempD2Ev(ptr returned)
attributes #0 = { nounwind }
attributes #1 = { noreturn }
attributes #2 = { noreturn nounwind }
; CHECK: __cpp_exception:
|