-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
codegen: Copy to an alloca when the argument is neither by-val nor by-move for indirect pointer. #155343
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
dianqk
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
dianqk:indirect-by-ref
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.
+205
−64
Open
codegen: Copy to an alloca when the argument is neither by-val nor by-move for indirect pointer. #155343
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,89 @@ | ||
| //! Regression test for issue <https://github.com/rust-lang/rust/issues/155241>. | ||
| //! Arguments passed indirectly via a hidden pointer must be copied to an alloca, | ||
| //! except for by-val or by-move. | ||
| //@ compile-flags: -Cno-prepopulate-passes -Copt-level=3 | ||
| //@ only-x86_64-unknown-linux-gnu | ||
|
|
||
| #![crate_type = "lib"] | ||
| #![feature(fn_traits, stmt_expr_attributes)] | ||
| #![expect(unused)] | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| struct Thing(usize, usize, usize); | ||
|
|
||
| // The argument of the second call is a by-move argument. | ||
|
|
||
| // CHECK-LABEL: @normal | ||
| // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[normal_V1:%.*]], ptr align 8 %value, i64 24, i1 false) | ||
| // CHECK: call void @opaque(ptr{{.*}} [[normal_V1]]) | ||
| // CHECK: call void @opaque(ptr{{.*}} %value) | ||
| // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[normal_V3:%.*]], ptr align 8 @anon{{.*}}, i64 24, i1 false) | ||
| // CHECK: call void @opaque(ptr{{.*}} [[normal_V3]]) | ||
| #[unsafe(no_mangle)] | ||
| pub fn normal() { | ||
| #[inline(never)] | ||
| #[unsafe(no_mangle)] | ||
| fn opaque(mut thing: Thing) { | ||
| thing.0 = 1; | ||
| } | ||
| let value = Thing(0, 0, 0); | ||
| opaque(value); | ||
| opaque(value); | ||
| const VALUE: Thing = Thing(0, 0, 0); | ||
| opaque(VALUE); | ||
| } | ||
|
|
||
| // The argument of the second call is a by-move argument. | ||
|
|
||
| // CHECK-LABEL: @untupled | ||
| // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[untupled_V1:%.*]], ptr align 8 %value, i64 24, i1 false) | ||
| // CHECK: call indirect_bycopy_bymove_byval::untupled::{closure#0} | ||
| // CHECK-NEXT: call void @{{.*}}(ptr {{.*}}, ptr{{.*}} [[untupled_V1]]) | ||
| // CHECK: call indirect_bycopy_bymove_byval::untupled::{closure#1} | ||
| // CHECK-NEXT: call void @{{.*}}(ptr {{.*}}, ptr{{.*}} %value) | ||
| // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[untupled_V3:%.*]], ptr align 8 @anon{{.*}}, i64 24, i1 false) | ||
| // CHECK: call indirect_bycopy_bymove_byval::untupled::{closure#2} | ||
| // CHECK-NEXT: call void @{{.*}}(ptr {{.*}}, ptr{{.*}} [[untupled_V3]]) | ||
| #[unsafe(no_mangle)] | ||
| pub fn untupled() { | ||
| let value = (Thing(0, 0, 0),); | ||
| (#[inline(never)] | ||
| |mut thing: Thing| { | ||
| thing.0 = 1; | ||
| }) | ||
| .call(value); | ||
| (#[inline(never)] | ||
| |mut thing: Thing| { | ||
| thing.0 = 2; | ||
| }) | ||
| .call(value); | ||
| const VALUE: (Thing,) = (Thing(0, 0, 0),); | ||
| (#[inline(never)] | ||
| |mut thing: Thing| { | ||
| thing.0 = 3; | ||
| }) | ||
| .call(VALUE); | ||
| } | ||
|
|
||
| // All memcpy calls are redundant for byval. | ||
|
|
||
| // CHECK-LABEL: @byval | ||
| // CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} %value) | ||
| // CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} %value) | ||
| // CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} @anon{{.*}}) | ||
| #[unsafe(no_mangle)] | ||
| pub fn byval() { | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| struct Thing(usize, usize, usize); | ||
| #[inline(never)] | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn opaque_byval(mut thing: Thing) { | ||
| thing.0 = 1; | ||
| } | ||
| let value = Thing(0, 0, 0); | ||
| opaque_byval(value); | ||
| opaque_byval(value); | ||
| const VALUE: Thing = Thing(0, 0, 0); | ||
| opaque_byval(VALUE); | ||
| } |
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,57 @@ | ||
| //! Regression test for issue <https://github.com/rust-lang/rust/issues/155241>. | ||
| //@ run-pass | ||
| //@ revisions: noopt opt | ||
| //@[noopt] compile-flags: -C opt-level=0 | ||
| //@[opt] compile-flags: -C opt-level=3 | ||
|
|
||
| #![feature(fn_traits, stmt_expr_attributes)] | ||
| #![expect(unused)] | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| struct Thing { | ||
| x: usize, | ||
| y: usize, | ||
| z: usize, | ||
| } | ||
|
|
||
| #[inline(never)] | ||
| fn opt_0() { | ||
| let value = (Thing { x: 0, y: 0, z: 0 },); | ||
| (|mut thing: Thing| { | ||
| thing.z = 1; | ||
| }) | ||
| .call(value); | ||
| assert_eq!(value.0.z, 0); | ||
| } | ||
|
|
||
| #[inline(never)] | ||
| fn opt_3() { | ||
| fn with(f: impl FnOnce(Vec<usize>)) { | ||
| f(Vec::new()) | ||
| } | ||
| with(|mut v| v.resize(2, 1)); | ||
| with(|v| { | ||
| if v.len() != 0 { | ||
| unreachable!(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| #[inline(never)] | ||
| fn const_() { | ||
| const VALUE: (Thing,) = (Thing { x: 0, y: 0, z: 0 },); | ||
|
|
||
| (#[inline(never)] | ||
| |mut thing: Thing| { | ||
| thing.z = 1; | ||
| std::hint::black_box(&mut thing.z); | ||
| assert_eq!(thing.z, 1); | ||
| }) | ||
| .call(VALUE); | ||
| } | ||
|
|
||
| fn main() { | ||
| opt_0(); | ||
| opt_3(); | ||
| const_(); | ||
| } |
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.
A redundant memcpy is skipped here.
View changes since the review