-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 SingleUseConsts
mir-opt pass
#125910
Add SingleUseConsts
mir-opt pass
#125910
Conversation
a02993b
to
fea5299
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Add `SingleUseConsts` mir-opt pass r? ghost
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST; | ||
switchInt(move _7) -> [0: bb1, otherwise: bb2]; | ||
StorageLive(_9); | ||
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main impetus for this change. By doing this rewrite, it'll be easier for other things to see that it's a switch on a constant. For example, the inliner wants to lower the cost of inlining such a branch, and codegen wants to use it to emit a goto instead of a switch, even in debug.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (d1611ef): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking 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. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)Results (primary -3.9%, secondary -2.6%)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.
CyclesResults (primary -0.8%, secondary -1.6%)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.
Binary sizeResults (primary -0.2%, secondary -0.5%)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.
Bootstrap: 667.872s -> 669.455s (0.24%) |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Add `SingleUseConsts` mir-opt pass r? ghost
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (620bf99): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking 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. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)Results (primary -3.5%, secondary -3.5%)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.
CyclesResults (primary 0.9%, secondary -0.5%)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.
Binary sizeResults (primary -0.2%, secondary -0.6%)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.
Bootstrap: 670.518s -> 671.554s (0.15%) |
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
let StatementKind::Assign(place_and_rvalue) = init_statement.kind else { | ||
bug!("No longer an assign?"); | ||
}; | ||
let (place, rvalue) = *place_and_rvalue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you deliberately avoiding using a box pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never even thought about using one -- my mental "how do I write a pattern for this" doesn't use the unstable stuff.
Since this is infallible, my instinct is to not use it. When it makes the control flow way easier in a refutable pattern then sure I'd say to use a box pattern, but here that's not the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mm I meant if you use a box pattern you can combine these two lines. As far as I'm aware, a box pattern is the only way to do so.
But if you don't want to that's fine. I'm just used to seeing box patterns used for matching on StatementKind::Assign.
bug!("No longer an assign?"); | ||
}; | ||
let (place, rvalue) = *place_and_rvalue; | ||
debug_assert_eq!(place.as_local(), Some(local)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a perf argument for enabling this only in debug builds? As a general rule, I prefer to have such checks either on for everyone. If a check for some absurd circumstance is going to trip, it seems more likely to trip in the wild than in CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a perf argument for it. I wrote it more as documentation than expecting it to ever fire -- a "this thing we just popped was the assignment to the local".
I can make it an assert_eq
if you want 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, changed to assert_eq!
.
So why haven't we deleted the pass? I know we have somewhat of a tradition of keeping MIR transforms that are off by default or broken in the tree and I think that's extremely dubious. Especially in this case. What value will someone get from |
d543a8e
to
82e331d
Compare
This comment has been minimized.
This comment has been minimized.
82e331d
to
8fbab18
Compare
Added a new commit that deletes it (and rebased for tidy). |
Thanks! |
☀️ Test successful - checks-actions |
Finished benchmarking commit (fa1681c): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDNext Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)Results (primary -0.2%, secondary 5.6%)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.
CyclesResults (secondary -5.2%)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.
Binary sizeResults (primary -0.1%, secondary -0.6%)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.
Bootstrap: 673.568s -> 675.712s (0.32%) |
Add `SingleUseConsts` mir-opt pass The goal here is to make a pass that can be run in debug builds to simplify the common case of constants that are used just once -- that doesn't need SSA handling and avoids any potential downside of multi-use constants. In particular, to simplify the `if T::IS_ZST` pattern that's common in the standard library. By also handling the case of constants that are *never* actually used this fully replaces the `ConstDebugInfo` pass, since it has all the information needed to do that naturally from the traversal it needs to do anyway. This is roughly a wash on instructions on its own (a couple regressions, a few improvements rust-lang/rust#125910 (comment)), with a bunch of size improvements. So I'd like to land it as its own PR, then do follow-ups to take more advantage of it (in the inliner, cg_ssa, etc). r? `@saethlin`
Wash on instruction counts, but nice binary size wins. The cycles improvements on @rustbot label: +perf-regression-triaged |
Related changes: - rust-lang/rust#125910: Introduces a new constant propagation pass which broke Kani coverage tests. For now, disable this pass if coverage is enabled. - rust-lang/rust#126410: Rename ConstOperands
Related changes: - rust-lang/rust#125910: Introduces a new constant propagation pass which broke Kani coverage tests. For now, disable this pass if coverage is enabled. - rust-lang/rust#126410: Rename ConstOperands Resolves #3260
Add `SingleUseConsts` mir-opt pass The goal here is to make a pass that can be run in debug builds to simplify the common case of constants that are used just once -- that doesn't need SSA handling and avoids any potential downside of multi-use constants. In particular, to simplify the `if T::IS_ZST` pattern that's common in the standard library. By also handling the case of constants that are *never* actually used this fully replaces the `ConstDebugInfo` pass, since it has all the information needed to do that naturally from the traversal it needs to do anyway. This is roughly a wash on instructions on its own (a couple regressions, a few improvements rust-lang/rust#125910 (comment)), with a bunch of size improvements. So I'd like to land it as its own PR, then do follow-ups to take more advantage of it (in the inliner, cg_ssa, etc). r? `@saethlin`
The goal here is to make a pass that can be run in debug builds to simplify the common case of constants that are used just once -- that doesn't need SSA handling and avoids any potential downside of multi-use constants. In particular, to simplify the
if T::IS_ZST
pattern that's common in the standard library.By also handling the case of constants that are never actually used this fully replaces the
ConstDebugInfo
pass, since it has all the information needed to do that naturally from the traversal it needs to do anyway.This is roughly a wash on instructions on its own (a couple regressions, a few improvements #125910 (comment)), with a bunch of size improvements. So I'd like to land it as its own PR, then do follow-ups to take more advantage of it (in the inliner, cg_ssa, etc).
r? @saethlin