Skip to content

Commit 67f2852

Browse files
author
Joe Shajrawi
committed
Code Size: copy_addr cleanup - get rid of mightContainMetadata
1 parent 62d823c commit 67f2852

File tree

6 files changed

+55
-88
lines changed

6 files changed

+55
-88
lines changed

lib/IRGen/GenEnum.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,8 +2743,8 @@ namespace {
27432743
&typeToMetadataVec,
27442744
SILType T) const override {
27452745
auto canType = T.getSwiftRValueType();
2746-
if (irgen::mightContainMetadata(IGF.IGM, canType)) {
2747-
auto *metadata = IGF.emitTypeMetadataRef(canType);
2746+
if (shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
2747+
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
27482748
assert(metadata && "Expected Type Metadata Ref");
27492749
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
27502750
}
@@ -4415,8 +4415,8 @@ namespace {
44154415
&typeToMetadataVec,
44164416
SILType T) const override {
44174417
auto canType = T.getSwiftRValueType();
4418-
if (irgen::mightContainMetadata(IGF.IGM, canType)) {
4419-
auto *metadata = IGF.emitTypeMetadataRef(canType);
4418+
if (shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
4419+
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
44204420
assert(metadata && "Expected Type Metadata Ref");
44214421
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
44224422
}
@@ -4956,10 +4956,10 @@ namespace {
49564956
&typeToMetadataVec,
49574957
SILType T) const override {
49584958
auto canType = T.getSwiftRValueType();
4959-
if (!irgen::mightContainMetadata(IGF.IGM, canType)) {
4959+
if (!shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
49604960
return;
49614961
}
4962-
auto *metadata = IGF.emitTypeMetadataRef(canType);
4962+
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
49634963
assert(metadata && "Expected Type Metadata Ref");
49644964
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
49654965
}

lib/IRGen/GenEnum.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,24 @@ class EnumImplStrategy {
436436
&typeToMetadataVec,
437437
SILType T) const = 0;
438438

439+
bool shouldEmitMetadataRefForLayout(IRGenModule &IGM,
440+
const CanType canType) const {
441+
if (!irgen::isTypeDependent(IGM, canType)) {
442+
return false;
443+
}
444+
if (canType->is<EnumType>()) {
445+
return true;
446+
}
447+
auto genEnum = cast<BoundGenericEnumType>(canType);
448+
assert(genEnum && "Expected a BoundGenericEnumType");
449+
for (auto arg : genEnum->getGenericArgs()) {
450+
if (isTypeDependent(IGM, arg->getCanonicalType())) {
451+
return true;
452+
}
453+
}
454+
return false;
455+
}
456+
439457
private:
440458
EnumImplStrategy(const EnumImplStrategy &) = delete;
441459
EnumImplStrategy &operator=(const EnumImplStrategy &) = delete;

lib/IRGen/GenRecord.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class RecordTypeInfoImpl : public Base,
258258
&typeToMetadataVec,
259259
SILType T) const override {
260260
auto canType = T.getSwiftRValueType();
261-
if (irgen::mightContainMetadata(IGF.IGM, canType)) {
261+
if (shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
262262
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
263263
assert(metadata && "Expected Type Metadata Ref");
264264
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
@@ -271,6 +271,24 @@ class RecordTypeInfoImpl : public Base,
271271
fType);
272272
}
273273
}
274+
275+
private:
276+
// For some types we might need the record's layout metadata
277+
bool shouldEmitMetadataRefForLayout(IRGenModule &IGM,
278+
const CanType canType) const {
279+
if (!irgen::isTypeDependent(IGM, canType)) {
280+
return false;
281+
}
282+
if (auto genTuple = dyn_cast<TupleType>(canType)) {
283+
for (auto elt : genTuple->getElements()) {
284+
if (isTypeDependent(IGM, elt.getType()->getCanonicalType())) {
285+
return true;
286+
}
287+
}
288+
return false;
289+
}
290+
return true;
291+
}
274292
};
275293

276294
template <class Impl, class Base, class FieldImpl_,

lib/IRGen/GenType.cpp

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,87 +1800,10 @@ static bool isIRTypeDependent(IRGenModule &IGM, NominalTypeDecl *decl) {
18001800
}
18011801
}
18021802

1803-
bool irgen::mightContainMetadata(const IRGenModule &IGM, const CanType type) {
1803+
bool irgen::isTypeDependent(const IRGenModule &IGM, const CanType type) {
18041804
IRGenModule &IGMCast = const_cast<IRGenModule &>(IGM);
18051805
auto runType = irgen::getRuntimeReifiedType(IGMCast, type);
1806-
if (!IsIRTypeDependent(IGMCast).visit(runType)) {
1807-
return false;
1808-
}
1809-
switch (runType->getKind()) {
1810-
#define SUGARED_TYPE(ID, SUPER) case TypeKind::ID:
1811-
#define UNCHECKED_TYPE(ID, SUPER) case TypeKind::ID:
1812-
#define TYPE(ID, SUPER)
1813-
#include "swift/AST/TypeNodes.def"
1814-
case TypeKind::Error:
1815-
llvm_unreachable("kind is invalid for a canonical type");
1816-
1817-
#define ARTIFICIAL_TYPE(ID, SUPER) case TypeKind::ID:
1818-
#define TYPE(ID, SUPER)
1819-
#include "swift/AST/TypeNodes.def"
1820-
case TypeKind::LValue:
1821-
case TypeKind::InOut:
1822-
case TypeKind::DynamicSelf:
1823-
return false;
1824-
1825-
// Builtin types might contain metadata
1826-
#define BUILTIN_TYPE(ID, SUPER) case TypeKind::ID:
1827-
#define TYPE(ID, SUPER)
1828-
#include "swift/AST/TypeNodes.def"
1829-
case TypeKind::Module:
1830-
return true;
1831-
1832-
// Type parameters
1833-
case TypeKind::Archetype:
1834-
case TypeKind::GenericTypeParam:
1835-
case TypeKind::DependentMember:
1836-
return true;
1837-
1838-
case TypeKind::Tuple: {
1839-
auto genTuple = cast<TupleType>(runType);
1840-
for (auto elt : genTuple->getElements()) {
1841-
if (mightContainMetadata(IGM, elt.getType()->getCanonicalType())) {
1842-
return true;
1843-
}
1844-
}
1845-
return false;
1846-
}
1847-
1848-
// Nominal types
1849-
case TypeKind::Class:
1850-
case TypeKind::Enum:
1851-
case TypeKind::Protocol:
1852-
case TypeKind::Struct:
1853-
return true;
1854-
1855-
case TypeKind::BoundGenericEnum: {
1856-
auto genEnum = cast<BoundGenericEnumType>(runType);
1857-
for (auto arg : genEnum->getGenericArgs()) {
1858-
if (mightContainMetadata(IGM, arg->getCanonicalType())) {
1859-
return true;
1860-
}
1861-
}
1862-
return false;
1863-
}
1864-
1865-
// Bound generic types
1866-
case TypeKind::BoundGenericClass:
1867-
case TypeKind::BoundGenericStruct:
1868-
return true;
1869-
1870-
// Function types do not have metadata
1871-
case TypeKind::Function:
1872-
case TypeKind::GenericFunction:
1873-
return false;
1874-
1875-
case TypeKind::ProtocolComposition:
1876-
return true;
1877-
1878-
// Metatypes
1879-
case TypeKind::Metatype:
1880-
case TypeKind::ExistentialMetatype:
1881-
return true;
1882-
}
1883-
llvm_unreachable("bad type kind");
1806+
return IsIRTypeDependent(IGMCast).visit(runType);
18841807
}
18851808

18861809
TypeCacheEntry TypeConverter::convertAnyNominalType(CanType type,

lib/IRGen/GenValueWitness.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,12 +1392,20 @@ void TypeInfo::assignArrayWithTake(IRGenFunction &IGF, Address dest,
13921392
emitAssignArrayWithTakeCall(IGF, T, dest, src, count);
13931393
}
13941394

1395+
static bool shouldEmitTypeMetadata(const CanType canType) {
1396+
// Determine whether this type is an Archetype itself.
1397+
if (!canType->getWithoutSpecifierType()->is<ArchetypeType>()) {
1398+
return false;
1399+
}
1400+
return true;
1401+
}
1402+
13951403
void TypeInfo::collectArchetypeMetadata(
13961404
IRGenFunction &IGF,
13971405
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4> &typeToMetadataVec,
13981406
SILType T) const {
13991407
auto canType = T.getSwiftRValueType();
1400-
if (irgen::mightContainMetadata(IGF.IGM, canType)) {
1408+
if (shouldEmitTypeMetadata(canType)) {
14011409
auto *metadata = IGF.emitTypeMetadataRef(canType);
14021410
assert(metadata && "Expected Type Metadata Ref");
14031411
typeToMetadataVec.push_back(std::make_pair(canType, metadata));

lib/IRGen/TypeInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class TypeInfo {
500500
llvm::Value *typeMetadata,
501501
SILType T) const;
502502
};
503-
bool mightContainMetadata(const IRGenModule &IGM, const CanType type);
503+
bool isTypeDependent(const IRGenModule &IGM, const CanType type);
504504

505505
} // end namespace irgen
506506
} // end namespace swift

0 commit comments

Comments
 (0)