-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
avoid phi node for pointers flowing into Vec appends #130998
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
+50
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,30 @@ | ||
| //@ compile-flags: -O -Zmerge-functions=disabled | ||
| //@ min-llvm-version: 21 | ||
| #![crate_type = "lib"] | ||
|
|
||
| //! Check that a temporary intermediate allocations can eliminated and replaced | ||
| //! with memcpy forwarding. | ||
| //! This requires Vec code to be structured in a way that avoids phi nodes from the | ||
| //! zero-capacity length flowing into the memcpy arguments. | ||
|
|
||
| // CHECK-LABEL: @vec_append_with_temp_alloc | ||
| #[no_mangle] | ||
| pub fn vec_append_with_temp_alloc(dst: &mut Vec<u8>, src: &[u8]) { | ||
| // CHECK-NOT: call void @llvm.memcpy | ||
| // CHECK: call void @llvm.memcpy.{{.*}}%dst.i{{.*}}%src.0 | ||
| // CHECK-NOT: call void @llvm.memcpy | ||
| let temp = src.to_vec(); | ||
| dst.extend(&temp); | ||
| // CHECK: ret | ||
| } | ||
|
|
||
| // CHECK-LABEL: @string_append_with_temp_alloc | ||
| #[no_mangle] | ||
| pub fn string_append_with_temp_alloc(dst: &mut String, src: &str) { | ||
| // CHECK-NOT: call void @llvm.memcpy | ||
| // CHECK: call void @llvm.memcpy.{{.*}}%dst.i{{.*}}%src.0 | ||
| // CHECK-NOT: call void @llvm.memcpy | ||
| let temp = src.to_string(); | ||
| dst.push_str(&temp); | ||
| // CHECK: ret | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
It would be good to have some sort of justification for why this is correct. This operation destroys the provenance given to it, why is it okay to consider that non-capturing?
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.
My understanding from the zulip conversation is that since the allocation ends there's nothing further that the callee could do that would be relevant to aliasing rules of this allocation.
Whether or not the allocator touches the memory afterwards (e.g. for zeroing), isn't relevant to this side of the allocator boundary.
I assume we need to tell LLVMs both things separately because they just happen to be tracked separately.
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.
@nikic just to check the obvious, adding this attribute wouldn't allow
to be reordered to
Being an allocator method already inhibits that reordering.
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.
"Does not capture provenance" means "if the function call stashes the pointer somewhere, accessing that pointer after the function returns is UB". It does not limit what can be done with the pointer within the function itself.
FWIW, the C free function is marked
captures(none).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.
Any reason why we shouldn't match what C is doing then?
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.
My thinking was that if GlobalAlloc is used, it may inspect the address of the pointer, so using
captures(none)may not be correct.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.
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.
But C allocators would also have to look at the address (e.g. comparing it to memory pools). Is the difference that those are assumed to be compiled separately?
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.
What we care about here are effects that are observable outside the allocator. Specifically, what I had in mind is
GlobalAlloc::dealloc()doing something like print "I'm now freeing pointer 0xdeadbeef".