Skip to content

[IRGen] Make sure a C++ constructor thunk is called when it's needed #71790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,10 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
return ctorAddress;
}

// Check whether we've created the thunk already.
if (auto *thunkFn = IGM.Module.getFunction(name))
return thunkFn;

llvm::Function *thunk = llvm::Function::Create(
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module);

Expand Down Expand Up @@ -3546,10 +3550,15 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
LinkEntity entity =
LinkEntity::forSILFunction(f, shouldCallPreviousImplementation);

// Check whether we've created the function already.
auto clangDecl = f->getClangDecl();
auto cxxCtor = dyn_cast_or_null<clang::CXXConstructorDecl>(clangDecl);

// Check whether we've created the function already. If the function is a C++
// constructor, don't return the constructor here as a thunk might be needed
// to call the constructor.
// FIXME: We should integrate this into the LinkEntity cache more cleanly.
llvm::Function *fn = Module.getFunction(entity.mangleAsString());
if (fn) {
if (fn && !cxxCtor) {
if (forDefinition) {
updateLinkageForDefinition(*this, fn, entity);
}
Expand All @@ -3561,7 +3570,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
// the insert-before point.
llvm::Constant *clangAddr = nullptr;
bool isObjCDirect = false;
if (auto clangDecl = f->getClangDecl()) {
if (clangDecl) {
// If we have an Objective-C Clang declaration, it must be a direct
// method and we want to generate the IR declaration ourselves.
if (auto objcDecl = dyn_cast<clang::ObjCMethodDecl>(clangDecl)) {
Expand All @@ -3572,7 +3581,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
clangAddr = getAddrOfClangGlobalDecl(globalDecl, forDefinition);
}

if (auto ctor = dyn_cast<clang::CXXConstructorDecl>(clangDecl)) {
if (cxxCtor) {
Signature signature = getSignature(f->getLoweredFunctionType());

// The thunk has private linkage, so it doesn't need to have a predictable
Expand All @@ -3582,7 +3591,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
stream << "__swift_cxx_ctor";
entity.mangle(stream);

clangAddr = emitCXXConstructorThunkIfNeeded(*this, signature, ctor, name,
clangAddr = emitCXXConstructorThunkIfNeeded(*this, signature, cxxCtor, name,
clangAddr);
}
}
Expand Down
7 changes: 5 additions & 2 deletions test/Interop/Cxx/class/constructors-irgen-windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import Constructors
import TypeClassification

public func createHasVirtualBase() -> HasVirtualBase {
// MICROSOFT_X64: define dllexport swiftcc void @"$s7MySwift20createHasVirtualBaseSo0{{bcD0VyF|deF0VyF}}"(ptr noalias sret({{.*}}) %0)
// MICROSOFT_X64: define dllexport swiftcc void @"$s7MySwift20createHasVirtualBaseSo0{{bcD0VyF|deF0VyF}}"(ptr noalias sret({{.*}}) [[V0:%.*]])
// MICROSOFT_X64-NOT: define
// Note `this` return type and implicit "most derived" argument.
// MICROSOFT_X64: call ptr @"??0HasVirtualBase@@QEAA@UArgType@@@Z"(ptr %{{[0-9]+}}, i32 %{{[0-9]+}}, i32 1)
// MICROSOFT_X64: [[V1:%.*]] = alloca %{{.*}}, align 8
// MICROSOFT_X64: call ptr @"??0HasVirtualBase@@QEAA@UArgType@@@Z"(ptr [[V1]], i32 %{{[0-9]+}}, i32 1)
// MICROSOFT_X64: call ptr @"??0HasVirtualBase@@QEAA@UArgType@@@Z"(ptr [[V0]], i32 %{{[0-9]+}}, i32 1)
let _ : HasVirtualBase = HasVirtualBase(ArgType())
return HasVirtualBase(ArgType())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xcc -fno-rtti -O)
//
// REQUIRES: executable_test
// XFAIL: OS=windows-msvc

import CustomDestructor
import StdlibUnittest
Expand Down