Skip to content

Commit c7d3e8f

Browse files
committed
[NFC] Generalize how Conversion works with reabstraction conversions and
force callers to specify the input lowered type as well.
1 parent 44d7d06 commit c7d3e8f

11 files changed

+318
-125
lines changed

lib/SILGen/ArgumentSource.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &SGF,
8181
SILType loweredTy,
8282
SGFContext C) && {
8383
auto substFormalType = getSubstRValueType();
84+
auto loweredFormalTy = SGF.getLoweredType(substFormalType);
8485
auto conversion =
85-
Conversion::getSubstToOrig(origFormalType, substFormalType, loweredTy);
86+
Conversion::getSubstToOrig(origFormalType, substFormalType,
87+
loweredFormalTy, loweredTy);
8688
return std::move(*this).getConverted(SGF, conversion, C);
8789
}
8890

lib/SILGen/Conversion.h

+56-41
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ class Conversion {
5252
/// A subtype conversion.
5353
Subtype,
5454

55-
/// An orig-to-subst conversion.
56-
OrigToSubst,
57-
58-
/// A subst-to-orig conversion. These can always be annihilated.
59-
SubstToOrig,
55+
/// A reabstraction conversion. There can also be a subtype difference
56+
/// between the substituted types.
57+
Reabstract,
6058
};
6159

6260
static bool isBridgingKind(KindTy kind) {
@@ -69,17 +67,15 @@ class Conversion {
6967
case Subtype:
7068
return true;
7169

72-
case OrigToSubst:
73-
case SubstToOrig:
70+
case Reabstract:
7471
return false;
7572
}
7673
llvm_unreachable("bad kind");
7774
}
7875

7976
static bool isReabstractionKind(KindTy kind) {
8077
switch (kind) {
81-
case OrigToSubst:
82-
case SubstToOrig:
78+
case Reabstract:
8379
return true;
8480

8581
case BridgeToObjC:
@@ -104,12 +100,12 @@ class Conversion {
104100
};
105101

106102
struct ReabstractionTypes {
107-
// Whether this abstraction pattern applies to the input or output
108-
// substituted type is determined by the kind.
109-
AbstractionPattern OrigType;
110-
CanType SubstSourceType;
111-
CanType SubstResultType;
112-
SILType LoweredResultType;
103+
AbstractionPattern InputOrigType;
104+
AbstractionPattern OutputOrigType;
105+
CanType InputSubstType;
106+
CanType OutputSubstType;
107+
SILType InputLoweredTy;
108+
SILType OutputLoweredTy;
113109
};
114110

115111
using Members = ExternalUnionMembers<BridgingTypes, ReabstractionTypes>;
@@ -124,8 +120,7 @@ class Conversion {
124120
case Subtype:
125121
return Members::indexOf<BridgingTypes>();
126122

127-
case OrigToSubst:
128-
case SubstToOrig:
123+
case Reabstract:
129124
return Members::indexOf<ReabstractionTypes>();
130125
}
131126
llvm_unreachable("bad kind");
@@ -142,32 +137,41 @@ class Conversion {
142137
loweredResultTy, isExplicit);
143138
}
144139

145-
Conversion(KindTy kind, AbstractionPattern origType, CanType substSourceType,
146-
CanType substResultType, SILType loweredResultTy)
147-
: Kind(kind) {
148-
Types.emplaceAggregate<ReabstractionTypes>(kind, origType, substSourceType,
149-
substResultType, loweredResultTy);
140+
Conversion(AbstractionPattern inputOrigType, CanType inputSubstType,
141+
SILType inputLoweredTy,
142+
AbstractionPattern outputOrigType, CanType outputSubstType,
143+
SILType outputLoweredTy)
144+
: Kind(Reabstract) {
145+
Types.emplaceAggregate<ReabstractionTypes>(Kind, inputOrigType, outputOrigType,
146+
inputSubstType, outputSubstType,
147+
inputLoweredTy, outputLoweredTy);
150148
}
151149

152150
public:
153151
static Conversion getOrigToSubst(AbstractionPattern origType,
154152
CanType substType,
155-
SILType loweredResultTy) {
156-
return Conversion(OrigToSubst, origType, substType, substType, loweredResultTy);
153+
SILType inputLoweredTy,
154+
SILType outputLoweredTy) {
155+
return getReabstract(origType, substType, inputLoweredTy,
156+
AbstractionPattern(substType), substType, outputLoweredTy);
157157
}
158158

159159
static Conversion getSubstToOrig(AbstractionPattern origType,
160160
CanType substType,
161-
SILType loweredResultTy) {
162-
return Conversion(SubstToOrig, origType, substType, substType, loweredResultTy);
161+
SILType inputLoweredTy,
162+
SILType outputLoweredTy) {
163+
return getReabstract(AbstractionPattern(substType), substType, inputLoweredTy,
164+
origType, substType, outputLoweredTy);
163165
}
164166

165-
static Conversion getSubstToOrig(CanType inputSubstType,
166-
AbstractionPattern outputOrigType,
167-
CanType outputSubstType,
168-
SILType loweredResultTy) {
169-
return Conversion(SubstToOrig, outputOrigType, inputSubstType,
170-
outputSubstType, loweredResultTy);
167+
static Conversion getReabstract(AbstractionPattern inputOrigType,
168+
CanType inputSubstType,
169+
SILType inputLoweredTy,
170+
AbstractionPattern outputOrigType,
171+
CanType outputSubstType,
172+
SILType outputLoweredTy) {
173+
return Conversion(inputOrigType, inputSubstType, inputLoweredTy,
174+
outputOrigType, outputSubstType, outputLoweredTy);
171175
}
172176

173177
static Conversion getBridging(KindTy kind, CanType origType,
@@ -194,20 +198,28 @@ class Conversion {
194198
return isReabstractionKind(getKind());
195199
}
196200

197-
AbstractionPattern getReabstractionOrigType() const {
198-
return Types.get<ReabstractionTypes>(Kind).OrigType;
201+
AbstractionPattern getReabstractionInputOrigType() const {
202+
return Types.get<ReabstractionTypes>(Kind).InputOrigType;
203+
}
204+
205+
CanType getReabstractionInputSubstType() const {
206+
return Types.get<ReabstractionTypes>(Kind).InputSubstType;
207+
}
208+
209+
SILType getReabstractionInputLoweredType() const {
210+
return Types.get<ReabstractionTypes>(Kind).InputLoweredTy;
199211
}
200212

201-
CanType getReabstractionSubstSourceType() const {
202-
return Types.get<ReabstractionTypes>(Kind).SubstSourceType;
213+
AbstractionPattern getReabstractionOutputOrigType() const {
214+
return Types.get<ReabstractionTypes>(Kind).OutputOrigType;
203215
}
204216

205-
CanType getReabstractionSubstResultType() const {
206-
return Types.get<ReabstractionTypes>(Kind).SubstResultType;
217+
CanType getReabstractionOutputSubstType() const {
218+
return Types.get<ReabstractionTypes>(Kind).OutputSubstType;
207219
}
208220

209-
SILType getReabstractionLoweredResultType() const {
210-
return Types.get<ReabstractionTypes>(Kind).LoweredResultType;
221+
SILType getReabstractionOutputLoweredType() const {
222+
return Types.get<ReabstractionTypes>(Kind).OutputLoweredTy;
211223
}
212224

213225
bool isBridgingExplicit() const {
@@ -259,7 +271,10 @@ class ConversionPeepholeHint {
259271

260272
/// The inner conversion is a subtype conversion and can be done implicitly
261273
/// as part of the outer conversion.
262-
SubtypeIntoSubstToOrig,
274+
SubtypeIntoReabstract,
275+
276+
/// Both conversions are reabstractions and can be combined.
277+
Reabstract,
263278
};
264279

265280
private:

lib/SILGen/RValue.h

+5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ class RValue {
234234
/// The values must not require any cleanups.
235235
SILValue getUnmanagedSingleValue(SILGenFunction &SGF, SILLocation l) const &;
236236

237+
SILType getTypeOfSingleValue() const & {
238+
assert(isComplete() && values.size() == 1);
239+
return values[0].getType();
240+
}
241+
237242
ManagedValue getScalarValue() && {
238243
if (isInContext()) {
239244
makeUsed();

lib/SILGen/ResultPlan.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class ScalarResultPlan final : public ResultPlan {
306306
loweredResultTy);
307307
} else {
308308
return Conversion::getOrigToSubst(origType, substType,
309-
loweredResultTy);
309+
value.getType(), loweredResultTy);
310310
}
311311
}();
312312

lib/SILGen/SILGenApply.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -3826,6 +3826,7 @@ class ArgEmitter {
38263826
case SILFunctionLanguage::Swift:
38273827
return Conversion::getSubstToOrig(origParamType,
38283828
arg.getSubstRValueType(),
3829+
loweredSubstArgType,
38293830
param.getSILStorageInterfaceType());
38303831
case SILFunctionLanguage::C:
38313832
return Conversion::getBridging(Conversion::BridgeToObjC,
@@ -4051,7 +4052,9 @@ class ArgEmitter {
40514052
convertingInit.emplace(
40524053
Conversion::getSubstToOrig(
40534054
origExpansionType.getPackExpansionPatternType(),
4054-
substPatternType, expectedElementType),
4055+
substPatternType,
4056+
SILType::getPrimitiveObjectType(loweredPatternTy),
4057+
expectedElementType),
40554058
SGFContext(innermostInit));
40564059
innermostInit = &*convertingInit;
40574060
}
@@ -7273,6 +7276,7 @@ ManagedValue SILGenFunction::emitAsyncLetStart(
72737276
origParamType);
72747277

72757278
auto conversion = Conversion::getSubstToOrig(origParam, substParamType,
7279+
getLoweredType(asyncLetEntryPoint->getType()),
72767280
getLoweredType(origParam, substParamType));
72777281
auto taskFunction = emitConvertedRValue(asyncLetEntryPoint, conversion);
72787282

lib/SILGen/SILGenBuiltin.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,18 @@ static ManagedValue emitCreateAsyncTask(SILGenFunction &SGF, SILLocation loc,
16371637
CanType substParamType = fnArg.getSubstRValueType();
16381638
auto loweredParamTy = SGF.getLoweredType(origParamType, substParamType);
16391639

1640+
// The main actor path doesn't give us a value that actually matches the
1641+
// formal type at all, so this is the best we can do.
1642+
SILType loweredSubstParamTy;
1643+
if (fnArg.isRValue()) {
1644+
loweredSubstParamTy = fnArg.peekRValue().getTypeOfSingleValue();
1645+
} else {
1646+
loweredSubstParamTy = SGF.getLoweredType(substParamType);
1647+
}
1648+
16401649
auto conversion =
1641-
Conversion::getSubstToOrig(origParamType, substParamType, loweredParamTy);
1650+
Conversion::getSubstToOrig(origParamType, substParamType,
1651+
loweredSubstParamTy, loweredParamTy);
16421652
return std::move(fnArg).getConverted(SGF, conversion);
16431653
}();
16441654

lib/SILGen/SILGenConstructor.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,8 @@ void SILGenFunction::emitMemberInitializer(DeclContext *dc, VarDecl *selfDecl,
16351635

16361636
if (needsConvertingInit) {
16371637
Conversion conversion =
1638-
Conversion::getSubstToOrig(origType, substType, loweredResultTy);
1638+
Conversion::getSubstToOrig(origType, substType,
1639+
loweredSubstTy, loweredResultTy);
16391640

16401641
ConvertingInitialization convertingInit(conversion,
16411642
SGFContext(memberInit.get()));

0 commit comments

Comments
 (0)