Skip to content
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
113 changes: 4 additions & 109 deletions clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1410,112 +1410,6 @@ static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF,
return true;
}

/// Get or create the MSVC-compatible __global_delete wrapper function.
///
/// Destructor helpers call __global_delete instead of ::operator delete
/// directly. If this TU contains a ::delete expression (or a dllexport class
/// whose deleting destructor takes the global-delete path), a real forwarding
/// body is emitted at end-of-file. If ::delete is never used anywhere in the
/// program, then no definition will exist and the `/ALTERNATENAME` linker
/// directive will cause the linker to use __empty_global_delete as the
/// definition. __empty_global_delete is never expected to actually be called,
/// hence it is a trap function (a deliberate deviation from MSVC, whose empty
/// is a no-op).
///
/// Array delete[] uses a parallel __global_array_delete wrapper, matching
/// MSVC. The scalar and array wrappers of a given signature share a single
/// __empty_global_delete fallback.
static llvm::Constant *
getOrCreateMSVCGlobalDeleteWrapper(CodeGenModule &CGM,
const FunctionDecl *GlobOD) {
assert(CGM.getTarget().getCXXABI().isMicrosoft() &&
"__global_delete wrapper is only used with the Microsoft ABI");
llvm::Module &M = CGM.getModule();
llvm::LLVMContext &LLVMCtx = M.getContext();

llvm::Constant *GlobDeleteCallee = CGM.GetAddrOfFunction(GlobOD);
auto *GlobDeleteFn = cast<llvm::Function>(GlobDeleteCallee);
llvm::FunctionType *FnTy = GlobDeleteFn->getFunctionType();

// Derive the wrapper and empty-fallback mangled names. MSVC uses distinct
// wrapper names for scalar vs array global delete, but a single shared empty
// fallback per signature:
// Global ::operator delete mangling: ??3@<signature>
// -> wrapper ?__global_delete@@<signature>
// Global ::operator delete[] mangling: ??_V@<signature>
// -> wrapper ?__global_array_delete@@<signature>
// shared fallback: ?__empty_global_delete@@<signature>
StringRef GlobDeleteMangledName = GlobDeleteFn->getName();
StringRef Signature;
const char *WrapperBase;
if (GlobDeleteMangledName.starts_with("??3@")) {
Signature = GlobDeleteMangledName.substr(4);
WrapperBase = "?__global_delete@@";
} else if (GlobDeleteMangledName.starts_with("??_V@")) {
Signature = GlobDeleteMangledName.substr(5);
WrapperBase = "?__global_array_delete@@";
} else {
llvm_unreachable("unexpected global operator delete mangling");
}

std::string GlobalDeleteName = (WrapperBase + Signature).str();
std::string EmptyGlobalDeleteName =
("?__empty_global_delete@@" + Signature).str();

// Only set up the wrapper once per module.
if (llvm::Function *Existing = M.getFunction(GlobalDeleteName))
return Existing;

// Create the shared __empty_global_delete fallback if it doesn't already
// exist. The scalar and array wrappers of a given signature share one empty
// (matching MSVC, whose weak externals both point at a single
// __empty_global_delete). The body traps: this path is unreachable at
// runtime when ::delete is never used (a deliberate deviation from MSVC,
// whose empty is a no-op; see the doc comment above).
llvm::Function *EmptyFn = M.getFunction(EmptyGlobalDeleteName);
if (!EmptyFn) {
EmptyFn = llvm::Function::Create(
FnTy, llvm::GlobalValue::LinkOnceODRLinkage, EmptyGlobalDeleteName, &M);
EmptyFn->setComdat(M.getOrInsertComdat(EmptyGlobalDeleteName));
EmptyFn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
CGM.SetLLVMFunctionAttributes(
GlobalDecl(GlobOD),
CGM.getTypes().arrangeGlobalDeclaration(GlobalDecl(GlobOD)), EmptyFn,
/*IsThunk=*/false);
CGM.SetLLVMFunctionAttributesForDefinition(GlobOD, EmptyFn);
CGM.getTargetCodeGenInfo().setTargetAttributes(GlobOD, EmptyFn, CGM);
auto *BB = llvm::BasicBlock::Create(LLVMCtx, "", EmptyFn);
llvm::Function *TrapFn =
llvm::Intrinsic::getOrInsertDeclaration(&M, llvm::Intrinsic::trap);
auto *TrapCall = llvm::CallInst::Create(TrapFn, {}, "", BB);
TrapCall->setDoesNotReturn();
TrapCall->setDoesNotThrow();
new llvm::UnreachableInst(LLVMCtx, BB);

// Nothing directly uses the empty other than the /alternatename directive,
// so explicitly mark it as used.
appendToUsed(M, {EmptyFn});
}

// Emit /ALTERNATENAME linker directive: if this wrapper isn't provided,
// fall back to the trapping __empty_global_delete.
std::string AltOption =
"/alternatename:" + GlobalDeleteName + "=" + EmptyGlobalDeleteName;
auto *AltMD =
llvm::MDNode::get(LLVMCtx, {llvm::MDString::get(LLVMCtx, AltOption)});
M.getOrInsertNamedMetadata("llvm.linker.options")->addOperand(AltMD);

// Return the __global_delete wrapper function to call.
auto GlobalDeleteCallee = M.getOrInsertFunction(GlobalDeleteName, FnTy);
auto *GlobalDeleteFn = cast<llvm::Function>(GlobalDeleteCallee.getCallee());

// Register this variant so we can emit a real forwarding body at end-of-TU
// if this TU contains any direct use of global ::operator delete.
CGM.addPendingGlobalDelete(GlobalDeleteFn, GlobOD);

return GlobalDeleteFn;
}

static void EmitConditionalArrayDtorCall(const CXXDestructorDecl *DD,
CodeGenFunction &CGF,
llvm::Value *ShouldDeleteCondition) {
Expand Down Expand Up @@ -1602,8 +1496,9 @@ static void EmitConditionalArrayDtorCall(const CXXDestructorDecl *DD,
// Use __global_delete wrapper instead of directly calling
// ::operator delete to match MSVC's behavior. See the doc comment on
// getOrCreateMSVCGlobalDeleteWrapper for details.
llvm::Constant *GlobalDeleteWrapper = getOrCreateMSVCGlobalDeleteWrapper(
CGF.CGM, Dtor->getGlobalArrayOperatorDelete());
llvm::Constant *GlobalDeleteWrapper =
CGF.CGM.getOrCreateMSVCGlobalDeleteWrapper(
Dtor->getGlobalArrayOperatorDelete());
// For dllexport classes, emit forwarding bodies since the dtor is
// exported and another TU may not provide the forwarding body.
if (Dtor->hasAttr<DLLExportAttr>())
Expand Down Expand Up @@ -1870,7 +1765,7 @@ void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF,
// ::operator delete to match MSVC's behavior. See the doc comment on
// getOrCreateMSVCGlobalDeleteWrapper for details.
llvm::Constant *GlobalDeleteWrapper =
getOrCreateMSVCGlobalDeleteWrapper(CGF.CGM, GlobOD);
CGF.CGM.getOrCreateMSVCGlobalDeleteWrapper(GlobOD);
// For dllexport classes, emit forwarding bodies since the dtor is
// exported and another TU may not provide the forwarding body.
if (Dtor->hasAttr<DLLExportAttr>())
Expand Down
14 changes: 13 additions & 1 deletion clang/lib/CodeGen/CGExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,8 +2108,20 @@ void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
// operator delete are both irrelevant to the trigger.
if (E->isGlobalDelete() && CGM.getTarget().getCXXABI().isMicrosoft()) {
const CXXRecordDecl *RD = E->getDestroyedType()->getAsCXXRecordDecl();
if (RD && RD->hasDefinition() && !RD->hasTrivialDestructor())
if (RD && RD->hasDefinition() && !RD->hasTrivialDestructor()) {
CGM.noteDirectGlobalDelete();
// Ensure a __global_delete wrapper (and thus a strong forwarding body)
// is emitted in THIS TU for the resolved global ::operator delete, even
// when no vector deleting destructor here references it. Without this, a
// TU that only does ::delete (with the deleting destructor defined in
// another TU) would emit no forwarder, leaving the wrapper bound to the
// trapping empty fallback and crashing at runtime.
const FunctionDecl *OD = E->getOperatorDelete();
assert(!isa<CXXMethodDecl>(OD) &&
"global ::delete should resolve to a namespace-scope "
"operator delete");
CGM.getOrCreateMSVCGlobalDeleteWrapper(OD);
}
}

// Null check the pointer.
Expand Down
140 changes: 127 additions & 13 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8906,45 +8906,159 @@ void CodeGenModule::requireVectorDestructorDefinition(const CXXRecordDecl *RD) {
}

void CodeGenModule::addPendingGlobalDelete(
llvm::Function *GlobalDeleteFn, const FunctionDecl *OperatorDeleteFD) {
llvm::GlobalAlias *GlobalDeleteAlias,
const FunctionDecl *OperatorDeleteFD) {
// insert() is a no-op if this wrapper has already been recorded, keeping the
// first FunctionDecl seen for it.
PendingMSVCGlobalDeletes.insert({GlobalDeleteFn, OperatorDeleteFD});
PendingMSVCGlobalDeletes.insert({GlobalDeleteAlias, OperatorDeleteFD});
}

void CodeGenModule::noteDirectGlobalDelete() { HasDirectGlobalDelete = true; }

/// Get or create the MSVC-compatible __global_delete wrapper function.
///
/// Destructor helpers call __global_delete instead of ::operator delete
/// directly. If this TU contains a ::delete expression (or a dllexport class
/// whose deleting destructor takes the global-delete path), a real forwarding
/// body is emitted at end-of-file. If ::delete is never used anywhere in the
/// program, then no forwarding body is emitted and the wrapper defaults to a
/// weak alias to __empty_global_delete. __empty_global_delete is never
/// expected to actually be called, hence it is a trap function (a deliberate
/// deviation from MSVC, whose empty is a no-op).
///
/// Array delete[] uses a parallel __global_array_delete wrapper, matching
/// MSVC. The scalar and array wrappers of a given signature share a single
/// __empty_global_delete fallback.
llvm::Constant *
CodeGenModule::getOrCreateMSVCGlobalDeleteWrapper(const FunctionDecl *GlobOD) {
assert(getTarget().getCXXABI().isMicrosoft() &&
"__global_delete wrapper is only used with the Microsoft ABI");
llvm::Module &M = getModule();
llvm::LLVMContext &LLVMCtx = M.getContext();

llvm::Constant *GlobDeleteCallee = GetAddrOfFunction(GlobOD);
auto *GlobDeleteFn = cast<llvm::Function>(GlobDeleteCallee);
llvm::FunctionType *FnTy = GlobDeleteFn->getFunctionType();

// Derive the wrapper and empty-fallback mangled names. MSVC uses distinct
// wrapper names for scalar vs array global delete, but a single shared empty
// fallback per signature:
// Global ::operator delete mangling: ??3@<signature>
// -> wrapper ?__global_delete@@<signature>
// Global ::operator delete[] mangling: ??_V@<signature>
// -> wrapper ?__global_array_delete@@<signature>
// shared fallback: ?__empty_global_delete@@<signature>
StringRef GlobDeleteMangledName = GlobDeleteFn->getName();
StringRef Signature;
const char *WrapperBase;
if (GlobDeleteMangledName.starts_with("??3@")) {
Signature = GlobDeleteMangledName.substr(4);
WrapperBase = "?__global_delete@@";
} else if (GlobDeleteMangledName.starts_with("??_V@")) {
Signature = GlobDeleteMangledName.substr(5);
WrapperBase = "?__global_array_delete@@";
} else {
llvm_unreachable("unexpected global operator delete mangling");
}

std::string GlobalDeleteName = (WrapperBase + Signature).str();
std::string EmptyGlobalDeleteName =
("?__empty_global_delete@@" + Signature).str();

// Only set up the wrapper once per module. The wrapper may be a weak alias
// (the default fallback) or, once replaced, a real forwarding function.
if (llvm::GlobalValue *Existing = M.getNamedValue(GlobalDeleteName))
return Existing;

// Create the shared __empty_global_delete fallback if it doesn't already
// exist. The scalar and array wrappers of a given signature share one empty
// (matching MSVC, whose weak externals both point at a single
// __empty_global_delete). The body traps: this path is unreachable at
// runtime when ::delete is never used (a deliberate deviation from MSVC,
// whose empty is a no-op; see the doc comment above).
llvm::Function *EmptyFn = M.getFunction(EmptyGlobalDeleteName);
if (!EmptyFn) {
EmptyFn = llvm::Function::Create(
FnTy, llvm::GlobalValue::LinkOnceODRLinkage, EmptyGlobalDeleteName, &M);
EmptyFn->setComdat(M.getOrInsertComdat(EmptyGlobalDeleteName));
EmptyFn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
SetLLVMFunctionAttributes(
GlobalDecl(GlobOD),
getTypes().arrangeGlobalDeclaration(GlobalDecl(GlobOD)), EmptyFn,
/*IsThunk=*/false);
SetLLVMFunctionAttributesForDefinition(GlobOD, EmptyFn);
getTargetCodeGenInfo().setTargetAttributes(GlobOD, EmptyFn, *this);
auto *BB = llvm::BasicBlock::Create(LLVMCtx, "", EmptyFn);
llvm::Function *TrapFn =
llvm::Intrinsic::getOrInsertDeclaration(&M, llvm::Intrinsic::trap);
auto *TrapCall = llvm::CallInst::Create(TrapFn, {}, "", BB);
TrapCall->setDoesNotReturn();
TrapCall->setDoesNotThrow();
new llvm::UnreachableInst(LLVMCtx, BB);

// The empty is referenced only by the wrapper's weak alias. When this TU
// uses ::delete that alias is replaced by a real forwarding body, leaving
// the empty otherwise unreferenced, so explicitly mark it used to ensure
// it is always emitted (matching MSVC).
appendToUsed(M, {EmptyFn});
}

// The wrapper defaults to a weak alias to the trapping __empty_global_delete
// fallback (see the doc comment above for why this is a weak alias rather
// than an /alternatename directive). If this TU directly uses global
// ::operator delete, the alias is replaced with a real forwarding body in
// emitGlobalDeleteForwardingBodies().
auto *GlobalDeleteAlias = llvm::GlobalAlias::create(
FnTy, GlobDeleteFn->getAddressSpace(), llvm::GlobalValue::WeakAnyLinkage,
GlobalDeleteName, EmptyFn, &M);

// Register this variant so we can replace the alias with a real forwarding
// body at end-of-TU if this TU contains any direct use of global
// ::operator delete.
addPendingGlobalDelete(GlobalDeleteAlias, GlobOD);

return GlobalDeleteAlias;
}

void CodeGenModule::emitGlobalDeleteForwardingBodies() {
// MSVC-compatible __global_delete forwarding bodies.
//
// Destructor helpers call __global_delete but they are only needed if there
// is a direct use of ::operator delete. When this TU contains a ::delete
// expression (or a dllexport deleting destructor that takes the global-delete
// path), we know ::operator delete must exist, so we emit a real
// __global_delete definition that forwards to it.
// path), we know ::operator delete must exist, so we replace the wrapper's
// weak alias-to-empty fallback with a real __global_delete definition that
// forwards to it.
if (!HasDirectGlobalDelete)
return;

for (const auto &Entry : PendingMSVCGlobalDeletes) {
llvm::Function *GlobDelFn = Entry.first;
if (!GlobDelFn->isDeclaration())
continue;

llvm::GlobalAlias *Alias = Entry.first;
const FunctionDecl *OperatorDeleteFD = Entry.second;
llvm::Constant *RealDeleteFn = GetAddrOfFunction(OperatorDeleteFD);

// Create the forwarding body: call ::operator delete with all args.
// Create the strong forwarding function. Use LinkOnceODR so multiple TUs
// can emit this without conflicts.
auto *FnTy = cast<llvm::FunctionType>(Alias->getValueType());
auto *GlobDelFn =
llvm::Function::Create(FnTy, llvm::GlobalValue::LinkOnceODRLinkage,
Alias->getAddressSpace(), "", &getModule());

// Emit the forwarding body: call ::operator delete with all args.
auto *BB =
llvm::BasicBlock::Create(getModule().getContext(), "", GlobDelFn);
llvm::SmallVector<llvm::Value *, 4> Args;
for (auto &Arg : GlobDelFn->args())
Args.push_back(&Arg);
llvm::CallInst::Create(GlobDelFn->getFunctionType(), RealDeleteFn, Args, "",
BB);
llvm::CallInst::Create(FnTy, RealDeleteFn, Args, "", BB);
llvm::ReturnInst::Create(getModule().getContext(), BB);

// Use LinkOnceODR so multiple TUs can emit this without conflicts.
GlobDelFn->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
// Replace the weak alias fallback with the real forwarding body, taking
// over its name.
Alias->replaceAllUsesWith(GlobDelFn);
GlobDelFn->takeName(Alias);
Alias->eraseFromParent();

GlobDelFn->setComdat(getModule().getOrInsertComdat(GlobDelFn->getName()));
SetLLVMFunctionAttributes(
GlobalDecl(OperatorDeleteFD),
Expand Down
13 changes: 10 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ class CodeGenModule : public CodeGenTypeCache {
llvm::SmallPtrSet<const CXXRecordDecl *, 16> RequireVectorDeletingDtor;

/// Pending MSVC __global_delete variants that may need forwarding bodies.
/// Maps each __global_delete wrapper function to the corresponding global
/// Maps each __global_delete wrapper alias to the corresponding global
/// ::operator delete FunctionDecl, in insertion order.
llvm::MapVector<llvm::Function *, const FunctionDecl *>
llvm::MapVector<llvm::GlobalAlias *, const FunctionDecl *>
PendingMSVCGlobalDeletes;

/// Whether this TU contains a direct use of global ::operator delete
Expand Down Expand Up @@ -1654,9 +1654,16 @@ class CodeGenModule : public CodeGenTypeCache {
void requireVectorDestructorDefinition(const CXXRecordDecl *RD);

/// Record a pending __global_delete variant that may need a forwarding body.
void addPendingGlobalDelete(llvm::Function *GlobalDeleteFn,
void addPendingGlobalDelete(llvm::GlobalAlias *GlobalDeleteAlias,
const FunctionDecl *OperatorDeleteFD);

/// Get or create the MSVC-compatible __global_delete wrapper for the given
/// global ::operator delete, registering it as a pending variant so a
/// forwarding body can be emitted if this TU directly uses global
/// ::operator delete.
llvm::Constant *
getOrCreateMSVCGlobalDeleteWrapper(const FunctionDecl *GlobOD);

/// Note that global ::operator delete is directly used in this TU.
void noteDirectGlobalDelete();

Expand Down
Loading
Loading