Skip to content

Commit 5c97b21

Browse files
committed
Merge pull request #1253 from dduan/delete_typelowering_dead_code
early exit in TypeLowering for address-only type
2 parents 31365c3 + c5f9fc1 commit 5c97b21

File tree

3 files changed

+18
-83
lines changed

3 files changed

+18
-83
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ class TypeLowering {
151151
return !isAddressOnly();
152152
}
153153

154-
/// True if the type was successfully lowered, false if there was an error
155-
/// during type lowering.
156-
virtual bool isValid() const {
157-
return true;
158-
}
159-
160154
/// Returns true if the type is trivial, meaning it is a loadable
161155
/// value type with no reference type members that require releasing.
162156
bool isTrivial() const {

lib/SIL/TypeLowering.cpp

Lines changed: 18 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,12 @@ namespace {
395395
// Tuples depend on their elements.
396396
RetTy visitTupleType(CanTupleType type) {
397397
bool hasReference = false;
398-
// TODO: We ought to be able to early-exit as soon as we've established
399-
// that a type is address-only. However, we also currently rely on
400-
// SIL lowering to catch unsupported recursive value types.
401-
bool isAddressOnly = false;
402398
for (auto eltType : type.getElementTypes()) {
403399
switch (classifyType(eltType, M, Sig, Expansion)) {
404400
case LoweredTypeKind::Trivial:
405401
continue;
406402
case LoweredTypeKind::AddressOnly:
407-
isAddressOnly = true;
408-
continue;
403+
return asImpl().handleAddressOnly(type);
409404
case LoweredTypeKind::Reference:
410405
case LoweredTypeKind::AggWithReference:
411406
hasReference = true;
@@ -414,8 +409,6 @@ namespace {
414409
llvm_unreachable("bad type classification");
415410
}
416411

417-
if (isAddressOnly)
418-
return asImpl().handleAddressOnly(type);
419412
if (hasReference)
420413
return asImpl().handleAggWithReference(type);
421414
return asImpl().handleTrivial(type);
@@ -1034,17 +1027,6 @@ namespace {
10341027
llvm_unreachable("cannot destroy an UnsafeValueBuffer!");
10351028
}
10361029
};
1037-
1038-
/// A class that acts as a stand-in for improperly recursive types.
1039-
class RecursiveErrorTypeLowering : public AddressOnlyTypeLowering {
1040-
public:
1041-
RecursiveErrorTypeLowering(SILType type)
1042-
: AddressOnlyTypeLowering(type) {}
1043-
1044-
bool isValid() const override {
1045-
return false;
1046-
}
1047-
};
10481030

10491031
/// Build the appropriate TypeLowering subclass for the given type.
10501032
class LowerType
@@ -1136,39 +1118,30 @@ namespace {
11361118
const TypeLowering *visitTupleType(CanTupleType tupleType) {
11371119
typedef LoadableTupleTypeLowering::Child Child;
11381120
SmallVector<Child, 8> childElts;
1139-
// TODO: We ought to be able to early-exit as soon as we've established
1140-
// that a type is address-only. However, we also currently rely on
1141-
// SIL lowering to catch unsupported recursive value types.
1142-
bool isAddressOnly = false;
11431121
bool hasOnlyTrivialChildren = true;
11441122

11451123
unsigned i = 0;
11461124
for (auto eltType : tupleType.getElementTypes()) {
11471125
auto &lowering = TC.getTypeLowering(eltType);
11481126
if (lowering.isAddressOnly())
1149-
isAddressOnly = true;
1127+
return handleAddressOnly(tupleType);
11501128
hasOnlyTrivialChildren &= lowering.isTrivial();
11511129
childElts.push_back(Child(i, lowering));
11521130
++i;
11531131
}
11541132

1155-
if (isAddressOnly)
1156-
return handleAddressOnly(tupleType);
11571133
if (hasOnlyTrivialChildren)
11581134
return handleTrivial(tupleType);
1135+
11591136
return new (TC, Dependent) LoadableTupleTypeLowering(OrigType);
11601137
}
11611138

11621139
const TypeLowering *visitAnyStructType(CanType structType, StructDecl *D) {
1163-
// TODO: We ought to be able to early-exit as soon as we've established
1164-
// that a type is address-only. However, we also currently rely on
1165-
// SIL lowering to catch unsupported recursive value types.
1166-
bool isAddressOnly = false;
1167-
1140+
11681141
// For now, if the type does not have a fixed layout in all resilience
11691142
// domains, we will treat it as address-only in SIL.
11701143
if (!D->hasFixedLayout(M.getSwiftModule(), Expansion))
1171-
isAddressOnly = true;
1144+
return handleAddressOnly(structType);
11721145

11731146
// Classify the type according to its stored properties.
11741147
bool trivial = true;
@@ -1178,8 +1151,7 @@ namespace {
11781151
switch (classifyType(substFieldType->getCanonicalType(),
11791152
M, Sig, Expansion)) {
11801153
case LoweredTypeKind::AddressOnly:
1181-
isAddressOnly = true;
1182-
break;
1154+
return handleAddressOnly(structType);
11831155
case LoweredTypeKind::AggWithReference:
11841156
case LoweredTypeKind::Reference:
11851157
trivial = false;
@@ -1189,23 +1161,17 @@ namespace {
11891161
}
11901162
}
11911163

1192-
if (isAddressOnly)
1193-
return handleAddressOnly(structType);
11941164
if (trivial)
11951165
return handleTrivial(structType);
11961166
return new (TC, Dependent) LoadableStructTypeLowering(OrigType);
11971167
}
11981168

11991169
const TypeLowering *visitAnyEnumType(CanType enumType, EnumDecl *D) {
1200-
// TODO: We ought to be able to early-exit as soon as we've established
1201-
// that a type is address-only. However, we also currently rely on
1202-
// SIL lowering to catch unsupported recursive value types.
1203-
bool isAddressOnly = false;
12041170

12051171
// For now, if the type does not have a fixed layout in all resilience
12061172
// domains, we will treat it as address-only in SIL.
12071173
if (!D->hasFixedLayout(M.getSwiftModule(), Expansion))
1208-
isAddressOnly = true;
1174+
return handleAddressOnly(enumType);
12091175

12101176
// Lower Self? as if it were Whatever? and Self! as if it were Whatever!.
12111177
if (auto genericEnum = dyn_cast<BoundGenericEnumType>(enumType)) {
@@ -1238,8 +1204,6 @@ namespace {
12381204
// is still address only, because we don't know how many bits
12391205
// are used for the discriminator.
12401206
if (D->isIndirect()) {
1241-
if (isAddressOnly)
1242-
return handleAddressOnly(OrigType);
12431207
return new (TC, Dependent) LoadableEnumTypeLowering(OrigType);
12441208
}
12451209

@@ -1267,8 +1231,7 @@ namespace {
12671231
switch (classifyType(substEltType->getCanonicalType(),
12681232
M, Sig, Expansion)) {
12691233
case LoweredTypeKind::AddressOnly:
1270-
isAddressOnly = true;
1271-
break;
1234+
return handleAddressOnly(enumType);
12721235
case LoweredTypeKind::AggWithReference:
12731236
case LoweredTypeKind::Reference:
12741237
trivial = false;
@@ -1278,8 +1241,6 @@ namespace {
12781241
}
12791242

12801243
}
1281-
if (isAddressOnly)
1282-
return handleAddressOnly(enumType);
12831244
if (trivial)
12841245
return handleTrivial(enumType);
12851246
return new (TC, Dependent) LoadableEnumTypeLowering(OrigType);
@@ -1326,35 +1287,17 @@ const TypeLowering *TypeConverter::find(TypeKey k) {
13261287
auto found = Types.find(ck);
13271288
if (found == Types.end())
13281289
return nullptr;
1329-
// We place a null placeholder in the hashtable to catch
1330-
// reentrancy, which arises as a result of improper recursion.
1331-
// TODO: We should diagnose nonterminating recursion in Sema, and implement
1332-
// terminating recursive enums, instead of diagnosing here.
1333-
// When that Sema check is in place, we should reinstate the early-exit
1334-
// behavior for address-only types (marked by other TODO: items throughout
1335-
// this file).
1336-
if (auto elt = found->second)
1337-
return elt;
1338-
1339-
// Try to complain about a nominal type.
1340-
auto nomTy = k.SubstType.getAnyNominal();
1341-
assert(nomTy && "non-nominal types should not be recursive");
1342-
1343-
auto result = new (*this, k.isDependent()) RecursiveErrorTypeLowering(
1344-
SILType::getPrimitiveAddressType(k.SubstType));
1345-
found->second = result;
1346-
return result;
1290+
1291+
assert(found->second && "type recursion not caught in Sema");
1292+
return found->second;
13471293
}
13481294

13491295
void TypeConverter::insert(TypeKey k, const TypeLowering *tl) {
13501296
if (!k.isCacheable()) return;
13511297

13521298
auto &Types = k.isDependent() ? DependentTypes : IndependentTypes;
1353-
// TODO: Types[k] should always be null at this point, except that we
1354-
// rely on type lowering to discover recursive value types right now.
1355-
auto ck = k.getCachingKey();
1356-
if (!Types[ck])
1357-
Types[ck] = tl;
1299+
1300+
Types[k.getCachingKey()] = tl;
13581301
}
13591302

13601303
#ifndef NDEBUG
@@ -1629,11 +1572,11 @@ getTypeLoweringForUncachedLoweredFunctionType(TypeKey key) {
16291572
assert(isa<AnyFunctionType>(key.SubstType));
16301573
assert(key.UncurryLevel == 0);
16311574

1575+
#ifdef DEBUG
16321576
// Catch recursions.
1633-
// FIXME: These should be bugs, so we shouldn't need to do this in release
1634-
// builds.
16351577
insert(key, nullptr);
1636-
1578+
#endif
1579+
16371580
// Generic functions aren't first-class values and shouldn't end up lowered
16381581
// through this interface.
16391582
assert(!isa<PolymorphicFunctionType>(key.SubstType)
@@ -1657,10 +1600,10 @@ TypeConverter::getTypeLoweringForUncachedLoweredType(TypeKey key) {
16571600
assert(!find(key) && "re-entrant or already cached");
16581601
assert(isLoweredType(key.SubstType) && "didn't lower out l-value type?");
16591602

1603+
#ifdef DEBUG
16601604
// Catch reentrancy bugs.
1661-
// FIXME: These should be bugs, so we shouldn't need to do this in release
1662-
// builds.
16631605
insert(key, nullptr);
1606+
#endif
16641607

16651608
CanType contextType = key.SubstType;
16661609
// FIXME: Get expansion from SILFunction

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ getFullyReferenceableStruct(SILType Ty) {
4141
}
4242

4343
static unsigned getNumSubElements(SILType T, SILModule &M) {
44-
if (!M.getTypeLowering(T).isValid())
45-
return 0;
4644

4745
if (auto TT = T.getAs<TupleType>()) {
4846
unsigned NumElements = 0;

0 commit comments

Comments
 (0)