Skip to content

Commit f21539a

Browse files
committed
add flag to split sourcemap and span encoding out of main rmeta file
1 parent aa0082b commit f21539a

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
546546
}
547547

548548
fn decode_span(&mut self) -> Span {
549+
if !self.cdata().has_rmeta_extras() {
550+
return DUMMY_SP;
551+
}
552+
549553
let start = self.position();
550554
let tag = SpanTag(self.peek_byte());
551555
let data = if tag.kind() == SpanKind::Indirect {
@@ -1628,8 +1632,8 @@ impl<'a> CrateMetadataRef<'a> {
16281632
path.filter(|_| {
16291633
// Only spend time on further checks if we have what to translate *to*.
16301634
real_source_base_dir.is_some()
1631-
// Some tests need the translation to be always skipped.
1632-
&& sess.opts.unstable_opts.translate_remapped_path_to_local_path
1635+
// Some tests need the translation to be always skipped.
1636+
&& sess.opts.unstable_opts.translate_remapped_path_to_local_path
16331637
})
16341638
.filter(|virtual_dir| {
16351639
// Don't translate away `/rustc/$hash` if we're still remapping to it,
@@ -1999,6 +2003,10 @@ impl CrateMetadata {
19992003
self.root.has_default_lib_allocator
20002004
}
20012005

2006+
pub(crate) fn has_rmeta_extras(&self) -> bool {
2007+
self.root.has_rmeta_extras
2008+
}
2009+
20022010
pub(crate) fn is_proc_macro_crate(&self) -> bool {
20032011
self.root.is_proc_macro_crate()
20042012
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
168168
}
169169

170170
fn encode_span(&mut self, span: Span) {
171+
if self.tcx.sess.is_split_rmeta_enabled() {
172+
return;
173+
}
174+
171175
match self.span_shorthands.entry(span) {
172176
Entry::Occupied(o) => {
173177
// If an offset is smaller than the absolute position, we encode with the offset.
@@ -436,7 +440,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
436440
assert!(
437441
last_pos <= position,
438442
"make sure that the calls to `lazy*` \
439-
are in the same order as the metadata fields",
443+
are in the same order as the metadata fields",
440444
);
441445
position.get() - last_pos.get()
442446
}
@@ -603,6 +607,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
603607
adapted.encode(&mut self.opaque)
604608
}
605609

610+
fn dont_encode_source_map(
611+
&mut self,
612+
) -> LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>> {
613+
let adapted = TableBuilder::default();
614+
adapted.encode(&mut self.opaque)
615+
}
616+
606617
fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> {
607618
let tcx = self.tcx;
608619
let mut stats: Vec<(&'static str, usize)> = Vec::with_capacity(32);
@@ -708,7 +719,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
708719

709720
// Encode source_map. This needs to be done last, because encoding `Span`s tells us which
710721
// `SourceFiles` we actually need to encode.
711-
let source_map = stat!("source-map", || self.encode_source_map());
722+
let source_map = if tcx.sess.is_split_rmeta_enabled() {
723+
stat!("source-map", || self.dont_encode_source_map())
724+
} else {
725+
stat!("source-map", || self.encode_source_map())
726+
};
712727
let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers());
713728

714729
let root = stat!("final", || {
@@ -733,6 +748,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
733748
attrs,
734749
sym::default_lib_allocator,
735750
),
751+
has_rmeta_extras: !tcx.sess.is_split_rmeta_enabled(),
736752
proc_macro_data,
737753
debugger_visualizers,
738754
compiler_builtins: ast::attr::contains_name(attrs, sym::compiler_builtins),
@@ -1706,7 +1722,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17061722

17071723
record_array!(self.tables.module_children_non_reexports[def_id] <-
17081724
module_children.iter().filter(|child| child.reexport_chain.is_empty())
1709-
.map(|child| child.res.def_id().index));
1725+
.map(|child| child.res.def_id().index));
17101726

17111727
record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
17121728
module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
@@ -1755,7 +1771,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17551771
if matches!(rpitit_info, ty::ImplTraitInTraitData::Trait { .. }) {
17561772
record_array!(
17571773
self.tables.assumed_wf_types_for_rpitit[def_id]
1758-
<- self.tcx.assumed_wf_types_for_rpitit(def_id)
1774+
<- self.tcx.assumed_wf_types_for_rpitit(def_id)
17591775
);
17601776
self.encode_precise_capturing_args(def_id);
17611777
}
@@ -1838,7 +1854,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18381854
for &local_def_id in tcx.mir_keys(()) {
18391855
if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) {
18401856
record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <-
1841-
self.tcx.deduced_param_attrs(local_def_id.to_def_id()));
1857+
self.tcx.deduced_param_attrs(local_def_id.to_def_id()));
18421858
}
18431859
}
18441860
}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ pub(crate) struct CrateRoot {
260260
has_alloc_error_handler: bool,
261261
has_panic_handler: bool,
262262
has_default_lib_allocator: bool,
263+
has_rmeta_extras: bool,
263264

264265
crate_deps: LazyArray<CrateDep>,
265266
dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,8 @@ written to standard error output)"),
25372537
by the linker"),
25382538
split_lto_unit: Option<bool> = (None, parse_opt_bool, [TRACKED],
25392539
"enable LTO unit splitting (default: no)"),
2540+
split_rmeta: Option<bool> = (None, parse_opt_bool, [TRACKED],
2541+
"split extra rmeta data that isn't relevant rlib ABI into a separate file"),
25402542
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
25412543
"hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"),
25422544
#[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")]

compiler/rustc_session/src/session.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ impl Session {
400400
self.opts.unstable_opts.split_lto_unit == Some(true)
401401
}
402402

403+
pub fn is_split_rmeta_enabled(&self) -> bool {
404+
self.opts.unstable_opts.split_rmeta == Some(true)
405+
}
406+
403407
/// Check whether this compile session and crate type use static crt.
404408
pub fn crt_static(&self, crate_type: Option<CrateType>) -> bool {
405409
if !self.target.crt_static_respected {

tests/run-make/rdr-ignores-comments/rmake.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
// tests that changing the content of a comment will cause a change in the rmeta of a file
2-
use run_make_support::{rfs, rustc};
1+
// tests that changing the content of a comment will not cause a change in the rmeta of a library if
2+
// -Zsplit-rmeta is enabled
33
use std::hash::{Hash, Hasher};
44
use std::path::Path;
55

6+
use run_make_support::{rfs, rustc};
7+
68
fn main() {
79
let before = check_and_hash("before.rs");
810
let after = check_and_hash("after.rs");
911
dbg!(before, after);
10-
assert_neq!(before, after);
12+
assert_eq!(before, after);
1113
}
1214

1315
fn check_and_hash<P>(filename: P) -> u64
1416
where
1517
P: AsRef<Path>,
1618
{
1719
rfs::rename(filename, "foo.rs");
18-
rustc().input("foo.rs").emit("metadata").run();
20+
rustc().input("foo.rs").emit("metadata").arg("-Zsplit-rmeta").run();
1921
// hash the output
2022
let bytes = rfs::read("libfoo.rmeta");
2123
let mut hasher = std::hash::DefaultHasher::new();

0 commit comments

Comments
 (0)