Skip to content

Commit fbef300

Browse files
committed
metadata-link: add -Zmetadata-link flag
This adds the -Zmetadata-link flag, whose intent is to make the output of `--emit metadata` equivalent to `--emit metadata,link`. The goal is to allow pipelined builds with separate invocations of rustc. This is desireable for two reasons: 1. The "artifact notification" system that rustc and cargo use to communicate is very cargo-specific - it doesn't fit well into other builds systems such as Buck or Bazel. In general its incompatible with any build system which only recognizes output artifacts once the compiler process has terminated. This is a particular problem when compilation is distributed. 2. The rmeta file could be cached independently from the rlib. For example, if one generates compilation-ready rmeta files as part of "check" build, then those can be directly used for a full compilation from cache, without having to regenerate them. This means the build turns into a highly parallelizable invocation of `rustc --emit link` commands using cached `rmeta` files as input. Initially this flag `-Zmetadata-link`, but ultimately I'd expect to stabilize it as `-Cmetadata-link` (or a better name).
1 parent 3222439 commit fbef300

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
4545
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportLevel> {
4646
assert_eq!(cnum, LOCAL_CRATE);
4747

48-
if !tcx.sess.opts.output_types.should_codegen() {
48+
if !tcx.sess.opts.output_types.should_codegen() && !tcx.sess.opts.debugging_opts.metadata_link {
4949
return Default::default();
5050
}
5151

@@ -164,7 +164,7 @@ fn exported_symbols_provider_local(
164164
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
165165
assert_eq!(cnum, LOCAL_CRATE);
166166

167-
if !tcx.sess.opts.output_types.should_codegen() {
167+
if !tcx.sess.opts.output_types.should_codegen() && !tcx.sess.opts.debugging_opts.metadata_link {
168168
return &[];
169169
}
170170

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ fn test_debugging_options_tracking_hash() {
706706
tracked!(instrument_mcount, true);
707707
tracked!(link_only, true);
708708
tracked!(merge_functions, Some(MergeFunctions::Disabled));
709+
tracked!(metadata_link, true);
709710
tracked!(mir_emit_retag, true);
710711
tracked!(mir_opt_level, Some(4));
711712
tracked!(mutable_noalias, Some(true));

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,14 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
815815
///
816816
/// Return a pair, resp. for CTFE and for LLVM.
817817
fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
818+
let always_encode_mir = tcx.sess.opts.debugging_opts.always_encode_mir;
819+
let metadata_link = tcx.sess.opts.debugging_opts.metadata_link;
820+
818821
match tcx.def_kind(def_id) {
819822
// Constructors
820823
DefKind::Ctor(_, _) => {
821-
let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
822-
|| tcx.sess.opts.debugging_opts.always_encode_mir;
824+
let mir_opt_base =
825+
tcx.sess.opts.output_types.should_codegen() || always_encode_mir || metadata_link;
823826
(true, mir_opt_base)
824827
}
825828
// Constants
@@ -831,19 +834,17 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
831834
let generics = tcx.generics_of(def_id);
832835
let needs_inline = (generics.requires_monomorphization(tcx)
833836
|| tcx.codegen_fn_attrs(def_id).requests_inline())
834-
&& tcx.sess.opts.output_types.should_codegen();
837+
&& (tcx.sess.opts.output_types.should_codegen() || metadata_link);
835838
// Only check the presence of the `const` modifier.
836839
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id());
837-
let always_encode_mir = tcx.sess.opts.debugging_opts.always_encode_mir;
838840
(is_const_fn, needs_inline || always_encode_mir)
839841
}
840842
// Closures can't be const fn.
841843
DefKind::Closure => {
842844
let generics = tcx.generics_of(def_id);
843845
let needs_inline = (generics.requires_monomorphization(tcx)
844846
|| tcx.codegen_fn_attrs(def_id).requests_inline())
845-
&& tcx.sess.opts.output_types.should_codegen();
846-
let always_encode_mir = tcx.sess.opts.debugging_opts.always_encode_mir;
847+
&& (tcx.sess.opts.output_types.should_codegen() || metadata_link);
847848
(false, needs_inline || always_encode_mir)
848849
}
849850
// Generators require optimized MIR to compute layout.
@@ -2020,7 +2021,7 @@ impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'tcx> {
20202021
/// Used to prefetch queries which will be needed later by metadata encoding.
20212022
/// Only a subset of the queries are actually prefetched to keep this code smaller.
20222023
fn prefetch_mir(tcx: TyCtxt<'_>) {
2023-
if !tcx.sess.opts.output_types.should_codegen() {
2024+
if !tcx.sess.opts.output_types.should_codegen() && !tcx.sess.opts.debugging_opts.metadata_link {
20242025
// We won't emit MIR, so don't prefetch it.
20252026
return;
20262027
}

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,8 @@ options! {
11211121
the same values as the target option of the same name"),
11221122
meta_stats: bool = (false, parse_bool, [UNTRACKED],
11231123
"gather metadata statistics (default: no)"),
1124+
metadata_link: bool = (false, parse_bool, [TRACKED],
1125+
"always emit metadata that's equivalent to metadata,link"),
11241126
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
11251127
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
11261128
(default: no)"),

0 commit comments

Comments
 (0)