-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Remove fewer Storage calls in CopyProp and GVN #142531
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
base: master
Are you sure you want to change the base?
Remove fewer Storage calls in CopyProp and GVN #142531
Conversation
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…try> Remove fewer Storage calls in `copy_prop` Modify the `copy_prop` MIR optimization pass to remove fewer `Storage{Live,Dead}` calls, allowing for better optimizations by LLVM - see #141649. ### Details This is my attempt to fix the mentioned issue (this is the first part, I also implemented a similar solution for GVN in [this branch](https://github.com/rust-lang/rust/compare/master...ohadravid:rust:better-storage-calls-gvn-v2?expand=1)). The idea is to use the `MaybeStorageDead` analysis and remove only the storage calls of `head`s that are maybe-storage-dead when the associated `local` is accessed (or, conversely, keep the storage of `head`s that are for-sure alive in _every_ relevant access). When combined with the GVN change, the final example in the issue (#141649 (comment)) is optimized as expected by LLVM. I also measured the effect on a few functions in `rav1d` (where I originally saw the issue) and observed reduced stack usage in several of them. This is my first attempt at working with MIR optimizations, so it's possible this isn't the right approach — but all tests pass, and the resulting diffs appear correct. r? tmiasko since he commented on the issue and pointed to these passes.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (ef7d206): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 0.7%, secondary 3.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -0.6%, secondary -0.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.0%, secondary 0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 757.399s -> 756.065s (-0.18%) |
@matthiaskrgr - I updated the impl to stop re-checking once a head is found to be maybe-dead, which should be a bit better |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…try> Remove fewer Storage calls in `copy_prop` Modify the `copy_prop` MIR optimization pass to remove fewer `Storage{Live,Dead}` calls, allowing for better optimizations by LLVM - see #141649. ### Details This is my attempt to fix the mentioned issue (this is the first part, I also implemented a similar solution for GVN in [this branch](https://github.com/rust-lang/rust/compare/master...ohadravid:rust:better-storage-calls-gvn-v2?expand=1)). The idea is to use the `MaybeStorageDead` analysis and remove only the storage calls of `head`s that are maybe-storage-dead when the associated `local` is accessed (or, conversely, keep the storage of `head`s that are for-sure alive in _every_ relevant access). When combined with the GVN change, the final example in the issue (#141649 (comment)) is optimized as expected by LLVM. I also measured the effect on a few functions in `rav1d` (where I originally saw the issue) and observed reduced stack usage in several of them. This is my first attempt at working with MIR optimizations, so it's possible this isn't the right approach — but all tests pass, and the resulting diffs appear correct. r? tmiasko since he commented on the issue and pointed to these passes.
Should this check happen in |
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
I'm not sure how to make this work: using Is there a different way to do this? |
Finished benchmarking commit (c0a2949): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -0.1%, secondary -1.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -1.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary -0.0%, secondary 0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 756.494s -> 757.685s (0.16%) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
f282ae6
to
dcb58d1
Compare
This comment has been minimized.
This comment has been minimized.
dcb58d1
to
ad0ab67
Compare
This comment has been minimized.
This comment has been minimized.
ad0ab67
to
aa11a50
Compare
This comment has been minimized.
This comment has been minimized.
365edc7
to
9beb9b6
Compare
Remove fewer Storage calls in GVN Followup to #142531 (Remove fewer Storage calls in `copy_prop`) Modify the GVN MIR optimization pass to remove fewer Storage{Live,Dead} calls, allowing for better optimizations by LLVM - see #141649. After replacing locals with values, use the `MaybeStorageDead` analysis to check that the replaced locals are storage-live. **A slight problem**: In #142531, `@tmiasko` noted #142531 (comment) that `MaybeStorageDead` isn't enough since there can be a `Live(_1); Dead(_1); Live(_1);` block which forces the optimization to check that each value is initialised (and not only storage-live). This is easy enough in `copy_prop` (because we are checking _before_ the replacement), but in GVN it is actually hard to tell for each statement if the local must be initialized or not after the fact (and modifying `VnState` seems even harder). I opted for something else which might be wrong (implemented in the last two commits): If we consider `Dead->Live` to be the same as `Deinit`, than such a local shouldn't be considered SSA - so I updated `SsaVisitor` to mark such cases as non-SSA. r? tmiasko
This comment has been minimized.
This comment has been minimized.
9beb9b6
to
6078a69
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@ohadravid do you mind merging this PR and #142819? Both should use the same code to decide whether to keep or remove storage statements. And I fear that having 2 PRs mean that @tmiasko and I won't see each other ideas and give you diverging advice. |
… to remove fewer storage statements
…r storage statements
fdcc8a6
to
26fc160
Compare
@cjgillot , @tmiasko - merged both PR here. Current impls are based on the new Does GVN require an additional check against borrowed locals like mentioned in #142531 (comment)? Both only do the more complex analysis when And thank you both for reviewing these and explaining everything! 🙏 |
copy_prop
let mut head_storage_to_check = DenseBitSet::new_empty(fully_moved.domain_size()); | ||
let mut storage_to_remove = DenseBitSet::new_empty(fully_moved.domain_size()); |
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.
The information stored in head_storage_to_check
is redundant, since one can always examine storage_to_remove
instead. Can you remove head_storage_to_check
?
storage_to_remove, | ||
}; | ||
|
||
storage_checker.visit_body(body); |
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.
Visit only reachable blocks with traversal::reachable
. By default the dataflow engine prohibits obtaining results from unreachable blocks (there is a debug assertion).
Can you also add a test that code from unreachable blocks doesn't block the optimization?
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
pub fn f(_1: &mut usize) {
mir! {
let _2: usize;
let _3: usize;
{
StorageLive(_2);
_2 = 42;
_3 = _2;
(*_1) = _3;
StorageDead(_2);
Return()
}
bb1 = {
// Ensure that _2 is considered uninitialized by `MaybeUninitializedLocals`.
StorageLive(_2);
// Use of _3 (in an unreachable block) when definition of _2 is unavailable.
(*_1) = _3;
StorageDead(_2);
Return()
}
}
}
@@ -0,0 +1,30 @@ | |||
// skip-filecheck |
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.
Add FileCheck annotations for the test (and to all new tests in general).
@@ -0,0 +1,61 @@ | |||
// skip-filecheck |
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.
Add FileCheck annotations for the test.
/// | ||
/// This is a simpler analysis than `MaybeUninitializedPlaces`, because it does not track | ||
/// individual fields. | ||
pub struct MaybeUninitializedLocals; |
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.
The results of the analysis are only meaningful for locals in SSA form. Can you move the implementation to the same module as SsaLocals
.
fn visit_local(&mut self, local: Local, context: PlaceContext, loc: Location) { | ||
// We don't need to check storage statements and statements for which the local doesn't need to be initialized. |
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.
For local == head
we would be preserving the existing behavior and we don't need to check anything. Return early in that situation.
Could you also add test for this? We should optimize and keep storage statements in:
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
pub fn f(_1: &mut usize) {
mir! {
let _2: usize;
let _3: usize;
{
StorageLive(_2);
_2 = 0;
_3 = _2;
(*_1) = _3;
StorageDead(_2);
(*_1) = _2;
Return()
}
}
}
// skip-filecheck | ||
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY | ||
//@ test-mir-pass: CopyProp | ||
|
||
// EMIT_MIR issue_141649.main.CopyProp.diff | ||
fn main() { |
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.
Add FileCheck annotations along with an explanation of the test. I would also suggest writing MIR directly, so that the correspondence between source and FileCheck assertions is clear.
I am not familiar with GVN, so I will leave review of that part to @cjgillot . |
☔ The latest upstream changes (presumably #142870) made this pull request unmergeable. Please resolve the merge conflicts. |
Modify the CopyProp and GVN MIR optimization passes to remove fewer
Storage{Live,Dead}
calls, allowing for better optimizations by LLVM - see #141649.Details
The idea is to use a new
MaybeUninitializedLocals
analysis and remove only the storage calls of locals that are maybe-uninit when accessed in a new location.