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

assume value ranges in transmute #109993

Merged
merged 2 commits into from
Apr 20, 2023
Merged

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Apr 6, 2023

Fixes #109958

@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2023

r? @lcnr

(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 Apr 6, 2023
@scottmcm
Copy link
Member Author

scottmcm commented Apr 6, 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 Apr 6, 2023
@bors
Copy link
Contributor

bors commented Apr 6, 2023

⌛ Trying commit c7d3c9f10a4c1b95a290936fc1acd585e42f1f5e with merge b359f2b4f1c26bbaf475b4f8cdaa87a57a4f3d82...

@bors
Copy link
Contributor

bors commented Apr 6, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b359f2b4f1c26bbaf475b4f8cdaa87a57a4f3d82): comparison URL.

Overall result: no relevant changes - 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 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)
1.6% [1.6%, 1.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-2.7%, -1.3%] 2
All ❌✅ (primary) - - 0

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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.9% [-4.0%, -3.8%] 3
All ❌✅ (primary) - - 0

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

oli-obk commented Apr 6, 2023

For as casts we did this in MIR. Can this be done here, too? Ideally sharing the code?

@lcnr
Copy link
Contributor

lcnr commented Apr 10, 2023

r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned lcnr Apr 10, 2023
@scottmcm
Copy link
Member Author

@oli-obk I think it depends if it's supposed to work on generics or not.

If something like #106281 (comment) happens, then at MIR level we wouldn't reliably know what the type actually is to add these -- MIR could just see type parameters.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 11, 2023

Duh... yea, the enum to int casts are not possible on generics, so we never had an issue with that there.

@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 Apr 11, 2023
@scottmcm
Copy link
Member Author

Finally got back to this.

I've added a bunch of codegen tests to demonstrate that this works as expected, as well as some clarification comments.

@rustbot ready

@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 Apr 20, 2023
@oli-obk
Copy link
Contributor

oli-obk commented Apr 20, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Apr 20, 2023

📌 Commit baf98e7 has been approved by oli-obk

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

bors commented Apr 20, 2023

⌛ Testing commit baf98e7 with merge 7e23d18...

@bors
Copy link
Contributor

bors commented Apr 20, 2023

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 7e23d18 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 20, 2023
@bors bors merged commit 7e23d18 into rust-lang:master Apr 20, 2023
@rustbot rustbot added this to the 1.71.0 milestone Apr 20, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7e23d18): 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)
-1.8% [-1.8%, -1.8%] 1
All ❌✅ (primary) - - 0

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

Cycles

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

@scottmcm scottmcm deleted the transmute-niches branch April 20, 2023 17:37
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.

Missing optimization when transmuting enums
6 participants