Skip to content

Commit b9384f5

Browse files
authored
Merge pull request #12265 from slavapestov/sil-dynamic-method
Fold dynamic_method into objc_method
2 parents 1d2d0a6 + 514aa99 commit b9384f5

26 files changed

+131
-265
lines changed

docs/SIL.rst

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,7 @@ VTables
10341034
sil-vtable-entry ::= sil-decl-ref ':' sil-linkage? sil-function-name
10351035

10361036
SIL represents dynamic dispatch for class methods using the `class_method`_,
1037-
`super_method`_, `objc_method`_, `objc_super_method`_ and `dynamic_method`_
1038-
instructions.
1037+
`super_method`_, `objc_method`_, and `objc_super_method`_ instructions.
10391038

10401039
The potential destinations for `class_method`_ and `super_method`_ are
10411040
tracked in ``sil_vtable`` declarations for every class type. The declaration
@@ -2945,10 +2944,9 @@ These instructions perform dynamic lookup of class and generic methods.
29452944
The ``class_method`` and ``super_method`` instructions must reference
29462945
Swift native methods and always use vtable dispatch.
29472946

2948-
The ``objc_method``, ``objc_super_method`` and ``dynamic_method``
2949-
instructions must reference Objective-C methods (indicated by the
2950-
``foreign`` marker on a method reference, as in
2951-
``#NSObject.description!1.foreign``).
2947+
The ``objc_method`` and ``objc_super_method`` instructions must reference
2948+
Objective-C methods (indicated by the ``foreign`` marker on a method
2949+
reference, as in ``#NSObject.description!1.foreign``).
29522950

29532951
Note that ``objc_msgSend`` invocations can only be used as the callee
29542952
of an ``apply`` instruction or ``partial_apply`` instruction. They cannot
@@ -3046,40 +3044,6 @@ archetype of the original protocol and have the ``witness_method`` calling
30463044
convention. If the referenced protocol is an ``@objc`` protocol, the
30473045
resulting type has the ``objc`` calling convention.
30483046

3049-
dynamic_method
3050-
``````````````
3051-
::
3052-
3053-
sil-instruction ::= 'dynamic_method' sil-method-attributes?
3054-
sil-operand ',' sil-decl-ref ':' sil-type
3055-
3056-
%1 = dynamic_method %0 : $P, #X.method!1 : $@convention(thin) U -> V
3057-
// %0 must be of a protocol or protocol composition type $P,
3058-
// where $P contains the Swift.DynamicLookup protocol
3059-
// #X.method!1 must be a reference to an @objc method of any class
3060-
// or protocol type
3061-
//
3062-
// The "self" argument of the method type $@convention(thin) U -> V must be
3063-
// AnyObject
3064-
3065-
Looks up the implementation of an Objective-C method with the same
3066-
selector as the named method for the dynamic type of the
3067-
value inside an existential container. The "self" operand of the result
3068-
function value is represented using an opaque type, the value for which must
3069-
be projected out as a value of type ``AnyObject``.
3070-
3071-
It is undefined behavior if the dynamic type of the operand does not
3072-
have an implementation for the Objective-C method with the selector to
3073-
which the ``dynamic_method`` instruction refers, or if that
3074-
implementation has parameter or result types that are incompatible
3075-
with the method referenced by ``dynamic_method``.
3076-
This instruction should only be used in cases where its result will be
3077-
immediately consumed by an operation that performs the selector check
3078-
itself (e.g., an ``apply`` that lowers to ``objc_msgSend``).
3079-
To query whether the operand has an implementation for the given
3080-
method and safely handle the case where it does not, use
3081-
`dynamic_method_br`_.
3082-
30833047
Function Application
30843048
~~~~~~~~~~~~~~~~~~~~
30853049

include/swift/SIL/PatternMatch.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ UNARY_OP_MATCH_WITH_ARG_MATCHER(ClassMethodInst)
370370
UNARY_OP_MATCH_WITH_ARG_MATCHER(ObjCMethodInst)
371371
UNARY_OP_MATCH_WITH_ARG_MATCHER(SuperMethodInst)
372372
UNARY_OP_MATCH_WITH_ARG_MATCHER(ObjCSuperMethodInst)
373-
UNARY_OP_MATCH_WITH_ARG_MATCHER(DynamicMethodInst)
374373
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialAddrInst)
375374
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialRefInst)
376375
UNARY_OP_MATCH_WITH_ARG_MATCHER(OpenExistentialValueInst)

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,8 +1198,9 @@ class SILBuilder {
11981198

11991199
ObjCMethodInst *createObjCMethod(SILLocation Loc, SILValue Operand,
12001200
SILDeclRef Member, SILType MethodTy) {
1201-
return insert(new (getModule()) ObjCMethodInst(
1202-
getSILDebugLocation(Loc), Operand, Member, MethodTy));
1201+
return insert(ObjCMethodInst::create(
1202+
getSILDebugLocation(Loc), Operand, Member, MethodTy,
1203+
&getFunction(), OpenedArchetypes));
12031204
}
12041205

12051206
ObjCSuperMethodInst *createObjCSuperMethod(SILLocation Loc, SILValue Operand,
@@ -1217,13 +1218,6 @@ class SILBuilder {
12171218
&getFunction(), OpenedArchetypes, Volatile));
12181219
}
12191220

1220-
DynamicMethodInst *createDynamicMethod(SILLocation Loc, SILValue Operand,
1221-
SILDeclRef Member, SILType MethodTy) {
1222-
return insert(DynamicMethodInst::create(
1223-
getSILDebugLocation(Loc), Operand, Member, MethodTy,
1224-
&getFunction(), OpenedArchetypes));
1225-
}
1226-
12271221
OpenExistentialAddrInst *
12281222
createOpenExistentialAddr(SILLocation Loc, SILValue Operand, SILType SelfTy,
12291223
OpenedExistentialAccess ForAccess) {

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ SILCloner<ImplClass>::visitObjCMethodInst(ObjCMethodInst *Inst) {
15571557
getBuilder().createObjCMethod(getOpLocation(Inst->getLoc()),
15581558
getOpValue(Inst->getOperand()),
15591559
Inst->getMember(),
1560-
Inst->getType()));
1560+
getOpType(Inst->getType())));
15611561
}
15621562

15631563
template<typename ImplClass>
@@ -1601,17 +1601,6 @@ SILCloner<ImplClass>::visitWitnessMethodInst(WitnessMethodInst *Inst) {
16011601
Inst->isVolatile()));
16021602
}
16031603

1604-
template<typename ImplClass>
1605-
void
1606-
SILCloner<ImplClass>::visitDynamicMethodInst(DynamicMethodInst *Inst) {
1607-
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1608-
doPostProcess(Inst,
1609-
getBuilder().createDynamicMethod(getOpLocation(Inst->getLoc()),
1610-
getOpValue(Inst->getOperand()),
1611-
Inst->getMember(),
1612-
getOpType(Inst->getType())));
1613-
}
1614-
16151604
template<typename ImplClass>
16161605
void
16171606
SILCloner<ImplClass>::visitOpenExistentialAddrInst(OpenExistentialAddrInst *Inst) {

include/swift/SIL/SILInstruction.h

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4870,15 +4870,24 @@ class SuperMethodInst
48704870
/// ObjCMethodInst - Given the address of a value of class type and a method
48714871
/// constant, extracts the implementation of that method for the dynamic
48724872
/// instance type of the class.
4873-
class ObjCMethodInst
4874-
: public UnaryInstructionBase<SILInstructionKind::ObjCMethodInst,
4875-
MethodInst>
4873+
class ObjCMethodInst final
4874+
: public UnaryInstructionWithTypeDependentOperandsBase<
4875+
SILInstructionKind::ObjCMethodInst,
4876+
ObjCMethodInst,
4877+
MethodInst>
48764878
{
48774879
friend SILBuilder;
48784880

48794881
ObjCMethodInst(SILDebugLocation DebugLoc, SILValue Operand,
4882+
ArrayRef<SILValue> TypeDependentOperands,
48804883
SILDeclRef Member, SILType Ty)
4881-
: UnaryInstructionBase(DebugLoc, Operand, Ty, Member) {}
4884+
: UnaryInstructionWithTypeDependentOperandsBase(DebugLoc, Operand,
4885+
TypeDependentOperands, Ty, Member) {}
4886+
4887+
static ObjCMethodInst *
4888+
create(SILDebugLocation DebugLoc, SILValue Operand,
4889+
SILDeclRef Member, SILType Ty, SILFunction *F,
4890+
SILOpenedArchetypesState &OpenedArchetypes);
48824891
};
48834892

48844893
/// ObjCSuperMethodInst - Given the address of a value of class type and a method
@@ -4921,6 +4930,18 @@ class WitnessMethodInst final
49214930
TypeDependentOperands);
49224931
}
49234932

4933+
/// Create a witness method call of a protocol requirement, passing in a lookup
4934+
/// type and conformance.
4935+
///
4936+
/// At runtime, the witness is looked up in the conformance of the lookup type
4937+
/// to the protocol.
4938+
///
4939+
/// The lookup type is usually an archetype, but it will be concrete if the
4940+
/// witness_method instruction is inside a function body that was specialized.
4941+
///
4942+
/// The conformance must exactly match the requirement; the caller must handle
4943+
/// the case where the requirement is defined in a base protocol that is
4944+
/// refined by the conforming protocol.
49244945
static WitnessMethodInst *
49254946
create(SILDebugLocation DebugLoc, CanType LookupType,
49264947
ProtocolConformanceRef Conformance, SILDeclRef Member, SILType Ty,
@@ -4962,30 +4983,6 @@ class WitnessMethodInst final
49624983
}
49634984
};
49644985

4965-
/// Given the address of a value of AnyObject protocol type and a method
4966-
/// constant referring to some Objective-C method, performs dynamic method
4967-
/// lookup to extract the implementation of that method. This method lookup
4968-
/// can fail at run-time
4969-
class DynamicMethodInst final
4970-
: public UnaryInstructionWithTypeDependentOperandsBase<
4971-
SILInstructionKind::DynamicMethodInst,
4972-
DynamicMethodInst,
4973-
MethodInst>
4974-
{
4975-
friend SILBuilder;
4976-
4977-
DynamicMethodInst(SILDebugLocation DebugLoc, SILValue Operand,
4978-
ArrayRef<SILValue> TypeDependentOperands,
4979-
SILDeclRef Member, SILType Ty)
4980-
: UnaryInstructionWithTypeDependentOperandsBase(DebugLoc, Operand,
4981-
TypeDependentOperands, Ty, Member) {}
4982-
4983-
static DynamicMethodInst *
4984-
create(SILDebugLocation DebugLoc, SILValue Operand,
4985-
SILDeclRef Member, SILType Ty, SILFunction *F,
4986-
SILOpenedArchetypesState &OpenedArchetypes);
4987-
};
4988-
49894986
/// Access allowed to the opened value by the open_existential_addr instruction.
49904987
/// Allowing mutable access to the opened existential requires a boxed
49914988
/// existential value's box to be unique.

include/swift/SIL/SILNodes.def

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
255255
MethodInst, None, DoesNotRelease)
256256
SINGLE_VALUE_INST(WitnessMethodInst, witness_method,
257257
MethodInst, None, DoesNotRelease)
258-
SINGLE_VALUE_INST(DynamicMethodInst, dynamic_method,
259-
MethodInst, None, DoesNotRelease)
260-
SINGLE_VALUE_INST_RANGE(MethodInst, ClassMethodInst, DynamicMethodInst)
258+
SINGLE_VALUE_INST_RANGE(MethodInst, ClassMethodInst, WitnessMethodInst)
261259

262260
// Conversions
263261
ABSTRACT_SINGLE_VALUE_INST(ConversionInst, SingleValueInstruction)

lib/IRGen/IRGenSIL.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,6 @@ class IRGenSILFunction :
895895
void visitObjCMethodInst(ObjCMethodInst *i);
896896
void visitObjCSuperMethodInst(ObjCSuperMethodInst *i);
897897
void visitWitnessMethodInst(WitnessMethodInst *i);
898-
void visitDynamicMethodInst(DynamicMethodInst *i);
899898

900899
void visitAllocValueBufferInst(AllocValueBufferInst *i);
901900
void visitProjectValueBufferInst(ProjectValueBufferInst *i);
@@ -5001,12 +5000,6 @@ IRGenSILFunction::visitProjectExistentialBoxInst(ProjectExistentialBoxInst *i) {
50015000
}
50025001
}
50035002

5004-
void IRGenSILFunction::visitDynamicMethodInst(DynamicMethodInst *i) {
5005-
assert(i->getMember().isForeign && "dynamic_method requires [objc] method");
5006-
setLoweredObjCMethod(i, i->getMember());
5007-
return;
5008-
}
5009-
50105003
void IRGenSILFunction::visitWitnessMethodInst(swift::WitnessMethodInst *i) {
50115004
// For Objective-C classes we need to arrange for a msgSend
50125005
// to happen when the method is called.

lib/IRGen/LoadableByAddress.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ void LargeValueVisitor::mapValueStorage() {
478478
case SILInstructionKind::SuperMethodInst:
479479
case SILInstructionKind::ObjCMethodInst:
480480
case SILInstructionKind::ObjCSuperMethodInst:
481-
case SILInstructionKind::DynamicMethodInst:
482481
case SILInstructionKind::WitnessMethodInst: {
483482
// TODO Any more instructions to add here?
484483
auto *MI = dyn_cast<MethodInst>(currIns);
@@ -2037,14 +2036,6 @@ static void rewriteFunction(StructLoweringState &pass,
20372036
newSILType);
20382037
break;
20392038
}
2040-
case SILInstructionKind::DynamicMethodInst: {
2041-
auto *DMI = dyn_cast<DynamicMethodInst>(instr);
2042-
assert(DMI && "ValueKind is Witness Method but dyn_cast failed");
2043-
SILValue selfValue = instr->getOperand(0);
2044-
newInstr = methodBuilder.createDynamicMethod(loc, selfValue, member,
2045-
newSILType);
2046-
break;
2047-
}
20482039
case SILInstructionKind::WitnessMethodInst: {
20492040
auto *WMI = dyn_cast<WitnessMethodInst>(instr);
20502041
assert(!WMI->isVolatile());

lib/ParseSIL/ParseSIL.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,8 +3784,7 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
37843784
case SILInstructionKind::ClassMethodInst:
37853785
case SILInstructionKind::SuperMethodInst:
37863786
case SILInstructionKind::ObjCMethodInst:
3787-
case SILInstructionKind::ObjCSuperMethodInst:
3788-
case SILInstructionKind::DynamicMethodInst: {
3787+
case SILInstructionKind::ObjCSuperMethodInst: {
37893788
SILDeclRef Member;
37903789
SILType MethodTy;
37913790
SourceLoc TyLoc;
@@ -3816,9 +3815,6 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
38163815
case SILInstructionKind::ObjCSuperMethodInst:
38173816
ResultVal = B.createObjCSuperMethod(InstLoc, Val, Member, MethodTy);
38183817
break;
3819-
case SILInstructionKind::DynamicMethodInst:
3820-
ResultVal = B.createDynamicMethod(InstLoc, Val, Member, MethodTy);
3821-
break;
38223818
}
38233819
break;
38243820
}

lib/SIL/SILInstructions.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,18 +1592,6 @@ DynamicMethodBranchInst::create(SILDebugLocation Loc, SILValue Operand,
15921592
DynamicMethodBranchInst(Loc, Operand, Member, HasMethodBB, NoMethodBB);
15931593
}
15941594

1595-
/// Create a witness method call of a protocol requirement, passing in a lookup
1596-
/// type and conformance.
1597-
///
1598-
/// At runtime, the witness is looked up in the conformance of the lookup type
1599-
/// to the protocol.
1600-
///
1601-
/// The lookup type is usually an archetype, but it will be concrete if the
1602-
/// witness_method instruction is inside a function body that was specialized.
1603-
///
1604-
/// The conformance must exactly match the requirement; the caller must handle
1605-
/// the case where the requirement is defined in a base protocol that is
1606-
/// refined by the conforming protocol.
16071595
WitnessMethodInst *
16081596
WitnessMethodInst::create(SILDebugLocation Loc, CanType LookupType,
16091597
ProtocolConformanceRef Conformance, SILDeclRef Member,
@@ -1627,21 +1615,21 @@ WitnessMethodInst::create(SILDebugLocation Loc, CanType LookupType,
16271615
Ty, TypeDependentOperands, Volatile);
16281616
}
16291617

1630-
DynamicMethodInst *
1631-
DynamicMethodInst::create(SILDebugLocation DebugLoc, SILValue Operand,
1632-
SILDeclRef Member, SILType Ty, SILFunction *F,
1633-
SILOpenedArchetypesState &OpenedArchetypes) {
1618+
ObjCMethodInst *
1619+
ObjCMethodInst::create(SILDebugLocation DebugLoc, SILValue Operand,
1620+
SILDeclRef Member, SILType Ty, SILFunction *F,
1621+
SILOpenedArchetypesState &OpenedArchetypes) {
16341622
SILModule &Mod = F->getModule();
16351623
SmallVector<SILValue, 8> TypeDependentOperands;
16361624
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, *F,
16371625
Ty.getSwiftRValueType());
16381626

16391627
unsigned size =
16401628
totalSizeToAlloc<swift::Operand>(1 + TypeDependentOperands.size());
1641-
void *Buffer = Mod.allocateInst(size, alignof(DynamicMethodInst));
1642-
return ::new (Buffer) DynamicMethodInst(DebugLoc, Operand,
1643-
TypeDependentOperands,
1644-
Member, Ty);
1629+
void *Buffer = Mod.allocateInst(size, alignof(ObjCMethodInst));
1630+
return ::new (Buffer) ObjCMethodInst(DebugLoc, Operand,
1631+
TypeDependentOperands,
1632+
Member, Ty);
16451633
}
16461634

16471635
InitExistentialAddrInst *InitExistentialAddrInst::create(

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, SuperMethod)
609609
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, ObjCSuperMethod)
610610
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, BridgeObjectToWord)
611611
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, CopyBlock)
612-
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, DynamicMethod)
613612
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, OpenExistentialBox)
614613
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, RefTailAddr)
615614
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP(MustBeLive, RefToRawPointer)

lib/SIL/SILPrinter.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,12 +1631,6 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
16311631
}
16321632
*this << " : " << WMI->getType();
16331633
}
1634-
void visitDynamicMethodInst(DynamicMethodInst *DMI) {
1635-
printMethodInst(DMI, DMI->getOperand());
1636-
*this << " : " << DMI->getMember().getDecl()->getInterfaceType();
1637-
*this << ", ";
1638-
*this << DMI->getType();
1639-
}
16401634
void visitOpenExistentialAddrInst(OpenExistentialAddrInst *OI) {
16411635
if (OI->getAccessKind() == OpenedExistentialAccess::Immutable)
16421636
*this << "immutable_access ";

0 commit comments

Comments
 (0)