-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Copy byval argument to alloca if alignment is insufficient #122212
Merged
Merged
Changes from all commits
Commits
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 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 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 |
---|---|---|
|
@@ -367,29 +367,45 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( | |
} | ||
} | ||
|
||
if arg.is_sized_indirect() { | ||
// Don't copy an indirect argument to an alloca, the caller | ||
// already put it in a temporary alloca and gave it up. | ||
// FIXME: lifetimes | ||
let llarg = bx.get_param(llarg_idx); | ||
llarg_idx += 1; | ||
LocalRef::Place(PlaceRef::new_sized(llarg, arg.layout)) | ||
} else if arg.is_unsized_indirect() { | ||
// As the storage for the indirect argument lives during | ||
// the whole function call, we just copy the fat pointer. | ||
let llarg = bx.get_param(llarg_idx); | ||
llarg_idx += 1; | ||
let llextra = bx.get_param(llarg_idx); | ||
llarg_idx += 1; | ||
let indirect_operand = OperandValue::Pair(llarg, llextra); | ||
|
||
let tmp = PlaceRef::alloca_unsized_indirect(bx, arg.layout); | ||
indirect_operand.store(bx, tmp); | ||
LocalRef::UnsizedPlace(tmp) | ||
} else { | ||
let tmp = PlaceRef::alloca(bx, arg.layout); | ||
bx.store_fn_arg(arg, &mut llarg_idx, tmp); | ||
LocalRef::Place(tmp) | ||
match arg.mode { | ||
// Sized indirect arguments | ||
PassMode::Indirect { attrs, meta_attrs: None, on_stack: _ } => { | ||
// Don't copy an indirect argument to an alloca, the caller already put it | ||
// in a temporary alloca and gave it up. | ||
// FIXME: lifetimes | ||
if let Some(pointee_align) = attrs.pointee_align | ||
&& pointee_align < arg.layout.align.abi | ||
{ | ||
// ...unless the argument is underaligned, then we need to copy it to | ||
// a higher-aligned alloca. | ||
let tmp = PlaceRef::alloca(bx, arg.layout); | ||
bx.store_fn_arg(arg, &mut llarg_idx, tmp); | ||
LocalRef::Place(tmp) | ||
} else { | ||
Comment on lines
+376
to
+384
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. This is the only functional change in this file. |
||
let llarg = bx.get_param(llarg_idx); | ||
llarg_idx += 1; | ||
LocalRef::Place(PlaceRef::new_sized(llarg, arg.layout)) | ||
} | ||
} | ||
// Unsized indirect qrguments | ||
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => { | ||
// As the storage for the indirect argument lives during | ||
// the whole function call, we just copy the fat pointer. | ||
let llarg = bx.get_param(llarg_idx); | ||
llarg_idx += 1; | ||
let llextra = bx.get_param(llarg_idx); | ||
llarg_idx += 1; | ||
let indirect_operand = OperandValue::Pair(llarg, llextra); | ||
|
||
let tmp = PlaceRef::alloca_unsized_indirect(bx, arg.layout); | ||
indirect_operand.store(bx, tmp); | ||
LocalRef::UnsizedPlace(tmp) | ||
} | ||
_ => { | ||
let tmp = PlaceRef::alloca(bx, arg.layout); | ||
bx.store_fn_arg(arg, &mut llarg_idx, tmp); | ||
LocalRef::Place(tmp) | ||
} | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
This file contains 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 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,126 @@ | ||
// ignore-tidy-linelength | ||
//@ revisions:i686-linux x86_64-linux | ||
|
||
//@[i686-linux] compile-flags: --target i686-unknown-linux-gnu | ||
//@[i686-linux] needs-llvm-components: x86 | ||
//@[x86_64-linux] compile-flags: --target x86_64-unknown-linux-gnu | ||
//@[x86_64-linux] needs-llvm-components: x86 | ||
|
||
// Tests that we correctly copy arguments into allocas when the alignment of the byval argument | ||
// is different from the alignment of the Rust type. | ||
|
||
// For the following test cases: | ||
// All of the `*_decreases_alignment` functions should codegen to a direct call, since the | ||
// alignment is already sufficient. | ||
// All off the `*_increases_alignment` functions should copy the argument to an alloca | ||
// on i686-unknown-linux-gnu, since the alignment needs to be increased, and should codegen | ||
// to a direct call on x86_64-unknown-linux-gnu, where byval alignment matches Rust alignment. | ||
|
||
#![feature(no_core, lang_items)] | ||
#![crate_type = "lib"] | ||
#![no_std] | ||
#![no_core] | ||
#![allow(non_camel_case_types)] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
#[lang = "freeze"] | ||
trait Freeze {} | ||
#[lang = "copy"] | ||
trait Copy {} | ||
|
||
// This type has align 1 in Rust, but as a byval argument on i686-linux, it will have align 4. | ||
#[repr(C)] | ||
#[repr(packed)] | ||
struct Align1 { | ||
x: u128, | ||
y: u128, | ||
z: u128, | ||
} | ||
|
||
// This type has align 16 in Rust, but as a byval argument on i686-linux, it will have align 4. | ||
#[repr(C)] | ||
#[repr(align(16))] | ||
struct Align16 { | ||
x: u128, | ||
y: u128, | ||
z: u128, | ||
} | ||
|
||
extern "C" { | ||
fn extern_c_align1(x: Align1); | ||
fn extern_c_align16(x: Align16); | ||
} | ||
|
||
// CHECK-LABEL: @rust_to_c_increases_alignment | ||
#[no_mangle] | ||
pub unsafe fn rust_to_c_increases_alignment(x: Align1) { | ||
// i686-linux: start: | ||
// i686-linux-NEXT: [[ALLOCA:%[0-9a-z]+]] = alloca %Align1, align 4 | ||
// i686-linux-NEXT: call void @llvm.memcpy.{{.+}}(ptr {{.*}}align 4 {{.*}}[[ALLOCA]], ptr {{.*}}align 1 {{.*}}%x | ||
// i686-linux-NEXT: call void @extern_c_align1({{.+}} [[ALLOCA]]) | ||
|
||
// x86_64-linux: start: | ||
// x86_64-linux-NEXT: call void @extern_c_align1 | ||
extern_c_align1(x); | ||
} | ||
|
||
// CHECK-LABEL: @rust_to_c_decreases_alignment | ||
#[no_mangle] | ||
pub unsafe fn rust_to_c_decreases_alignment(x: Align16) { | ||
// CHECK: start: | ||
// CHECK-NEXT: call void @extern_c_align16 | ||
extern_c_align16(x); | ||
} | ||
|
||
extern "Rust" { | ||
fn extern_rust_align1(x: Align1); | ||
fn extern_rust_align16(x: Align16); | ||
} | ||
|
||
// CHECK-LABEL: @c_to_rust_decreases_alignment | ||
#[no_mangle] | ||
pub unsafe extern "C" fn c_to_rust_decreases_alignment(x: Align1) { | ||
// CHECK: start: | ||
// CHECK-NEXT: call void @extern_rust_align1 | ||
extern_rust_align1(x); | ||
} | ||
|
||
// CHECK-LABEL: @c_to_rust_increases_alignment | ||
#[no_mangle] | ||
pub unsafe extern "C" fn c_to_rust_increases_alignment(x: Align16) { | ||
// i686-linux: start: | ||
// i686-linux-NEXT: [[ALLOCA:%[0-9a-z]+]] = alloca %Align16, align 16 | ||
// i686-linux-NEXT: call void @llvm.memcpy.{{.+}}(ptr {{.*}}align 16 {{.*}}[[ALLOCA]], ptr {{.*}}align 4 {{.*}}%0 | ||
// i686-linux-NEXT: call void @extern_rust_align16({{.+}} [[ALLOCA]]) | ||
|
||
// x86_64-linux: start: | ||
// x86_64-linux-NEXT: call void @extern_rust_align16 | ||
extern_rust_align16(x); | ||
} | ||
|
||
extern "Rust" { | ||
fn extern_rust_ref_align1(x: &Align1); | ||
fn extern_rust_ref_align16(x: &Align16); | ||
} | ||
|
||
// CHECK-LABEL: @c_to_rust_ref_decreases_alignment | ||
#[no_mangle] | ||
pub unsafe extern "C" fn c_to_rust_ref_decreases_alignment(x: Align1) { | ||
// CHECK: start: | ||
// CHECK-NEXT: call void @extern_rust_ref_align1 | ||
extern_rust_ref_align1(&x); | ||
} | ||
|
||
// CHECK-LABEL: @c_to_rust_ref_increases_alignment | ||
#[no_mangle] | ||
pub unsafe extern "C" fn c_to_rust_ref_increases_alignment(x: Align16) { | ||
// i686-linux: start: | ||
// i686-linux-NEXT: [[ALLOCA:%[0-9a-z]+]] = alloca %Align16, align 16 | ||
// i686-linux-NEXT: call void @llvm.memcpy.{{.+}}(ptr {{.*}}align 16 {{.*}}[[ALLOCA]], ptr {{.*}}align 4 {{.*}}%0 | ||
// i686-linux-NEXT: call void @extern_rust_ref_align16({{.+}} [[ALLOCA]]) | ||
|
||
// x86_64-linux: start: | ||
// x86_64-linux-NEXT: call void @extern_rust_ref_align16 | ||
extern_rust_ref_align16(&x); | ||
} |
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.
The only functional change in this file is replacing
with
since the argument alignment is not always equal to the Rust ABI alignment.