@@ -3332,6 +3332,10 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
3332
3332
return ctorAddress;
3333
3333
}
3334
3334
3335
+ // Check whether we've created the thunk already.
3336
+ if (auto *thunkFn = IGM.Module .getFunction (name))
3337
+ return thunkFn;
3338
+
3335
3339
llvm::Function *thunk = llvm::Function::Create (
3336
3340
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module );
3337
3341
@@ -3412,10 +3416,15 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3412
3416
LinkEntity entity =
3413
3417
LinkEntity::forSILFunction (f, shouldCallPreviousImplementation);
3414
3418
3415
- // Check whether we've created the function already.
3419
+ auto clangDecl = f->getClangDecl ();
3420
+ auto cxxCtor = dyn_cast_or_null<clang::CXXConstructorDecl>(clangDecl);
3421
+
3422
+ // Check whether we've created the function already. If the function is a C++
3423
+ // constructor, don't return the constructor here as a thunk might be needed
3424
+ // to call the constructor.
3416
3425
// FIXME: We should integrate this into the LinkEntity cache more cleanly.
3417
3426
llvm::Function *fn = Module.getFunction (entity.mangleAsString ());
3418
- if (fn) {
3427
+ if (fn && !cxxCtor ) {
3419
3428
if (forDefinition) {
3420
3429
updateLinkageForDefinition (*this , fn, entity);
3421
3430
}
@@ -3427,7 +3436,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3427
3436
// the insert-before point.
3428
3437
llvm::Constant *clangAddr = nullptr ;
3429
3438
bool isObjCDirect = false ;
3430
- if (auto clangDecl = f-> getClangDecl () ) {
3439
+ if (clangDecl) {
3431
3440
// If we have an Objective-C Clang declaration, it must be a direct
3432
3441
// method and we want to generate the IR declaration ourselves.
3433
3442
if (auto objcDecl = dyn_cast<clang::ObjCMethodDecl>(clangDecl)) {
@@ -3438,7 +3447,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3438
3447
clangAddr = getAddrOfClangGlobalDecl (globalDecl, forDefinition);
3439
3448
}
3440
3449
3441
- if (auto ctor = dyn_cast<clang::CXXConstructorDecl>(clangDecl) ) {
3450
+ if (cxxCtor ) {
3442
3451
Signature signature = getSignature (f->getLoweredFunctionType ());
3443
3452
3444
3453
// The thunk has private linkage, so it doesn't need to have a predictable
@@ -3448,7 +3457,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
3448
3457
stream << " __swift_cxx_ctor" ;
3449
3458
entity.mangle (stream);
3450
3459
3451
- clangAddr = emitCXXConstructorThunkIfNeeded (*this , signature, ctor , name,
3460
+ clangAddr = emitCXXConstructorThunkIfNeeded (*this , signature, cxxCtor , name,
3452
3461
clangAddr);
3453
3462
}
3454
3463
}
0 commit comments