-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang] Use tighter lifetime bounds for C temporary arguments #170518
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
Open
ilovepi
wants to merge
6
commits into
main
Choose a base branch
from
users/ilovepi/clang-lifetimes-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−25
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2513476
[clang] Use tighter lifetime bounds for C temporary arguments
ilovepi 8be2cad
Update comment to be more accurate
ilovepi 78523ec
Update invoke test for tighter lifetimes
ilovepi 5d714bd
Add cleanups for the error path and add more tests
ilovepi aa32d94
Avoid checking NoLifetimeMarkersForTemporaries
ilovepi 2560fa6
Fix test checks
ilovepi 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog %s -verify=x86-precise | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog -sloppy-temporary-lifetimes %s -verify=x86-sloppy | ||
|
|
||
| // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog %s -verify=aarch64-precise | ||
| // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog -sloppy-temporary-lifetimes %s -verify=aarch64-sloppy | ||
|
|
||
| // RUN: %clang_cc1 -triple riscv64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog %s -verify=riscv-precise | ||
| // RUN: %clang_cc1 -triple riscv64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog -sloppy-temporary-lifetimes %s -verify=riscv-sloppy | ||
|
|
||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog %s -verify=x86-precise -xc++ | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog -sloppy-temporary-lifetimes %s -verify=x86-sloppy -xc++ | ||
|
|
||
| // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog %s -verify=aarch64-precise -xc++ | ||
| // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog -sloppy-temporary-lifetimes %s -verify=aarch64-sloppy -xc++ | ||
|
|
||
| // RUN: %clang_cc1 -triple riscv64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog %s -verify=riscv-precise -xc++ | ||
| // RUN: %clang_cc1 -triple riscv64-unknown-linux-gnu -O1 -emit-codegen-only -Rpass-analysis=prologepilog -sloppy-temporary-lifetimes %s -verify=riscv-sloppy -xc++ | ||
|
|
||
|
|
||
| typedef struct { char x[32]; } A; | ||
| typedef struct { char *w, *x, *y, *z; } B; | ||
|
|
||
| void useA(A); | ||
| void useB(B); | ||
| A genA(void); | ||
| B genB(void); | ||
|
|
||
| void t1(int c) { | ||
| // x86-precise-remark@-1 {{40 stack bytes}} | ||
| // x86-sloppy-remark@-2 {{72 stack bytes}} | ||
| // aarch64-precise-remark@-3 {{48 stack bytes}} | ||
| // aarch64-sloppy-remark@-4 {{80 stack bytes}} | ||
| // riscv-precise-remark@-5 {{48 stack bytes}} | ||
| // riscv-sloppy-remark@-6 {{80 stack bytes}} | ||
|
|
||
| if (c) | ||
| useA(genA()); | ||
| else | ||
| useA(genA()); | ||
| } | ||
|
|
||
| void t2(void) { | ||
| // x86-precise-remark@-1 {{40 stack bytes}} | ||
| // x86-sloppy-remark@-2 {{72 stack bytes}} | ||
| // aarch64-precise-remark@-3 {{48 stack bytes}} | ||
| // aarch64-sloppy-remark@-4 {{80 stack bytes}} | ||
| // riscv-precise-remark@-5 {{48 stack bytes}} | ||
| // riscv-sloppy-remark@-6 {{80 stack bytes}} | ||
|
|
||
| useA(genA()); | ||
| useA(genA()); | ||
| } | ||
|
|
||
| void t3(void) { | ||
| // x86-precise-remark@-1 {{40 stack bytes}} | ||
| // x86-sloppy-remark@-2 {{72 stack bytes}} | ||
| // aarch64-precise-remark@-3 {{48 stack bytes}} | ||
| // aarch64-sloppy-remark@-4 {{80 stack bytes}} | ||
| // riscv-precise-remark@-5 {{48 stack bytes}} | ||
| // riscv-sloppy-remark@-6 {{80 stack bytes}} | ||
|
|
||
| useB(genB()); | ||
| useB(genB()); | ||
| } | ||
|
|
||
| #ifdef __cplusplus | ||
| struct C { | ||
| char x[24]; | ||
| char *ptr; | ||
| ~C() {}; | ||
| }; | ||
|
|
||
| void useC(C); | ||
| C genC(void); | ||
|
|
||
| // This case works in C++, since its AST is structured slightly differently | ||
| // than it is in C (CompundStmt/ExprWithCleanup/CallExpr vs CompundStmt/CallExpr). | ||
| void t4() { | ||
| // x86-precise-remark@-1 {{40 stack bytes}} | ||
| // x86-sloppy-remark@-2 {{72 stack bytes}} | ||
| // aarch64-precise-remark@-3 {{48 stack bytes}} | ||
| // aarch64-sloppy-remark@-4 {{80 stack bytes}} | ||
| // riscv-precise-remark@-5 {{48 stack bytes}} | ||
| // riscv-sloppy-remark@-6 {{80 stack bytes}} | ||
|
|
||
| useC(genC()); | ||
| useC(genC()); | ||
| } | ||
| #endif |
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 think this still doesn't work quite right.
Consider the case where you have two calls which can throw in the expression. Something like
f2(f1(X{})). If f1 throws an exception, you correctly end the lifetime, but if f2 throws an exception, you end up calling lifetime.end on an alloca where the lifetime already ended.You can probably fudge this by deactivating the cleanup after the call. But it would be cleaner to introduce a new scope for the call.
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.
Hmm, yeah, that's a good point. Thanks for the suggestion. That sounds promising.