Skip to content

fix is_single_fp_element for s390x and x86 - #161987

Open
folkertdev wants to merge 2 commits into
rust-lang:mainfrom
folkertdev:single-fp-element
Open

fix is_single_fp_element for s390x and x86#161987
folkertdev wants to merge 2 commits into
rust-lang:mainfrom
folkertdev:single-fp-element

Conversation

@folkertdev

Copy link
Copy Markdown
Contributor

In #161950 (comment) we discovered that the old is_single_fp_element is incorrect in a number of ways.

  • it did not consider f16 or f128
  • it did not consider transparent wrappers
  • on x86, it incorrectly accepted over-aligned types
  • on s390x, it incorrectly accepted single-element unions and arrays

So, in practice each target does something slightly different here, and I've split the function into two.

cc @beetrees

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 29, 2026
Comment on lines +69 to +72
// 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()`.

@beetrees beetrees Aug 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @uweigand in general on this PR but here in particular

@beetrees beetrees Aug 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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)

Comment thread compiler/rustc_abi/src/layout/ty.rs Outdated
Comment thread compiler/rustc_target/src/callconv/s390x.rs Outdated
@folkertdev

Copy link
Copy Markdown
Contributor Author

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);

@RalfJung RalfJung Aug 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

Comment on lines +97 to +111
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.

@RalfJung RalfJung Aug 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uweigand @cuviper -- looks like we have the choice of either implementing the ABI correctly according to the spec, or implementing the ABI GCC/clang use. The two sadly disagree. What would you prefer we do?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@folkertdev

Copy link
Copy Markdown
Contributor Author

r? beetrees

@folkertdev
folkertdev marked this pull request as ready for review September 8, 2026 17:21
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 8, 2026
Comment on lines +25 to +33
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
}
}

@beetrees beetrees Sep 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-freg-struct-return ignores all zero-sized fields (compiler explorer).

View changes since the review

Comment on lines +17 to +35
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,
}

@beetrees beetrees Sep 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to do this in a loop like the x86 is_single_fp_element does?

View changes since the review

Comment on lines +25 to +32
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
}

@beetrees beetrees Sep 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 8, 2026
@rustbot

rustbot commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Comment on lines +106 to +108
// 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.

@beetrees beetrees Sep 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants