-
Notifications
You must be signed in to change notification settings - Fork 18.8k
[clang][win] MSVC-compat: Use __global_delete wrapper in deleting destructors instead of directly referencing ::operator delete
#188372
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
041d191
[clang][win] MSVC-compat: Use __global_delete wrapper in deleting des…
dpaoliello f45f78f
Add comment to clarify
dpaoliello e67ed9a
Handle vector deletes correctly, use a map of functions instead of na…
dpaoliello 6d8e27a
Address PR feedback
dpaoliello 003e2b8
Add new tests
dpaoliello 52cf685
Apply PR suggestion
dpaoliello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1140,6 +1140,7 @@ void CodeGenModule::Release() { | |
| applyReplacements(); | ||
| emitMultiVersionFunctions(); | ||
| emitPFPFieldsWithEvaluatedOffset(); | ||
| emitGlobalDeleteForwardingBodies(); | ||
|
|
||
| if (Context.getLangOpts().IncrementalExtensions && | ||
| GlobalTopLevelStmtBlockInFlight.first) { | ||
|
|
@@ -8910,3 +8911,54 @@ void CodeGenModule::requireVectorDestructorDefinition(const CXXRecordDecl *RD) { | |
| // even if destructor is only declared. | ||
| addDeferredDeclToEmit(VectorDtorGD); | ||
| } | ||
|
|
||
| void CodeGenModule::addPendingGlobalDelete( | ||
| llvm::Function *GlobalDeleteFn, 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}); | ||
| } | ||
|
|
||
| void CodeGenModule::noteDirectGlobalDelete() { HasDirectGlobalDelete = true; } | ||
|
|
||
| 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. | ||
| if (!HasDirectGlobalDelete) | ||
| return; | ||
|
|
||
| for (const auto &Entry : PendingMSVCGlobalDeletes) { | ||
| llvm::Function *GlobDelFn = Entry.first; | ||
| if (!GlobDelFn->isDeclaration()) | ||
| continue; | ||
|
|
||
| const FunctionDecl *OperatorDeleteFD = Entry.second; | ||
| llvm::Constant *RealDeleteFn = GetAddrOfFunction(OperatorDeleteFD); | ||
|
|
||
| // Create 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::ReturnInst::Create(getModule().getContext(), BB); | ||
|
|
||
| // Use LinkOnceODR so multiple TUs can emit this without conflicts. | ||
| GlobDelFn->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); | ||
| GlobDelFn->setComdat(getModule().getOrInsertComdat(GlobDelFn->getName())); | ||
| SetLLVMFunctionAttributes( | ||
| GlobalDecl(OperatorDeleteFD), | ||
| getTypes().arrangeGlobalDeclaration(GlobalDecl(OperatorDeleteFD)), | ||
| GlobDelFn, /*IsThunk=*/false); | ||
| SetLLVMFunctionAttributesForDefinition(OperatorDeleteFD, GlobDelFn); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Same issue with attributes here.) |
||
| getTargetCodeGenInfo().setTargetAttributes(OperatorDeleteFD, GlobDelFn, | ||
| *this); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how much practical effect it has here, but the general pattern is to call CGM.SetLLVMFunctionAttributes(), then CGM.SetLLVMFunctionAttributesForDefinition(), then getTargetCodeGenInfo().setTargetAttributes(). (We should really clean this up at some point...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.