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