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
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-*
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 |
| 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 comment has been minimized.
This comment has been minimized.
| // 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.
There was a problem hiding this comment.
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
callconvcan't handle this type becauseis_x87_f80_lang_itemreturns 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?
There was a problem hiding this comment.
Reliably ICEing when it comes up in the ABI seems fine.
Why is the array stride wrong?
There was a problem hiding this comment.
Yes, I just checked locally. Bors is having a rough time but I'll add a test once that merges.
| 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).
e3ac46d to
fbd679f
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
fbd679f to
861c258
Compare
861c258 to
458a970
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. |
| // CHECK: getelementptr inbounds nuw i8, ptr %arr, {{i32|i64}} 10 | ||
| let [_, b] = *arr; | ||
| b | ||
| } |
There was a problem hiding this comment.
This now tests the size and stride of x87_f80_raw.
| // 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.
Is this still relevant with #165146 now being merged?
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