-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
fix is_single_fp_element for s390x and x86
#161987
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
Draft
folkertdev
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
folkertdev:single-fp-element
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.
Draft
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 |
|---|---|---|
| @@ -1,11 +1,40 @@ | ||
| // Reference: ELF Application Binary Interface s390x Supplement | ||
| // https://github.com/IBM/s390x-abi | ||
|
|
||
| use rustc_abi::{BackendRepr, HasDataLayout, TyAbiInterface}; | ||
| use rustc_abi::{BackendRepr, FieldsShape, HasDataLayout, Primitive, TyAbiInterface, TyAndLayout}; | ||
|
|
||
| use crate::callconv::{ArgAbi, FnAbi, Reg}; | ||
| use crate::spec::{Env, HasTargetSpec, Os}; | ||
|
|
||
| /// Is this a struct with a single float field? | ||
| fn is_single_fp_element<'a, Ty, C>(mut layout: TyAndLayout<'a, Ty>, cx: &C) -> bool | ||
| where | ||
| Ty: TyAbiInterface<'a, C> + Copy, | ||
| C: HasDataLayout, | ||
| { | ||
| // Contrary to X86, trailing padding is allowed on s390x. | ||
|
|
||
| layout = layout.peel_transparent_wrappers(cx); | ||
| match layout.backend_repr { | ||
| BackendRepr::Scalar(scalar) => match scalar.primitive() { | ||
| Primitive::Float(_) => true, | ||
| Primitive::Int(_, _) | Primitive::Pointer(_) => false, | ||
| }, | ||
| BackendRepr::Memory { .. } => { | ||
| // A single-element array or union does not qualify. | ||
| if let FieldsShape::Arbitrary { .. } = layout.fields | ||
| && layout.fields.count() == 1 | ||
| && layout.fields.offset(0).bytes() == 0 | ||
| { | ||
| is_single_fp_element(layout.field(cx, 0), cx) | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) { | ||
| let size = ret.layout.size; | ||
| if size.bits() <= 128 && matches!(ret.layout.backend_repr, BackendRepr::SimdVector { .. }) { | ||
|
|
@@ -65,8 +94,23 @@ where | |
| return; | ||
| } | ||
|
|
||
| if arg.layout.is_single_fp_element(cx) { | ||
| if is_single_fp_element(arg.layout, cx) { | ||
| // Match GCC and Clang by explicitly passing padding, even though their behavior violates | ||
| // (our reading of) the specification, which says that: | ||
| // | ||
| // > Structures equivalent to a floating point type are passed in floating point registers. | ||
| // > A structure is equivalent to a floating point type if and only if it has exactly one | ||
| // > member, which is either of floating point type of itself a structure equivalent to a | ||
| // > floating point type. | ||
| // | ||
| // When the alignment is at most 8 but still overaligns the element, our implementation | ||
| // (matching GCC and Clang) is compliant but does require suboptimally large loads and | ||
| // stores. | ||
| // | ||
| // When the alignment is higher than 8, we passed the argument indirectly, which violates | ||
| // the specification but is consistent with GCC and Clang. | ||
|
Comment on lines
+97
to
+111
Member
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. |
||
| match size.bytes() { | ||
| 2 => arg.cast_to(Reg::f16()), | ||
| 4 => arg.cast_to(Reg::f32()), | ||
| 8 => arg.cast_to(Reg::f64()), | ||
| _ => arg.make_indirect(), | ||
|
|
||
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,129 @@ | ||
| //@ add-minicore | ||
| //@ needs-llvm-components: systemz | ||
| //@ compile-flags: --target=s390x-unknown-linux-gnu -Copt-level=3 -Zmerge-functions=disabled | ||
| #![crate_type = "lib"] | ||
| #![feature(no_core, f16, f128)] | ||
| #![no_core] | ||
|
|
||
| extern crate minicore; | ||
| use minicore::hint::black_box; | ||
| use minicore::*; | ||
|
|
||
| #[repr(C)] | ||
| struct Wrapper<T>(T); | ||
|
|
||
| // CHECK: define void @plain_f16(half noundef %x) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn plain_f16(x: f16) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @wrapped_f16(half %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn wrapped_f16(x: Wrapper<f16>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @plain_f32(float noundef %x) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn plain_f32(x: f32) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @wrapped_f32(float %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn wrapped_f32(x: Wrapper<f32>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @plain_f64(double noundef %x) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn plain_f64(x: f64) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @wrapped_f64(double %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn wrapped_f64(x: Wrapper<f64>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @plain_f128(ptr {{.*}}dereferenceable(16) %x) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn plain_f128(x: f128) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @wrapped_f128(ptr {{.*}}dereferenceable(16) %x) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn wrapped_f128(x: Wrapper<f128>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| #[repr(transparent)] | ||
| struct Transparent<T>(T); | ||
|
|
||
| // CHECK: define void @transparent_wrapped_f32(float %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn transparent_wrapped_f32(x: Transparent<Wrapper<f32>>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @transparent_transparent_wrapped_f32(float %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn transparent_transparent_wrapped_f32(x: Transparent<Transparent<Wrapper<f32>>>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| #[repr(C, align(8))] | ||
| struct Aligned8Wrapper<T>(T); | ||
|
|
||
| // CHECK: define void @aligned_8_wrapped_f16(double %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn aligned_8_wrapped_f16(x: Aligned8Wrapper<f16>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // CHECK: define void @aligned_8_wrapped_f32(double %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn aligned_8_wrapped_f32(x: Aligned8Wrapper<f32>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| #[repr(C, align(16))] | ||
| struct Aligned16Wrapper<T>(T); | ||
|
|
||
| // CHECK: define void @aligned_16_wrapped_f32(ptr {{.*}}dereferenceable(16) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn aligned_16_wrapped_f32(x: Aligned16Wrapper<f32>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| union UnionWrapper<T: Copy> { | ||
| a: T, | ||
| } | ||
|
|
||
| // A repr(C) union does not count. | ||
| // | ||
| // CHECK: define void @union_wrapped_f32(i32 %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn union_wrapped_f32(x: UnionWrapper<f32>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // But a repr(transparent) union does. | ||
| // | ||
| // CHECK: define void @maybe_uninit_f32(float %x) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn maybe_uninit_f32(x: MaybeUninit<f32>) { | ||
| black_box(x); | ||
| } | ||
|
|
||
| // A single-element array also does not count. | ||
| // | ||
| // CHECK: define void @array_f32(i32 %0) | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn array_f32(x: [f32; 1]) { | ||
| black_box(x); | ||
| } |
Oops, something went wrong.
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.
TIL that
peel_transparent_wrappersexists. However, its logic can only work for non-1-ZST types. The function should be renamed to reflect that as people might think it handles repr(transparent) for everything, and the doc comment ofpeel_transparent_wrappersshould be clarified to call this out.View changes since the review