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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
|
# ClangIR Cleanup and Exception Handling Design
::: {.contents local=""}
:::
## Overview
This document describes the design for C++ cleanups and exception
handling representation and lowering in the CIR dialect. The initial CIR
generation will follow the general structure of the cleanup and
exception handling code in Clang's LLVM IR generation. In particular,
we will continue to use the `EHScopeStack` with pushing and popping of
`EHScopeStack::Cleanup` objects to drive the creation of cleanup scopes
within CIR.
However, the LLVM IR generated by Clang is fundamentally unstructured
and therefore isn't well suited to the goals of CIR. Therefore, we are
proposing a high-level representation that follows MLIR's structured
control flow model.
The `cir::LowerCFG` pass will lower this high-level representation to a
different form where control flow is block-based and explicit. This form
will more closely resemble the LLVM IR used when Clang is generating
LLVM IR directly. However, this form will still be ABI-agnostic.
An additional pass will be introduced to lower the flattened form to an
ABI-specific representation. This ABI-specific form will have a direct
correspondence to the LLVM IR exception handling representation for a
given target.
## High-level CIR representation
### Normal and EH cleanups
Scopes that require normal or EH cleanup will be represented using a new
operation, `cir.cleanup.scope`.
```
cir.cleanup.scope {
// body region
} cleanup [normal|eh|all] {
// cleanup instructions
}
```
Execution begins with the first operation in the body region and
continues according to normal control flow semantics until a terminating
operation (`cir.yield`, `cir.break`, `cir.return`, `cir.continue`) is
encountered or an exception is thrown.
If the cleanup region is marked as `eh_only`, normal control flow exits
from the body region skip the cleanup region and continue to their
normal destination according to the semantics of the operation. If the
cleanup region is not marked as `eh_only`, normal control flow exits
from the body region must execute the cleanup region before control is
transferred to the destination implied by the operation.
If a `cir.goto` operation occurs within a cleanup scope, the behavior
depends on the target of the operation. If the target is within the
same cleanup scope, control is transferred to the target block directly.
If the target is not within the cleanup scope, control is transferred to
the cleanup region according to the rules described above for normal
exits before branching to the destination of the goto operation.
While we do not expect to encounter `cir.br` or `cir.brcond` operations
that exit a cleanup scope, if such a thing did happen, it would follow
the rules described above for `cir.goto` operations.
The `cir.indirect_br` operation is not permitted within a cleanup scope.
When an exception is thrown from within a cleanup scope and not caught
within the scope, the cleanup region must be executed before handling of
the exception continues. If the cleanup scope is nested within another
cleanup scope, the cleanup region of the inner scope is executed,
followed by the cleanup region of the outer scope, and handling
continues according to these rules. If the cleanup scope is nested
within a try operation, the cleanup region is executed before control is
transferred to the catch handlers. If an exception is thrown from within
a cleanup region that is not nested within either another cleanup region
or a try operation, the cleanup region is executed and then exception
unwinding continues as if a `cir.resume` operation had been executed.
If a `cir.resume` operation occurs within a cleanup scope, for example,
if the scope contains a try operation with uncaught exception types, the
`cir.resume` operation will unwind to the cleanup region of the enclosing
cleanup scope.
Note that this design eliminates the need for synthetic try operations,
such as were used to represent calls within a cleanup scope in the
ClangIR incubator project.
#### Implementation notes
The `cir.cleanup.scope` must be created when we call `pushCleanup`. We
will need to set the insertion point at that time. When each cleanup
block is popped, we will need to set the insertion point to immediately
following the cleanup scope operation. If `forceCleanups()` is called,
it will pop cleanup blocks, which is good.
#### Example: Automatic storage object cleanup
**C++**
``` c++
void someFunc() {
SomeClass c;
c.doSomething();
}
```
**CIR**
```
cir.func @someFunc() {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClass11doSomethingEv(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
} cleanup normal {
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
cir.return
}
```
In this example, we create an instance of `SomeClass` which has a
constructor and a destructor. If an exception occurs within the
constructor call, it unwinds without any handling in this function. The
cleanup scope is not entered in that case. Once the object has been
constructed, we enter a cleanup scope which continues until the object
goes out of scope, in this case for the remainder of the function.
If an exception is thrown from within the `doSomething()` function, we
execute the cleanup region, calling the `SomeClass` destructor before
continuing to unwind the exception. If the call to `doSomething()`
completes successfully, the object goes out of scope and we execute the
cleanup region, calling the destructor, before continuing to the return
operation.
#### Example: Multiple automatic objects
**C++**
``` c++
void someFunc() {
SomeClass c;
SomeClass c2;
c.doSomething();
SomeClass c3;
c3.doSomething();
}
```
**CIR**
```
cir.func @someFunc() {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
%1 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c2", init]
%2 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c3", init]
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClassC1Ev(%1) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClass11doSomethingEv(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.call @_ZN9SomeClassC1Ev(%2) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClass11doSomethingEv(%2) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
} cleanup normal {
cir.call @_ZN9SomeClassD1Ev(%2) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
cir.yield
} cleanup normal {
cir.call @_ZN9SomeClassD1Ev(%1) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
cir.yield
} cleanup normal {
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
cir.return
}
```
In this example, we have three objects with automatic storage duration.
The destructor must be called for each object that has been constructed,
and the destructors must be called in reverse order of object creation.
We guarantee that by creating nested cleanup scopes as each object is
constructed.
Normal execution control flows through the body region of each of the
nested cleanup scopes until the body of the innermost scope. Next, the
cleanup scopes are visited, calling the destructor once in each cleanup
scope, in reverse order of the object construction.
#### Implementation notes
Branch through cleanups will be handled during flattening. In the
structured CIR representation, an operation like `cir.break`,
`cir.return`, or `cir.continue` has well-defined behavior. We will need
to define the semantics such that they include visiting the cleanup
region before continuing to their currently defined destination.
#### Example: Branch through cleanup
**C++**
``` c++
int someFunc() {
int i = 0;
while (true) {
SomeClass c;
if (i == 3)
continue;
if (i == 7)
break;
i = c.get();
}
return i;
}
```
**CIR**
```
cir.func @someFunc() -> !s32i {
%0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
%1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
%2 = cir.const #cir.int<0> : !s32i
cir.store align(4) %2, %1 : !s32i, !cir.ptr<!s32i>
cir.scope {
cir.while {
%5 = cir.const #true
cir.condition(%5)
} do {
cir.scope {
%5 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.call @_ZN9SomeClassC1Ev(%5) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.scope { // This is a scope for the `if`, unrelated to cleanups
%7 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
%8 = cir.const #cir.int<3> : !s32i
%9 = cir.cmp(eq, %7, %8) : !s32i, !cir.bool
cir.if %9 {
cir.continue // This implicitly branches through the cleanup region
}
}
cir.scope { // This is a scope for the `if`, unrelated to cleanups
%7 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
%8 = cir.const #cir.int<7> : !s32i
%9 = cir.cmp(eq, %7, %8) : !s32i, !cir.bool
cir.if %9 {
cir.break // This implicitly branches through the cleanup region
}
}
%6 = cir.call @_ZN9SomeClass3getEv(%5) : (!cir.ptr<!rec_SomeClass>) -> !s32i
cir.store align(4) %6, %1 : !s32i, !cir.ptr<!s32i>
cir.yield
} cleanup normal {
cir.call @_ZN9SomeClassD1Ev(%5) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
}
cir.yield
}
}
%3 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
cir.store %3, %0 : !s32i, !cir.ptr<!s32i>
%4 = cir.load %0 : !cir.ptr<!s32i>, !s32i
cir.return %4 : !s32i
}
```
In this example we have a cleanup scope inside the body of a
`while-loop`, and multiple instructions that may exit the loop body with
different destinations. When the `cir.continue` operation is executed,
it will transfer control to the cleanup region, which calls the object
destructor before transferring control to the while condition region
according to the semantics of the `cir.continue` operation.
When the `cir.break` operation is executed, it will transfer control to
the cleanup region, which calls the object destructor before
transferring control to the operation following the while loop according
to the semantics of the `cir.break` operation.
If neither the `cir.continue` or `cir.break` operations are executed
during an iteration of the loop, when the end of the cleanup scope's
body region is reached, control will be transferred to the cleanup
region, which calls the object destructor before transferring control to
the next operation following the cleanup scope, in this case falling
through to the `cir.yield` operation to complete the loop iteration.
This control flow is implicit in the semantics of the CIR operations at
this point. When this CIR is flattened, explicit branches and a switch
on destination slots will be created, matching the LLVM IR control flow
for cleanup block sharing.
#### Example: EH-only cleanup
**C++**
``` c++
class Base {
public:
Base();
~Base();
};
class Derived : public Base {
public:
Derived() : Base() { f(); }
~Derived();
};
```
**CIR**
```
cir.func @_ZN7DerivedC2Ev(%arg0: !cir.ptr<!rec_Derived>) {
%0 = cir.alloca !cir.ptr<!rec_Derived>, !cir.ptr<!cir.ptr<!rec_Derived>>, ["this", init]
cir.store %arg0, %0 : !cir.ptr<!rec_Derived>, !cir.ptr<!cir.ptr<!rec_Derived>>
%1 = cir.load %0 : !cir.ptr<!cir.ptr<!rec_Derived>>, !cir.ptr<!rec_Derived>
%2 = cir.base_class_addr %1 : !cir.ptr<!rec_Derived> nonnull [0] -> !cir.ptr<!rec_Base>
cir.call @_ZN4BaseC2Ev(%2) : (!cir.ptr<!rec_Base>) -> ()
cir.cleanup.scope {
cir.call exception @_Z1fv() : () -> ()
cir.yield
} cleanup eh {
%3 = cir.base_class_addr %1 : !cir.ptr<!rec_Derived> nonnull [0] -> !cir.ptr<!rec_Base>
cir.call @_ZN4BaseD2Ev(%3) : (!cir.ptr<!rec_Base>) -> ()
cir.resume
}
cir.return
}
```
In this example, the `Derived` constructor calls the `Base` constructor
and then calls a function which may throw an exception. If an exception
is thrown, we must call the `Base` destructor before continuing to
unwind the exception. However, if no exception is thrown, we do not call
the destructor. Therefore, this cleanup handler is marked as eh_only.
### Try Operations and Exception Handling
Try-catch blocks will be represented, as they are in the ClangIR
incubator project, using a `cir.try` operation.
```
cir.try {
cir.call exception @function() : () -> ()
cir.yield
} catch [type #cir.global_view<@_ZTIPf> : !cir.ptr<!u8i>] {
...
cir.yield
} unwind {
cir.resume
}
```
The operation consists of a try region, which contains the operations to
be executed during normal execution, and one or more handler regions,
which represent catch handlers or the fallback unwind for uncaught
exceptions.
#### Example: Simple try-catch
**C++**
``` c++
void someFunc() {
try {
f();
} catch (std::exception &e) {
// Do nothing
}
}
```
**CIR**
```
cir.func @someFunc(){
cir.scope {
cir.try {
cir.call exception @_Z1fv() : () -> ()
cir.yield
} catch [type #cir.global_view<@_ZTISt9exception> : !cir.ptr<!u8i>] {
cir.yield
} unwind {
cir.resume
}
}
cir.return
}
```
If the call to `f()` throws an exception that matches the handled type
(`std::exception&`), control will be transferred to the catch handler
for that type, which simply yields, continuing execution immediately
after the try operation.
If the call to `f()` throws any other type of exception, control will be
transferred to the unwind region, which simply continues unwinding the
exception at the next level, in this case, the handlers (if any) for the
function that called `someFunc()`.
#### Example: Try-catch with catch all
**C++**
``` c++
void someFunc() {
try {
f();
} catch (std::exception &e) {
// Do nothing
} catch (...) {
// Do nothing
}
}
```
**CIR**
```
cir.func @someFunc(){
cir.scope {
cir.try {
cir.call exception @_Z1fv() : () -> ()
cir.yield
} catch [type #cir.global_view<@_ZTISt9exception> : !cir.ptr<!u8i>] {
cir.yield
} catch all {
cir.yield
}
}
cir.return
}
```
In this case, if the call to `f()` throws an exception that matches the
handled type (`std::exception&`), everything works exactly as in the
previous example. Control will be transferred to the catch handler for
that type, which simply yields, continuing execution immediately after
the try operation.
If the call to `f()` throws any other type of exception, control will be
transferred to the catch all region, which also yields, continuing
execution immediately after the try operation.
#### Example: Try-catch with cleanup
**C++**
``` c++
void someFunc() {
try {
SomeClass c;
c.doSomething();
} catch (...) {
// Do nothing
}
}
```
**CIR**
```
cir.func @someFunc(){
cir.scope {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try {
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClass11doSomethingEv(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
} cleanup all {
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
} catch all {
cir.yield
}
}
cir.return
}
```
In this case, an object that requires cleanup is instantiated inside the
try block scope. If the call to `doSomething()` throws an exception, the
cleanup region will be executed before control is transferred to the
catch handler.
#### Example: Try-catch within a cleanup region
**C++**
``` c++
void someFunc() {
SomeClass c;
try {
c.doSomething();
} catch (std::exception& e) {
// Do nothing
}
}
```
**CIR**
```
cir.func @someFunc(){
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.scope {
cir.try {
cir.call @_ZN9SomeClass11doSomethingEv(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
} catch [type #cir.global_view<@_ZTISt9exception> : !cir.ptr<!u8i>] {
cir.yield
} unwind {
cir.resume
}
}
cir.yield
} cleanup all {
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
cir.return
}
```
In this case, the object that requires cleanup is instantiated outside
the try block scope, and not all exception types have catch handlers.
If the call to `doSomething()` throws an exception of type
`std::exception&`, control will be transferred to the catch handler,
which will simply continue execution at the point immediately following
the try operation, and the cleanup handler will be executed when the
cleanup scope is exited normally.
If the call to `doSomething()` throws any other exception of type,
control will be transferred to the unwind region, which executes
`cir.resume` to continue unwinding the exception. However, the cleanup
region of the cleanup scope will be executed before exception unwinding
continues because we are exiting the scope via the `cir.resume`
operation.
### Partial Array Cleanup
Partial array cleanup is a special case because the details of array
construction and deletion are already encapsulated within high-level CIR
operations. When an array of objects is constructed, the constructor for
each object is called sequentially. If one of the constructors throws an
exception, we must call the destructor for each object that was
previously constructed in reverse order of their construction. In the
high-level CIR representation, we have a single operation,
`cir.array.ctor` to represent the array construction. Because the
cleanup needed is entirely within the scope of this operation, we can
represent the cleanup by adding a cleanup region to this operation.
```
cir.array.ctor(%0 : !cir.ptr<!cir.array<!rec_SomeClass x 16>>) {
^bb0(%arg0: !cir.ptr<!rec_SomeClass>):
cir.call @_ZN9SomeClassC1Ev(%arg0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
} cleanup {
^bb0(%arg0: !cir.ptr<!rec_SomeClass>):
cir.call @_ZN9SomeClassD1Ev(%arg0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
```
This representation shows how a single instance of the object is
initialized and cleaned up. When the operation is transformed to a
low-level form (during `cir::LoweringPrepare`), these two regions will
be expanded to a loop within a `cir.cleanup.scope` for the
initialization, and a loop within the cleanup scope's cleanup region to
perform the partial array cleanup, as follows
```
cir.scope {
%1 = cir.const #cir.int<16> : !u64i
%2 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!rec_SomeClass x 16>> -> !cir.ptr<!rec_SomeClass>
%3 = cir.ptr_stride %2, %1 : (!cir.ptr<!rec_SomeClass>, !u64i) -> !cir.ptr<!rec_SomeClass>
%4 = cir.alloca !cir.ptr<!rec_SomeClass>, !cir.ptr<!cir.ptr<!rec_SomeClass>>, ["__array_idx"]
cir.store %2, %4 : !cir.ptr<!rec_SomeClass>, !cir.ptr<!cir.ptr<!rec_SomeClass>>
cir.cleanup.scope {
cir.do {
%5 = cir.load %4 : !cir.ptr<!cir.ptr<!rec_SomeClass>>, !cir.ptr<!rec_SomeClass>
cir.call @_ZN9SomeClassC1Ev(%5) : (!cir.ptr<!rec_SomeClass>) -> ()
%6 = cir.const #cir.int<1> : !u64i
%7 = cir.ptr_stride %5, %6 : (!cir.ptr<!rec_SomeClass>, !u64i) -> !cir.ptr<!rec_SomeClass>
cir.store %7, %4 : !cir.ptr<!rec_SomeClass>, !cir.ptr<!cir.ptr<!rec_SomeClass>>
cir.yield
} while {
%5 = cir.load %4 : !cir.ptr<!cir.ptr<!rec_SomeClass>>, !cir.ptr<!rec_SomeClass>
%6 = cir.cmp(ne, %5, %3) : !cir.ptr<!rec_SomeClass>, !cir.bool
cir.condition(%6)
}
} cleanup eh {
cir.while {
%5 = cir.load %4 : !cir.ptr<!cir.ptr<!rec_SomeClass>>, !cir.ptr<!rec_SomeClass>
%6 = cir.cmp(ne, %5, %2) : !cir.ptr<!rec_SomeClass>, !cir.bool
cir.condition(%6)
} cir.do {
%5 = cir.load %4 : !cir.ptr<!cir.ptr<!rec_SomeClass>>, !cir.ptr<!rec_SomeClass>
%6 = cir.const #cir.int<-1> : !s64i
%7 = cir.ptr_stride %5, %6 : (!cir.ptr<!rec_SomeClass>, !s64i) -> !cir.ptr<!rec_SomeClass>
cir.call @_ZN9SomeClassD1Ev(%7) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.store %7, %4 : !cir.ptr<!rec_SomeClass>, !cir.ptr<!cir.ptr<!rec_SomeClass>>
cir.yield
}
}
}
```
Here, both the construction and cleanup loops use the same temporary
pointer variable to track their location. If an exception is thrown by
one of the constructor, the `__array_idx` variable will point to the
object that was being constructed when the exception was thrown. If the
exception was thrown during construction of the first object,
`__array_idx` will point to the start of the array, and so no destructor
will be called. If an exception is thrown during the constructor call
for any other object, `__array_idx` will not point to the start of the
array, and so the cleanup region will decrement the pointer, call the
destructor for the previous object, and so on until we reach the
beginning of the array. This corresponds to the way that partial array
destruction is handled in Clang's LLVM IR codegen.
## CFG Flattening
Before CIR can be lowered to the LLVM dialect, the CFG must be
flattened. That is, functions must not contain nested regions, and all
blocks in the function must belong to the parent region. This state is
formed by the `cir::FlattenCFG` pass. This pass will need to transform
the high-level CIR representation described above to a flat form where
cleanups and exception handling are explicitly routed through blocks,
which are shared as needed.
The CIR representation will remain ABI agnostic after the flattening
pass. The flattening pass will implement the semantics for branching
through cleanup regions using the same slot and dispatch mechanism used
in Clang's LLVM IR codegen.
### Exception Handling
Flattening the CIR for exception handling, including any cleanups that
must be performed during exception unwinding, requires some specialized
CIR operations. The operations that were used in the ClangIR incubator
project were closely matched to the Itanium exception handling ABI. In
order to achieve a representation that also works well for other ABIs,
the following new operations are being proposed: `cir.eh.initiate`,
`cir.eh.dispatch`, `cir.begin_cleanup`, `cir.end_cleanup`,
`cir.begin_catch`, and `cir.end_catch`.
Any time a cir.call operation that may throw and exception appears
within the try region of a `cir.try` operation or within the body region
of a `cir.cleanup.scope` with a cleanup region marked as an exception
cleanup, the call will be converted to a `cir.try_call` operation, with
normal and unwind destinations. The first operation in the unwind
destination block must be a `cir.eh.initiate` operation.
`%eh_token = cir.eh.initiate [cleanup]`
If this destination includes cleanup code, the cleanup keyword will be
present, and the cleanup code will be executed before the exception is
dispatched to any handlers. The `cir.eh.initiate` operation returns a
value of type `!cir.eh_token`. This is an opaque value that will be used
during ABI-lowering. At this phase, it conceptually represents the
exception that was thrown and is passed as the argument to the
`cir.begin_cleanup`, `cir.begin_catch`, and `cir.eh.dispatch`
operations.
```
cir.eh.dispatch %eh_token : !cir.eh_token [
catch (#cir.global_view<@_ZTIi> : !u32i) : ^bb6
catch_all : ^bb7
]
cir.eh.dispatch %eh_token : !cir.eh_token [
catch (#cir.global_view<@_ZTIi> : !u32i) : ^bb6
unwind : ^bb7
]
```
The `cir.eh.dispatch` operation behaves similarly to the LLVM IR switch
instruction. It takes as an argument a token that was returned by a
previous `cir.eh.initiate` operation. It then has a list of key-value
pairs, where the key is either a type identifier, the keyword catch_all,
or the keyword unwind and the value is a block to which execution should
be transferred if the key is matched. Although the example above shows
both the catch_all and unwind keyword, in practice only one or the other
will be present, but the operation is required to have one of these
values.
When we are unwinding an exception with cleanups, the `cir.eh.initiate`
operation will be marked with the cleanup attribute and will be followed
by a branch to the cleanup block, passing the EH token as an operand to
the block. The cleanup block will begin with a call to
`cir.begin_cleanup` which returns a cleanup token.
```
^bb4 (%eh_token : !cir.eh_token):
%cleanup_token = cir.begin_cleanup %eh_token : !cir.eh_token -> !cir.cleanup_token
```
This is followed by the operations to perform the cleanup and then a
cir.end_cleanup operation.
`cir.end_cleanup(%cleanup_token : !cir.cleanup_token)`
Finally, the cleanup block either branches to a catch dispatch block or
executes a `cir.resume` operation to continue unwinding the exception.
When an exception is caught, the catch block will receive the eh token
for the exception being caught as an argument, and the first operation
of the catch handling block must be a `cir.begin_catch` operation.
```
^bb6 (%token : !cir.eh_token):
%catch_token, %exn_ptr = cir.begin_catch %8 -> (!cir.catch_token, !cir.ptr<!s32i>)
```
The `cir.begin_catch` operation returns two values: a new token that
uniquely identify this catch handler, and a pointer to the exception
object. All paths through the catch handler must converge on a single
`cir.end_catch` operation, which marks the end of the handler.
`cir.end_catch %catch_token`
The argument to the `cir.end_catch` operation is the token returned by
the `cir.begin_catch` operation.
#### Example: Try-catch with cleanup
**C++**
``` c++
void someFunc() {
try {
SomeClass c;
c.doSomething();
} catch (...) {
// Do nothing
}
}
```
**High-level CIR**
```
cir.func @someFunc(){
cir.scope {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try {
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClass11doSomethingEv(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
} cleanup all {
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
} catch all {
cir.yield
}
}
cir.return
}
```
**Flattened CIR**
```
cir.func @someFunc(){
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try_call @_ZN9SomeClassC1Ev(%0) ^bb1, ^bb3 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb1
cir.try_call @_ZN9SomeClass11doSomethingEv(%0) ^bb2, ^bb4 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb2 // Normal cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb8
^bb3 // EH catch (from entry block)
%1 = cir.eh.initiate : !cir.eh_token
cir.br ^bb6(%1 : !cir.eh_token)
^bb4 // EH cleanup (from ^bb1)
%2 = cir.eh.initiate cleanup : !cir.eh_token
cir.br ^bb5(%2 : !cir.eh_token)
^bb5(%eh_token : !cir.eh_token)
%3 = cir.begin_cleanup(%eh_token : !cir.eh_token) : !cir.cleanup_token
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.end_cleanup(%3 : !cir.cleanup_token)
cir.br ^bb6(%eh_token : !cir.eh_token)
^bb6(%eh_token.1 : !cir.eh_token) // Catch dispatch (from ^bb3 or ^bb4)
cir.eh.dispatch %eh_token.1 : !cir.eh_token [
catch_all : ^bb7
]
^bb7(%eh_token.2 : !cir.eh_token)
%catch.token = cir.begin_catch(%eh_token.2 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb8
^bb8 // Normal continue (from ^bb2 or ^bb6)
cir.return
}
```
In this example, the normal cleanup is performed in a different block
than the EH cleanup. This follows the pattern established by Clang's
LLVM IR codegen. Only the EH cleanup requires `cir.begin_cleanup` and
`cir.end_cleanup` operations.
If the `SomeClass` constructor throws an exception, it unwinds to an EH
catch block (`^bb3`), which has excecutes a `cir.eh.initiate` operation
before branching to a shared catch dispatch block (`^bb6`).
If the `doSomething()` function throws an exception, it unwinds to an EH
block `^bb4` that performs cleanup before branching to the shared catch
dispatch block (`^bb5`).
#### Example: Cleanup with unhandled exception
**C++**
``` c++
void someFunc() {
SomeClass c;
c.doSomething();
}
```
**High-level CIR**
```
cir.func @someFunc(){
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.call @_ZN9SomeClass11doSomethingEv(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
} cleanup all {
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
cir.return
}
```
**Flattened CIR**
```
cir.func @someFunc(){
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.try_call @_ZN9SomeClass11doSomethingEv(%0) ^bb1, ^bb2 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb1 // Normal cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb4
^bb2 // EH cleanup (from entry block)
%1 = cir.eh.initiate cleanup : !cir.eh_token
cir.br ^bb3(%1 : !cir.eh_token)
^bb3(%eh_token : !cir.eh_token) // Perform cleanup
%2 = cir.begin_cleanup(%eh_token : !cir.eh_token) : !cir.cleanup_token
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.end_cleanup(%2 : !cir.cleanup_token)
cir.resume // Unwind to caller
^bb4 // Normal continue (from ^bb1)
cir.return
}
```
In this example, if `doSomething()` throws an exception, it unwinds to
the EH cleanup block (`^bb2`), which branches to `^bb3` to perform the
cleanup, but because we have no catch handler, we execute `cir.resume`
after the cleanup to unwind to the function that called `someFunc()`.
#### Example: Shared cleanups
**C++**
``` c++
int someFunc() {
int i = 0;
while (true) {
SomeClass c;
if (i == 3)
continue;
if (i == 7)
break;
i = c.get();
}
return i;
}
```
**CIR**
```
cir.func @someFunc() -> !s32i {
%0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
%1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
%2 = cir.const #cir.int<0> : !s32i
cir.store align(4) %2, %1 : !s32i, !cir.ptr<!s32i>
cir.scope {
cir.while {
%5 = cir.const #true
cir.condition(%5)
} do {
cir.scope {
%5 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.call @_ZN9SomeClassC1Ev(%5) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanup.scope {
cir.scope {
%7 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
%8 = cir.const #cir.int<3> : !s32i
%9 = cir.cmp(eq, %7, %8) : !s32i, !cir.bool
cir.if %9 {
cir.continue
}
}
cir.scope {
%7 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
%8 = cir.const #cir.int<7> : !s32i
%9 = cir.cmp(eq, %7, %8) : !s32i, !cir.bool
cir.if %9 {
cir.break
}
}
%6 = cir.call @_ZN9SomeClass3getEv(%5) : (!cir.ptr<!rec_SomeClass>) -> !s32i
cir.store align(4) %6, %1 : !s32i, !cir.ptr<!s32i>
cir.yield
} cleanup all {
cir.call @_ZN9SomeClassD1Ev(%5) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.yield
}
}
cir.yield
}
}
%3 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
cir.store %3, %0 : !s32i, !cir.ptr<!s32i>
%4 = cir.load %0 : !cir.ptr<!s32i>, !s32i
cir.return %4 : !s32i
}
```
**Flattened CIR**
```
cir.func @someFunc() -> !s32i {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
%1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__cleanup_dest_slot "]
%2 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
%3 = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
%4 = cir.const #cir.int<0> : !s32i
cir.store align(4) %4, %3 : !s32i, !cir.ptr<!s32i>
cir.br ^bb1
^bb1: // 3 preds: ^bb0, ^bb9, ^bb11
%5 = cir.const #true
cir.brcond %5 ^bb2, ^bb12
^bb2: // pred: ^bb1
cir.call @_ZN9SomeClassC1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb3
^bb3: // pred: ^bb2
%6 = cir.load align(4) %3 : !cir.ptr<!s32i>, !s32i
%7 = cir.const #cir.int<3> : !s32i
%8 = cir.cmp(eq, %6, %7) : !s32i, !cir.bool
cir.brcond %8 ^bb4, ^bb5
^bb4: // pred: ^bb3
// Set the destination slot and branch through cleanup
%9 = cir.const #cir.int<0> : !s32i
cir.store %9, %1 : !s32i, !cir.ptr<!s32i>
cir.br ^bb9
^bb5: // pred: ^bb3
%10 = cir.load align(4) %3 : !cir.ptr<!s32i>, !s32i
%11 = cir.const #cir.int<7> : !s32i
%12 = cir.cmp(eq, %10, %11) : !s32i, !cir.bool
cir.brcond %12 ^bb6, ^bb7
^bb6: // pred: ^bb5
// Set the destination slot and branch through cleanup
%13 = cir.const #cir.int<1> : !s32i
cir.store %13, %1 : !s32i, !cir.ptr<!s32i>
cir.br ^bb9
^bb7: // pred: ^bb5
%14 = cir.call @_ZN9SomeClass3getEv(%0) : (!cir.ptr<!rec_SomeClass>) -> !s32i
cir.store align(4) %14, %3 : !s32i, !cir.ptr<!s32i>
cir.br ^bb8
^bb8: // pred: ^bb7
// Set the destination slot and branch through cleanup
%15 = cir.const #cir.int<2> : !s32i
cir.store %15, %1 : !s32i, !cir.ptr<!s32i>
cir.br ^bb9
^bb9: // pred
// Shared cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
%16 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
cir.switch.flat %16 : !s32i, ^bb10 [
0: ^bb1 // continue
1: ^bb12 // break
2: ^bb11 // end of loop
]
^bb10: // preds: ^bb9
cir.unreachable
^bb11: // pred: ^bb9
cir.br ^bb1
^bb12: // pred: ^bb1
%17 = cir.load align(4) %3 : !cir.ptr<!s32i>, !s32i
cir.store align(4) %17, %2 : !s32i, !cir.ptr<!s32i>
%18 = cir.load align(4) %2 : !cir.ptr<!s32i>, !s32i
cir.return %18 : !s32i
}
```
In this example we have a cleanup scope inside the body of a while loop,
and multiple instructions that may exit the loop body with different
destinations. For simplicity, the example is shown without exception
handling.
When any of the conditions that exit a loop iteration occur (continue,
break, or completion of an iteration), we set a cleanup destination slot
to a unique value and branch to a shared normal cleanup block. That
block performs the cleanup and then compares the cleanup destination
slot value to the set of expected constants and branches to the
corresponding destination.
For example, when the continue instruction is reached, we set the
cleanup destination slot (`%1`) to zero, branch to the shared cleanup
block (`^bb9`), which calls the `SomeClass` destructor, then uses
`cir.switch.flat` to switch on the cleanup destination slot value and,
finding it to be zero, branches to the loop condition block (`^bb1`).
If none of the expected values is matched, the `cir.switch.flat`
branches to a block with a `cir.unreachable` operation. This corresponds
to the behavior of Clang's LLVM IR codegen.
## ABI Lowering
A new pass will be introduced to lower the flattened representation to
lower the ABI-agnostic flattened CIR representation to an ABI-specific
form. This will be a separate pass from the main CXXABI lowering pass,
which runs before CFG flattening. The ABI lowering pass will introduce
personality functions and ABI-specific exception handling operations.
This new pass will make use of the `cir::CXXABI` interface class and
ABI-specific subclasses, but it will introduce a new set of interface
methods for use with the exception handling ABI.
For each supported exception handling ABI, the operations and function
calls used will have a direct correspondence to the LLVM IR instructions
and runtime library functions used for that ABI. The LLVM IR exception
handling model is described in detail here: [LLVM Exception
Handling](https://llvm.org/docs/ExceptionHandling.html).
A personality function attribute will be added to functions that require
it during the ABI lowering phase.
### Itanium ABI Lowering
The Itanium exception handling ABI representation replaces the
`cir.eh.initiate` and `cir.eh.dispatch` operations with a
`cir.eh.landingpad` operation and a series of `cir.compare` and
`cir.brcond` operations to model the correct handling based on type IDs
for the catch handlers. The `cir.begin_cleanup` and `cir.end_cleanup`
operations are simply dropped. The `cir.begin_catch` operation becomes a
call to `__cxa_begin_catch`. The `cir.end_catch` operation becomes a
call to `__cxa_end_catch`.
The only operation that is specific to Itanium exception handling is
`cir.eh.landingpad`.
`%exn_ptr_0, %type_id = cir.eh.landingpad [@_ZTISt9exception] : !cir.ptr<!void>, !u32i`
This operation corresponds directly to the LLVM IR landingpad
instruction. It may have a list of type IDs that the handler can catch
(or null for \"catch all\") or it may have the cleanup attribute if the
handler performs cleanup but does not catch any exceptions.
#### Example: Try-catch with cleanup
**Flattened CIR**
```
cir.func @someFunc(){
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try_call @_ZN9SomeClassC1Ev(%0) ^bb1, ^bb3 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb1
cir.try_call @_ZN9SomeClass11doSomethingEv(%0) ^bb2, ^bb4 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb2 // Normal cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb8
^bb3 // EH catch (from entry block)
%1 = cir.eh.initiate : !cir.eh_token
cir.br ^bb6(%1 : !cir.eh_token)
^bb4 // EH cleanup (from ^bb1)
%2 = cir.eh.initiate cleanup : !cir.eh_token
cir.br ^bb5(%2 : !cir.eh_token)
^bb5(%eh_token : !cir.eh_token)
%3 = cir.begin_cleanup(%eh_token : !cir.eh_token) : !cir.cleanup_token
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.end_cleanup(%3 : !cir.cleanup_token)
cir.br ^bb6(%eh_token : !cir.eh_token)
^bb6(%eh_token.1 : !cir.eh_token) // Catch dispatch (from ^bb3 or ^bb4)
cir.eh.dispatch %eh_token.1 : !cir.eh_token [
catch_all : ^bb7
]
^bb7(%eh_token.2 : !cir.eh_token)
%catch.token = cir.begin_catch(%eh_token.2 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb8
^bb8 // Normal continue (from ^bb2 or ^bb6)
cir.return
}
```
**ABI-lowered CIR**
```
cir.func @someFunc() #personality_fn = @__gxx_personality_v0 {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try_call @_ZN9SomeClassC1Ev(%0) ^bb1, ^bb3 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb1
cir.try_call @_ZN9SomeClass11doSomethingEv(%0) ^bb2, ^bb4 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb2 // Normal cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb8
^bb3 // EH catch (from entry block)
%exn, %type_id = cir.eh.landingpad [null] : (!cir.ptr<!void>, !u32i)
cir.br ^bb6(%exn, &type_id : !cir.ptr<!void>, !u32i)
^bb4 // EH cleanup (from ^bb1)
%exn.1, %type_id.1 = cir.eh.landingpad cleanup [null] : (!cir.ptr<!void>, !u32i)
cir.br ^bb5(%exn, %type_id : !cir.ptr<!void>, !u32i)
^bb5(%1: !cir.ptr<!void>, %2: !u32i)
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb6(%1, %2 : !cir.ptr<!void>, !u32i)
^bb6(%3: !cir.ptr<!void>, %4: !u32i) // Catch dispatch (from ^bb3 or ^bb4)
cir.br ^bb7(%3, %4 : !cir.ptr<!void>, !u32i)
^bb7(%5: !cir.ptr<!void>, %6: !u32i) // Catch all handler
%7 = cir.call @__cxa_begin_catch(%5 : !cir.ptr<!void>)
cir.call @__cxa_end_catch()
cir.br ^bb8
^bb8 // Normal continue (from ^bb2 or ^bb6)
cir.return
}
```
In this example, if an exception is thrown by the `SomeClass`
constructor, it unwinds to a landing pad block (`^bb3`), which branches
to the shared catch dispatch block (`^bb6`), which branches to the catch
all handler block (`^bb7`). The catch all handler calls
`__cxa_begin_catch` and `__cxa_end_catch` and then continues to the
normal continuation block (`^bb8`).
#### Example: Try-catch with multiple catch handlers
**Flattened CIR**
```
cir.func @someFunc(){
cir.try_call @f() ^bb1, ^bb2
^bb1
cir.br ^bb7
^bb2 // EH catch (from entry block)
%1 = cir.eh.initiate : !cir.eh_token
cir.br ^bb3(%1 : !cir.eh_token)
^bb3(%eh_token : !cir.eh_token) // Catch dispatch (from ^bb2)
cir.eh.dispatch %eh_token : !cir.eh_token [
catch (#cir.global_view<@_ZTIi> : !u32i) : ^bb4
catch (#cir.global_view<@_ZTIf> : !u32i) : ^bb5
catch_all : ^bb6
]
^bb4(%eh_token.1 : !cir.eh_token) // Catch handler for int exception
%catch.token = cir.begin_catch(%eh_token.1 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb7
^bb5(%eh_token.2 : !cir.eh_token) // Catch handler for float exception
%catch.token = cir.begin_catch(%eh_token.2 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb7
^bb6(%eh_token.3 : !cir.eh_token) // Catch all handler
%catch.token = cir.begin_catch(%eh_token.3 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb7
^bb7 // Normal continue (from ^bb1, ^bb4, ^bb5, or ^bb6)
cir.return
}
```
**ABI-lowered CIR**
```
cir.func @someFunc() #personality_fn = @__gxx_personality_v0 {
cir.try_call @f() ^bb1, ^bb2
^bb1
cir.br ^bb8
^bb2 // EH catch (from entry block)
%exn, %type_id = cir.eh.landingpad [null] : (!cir.ptr<!void>, !u32i)
cir.br ^bb3(%exn, &type_id : !cir.ptr<!void>, !u32i)
^bb3(%0: !cir.ptr<!void>, %1: !u32i) // Catch compare for int exception
%2 = cir.eh.typeid @_ZTIi : !u32i
%3 = cir.cmp(eq, %1, %2) : !u32i, !cir.bool
cir.brcond %3 ^bb4(%0 : !cir.ptr<!void>), ^bb5(%0, %1 : !cir.ptr<!void>, !u32i)
^bb4(%4: !cir.ptr<!void>, %5: !u32i) // Catch all handler for int exception
%6 = cir.call @__cxa_begin_catch(%4 : !cir.ptr<!void>)
cir.call @__cxa_end_catch()
cir.br ^bb8
^bb5(%7: !cir.ptr<!void>, %8: !u32i) // Catch compare for float exception
%9 = cir.eh.typeid @_ZTIf : !u32i
%10 = cir.cmp(eq, %8, %9) : !u32i, !cir.bool
cir.brcond %10 ^bb7(%7 : !cir.ptr<!void>), ^bb8(%7 : !cir.ptr<!void>)
^bb6(%11: !cir.ptr<!void>, %12: !u32i) // Catch all handler for float exception
%13 = cir.call @__cxa_begin_catch(%11 : !cir.ptr<!void>)
cir.call @__cxa_end_catch()
cir.br ^bb8
^bb7(%14: !cir.ptr<!void>) // Catch all handler
%15 = cir.call @__cxa_begin_catch(%14 : !cir.ptr<!void>)
cir.call @__cxa_end_catch()
cir.br ^bb8
^bb8 // Normal continue (from ^bb1, ^bb4, ^bb6, or ^bb7)
cir.return
}
```
In this example, if an exception is thrown by the `f()` call, it unwinds
to a landing pad block (`^bb2`), which uses the `cir.eh.landingpad`
operation to capture the exception pointer and its type id, then branches
to `^bb3` to begin searching for a catch handler that handles the type id
of the exception. Each catch handler simply consumes the exception by
calling `__cxa_begin_catch` and `__cxa_end_catch` and then continues to
the normal continuation block (`^bb8`).
### Microsoft C++ ABI Lowering
The Microsoft C++ exception handling ABI representation drops the
`cir.eh.initiate` operation and replaces the `cir.eh.dispatch` operation
with `cir.eh.catchswitch` operation. The `cir.begin_cleanup` and
`cir.end_cleanup` operations are replaced with `cir.cleanuppad` and
`cir.cleanupret` respectively, and the `cir.begin_catch` and
`cir.end_catch` operations are replaced with `cir.catchpad` and
`cir.catchret`.
Each of these operations corresponds directly to a similarly named
instruction in LLVM IR and have the same semantics. The first operation
in the unwind destination of a `cir.try_call` must be either
`cir.eh.catchswitch` or `cir.cleanuppad`.
`%4 = cir.eh.catchswitch within none [^bb2, ^bb3] unwind to caller`
The `cir.eh.catchswitch` operation takes an operand which specifies the
parent token, which may either be none or the token returned by a
previous `cir.catchpad` operation. This is followed by a list of blocks
which contain catch handlers. Each block in this list must begin with a
`cir.catchpad` operation. Finally, the unwind destination is provided to
specify where excution continues if the exception is not caught by any
of the handlers, with unwind to caller indicating that the unwind is not
handled further in the current function. This operation returns a token
that is used as the operand for `cir.catchpad` operations associated
with this switch.
`%5 = cir.cleanuppad within none []`
The `cir.cleanuppad` operation takes an operand which specifies the
parent token, which may either be none or the token returned by a
previous `cir.catchpad` operation. This is followed by a arguments
required by the personality function. In the case of C++ exception
handlers, the personality function will be `__CxxFrameHandler3` and the
argument list will be empty. This operation returns a token that is used
as the operand for the associated `cir.cleanupret` operation.
`cir.cleanupret from %5 unwind to ^bb7`
The `cir.cleanupret` operation takes an operand which specifies the
`cir.cleanuppad` operation which is completed by this operation and a
block at which unwinding of the current exception continues (or unwind
to caller if there is no catch handling in the current function).
`%8 = cir.catchpad within %4 [ptr @"??_R0H@8", i32 0, ptr %e]`
The `cir.catchpad` operation takes an operand which specifies the parent
token, which must have been return by a previous `cir.catchswitch`
operation. This is followed by a list of arguments, beginning with the
typeid for the type of exception being caught (or null for catch all),
followed by a type info flag value, followed by a pointer to the
in-flight exception. This operation returns a token that is used as the
operand for the associated `cir.catchret` operation or as the parent for
any `cir.catchswitch` or `cir.cleanuppad` operations that are nested
within this catch handler.
`cir.catchret from %8 to ^bb8`
The `cir.catchret` operation takes an operand which specifies the
`cir.catchpad` operation which is completed by this operation and a
block at which excution should be resumed.
#### Example: Try-catch with cleanup
**Flattened CIR**
```
cir.func @someFunc() {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try_call @_ZN9SomeClassC1Ev(%0) ^bb1, ^bb3 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb1
cir.try_call @_ZN9SomeClass11doSomethingEv(%0) ^bb2, ^bb4 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb2 // Normal cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb8
^bb3 // EH catch (from entry block)
%1 = cir.eh.initiate : !cir.eh_token
cir.br ^bb6(%1 : !cir.eh_token)
^bb4 // EH cleanup (from ^bb1)
%2 = cir.eh.initiate cleanup : !cir.eh_token
cir.br ^bb5(%2 : !cir.eh_token)
^bb5(%eh_token : !cir.eh_token)
%3 = cir.begin_cleanup(%eh_token : !cir.eh_token) : !cir.cleanup_token
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.end_cleanup(%3 : !cir.cleanup_token)
cir.br ^bb6(%eh_token : !cir.eh_token)
^bb6(%eh_token.1 : !cir.eh_token) // Catch dispatch (from ^bb3 or ^bb4)
cir.eh.dispatch %eh_token.1 : !cir.eh_token [
catch_all : ^bb7
]
^bb7(%eh_token.2 : !cir.eh_token)
%catch.token = cir.begin_catch(%eh_token.2 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb8
^bb8 // Normal continue (from ^bb2 or ^bb6)
cir.return
}
```
**ABI-lowered CIR**
```
cir.func @someFunc() #personality_fn = @ __CxxFrameHandler3 {
%0 = cir.alloca !rec_SomeClass, !cir.ptr<!rec_SomeClass>, ["c", init]
cir.try_call @_ZN9SomeClassC1Ev(%0) ^bb1, ^bb4 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb1
cir.try_call @_ZN9SomeClass11doSomethingEv(%0) ^bb2, ^bb3 : (!cir.ptr<!rec_SomeClass>) -> ()
^bb2 // Normal cleanup
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.br ^bb6
^bb3 // EH cleanup (from ^bb1)
%1 = cir.cleanuppad within none : !cir.cleanup_token
cir.call @_ZN9SomeClassD1Ev(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
cir.cleanupret from %1 unwind to ^bb4
^bb4 // Catch dispatch (from ^bb3 or ^bb4)
%2 = cir.catchswitch within none [^bb5] unwind to caller
^bb5
%catch.token = cir.catchpad within %2 [null : !cir.ptr<!void>] : !cir.catch_token
cir.catchret within %catch.token to ^bb6
^bb6 // Normal continue (from ^bb2 or ^bb6)
cir.return
}
```
#### Example: Try-catch with multiple catch handlers
**Flattened CIR**
```
cir.func @someFunc(){
cir.try_call @f() ^bb1, ^bb2
^bb1
cir.br ^bb7
^bb2 // EH catch (from entry block)
%1 = cir.eh.initiate : !cir.eh_token
cir.br ^bb3(%1 : !cir.eh_token)
^bb3(%eh_token : !cir.eh_token) // Catch dispatch (from ^bb2)
cir.eh.dispatch %eh_token : !cir.eh_token [
catch (#cir.global_view<@_ZTIi> : !u32i) : ^bb4
catch (#cir.global_view<@_ZTIf> : !u32i) : ^bb5
catch_all : ^bb6
]
^bb4(%eh_token.1 : !cir.eh_token) // Catch handler for int exception
%catch.token = cir.begin_catch(%eh_token.1 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb7
^bb5(%eh_token.2 : !cir.eh_token) // Catch handler for float exception
%catch.token = cir.begin_catch(%eh_token.2 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb7
^bb6(%eh_token.3 : !cir.eh_token) // Catch all handler
%catch.token = cir.begin_catch(%eh_token.3 : !cir.eh_token) : !cir.catch_token
cir.end_catch(%catch.token : !cir.catch_token)
cir.br ^bb7
^bb7 // Normal continue (from ^bb1, ^bb4, ^bb5, or ^bb6)
cir.return
}
```
**ABI-lowered CIR**
```
cir.func @someFunc() #personality_fn = @__CxxFrameHandler3 {
cir.try_call @f() ^bb1, ^bb2
^bb1
cir.br ^bb6
^bb2 // EH catch (from entry block)
%0 = cir.catchswitch within none [^bb3, ^bb4, ^bb5] unwind to caller
^bb3(%0: !cir.ptr<!void>) // Catch handler for int exception
%1 = cir.catchpad within %0 [eh.typeid @"??_R0H@8", 0, %0 : (!cir.ptr<!void>, !u32i, !cir.ptr<!void>)] : !cir.catch_token
cir.catchret from %1 to ^bb6
^bb4(%2: !cir.ptr<!void>) // Catch compare for float exception
%2 = cir.catchpad within %0 [eh.typeid @"??_R0M@8", 0, %0 : (!cir.ptr<!void>, !u32i, !cir.ptr<!void>)] : !cir.catch_token
cir.catchret from %2 to ^bb6
^bb5(%3: !cir.ptr<!void>) // Catch all handler
%4 = cir.catchpad within %0 [null, 64, null : (!cir.ptr<!void>, !u32i, !cir.ptr<!void>)] : !cir.catch_token
cir.catchret from %4 to ^bb6
^bb6 // Normal continue (from ^bb1, ^bb3, ^bb4, or ^bb5)
cir.return
}
```
In this example, if an exception is thrown by the `f()` call, it unwinds
to a catch dispatch block (`^bb2`), which uses the `cir.catchswitch`
operation to dispatch to a catch handler (`^bb3`, `^bb4`, or `^bb5`)
based on the type id of the exception. The actual comparisons in this
case will be handled by the personality function, using tables that are
generated from the `cir.catchpad` operations. Each catch handler simply
continues to the normal continuation block (`^bb6`) using the
`cir.catchret` operation.
|