Skip to content

Commit c15c671

Browse files
authored
Merge pull request #11034 from atrick/silval-work
2 parents 809045f + 4db2a46 commit c15c671

31 files changed

+301
-177
lines changed

docs/SIL.rst

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,9 +3828,9 @@ container may use one of several representations:
38283828
before IR generation.
38293829
The following instructions manipulate "loadable" opaque existential containers:
38303830

3831-
* `init_existential_opaque`_
3832-
* `open_existential_opaque`_
3833-
* `deinit_existential_opaque`_
3831+
* `init_existential_value`_
3832+
* `open_existential_value`_
3833+
* `deinit_existential_value`_
38343834

38353835
- **Class existential containers**: If a protocol type is constrained by one or
38363836
more class protocols, then the existential container for that type is
@@ -3859,6 +3859,7 @@ container may use one of several representations:
38593859
* `alloc_existential_box`_
38603860
* `project_existential_box`_
38613861
* `open_existential_box`_
3862+
* `open_existential_box_value`_
38623863
* `dealloc_existential_box`_
38633864

38643865
Some existential types may additionally support specialized representations
@@ -3904,14 +3905,14 @@ initialized existential container can be destroyed with ``destroy_addr`` as
39043905
usual. It is undefined behavior to ``destroy_addr`` a partially-initialized
39053906
existential container.
39063907

3907-
init_existential_opaque
3908-
```````````````````````
3908+
init_existential_value
3909+
``````````````````````
39093910
::
39103911

3911-
sil-instruction ::= 'init_existential_opaque' sil-operand ':' sil-type ','
3912+
sil-instruction ::= 'init_existential_value' sil-operand ':' sil-type ','
39123913
sil-type
39133914

3914-
%1 = init_existential_opaque %0 : $L' : $C, $P
3915+
%1 = init_existential_value %0 : $L' : $C, $P
39153916
// %0 must be of loadable type $L', lowered from AST type $L, conforming to
39163917
// protocol(s) $P
39173918
// %1 will be of type $P
@@ -3935,20 +3936,20 @@ existential containers that have been partially initialized by
39353936
``init_existential_addr`` but haven't had their contained value initialized.
39363937
A fully initialized existential must be destroyed with ``destroy_addr``.
39373938

3938-
deinit_existential_opaque
3939-
`````````````````````````
3939+
deinit_existential_value
3940+
````````````````````````
39403941
::
39413942

3942-
sil-instruction ::= 'deinit_existential_opaque' sil-operand
3943+
sil-instruction ::= 'deinit_existential_value' sil-operand
39433944

3944-
deinit_existential_opaque %0 : $P
3945+
deinit_existential_value %0 : $P
39453946
// %0 must be of a $P opaque type for non-class protocol or protocol
39463947
// composition type P
39473948

39483949
Undoes the partial initialization performed by
3949-
``init_existential_opaque``. ``deinit_existential_opaque`` is only valid for
3950+
``init_existential_value``. ``deinit_existential_value`` is only valid for
39503951
existential containers that have been partially initialized by
3951-
``init_existential_opaque`` but haven't had their contained value initialized.
3952+
``init_existential_value`` but haven't had their contained value initialized.
39523953
A fully initialized existential must be destroyed with ``destroy_value``.
39533954

39543955
open_existential_addr
@@ -3976,13 +3977,13 @@ access constraint: The returned address can either allow ``mutable_access`` or
39763977
(e.g ``destroy_addr`` or ``copy_addr [take]``) or mutate the value at the
39773978
address if they have ``mutable_access``.
39783979

3979-
open_existential_opaque
3980-
```````````````````````
3980+
open_existential_value
3981+
``````````````````````
39813982
::
39823983

3983-
sil-instruction ::= 'open_existential_opaque' sil-operand 'to' sil-type
3984+
sil-instruction ::= 'open_existential_value' sil-operand 'to' sil-type
39843985

3985-
%1 = open_existential_opaque %0 : $P to $@opened P
3986+
%1 = open_existential_value %0 : $P to $@opened P
39863987
// %0 must be of a $P type for non-class protocol or protocol composition
39873988
// type P
39883989
// $@opened P must be a unique archetype that refers to an opened
@@ -4118,6 +4119,22 @@ the owning box and the enclosing function; in order to "open" a boxed
41184119
existential that has directly adopted a class reference, temporary scratch
41194120
space may need to have been allocated.
41204121

4122+
open_existential_box_value
4123+
``````````````````````````
4124+
::
4125+
4126+
sil-instruction ::= 'open_existential_box_value' sil-operand 'to' sil-type
4127+
4128+
%1 = open_existential_box_value %0 : $P to $@opened P
4129+
// %0 must be a value of boxed protocol or protocol composition type $P
4130+
// %@opened P must be a unique archetype that refers to an opened
4131+
// existential type P
4132+
// %1 will be of type $@opened P
4133+
4134+
Projects the value inside a boxed existential container, and uses the enclosed
4135+
type and protocol conformance metadata to bind the opened archetype ``$@opened
4136+
P``.
4137+
41214138
dealloc_existential_box
41224139
```````````````````````
41234140
::

include/swift/SIL/PatternMatch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,12 @@ UNARY_OP_MATCH_WITH_ARG_MATCHER(SuperMethodInst)
366366
UNARY_OP_MATCH_WITH_ARG_MATCHER(DynamicMethodInst)
367367
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialAddrInst)
368368
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialRefInst)
369-
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialOpaqueInst)
369+
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialValueInst)
370370
UNARY_OP_MATCH_WITH_ARG_MATCHER(InitExistentialAddrInst)
371-
UNARY_OP_MATCH_WITH_ARG_MATCHER(InitExistentialOpaqueInst)
371+
UNARY_OP_MATCH_WITH_ARG_MATCHER(InitExistentialValueInst)
372372
UNARY_OP_MATCH_WITH_ARG_MATCHER(InitExistentialRefInst)
373373
UNARY_OP_MATCH_WITH_ARG_MATCHER(DeinitExistentialAddrInst)
374-
UNARY_OP_MATCH_WITH_ARG_MATCHER(DeinitExistentialOpaqueInst)
374+
UNARY_OP_MATCH_WITH_ARG_MATCHER(DeinitExistentialValueInst)
375375
UNARY_OP_MATCH_WITH_ARG_MATCHER(ProjectBlockStorageInst)
376376
UNARY_OP_MATCH_WITH_ARG_MATCHER(StrongRetainInst)
377377
UNARY_OP_MATCH_WITH_ARG_MATCHER(StrongReleaseInst)

include/swift/SIL/SILBuilder.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,10 +1188,10 @@ class SILBuilder {
11881188
return I;
11891189
}
11901190

1191-
OpenExistentialOpaqueInst *createOpenExistentialOpaque(SILLocation Loc,
1191+
OpenExistentialValueInst *createOpenExistentialValue(SILLocation Loc,
11921192
SILValue Operand,
11931193
SILType SelfTy) {
1194-
auto *I = insert(new (F.getModule()) OpenExistentialOpaqueInst(
1194+
auto *I = insert(new (F.getModule()) OpenExistentialValueInst(
11951195
getSILDebugLocation(Loc), Operand, SelfTy));
11961196
if (OpenedArchetypesTracker)
11971197
OpenedArchetypesTracker->registerOpenedArchetypes(I);
@@ -1226,6 +1226,15 @@ class SILBuilder {
12261226
return I;
12271227
}
12281228

1229+
OpenExistentialBoxValueInst *
1230+
createOpenExistentialBoxValue(SILLocation Loc, SILValue Operand, SILType Ty) {
1231+
auto *I = insert(new (F.getModule()) OpenExistentialBoxValueInst(
1232+
getSILDebugLocation(Loc), Operand, Ty));
1233+
if (OpenedArchetypesTracker)
1234+
OpenedArchetypesTracker->registerOpenedArchetypes(I);
1235+
return I;
1236+
}
1237+
12291238
InitExistentialAddrInst *
12301239
createInitExistentialAddr(SILLocation Loc, SILValue Existential,
12311240
CanType FormalConcreteType,
@@ -1236,11 +1245,11 @@ class SILBuilder {
12361245
LoweredConcreteType, Conformances, &F, OpenedArchetypes));
12371246
}
12381247

1239-
InitExistentialOpaqueInst *
1240-
createInitExistentialOpaque(SILLocation Loc, SILType ExistentialType,
1248+
InitExistentialValueInst *
1249+
createInitExistentialValue(SILLocation Loc, SILType ExistentialType,
12411250
CanType FormalConcreteType, SILValue Concrete,
12421251
ArrayRef<ProtocolConformanceRef> Conformances) {
1243-
return insert(InitExistentialOpaqueInst::create(
1252+
return insert(InitExistentialValueInst::create(
12441253
getSILDebugLocation(Loc), ExistentialType, FormalConcreteType, Concrete,
12451254
Conformances, &F, OpenedArchetypes));
12461255
}
@@ -1269,9 +1278,9 @@ class SILBuilder {
12691278
getSILDebugLocation(Loc), Existential));
12701279
}
12711280

1272-
DeinitExistentialOpaqueInst *
1273-
createDeinitExistentialOpaque(SILLocation Loc, SILValue Existential) {
1274-
return insert(new (F.getModule()) DeinitExistentialOpaqueInst(
1281+
DeinitExistentialValueInst *
1282+
createDeinitExistentialValue(SILLocation Loc, SILValue Existential) {
1283+
return insert(new (F.getModule()) DeinitExistentialValueInst(
12751284
getSILDebugLocation(Loc), Existential));
12761285
}
12771286

include/swift/SIL/SILCloner.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,8 +1586,8 @@ SILCloner<ImplClass>::visitOpenExistentialAddrInst(OpenExistentialAddrInst *Inst
15861586
}
15871587

15881588
template <typename ImplClass>
1589-
void SILCloner<ImplClass>::visitOpenExistentialOpaqueInst(
1590-
OpenExistentialOpaqueInst *Inst) {
1589+
void SILCloner<ImplClass>::visitOpenExistentialValueInst(
1590+
OpenExistentialValueInst *Inst) {
15911591
// Create a new archetype for this opened existential type.
15921592
auto archetypeTy =
15931593
Inst->getType().getSwiftRValueType()->castTo<ArchetypeType>();
@@ -1596,7 +1596,7 @@ void SILCloner<ImplClass>::visitOpenExistentialOpaqueInst(
15961596
ArchetypeType::getOpened(archetypeTy->getOpenedExistentialType()));
15971597

15981598
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1599-
doPostProcess(Inst, getBuilder().createOpenExistentialOpaque(
1599+
doPostProcess(Inst, getBuilder().createOpenExistentialValue(
16001600
getOpLocation(Inst->getLoc()),
16011601
getOpValue(Inst->getOperand()),
16021602
getOpType(Inst->getType())));
@@ -1671,6 +1671,24 @@ visitOpenExistentialBoxInst(OpenExistentialBoxInst *Inst) {
16711671
getOpType(Inst->getType())));
16721672
}
16731673

1674+
template<typename ImplClass>
1675+
void
1676+
SILCloner<ImplClass>::
1677+
visitOpenExistentialBoxValueInst(OpenExistentialBoxValueInst *Inst) {
1678+
// Create a new archetype for this opened existential type.
1679+
auto archetypeTy
1680+
= Inst->getType().getSwiftRValueType()->castTo<ArchetypeType>();
1681+
registerOpenedExistentialRemapping(
1682+
archetypeTy,
1683+
ArchetypeType::getOpened(archetypeTy->getOpenedExistentialType()));
1684+
1685+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1686+
doPostProcess(Inst,
1687+
getBuilder().createOpenExistentialBoxValue(getOpLocation(Inst->getLoc()),
1688+
getOpValue(Inst->getOperand()),
1689+
getOpType(Inst->getType())));
1690+
}
1691+
16741692
template<typename ImplClass>
16751693
void
16761694
SILCloner<ImplClass>::visitInitExistentialAddrInst(InitExistentialAddrInst *Inst) {
@@ -1689,16 +1707,16 @@ SILCloner<ImplClass>::visitInitExistentialAddrInst(InitExistentialAddrInst *Inst
16891707
}
16901708

16911709
template <typename ImplClass>
1692-
void SILCloner<ImplClass>::visitInitExistentialOpaqueInst(
1693-
InitExistentialOpaqueInst *Inst) {
1710+
void SILCloner<ImplClass>::visitInitExistentialValueInst(
1711+
InitExistentialValueInst *Inst) {
16941712
CanType origFormalType = Inst->getFormalConcreteType();
16951713

16961714
auto conformances = getOpConformances(origFormalType,
16971715
Inst->getConformances());
16981716

16991717
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
17001718
doPostProcess(Inst,
1701-
getBuilder().createInitExistentialOpaque(
1719+
getBuilder().createInitExistentialValue(
17021720
getOpLocation(Inst->getLoc()), getOpType(Inst->getType()),
17031721
getOpASTType(origFormalType),
17041722
getOpValue(Inst->getOperand()), conformances));
@@ -1747,11 +1765,11 @@ SILCloner<ImplClass>::visitDeinitExistentialAddrInst(DeinitExistentialAddrInst *
17471765
}
17481766

17491767
template <typename ImplClass>
1750-
void SILCloner<ImplClass>::visitDeinitExistentialOpaqueInst(
1751-
DeinitExistentialOpaqueInst *Inst) {
1768+
void SILCloner<ImplClass>::visitDeinitExistentialValueInst(
1769+
DeinitExistentialValueInst *Inst) {
17521770
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
17531771
doPostProcess(
1754-
Inst, getBuilder().createDeinitExistentialOpaque(
1772+
Inst, getBuilder().createDeinitExistentialValue(
17551773
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
17561774
}
17571775

include/swift/SIL/SILInstruction.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,11 +4700,11 @@ class OpenExistentialAddrInst
47004700
/// Given an opaque value referring to an existential, "opens" the
47014701
/// existential by returning a pointer to a fresh archetype T, which also
47024702
/// captures the (dynamic) conformances.
4703-
class OpenExistentialOpaqueInst
4704-
: public UnaryInstructionBase<ValueKind::OpenExistentialOpaqueInst> {
4703+
class OpenExistentialValueInst
4704+
: public UnaryInstructionBase<ValueKind::OpenExistentialValueInst> {
47054705
friend SILBuilder;
47064706

4707-
OpenExistentialOpaqueInst(SILDebugLocation DebugLoc, SILValue Operand,
4707+
OpenExistentialValueInst(SILDebugLocation DebugLoc, SILValue Operand,
47084708
SILType SelfTy);
47094709
};
47104710

@@ -4745,6 +4745,17 @@ class OpenExistentialBoxInst
47454745
SILType ty);
47464746
};
47474747

4748+
/// Given a boxed existential container, "opens" the existential by returning a
4749+
/// fresh archetype T, which also captures the (dynamic) conformances.
4750+
class OpenExistentialBoxValueInst
4751+
: public UnaryInstructionBase<ValueKind::OpenExistentialBoxValueInst>
4752+
{
4753+
friend SILBuilder;
4754+
4755+
OpenExistentialBoxValueInst(SILDebugLocation DebugLoc, SILValue operand,
4756+
SILType ty);
4757+
};
4758+
47484759
/// Given an address to an uninitialized buffer of
47494760
/// a protocol type, initializes its existential container to contain a concrete
47504761
/// value of the given type, and returns the address of the uninitialized
@@ -4794,24 +4805,24 @@ class InitExistentialAddrInst final
47944805
/// initializes its existential container to contain a concrete
47954806
/// value of the given type, and returns the uninitialized
47964807
/// concrete value inside the existential container.
4797-
class InitExistentialOpaqueInst final
4808+
class InitExistentialValueInst final
47984809
: public UnaryInstructionWithTypeDependentOperandsBase<
4799-
ValueKind::InitExistentialOpaqueInst, InitExistentialOpaqueInst,
4810+
ValueKind::InitExistentialValueInst, InitExistentialValueInst,
48004811
SILInstruction, true> {
48014812
friend SILBuilder;
48024813

48034814
CanType ConcreteType;
48044815
ArrayRef<ProtocolConformanceRef> Conformances;
48054816

4806-
InitExistentialOpaqueInst(SILDebugLocation DebugLoc, SILType ExistentialType,
4817+
InitExistentialValueInst(SILDebugLocation DebugLoc, SILType ExistentialType,
48074818
CanType FormalConcreteType, SILValue Instance,
48084819
ArrayRef<SILValue> TypeDependentOperands,
48094820
ArrayRef<ProtocolConformanceRef> Conformances)
48104821
: UnaryInstructionWithTypeDependentOperandsBase(
48114822
DebugLoc, Instance, TypeDependentOperands, ExistentialType),
48124823
ConcreteType(FormalConcreteType), Conformances(Conformances) {}
48134824

4814-
static InitExistentialOpaqueInst *
4825+
static InitExistentialValueInst *
48154826
create(SILDebugLocation DebugLoc, SILType ExistentialType,
48164827
CanType ConcreteType, SILValue Instance,
48174828
ArrayRef<ProtocolConformanceRef> Conformances, SILFunction *Parent,
@@ -4928,12 +4939,12 @@ class DeinitExistentialAddrInst
49284939
: UnaryInstructionBase(DebugLoc, Existential) {}
49294940
};
49304941

4931-
class DeinitExistentialOpaqueInst
4932-
: public UnaryInstructionBase<ValueKind::DeinitExistentialOpaqueInst,
4942+
class DeinitExistentialValueInst
4943+
: public UnaryInstructionBase<ValueKind::DeinitExistentialValueInst,
49334944
SILInstruction, /*HAS_RESULT*/ false> {
49344945
friend SILBuilder;
49354946

4936-
DeinitExistentialOpaqueInst(SILDebugLocation DebugLoc, SILValue Existential)
4947+
DeinitExistentialValueInst(SILDebugLocation DebugLoc, SILValue Existential)
49374948
: UnaryInstructionBase(DebugLoc, Existential) {}
49384949
};
49394950

include/swift/SIL/SILNodes.def

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,19 @@ ABSTRACT_VALUE(SILInstruction, ValueBase)
233233

234234
// Protocol and Protocol Composition Types
235235
INST(InitExistentialAddrInst, SILInstruction, init_existential_addr, MayWrite, DoesNotRelease)
236-
INST(InitExistentialOpaqueInst, SILInstruction, init_existential_opaque, MayWrite, DoesNotRelease)
236+
INST(InitExistentialValueInst, SILInstruction, init_existential_value, MayWrite, DoesNotRelease)
237237
INST(DeinitExistentialAddrInst, SILInstruction, deinit_existential_addr, MayHaveSideEffects,
238238
DoesNotRelease)
239-
INST(DeinitExistentialOpaqueInst, SILInstruction, deinit_existential_opaque, MayHaveSideEffects,
239+
INST(DeinitExistentialValueInst, SILInstruction, deinit_existential_value, MayHaveSideEffects,
240240
DoesNotRelease)
241241
INST(OpenExistentialAddrInst, SILInstruction, open_existential_addr, MayRead, DoesNotRelease)
242242
INST(InitExistentialRefInst, SILInstruction, init_existential_ref, None, DoesNotRelease)
243243
INST(OpenExistentialRefInst, SILInstruction, open_existential_ref, None, DoesNotRelease)
244244
INST(InitExistentialMetatypeInst, SILInstruction, init_existential_metatype, None, DoesNotRelease)
245245
INST(OpenExistentialMetatypeInst, SILInstruction, open_existential_metatype, None, DoesNotRelease)
246246
INST(OpenExistentialBoxInst, SILInstruction, open_existential_box, MayRead, DoesNotRelease)
247-
INST(OpenExistentialOpaqueInst, SILInstruction, open_existential_opaque, MayRead, DoesNotRelease)
247+
INST(OpenExistentialValueInst, SILInstruction, open_existential_value, MayRead, DoesNotRelease)
248+
INST(OpenExistentialBoxValueInst, SILInstruction, open_existential_box_value, MayRead, DoesNotRelease)
248249

249250
// Blocks
250251
INST(ProjectBlockStorageInst, SILInstruction, project_block_storage, None, DoesNotRelease)

0 commit comments

Comments
 (0)