16
16
#include " swift/SIL/SILDebugScope.h"
17
17
#include " swift/SIL/SILFunction.h"
18
18
#include " swift/SIL/SILModule.h"
19
+ #include " swift/SIL/SILOpenedArchetypesTracker.h"
19
20
#include " llvm/ADT/PointerUnion.h"
20
21
#include " llvm/ADT/StringExtras.h"
21
22
@@ -40,6 +41,40 @@ class SILBuilder {
40
41
// / recorded in this list.
41
42
SmallVectorImpl<SILInstruction *> *InsertedInstrs = nullptr ;
42
43
44
+ // / Maps opened archetypes to their definitions. If provided,
45
+ // / can be used by the builder. It is supposed to be used
46
+ // / only by SILGen or SIL deserializers.
47
+ SILOpenedArchetypesTracker *OpenedArchetypes = nullptr ;
48
+
49
+ bool canProvideTypeDefs () const {
50
+ return OpenedArchetypes != nullptr ;
51
+ }
52
+
53
+ // Build a set of typedef operands for an apply instruction. This is done by
54
+ // scanning the list of substitutions and collecting all opened archetypes.
55
+ void createApplyTypeDefs (SILInstruction *I, ArrayRef<Substitution> Subs) {
56
+ // Copy the typedefs into the apply instruction.
57
+ if (canProvideTypeDefs ()) {
58
+ unsigned Idx = 0 ;
59
+ auto TypeDefs = I->getTypeDefOperandsBuf ();
60
+ for (auto Sub : Subs) {
61
+ auto Ty = Sub.getReplacement ().getCanonicalTypeOrNull ();
62
+ if (!Ty->hasOpenedExistential ())
63
+ continue ;
64
+ Ty.visit ([&](Type t) {
65
+ if (t->isOpenedExistential ()) {
66
+ auto Def =
67
+ getOpenedArchetypeDef (getModule ().Types .getLoweredType (t),
68
+ getModule (), OpenedArchetypes);
69
+ assert (getOpenedArchetypeOf (Def->getType ().getSwiftRValueType ()) &&
70
+ " Typedef should be of an opened existential type" );
71
+ TypeDefs[Idx++].set (Def);
72
+ }
73
+ });
74
+ }
75
+ }
76
+ }
77
+
43
78
public:
44
79
SILBuilder (SILFunction &F) : F(F), BB(0 ) {}
45
80
@@ -75,6 +110,14 @@ class SILBuilder {
75
110
return F.getModule ().getTypeLowering (T);
76
111
}
77
112
113
+ void setOpenedArchetypes (SILOpenedArchetypesTracker &OpenedArchetypes) {
114
+ this ->OpenedArchetypes = &OpenedArchetypes;
115
+ }
116
+
117
+ SILOpenedArchetypesTracker *getOpenedArchetypes () const {
118
+ return OpenedArchetypes;
119
+ }
120
+
78
121
void setCurrentDebugScope (const SILDebugScope *DS) { CurDebugScope = DS; }
79
122
const SILDebugScope *getCurrentDebugScope () const { return CurDebugScope; }
80
123
@@ -260,8 +303,10 @@ class SILBuilder {
260
303
ApplyInst *createApply (SILLocation Loc, SILValue Fn, SILType SubstFnTy,
261
304
SILType Result, ArrayRef<Substitution> Subs,
262
305
ArrayRef<SILValue> Args, bool isNonThrowing) {
263
- return insert (ApplyInst::create (getSILDebugLocation (Loc), Fn, SubstFnTy,
306
+ auto *I = insert (ApplyInst::create (getSILDebugLocation (Loc), Fn, SubstFnTy,
264
307
Result, Subs, Args, isNonThrowing, F));
308
+ createApplyTypeDefs (I, Subs);
309
+ return I;
265
310
}
266
311
267
312
ApplyInst *createApply (SILLocation Loc, SILValue Fn, ArrayRef<SILValue> Args,
@@ -276,18 +321,22 @@ class SILBuilder {
276
321
ArrayRef<Substitution> subs,
277
322
ArrayRef<SILValue> args, SILBasicBlock *normalBB,
278
323
SILBasicBlock *errorBB) {
279
- return insertTerminator (TryApplyInst::create (getSILDebugLocation (Loc),
324
+ auto *I = insertTerminator (TryApplyInst::create (getSILDebugLocation (Loc),
280
325
fn, substFnTy, subs, args,
281
326
normalBB, errorBB, F));
327
+ createApplyTypeDefs (I, subs);
328
+ return I;
282
329
}
283
330
284
331
PartialApplyInst *createPartialApply (SILLocation Loc, SILValue Fn,
285
332
SILType SubstFnTy,
286
333
ArrayRef<Substitution> Subs,
287
334
ArrayRef<SILValue> Args,
288
335
SILType ClosureTy) {
289
- return insert (PartialApplyInst::create (
336
+ auto I = insert (PartialApplyInst::create (
290
337
getSILDebugLocation (Loc), Fn, SubstFnTy, Subs, Args, ClosureTy, F));
338
+ createApplyTypeDefs (I, Subs);
339
+ return I;
291
340
}
292
341
293
342
BuiltinInst *createBuiltin (SILLocation Loc, Identifier Name, SILType ResultTy,
@@ -526,8 +575,11 @@ class SILBuilder {
526
575
527
576
UncheckedRefCastInst *createUncheckedRefCast (SILLocation Loc, SILValue Op,
528
577
SILType Ty) {
529
- return insert (new (F.getModule ()) UncheckedRefCastInst (
578
+ auto *I = insert (new (F.getModule ()) UncheckedRefCastInst (
530
579
getSILDebugLocation (Loc), Op, Ty));
580
+ I->getTypeDefOperandsBuf ()[0 ].set (
581
+ getOpenedArchetypeDef (Ty, getModule (), OpenedArchetypes));
582
+ return I;
531
583
}
532
584
533
585
UncheckedRefCastAddrInst *
@@ -539,20 +591,29 @@ class SILBuilder {
539
591
540
592
UncheckedAddrCastInst *createUncheckedAddrCast (SILLocation Loc, SILValue Op,
541
593
SILType Ty) {
542
- return insert (new (F.getModule ()) UncheckedAddrCastInst (
594
+ auto *I = insert (new (F.getModule ()) UncheckedAddrCastInst (
543
595
getSILDebugLocation (Loc), Op, Ty));
596
+ I->getTypeDefOperandsBuf ()[0 ].set (
597
+ getOpenedArchetypeDef (Ty, getModule (), OpenedArchetypes));
598
+ return I;
544
599
}
545
600
546
601
UncheckedTrivialBitCastInst *
547
602
createUncheckedTrivialBitCast (SILLocation Loc, SILValue Op, SILType Ty) {
548
- return insert (new (F.getModule ()) UncheckedTrivialBitCastInst (
603
+ auto *I = insert (new (F.getModule ()) UncheckedTrivialBitCastInst (
549
604
getSILDebugLocation (Loc), Op, Ty));
605
+ I->getTypeDefOperandsBuf ()[0 ].set (
606
+ getOpenedArchetypeDef (Ty, getModule (), OpenedArchetypes));
607
+ return I;
550
608
}
551
609
552
610
UncheckedBitwiseCastInst *
553
611
createUncheckedBitwiseCast (SILLocation Loc, SILValue Op, SILType Ty) {
554
- return insert (new (F.getModule ()) UncheckedBitwiseCastInst (
612
+ auto *I = insert (new (F.getModule ()) UncheckedBitwiseCastInst (
555
613
getSILDebugLocation (Loc), Op, Ty));
614
+ I->getTypeDefOperandsBuf ()[0 ].set (
615
+ getOpenedArchetypeDef (Ty, getModule (), OpenedArchetypes));
616
+ return I;
556
617
}
557
618
558
619
RefToBridgeObjectInst *createRefToBridgeObject (SILLocation Loc, SILValue Ref,
@@ -648,8 +709,11 @@ class SILBuilder {
648
709
649
710
UnconditionalCheckedCastInst *
650
711
createUnconditionalCheckedCast (SILLocation Loc, SILValue op, SILType destTy) {
651
- return insert (new (F.getModule ()) UnconditionalCheckedCastInst (
712
+ auto *I = insert (new (F.getModule ()) UnconditionalCheckedCastInst (
652
713
getSILDebugLocation (Loc), op, destTy));
714
+ I->getTypeDefOperandsBuf ()[0 ].set (
715
+ getOpenedArchetypeDef (destTy, getModule (), OpenedArchetypes));
716
+ return I;
653
717
}
654
718
655
719
UnconditionalCheckedCastAddrInst *createUnconditionalCheckedCastAddr (
@@ -912,55 +976,78 @@ class SILBuilder {
912
976
913
977
OpenExistentialAddrInst *
914
978
createOpenExistentialAddr (SILLocation Loc, SILValue Operand, SILType SelfTy) {
915
- return insert (new (F.getModule ()) OpenExistentialAddrInst (
979
+ auto *I = insert (new (F.getModule ()) OpenExistentialAddrInst (
916
980
getSILDebugLocation (Loc), Operand, SelfTy));
981
+ if (canProvideTypeDefs ())
982
+ OpenedArchetypes->registerOpenedArchetypes (I);
983
+ return I;
917
984
}
918
985
919
986
OpenExistentialMetatypeInst *createOpenExistentialMetatype (SILLocation Loc,
920
987
SILValue operand,
921
988
SILType selfTy) {
922
- return insert (new (F.getModule ()) OpenExistentialMetatypeInst (
989
+ auto *I = insert (new (F.getModule ()) OpenExistentialMetatypeInst (
923
990
getSILDebugLocation (Loc), operand, selfTy));
924
- }
991
+ if (canProvideTypeDefs ())
992
+ OpenedArchetypes->registerOpenedArchetypes (I);
993
+ return I;
994
+ }
925
995
926
996
OpenExistentialRefInst *
927
997
createOpenExistentialRef (SILLocation Loc, SILValue Operand, SILType Ty) {
928
- return insert (new (F.getModule ()) OpenExistentialRefInst (
998
+ auto *I = insert (new (F.getModule ()) OpenExistentialRefInst (
929
999
getSILDebugLocation (Loc), Operand, Ty));
1000
+ if (canProvideTypeDefs ())
1001
+ OpenedArchetypes->registerOpenedArchetypes (I);
1002
+ return I;
930
1003
}
931
1004
932
1005
OpenExistentialBoxInst *
933
1006
createOpenExistentialBox (SILLocation Loc, SILValue Operand, SILType Ty) {
934
- return insert (new (F.getModule ()) OpenExistentialBoxInst (
1007
+ auto *I = insert (new (F.getModule ()) OpenExistentialBoxInst (
935
1008
getSILDebugLocation (Loc), Operand, Ty));
936
- }
1009
+ if (canProvideTypeDefs ())
1010
+ OpenedArchetypes->registerOpenedArchetypes (I);
1011
+ return I;
1012
+ }
937
1013
938
1014
InitExistentialAddrInst *
939
1015
createInitExistentialAddr (SILLocation Loc, SILValue Existential,
940
1016
CanType FormalConcreteType,
941
1017
SILType LoweredConcreteType,
942
1018
ArrayRef<ProtocolConformanceRef> Conformances) {
943
- return insert (InitExistentialAddrInst::create (
1019
+ auto *I = insert (InitExistentialAddrInst::create (
944
1020
getSILDebugLocation (Loc), Existential, FormalConcreteType,
945
1021
LoweredConcreteType, Conformances, &F));
1022
+ I->getTypeDefOperandsBuf ()[0 ].set (getOpenedArchetypeDef (
1023
+ getModule ().Types .getLoweredType (FormalConcreteType), getModule (),
1024
+ OpenedArchetypes));
1025
+ return I;
946
1026
}
947
1027
948
1028
InitExistentialMetatypeInst *
949
1029
createInitExistentialMetatype (SILLocation Loc, SILValue metatype,
950
1030
SILType existentialType,
951
1031
ArrayRef<ProtocolConformanceRef> conformances) {
952
- return insert (InitExistentialMetatypeInst::create (
1032
+ auto *I = insert (InitExistentialMetatypeInst::create (
953
1033
getSILDebugLocation (Loc), existentialType, metatype, conformances,
954
1034
&F));
1035
+ I->getTypeDefOperandsBuf ()[0 ].set (
1036
+ getOpenedArchetypeDef (existentialType, getModule (), OpenedArchetypes));
1037
+ return I;
955
1038
}
956
1039
957
1040
InitExistentialRefInst *
958
1041
createInitExistentialRef (SILLocation Loc, SILType ExistentialType,
959
1042
CanType FormalConcreteType, SILValue Concrete,
960
1043
ArrayRef<ProtocolConformanceRef> Conformances) {
961
- return insert (InitExistentialRefInst::create (
1044
+ auto *I = insert (InitExistentialRefInst::create (
962
1045
getSILDebugLocation (Loc), ExistentialType, FormalConcreteType,
963
1046
Concrete, Conformances, &F));
1047
+ I->getTypeDefOperandsBuf ()[0 ].set (getOpenedArchetypeDef (
1048
+ getModule ().Types .getLoweredType (FormalConcreteType), getModule (),
1049
+ OpenedArchetypes));
1050
+ return I;
964
1051
}
965
1052
966
1053
DeinitExistentialAddrInst *createDeinitExistentialAddr (SILLocation Loc,
@@ -1306,9 +1393,12 @@ class SILBuilder {
1306
1393
SILValue op, SILType destTy,
1307
1394
SILBasicBlock *successBB,
1308
1395
SILBasicBlock *failureBB) {
1309
- return insertTerminator (new (F.getModule ()) CheckedCastBranchInst (
1310
- getSILDebugLocation (Loc), isExact, op, destTy, successBB,
1311
- failureBB));
1396
+ auto *I = insertTerminator (new (F.getModule ()) CheckedCastBranchInst (
1397
+ getSILDebugLocation (Loc), isExact, op, destTy,
1398
+ successBB, failureBB));
1399
+ I->getTypeDefOperandsBuf ()[0 ].set (
1400
+ getOpenedArchetypeDef (destTy, getModule (), OpenedArchetypes));
1401
+ return I;
1312
1402
}
1313
1403
1314
1404
CheckedCastAddrBranchInst *
0 commit comments