Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Mar 13, 2023

This replaces the prior hacky calculation with an option_some_offset intrinsic that directly gets the offset from the type layout.

cc @scottmcm #108545

@rustbot
Copy link
Collaborator

rustbot commented Mar 13, 2023

r? @scottmcm

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added 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. labels Mar 13, 2023
@rustbot
Copy link
Collaborator

rustbot commented Mar 13, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@llogiq llogiq mentioned this pull request Mar 13, 2023
3 tasks

#[cfg(not(bootstrap))]
extern "rust-intrinsic" {
/// The offset of the `Some(_)` value of an `Option<T>`. Used internally for
Copy link
Contributor

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

/// 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>();
Copy link
Member

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.

@llogiq
Copy link
Contributor Author

llogiq commented Mar 14, 2023

I'm a bit confused about the error. I had included the const-unstable feature in library/core/src/lib.rs, but it appears to be required whenever the const is actually instantiated? That to me looks like a bug in the feature gate check. Should I simply make the intrinsic const-stable?

@oli-obk
Copy link
Contributor

oli-obk commented Mar 14, 2023

Should I simply make the intrinsic const-stable?

if the intrinsic is used from stable code, then yes, you need to make it stable.

@@ -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" {
Copy link
Member

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?

@llogiq llogiq force-pushed the option-some-offset-intrinsic branch from da1a88c to 86bc827 Compare March 14, 2023 12:24
Comment on lines +112 to +113
let FieldsShape::Arbitrary { offsets, .. } = &variant.fields else { bug!() };
bx.const_usize(offsets[0].bytes())
Copy link
Member

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!() };
Copy link
Member

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

Copy link
Member

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)
Copy link
Member

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.

Copy link
Contributor Author

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?

@RalfJung
Copy link
Member

I'm a bit confused about the error. I had included the const-unstable feature in library/core/src/lib.rs, but it appears to be required whenever the const is actually instantiated? That to me looks like a bug in the feature gate check. Should I simply make the intrinsic const-stable?

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.

@llogiq llogiq force-pushed the option-some-offset-intrinsic branch from 86bc827 to a5ac44e Compare March 14, 2023 17:13
Copy link
Member

@scottmcm scottmcm left a 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

library/core/src/intrinsics.rs Show resolved Hide resolved
@llogiq
Copy link
Contributor Author

llogiq commented Mar 15, 2023

Closing this in favor of a offset_payload_ptr-intrinsic based solution.

@llogiq llogiq closed this Mar 15, 2023
@llogiq llogiq deleted the option-some-offset-intrinsic branch March 15, 2023 09:44
@michaelwoerister michaelwoerister removed their assignment Mar 17, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 21, 2023
…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).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 21, 2023
…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).
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 22, 2023
…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).
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 22, 2023
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

9 participants