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

move Option::as_slice to intrinsic #109179

Merged
merged 1 commit into from
Mar 22, 2023

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Mar 15, 2023

@scottmcm suggested on #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 #107224 (though perhaps other changes to the prior implementation, which I left for bootstrapping, are needed).

@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 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 15, 2023
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

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

@llogiq
Copy link
Contributor Author

llogiq commented Mar 15, 2023

No, rustbot, this is not for the libs team (although it touches on libs).

r? @rust-lang/compiler

@rustbot rustbot assigned eholk and unassigned scottmcm Mar 15, 2023
@llogiq
Copy link
Contributor Author

llogiq commented Mar 15, 2023

@scottmcm thanks for the help. I'll push the changes you proposed shortly once I have the test run complete.

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be done, a niche optimization doesn't justify a compiler intrinsic with all of its maintenance cost imo.

We should instead implement offset_of support for enums (btw is someone working on that? I could take a look if nobody is currently implementing this)

compiler/rustc_hir_analysis/src/check/intrinsic.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_analysis/src/check/intrinsic.rs Outdated Show resolved Hide resolved
@llogiq llogiq force-pushed the intrinsically-option-as-slice branch from e552c50 to 70da052 Compare March 15, 2023 22:16
@rust-log-analyzer

This comment has been minimized.

@llogiq
Copy link
Contributor Author

llogiq commented Mar 16, 2023

I'm fine with extending offset_of!, too, even if unstably, but as that wasn't ready yet, and the current implementation blocks a LLVM upgrade, I suggested this as a stop-gap measure.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 16, 2023

to unblock the LLVM upgrade we could simply change the body to

match self {
    None => &[],
    Some(x) => std::slice::from_ref(x),
}

Less efficient, but obviously correct

@llogiq llogiq force-pushed the intrinsically-option-as-slice branch 2 times, most recently from 1e7f303 to 50846d5 Compare March 16, 2023 18:30
@rust-log-analyzer

This comment has been minimized.

@JakobDegen
Copy link
Contributor

I don't think this should be done, a niche optimization doesn't justify a compiler intrinsic with all of its maintenance cost imo.

Idk, an intrinsic that gets lowered in mir doesn't really have a lot of maintenance cost imo. Especially with something like this, which we'll reliably be able to support between changes to mir

@oli-obk
Copy link
Contributor

oli-obk commented Mar 17, 2023

The only reason not to do this and pick my inefficient version instead is that it serves as motivation to work on offset_of to support enum variants.

@llogiq llogiq force-pushed the intrinsically-option-as-slice branch from 50846d5 to a2d33d4 Compare March 17, 2023 06:55
@llogiq
Copy link
Contributor Author

llogiq commented Mar 17, 2023

Don't worry about that, @oli-obk, I'll extend offset_of! once it's ready and then remove the intrinsic. Edit: I hope that also alleviates @WaffleLapkin 's objections.

@llogiq
Copy link
Contributor Author

llogiq commented Mar 17, 2023

I should however replace the old implementation with @oli-obk's proposal to ensure that the LLVM build is actually unblocked.

@WaffleLapkin
Copy link
Member

I'll extend offset_of! once it's ready and then remove the intrinsic.

FYI: @drmeepster (author of #106934) had plans on doing this after the PR merges, don't forget to communicate with him, so the work is not duplicated :)

Edit: I hope that also alleviates WaffleLapkin's objections.

I still don't love the idea of adding an intrinsic, but I won't object to merging this in the meantime.

@llogiq
Copy link
Contributor Author

llogiq commented Mar 17, 2023

@eholk do you have time to r+ this? Or should we ask for another reviewer?

@eholk
Copy link
Contributor

eholk commented Mar 18, 2023

The code looks pretty good to me. Is the main reason to do this to unblock the LLVM upgrade, or are there further benefits for it?

@llogiq llogiq force-pushed the intrinsically-option-as-slice branch from 6fa28de to 27e9ee9 Compare March 18, 2023 06:18
@llogiq
Copy link
Contributor Author

llogiq commented Mar 18, 2023

That and the previous implementation was a hack that could result in suboptimal codegen if the type layout algorithm changed in a way that led to a different payload offset between Option<T> and Option<MaybeUninit<T>>. With this implementation that is no longer a concern.

Again, this is only a stop-gap until we can get the same effect via offset_of! as that implementation needs more work and then must still be extended to cover enums.

@llogiq llogiq mentioned this pull request Mar 19, 2023
3 tasks
@llogiq
Copy link
Contributor Author

llogiq commented Mar 20, 2023

@eholk do you need any further information to decide how to proceed?

@eholk
Copy link
Contributor

eholk commented Mar 20, 2023

Looks good to me!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 20, 2023

📌 Commit 27e9ee9 has been approved by eholk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 20, 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).
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2023
Rollup of 11 pull requests

Successful merges:

 - rust-lang#100311 (Fix handling of trailing bare CR in str::lines)
 - rust-lang#108997 (Change text -> rust highlighting in sanitizer.md)
 - rust-lang#109179 (move Option::as_slice to intrinsic)
 - rust-lang#109187 (Render source page layout with Askama)
 - rust-lang#109280 (Remove `VecMap`)
 - rust-lang#109295 (refactor `fn bootstrap::builder::Builder::compiler_for` logic)
 - rust-lang#109312 (rustdoc: Cleanup parent module tracking for doc links)
 - rust-lang#109317 (Update links for custom discriminants.)
 - rust-lang#109405 (RPITITs are `DefKind::Opaque` with new lowering strategy)
 - rust-lang#109414 (Do not consider synthesized RPITITs on missing items checks)
 - rust-lang#109435 (Detect uninhabited types early in const eval)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 14d0646 into rust-lang:master Mar 22, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 22, 2023
@llogiq llogiq deleted the intrinsically-option-as-slice branch March 22, 2023 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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