Skip to content

Commit 23954e5

Browse files
committed
(crudely) implement MIR-only rlibs
1 parent 1ca424c commit 23954e5

File tree

13 files changed

+193
-19
lines changed

13 files changed

+193
-19
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,14 @@ fn exported_symbols_provider_local(
222222
if allocator_kind_for_codegen(tcx).is_some() {
223223
for symbol_name in ALLOCATOR_METHODS
224224
.iter()
225-
.map(|method| format!("__rust_{}", method.name))
226-
.chain(["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()])
225+
.flat_map(|method| {
226+
[format!("__rust_{}", method.name), format!("__rdl_{}", method.name)]
227+
})
228+
.chain([
229+
"__rust_alloc_error_handler".to_string(),
230+
OomStrategy::SYMBOL.to_string(),
231+
"__rg_oom".to_string(),
232+
])
227233
{
228234
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
229235

@@ -374,6 +380,30 @@ fn exported_symbols_provider_local(
374380
}
375381
}
376382

383+
if tcx.building_mir_only_rlib() {
384+
for def_id in tcx.mir_keys(()) {
385+
if !matches!(tcx.def_kind(def_id.to_def_id()), DefKind::Static { .. }) {
386+
continue;
387+
}
388+
if tcx.is_reachable_non_generic(def_id.to_def_id()) {
389+
continue;
390+
}
391+
let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id());
392+
symbols.push((
393+
ExportedSymbol::NonGeneric(def_id.to_def_id()),
394+
SymbolExportInfo {
395+
level: symbol_export_level(tcx, def_id.to_def_id()),
396+
kind: if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
397+
SymbolExportKind::Tls
398+
} else {
399+
SymbolExportKind::Data
400+
},
401+
used: true,
402+
},
403+
));
404+
}
405+
}
406+
377407
// Sort so we get a stable incr. comp. hash.
378408
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
379409

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ fn test_unstable_options_tracking_hash() {
787787
tracked!(mir_emit_retag, true);
788788
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
789789
tracked!(mir_keep_place_mention, true);
790+
tracked!(mir_only_rlibs, true);
790791
tracked!(mir_opt_level, Some(4));
791792
tracked!(move_size_limit, Some(4096));
792793
tracked!(mutable_noalias, false);

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

+8
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
529529
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
530530
)
531531
},
532+
mir_only_crates: |tcx, ()| {
533+
tcx.untracked().cstore.freeze();
534+
let store = CStore::from_tcx(tcx);
535+
let crates = store
536+
.iter_crate_data()
537+
.filter_map(|(cnum, data)| if data.root.is_mir_only { Some(cnum) } else { None });
538+
tcx.arena.alloc_from_iter(crates)
539+
},
532540
..providers.queries
533541
};
534542
provide_extern(&mut providers.extern_queries);

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
739739
impls,
740740
incoherent_impls,
741741
exported_symbols,
742+
is_mir_only: tcx.building_mir_only_rlib(),
742743
interpret_alloc_index,
743744
tables,
744745
syntax_contexts,
@@ -1051,12 +1052,13 @@ fn should_encode_mir(
10511052
reachable_set: &LocalDefIdSet,
10521053
def_id: LocalDefId,
10531054
) -> (bool, bool) {
1055+
let opts = &tcx.sess.opts;
1056+
let mir_required = opts.unstable_opts.always_encode_mir || tcx.building_mir_only_rlib();
10541057
match tcx.def_kind(def_id) {
10551058
// Constructors
10561059
DefKind::Ctor(_, _) => {
1057-
let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
1058-
|| tcx.sess.opts.unstable_opts.always_encode_mir;
1059-
(true, mir_opt_base)
1060+
let opt = mir_required || opts.output_types.should_codegen();
1061+
(true, opt)
10601062
}
10611063
// Constants
10621064
DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst | DefKind::Const => {
@@ -1067,7 +1069,7 @@ fn should_encode_mir(
10671069
// Full-fledged functions + closures
10681070
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10691071
let generics = tcx.generics_of(def_id);
1070-
let mut opt = tcx.sess.opts.unstable_opts.always_encode_mir
1072+
let mut opt = mir_required
10711073
|| (tcx.sess.opts.output_types.should_codegen()
10721074
&& reachable_set.contains(&def_id)
10731075
&& (generics.requires_monomorphization(tcx)

compiler/rustc_metadata/src/rmeta/mod.rs

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

278278
exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
279+
is_mir_only: bool,
279280

280281
syntax_contexts: SyntaxContextTable,
281282
expn_data: ExpnDataTable,

compiler/rustc_middle/src/mir/mono.rs

+11
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ impl<'tcx> MonoItem<'tcx> {
9999
}
100100

101101
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
102+
// Always do LocalCopy codegen when building a MIR-only rlib
103+
if tcx.building_mir_only_rlib() {
104+
return InstantiationMode::LocalCopy;
105+
}
106+
// If this is a monomorphization from a MIR-only rlib and we are building another lib, do
107+
// local codegen.
108+
if tcx.mir_only_crates(()).iter().any(|c| *c == self.def_id().krate)
109+
&& tcx.crate_types() == &[rustc_session::config::CrateType::Rlib]
110+
{
111+
return InstantiationMode::LocalCopy;
112+
}
102113
let generate_cgu_internal_copies = tcx
103114
.sess
104115
.opts

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,11 @@ rustc_queries! {
22302230
query find_field((def_id, ident): (DefId, rustc_span::symbol::Ident)) -> Option<rustc_target::abi::FieldIdx> {
22312231
desc { |tcx| "find the index of maybe nested field `{ident}` in `{}`", tcx.def_path_str(def_id) }
22322232
}
2233+
2234+
query mir_only_crates(_: ()) -> &'tcx [CrateNum] {
2235+
eval_always
2236+
desc { "fetching all foreign crates built in mir-only mode" }
2237+
}
22332238
}
22342239

22352240
rustc_query_append! { define_callbacks! }

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,10 @@ impl<'tcx> TyCtxt<'tcx> {
10831083
pub fn dcx(self) -> &'tcx DiagCtxt {
10841084
self.sess.dcx()
10851085
}
1086+
1087+
pub fn building_mir_only_rlib(self) -> bool {
1088+
self.sess.opts.unstable_opts.mir_only_rlibs && self.crate_types() == &[CrateType::Rlib]
1089+
}
10861090
}
10871091

10881092
impl<'tcx> TyCtxtAt<'tcx> {

compiler/rustc_monomorphize/src/collector.rs

+97-7
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ use rustc_hir as hir;
170170
use rustc_hir::def::DefKind;
171171
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
172172
use rustc_hir::lang_items::LangItem;
173+
use rustc_middle::middle::exported_symbols::ExportedSymbol;
173174
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, GlobalAlloc, Scalar};
174175
use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
175176
use rustc_middle::mir::visit::Visitor as MirVisitor;
@@ -184,6 +185,7 @@ use rustc_middle::ty::{
184185
};
185186
use rustc_middle::ty::{GenericArgKind, GenericArgs};
186187
use rustc_middle::{middle::codegen_fn_attrs::CodegenFnAttrFlags, mir::visit::TyContext};
188+
use rustc_session::config::CrateType;
187189
use rustc_session::config::EntryFnType;
188190
use rustc_session::lint::builtin::LARGE_ASSIGNMENTS;
189191
use rustc_session::Limit;
@@ -316,6 +318,7 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
316318
}
317319

318320
collector.push_extra_entry_roots();
321+
collector.push_extra_roots_from_mir_only_rlibs();
319322
}
320323

321324
// We can only codegen items that are instantiable - items all of
@@ -1025,9 +1028,24 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10251028
return true;
10261029
};
10271030

1031+
let def_is_for_mir_only_rlib = if def_id.krate == rustc_hir::def_id::LOCAL_CRATE {
1032+
tcx.building_mir_only_rlib()
1033+
} else {
1034+
tcx.mir_only_crates(()).iter().any(|c| *c == def_id.krate)
1035+
};
1036+
10281037
if tcx.is_foreign_item(def_id) {
1029-
// Foreign items are always linked against, there's no way of instantiating them.
1030-
return false;
1038+
if def_is_for_mir_only_rlib {
1039+
return tcx.is_mir_available(instance.def_id());
1040+
} else {
1041+
// Foreign items are always linked against, there's no way of instantiating them.
1042+
return false;
1043+
}
1044+
}
1045+
1046+
if def_is_for_mir_only_rlib {
1047+
let has_mir = tcx.is_mir_available(instance.def_id());
1048+
return has_mir || matches!(tcx.def_kind(instance.def_id()), DefKind::Static { .. });
10311049
}
10321050

10331051
if tcx.intrinsic(def_id).is_some_and(|i| i.must_be_overridden) {
@@ -1040,18 +1058,20 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10401058
return true;
10411059
}
10421060

1061+
if !def_is_for_mir_only_rlib {
1062+
if let DefKind::Static { .. } = tcx.def_kind(def_id) {
1063+
// We cannot monomorphize statics from upstream crates.
1064+
return false;
1065+
}
1066+
}
1067+
10431068
if tcx.is_reachable_non_generic(def_id)
10441069
|| instance.polymorphize(tcx).upstream_monomorphization(tcx).is_some()
10451070
{
10461071
// We can link to the item in question, no instance needed in this crate.
10471072
return false;
10481073
}
10491074

1050-
if let DefKind::Static { .. } = tcx.def_kind(def_id) {
1051-
// We cannot monomorphize statics from upstream crates.
1052-
return false;
1053-
}
1054-
10551075
if !tcx.is_mir_available(def_id) {
10561076
tcx.dcx().emit_fatal(NoOptimizedMir {
10571077
span: tcx.def_span(def_id),
@@ -1358,6 +1378,76 @@ impl<'v> RootCollector<'_, 'v> {
13581378

13591379
self.output.push(create_fn_mono_item(self.tcx, start_instance, DUMMY_SP));
13601380
}
1381+
1382+
fn push_extra_roots_from_mir_only_rlibs(&mut self) {
1383+
// An upstream extern function may be used anywhere in the dependency tree, so we
1384+
// cannot do any reachability analysis on them. We blindly monomorphize every
1385+
// extern function declared anywhere in our dependency tree. We must give them
1386+
// GloballyShared codegen because we don't know if the only call to an upstream
1387+
// extern function is also upstream: We don't have reachability information. All we
1388+
// can do is codegen all extern functions and pray for the linker to delete the
1389+
// ones that are reachable.
1390+
if !self.tcx.crate_types().iter().any(|c| !matches!(c, CrateType::Rlib)) {
1391+
return;
1392+
}
1393+
1394+
/*
1395+
eprintln!(
1396+
"Monomorphizing upstream crates for {:?}, {:?}",
1397+
self.tcx.crate_name(rustc_span::def_id::LOCAL_CRATE),
1398+
self.tcx.crate_types()
1399+
);
1400+
for krate in self.tcx.mir_only_crates(()) {
1401+
eprintln!("{:?}", self.tcx.crate_name(*krate));
1402+
}
1403+
*/
1404+
1405+
for (symbol, _info) in self
1406+
.tcx
1407+
.mir_only_crates(())
1408+
.into_iter()
1409+
.filter(|krate| {
1410+
if ["alloc", "core", "std"].contains(&self.tcx.crate_name(**krate).as_str())
1411+
&& self.tcx.crate_types() == &[CrateType::ProcMacro]
1412+
{
1413+
false
1414+
} else {
1415+
if self.tcx.crate_types() == &[CrateType::ProcMacro] {
1416+
eprintln!("{:?}", self.tcx.crate_name(**krate).as_str());
1417+
}
1418+
true
1419+
}
1420+
})
1421+
.flat_map(|krate| self.tcx.exported_symbols(*krate))
1422+
{
1423+
let def_id = match symbol {
1424+
ExportedSymbol::NonGeneric(def_id) => def_id,
1425+
ExportedSymbol::ThreadLocalShim(def_id) => {
1426+
//eprintln!("{:?}", def_id);
1427+
let item = MonoItem::Fn(Instance {
1428+
def: InstanceDef::ThreadLocalShim(*def_id),
1429+
args: GenericArgs::empty(),
1430+
});
1431+
self.output.push(dummy_spanned(item));
1432+
continue;
1433+
}
1434+
_ => continue,
1435+
};
1436+
match self.tcx.def_kind(def_id) {
1437+
DefKind::Fn | DefKind::AssocFn => {
1438+
//eprintln!("{:?}", def_id);
1439+
let instance = Instance::mono(self.tcx, *def_id);
1440+
let item = create_fn_mono_item(self.tcx, instance, DUMMY_SP);
1441+
self.output.push(item);
1442+
}
1443+
DefKind::Static { .. } => {
1444+
//eprintln!("{:?}", def_id);
1445+
self.output.push(dummy_spanned(MonoItem::Static(*def_id)));
1446+
}
1447+
_ => {}
1448+
}
1449+
}
1450+
}
13611451
}
13621452

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

compiler/rustc_monomorphize/src/partitioning.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ fn partition<'tcx, I>(
141141
where
142142
I: Iterator<Item = MonoItem<'tcx>>,
143143
{
144+
if tcx.building_mir_only_rlib() {
145+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
146+
let cgu_name = fallback_cgu_name(cgu_name_builder);
147+
return vec![CodegenUnit::new(cgu_name)];
148+
}
149+
144150
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning");
145151

146152
let cx = &PartitioningCx { tcx, usage_map };
@@ -165,6 +171,10 @@ where
165171
debug_dump(tcx, "MERGE", &codegen_units);
166172
}
167173

174+
if !codegen_units.is_sorted_by(|a, b| a.name().as_str() < b.name().as_str()) {
175+
bug!("unsorted CGUs");
176+
}
177+
168178
// Make as many symbols "internal" as possible, so LLVM has more freedom to
169179
// optimize.
170180
if !tcx.sess.link_dead_code() {
@@ -185,7 +195,12 @@ where
185195
for cgu in codegen_units.iter() {
186196
names += &format!("- {}\n", cgu.name());
187197
}
188-
bug!("unsorted CGUs:\n{names}");
198+
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
199+
let mut sorted_names = String::new();
200+
for cgu in codegen_units.iter() {
201+
sorted_names += &format!("- {}\n", cgu.name());
202+
}
203+
bug!("unsorted CGUs:\n{names}\n{sorted_names}");
189204
}
190205

191206
codegen_units
@@ -209,6 +224,9 @@ where
209224
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
210225
let cgu_name_cache = &mut FxHashMap::default();
211226

227+
let start_fn = cx.tcx.lang_items().start_fn();
228+
let entry_fn = cx.tcx.entry_fn(()).map(|(id, _)| id);
229+
212230
for mono_item in mono_items {
213231
// Handle only root (GloballyShared) items directly here. Inlined (LocalCopy) items
214232
// are handled at the bottom of the loop based on reachability, with one exception.
@@ -217,7 +235,8 @@ where
217235
match mono_item.instantiation_mode(cx.tcx) {
218236
InstantiationMode::GloballyShared { .. } => {}
219237
InstantiationMode::LocalCopy => {
220-
if Some(mono_item.def_id()) != cx.tcx.lang_items().start_fn() {
238+
let def_id = mono_item.def_id();
239+
if ![start_fn, entry_fn].contains(&Some(def_id)) {
221240
continue;
222241
}
223242
}
@@ -239,7 +258,7 @@ where
239258

240259
let cgu = codegen_units.entry(cgu_name).or_insert_with(|| CodegenUnit::new(cgu_name));
241260

242-
let mut can_be_internalized = true;
261+
let mut can_be_internalized = false;
243262
let (linkage, visibility) = mono_item_linkage_and_visibility(
244263
cx.tcx,
245264
&mono_item,

compiler/rustc_session/src/options.rs

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

library/std/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ repository = "https://github.com/rust-lang/rust.git"
88
description = "The Rust Standard Library"
99
edition = "2021"
1010

11-
[lib]
12-
crate-type = ["dylib", "rlib"]
13-
1411
[dependencies]
1512
alloc = { path = "../alloc", public = true }
1613
cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }

0 commit comments

Comments
 (0)