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

Pessimistically assume opaque types are !Freeze #113617

Closed
wants to merge 3 commits into from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jul 12, 2023

fixes #112602

This PR fixes a cycle bug, and thus rely entirely on the body-analysis of the constant, instead of trying to look at the opaque type's hidden type. The main issue is that const fn and const items share their analysis, even though const fn don't care about the resulting qualifications, and only need them within the body to detect currently-illegal things like mutable references or interior mutability behind references. If we pessimistically assume opaque types have interior mutability, we can avoid the cycle errors, but are still respecting the feature gates and soundness.

As the tests I added show, we assume that opaque types have interior mutable data, even if the hidden type is a String. Note that we have a body-analysis, which shows that for None or () there can be no interior mutability, and thus appear to be looking behind an opaque type, when we are in fact looking at a constant's body.

r? lang

@rustbot
Copy link
Collaborator

rustbot commented Jul 12, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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. labels Jul 12, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jul 12, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@oli-obk oli-obk added T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 12, 2023
Comment on lines -300 to +319
let const_qualifs = tcx.mir_const_qualif(def);

let const_qualifs = match tcx.def_kind(def) {
DefKind::Fn | DefKind::AssocFn | DefKind::Closure
if tcx.constness(def) == hir::Constness::Const
|| tcx.is_const_default_method(def.to_def_id()) =>
{
tcx.mir_const_qualif(def)
}
DefKind::AssocConst
| DefKind::Const
| DefKind::Static(_)
| DefKind::InlineConst
| DefKind::AnonConst => tcx.mir_const_qualif(def),
_ => ConstQualifs::default(),
};
Copy link
Contributor Author

@oli-obk oli-obk Jul 12, 2023

Choose a reason for hiding this comment

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

This change does not affect any logic and just reduces the number of mir_const_qualif calls, and thus hopefully also reduces the cost of caching things

@oli-obk
Copy link
Contributor Author

oli-obk commented Jul 12, 2023

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 12, 2023
@bors
Copy link
Contributor

bors commented Jul 12, 2023

⌛ Trying commit b39f459 with merge edce3b8a2c0692991244661e01c4b20430e8c3ed...

@bors
Copy link
Contributor

bors commented Jul 12, 2023

☀️ Try build successful - checks-actions
Build commit: edce3b8a2c0692991244661e01c4b20430e8c3ed (edce3b8a2c0692991244661e01c4b20430e8c3ed)

@rust-timer

This comment has been minimized.

@lukas-code
Copy link
Member

Note that this can break some programs that currently compile on stable, for example:

const fn foo() -> impl Sized {}

const _: () = {
    let x = foo();
    let _ = &x;
    core::mem::forget(x)
};

Is this considered acceptable breakage? I guess you do have to go pretty out of your way to write code like that.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (edce3b8a2c0692991244661e01c4b20430e8c3ed): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.6% [-0.7%, -0.6%] 6
Improvements ✅
(secondary)
-0.6% [-0.6%, -0.6%] 3
All ❌✅ (primary) -0.6% [-0.7%, -0.6%] 6

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.8% [-2.1%, -1.3%] 3
Improvements ✅
(secondary)
-2.6% [-3.2%, -1.9%] 5
All ❌✅ (primary) -1.8% [-2.1%, -1.3%] 3

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.1% [2.6%, 3.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 657.866s -> 658.453s (0.09%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 13, 2023
@oli-obk
Copy link
Contributor Author

oli-obk commented Oct 25, 2023

closing in favor of #113169

@oli-obk oli-obk closed this Oct 25, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 25, 2023
Only call `mir_const_qualif` if absolutely necessary

Pull the perf change out of rust-lang#113617

This should not have any impact on behaviour (if it does, we'll see an ICE)
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 25, 2023
Only call `mir_const_qualif` if absolutely necessary

Pull the perf change out of rust-lang#113617

This should not have any impact on behaviour (if it does, we'll see an ICE)
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 27, 2023
…rochenkov

Only call `mir_const_qualif` if absolutely necessary

Pull the perf change out of rust-lang#113617

This should not have any impact on behaviour (if it does, we'll see an ICE)
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Oct 28, 2023
Only call `mir_const_qualif` if absolutely necessary

Pull the perf change out of rust-lang/rust#113617

This should not have any impact on behaviour (if it does, we'll see an ICE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cycle detected when computing type, but only when function is const
6 participants