-
Notifications
You must be signed in to change notification settings - Fork 18.1k
[Clang] Mark this pointer in destructors dead_on_return #166276
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
Changes from 3 commits
7a3dec4
6ec0952
1ca199a
98bcfbf
2df7522
45ad4dd
cefb84d
9d877f0
4d388d6
4c24acd
afb2e1b
ec1f242
752b685
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| #include "clang/AST/DeclCXX.h" | ||
| #include "clang/AST/DeclObjC.h" | ||
| #include "clang/Basic/CodeGenOptions.h" | ||
| #include "clang/Basic/LLVM.h" | ||
|
boomanaiden154 marked this conversation as resolved.
Outdated
|
||
| #include "clang/Basic/TargetInfo.h" | ||
| #include "clang/CodeGen/CGFunctionInfo.h" | ||
| #include "clang/CodeGen/SwiftCallingConv.h" | ||
|
|
@@ -2767,7 +2768,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, | |
| } | ||
|
|
||
| // Apply `nonnull`, `dereferenceable(N)` and `align N` to the `this` argument, | ||
| // unless this is a thunk function. | ||
| // unless this is a thunk function. Add dead_on_return to the `this` argument | ||
| // in base class destructors to aid in DSE. | ||
| // FIXME: fix this properly, https://reviews.llvm.org/D100388 | ||
| if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() && | ||
| !FI.arg_begin()->type->isVoidPointerType() && !IsThunk) { | ||
|
|
@@ -2800,6 +2802,19 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, | |
| .getAsAlign(); | ||
| Attrs.addAlignmentAttr(Alignment); | ||
|
|
||
| const auto *DD = dyn_cast_if_present<CXXDestructorDecl>( | ||
| CalleeInfo.getCalleeDecl().getDecl()); | ||
| if (DD && CodeGenOpts.MarkObjectsDeadAfterDestructors) { | ||
| const CXXRecordDecl *ClassDecl = | ||
| dyn_cast<CXXRecordDecl>(DD->getDeclContext()); | ||
| // TODO(boomanaiden154): We are being intentionally conservative here | ||
| // as we gain experience with this optimization. These checks should be | ||
| // removed once we have done further integration testing. | ||
| if (ClassDecl->getNumBases() == 0 && ClassDecl->getNumVBases() == 0) { | ||
|
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. Why do we have to limit this to only destructors of classes with no bases? Whatever the reason (caution, incremental change, etc), comments here would be appreciated.
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. Virtual base subobjects aren't dead after a base subobject destructor call, but yeah, I can't think of a reason to limit this because of non-virtual bases alone. And even virtual bases are dead after a complete-object destructor call.
Contributor
Author
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. For now, just to be conservative while we get implementation experience. I've added a TODO for myself to remove this after we do more testing.
boomanaiden154 marked this conversation as resolved.
Outdated
|
||
| Attrs.addAttribute(llvm::Attribute::DeadOnReturn); | ||
| } | ||
| } | ||
|
|
||
| ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // RUN: %clang_cc1 -x c++ -emit-llvm -triple x86_64-unknown-linux-gnu -o - %s | FileCheck --check-prefixes=CHECK,CHECK-ENABLED %s | ||
| // RUN: %clang_cc1 -x c++ -emit-llvm -triple x86_64-unknown-linux-gnu -fmark-objects-dead-after-destructors -o - %s | FileCheck --check-prefixes=CHECK,CHECK-ENABLED %s | ||
| // RUN: %clang_cc1 -x c++ -emit-llvm -triple x86_64-unknown-linux-gnu -fno-mark-objects-dead-after-destructors -o - %s | FileCheck --check-prefixes=CHECK,CHECK-DISABLED %s | ||
|
|
||
| // CHECK: define linkonce_odr void @_ZN3barD2Ev(ptr noundef nonnull align 1 dereferenceable(1) %this) | ||
| // CHECK-ENABLED: define linkonce_odr void @_ZN3fooD2Ev(ptr dead_on_return noundef nonnull align 1 dereferenceable(1) %this) | ||
| // CHECK-DISABLED: define linkonce_odr void @_ZN3fooD2Ev(ptr noundef nonnull align 1 dereferenceable(1) %this) | ||
|
|
||
| int dummyFunction(); | ||
|
|
||
| class foo { | ||
| public: | ||
| ~foo() { | ||
| dummyFunction(); | ||
| } | ||
| }; | ||
|
|
||
| class bar : foo { | ||
| public: | ||
| ~bar() { | ||
| dummyFunction(); | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| bar baz; | ||
| return 0; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.