Skip to content

Commit 1a13a12

Browse files
committed
(crudely) implement MIR-only rlibs
1 parent f415c07 commit 1a13a12

File tree

11 files changed

+161
-18
lines changed

11 files changed

+161
-18
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,14 @@ fn exported_symbols_provider_local(
209209
if allocator_kind_for_codegen(tcx).is_some() {
210210
for symbol_name in ALLOCATOR_METHODS
211211
.iter()
212-
.map(|method| format!("__rust_{}", method.name))
213-
.chain(["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()])
212+
.flat_map(|method| {
213+
[format!("__rust_{}", method.name), format!("__rdl_{}", method.name)]
214+
})
215+
.chain([
216+
"__rust_alloc_error_handler".to_string(),
217+
OomStrategy::SYMBOL.to_string(),
218+
"__rg_oom".to_string(),
219+
])
214220
{
215221
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
216222

@@ -359,6 +365,27 @@ fn exported_symbols_provider_local(
359365
}
360366
}
361367

368+
if tcx.building_mir_only_rlib() {
369+
for def_id in tcx.mir_keys(()) {
370+
if !matches!(tcx.def_kind(def_id.to_def_id()), DefKind::Static { .. }) {
371+
continue;
372+
}
373+
if tcx.is_reachable_non_generic(def_id.to_def_id()) {
374+
continue;
375+
}
376+
let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id());
377+
symbols.push((ExportedSymbol::NonGeneric(def_id.to_def_id()), SymbolExportInfo {
378+
level: symbol_export_level(tcx, def_id.to_def_id()),
379+
kind: if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
380+
SymbolExportKind::Tls
381+
} else {
382+
SymbolExportKind::Data
383+
},
384+
used: true,
385+
}));
386+
}
387+
}
388+
362389
// Sort so we get a stable incr. comp. hash.
363390
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
364391

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ fn test_unstable_options_tracking_hash() {
806806
tracked!(mir_emit_retag, true);
807807
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
808808
tracked!(mir_keep_place_mention, true);
809+
tracked!(mir_only_rlibs, true);
809810
tracked!(mir_opt_level, Some(4));
810811
tracked!(move_size_limit, Some(4096));
811812
tracked!(mutable_noalias, false);

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+8
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,14 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
572572
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
573573
)
574574
},
575+
mir_only_crates: |tcx, ()| {
576+
tcx.untracked().cstore.freeze();
577+
let store = CStore::from_tcx(tcx);
578+
let crates = store
579+
.iter_crate_data()
580+
.filter_map(|(cnum, data)| if data.root.is_mir_only { Some(cnum) } else { None });
581+
tcx.arena.alloc_from_iter(crates)
582+
},
575583
..providers.queries
576584
};
577585
provide_extern(&mut providers.extern_queries);

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
736736
impls,
737737
incoherent_impls,
738738
exported_symbols,
739+
is_mir_only: tcx.building_mir_only_rlib(),
739740
interpret_alloc_index,
740741
tables,
741742
syntax_contexts,
@@ -1058,12 +1059,13 @@ fn should_encode_mir(
10581059
reachable_set: &LocalDefIdSet,
10591060
def_id: LocalDefId,
10601061
) -> (bool, bool) {
1062+
let opts = &tcx.sess.opts;
1063+
let mir_required = opts.unstable_opts.always_encode_mir || tcx.building_mir_only_rlib();
10611064
match tcx.def_kind(def_id) {
10621065
// Constructors
10631066
DefKind::Ctor(_, _) => {
1064-
let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
1065-
|| tcx.sess.opts.unstable_opts.always_encode_mir;
1066-
(true, mir_opt_base)
1067+
let opt = mir_required || opts.output_types.should_codegen();
1068+
(true, opt)
10671069
}
10681070
// Constants
10691071
DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst | DefKind::Const => {
@@ -1075,7 +1077,7 @@ fn should_encode_mir(
10751077
// Full-fledged functions + closures
10761078
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10771079
let generics = tcx.generics_of(def_id);
1078-
let opt = tcx.sess.opts.unstable_opts.always_encode_mir
1080+
let opt = mir_required
10791081
|| (tcx.sess.opts.output_types.should_codegen()
10801082
&& reachable_set.contains(&def_id)
10811083
&& (generics.requires_monomorphization(tcx)

compiler/rustc_metadata/src/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ pub(crate) struct CrateRoot {
275275
debugger_visualizers: LazyArray<DebuggerVisualizerFile>,
276276

277277
exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
278+
is_mir_only: bool,
278279

279280
syntax_contexts: SyntaxContextTable,
280281
expn_data: ExpnDataTable,

compiler/rustc_middle/src/mir/mono.rs

+11
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ impl<'tcx> MonoItem<'tcx> {
9191
}
9292

9393
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
94+
// Always do LocalCopy codegen when building a MIR-only rlib
95+
if tcx.building_mir_only_rlib() {
96+
return InstantiationMode::LocalCopy;
97+
}
98+
// If this is a monomorphization from a MIR-only rlib and we are building another lib, do
99+
// local codegen.
100+
if tcx.mir_only_crates(()).iter().any(|c| *c == self.def_id().krate)
101+
&& tcx.crate_types() == &[rustc_session::config::CrateType::Rlib]
102+
{
103+
return InstantiationMode::LocalCopy;
104+
}
94105
let generate_cgu_internal_copies = tcx
95106
.sess
96107
.opts

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,11 @@ rustc_queries! {
23712371
desc { "estimating codegen size of `{}`", key }
23722372
cache_on_disk_if { true }
23732373
}
2374+
2375+
query mir_only_crates(_: ()) -> &'tcx [CrateNum] {
2376+
eval_always
2377+
desc { "fetching all foreign crates built in mir-only mode" }
2378+
}
23742379
}
23752380

23762381
rustc_query_append! { define_callbacks! }

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,10 @@ impl<'tcx> TyCtxt<'tcx> {
17991799
pub fn dcx(self) -> DiagCtxtHandle<'tcx> {
18001800
self.sess.dcx()
18011801
}
1802+
1803+
pub fn building_mir_only_rlib(self) -> bool {
1804+
self.sess.opts.unstable_opts.mir_only_rlibs && self.crate_types() == &[CrateType::Rlib]
1805+
}
18021806
}
18031807

18041808
impl<'tcx> TyCtxtAt<'tcx> {

compiler/rustc_monomorphize/src/collector.rs

+71-8
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ use rustc_hir::def::DefKind;
215215
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
216216
use rustc_hir::lang_items::LangItem;
217217
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
218+
use rustc_middle::middle::exported_symbols::ExportedSymbol;
218219
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, GlobalAlloc, Scalar};
219220
use rustc_middle::mir::mono::{CollectionMode, InstantiationMode, MonoItem};
220221
use rustc_middle::mir::visit::Visitor as MirVisitor;
@@ -230,7 +231,7 @@ use rustc_middle::ty::{
230231
use rustc_middle::util::Providers;
231232
use rustc_middle::{bug, span_bug};
232233
use rustc_session::Limit;
233-
use rustc_session::config::EntryFnType;
234+
use rustc_session::config::{CrateType, EntryFnType};
234235
use rustc_span::source_map::{Spanned, dummy_spanned, respan};
235236
use rustc_span::symbol::sym;
236237
use rustc_span::{DUMMY_SP, Span};
@@ -955,23 +956,40 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxtAt<'tcx>, instance: Instance<'tcx>) -
955956
return true;
956957
};
957958

959+
let def_is_for_mir_only_rlib = if def_id.krate == rustc_hir::def_id::LOCAL_CRATE {
960+
tcx.building_mir_only_rlib()
961+
} else {
962+
tcx.mir_only_crates(()).iter().any(|c| *c == def_id.krate)
963+
};
964+
958965
if tcx.is_foreign_item(def_id) {
959-
// Foreign items are always linked against, there's no way of instantiating them.
960-
return false;
966+
if def_is_for_mir_only_rlib {
967+
return tcx.is_mir_available(instance.def_id());
968+
} else {
969+
// Foreign items are always linked against, there's no way of instantiating them.
970+
return false;
971+
}
972+
}
973+
974+
if def_is_for_mir_only_rlib {
975+
let has_mir = tcx.is_mir_available(instance.def_id());
976+
return has_mir || matches!(tcx.def_kind(instance.def_id()), DefKind::Static { .. });
961977
}
962978

963979
if def_id.is_local() {
964980
// Local items cannot be referred to locally without monomorphizing them locally.
965981
return true;
966982
}
967983

968-
if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(*tcx).is_some() {
969-
// We can link to the item in question, no instance needed in this crate.
970-
return false;
984+
if !def_is_for_mir_only_rlib {
985+
if let DefKind::Static { .. } = tcx.def_kind(def_id) {
986+
// We cannot monomorphize statics from upstream crates.
987+
return false;
988+
}
971989
}
972990

973-
if let DefKind::Static { .. } = tcx.def_kind(def_id) {
974-
// We cannot monomorphize statics from upstream crates.
991+
if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(*tcx).is_some() {
992+
// We can link to the item in question, no instance needed in this crate.
975993
return false;
976994
}
977995

@@ -1370,6 +1388,7 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionStrategy) -> Vec<MonoI
13701388
}
13711389

13721390
collector.push_extra_entry_roots();
1391+
collector.push_extra_roots_from_mir_only_rlibs();
13731392
}
13741393

13751394
// We can only codegen items that are instantiable - items all of
@@ -1514,6 +1533,50 @@ impl<'v> RootCollector<'_, 'v> {
15141533

15151534
self.output.push(create_fn_mono_item(self.tcx, start_instance, DUMMY_SP));
15161535
}
1536+
1537+
fn push_extra_roots_from_mir_only_rlibs(&mut self) {
1538+
// An upstream extern function may be used anywhere in the dependency tree, so we
1539+
// cannot do any reachability analysis on them. We blindly monomorphize every
1540+
// extern function declared anywhere in our dependency tree. We must give them
1541+
// GloballyShared codegen because we don't know if the only call to an upstream
1542+
// extern function is also upstream: We don't have reachability information. All we
1543+
// can do is codegen all extern functions and pray for the linker to delete the
1544+
// ones that are reachable.
1545+
if !self.tcx.crate_types().iter().any(|c| !matches!(c, CrateType::Rlib)) {
1546+
return;
1547+
}
1548+
1549+
for (symbol, _info) in self
1550+
.tcx
1551+
.mir_only_crates(())
1552+
.into_iter()
1553+
.flat_map(|krate| self.tcx.exported_symbols(*krate))
1554+
{
1555+
let def_id = match symbol {
1556+
ExportedSymbol::NonGeneric(def_id) => def_id,
1557+
ExportedSymbol::ThreadLocalShim(def_id) => {
1558+
let item = MonoItem::Fn(Instance {
1559+
def: InstanceKind::ThreadLocalShim(*def_id),
1560+
args: GenericArgs::empty(),
1561+
});
1562+
self.output.push(dummy_spanned(item));
1563+
continue;
1564+
}
1565+
_ => continue,
1566+
};
1567+
match self.tcx.def_kind(def_id) {
1568+
DefKind::Fn | DefKind::AssocFn => {
1569+
let instance = Instance::mono(self.tcx, *def_id);
1570+
let item = create_fn_mono_item(self.tcx, instance, DUMMY_SP);
1571+
self.output.push(item);
1572+
}
1573+
DefKind::Static { .. } => {
1574+
self.output.push(dummy_spanned(MonoItem::Static(*def_id)));
1575+
}
1576+
_ => {}
1577+
}
1578+
}
1579+
}
15171580
}
15181581

15191582
#[instrument(level = "debug", skip(tcx, output))]

compiler/rustc_monomorphize/src/partitioning.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ fn partition<'tcx, I>(
145145
where
146146
I: Iterator<Item = MonoItem<'tcx>>,
147147
{
148+
if tcx.building_mir_only_rlib() {
149+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
150+
let cgu_name = fallback_cgu_name(cgu_name_builder);
151+
return vec![CodegenUnit::new(cgu_name)];
152+
}
153+
148154
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning");
149155

150156
let cx = &PartitioningCx { tcx, usage_map };
@@ -169,6 +175,10 @@ where
169175
debug_dump(tcx, "MERGE", &codegen_units);
170176
}
171177

178+
if !codegen_units.is_sorted_by(|a, b| a.name().as_str() < b.name().as_str()) {
179+
bug!("unsorted CGUs");
180+
}
181+
172182
// Make as many symbols "internal" as possible, so LLVM has more freedom to
173183
// optimize.
174184
if !tcx.sess.link_dead_code() {
@@ -189,7 +199,12 @@ where
189199
for cgu in codegen_units.iter() {
190200
names += &format!("- {}\n", cgu.name());
191201
}
192-
bug!("unsorted CGUs:\n{names}");
202+
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
203+
let mut sorted_names = String::new();
204+
for cgu in codegen_units.iter() {
205+
sorted_names += &format!("- {}\n", cgu.name());
206+
}
207+
bug!("unsorted CGUs:\n{names}\n{sorted_names}");
193208
}
194209

195210
codegen_units
@@ -213,6 +228,9 @@ where
213228
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
214229
let cgu_name_cache = &mut UnordMap::default();
215230

231+
let start_fn = cx.tcx.lang_items().start_fn();
232+
let entry_fn = cx.tcx.entry_fn(()).map(|(id, _)| id);
233+
216234
for mono_item in mono_items {
217235
// Handle only root (GloballyShared) items directly here. Inlined (LocalCopy) items
218236
// are handled at the bottom of the loop based on reachability, with one exception.
@@ -221,7 +239,8 @@ where
221239
match mono_item.instantiation_mode(cx.tcx) {
222240
InstantiationMode::GloballyShared { .. } => {}
223241
InstantiationMode::LocalCopy => {
224-
if Some(mono_item.def_id()) != cx.tcx.lang_items().start_fn() {
242+
let def_id = mono_item.def_id();
243+
if ![start_fn, entry_fn].contains(&Some(def_id)) {
225244
continue;
226245
}
227246
}
@@ -243,7 +262,7 @@ where
243262

244263
let cgu = codegen_units.entry(cgu_name).or_insert_with(|| CodegenUnit::new(cgu_name));
245264

246-
let mut can_be_internalized = true;
265+
let mut can_be_internalized = false;
247266
let (linkage, visibility) = mono_item_linkage_and_visibility(
248267
cx.tcx,
249268
&mono_item,
@@ -486,7 +505,7 @@ fn merge_codegen_units<'tcx>(
486505
// If we didn't zero-pad the sorted-by-name order would be `XYZ-cgu.0`,
487506
// `XYZ-cgu.1`, `XYZ-cgu.10`, `XYZ-cgu.11`, ..., `XYZ-cgu.2`, etc.
488507
codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
489-
let num_digits = codegen_units.len().ilog10() as usize + 1;
508+
let num_digits = std::hint::black_box(codegen_units.len().ilog10() as usize + 1);
490509
for (index, cgu) in codegen_units.iter_mut().enumerate() {
491510
// Note: `WorkItem::short_description` depends on this name ending
492511
// with `-cgu.` followed by a numeric suffix. Please keep it in

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,8 @@ options! {
18901890
mir_keep_place_mention: bool = (false, parse_bool, [TRACKED],
18911891
"keep place mention MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
18921892
(default: no)"),
1893+
mir_only_rlibs: bool = (false, parse_bool, [TRACKED],
1894+
"only generate MIR when building rlibs (default: no)"),
18931895
#[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")]
18941896
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
18951897
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),

0 commit comments

Comments
 (0)