Skip to content

Commit f6781de

Browse files
author
Joe Shajrawi
committed
copy_addr outline: cleanups based on review
1 parent 67f2852 commit f6781de

17 files changed

+141
-181
lines changed

lib/IRGen/FixedTypeInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class FixedTypeInfo : public TypeInfo {
228228
llvm::Value *vwtable,
229229
SILType T) const override {}
230230

231+
void collectArchetypeMetadata(
232+
IRGenFunction &IGF,
233+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
234+
SILType T) const override {
235+
return;
236+
}
237+
231238
llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
232239
llvm::Value *numEmptyCases,
233240
Address enumAddr,

lib/IRGen/GenArchetype.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ class OpaqueArchetypeTypeInfo
9191
static const OpaqueArchetypeTypeInfo *create(llvm::Type *type) {
9292
return new OpaqueArchetypeTypeInfo(type);
9393
}
94+
95+
void collectArchetypeMetadata(
96+
IRGenFunction &IGF,
97+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
98+
SILType T) const override {
99+
auto canType = T.getSwiftRValueType();
100+
if (typeToMetadataVec.find(canType) != typeToMetadataVec.end()) {
101+
return;
102+
}
103+
auto *metadata = IGF.emitTypeMetadataRef(canType);
104+
assert(metadata && "Expected Type Metadata Ref");
105+
typeToMetadataVec.insert(std::make_pair(canType, metadata));
106+
}
94107
};
95108

96109
/// A type implementation for a class archetype, that is, an archetype

lib/IRGen/GenDecl.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,8 +3542,7 @@ IRGenModule::getOrCreateReleaseFunction(const TypeInfo &objectTI, Type t,
35423542
void IRGenModule::generateCallToOutlinedCopyAddr(
35433543
IRGenFunction &IGF, const TypeInfo &objectTI, Address dest, Address src,
35443544
SILType T, const OutlinedCopyAddrFunction MethodToCall,
3545-
const llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
3546-
*typeToMetadataVec) {
3545+
const llvm::MapVector<CanType, llvm::Value *> *typeToMetadataVec) {
35473546
llvm::SmallVector<llvm::Value *, 4> argsVec;
35483547
argsVec.push_back(src.getAddress());
35493548
argsVec.push_back(dest.getAddress());
@@ -3558,6 +3557,9 @@ void IRGenModule::generateCallToOutlinedCopyAddr(
35583557
llvm::Type *llvmType = dest->getType();
35593558
auto *outlinedF =
35603559
(this->*MethodToCall)(objectTI, llvmType, T, typeToMetadataVec);
3560+
llvm::Function *fn = dyn_cast<llvm::Function>(outlinedF);
3561+
assert(fn && "Expected llvm::Function");
3562+
fn->setLinkage(llvm::GlobalValue::InternalLinkage);
35613563
llvm::CallInst *call = IGF.Builder.CreateCall(outlinedF, argsVec);
35623564
call->setCallingConv(DefaultCC);
35633565
}
@@ -3568,8 +3570,7 @@ llvm::Constant *IRGenModule::getOrCreateOutlinedCopyAddrHelperFunction(
35683570
llvm::function_ref<void(const TypeInfo &objectTI, IRGenFunction &IGF,
35693571
Address dest, Address src, SILType T)>
35703572
Generate,
3571-
const llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
3572-
*typeToMetadataVec) {
3573+
const llvm::MapVector<CanType, llvm::Value *> *typeToMetadataVec) {
35733574
llvm::SmallVector<llvm::Type *, 4> argsTysVec;
35743575
argsTysVec.push_back(llvmType);
35753576
argsTysVec.push_back(llvmType);
@@ -3600,8 +3601,7 @@ llvm::Constant *IRGenModule::getOrCreateOutlinedCopyAddrHelperFunction(
36003601

36013602
llvm::Constant *IRGenModule::getOrCreateOutlinedInitializeWithTakeFunction(
36023603
const TypeInfo &objectTI, llvm::Type *llvmType, SILType addrTy,
3603-
const llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
3604-
*typeToMetadataVec) {
3604+
const llvm::MapVector<CanType, llvm::Value *> *typeToMetadataVec) {
36053605
IRGenMangler mangler;
36063606
CanType canType = addrTy.getSwiftRValueType();
36073607
std::string funcName =
@@ -3616,8 +3616,7 @@ llvm::Constant *IRGenModule::getOrCreateOutlinedInitializeWithTakeFunction(
36163616

36173617
llvm::Constant *IRGenModule::getOrCreateOutlinedInitializeWithCopyFunction(
36183618
const TypeInfo &objectTI, llvm::Type *llvmType, SILType addrTy,
3619-
const llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
3620-
*typeToMetadataVec) {
3619+
const llvm::MapVector<CanType, llvm::Value *> *typeToMetadataVec) {
36213620
IRGenMangler mangler;
36223621
CanType canType = addrTy.getObjectType().getSwiftRValueType();
36233622
std::string funcName =
@@ -3632,8 +3631,7 @@ llvm::Constant *IRGenModule::getOrCreateOutlinedInitializeWithCopyFunction(
36323631

36333632
llvm::Constant *IRGenModule::getOrCreateOutlinedAssignWithTakeFunction(
36343633
const TypeInfo &objectTI, llvm::Type *llvmType, SILType addrTy,
3635-
const llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
3636-
*typeToMetadataVec) {
3634+
const llvm::MapVector<CanType, llvm::Value *> *typeToMetadataVec) {
36373635
IRGenMangler mangler;
36383636
CanType canType = addrTy.getObjectType().getSwiftRValueType();
36393637
std::string funcName =
@@ -3648,8 +3646,7 @@ llvm::Constant *IRGenModule::getOrCreateOutlinedAssignWithTakeFunction(
36483646

36493647
llvm::Constant *IRGenModule::getOrCreateOutlinedAssignWithCopyFunction(
36503648
const TypeInfo &objectTI, llvm::Type *llvmType, SILType addrTy,
3651-
const llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
3652-
*typeToMetadataVec) {
3649+
const llvm::MapVector<CanType, llvm::Value *> *typeToMetadataVec) {
36533650
IRGenMangler mangler;
36543651
CanType canType = addrTy.getObjectType().getSwiftRValueType();
36553652
std::string funcName =
@@ -3662,7 +3659,13 @@ llvm::Constant *IRGenModule::getOrCreateOutlinedAssignWithCopyFunction(
36623659
objectTI, llvmType, addrTy, funcName, GenFunc, typeToMetadataVec);
36633660
}
36643661

3662+
// IRGen is only multi-threaded during LLVM part
3663+
// We don't need to be thread safe even
3664+
// We are working on the primary module *before* LLVM
36653665
unsigned IRGenModule::getCanTypeID(const CanType type) {
3666+
if (this != IRGen.getPrimaryIGM()) {
3667+
return IRGen.getPrimaryIGM()->getCanTypeID(type);
3668+
}
36663669
auto it = typeToUniqueID.find(type.getPointer());
36673670
if (it != typeToUniqueID.end()) {
36683671
return it->second;

lib/IRGen/GenEnum.cpp

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ namespace {
443443
getSingleton()->assignWithCopy(
444444
IGF, dest, src, getSingletonType(IGF.IGM, T), isOutlined);
445445
} else {
446-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
447-
typeToMetadataVec;
446+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
448447
if (T.hasArchetype()) {
449448
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
450449
}
@@ -464,8 +463,7 @@ namespace {
464463
getSingleton()->assignWithTake(
465464
IGF, dest, src, getSingletonType(IGF.IGM, T), isOutlined);
466465
} else {
467-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
468-
typeToMetadataVec;
466+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
469467
if (T.hasArchetype()) {
470468
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
471469
}
@@ -492,8 +490,7 @@ namespace {
492490
getSingleton()->initializeWithCopy(
493491
IGF, dest, src, getSingletonType(IGF.IGM, T), isOutlined);
494492
} else {
495-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
496-
typeToMetadataVec;
493+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
497494
if (T.hasArchetype()) {
498495
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
499496
}
@@ -513,8 +510,7 @@ namespace {
513510
getSingleton()->initializeWithTake(
514511
IGF, dest, src, getSingletonType(IGF.IGM, T), isOutlined);
515512
} else {
516-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
517-
typeToMetadataVec;
513+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
518514
if (T.hasArchetype()) {
519515
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
520516
}
@@ -527,8 +523,7 @@ namespace {
527523

528524
void collectArchetypeMetadata(
529525
IRGenFunction &IGF,
530-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
531-
&typeToMetadataVec,
526+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
532527
SILType T) const override {
533528
if (!getSingleton())
534529
return;
@@ -936,8 +931,7 @@ namespace {
936931

937932
void collectArchetypeMetadata(
938933
IRGenFunction &IGF,
939-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
940-
&typeToMetadataVec,
934+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
941935
SILType T) const override {
942936
auto canType = T.getSwiftRValueType();
943937
assert(!canType->hasArchetype() &&
@@ -1374,8 +1368,7 @@ namespace {
13741368

13751369
void collectArchetypeMetadata(
13761370
IRGenFunction &IGF,
1377-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
1378-
&typeToMetadataVec,
1371+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
13791372
SILType T) const override {
13801373
assert(TIK >= Loadable);
13811374
}
@@ -2674,8 +2667,7 @@ namespace {
26742667
if (isOutlined || T.hasOpenedExistential()) {
26752668
emitIndirectAssign(IGF, dest, src, T, IsNotTake, isOutlined);
26762669
} else {
2677-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
2678-
typeToMetadataVec;
2670+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
26792671
if (T.hasArchetype()) {
26802672
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
26812673
}
@@ -2691,8 +2683,7 @@ namespace {
26912683
if (isOutlined || T.hasOpenedExistential()) {
26922684
emitIndirectAssign(IGF, dest, src, T, IsTake, isOutlined);
26932685
} else {
2694-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
2695-
typeToMetadataVec;
2686+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
26962687
if (T.hasArchetype()) {
26972688
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
26982689
}
@@ -2708,8 +2699,7 @@ namespace {
27082699
if (isOutlined || T.hasOpenedExistential()) {
27092700
emitIndirectInitialize(IGF, dest, src, T, IsNotTake, isOutlined);
27102701
} else {
2711-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
2712-
typeToMetadataVec;
2702+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
27132703
if (T.hasArchetype()) {
27142704
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
27152705
}
@@ -2725,8 +2715,7 @@ namespace {
27252715
if (isOutlined || T.hasOpenedExistential()) {
27262716
emitIndirectInitialize(IGF, dest, src, T, IsTake, isOutlined);
27272717
} else {
2728-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
2729-
typeToMetadataVec;
2718+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
27302719
if (T.hasArchetype()) {
27312720
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
27322721
}
@@ -2739,20 +2728,22 @@ namespace {
27392728

27402729
void collectArchetypeMetadata(
27412730
IRGenFunction &IGF,
2742-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
2743-
&typeToMetadataVec,
2731+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
27442732
SILType T) const override {
27452733
auto canType = T.getSwiftRValueType();
2746-
if (shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
2747-
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
2748-
assert(metadata && "Expected Type Metadata Ref");
2749-
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
2750-
}
2734+
// get the size before insertions
2735+
auto SZ = typeToMetadataVec.size();
27512736
if (CopyDestroyKind == Normal) {
27522737
auto payloadT = getPayloadType(IGF.IGM, T);
27532738
getPayloadTypeInfo().collectArchetypeMetadata(IGF, typeToMetadataVec,
27542739
payloadT);
27552740
}
2741+
if (typeToMetadataVec.find(canType) == typeToMetadataVec.end() &&
2742+
SZ != typeToMetadataVec.size()) {
2743+
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
2744+
assert(metadata && "Expected Type Metadata Ref");
2745+
typeToMetadataVec.insert(std::make_pair(canType, metadata));
2746+
}
27562747
}
27572748

27582749
void storeTag(IRGenFunction &IGF,
@@ -4346,8 +4337,7 @@ namespace {
43464337
if (isOutlined || T.hasOpenedExistential()) {
43474338
emitIndirectAssign(IGF, dest, src, T, IsNotTake, isOutlined);
43484339
} else {
4349-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
4350-
typeToMetadataVec;
4340+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
43514341
if (T.hasArchetype()) {
43524342
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
43534343
}
@@ -4363,8 +4353,7 @@ namespace {
43634353
if (isOutlined || T.hasOpenedExistential()) {
43644354
emitIndirectAssign(IGF, dest, src, T, IsTake, isOutlined);
43654355
} else {
4366-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
4367-
typeToMetadataVec;
4356+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
43684357
if (T.hasArchetype()) {
43694358
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
43704359
}
@@ -4380,8 +4369,7 @@ namespace {
43804369
if (isOutlined || T.hasOpenedExistential()) {
43814370
emitIndirectInitialize(IGF, dest, src, T, IsNotTake, isOutlined);
43824371
} else {
4383-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
4384-
typeToMetadataVec;
4372+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
43854373
if (T.hasArchetype()) {
43864374
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
43874375
}
@@ -4397,8 +4385,7 @@ namespace {
43974385
if (isOutlined || T.hasOpenedExistential()) {
43984386
emitIndirectInitialize(IGF, dest, src, T, IsTake, isOutlined);
43994387
} else {
4400-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
4401-
typeToMetadataVec;
4388+
llvm::MapVector<CanType, llvm::Value *> typeToMetadataVec;
44024389
if (T.hasArchetype()) {
44034390
collectArchetypeMetadata(IGF, typeToMetadataVec, T);
44044391
}
@@ -4411,15 +4398,11 @@ namespace {
44114398

44124399
void collectArchetypeMetadata(
44134400
IRGenFunction &IGF,
4414-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
4415-
&typeToMetadataVec,
4401+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
44164402
SILType T) const override {
44174403
auto canType = T.getSwiftRValueType();
4418-
if (shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
4419-
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
4420-
assert(metadata && "Expected Type Metadata Ref");
4421-
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
4422-
}
4404+
// get the size before insertions
4405+
auto SZ = typeToMetadataVec.size();
44234406
if (CopyDestroyKind != Normal) {
44244407
return;
44254408
}
@@ -4429,6 +4412,12 @@ namespace {
44294412
auto &payloadTI = *payloadCasePair.ti;
44304413
payloadTI.collectArchetypeMetadata(IGF, typeToMetadataVec, PayloadT);
44314414
}
4415+
if (typeToMetadataVec.find(canType) == typeToMetadataVec.end() &&
4416+
typeToMetadataVec.size() != SZ) {
4417+
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
4418+
assert(metadata && "Expected Type Metadata Ref");
4419+
typeToMetadataVec.insert(std::make_pair(canType, metadata));
4420+
}
44324421
}
44334422

44344423
void destroy(IRGenFunction &IGF, Address addr, SILType T) const override {
@@ -4952,16 +4941,15 @@ namespace {
49524941

49534942
void collectArchetypeMetadata(
49544943
IRGenFunction &IGF,
4955-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
4956-
&typeToMetadataVec,
4944+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
49574945
SILType T) const override {
49584946
auto canType = T.getSwiftRValueType();
4959-
if (!shouldEmitMetadataRefForLayout(IGF.IGM, canType)) {
4947+
if (typeToMetadataVec.find(canType) != typeToMetadataVec.end()) {
49604948
return;
49614949
}
49624950
auto *metadata = IGF.emitTypeMetadataRefForLayout(T);
49634951
assert(metadata && "Expected Type Metadata Ref");
4964-
typeToMetadataVec.push_back(std::make_pair(canType, metadata));
4952+
typeToMetadataVec.insert(std::make_pair(canType, metadata));
49654953
}
49664954

49674955
void destroy(IRGenFunction &IGF, Address addr, SILType T) const override {
@@ -5346,8 +5334,7 @@ namespace {
53465334
}
53475335
void collectArchetypeMetadata(
53485336
IRGenFunction &IGF,
5349-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
5350-
&typeToMetadataVec,
5337+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
53515338
SILType T) const override {
53525339
return Strategy.collectArchetypeMetadata(IGF, typeToMetadataVec, T);
53535340
}

lib/IRGen/GenEnum.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -432,28 +432,9 @@ class EnumImplStrategy {
432432

433433
virtual void collectArchetypeMetadata(
434434
IRGenFunction &IGF,
435-
llvm::SmallVector<std::pair<CanType, llvm::Value *>, 4>
436-
&typeToMetadataVec,
435+
llvm::MapVector<CanType, llvm::Value *> &typeToMetadataVec,
437436
SILType T) const = 0;
438437

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-
457438
private:
458439
EnumImplStrategy(const EnumImplStrategy &) = delete;
459440
EnumImplStrategy &operator=(const EnumImplStrategy &) = delete;

0 commit comments

Comments
 (0)