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 an InstCombine for redundant casts #108246

Merged
merged 1 commit into from
Feb 22, 2023

Conversation

saethlin
Copy link
Member

@rustbot label +A-mir-opt

@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2023

r? @eholk

(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. labels Feb 19, 2023
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added the A-mir-opt Area: MIR optimizations label Feb 19, 2023
Comment on lines 4 to 12
pub fn redundant(x: *const u8) -> *const u8 {
x as *const u8
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this testing anything? Rust playground is telling me that this hasn't changed anything from the MIR that gets emitted for redundant on stable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure wasn't testing anything. I wish we had a better way to test stuff like this. The easy way out is to look at the pass's diff, but I don't care care very much what this particular pass does, only that the pattern gets optimized away.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should definitely have a // unit-test for this case. Specifically, I'd suggest

// unit-test InstCombine

pub fn redundant_primitive_cast(x: *mut u8) -> *mut u8 {
    x as *mut u8
}

pub fn redundant_cast_with_lifetimes<'a, 'b: 'a>(x: *const &'b u8) -> *const &'a u8 {
    x as *mut u8
}

I don't care care very much what this particular pass does

The goal of this kind of a test is to protect against someone introducing a bug into the pass that causes it not to do anything anymore (accidentally adds an if false, case gets forgotten in a refactor, that type of thing). You can additionally have a casts_e2e.rs test with all the PreCodegen.after examples you'd like.

Copy link
Member Author

@saethlin saethlin Feb 20, 2023

Choose a reason for hiding this comment

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

The InstCombine.diff for those examples are empty. I feel like there's something in MIR building that is skipping emitting the cast, and the situation I'm trying to optimize here only arises after inlining (which is why my test is the way it is)

@@ -142,6 +143,14 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
}
}

fn combine_cast(&self, _source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Cast(_kind, operand, ty) = rvalue {
if operand.ty(self.local_decls, self.tcx) == *ty {
Copy link
Member

Choose a reason for hiding this comment

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

Are regions erased in the MIR body here? Otherwise this won't apply any time a lifetime is present in the type being casted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, they should be. Seems like a good candidate for a test case though

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm. I made an attempt, but I do not think it is optimal. Probably good enough though?

@eholk
Copy link
Contributor

eholk commented Feb 21, 2023

@bors r? @compiler-errors

Since you're already looking at it :)

@rustbot rustbot assigned compiler-errors and unassigned eholk Feb 21, 2023
@compiler-errors
Copy link
Member

Yeah, good enough for me.

@bors r+

@bors
Copy link
Contributor

bors commented Feb 21, 2023

📌 Commit 0e05280 has been approved by compiler-errors

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 Feb 21, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2023
…llaumeGomez

Rollup of 8 pull requests

Successful merges:

 - rust-lang#108110 (Move some `InferCtxt` methods to `EvalCtxt` in new solver)
 - rust-lang#108168 (Fix ICE on type alias in recursion)
 - rust-lang#108230 (Convert a hard-warning about named static lifetimes into lint "unused_lifetimes")
 - rust-lang#108239 (Fix overlapping spans in removing extra arguments)
 - rust-lang#108246 (Add an InstCombine for redundant casts)
 - rust-lang#108264 (no-fail-fast support for tool testsuites)
 - rust-lang#108310 (rustdoc: Fix duplicated attributes for first reexport)
 - rust-lang#108318 (Remove unused FileDesc::get_cloexec)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4658210 into rust-lang:master Feb 22, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 22, 2023
@saethlin saethlin deleted the instcombine-redundant-casts branch March 15, 2023 00:33
scottmcm added a commit to scottmcm/rust that referenced this pull request Mar 29, 2023
Thanks to the combination of rust-lang#108246 and rust-lang#108442 it could already remove identity transmutes.

With this PR, it can also simplify them to `IntToInt` casts, `Discriminant` reads, or `Field` projections.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 29, 2023
…jgillot

Simplify transmutes in MIR InstCombine

Thanks to the combination of rust-lang#108246 and rust-lang#108442 it could already remove identity transmutes.

With this PR, it can also simplify them to `IntToInt` casts, `Discriminant` reads, or `Field` projections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations 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.

6 participants