Skip to content

add core::arch::{x86, x86_64}::x87_f80 - #163992

Open
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:x87_f80-simple
Open

folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:x87_f80-simple

Conversation

@folkertdev

@folkertdev folkertdev commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

tracking issue: #163982
cc #161787

Add x87_f80 in the same way as f16b and ppcf128.

This changes the signature of size because the size of x87_f80 depends on the target (12 bytes on 32-bit targets, 16 bytes on 64-bit targets).

It also breaks an invariant that scalars are the size of their fields: the x87_f80 scalar is 80 bits, but the field is the aforementioned 12 or 16 bytes. Let's hope we don't subtly rely on that somewhere.


It's unclear what to do with this type on softfloat targets. Clang and GCC reject the type when the x87 registers are not available. We should do the same, probably just with a cfg?

I don't think we have a good cfg for this though? x87 is still set on softfloat targets, while soft-float doesn't get set (that does not give a warning even, it probably should?). We also don't set cfg_abi.

future work

  • the ABI of Complex<x87_f80> has not yet been implemented
  • x87_f80 does not yet implement VaArgSafe

@folkertdev folkertdev added the F-c_longdouble `#![feature(c_longdouble)]` label Oct 8, 2026
@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 8, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@folkertdev

Copy link
Copy Markdown
Contributor Author

r? beetrees
cc @tgross35 @RalfJung

@folkertdev
folkertdev marked this pull request as ready for review October 8, 2026 19:07
@rustbot

rustbot commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @sayantn

Some changes occurred in compiler/rustc_attr_ir

cc @jdonszelmann, @JonathanBrouwer

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

rustc_codegen_gcc is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_gcc instead.

cc @antoyo, @GuillaumeGomez

@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 Oct 8, 2026
@folkertdev

Copy link
Copy Markdown
Contributor Author

@bors try jobs=test-various,test-i686-*

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 8, 2026
add `core::arch::{x86, x86_64}::x87_f80`


try-job: test-various
try-job: test-i686-*
@rust-bors

rust-bors Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 4923e95 (4923e95bc1c5973505d6b35dcddbc97d11ae69e5)
Base parent: 65efa19 (65efa194201c67ba29b7384255a534d61c2aa3cb)

@RalfJung

RalfJung commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

This changes the signature of size because the size of x87_f80 depends on the target (12 bytes on 32-bit targets, 16 bytes on 64-bit targets).

"f80" is 128 bits large on 64-bit targets...?!?? How does that make any sense?

It also breaks an invariant that scalars are the size of their fields: the x87_f80 scalar is 80 bits, but the field is the aforementioned 12 or 16 bytes. Let's hope we don't subtly rely on that somewhere.

I'd be surprised if we didn't. That's been pretty much part of the definition of being a scalar -- if a type has padding, it is not a scalar type. The interpreter was built around that assumption.
Cc @scottmcm who recently generalized what a scalar can be, but even with that I don't think it can have padding.

It does not sound to me like this type can be a scalar, then.

Comment on lines +600 to +607
// For `x87_f80` the primitive is 80 bits, but the field (after rounding up to
// the alignment) is 12 bytes (on 32-bit systems) or 16 bytes (64-bit).
// So we can't assert equality of size and layout.size.
assert_eq!(
size.align_to(s.default_align(self).abi),
mplace.layout.size,
"abi::Scalar is larger than its layout"
);

@RalfJung RalfJung Oct 9, 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.

The entire logic for dealing with scalars throughout the interpreter assumes that they have no padding. Changing this assertion is not enough, it just means the rest of the interpreter is now more or less silently wrong. Pretty much all codepaths that deal with scalars need to be audited. Same for every other MIR consumer.

I would strongly prefer if we didn't break the fundamental concept of a "scalar".

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.

To give just one example, here's one place we make that assumption:

Immediate::Scalar(scalar) => {
alloc.write_scalar(alloc_range(Size::ZERO, scalar.size()), scalar)?;
}

You can't even see it. But if you look at the ScalarPair path you can see that we have to ensure that padding gets reset to uninit, and in the Scalar path we don't have to worry about that.

Also, the basic APIs for reading/writing stuff in an Allocation are read_scalar/write_scalar. This again uses the term "scalar" to refer to something that has no padding. Same for the type Scalar. And so on.

@RalfJung

RalfJung commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

It would probably be possible, with a lot of effort, to fix all the places where we assume scalars have no padding. But I think that's a bad idea. It makes life harder for every MIR consumer. (Most likely it'll mean many MIR consumers will stay buggy as nobody will expect something as outlandish as this weird type.) This new type is the weird one here, we should not shape everything around a legacy type that hardly anyone needs and that has no justification to exist in the first place other than "it has always been here".

@beetrees

beetrees commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

I think it might make sense to introduce something like BackendRepr::PaddedScalar (name bikesheddable)? This would be useful for more than just x87_f80: e.g. currently we can (don't have my laptop to check if we currently do) represent #[repr(align(16))] struct A(u8, u8); as a BackendRepr::ScalarPair, whereas #[repr(align(16))] struct A(u8); currently has to be represented as BackendRepr::Memory as it has padding.

@tgross35

tgross35 commented Oct 9, 2026

Copy link
Copy Markdown
Member

For Rust's purposes could we just pretend it's a MaybeUninit<u128> on x86-64 rather than treating it as a padded scalar?

Comment on lines +121 to +124
if layout.backend_repr.is_x87_f80() {
// The lang item wraps a byte array, so it is not a newtype of a scalar.
return;
}

@RalfJung RalfJung Oct 15, 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.

Seems like this type is still breaking basic MIR assumptions that have been true since forever and that code inside and outside the compiler is likely to rely on in subtle ways. Why that?

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.

Well we don't have any 80-bit scalars available to wrap transparently. So we'd have to make this type fully magic. But I guess f32 kind of is (like, it doesn't wrap [u8; 4] or u32) so maybe that could just work. I'll have to look into that.

/// alignment, raising the total size to 12 or 16.
///
/// NOTE: this type MUST NOT be used anywhere except in the x87_f80 wrapper below.
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), lang = "x87_f80_raw")] // for docs

@RalfJung RalfJung Oct 15, 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.

What does "for docs" mean here? The attribute seems quite important not just for docs...?

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.

Right, it's the cfg_attr that is important for a docs build: we now error on encountering the lang item on non-x86 targets. Just to make sure we never trigger that in a doc build, never declare the lang item on non-x86 targets.

@beetrees

Copy link
Copy Markdown
Contributor

With this new implementation, Rust type x87_f80_raw is size 10 but the LLVM (non-immediate) backend type x86_fp80 has an allocation size of 12/16. While in the source code it is only ever interacted with via Rust type x87_fp80, AFAICT it can at minimum still end up as the type of a local variable via MIR passes like SROA. This feels like something that could cause issues, although I haven't been able to come up with an example, so maybe not? Would changing the non-immediate backend type (llvm_type) to [10 x i8] for anything with BackendRepr::Scalar(Float::X87F80) work to solve this?

@rust-bors

This comment has been minimized.

@scottmcm

scottmcm commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Cc @scottmcm who recently generalized what a scalar can be, but even with that I don't think it can have padding.

I was working on allowing different alignments, but as you say, not changing anything about adding padding or similar.


There's definitely a bunch of places that assume that Scalars of matching size are sound to transmute, for example. Changing that would affect all kinds of places and I agree that we shouldn't muck with that.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

Comment thread compiler/rustc_abi/src/lib.rs Resolved
Comment thread compiler/rustc_abi/src/layout/ty.rs Resolved
Comment on lines +78 to +81
// Map `core::arch::{x86, x86_64}::x87_f80` to LLVM's `x86_fp80`.
if Ty::is_x87_f80_lang_item(layout, cx) {
return cx.type_x87_f80();
}

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.

x87_f80 is BackendRepr::Memory, so we should never be loading or storing its LLVM type. Instead, when uncached_llvm_type is fetching the type of the field of a struct or array, it should replace any occurrences of x87_f80_raw with [10 x i8]. This will ensure the returned struct always has the right size, which I AFAICT is the problem this is here to solve. Additionally, could you add an assert with FIXME to CastTarget::llvm_type ensuring it never returns an LLVM struct/array containing an LLVM x86_fp80 - load_cast/store_cast currently load/store the LLVM cast target aggregate directly (which is not generally a good idea for reasons like this, but that can be fixed in a separate PR).

Comment thread compiler/rustc_abi/src/layout/ty.rs Resolved
/// The `x87_f80` wrapper type below corresponds to the alloc size: it applies the target-specific
/// alignment, raising the total size to 12 or 16.
///
/// NOTE: this type MUST NOT be used anywhere except in the x87_f80 wrapper below.

@beetrees beetrees Oct 28, 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.

This note shouldn't be necessary if everything is working properly. If it is, it suggests there's a deeper problem that needs to be fixed before this is merged. x87_f80 should be like Complex<T>: is a lang item for ABI purposes, but otherwise is a regular struct.

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.

I'm aware of two issues with the type when it's used standalone

  • the array stride is wrong, so getting second element out of a [x87_f80_raw; 2] reads at the wrong offset (and reads out of bounds)
  • the ABI calculation in callconv can't handle this type because is_x87_f80_lang_item returns false for it, you can trigger an ICE this way

As far as I can tell, neither of these issues can be observed from the outside when x87_f80_raw is private and only used as the element type of x87_f80.

So (how) should that be fixed?

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.

Reliably ICEing when it comes up in the ABI seems fine.

Why is the array stride wrong?

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.

Is the array stride issue fixed by #165146?

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.

Yes, I just checked locally. Bors is having a rough time but I'll add a test once that merges.

Comment thread tests/assembly-llvm/x87_f80.rs Resolved
16 => dl.f16_align,
32 => dl.f32_align,
64 => dl.f64_align,
80 => dl.x87_f80_align,

@beetrees beetrees Oct 28, 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.

Given that (80 / 8) % dl.x87_f80_align != 0, could you add a comment here explaining why this is correct (I think it is as the align is only used to affect the overall alignment of the cast alloca, but we should document that fact somewhere to avoid confusion).

View changes since the review

@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 Oct 28, 2026
@folkertdev
folkertdev force-pushed the x87_f80-simple branch 3 times, most recently from e3ac46d to fbd679f Compare October 31, 2026 13:52
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

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.

@folkertdev folkertdev left a comment •

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.

@rustbot ready

for another look, anyway.

View changes since this review

// CHECK: getelementptr inbounds nuw i8, ptr %arr, {{i32|i64}} 10
let [_, b] = *arr;
b
}

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.

This now tests the size and stride of x87_f80_raw.

Comment on lines +78 to +81
// Map `core::arch::{x86, x86_64}::x87_f80` to LLVM's `x86_fp80`.
if Ty::is_x87_f80_lang_item(layout, cx) {
return cx.type_x87_f80();
}

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.

Is this still relevant with #165146 now being merged?

@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 Oct 31, 2026

This branch has not been deployed

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` F-c_longdouble `#![feature(c_longdouble)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants