Repository navigation
add core::arch::{x86, x86_64}::x87_f80 - #163992
folkertdev wants to merge 1 commit into
Conversation
This comment has been minimized.
This comment has been minimized.
b4fafc4 to
16b2882
Compare
This comment has been minimized.
This comment has been minimized.
16b2882 to
617db2d
Compare
| assert_eq!( | ||
| layout.layout.size(), | ||
| size, | ||
| "size mismatch between ABI and layout in {layout:#?}" | ||
| ); | ||
| if is_x87 { | ||
| assert_eq!( | ||
| layout.layout.size(), | ||
| size.align_to(align.unwrap()), | ||
| "size mismatch between ABI and layout in {layout:#?}" | ||
| ); | ||
| } else { | ||
| assert_eq!( | ||
| layout.layout.size(), | ||
| size, | ||
| "size mismatch between ABI and layout in {layout:#?}" | ||
| ); | ||
| } |
There was a problem hiding this comment.
The old invariant now no longer holds. CI passes here but who knows where we implicitly rely on it.
| // SAFETY: the first 10 bytes are initialized. | ||
| unsafe { *(&raw const self).cast::<[u8; 10]>() } |
There was a problem hiding this comment.
We could use https://doc.rust-lang.org/std/mem/fn.transmute_prefix.html here but I don't see that used anywhere else yet.
617db2d to
d582ca8
Compare
|
Some changes occurred in compiler/rustc_attr_ir cc @jdonszelmann, @JonathanBrouwer
cc @bjorn3 Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred to the CTFE machinery This PR changes rustc_public cc @oli-obk, @celinval, @ouz-a, @makai410
|
|
@bors try jobs=test-various,test-i686-* |
This comment has been minimized.
This comment has been minimized.
add `core::arch::{x86, x86_64}::x87_f80`
try-job: test-various
try-job: test-i686-*
| // SAFETY: we initialize the first 10 bytes, any bit pattern is valid. | ||
| unsafe { | ||
| let mut tmp = crate::mem::MaybeUninit::<Self>::uninit(); | ||
| tmp.as_mut_ptr().cast::<[u8; 10]>().write(bytes); | ||
| tmp.assume_init() | ||
| } |
There was a problem hiding this comment.
Maybe transmute_copy for a bit simpler impl? Maybe also for to_ne_bytes
There was a problem hiding this comment.
That is UB because size_of_val::<Src>(src) >= size_of::<Dst>() does not hold
https://doc.rust-lang.org/std/mem/fn.transmute_copy.html
Hence my suggestion for
| #[allow(non_camel_case_types)] | ||
| #[doc(cfg(any(target_arch = "x86", target_arch = "x86_64")))] | ||
| #[derive(Clone, Copy)] | ||
| pub struct x87_f80([u8; 10]); |
There was a problem hiding this comment.
Could you assert the size+align of this somewhere? Since it's an unusual combination of those for a primitive.
d582ca8 to
a9a37a4
Compare
"f80" is 128 bits large on 64-bit targets...?!?? How does that make any sense?
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. It does not sound to me like this type can be a scalar, then. |
| // 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" | ||
| ); |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
To give just one example, here's one place we make that assumption:
rust/compiler/rustc_const_eval/src/interpret/place.rs
Lines 792 to 794 in 6aaabbb
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.
|
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". |
|
I think it might make sense to introduce something like |
|
For Rust's purposes could we just pretend it's a |
This comment has been minimized.
This comment has been minimized.
dc364ad to
280df50
Compare
| // 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(); | ||
| } |
There was a problem hiding this comment.
So, this is really all it takes in the LLVM backend to make this type work. it gets the LLVM store size of 10 bytes (touched by load/store) and target-dependent alloc size of 12 or 16.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I think the LLVM type just behaves correctly though? It has a store size of 10 but an allocation size of 12/16, and LLVM uses the right value in the right context as far as I can tell. Hence, we don't need to manually convert to the array?
There was a problem hiding this comment.
phrased differently: what sort of code do you think fails with the current implementation, because we should test its behavior either way.
| let x87_f80 = scalar_unit(Primitive::Float(abi::Float::X87F80)); | ||
| assert_eq!(layout.size, abi::Float::X87F80.size()); | ||
| layout.align = abi::Float::X87F80.align(cx); | ||
| layout.size = layout.size.align_to(layout.align.abi); | ||
| layout.backend_repr = BackendRepr::Scalar(x87_f80); |
There was a problem hiding this comment.
Eh, yeah, seems to work.
| #[inline] | ||
| pub const fn to_ne_bytes(self) -> [u8; 10] { | ||
| // SAFETY: the first 10 bytes are initialized. | ||
| unsafe { (&raw const self).cast::<[u8; 10]>().read() } |
There was a problem hiding this comment.
A self.0 projection here trips up const eval, so a pointer read will have to do.
There was a problem hiding this comment.
What does "trips up const eval" mean?
There was a problem hiding this comment.
I misremembered and the actual problem is from_ne_bytes. I think the problem is similar to how directly using the inner array of the Simd type is fragile.
In this case we hit the bug! in
rust/compiler/rustc_codegen_ssa/src/mir/operand.rs
Lines 868 to 877 in 8a9dd7a
error: internal compiler error: compiler/rustc_codegen_ssa/src/mir/operand.rs:876:21: Tried to insert OperandRef(Ref(PlaceValue { llval: (ptr:ptr %1), llextra: None, align: Align(1 bytes) }) @ TyAndLayout { ty: [u8; 10], layout: Layout { size: Size(10 bytes), align: AbiAlign { abi: Align(1 bytes) }, backend_repr: Memory { sized: true }, fields: Array { stride: Size(1 bytes), count: 10 }, largest_niche: None, uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), repr_c: false, randomization_seed: 71776127651151883 } }) into 0.0 of OperandRefBuilder { val: Immediate(Right(x87_f80 is ..)), layout: TyAndLayout { ty: core::core_arch::x86::x87_f80_raw, layout: Layout { size: Size(10 bytes), align: AbiAlign { abi: Align(1 bytes) }, backend_repr: Scalar(x87_f80 is ..), fields: Arbitrary { offsets: [Size(0 bytes)], in_memory_order: [0] }, largest_niche: None, uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), repr_c: false, randomization_seed: 4653255336921355707 } } }
With debug assertions (if I remember right) you hit an assert earlier. But also, in line with Simd, it seems best to just not use projections into lang items like this?
There was a problem hiding this comment.
(That's not const-eval. ;)
I would have expected that this behaves mostly like a repr(align) struct where projections are fine. But I guess there's a reason that you made it more like Simd?
There was a problem hiding this comment.
The x87_f80_raw struct is still considered a scalar internally (with a size of 80 bits and an alignment of 1). Projecting an array out of that is fragile, and easy enough to work around.
There was a problem hiding this comment.
Oh yeah. That projection should not be allowed.
Why is this a lang item rather than a repr? It seems most parallel with repr(simd) and repr(complex).
There was a problem hiding this comment.
We don't have repr(complex) any more, we went with just a lang item for Complex<T>. From what I understand repr(simd) also has its issues.
The main annoyance (reading back the discussion on #158885) is the interaction with other repr attributes, and no longer being able to make use of default paths in some functions. A lang item is much more targeted.
There was a problem hiding this comment.
| unsafe { (&raw const self).cast::<[u8; 10]>().read() } | |
| unsafe { mem::transmute(self.0) } |
I think this should work now?
There was a problem hiding this comment.
No, self.0 has type core::num::imp::x87_f80 which is not 10 bytes in size. The raw field is (deliberately) private.
| if layout.backend_repr.is_x87_f80() { | ||
| // The lang item wraps a byte array, so it is not a newtype of a scalar. | ||
| return; | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
What does "for docs" mean here? The attribute seems quite important not just for docs...?
There was a problem hiding this comment.
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.
|
With this new implementation, Rust type |
This comment has been minimized.
This comment has been minimized.
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. |
280df50 to
87ea41c
Compare
This comment has been minimized.
This comment has been minimized.
87ea41c to
1cf2718
Compare
This comment has been minimized.
This comment has been minimized.
1cf2718 to
73c618c
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
73c618c to
40ddd5f
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. |
| F16 | F16B => dl.f16_align, | ||
| F32 => dl.f32_align, | ||
| F64 => dl.f64_align, | ||
| X87F80 => Align::ONE, |
There was a problem hiding this comment.
Might be worth a short comment here explaining why this isn't dl.x87_f80_align?
| // 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(); | ||
| } |
There was a problem hiding this comment.
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).
| /// 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. |
There was a problem hiding this comment.
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.
| #[inline] | ||
| pub const fn to_ne_bytes(self) -> [u8; 10] { | ||
| // SAFETY: the first 10 bytes are initialized. | ||
| unsafe { (&raw const self).cast::<[u8; 10]>().read() } |
There was a problem hiding this comment.
| unsafe { (&raw const self).cast::<[u8; 10]>().read() } | |
| unsafe { mem::transmute(self.0) } |
I think this should work now?
| // SAFETY: we initialize the first 10 bytes, the remaining bytes remain uninitialized. | ||
| // That is exactly the layout of x87_f80. | ||
| unsafe { | ||
| let mut this = crate::mem::MaybeUninit::<x87_f80>::uninit(); | ||
| this.as_mut_ptr().cast::<[u8; 10]>().write(bytes); | ||
| this.assume_init() | ||
| } |
There was a problem hiding this comment.
| // SAFETY: we initialize the first 10 bytes, the remaining bytes remain uninitialized. | |
| // That is exactly the layout of x87_f80. | |
| unsafe { | |
| let mut this = crate::mem::MaybeUninit::<x87_f80>::uninit(); | |
| this.as_mut_ptr().cast::<[u8; 10]>().write(bytes); | |
| this.assume_init() | |
| } | |
| // SAFETY: All bit patterns are valid for `x87_f80_raw` | |
| unsafe { Self(mem::transmute(bytes)) } |
There was a problem hiding this comment.
Similarly here the suggested transmute has a size mismatch.
| 16 => dl.f16_align, | ||
| 32 => dl.f32_align, | ||
| 64 => dl.f64_align, | ||
| 80 => dl.x87_f80_align, |
There was a problem hiding this comment.
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).
40ddd5f to
552d90d
Compare
552d90d to
e3ac46d
Compare
View all comments
tracking issue: #163982
cc #161787
Add
x87_f80in the same way asf16bandppcf128.This changes the signature of
sizebecause the size ofx87_f80depends 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_f80scalar 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
cfgfor this though?x87is still set on softfloat targets, whilesoft-floatdoesn't get set (that does not give a warning even, it probably should?). We also don't setcfg_abi.future work
Complex<x87_f80>has not yet been implementedx87_f80does not yet implementVaArgSafe