Skip to content

Commit ea85726

Browse files
authored
Merge pull request #23082 from slavapestov/sil-lowered-rvalue-type
Introduce TypeConverter::getLoweredRValueType() and use it in various places
2 parents 1ea700a + efa0b36 commit ea85726

File tree

12 files changed

+94
-80
lines changed

12 files changed

+94
-80
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,14 @@ class TypeConverter {
792792
return ti.getLoweredType();
793793
}
794794

795+
CanType getLoweredRValueType(Type t) {
796+
return getLoweredType(t).getASTType();
797+
}
798+
799+
CanType getLoweredRValueType(AbstractionPattern origType, Type substType) {
800+
return getLoweredType(origType, substType).getASTType();
801+
}
802+
795803
AbstractionPattern getAbstractionPattern(AbstractStorageDecl *storage,
796804
bool isNonObjC = false);
797805
AbstractionPattern getAbstractionPattern(VarDecl *var,
@@ -800,7 +808,7 @@ class TypeConverter {
800808
bool isNonObjC = false);
801809
AbstractionPattern getAbstractionPattern(EnumElementDecl *element);
802810

803-
SILType getLoweredTypeOfGlobal(VarDecl *var);
811+
CanType getLoweredTypeOfGlobal(VarDecl *var);
804812

805813
/// Return the SILFunctionType for a native function value of the
806814
/// given type.
@@ -1001,7 +1009,8 @@ class TypeConverter {
10011009
EnumElementDecl *elt);
10021010

10031011
private:
1004-
CanType getLoweredRValueType(AbstractionPattern origType, CanType substType);
1012+
CanType computeLoweredRValueType(AbstractionPattern origType,
1013+
CanType substType);
10051014

10061015
Type getLoweredCBridgedType(AbstractionPattern pattern, Type t,
10071016
Bridgeability bridging,

lib/RemoteAST/RemoteAST.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class RemoteASTContextImpl {
224224
getOffsetOfFieldFromIRGen(irgen::IRGenModule &IGM, Type type,
225225
NominalTypeDecl *typeDecl,
226226
RemoteAddress optMetadata, VarDecl *member) {
227-
SILType loweredTy = IGM.getSILTypes().getLoweredType(type);
227+
SILType loweredTy = IGM.getLoweredType(type);
228228

229229
MemberAccessStrategy strategy =
230230
(isa<StructDecl>(typeDecl)
@@ -334,7 +334,7 @@ class RemoteASTContextImpl {
334334
if (!irgen) return Result<uint64_t>::emplaceFailure(Failure::Unknown);
335335
auto &IGM = irgen->IGM;
336336

337-
SILType loweredTy = IGM.getSILTypes().getLoweredType(type);
337+
SILType loweredTy = IGM.getLoweredType(type);
338338

339339
// If the type has a statically fixed offset, return that.
340340
if (auto offset =

lib/SIL/Bridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ using namespace swift;
2929
using namespace swift::Lowering;
3030

3131

32-
SILType TypeConverter::getLoweredTypeOfGlobal(VarDecl *var) {
32+
CanType TypeConverter::getLoweredTypeOfGlobal(VarDecl *var) {
3333
AbstractionPattern origType = getAbstractionPattern(var);
3434
assert(!origType.isTypeParameter());
35-
return getLoweredType(origType, origType.getType()).getObjectType();
35+
return getLoweredRValueType(origType, origType.getType());
3636
}
3737

3838
AnyFunctionType::Param

lib/SIL/SILFunctionType.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,8 @@ static void destructureYieldsForCoroutine(SILModule &M,
857857

858858
// 'modify' yields an inout of the target type.
859859
if (accessor->getAccessorKind() == AccessorKind::Modify) {
860-
auto loweredValueTy = M.Types.getLoweredType(origType, canValueType);
861-
yields.push_back(SILYieldInfo(loweredValueTy.getASTType(),
860+
auto loweredValueTy = M.Types.getLoweredRValueType(origType, canValueType);
861+
yields.push_back(SILYieldInfo(loweredValueTy,
862862
ParameterConvention::Indirect_Inout));
863863
return;
864864
}
@@ -2411,8 +2411,7 @@ class SILTypeSubstituter :
24112411
}
24122412

24132413
AbstractionPattern abstraction(Sig, origType);
2414-
return TheSILModule.Types.getLoweredType(abstraction, substType)
2415-
.getASTType();
2414+
return TheSILModule.Types.getLoweredRValueType(abstraction, substType);
24162415
}
24172416
};
24182417

lib/SIL/SILType.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ SILType SILType::getFieldType(VarDecl *field, SILModule &M) const {
138138
baseTy->getTypeOfMember(M.getSwiftModule(),
139139
field, nullptr)->getCanonicalType();
140140
}
141-
auto loweredTy = M.Types.getLoweredType(origFieldTy, substFieldTy);
141+
142+
auto loweredTy = M.Types.getLoweredRValueType(origFieldTy, substFieldTy);
142143
if (isAddress() || getClassOrBoundGenericClass() != nullptr) {
143-
return loweredTy.getAddressType();
144+
return SILType::getPrimitiveAddressType(loweredTy);
144145
} else {
145-
return loweredTy.getObjectType();
146+
return SILType::getPrimitiveObjectType(loweredTy);
146147
}
147148
}
148149

@@ -166,9 +167,10 @@ SILType SILType::getEnumElementType(EnumElementDecl *elt, SILModule &M) const {
166167
getASTType()->getTypeOfMember(M.getSwiftModule(), elt,
167168
elt->getArgumentInterfaceType());
168169
auto loweredTy =
169-
M.Types.getLoweredType(M.Types.getAbstractionPattern(elt), substEltTy);
170+
M.Types.getLoweredRValueType(M.Types.getAbstractionPattern(elt),
171+
substEltTy);
170172

171-
return SILType(loweredTy.getASTType(), getCategory());
173+
return SILType(loweredTy, getCategory());
172174
}
173175

174176
bool SILType::isLoadableOrOpaque(SILModule &M) const {

lib/SIL/TypeLowering.cpp

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,9 +1343,9 @@ void TypeConverter::insert(TypeKey k, const TypeLowering *tl) {
13431343

13441344
/// Lower each of the elements of the substituted type according to
13451345
/// the abstraction pattern of the given original type.
1346-
static CanTupleType getLoweredTupleType(TypeConverter &tc,
1347-
AbstractionPattern origType,
1348-
CanTupleType substType) {
1346+
static CanTupleType computeLoweredTupleType(TypeConverter &tc,
1347+
AbstractionPattern origType,
1348+
CanTupleType substType) {
13491349
assert(origType.matchesTuple(substType));
13501350

13511351
// Does the lowered tuple type differ from the substituted type in
@@ -1365,9 +1365,8 @@ static CanTupleType getLoweredTupleType(TypeConverter &tc,
13651365
assert(Flags.getValueOwnership() == ValueOwnership::Default);
13661366
assert(!Flags.isVariadic());
13671367

1368-
SILType silType = tc.getLoweredType(origEltType, substEltType);
1369-
CanType loweredSubstEltType = silType.getASTType();
1370-
1368+
CanType loweredSubstEltType =
1369+
tc.getLoweredRValueType(origEltType, substEltType);
13711370
changed = (changed || substEltType != loweredSubstEltType ||
13721371
!Flags.isNone());
13731372

@@ -1388,15 +1387,15 @@ static CanTupleType getLoweredTupleType(TypeConverter &tc,
13881387
return cast<TupleType>(CanType(TupleType::get(loweredElts, tc.Context)));
13891388
}
13901389

1391-
static CanType getLoweredOptionalType(TypeConverter &tc,
1392-
AbstractionPattern origType,
1393-
CanType substType,
1394-
CanType substObjectType) {
1390+
static CanType computeLoweredOptionalType(TypeConverter &tc,
1391+
AbstractionPattern origType,
1392+
CanType substType,
1393+
CanType substObjectType) {
13951394
assert(substType.getOptionalObjectType() == substObjectType);
13961395

13971396
CanType loweredObjectType =
1398-
tc.getLoweredType(origType.getOptionalObjectType(), substObjectType)
1399-
.getASTType();
1397+
tc.getLoweredRValueType(origType.getOptionalObjectType(),
1398+
substObjectType);
14001399

14011400
// If the object type didn't change, we don't have to rebuild anything.
14021401
if (loweredObjectType == substObjectType) {
@@ -1407,13 +1406,13 @@ static CanType getLoweredOptionalType(TypeConverter &tc,
14071406
return CanType(BoundGenericEnumType::get(optDecl, Type(), loweredObjectType));
14081407
}
14091408

1410-
static CanType getLoweredReferenceStorageType(TypeConverter &tc,
1411-
AbstractionPattern origType,
1412-
CanReferenceStorageType substType) {
1409+
static CanType
1410+
computeLoweredReferenceStorageType(TypeConverter &tc,
1411+
AbstractionPattern origType,
1412+
CanReferenceStorageType substType) {
14131413
CanType loweredReferentType =
1414-
tc.getLoweredType(origType.getReferenceStorageReferentType(),
1415-
substType.getReferentType())
1416-
.getASTType();
1414+
tc.getLoweredRValueType(origType.getReferenceStorageReferentType(),
1415+
substType.getReferentType());
14171416

14181417
if (loweredReferentType == substType.getReferentType())
14191418
return substType;
@@ -1425,8 +1424,8 @@ static CanType getLoweredReferenceStorageType(TypeConverter &tc,
14251424
CanSILFunctionType
14261425
TypeConverter::getSILFunctionType(AbstractionPattern origType,
14271426
CanFunctionType substType) {
1428-
return getLoweredType(origType, substType)
1429-
.castTo<SILFunctionType>();
1427+
return cast<SILFunctionType>(
1428+
getLoweredRValueType(origType, substType));
14301429
}
14311430

14321431
const TypeLowering &
@@ -1445,7 +1444,7 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
14451444

14461445
// Lower the type.
14471446
CanType loweredSubstType =
1448-
getLoweredRValueType(origType, substType);
1447+
computeLoweredRValueType(origType, substType);
14491448

14501449
// If that didn't change the type and the key is cacheable, there's no
14511450
// point in re-checking the table, so just construct a type lowering
@@ -1467,8 +1466,8 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
14671466
return lowering;
14681467
}
14691468

1470-
CanType TypeConverter::getLoweredRValueType(AbstractionPattern origType,
1471-
CanType substType) {
1469+
CanType TypeConverter::computeLoweredRValueType(AbstractionPattern origType,
1470+
CanType substType) {
14721471
assert(!substType->hasError() &&
14731472
"Error types should not appear in type-checked AST");
14741473

@@ -1559,17 +1558,18 @@ CanType TypeConverter::getLoweredRValueType(AbstractionPattern origType,
15591558

15601559
// Lower tuple element types.
15611560
if (auto substTupleType = dyn_cast<TupleType>(substType)) {
1562-
return getLoweredTupleType(*this, origType, substTupleType);
1561+
return computeLoweredTupleType(*this, origType, substTupleType);
15631562
}
15641563

15651564
// Lower the referent type of reference storage types.
15661565
if (auto substRefType = dyn_cast<ReferenceStorageType>(substType)) {
1567-
return getLoweredReferenceStorageType(*this, origType, substRefType);
1566+
return computeLoweredReferenceStorageType(*this, origType, substRefType);
15681567
}
15691568

15701569
// Lower the object type of optional types.
15711570
if (auto substObjectType = substType.getOptionalObjectType()) {
1572-
return getLoweredOptionalType(*this, origType, substType, substObjectType);
1571+
return computeLoweredOptionalType(*this, origType,
1572+
substType, substObjectType);
15731573
}
15741574

15751575
// The Swift type directly corresponds to the lowered type.
@@ -1961,8 +1961,7 @@ SILType TypeConverter::getSubstitutedStorageType(AbstractStorageDecl *value,
19611961
substType = substType.getReferenceStorageReferent();
19621962
}
19631963

1964-
SILType silSubstType = getLoweredType(origType, substType).getAddressType();
1965-
substType = silSubstType.getASTType();
1964+
CanType substLoweredType = getLoweredRValueType(origType, substType);
19661965

19671966
// Type substitution preserves structural type structure, and the
19681967
// type-of-reference is only different in the outermost structural
@@ -1972,13 +1971,11 @@ SILType TypeConverter::getSubstitutedStorageType(AbstractStorageDecl *value,
19721971
// The only really significant manipulation there is with @weak and
19731972
// @unowned.
19741973
if (origRefType) {
1975-
substType = CanType(ReferenceStorageType::get(substType,
1976-
origRefType->getOwnership(),
1977-
Context));
1978-
return SILType::getPrimitiveType(substType, SILValueCategory::Address);
1974+
substLoweredType = CanReferenceStorageType::get(substType,
1975+
origRefType->getOwnership());
19791976
}
19801977

1981-
return silSubstType;
1978+
return SILType::getPrimitiveAddressType(substLoweredType);
19821979
}
19831980

19841981
void TypeConverter::pushGenericContext(CanGenericSignature sig) {
@@ -2490,7 +2487,8 @@ CanSILBoxType TypeConverter::getBoxTypeForEnumElement(SILType enumType,
24902487

24912488
if (boxSignature == CanGenericSignature()) {
24922489
auto eltIntfTy = elt->getArgumentInterfaceType();
2493-
auto boxVarTy = getLoweredType(eltIntfTy).getASTType();
2490+
2491+
auto boxVarTy = getLoweredRValueType(eltIntfTy);
24942492
auto layout = SILLayout::get(C, nullptr, SILField(boxVarTy, true));
24952493
return SILBoxType::get(C, layout, {});
24962494
}
@@ -2501,8 +2499,9 @@ CanSILBoxType TypeConverter::getBoxTypeForEnumElement(SILType enumType,
25012499
// Lower the enum element's argument in the box's context.
25022500
auto eltIntfTy = elt->getArgumentInterfaceType();
25032501
GenericContextScope scope(*this, boxSignature);
2504-
auto boxVarTy = getLoweredType(getAbstractionPattern(elt), eltIntfTy)
2505-
.getASTType();
2502+
2503+
auto boxVarTy = getLoweredRValueType(getAbstractionPattern(elt),
2504+
eltIntfTy);
25062505
auto layout = SILLayout::get(C, boxSignature, SILField(boxVarTy, true));
25072506

25082507
// Instantiate the layout with enum's substitution list.

lib/SILGen/LValue.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,20 @@ struct LValueTypeData {
5151
CanType SubstFormalType;
5252

5353
/// The lowered type of value that should be stored in the l-value.
54-
/// Always an object type.
5554
///
5655
/// On physical path components, projection yields an address of
5756
/// this type. On logical path components, materialize yields an
5857
/// address of this type, set expects a value of this type, and
59-
/// get yields a value of this type.
60-
SILType TypeOfRValue;
58+
/// get yields an object of this type.
59+
CanType TypeOfRValue;
6160

6261
SGFAccessKind AccessKind;
6362

6463
LValueTypeData() = default;
6564
LValueTypeData(SGFAccessKind accessKind, AbstractionPattern origFormalType,
66-
CanType substFormalType, SILType typeOfRValue)
65+
CanType substFormalType, CanType typeOfRValue)
6766
: OrigFormalType(origFormalType), SubstFormalType(substFormalType),
6867
TypeOfRValue(typeOfRValue), AccessKind(accessKind) {
69-
assert(typeOfRValue.isObject());
7068
assert(substFormalType->isMaterializable());
7169
}
7270

@@ -185,7 +183,9 @@ class PathComponent {
185183

186184
/// Returns the logical type-as-rvalue of the value addressed by the
187185
/// component. This is always an object type, never an address.
188-
SILType getTypeOfRValue() const { return TypeData.TypeOfRValue; }
186+
SILType getTypeOfRValue() const {
187+
return SILType::getPrimitiveObjectType(TypeData.TypeOfRValue);
188+
}
189189
AbstractionPattern getOrigFormalType() const {
190190
return TypeData.OrigFormalType;
191191
}
@@ -485,7 +485,9 @@ class LValue {
485485
/// Returns the type-of-rvalue of the logical object referenced by
486486
/// this l-value. Note that this may differ significantly from the
487487
/// type of l-value.
488-
SILType getTypeOfRValue() const { return getTypeData().TypeOfRValue; }
488+
SILType getTypeOfRValue() const {
489+
return SILType::getPrimitiveObjectType(getTypeData().TypeOfRValue);
490+
}
489491
CanType getSubstFormalType() const { return getTypeData().SubstFormalType; }
490492
AbstractionPattern getOrigFormalType() const {
491493
return getTypeData().OrigFormalType;

lib/SILGen/SILGen.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
11231123
auto *type = blockParam->getType()->castTo<FunctionType>();
11241124
Type initType = FunctionType::get({}, TupleType::getEmpty(C),
11251125
type->getExtInfo());
1126-
auto initSILType = getLoweredType(initType).castTo<SILFunctionType>();
1126+
auto initSILType = cast<SILFunctionType>(
1127+
Types.getLoweredRValueType(initType));
11271128

11281129
SILGenFunctionBuilder builder(*this);
11291130
auto *f = builder.createFunction(
@@ -1337,9 +1338,10 @@ SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
13371338
componentObjTy = genericEnv->mapTypeIntoContext(componentObjTy);
13381339
auto storageTy = M.Types.getSubstitutedStorageType(decl, componentObjTy);
13391340
auto opaqueTy =
1340-
M.Types.getLoweredType(AbstractionPattern::getOpaque(), componentObjTy);
1341+
M.Types.getLoweredRValueType(AbstractionPattern::getOpaque(),
1342+
componentObjTy);
13411343

1342-
return storageTy.getAddressType() == opaqueTy.getAddressType();
1344+
return storageTy.getASTType() == opaqueTy;
13431345
}
13441346
case AccessStrategy::DirectToAccessor:
13451347
case AccessStrategy::DispatchToAccessor:

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5159,8 +5159,7 @@ ManagedValue SILGenFunction::emitLValueToPointer(SILLocation loc, LValue &&lv,
51595159
case PTK_AutoreleasingUnsafeMutablePointer: {
51605160
// Set up a writeback through a +0 buffer.
51615161
LValueTypeData typeData = lv.getTypeData();
5162-
SILType rvalueType = SILType::getPrimitiveObjectType(
5163-
CanUnmanagedStorageType::get(typeData.TypeOfRValue.getASTType()));
5162+
auto rvalueType = CanUnmanagedStorageType::get(typeData.TypeOfRValue);
51645163

51655164
LValueTypeData unownedTypeData(
51665165
lv.getAccessKind(),

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ SILGlobalVariable *SILGenModule::getSILGlobalVariable(VarDecl *gDecl,
5454
return gv;
5555
}
5656

57-
SILType silTy = M.Types.getLoweredTypeOfGlobal(gDecl);
57+
SILType silTy = SILType::getPrimitiveObjectType(
58+
M.Types.getLoweredTypeOfGlobal(gDecl));
5859

5960
auto *silGlobal = SILGlobalVariable::create(M, silLinkage, IsNotSerialized,
6061
mangledName, silTy,

0 commit comments

Comments
 (0)