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

Ban ArrayToPointer and MutToConstPointer from runtime MIR #126308

Merged
merged 2 commits into from
Jun 20, 2024

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Jun 12, 2024

Zulip conversation: https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/CastKind.3A.3APointerCoercion.20in.20Runtime.20MIR/near/443955195

Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as PtrToPtr does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)

r? mir

@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 Jun 12, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jun 12, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

@RalfJung
Copy link
Member

RalfJung commented Jun 12, 2024

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)

How that? Can't GVN use an "or" pattern to cover all these cases together?

To me, banning them seems more complicated than simply allowing them.

@@ -506,6 +507,7 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
/// Returns the sequence of passes that lowers analysis to runtime MIR.
fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let passes: &[&dyn MirPass<'tcx>] = &[
&coercions_to_casts::CoercionsToCasts,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not inside 'cleanup_post_borrowck' ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because I was thinking of it as part of Analysis->Runtime, and that pass is inside Analysis Initial->PostCleanup.

I could move it there and make it part of the https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/syntax/enum.AnalysisPhase.html#variant.PostCleanup disallow list if you'd prefer? (I don't have particularly strong opinions about exactly when it should happen, so I'll take your advice here.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about this more, and decided that it's easier to write the why comment when it's in cleanup_post_borrowck, so I moved it there, and adjusted them to be banned starting in Analysis(PostCleanup) rather than just in Runtime(..).

@scottmcm
Copy link
Member Author

How that? Can't GVN use an "or" pattern to cover all these cases together?

I see it less as being about the code that's typed are more about "is it clear how you should think about things".

For example, today GVN groups these three here

CastKind::PointerCoercion(
ty::adjustment::PointerCoercion::MutToConstPointer
| ty::adjustment::PointerCoercion::ArrayToPointer
| ty::adjustment::PointerCoercion::UnsafeFnPointer,
) => {

but then only has PtrToPtr and MutToConstPointer in

if let PtrToPtr | PointerCoercion(MutToConstPointer) = kind
&& let Value::Cast { kind: inner_kind, value: inner_value, from: inner_from, to: _ } =
*self.get(value)
&& let PtrToPtr | PointerCoercion(MutToConstPointer) = inner_kind

Is that right? Should the former have MutToConstPointer in the same arm as PtrToPtr? Should the latter have ArrayToPointer in it too? I don't know.

I think it's much simpler when the answer to those things is "as it says on CastKind and MirPhase::Runtime, you only need PtrToPtr here; ignore the rest".

@RalfJung
Copy link
Member

RalfJung commented Jun 13, 2024

I think it's much simpler when the answer to those things is "as it says on CastKind and MirPhase::Runtime, you only need PtrToPtr here; ignore the rest".

What about grouping all of these together in a single CastKind variant PtrToPtr(PtrCastKind), and declaring that operationally, the PtrCastKind makes no difference, it just exists for borrowck? Then it's easy to consistently match on it and ignore the irrelevant details in mir-opts.

This is not a strong opinion though, I just feel like this PR is using the MIR validation to work around CastKind just not being of the right shape for some of its consumers.

@@ -143,6 +143,9 @@ pub enum RuntimePhase {
/// * [`TerminatorKind::CoroutineDrop`]
/// * [`Rvalue::Aggregate`] for any `AggregateKind` except `Array`
/// * [`PlaceElem::OpaqueCast`]
/// * [`CastKind::PointerCoercion`] with any of the following:
/// * [`PointerCoercion::ArrayToPointer`]
/// * [`PointerCoercion::MutToConstPointer`]
Copy link
Member

Choose a reason for hiding this comment

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

compiler/rustc_const_eval/src/interpret/cast.rs should then probably also explicitly panic on these casts.

@RalfJung
Copy link
Member

Ah, we have have this inner enum, PointerCoercion... but that enum is a pretty poor fit for runtime MIR in general, it is designed for coercion generation way earlier in the compiler. I wonder if we'd be better off just not using it in MIR... but I have no idea how this is relevant for borrowck.

@scottmcm
Copy link
Member Author

but I have no idea how this is relevant for borrowck.

I first tried just banning these from MIR altogether, but doing that meant there were some missing errors in one of the borrowck tests. I don't have the failure handy, but I think it was this one:

fn mut_to_const<'a, 'b>(x: *mut &'a i32) -> *const &'b i32 {
x //~ ERROR
}

So it's checks about lifetimes inside the pointee, I guess?

@RalfJung
Copy link
Member

I would expect the lifetimes in that test to be a red herring -- mut_to_const is the key. Someone has to reject that as a coercion (but allow it as a cast).

@RalfJung
Copy link
Member

Ah no never mind, I shouldn't write comments when I am too tired...

on a mut-to-const implicit coercion, the type needs to stay the same, and the lifetime equality part of "the same" is checked by borrowck.

@scottmcm
Copy link
Member Author

but that enum is a pretty poor fit for runtime MIR in general, it is designed for coercion generation way earlier in the compiler. I wonder if we'd be better off just not using it in MIR...

So I agree with this, but I'm not sure how to action on it. I don't really want to change https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/thir/enum.ExprKind.html#variant.PointerCoercion, and if we're stuck representing the distinction in MIR anyway, I don't know how helpful restructuring it would be.

What I've grokked here is that early MIR does need this distinction for borrowchecking coercions, but that coercion vs cast distinction is irrelevant in runtime MIR. So it doesn't sound that bad to me to just ban the borrowck-parts in later MIR phases, as I'm doing here? Even if we rearranged these cases to different CastKinds, I think I'd still rather just normalize things so there's fewer things MIR consumers have to deal with.

@RalfJung
Copy link
Member

I don't have a concrete proposal either, and it seems you considered the alternatives, so my concern isn't blocking. I feel like there's got to be a better way but don't feel strongly enough about it to try and find it. ;)

@rustbot
Copy link
Collaborator

rustbot commented Jun 15, 2024

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@saethlin
Copy link
Member

I'm okay with this change because it makes the runtime MIR dialect more sensible. I don't know how much this actually matters in practice, clearly it lets you clean up MIR transforms if you know what you're doing. But the fact that the enum variants still exist because all MIR dialects are the same type still makes implementing transforms confusing.

@bors r+

@bors
Copy link
Contributor

bors commented Jun 18, 2024

📌 Commit 64bba35 has been approved by saethlin

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 Jun 18, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jun 19, 2024
…thlin

Ban `ArrayToPointer` and `MutToConstPointer` from runtime MIR

Zulip conversation: <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/CastKind.3A.3APointerCoercion.20in.20Runtime.20MIR/near/443955195>

Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)

r? mir
fmease added a commit to fmease/rust that referenced this pull request Jun 19, 2024
…thlin

Ban `ArrayToPointer` and `MutToConstPointer` from runtime MIR

Zulip conversation: <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/CastKind.3A.3APointerCoercion.20in.20Runtime.20MIR/near/443955195>

Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)

r? mir
@fmease
Copy link
Member

fmease commented Jun 19, 2024

#126669 (comment)
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 19, 2024
Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)
@scottmcm
Copy link
Member Author

Rebased and re-blessed.

@bors r=saethlin rollup=iffy (already failed in a rollup once)

@bors
Copy link
Contributor

bors commented Jun 19, 2024

📌 Commit e04e351 has been approved by saethlin

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 19, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jun 19, 2024
…thlin

Ban `ArrayToPointer` and `MutToConstPointer` from runtime MIR

Zulip conversation: <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/CastKind.3A.3APointerCoercion.20in.20Runtime.20MIR/near/443955195>

Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)

r? mir
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 19, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#124807 (Migrate `run-make/rustdoc-io-error` to `rmake.rs`)
 - rust-lang#126095 (Migrate `link-args-order`, `ls-metadata` and `lto-readonly-lib` `run-make` tests to `rmake`)
 - rust-lang#126308 (Ban `ArrayToPointer` and `MutToConstPointer` from runtime MIR)
 - rust-lang#126620 (Actually taint InferCtxt when a fulfillment error is emitted)
 - rust-lang#126629 (Migrate `run-make/compressed-debuginfo` to `rmake.rs`)
 - rust-lang#126644 (Rewrite `extern-flag-rename-transitive`. `debugger-visualizer-dep-info`, `metadata-flag-frobs-symbols`, `extern-overrides-distribution` and `forced-unwind-terminate-pof` `run-make` tests to rmake)
 - rust-lang#126650 (Rename a bunch of things in the new solver and `rustc_type_ir`)

r? `@ghost`
`@rustbot` modify labels: rollup
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jun 19, 2024
…thlin

Ban `ArrayToPointer` and `MutToConstPointer` from runtime MIR

Zulip conversation: <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/CastKind.3A.3APointerCoercion.20in.20Runtime.20MIR/near/443955195>

Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)

r? mir
@bors
Copy link
Contributor

bors commented Jun 19, 2024

⌛ Testing commit e04e351 with merge 3d5d7a2...

@bors
Copy link
Contributor

bors commented Jun 20, 2024

☀️ Test successful - checks-actions
Approved by: saethlin
Pushing 3d5d7a2 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 20, 2024
@bors bors merged commit 3d5d7a2 into rust-lang:master Jun 20, 2024
7 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jun 20, 2024
@bors bors mentioned this pull request Jun 20, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3d5d7a2): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -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
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -2.4%, secondary 3.9%)

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.9% [3.1%, 5.4%] 3
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Cycles

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

Binary size

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

Bootstrap: 692.485s -> 691.149s (-0.19%)
Artifact size: 323.83 MiB -> 323.84 MiB (0.01%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants