fix is_single_fp_element for s390x and x86 - #161987
folkertdev wants to merge 3 commits into
Conversation
| // Match GCC and Clang in allowing trailing padding. This does appear to violate the | ||
| // specification, but is well-established in both compilers. | ||
| // | ||
| // e.g. `#[repr(C, align(4))] struct Foo(f16)` is passed as `Reg::f32()`. |
There was a problem hiding this comment.
I've done some further research into this. AFAICT it seems that Clang and GCC, while they do use larger loads/stores than needed, do (coincidentally?) end up doing the right thing with the full struct size is <= 8 bytes (specifically, loading e.g. a 8 byte struct as a f16, when the struct is a f16 followed by 6 bytes of padding, will mean the f16 ends up in the right place in the register anyway). The only difference between Clang/GCC's behaviour and the ABI spec is that structs which contain a single f16/f32/f64 that are larger than 8 bytes won't get passed in a floating point register. In summary, the problem is that Clang/GCC don't ignore the trailing padding when they should.
There was a problem hiding this comment.
cc @uweigand in general on this PR but here in particular
There was a problem hiding this comment.
(Re-reading the spec again, it does say "[...] load the argument value left-aligned into floating-point register [...]", which is probably why GCC/Clang load all the bytes instead of just the bytes of the floating-point value (of course it doesn't matter whether the padding bytes are loaded or not as they're padding, except when there's so much padding the full size of the values doesn't fit in the register). I'm guessing overaligned structs weren't considered one way or another when writing this part of the spec)
5681e66 to
50861ec
Compare
50861ec to
5867f93
Compare
2172749 to
9ae7ff3
Compare
|
I'll leave it as a draft because I'm not really sure who to assign here, and I'd like some feedback from the s390x target maintainer anyway. |
| 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. |
There was a problem hiding this comment.
We need to follow the de-facto ABI, which is that trailing padding is allowed if the total size including padding still remains a power-of-two <= 8 bytes. This is consistently implemented by all compilers on the platform - I think we should update the ABI spec accordingly.
|
r? beetrees |
| 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 | ||
| } |
There was a problem hiding this comment.
Unlike the x86 ABI, the s390x ABI (and Clang/GCC) does not allow empty structs here. Is there any Rust-wide expectation that #[repr(C)] struct A(f32, PhantomData<()>); has the same ABI as #[repr(C)] struct B(f32); (or the C struct B { float f; };)? I haven't been able to find any documentation on the subject. It feels like the definition of a ZST with a "trivial ABI" from the not-yet-merged #157973 would be logical to be ignored in structs, but I'm not sure if there has been any discussion about this. For now, probably worth at least leaving a FIXME here.
There was a problem hiding this comment.
No there's nothing official AFAIK. Only repr(C) structs where all fields have a C equivalent have any guarantees.
But I agree "completely ignore types with trivial ABI" makes sense both for the ABI (rust-lang/unsafe-code-guidelines#623) and for repr(C) layout.
There was a problem hiding this comment.
For C and C++, the s390x ABI does not allow empty structs. However, I don't think this necessarily has to impose a restriction on what we can define for the Rust ABI here - for types used across a cross-language boundary, this should not cause issues in practice.
Given that ZST are much more frequently used in Rust than in C, I do agree it makes sense to ignore them in Rust here.
There was a problem hiding this comment.
The logic here is shared between ABIs (extern "C", extern "Rust", etc.), so changing how ZSTs are handled here breaks compatibility with C.
We can carve out an exception for rustic ABIs specifically but I don't think that's relevant to this PR.
There was a problem hiding this comment.
Are you sure? The logic in this file should not be used for "Rust".
There was a problem hiding this comment.
The logic in this file is only for non-Rustic ABIs: Rustic ABIs use a separate code path that currently doesn't perform any target-specific adjustments on s390x.
There was a problem hiding this comment.
Ah, I thought that only happened later on.
Still, this is not the right place to have custom, incompatible behavior for ZSTs.
There was a problem hiding this comment.
The current behaviour is consistent with our current behaviour of not ignoring ZSTs in arguments if C doesn't: once #157973 is merged we'll probably make it so that only ZSTs with a non-trivial ABI matter, but that's for a future, more general PR.
|
Reminder, once the PR becomes ready for a review, use |
2992d1d to
2df6a25
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
|
|
||
| /// Finds the one field that is not a ZST. | ||
| /// Returns `None` if there are multiple non-ZST fields or only ZST-fields. | ||
| pub fn non_zst_field<C>(&self, cx: &C) -> Option<(FieldIdx, Self)> |
There was a problem hiding this comment.
Having non_zst_field and non_1zst_field with very similar names feels like an easy footgun for future ABI PRs. Maybe rename non_zst_field to non_zst_field_ignore_alignment (or similar) to make the distinction more obvious?
| #[repr(C)] | ||
| struct Wrapper<T>(T); | ||
|
|
||
| // CHECK: define void @plain_f16(half noundef %x) |
There was a problem hiding this comment.
I think these should be CHECK-LABEL, not CHECK?
| Primitive::Float(Float::F32 | Float::F64) => true, | ||
| Primitive::Float(Float::F16 | Float::F128) => false, |
There was a problem hiding this comment.
While it doesn't implement VaArgSafe yet, f16 is passed in floating point registers.
| Primitive::Float(Float::F32 | Float::F64) => true, | |
| Primitive::Float(Float::F16 | Float::F128) => false, | |
| Primitive::Float(Float::F16 | Float::F32 | Float::F64) => true, | |
| Primitive::Float(Float::F128) => false, |
2df6a25 to
0501e2b
Compare
0501e2b to
33871b9
Compare
|
@rustbot ready |
View all comments
In #161950 (comment) we discovered that the old
is_single_fp_elementis incorrect in a number of ways.f16orf128So, in practice each target does something slightly different here, and I've split the function into two.
cc @beetrees