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

Simplify transmutes in MIR InstCombine #109612

Merged
merged 1 commit into from
Mar 29, 2023

Conversation

scottmcm
Copy link
Member

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

With this PR, it can also simplify them to IntToInt casts, Discriminant reads, or Field projections.

@rustbot
Copy link
Collaborator

rustbot commented Mar 25, 2023

r? @cjgillot

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

rustbot commented Mar 25, 2023

These commits modify the Cargo.lock file. Random changes to Cargo.lock can be introduced when switching branches and rebasing PRs.
This was probably unintentional and should be reverted before this PR is merged.

If this was intentional then you can ignore this comment.

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@JakobDegen
Copy link
Contributor

One actionable comment, rest lgtm

@scottmcm scottmcm force-pushed the instcombine-transmutes branch 2 times, most recently from 169dfcc to df0e875 Compare March 25, 2023 22:48
@bors
Copy link
Contributor

bors commented Mar 26, 2023

☔ The latest upstream changes (presumably #106428) made this pull request unmergeable. Please resolve the merge conflicts.

@scottmcm scottmcm force-pushed the instcombine-transmutes branch 4 times, most recently from c7ec176 to 36d3ea4 Compare March 27, 2023 05:34
@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Mar 27, 2023
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 28, 2023
@cjgillot
Copy link
Contributor

@bors r+ rollup=iffy

@bors
Copy link
Contributor

bors commented Mar 28, 2023

📌 Commit 69ca902a689f775ca1382738158944d7c4fa259f has been approved by cjgillot

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 28, 2023
@bors
Copy link
Contributor

bors commented Mar 28, 2023

⌛ Testing commit 69ca902a689f775ca1382738158944d7c4fa259f with merge db6c420d803168d261a0e766671cbce72d98328c...

@bors
Copy link
Contributor

bors commented Mar 28, 2023

💔 Test failed - checks-actions

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Mar 28, 2023
@bors bors added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 28, 2023
@scottmcm
Copy link
Member Author

@bors retry ("Connection reset by peer (os error 104)" in test infra)

@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 28, 2023
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 28, 2023

⌛ Testing commit 69ca902a689f775ca1382738158944d7c4fa259f with merge f96b6ad273fdc9ef73e191b65d127d9a7546a0d1...

@bors
Copy link
Contributor

bors commented Mar 28, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 28, 2023
@rust-log-analyzer

This comment has been minimized.

@ehuss
Copy link
Contributor

ehuss commented Mar 28, 2023

@bors r-

(was inadvertently re-queued by a homu sync)

@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-review Status: Awaiting review from the assignee but also interested parties. labels Mar 28, 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.
@scottmcm
Copy link
Member Author

scottmcm commented Mar 29, 2023

Rebased and made the test a // unit-test: InstCombine to hopefully not hit another difference.

@bors r=cjgillot

@bors
Copy link
Contributor

bors commented Mar 29, 2023

📌 Commit f20af8d has been approved by cjgillot

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 Mar 29, 2023
@bors
Copy link
Contributor

bors commented Mar 29, 2023

⌛ Testing commit f20af8d with merge acd27bb...

@bors
Copy link
Contributor

bors commented Mar 29, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing acd27bb to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 29, 2023
@bors bors merged commit acd27bb into rust-lang:master Mar 29, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 29, 2023
@scottmcm scottmcm deleted the instcombine-transmutes branch March 29, 2023 04:25
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (acd27bb): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

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)
-0.1% [-0.1%, -0.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.1%, -0.1%] 1

Cycles

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

bors added a commit to rust-lang-ci/rust that referenced this pull request May 17, 2023
Stop turning transmutes into discriminant reads in mir-opt

Partially reverts rust-lang#109612, as after rust-lang#109993 these aren't actually equivalent any more, and I'm no longer confident this was ever an improvement in the first place.

Having this "simplification" meant that similar-looking code actually did somewhat different things.  For example,
```rust
pub unsafe fn demo1(x: std::cmp::Ordering) -> u8 {
    std::mem::transmute(x)
}
pub unsafe fn demo2(x: std::cmp::Ordering) -> i8 {
    std::mem::transmute(x)
}
```
in nightly today is generating <https://rust.godbolt.org/z/dPK58zW18>
```llvm
define noundef i8 `@_ZN7example5demo117h341ef313673d2ee6E(i8` noundef %x) unnamed_addr #0 {
  %0 = icmp uge i8 %x, -1
  %1 = icmp ule i8 %x, 1
  %2 = or i1 %0, %1
  call void `@llvm.assume(i1` %2)
  ret i8 %x
}

define noundef i8 `@_ZN7example5demo217h5ad29f361a3f5700E(i8` noundef %0) unnamed_addr #0 {
  %x = alloca i8, align 1
  store i8 %0, ptr %x, align 1
  %1 = load i8, ptr %x, align 1, !range !2, !noundef !3
  ret i8 %1
}
```

Which feels too different when the original code is essentially identical.

---

Aside: that example is different *after* optimizations too:
```llvm
define noundef i8 `@_ZN7example5demo117h341ef313673d2ee6E(i8` noundef returned %x) unnamed_addr #0 {
  %0 = add i8 %x, 1
  %1 = icmp ult i8 %0, 3
  tail call void `@llvm.assume(i1` %1)
  ret i8 %x
}

define noundef i8 `@_ZN7example5demo217h5ad29f361a3f5700E(i8` noundef returned %0) unnamed_addr #1 {
  ret i8 %0
}
```
so turning the `Transmute` into a `Discriminant` was arguably just making things worse, so leaving it alone instead -- and thus having less code in rustc -- seems clearly better.
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