fix is_single_fp_element for s390x and x86 - #161987
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
5867f93 to
2172749
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. |
| { | ||
| // Contrary to X86, trailing padding is allowed on s390x. | ||
|
|
||
| layout = layout.peel_transparent_wrappers(cx); |
There was a problem hiding this comment.
TIL that peel_transparent_wrappers exists. 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 of peel_transparent_wrappers should be clarified to call this out.
| 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 |
| BackendRepr::Memory { .. } => { | ||
| // Structs, unions and arrays all qualify. | ||
| if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 { | ||
| layout = layout.field(cx, 0); | ||
| continue; | ||
| } else { | ||
| false | ||
| } | ||
| } |
There was a problem hiding this comment.
-freg-struct-return ignores all zero-sized fields (compiler explorer).
| 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, | ||
| } |
There was a problem hiding this comment.
Any reason not to do this in a loop like the x86 is_single_fp_element does?
| 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.
|
Reminder, once the PR becomes ready for a review, use |
| // 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. |
There was a problem hiding this comment.
Upon further testing, while it is true that, for example, #[repr(C, align(8))] struct Foo(f32); is passed the same with cast_to(Reg::f32()) and cast_to(Reg::f64()) when the argument gets placed in registers, it is not equivalent if there are no floating point registers left and arguments are passed on the stack, as arguments are right-aligned within their 8-byte slot (I missed this when I was initially comparing them). The comment can probably simplified to something closer to what you originally had (that GCC/Clang don't ignore padding whereas our reading of the spec says the padding should be ignored).
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