Skip to content

Commit 9666ea6

Browse files
committed
SILGenApply: Avoid passing an expression to emitDynamicMemberRefExpr
1 parent 7c226dd commit 9666ea6

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6137,19 +6137,18 @@ static ManagedValue emitDynamicPartialApply(SILGenFunction &SGF,
61376137
return result;
61386138
}
61396139

6140-
RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
6141-
SGFContext c) {
6142-
// Emit the operand.
6143-
ManagedValue base = emitRValueAsSingleValue(e->getBase());
6140+
RValue SILGenFunction::emitDynamicMemberRef(SILLocation loc, SILValue operand,
6141+
ConcreteDeclRef memberRef,
6142+
CanType refTy, SGFContext C) {
6143+
assert(refTy->isOptional());
61446144

6145-
SILValue operand = base.getValue();
6146-
if (!e->getMember().getDecl()->isInstanceMember()) {
6145+
if (!memberRef.getDecl()->isInstanceMember()) {
61476146
auto metatype = operand->getType().castTo<MetatypeType>();
61486147
assert(metatype->getRepresentation() == MetatypeRepresentation::Thick);
61496148
metatype = CanMetatypeType::get(metatype.getInstanceType(),
61506149
MetatypeRepresentation::ObjC);
6151-
operand = B.createThickToObjCMetatype(e, operand,
6152-
SILType::getPrimitiveObjectType(metatype));
6150+
operand = B.createThickToObjCMetatype(
6151+
loc, operand, SILType::getPrimitiveObjectType(metatype));
61536152
}
61546153

61556154
// Create the continuation block.
@@ -6162,38 +6161,39 @@ RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
61626161
SILBasicBlock *hasMemberBB = createBasicBlock();
61636162

61646163
// The continuation block
6165-
auto memberMethodTy = e->getType()->getOptionalObjectType();
6164+
CanType memberMethodTy = refTy.getOptionalObjectType();
61666165

6167-
const TypeLowering &optTL = getTypeLowering(e->getType());
6166+
const TypeLowering &optTL = getTypeLowering(refTy);
61686167
auto loweredOptTy = optTL.getLoweredType();
61696168

6170-
SILValue optTemp = emitTemporaryAllocation(e, loweredOptTy);
6169+
SILValue optTemp = emitTemporaryAllocation(loc, loweredOptTy);
61716170

61726171
// Create the branch.
61736172
FuncDecl *memberFunc;
6174-
if (auto *VD = dyn_cast<VarDecl>(e->getMember().getDecl())) {
6173+
if (auto *VD = dyn_cast<VarDecl>(memberRef.getDecl())) {
61756174
memberFunc = VD->getOpaqueAccessor(AccessorKind::Get);
61766175
// FIXME: Verify ExtInfo state is correct, not working by accident.
61776176
CanFunctionType::ExtInfo info;
6178-
memberMethodTy = FunctionType::get({}, memberMethodTy, info);
6179-
} else
6180-
memberFunc = cast<FuncDecl>(e->getMember().getDecl());
6177+
memberMethodTy = CanFunctionType::get({}, memberMethodTy, info);
6178+
} else {
6179+
memberFunc = cast<FuncDecl>(memberRef.getDecl());
6180+
}
61816181
auto member = SILDeclRef(memberFunc, SILDeclRef::Kind::Func)
61826182
.asForeign();
6183-
B.createDynamicMethodBranch(e, operand, member, hasMemberBB, noMemberBB);
6183+
B.createDynamicMethodBranch(loc, operand, member, hasMemberBB, noMemberBB);
61846184

61856185
// Create the has-member branch.
61866186
{
61876187
B.emitBlock(hasMemberBB);
61886188

6189-
FullExpr hasMemberScope(Cleanups, CleanupLocation(e));
6189+
FullExpr hasMemberScope(Cleanups, CleanupLocation(loc));
61906190

61916191
// The argument to the has-member block is the uncurried method.
6192-
auto valueTy = e->getType()->getCanonicalType().getOptionalObjectType();
6192+
const CanType valueTy = refTy.getOptionalObjectType();
61936193
CanFunctionType methodTy;
61946194

61956195
// For a computed variable, we want the getter.
6196-
if (isa<VarDecl>(e->getMember().getDecl())) {
6196+
if (isa<VarDecl>(memberRef.getDecl())) {
61976197
// FIXME: Verify ExtInfo state is correct, not working by accident.
61986198
CanFunctionType::ExtInfo info;
61996199
methodTy = CanFunctionType::get({}, valueTy, info);
@@ -6205,50 +6205,49 @@ RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
62056205
// TODO: instead of building this and then potentially converting, we
62066206
// should just build a single thunk.
62076207
auto foreignMethodTy =
6208-
getPartialApplyOfDynamicMethodFormalType(SGM, member, e->getMember());
6208+
getPartialApplyOfDynamicMethodFormalType(SGM, member, memberRef);
62096209

62106210
// FIXME: Verify ExtInfo state is correct, not working by accident.
62116211
CanFunctionType::ExtInfo info;
62126212
FunctionType::Param arg(operand->getType().getASTType());
6213-
auto memberFnTy =
6214-
CanFunctionType::get({arg}, memberMethodTy->getCanonicalType(), info);
6213+
auto memberFnTy = CanFunctionType::get({arg}, memberMethodTy, info);
62156214

62166215
auto loweredMethodTy = getDynamicMethodLoweredType(SGM.M, member,
62176216
memberFnTy);
62186217
SILValue memberArg =
62196218
hasMemberBB->createPhiArgument(loweredMethodTy, OwnershipKind::Owned);
62206219

62216220
// Create the result value.
6222-
Scope applyScope(Cleanups, CleanupLocation(e));
6223-
ManagedValue result =
6224-
emitDynamicPartialApply(*this, e, memberArg, operand,
6225-
foreignMethodTy, methodTy);
6221+
Scope applyScope(Cleanups, CleanupLocation(loc));
6222+
ManagedValue result = emitDynamicPartialApply(
6223+
*this, loc, memberArg, operand, foreignMethodTy, methodTy);
62266224

62276225
RValue resultRV;
6228-
if (isa<VarDecl>(e->getMember().getDecl())) {
6229-
resultRV = emitMonomorphicApply(e, result, {},
6230-
foreignMethodTy.getResult(), valueTy,
6231-
ApplyOptions(), None, None);
6226+
if (isa<VarDecl>(memberRef.getDecl())) {
6227+
resultRV =
6228+
emitMonomorphicApply(loc, result, {}, foreignMethodTy.getResult(),
6229+
valueTy, ApplyOptions(), None, None);
62326230
} else {
6233-
resultRV = RValue(*this, e, valueTy, result);
6231+
resultRV = RValue(*this, loc, valueTy, result);
62346232
}
62356233

62366234
// Package up the result in an optional.
6237-
emitInjectOptionalValueInto(e, {e, std::move(resultRV)}, optTemp, optTL);
6235+
emitInjectOptionalValueInto(loc, {loc, std::move(resultRV)}, optTemp,
6236+
optTL);
62386237

62396238
applyScope.pop();
62406239
// Branch to the continuation block.
6241-
B.createBranch(e, contBB);
6240+
B.createBranch(loc, contBB);
62426241
}
62436242

62446243
// Create the no-member branch.
62456244
{
62466245
B.emitBlock(noMemberBB);
62476246

6248-
emitInjectOptionalNothingInto(e, optTemp, optTL);
6247+
emitInjectOptionalNothingInto(loc, optTemp, optTL);
62496248

62506249
// Branch to the continuation block.
6251-
B.createBranch(e, contBB);
6250+
B.createBranch(loc, contBB);
62526251
}
62536252

62546253
// Emit the continuation block.
@@ -6257,8 +6256,9 @@ RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
62576256
// Package up the result.
62586257
auto optResult = optTemp;
62596258
if (optTL.isLoadable())
6260-
optResult = optTL.emitLoad(B, e, optResult, LoadOwnershipQualifier::Take);
6261-
return RValue(*this, e, emitManagedRValueWithCleanup(optResult, optTL));
6259+
optResult = optTL.emitLoad(B, loc, optResult, LoadOwnershipQualifier::Take);
6260+
return RValue(*this, loc, refTy,
6261+
emitManagedRValueWithCleanup(optResult, optTL));
62626262
}
62636263

62646264
RValue SILGenFunction::emitDynamicSubscriptExpr(DynamicSubscriptExpr *e,

lib/SILGen/SILGenExpr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,13 @@ RValue RValueEmitter::visitDynamicMemberRefExpr(DynamicMemberRefExpr *E,
22192219
SGFContext C) {
22202220
assert(!E->isImplicitlyAsync() && "an actor-isolated @objc member?");
22212221
assert(!E->isImplicitlyThrows() && "an distributed-actor-isolated @objc member?");
2222-
return SGF.emitDynamicMemberRefExpr(E, C);
2222+
2223+
// Emit the operand (the base).
2224+
SILValue Operand = SGF.emitRValueAsSingleValue(E->getBase()).getValue();
2225+
2226+
// Emit the member reference.
2227+
return SGF.emitDynamicMemberRef(E, Operand, E->getMember(),
2228+
E->getType()->getCanonicalType(), C);
22232229
}
22242230

22252231
RValue RValueEmitter::

lib/SILGen/SILGenFunction.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
17511751
bool isSuppressed);
17521752

17531753
/// Emit a dynamic member reference.
1754-
RValue emitDynamicMemberRefExpr(DynamicMemberRefExpr *e, SGFContext c);
1754+
RValue emitDynamicMemberRef(SILLocation loc, SILValue operand,
1755+
ConcreteDeclRef memberRef, CanType refTy,
1756+
SGFContext C);
17551757

17561758
/// Emit a dynamic subscript.
17571759
RValue emitDynamicSubscriptExpr(DynamicSubscriptExpr *e, SGFContext c);

0 commit comments

Comments
 (0)