Skip to content

Commit

Permalink
Rollup merge of #100919 - sanxiyn:parallel-liveness, r=oli-obk
Browse files Browse the repository at this point in the history
Use par_body_owners for liveness

I did this refactoring while working on something else. Liveness is about bodies, there is no reason to use par_for_each_module here.

Tests are updated because things are visited in a different order. I checked diagnostics are same, just in a different (and IMO, better) order.
  • Loading branch information
matthiaskrgr authored Aug 24, 2022
2 parents ecf14d4 + f280138 commit 8cdf408
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 115 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,13 +911,13 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
});
},
{
sess.time("liveness_and_intrinsic_checking", || {
tcx.hir().par_for_each_module(|module| {
sess.time("liveness_checking", || {
tcx.hir().par_body_owners(|def_id| {
// this must run before MIR dump, because
// "not all control paths return a value" is reported here.
//
// maybe move the check to a MIR pass?
tcx.ensure().check_mod_liveness(module);
tcx.ensure().check_liveness(def_id.to_def_id());
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,8 @@ rustc_queries! {
desc { |tcx| "checking privacy in {}", describe_as_module(key, tcx) }
}

query check_mod_liveness(key: LocalDefId) -> () {
desc { |tcx| "checking liveness of variables in {}", describe_as_module(key, tcx) }
query check_liveness(key: DefId) {
desc { |tcx| "checking liveness of variables in {}", tcx.def_path_str(key) }
}

/// Return the live symbols in the crate for dead code check.
Expand Down
101 changes: 46 additions & 55 deletions compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::*;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Expr, HirId, HirIdMap, HirIdSet};
use rustc_index::vec::IndexVec;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, DefIdTree, RootVariableMinCaptureList, Ty, TyCtxt};
use rustc_session::lint;
Expand Down Expand Up @@ -139,12 +138,54 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
}
}

fn check_mod_liveness(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx));
fn check_liveness(tcx: TyCtxt<'_>, def_id: DefId) {
let local_def_id = match def_id.as_local() {
None => return,
Some(def_id) => def_id,
};

// Don't run unused pass for #[derive()]
let parent = tcx.local_parent(local_def_id);
if let DefKind::Impl = tcx.def_kind(parent)
&& tcx.has_attr(parent.to_def_id(), sym::automatically_derived)
{
return;
}

// Don't run unused pass for #[naked]
if tcx.has_attr(def_id, sym::naked) {
return;
}

let mut maps = IrMaps::new(tcx);
let body_id = tcx.hir().body_owned_by(local_def_id);
let hir_id = tcx.hir().body_owner(body_id);
let body = tcx.hir().body(body_id);

if let Some(upvars) = tcx.upvars_mentioned(def_id) {
for &var_hir_id in upvars.keys() {
let var_name = tcx.hir().name(var_hir_id);
maps.add_variable(Upvar(var_hir_id, var_name));
}
}

// gather up the various local variables, significant expressions,
// and so forth:
maps.visit_body(body);

// compute liveness
let mut lsets = Liveness::new(&mut maps, local_def_id);
let entry_ln = lsets.compute(&body, hir_id);
lsets.log_liveness(entry_ln, body_id.hir_id);

// check for various error conditions
lsets.visit_body(body);
lsets.warn_about_unused_upvars(entry_ln);
lsets.warn_about_unused_args(body, entry_ln);
}

pub fn provide(providers: &mut Providers) {
*providers = Providers { check_mod_liveness, ..*providers };
*providers = Providers { check_liveness, ..*providers };
}

// ______________________________________________________________________
Expand Down Expand Up @@ -316,56 +357,6 @@ impl<'tcx> IrMaps<'tcx> {
}

impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
type NestedFilter = nested_filter::OnlyBodies;

fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}

fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
debug!("visit_body {:?}", body.id());

// swap in a new set of IR maps for this body
let mut maps = IrMaps::new(self.tcx);
let hir_id = maps.tcx.hir().body_owner(body.id());
let local_def_id = maps.tcx.hir().local_def_id(hir_id);
let def_id = local_def_id.to_def_id();

// Don't run unused pass for #[derive()]
let parent = self.tcx.local_parent(local_def_id);
if let DefKind::Impl = self.tcx.def_kind(parent)
&& self.tcx.has_attr(parent.to_def_id(), sym::automatically_derived)
{
return;
}

// Don't run unused pass for #[naked]
if self.tcx.has_attr(def_id, sym::naked) {
return;
}

if let Some(upvars) = maps.tcx.upvars_mentioned(def_id) {
for &var_hir_id in upvars.keys() {
let var_name = maps.tcx.hir().name(var_hir_id);
maps.add_variable(Upvar(var_hir_id, var_name));
}
}

// gather up the various local variables, significant expressions,
// and so forth:
intravisit::walk_body(&mut maps, body);

// compute liveness
let mut lsets = Liveness::new(&mut maps, local_def_id);
let entry_ln = lsets.compute(&body, hir_id);
lsets.log_liveness(entry_ln, body.id().hir_id);

// check for various error conditions
lsets.visit_body(body);
lsets.warn_about_unused_upvars(entry_ln);
lsets.warn_about_unused_args(body, entry_ln);
}

fn visit_local(&mut self, local: &'tcx hir::Local<'tcx>) {
self.add_from_pat(&local.pat);
if local.els.is_some() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
warning: unused variable: `t2`
--> $DIR/destructure-pattern-closure-within-closure.rs:13:21
warning: unused variable: `g2`
--> $DIR/destructure-pattern-closure-within-closure.rs:10:17
|
LL | let (_, t2) = t;
| ^^ help: if this is intentional, prefix it with an underscore: `_t2`
LL | let (_, g2) = g;
| ^^ help: if this is intentional, prefix it with an underscore: `_g2`
|
note: the lint level is defined here
--> $DIR/destructure-pattern-closure-within-closure.rs:3:9
Expand All @@ -11,11 +11,11 @@ LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`

warning: unused variable: `g2`
--> $DIR/destructure-pattern-closure-within-closure.rs:10:17
warning: unused variable: `t2`
--> $DIR/destructure-pattern-closure-within-closure.rs:13:21
|
LL | let (_, g2) = g;
| ^^ help: if this is intentional, prefix it with an underscore: `_g2`
LL | let (_, t2) = t;
| ^^ help: if this is intentional, prefix it with an underscore: `_t2`

warning: 2 warnings emitted

12 changes: 6 additions & 6 deletions src/test/ui/lint/unused/issue-54180-unused-ref-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ LL | #![deny(unused)]
| ^^^^^^
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`

error: unused variable: `x`
--> $DIR/issue-54180-unused-ref-field.rs:29:45
|
LL | let _: i32 = points.iter().map(|Point { x, y }| y).sum();
| ^ help: try ignoring the field: `x: _`

error: unused variable: `f1`
--> $DIR/issue-54180-unused-ref-field.rs:26:13
|
Expand All @@ -29,5 +23,11 @@ error: unused variable: `x`
LL | Point { y, ref mut x } => y,
| ^^^^^^^^^ help: try ignoring the field: `x: _`

error: unused variable: `x`
--> $DIR/issue-54180-unused-ref-field.rs:29:45
|
LL | let _: i32 = points.iter().map(|Point { x, y }| y).sum();
| ^ help: try ignoring the field: `x: _`

error: aborting due to 4 previous errors

28 changes: 14 additions & 14 deletions src/test/ui/lint/unused/lint-unused-variables.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,55 @@ LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:68:9
--> $DIR/lint-unused-variables.rs:22:9
|
LL | a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:74:9
--> $DIR/lint-unused-variables.rs:29:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:42:9
--> $DIR/lint-unused-variables.rs:34:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:47:9
--> $DIR/lint-unused-variables.rs:42:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:22:9
|
LL | a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:29:9
--> $DIR/lint-unused-variables.rs:47:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:34:9
--> $DIR/lint-unused-variables.rs:55:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:55:9
--> $DIR/lint-unused-variables.rs:60:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:68:9
|
LL | a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:60:9
--> $DIR/lint-unused-variables.rs:74:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/liveness/liveness-consts.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ warning: unused variable: `z`
LL | pub fn f(x: [u8; { let s = 17; 100 }]) -> [u8; { let z = 18; 100 }] {
| ^ help: if this is intentional, prefix it with an underscore: `_z`

warning: unused variable: `z`
--> $DIR/liveness-consts.rs:60:13
|
LL | let z = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_z`

warning: value assigned to `t` is never read
--> $DIR/liveness-consts.rs:42:9
|
Expand All @@ -59,5 +53,11 @@ warning: unused variable: `w`
LL | let w = 10;
| ^ help: if this is intentional, prefix it with an underscore: `_w`

warning: unused variable: `z`
--> $DIR/liveness-consts.rs:60:13
|
LL | let z = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_z`

warning: 8 warnings emitted

Loading

0 comments on commit 8cdf408

Please sign in to comment.