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

Let MonoReachable traversal evaluate BinOps #129287

Closed
wants to merge 1 commit into from

Conversation

saethlin
Copy link
Member

This fixes the first bug in #129282

I don't know what the right organization is for the code here, but something probably has to get rearranged because this traversal now depends on rustc_const_eval so that it can use an InterpCx internally, so it can't live in rustc_middle.

r? @ghost

@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 Aug 20, 2024
@saethlin
Copy link
Member Author

@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 Aug 20, 2024
@bors
Copy link
Contributor

bors commented Aug 20, 2024

⌛ Trying commit 93ae299 with merge 448299e...

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 20, 2024
Let MonoReachable traversal evaluate BinOps

This fixes the first bug in rust-lang#129282

I don't know what the right organization is for the code here, but something probably has to get rearranged because this traversal now depends on `rustc_const_eval` so that it can use an `InterpCx` internally, so it can't live in `rustc_middle`.

r? `@ghost`
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Aug 20, 2024

💔 Test failed - checks-actions

@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 Aug 20, 2024
@saethlin saethlin force-pushed the mono-reachable-binops branch from 93ae299 to 10b7e0e Compare August 20, 2024 03:03
@scottmcm
Copy link
Member

scottmcm commented Aug 20, 2024

Pondering: Can we just tell people that they have to write this in a const block if they want this? It's not obvious to me that doing a bunch of extra const eval in mono is worth it.

(AKA "we should have const if and a lint encouraging its use in certain places".)

@saethlin
Copy link
Member Author

saethlin commented Aug 20, 2024

Pondering: Can we just tell people that they have to write this in a const block if they want this?
(AKA "we should have const if

I'm not interested in changing or adding to the language. If you want to do that, go right ahead, but it's not my thing. This seems like a useful optimization for the compiler to do, so I'm trying to see how that turns out.

I think the implementation ends up convoluted because of some deeper issues we have that I'm not going to solve. MIR is really not easy to ad-hoc evaluate, and also we don't have any post-mono MIR opts. Post-mono GVN for example would fix this. I'm pasting most of the impl out of GVN to do this.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-lang rust-lang deleted a comment from rust-timer Aug 20, 2024
@bors
Copy link
Contributor

bors commented Aug 20, 2024

⌛ Trying commit 10b7e0e with merge 012c415...

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 20, 2024
Let MonoReachable traversal evaluate BinOps

This fixes the first bug in rust-lang#129282

I don't know what the right organization is for the code here, but something probably has to get rearranged because this traversal now depends on `rustc_const_eval` so that it can use an `InterpCx` internally, so it can't live in `rustc_middle`.

r? `@ghost`
@bors
Copy link
Contributor

bors commented Aug 20, 2024

☀️ Try build successful - checks-actions
Build commit: 012c415 (012c415c2205b3cc9c6f0a157b6c7b45f4418fc4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (012c415): 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 (primary -2.3%)

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)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 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: 749.351s -> 750.569s (0.16%)
Artifact size: 338.82 MiB -> 338.89 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 20, 2024
@RalfJung
Copy link
Member

I'm not interested in changing or adding to the language.

It shouldn't be needed to change the language. This is stable:

  if const { SIZE < 4096 } {
    let arr = [0u8; SIZE];
    std::hint::black_box(&arr);
  } else {
    let vec = vec![0u8; SIZE];
    std::hint::black_box(&vec);
  }

If that works, IMO there's no sufficient justification for piling on hacks like this.

@RalfJung
Copy link
Member

Seems like that doesn't work. We should figure out why. I don't think we want to start integrating general optimizations into the monomorphization step -- if we decide we need post-mono optimizations on the MIR level, we should do it properly and generate MIR for that, not embed a bunch of hard-to-audit on-the-fly transformations hidden in various other passes.

@saethlin
Copy link
Member Author

saethlin commented Aug 22, 2024

We should figure out why.

We already know why: #129283

if we decide we need post-mono optimizations on the MIR level, we should do it properly and generate MIR for that,

This would require a broader discussion about either deciding how much compile time regression is acceptable or ripping out out codegen canonicalization from MIR to reduce it. I demonstrated that one of those is required in #125025

@saethlin saethlin added S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 22, 2024
@RalfJung
Copy link
Member

We already know why: #129283

So #129283 plus using const { ... } is enough to fix #129282? IMO that's enough. It's a debug build, after all, it's not even asking to be optimized.

@saethlin
Copy link
Member Author

there's no sufficient justification for piling on hacks like this.

This PR is an experiment. Unfortunately I can't collect perf results without posting a PR. I've set the status to "experimental" instead of "waiting on author" is that more clear?

@RalfJung
Copy link
Member

RalfJung commented Aug 22, 2024 via email

// SwitchInt(_1)
//
// And MIR for if intrinsics::ub_checks() looks like this:
// _1 = UbChecks()
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just make this a variant for mir::Const and avoid having this logic?

Copy link
Member

Choose a reason for hiding this comment

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

No, that would be really odd.

We could consider making UbChecks a const (giving it a DefId), similar to how we are making nullary intrinsics effectively consts. However, UbChecks is not actually a const: its value depends on tcx.sess which can have different values in different crates across the crate graph!

@saethlin
Copy link
Member Author

saethlin commented Oct 6, 2024

The merge conflicts here are a mess, so I don't plan on fixing them. I think the way to get such an operation merged is by doing post-mono GVN or something like that.

@saethlin saethlin closed this Oct 6, 2024
@saethlin saethlin deleted the mono-reachable-binops branch October 6, 2024 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. 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