Skip to content

Commit eceb61a

Browse files
authored
[silgen] Try to re-abstract first before going through other code paths in visitRecNonInOutBase. NFC. (#11770)
The main thing here is that we have a large block of code that we will never hit if we need to re-abstract. Instead, this commit reflows visitRecNonInOutBase to first check if re-abstraction is needed before doing anything else. If re-abstration is required, we quickly handle it and return. This commit also fixes some variable names to match the rest of the function. rdar://31521023
1 parent 4e50e4e commit eceb61a

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,38 +2045,48 @@ static ManagedValue visitRecNonInOutBase(SILGenLValue &SGL, Expr *e,
20452045
AccessKind accessKind,
20462046
LValueOptions options,
20472047
AbstractionPattern orig) {
2048-
SGFContext ctx;
20492048
auto &SGF = SGL.SGF;
20502049

2051-
if (auto *DRE = dyn_cast<DeclRefExpr>(e)) {
2050+
// For an rvalue base, apply the reabstraction (if any) eagerly, since
2051+
// there's no need for writeback.
2052+
if (orig.isValid()) {
2053+
return SGF.emitRValueAsOrig(
2054+
e, orig, SGF.getTypeLowering(orig, e->getType()->getRValueType()));
2055+
}
2056+
2057+
// Ok, at this point we know that re-abstraction is not required.
2058+
SGFContext ctx;
2059+
2060+
if (auto *dre = dyn_cast<DeclRefExpr>(e)) {
20522061
// Any reference to "self" can be done at +0 so long as it is a direct
20532062
// access, since we know it is guaranteed.
20542063
//
20552064
// TODO: it would be great to factor this even lower into SILGen to the
20562065
// point where we can see that the parameter is +0 guaranteed. Note that
20572066
// this handles the case in initializers where there is actually a stack
20582067
// allocation for it as well.
2059-
if (isa<ParamDecl>(DRE->getDecl()) &&
2060-
DRE->getDecl()->getFullName() == SGF.getASTContext().Id_self &&
2061-
DRE->getDecl()->isImplicit()) {
2068+
if (isa<ParamDecl>(dre->getDecl()) &&
2069+
dre->getDecl()->getFullName() == SGF.getASTContext().Id_self &&
2070+
dre->getDecl()->isImplicit()) {
20622071
ctx = SGFContext::AllowGuaranteedPlusZero;
20632072
if (SGF.SelfInitDelegationState != SILGenFunction::NormalSelf) {
20642073
// This needs to be inlined since there is a Formal Evaluation Scope
20652074
// in emitRValueForDecl that causing any borrow for this LValue to be
20662075
// popped too soon.
2067-
auto *vd = cast<ParamDecl>(DRE->getDecl());
2076+
auto *vd = cast<ParamDecl>(dre->getDecl());
20682077
ManagedValue selfLValue =
2069-
SGF.emitLValueForDecl(DRE, vd, DRE->getType()->getCanonicalType(),
2070-
AccessKind::Read, DRE->getAccessSemantics());
2071-
return SGF
2072-
.emitFormalEvaluationRValueForSelfInDelegationInit(
2073-
e, DRE->getType()->getCanonicalType(),
2074-
selfLValue.getLValueAddress(), ctx)
2075-
.getAsSingleValue(SGF, e);
2078+
SGF.emitLValueForDecl(dre, vd, dre->getType()->getCanonicalType(),
2079+
AccessKind::Read, dre->getAccessSemantics());
2080+
selfLValue = SGF.emitFormalEvaluationRValueForSelfInDelegationInit(
2081+
e, dre->getType()->getCanonicalType(),
2082+
selfLValue.getLValueAddress(), ctx)
2083+
.getAsSingleValue(SGF, e);
2084+
2085+
return selfLValue;
20762086
}
20772087
}
20782088

2079-
if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
2089+
if (auto *VD = dyn_cast<VarDecl>(dre->getDecl())) {
20802090
// All let values are guaranteed to be held alive across their lifetime,
20812091
// and won't change once initialized. Any loaded value is good for the
20822092
// duration of this expression evaluation.
@@ -2090,14 +2100,6 @@ static ManagedValue visitRecNonInOutBase(SILGenLValue &SGL, Expr *e,
20902100
ctx = SGFContext::AllowGuaranteedPlusZero;
20912101
}
20922102

2093-
// For an rvalue base, apply the reabstraction (if any) eagerly, since
2094-
// there's no need for writeback.
2095-
if (orig.isValid()) {
2096-
return SGF.emitRValueAsOrig(
2097-
e, orig, SGF.getTypeLowering(orig, e->getType()->getRValueType()));
2098-
}
2099-
2100-
// Otherwise, just go through normal emission.
21012103
return SGF.emitRValueAsSingleValue(e, ctx);
21022104
}
21032105

0 commit comments

Comments
 (0)