-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add intrinsic for Option::Some(_) offset #109095
Conversation
r? @scottmcm (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
Some changes occurred to the CTFE / Miri engine cc @rust-lang/miri |
library/core/src/intrinsics.rs
Outdated
|
||
#[cfg(not(bootstrap))] | ||
extern "rust-intrinsic" { | ||
/// The offset of the `Some(_)` value of an `Option<T>`. Used internally for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please re-add the FIXME from the hacky libstd impl here, so we remember to nuke this intrinsic in the future
library/core/src/option.rs
Outdated
/// As this version will only be ever used to compile rustc and the performance | ||
/// penalty is negligible, use a minimal implementation here. | ||
#[cfg(bootstrap)] | ||
const SOME_BYTE_OFFSET_GUESS: usize = mem::size_of::<Option<T>>() - mem::size_of::<T>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0 should be the safest choice here.
I'm a bit confused about the error. I had included the const-unstable feature in |
if the intrinsic is used from stable code, then yes, you need to make it stable. |
library/core/src/intrinsics.rs
Outdated
@@ -2550,3 +2550,13 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) { | |||
write_bytes(dst, val, count) | |||
} | |||
} | |||
|
|||
#[cfg(not(bootstrap))] | |||
extern "rust-intrinsic" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the intrinsics to the main extern "rust-intrinsic"
block instead and just cfg the fn directly?
da1a88c
to
86bc827
Compare
let FieldsShape::Arbitrary { offsets, .. } = &variant.fields else { bug!() }; | ||
bx.const_usize(offsets[0].bytes()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use variant.fields.offset(0)
? I am not sure if the bug!
here is truly unreachable. Layouts can take very surprising shapes sometimes.
sym::option_some_offset => { | ||
let ty = substs.type_at(0); | ||
let layout = bx.layout_of(ty); | ||
let Variants::Multiple { variants, .. } = layout.layout.variants() else { bug!() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will ICE on Option<!>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, a test for Option<Never>
would be a good add, regardless of implementation 👍
let Variants::Multiple { variants, .. } = layout.layout.variants() else { bug!() }; | ||
let Some(variant) = variants.iter().last() else { bug!() }; | ||
let FieldsShape::Arbitrary { offsets, .. } = &variant.fields else { bug!() }; | ||
ConstValue::from_target_usize(offsets[0].bytes(), &tcx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should avoid duplicating this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, where to put it then?
Const stability checking is fully recursive, unlike the usual library stability checking. We want to avoid accidentally stably exposing the behavior of some unstable function / intrinsic at compile time. |
86bc827
to
a5ac44e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this probably needs some updates before it can land, but I'm usually just a libs reviewer, so this should go to someone better equipped to evaluate the right way to do this in the compiler.
r? compiler
Closing this in favor of a |
…ce, r=eholk move Option::as_slice to intrinsic `@scottmcm` suggested on rust-lang#109095 I use a direct approach of unpacking the operation in MIR lowering, so here's the implementation. cc `@nikic` as this should hopefully unblock rust-lang#107224 (though perhaps other changes to the prior implementation, which I left for bootstrapping, are needed).
…ce, r=eholk move Option::as_slice to intrinsic ``@scottmcm`` suggested on rust-lang#109095 I use a direct approach of unpacking the operation in MIR lowering, so here's the implementation. cc ``@nikic`` as this should hopefully unblock rust-lang#107224 (though perhaps other changes to the prior implementation, which I left for bootstrapping, are needed).
…ce, r=eholk move Option::as_slice to intrinsic ```@scottmcm``` suggested on rust-lang#109095 I use a direct approach of unpacking the operation in MIR lowering, so here's the implementation. cc ```@nikic``` as this should hopefully unblock rust-lang#107224 (though perhaps other changes to the prior implementation, which I left for bootstrapping, are needed).
…ce, r=eholk move Option::as_slice to intrinsic ````@scottmcm```` suggested on rust-lang#109095 I use a direct approach of unpacking the operation in MIR lowering, so here's the implementation. cc ````@nikic```` as this should hopefully unblock rust-lang#107224 (though perhaps other changes to the prior implementation, which I left for bootstrapping, are needed).
This replaces the prior hacky calculation with an
option_some_offset
intrinsic that directly gets the offset from the type layout.cc @scottmcm #108545