@@ -3466,6 +3466,10 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
3466
3466
return ctorAddress;
3467
3467
}
3468
3468
3469
+ // Check whether we've created the thunk already.
3470
+ if (auto *thunkFn = IGM.Module .getFunction (name))
3471
+ return thunkFn;
3472
+
3469
3473
llvm::Function *thunk = llvm::Function::Create (
3470
3474
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module );
3471
3475
@@ -3546,10 +3550,15 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3546
3550
LinkEntity entity =
3547
3551
LinkEntity::forSILFunction (f, shouldCallPreviousImplementation);
3548
3552
3549
- // Check whether we've created the function already.
3553
+ auto clangDecl = f->getClangDecl ();
3554
+ auto cxxCtor = dyn_cast_or_null<clang::CXXConstructorDecl>(clangDecl);
3555
+
3556
+ // Check whether we've created the function already. If the function is a C++
3557
+ // constructor, don't return the constructor here as a thunk might be needed
3558
+ // to call the constructor.
3550
3559
// FIXME: We should integrate this into the LinkEntity cache more cleanly.
3551
3560
llvm::Function *fn = Module.getFunction (entity.mangleAsString ());
3552
- if (fn) {
3561
+ if (fn && !cxxCtor ) {
3553
3562
if (forDefinition) {
3554
3563
updateLinkageForDefinition (*this , fn, entity);
3555
3564
}
@@ -3561,7 +3570,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3561
3570
// the insert-before point.
3562
3571
llvm::Constant *clangAddr = nullptr ;
3563
3572
bool isObjCDirect = false ;
3564
- if (auto clangDecl = f-> getClangDecl () ) {
3573
+ if (clangDecl) {
3565
3574
// If we have an Objective-C Clang declaration, it must be a direct
3566
3575
// method and we want to generate the IR declaration ourselves.
3567
3576
if (auto objcDecl = dyn_cast<clang::ObjCMethodDecl>(clangDecl)) {
@@ -3572,7 +3581,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3572
3581
clangAddr = getAddrOfClangGlobalDecl (globalDecl, forDefinition);
3573
3582
}
3574
3583
3575
- if (auto ctor = dyn_cast<clang::CXXConstructorDecl>(clangDecl) ) {
3584
+ if (cxxCtor ) {
3576
3585
Signature signature = getSignature (f->getLoweredFunctionType ());
3577
3586
3578
3587
// The thunk has private linkage, so it doesn't need to have a predictable
@@ -3582,7 +3591,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3582
3591
stream << " __swift_cxx_ctor" ;
3583
3592
entity.mangle (stream);
3584
3593
3585
- clangAddr = emitCXXConstructorThunkIfNeeded (*this , signature, ctor , name,
3594
+ clangAddr = emitCXXConstructorThunkIfNeeded (*this , signature, cxxCtor , name,
3586
3595
clangAddr);
3587
3596
}
3588
3597
}
0 commit comments