-
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?
Changes from all commits
c84c657
002bc18
bddb8e4
26fc160
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ use rustc_index::bit_set::DenseBitSet; | |
use rustc_middle::mir::visit::*; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_mir_dataflow::impls::MaybeUninitializedLocals; | ||
use rustc_mir_dataflow::{Analysis, ResultsCursor}; | ||
use tracing::{debug, instrument}; | ||
|
||
use crate::ssa::SsaLocals; | ||
|
@@ -16,7 +18,7 @@ use crate::ssa::SsaLocals; | |
/// _d = move? _c | ||
/// where each of the locals is only assigned once. | ||
/// | ||
/// We want to replace all those locals by `_a`, either copied or moved. | ||
/// We want to replace all those locals by `_a` (the "head"), either copied or moved. | ||
pub(super) struct CopyProp; | ||
|
||
impl<'tcx> crate::MirPass<'tcx> for CopyProp { | ||
|
@@ -30,21 +32,56 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp { | |
|
||
let typing_env = body.typing_env(tcx); | ||
let ssa = SsaLocals::new(tcx, body, typing_env); | ||
debug!(borrowed_locals = ?ssa.borrowed_locals()); | ||
let borrowed_locals = ssa.borrowed_locals().clone(); | ||
|
||
debug!(?borrowed_locals); | ||
debug!(copy_classes = ?ssa.copy_classes()); | ||
|
||
let fully_moved = fully_moved_locals(&ssa, body); | ||
debug!(?fully_moved); | ||
|
||
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()); | ||
Comment on lines
+43
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The information stored in |
||
|
||
for (local, &head) in ssa.copy_classes().iter_enumerated() { | ||
if local != head { | ||
storage_to_remove.insert(head); | ||
// We need to determine if we can keep the head's storage statements (which enables better optimizations). | ||
// For every local's usage location, if the head is maybe-uninitialized, we'll need to remove it's storage statements. | ||
head_storage_to_check.insert(head); | ||
tmiasko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if borrowed_locals.contains(local) { | ||
// To keep the storage of a head, we require that none of the locals in it's copy class are borrowed, | ||
// since otherwise we cannot easily identify when it is used. | ||
storage_to_remove.insert(head); | ||
} | ||
} | ||
} | ||
|
||
let any_replacement = ssa.copy_classes().iter_enumerated().any(|(l, &h)| l != h); | ||
|
||
// Debug builds have no use for the storage statements, so avoid extra work. | ||
let storage_to_remove = if any_replacement && tcx.sess.emit_lifetime_markers() { | ||
let maybe_uninit = MaybeUninitializedLocals::new() | ||
.iterate_to_fixpoint(tcx, body, Some("mir_opt::copy_prop")) | ||
.into_results_cursor(body); | ||
|
||
let mut storage_checker = StorageChecker { | ||
maybe_uninit, | ||
copy_classes: ssa.copy_classes(), | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Visit only reachable blocks with 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()
}
}
} |
||
|
||
storage_checker.storage_to_remove | ||
} else { | ||
// Conservatively remove all storage statements for the head locals. | ||
head_storage_to_check | ||
}; | ||
|
||
debug!(?storage_to_remove); | ||
|
||
Replacer { tcx, copy_classes: ssa.copy_classes(), fully_moved, storage_to_remove } | ||
.visit_body_preserves_cfg(body); | ||
|
||
|
@@ -152,3 +189,51 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> { | |
} | ||
} | ||
} | ||
|
||
// Marks heads of copy classes that are maybe uninitialized at the location of a local | ||
// as needing storage statement removal. | ||
struct StorageChecker<'a, 'tcx> { | ||
maybe_uninit: ResultsCursor<'a, 'tcx, MaybeUninitializedLocals>, | ||
copy_classes: &'a IndexSlice<Local, Local>, | ||
head_storage_to_check: DenseBitSet<Local>, | ||
storage_to_remove: DenseBitSet<Local>, | ||
} | ||
|
||
impl<'a, 'tcx> Visitor<'tcx> for StorageChecker<'a, 'tcx> { | ||
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. | ||
Comment on lines
+203
to
+204
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For 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()
}
}
} |
||
match context { | ||
PlaceContext::MutatingUse( | ||
MutatingUseContext::Store | ||
| MutatingUseContext::Call | ||
| MutatingUseContext::Yield | ||
| MutatingUseContext::AsmOutput, | ||
) | ||
| PlaceContext::NonUse(_) => { | ||
return; | ||
} | ||
_ => {} | ||
}; | ||
|
||
let head = self.copy_classes[local]; | ||
|
||
// The head must be initialized at the location of the local, otherwise we must remove it's storage statements. | ||
if self.head_storage_to_check.contains(head) { | ||
self.maybe_uninit.seek_before_primary_effect(loc); | ||
|
||
if self.maybe_uninit.get().contains(head) { | ||
debug!( | ||
?loc, | ||
?context, | ||
?local, | ||
?head, | ||
"found a head at a location in which it is maybe uninit, marking head for storage statement removal" | ||
); | ||
self.storage_to_remove.insert(head); | ||
|
||
// Once we found a use of the head that is maybe uninit, we do not need to check it again. | ||
self.head_storage_to_check.remove(head); | ||
} | ||
} | ||
} | ||
} |
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
.