Skip to content

Commit c699b05

Browse files
authored
Rollup merge of #105286 - willcrichton:maximal-hir-to-mir-coverage, r=cjgillot
Add -Z maximal-hir-to-mir-coverage flag This PR adds a new unstable flag `-Z maximal-hir-to-mir-coverage` that changes the behavior of `maybe_lint_level_root_bounded`, pursuant to [a discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Mapping.20MIR.20to.20HIR). When enabled, this function will not search upwards for a lint root, but rather immediately return the provided HIR node ID. This change increases the granularity of the mapping between MIR locations and HIR nodes inside the `SourceScopeLocalData` data structures. This increase in granularity is useful for rustc consumers like [Flowistry](https://github.com/willcrichton/flowistry) that rely on getting source-mapping information about the MIR CFG that is as precise as possible. A test `maximal_mir_to_hir_coverage.rs` has been added to verify that this flag does not break anything. r? `@cjgillot` cc `@gavinleroy`
2 parents 967085e + d595884 commit c699b05

File tree

6 files changed

+59
-32
lines changed

6 files changed

+59
-32
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ fn test_unstable_options_tracking_hash() {
747747
tracked!(link_only, true);
748748
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
749749
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });
750+
tracked!(maximal_hir_to_mir_coverage, true);
750751
tracked!(merge_functions, Some(MergeFunctions::Disabled));
751752
tracked!(mir_emit_retag, true);
752753
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);

compiler/rustc_mir_build/src/build/mod.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -948,20 +948,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
948948
original_source_scope: SourceScope,
949949
pattern_span: Span,
950950
) {
951-
let tcx = self.tcx;
952-
let current_root = tcx.maybe_lint_level_root_bounded(arg_hir_id, self.hir_id);
953-
let parent_root = tcx.maybe_lint_level_root_bounded(
954-
self.source_scopes[original_source_scope]
955-
.local_data
956-
.as_ref()
957-
.assert_crate_local()
958-
.lint_root,
959-
self.hir_id,
960-
);
961-
if current_root != parent_root {
962-
self.source_scope =
963-
self.new_source_scope(pattern_span, LintLevel::Explicit(current_root), None);
964-
}
951+
let parent_id = self.source_scopes[original_source_scope]
952+
.local_data
953+
.as_ref()
954+
.assert_crate_local()
955+
.lint_root;
956+
self.maybe_new_source_scope(pattern_span, None, arg_hir_id, parent_id);
965957
}
966958

967959
fn get_unit_temp(&mut self) -> Place<'tcx> {

compiler/rustc_mir_build/src/build/scope.rs

+38-18
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use std::mem;
8585

8686
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
8787
use rustc_data_structures::fx::FxHashMap;
88+
use rustc_hir::HirId;
8889
use rustc_index::vec::IndexVec;
8990
use rustc_middle::middle::region;
9091
use rustc_middle::mir::*;
@@ -567,25 +568,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
567568
F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
568569
{
569570
let source_scope = self.source_scope;
570-
let tcx = self.tcx;
571571
if let LintLevel::Explicit(current_hir_id) = lint_level {
572-
// Use `maybe_lint_level_root_bounded` with `root_lint_level` as a bound
573-
// to avoid adding Hir dependencies on our parents.
574-
// We estimate the true lint roots here to avoid creating a lot of source scopes.
575-
576-
let parent_root = tcx.maybe_lint_level_root_bounded(
577-
self.source_scopes[source_scope].local_data.as_ref().assert_crate_local().lint_root,
578-
self.hir_id,
579-
);
580-
let current_root = tcx.maybe_lint_level_root_bounded(current_hir_id, self.hir_id);
581-
582-
if parent_root != current_root {
583-
self.source_scope = self.new_source_scope(
584-
region_scope.1.span,
585-
LintLevel::Explicit(current_root),
586-
None,
587-
);
588-
}
572+
let parent_id =
573+
self.source_scopes[source_scope].local_data.as_ref().assert_crate_local().lint_root;
574+
self.maybe_new_source_scope(region_scope.1.span, None, current_hir_id, parent_id);
589575
}
590576
self.push_scope(region_scope);
591577
let mut block;
@@ -758,6 +744,40 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
758744
))
759745
}
760746

747+
/// Possibly creates a new source scope if `current_root` and `parent_root`
748+
/// are different, or if -Zmaximal-hir-to-mir-coverage is enabled.
749+
pub(crate) fn maybe_new_source_scope(
750+
&mut self,
751+
span: Span,
752+
safety: Option<Safety>,
753+
current_id: HirId,
754+
parent_id: HirId,
755+
) {
756+
let (current_root, parent_root) =
757+
if self.tcx.sess.opts.unstable_opts.maximal_hir_to_mir_coverage {
758+
// Some consumers of rustc need to map MIR locations back to HIR nodes. Currently the
759+
// the only part of rustc that tracks MIR -> HIR is the `SourceScopeLocalData::lint_root`
760+
// field that tracks lint levels for MIR locations. Normally the number of source scopes
761+
// is limited to the set of nodes with lint annotations. The -Zmaximal-hir-to-mir-coverage
762+
// flag changes this behavior to maximize the number of source scopes, increasing the
763+
// granularity of the MIR->HIR mapping.
764+
(current_id, parent_id)
765+
} else {
766+
// Use `maybe_lint_level_root_bounded` with `self.hir_id` as a bound
767+
// to avoid adding Hir dependencies on our parents.
768+
// We estimate the true lint roots here to avoid creating a lot of source scopes.
769+
(
770+
self.tcx.maybe_lint_level_root_bounded(current_id, self.hir_id),
771+
self.tcx.maybe_lint_level_root_bounded(parent_id, self.hir_id),
772+
)
773+
};
774+
775+
if current_root != parent_root {
776+
let lint_level = LintLevel::Explicit(current_root);
777+
self.source_scope = self.new_source_scope(span, lint_level, safety);
778+
}
779+
}
780+
761781
/// Creates a new source scope, nested in the current one.
762782
pub(crate) fn new_source_scope(
763783
&mut self,

compiler/rustc_session/src/options.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,9 @@ options! {
13821382
"list the symbols defined by a library crate (default: no)"),
13831383
macro_backtrace: bool = (false, parse_bool, [UNTRACKED],
13841384
"show macro backtraces (default: no)"),
1385+
maximal_hir_to_mir_coverage: bool = (false, parse_bool, [TRACKED],
1386+
"save as much information as possible about the correspondence between MIR and HIR \
1387+
as source scopes (default: no)"),
13851388
merge_functions: Option<MergeFunctions> = (None, parse_merge_functions, [TRACKED],
13861389
"control the operation of the MergeFunctions LLVM pass, taking \
13871390
the same values as the target option of the same name"),

src/test/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
-Z location-detail=val -- what location details should be tracked when using caller_location, either `none`, or a comma separated list of location details, for which valid options are `file`, `line`, and `column` (default: `file,line,column`)
7878
-Z ls=val -- list the symbols defined by a library crate (default: no)
7979
-Z macro-backtrace=val -- show macro backtraces (default: no)
80+
-Z maximal-hir-to-mir-coverage=val -- save as much information as possible about the correspondence between MIR and HIR as source scopes (default: no)
8081
-Z merge-functions=val -- control the operation of the MergeFunctions LLVM pass, taking the same values as the target option of the same name
8182
-Z meta-stats=val -- gather metadata statistics (default: no)
8283
-Z mir-emit-retag=val -- emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 (default: no)
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-flags: -Zmaximal-hir-to-mir-coverage
2+
// run-pass
3+
4+
// Just making sure this flag is accepted and doesn't crash the compiler
5+
6+
fn main() {
7+
let x = 1;
8+
let y = x + 1;
9+
println!("{y}");
10+
}

0 commit comments

Comments
 (0)