Skip to content

Commit

Permalink
Rollup merge of rust-lang#84507 - crlf0710:codegen_nonlocal_main_wrap…
Browse files Browse the repository at this point in the history
…per, r=nagisa

Add primary marker on codegen unit and generate main wrapper on primary codegen.

This is the codegen part of changes extracted from rust-lang#84062.

This add a marker called `primary` on each codegen units, where exactly one codegen unit will be `primary = true` at a time. This specific codegen unit will take charge of generating `main` wrapper when `main` is imported from a foreign crate after the implementation of RFC 1260.

cc rust-lang#28937

I'm not sure who should i ask for review for codegen changes, so feel free to reassign.
r? `@nagisa`
  • Loading branch information
Dylan-DPC authored May 9, 2021
2 parents c0c6fb1 + 89a6705 commit 28ce404
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
17 changes: 3 additions & 14 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,20 +357,9 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
return None;
}
} else {
// FIXME: Add support for non-local main fn codegen
let span = cx.tcx().main_def.unwrap().span;
let n = 28937;
cx.sess()
.struct_span_err(span, "entry symbol `main` from foreign crate is not yet supported.")
.note(&format!(
"see issue #{} <https://github.com/rust-lang/rust/issues/{}> \
for more information",
n, n,
))
.emit();
cx.sess().abort_if_errors();
bug!();
} else if !cx.codegen_unit().is_primary() {
// We want to create the wrapper only when the codegen unit is the primary one
return None;
}

let main_llfn = cx.get_fn_addr(instance);
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ pub struct CodegenUnit<'tcx> {
name: Symbol,
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
size_estimate: Option<usize>,
primary: bool,
}

/// Specifies the linkage type for a `MonoItem`.
Expand Down Expand Up @@ -258,7 +259,7 @@ pub enum Visibility {

impl<'tcx> CodegenUnit<'tcx> {
pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
CodegenUnit { name, items: Default::default(), size_estimate: None }
CodegenUnit { name, items: Default::default(), size_estimate: None, primary: false }
}

pub fn name(&self) -> Symbol {
Expand All @@ -269,6 +270,14 @@ impl<'tcx> CodegenUnit<'tcx> {
self.name = name;
}

pub fn is_primary(&self) -> bool {
self.primary
}

pub fn make_primary(&mut self) {
self.primary = true;
}

pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
&self.items
}
Expand Down Expand Up @@ -378,6 +387,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for CodegenUnit<'tcx> {
name,
// The size estimate is not relevant to the hash
size_estimate: _,
primary: _,
} = *self;

name.hash_stable(hcx, hasher);
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_mir/src/monomorphize/partitioning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,14 @@ fn collect_and_partition_mono_items<'tcx>(
let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {
sync::join(
|| {
&*tcx.arena.alloc_from_iter(partition(
let mut codegen_units = partition(
tcx,
&mut items.iter().cloned(),
tcx.sess.codegen_units(),
&inlining_map,
))
);
codegen_units[0].make_primary();
&*tcx.arena.alloc_from_iter(codegen_units)
},
|| assert_symbols_are_distinct(tcx, items.iter()),
)
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/entry-point/imported_main_from_extern_crate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// build-fail
// run-pass
// aux-build:main_functions.rs

#![feature(imported_main)]

extern crate main_functions;
pub use main_functions::boilerplate as main; //~ ERROR entry symbol `main` from foreign crate

// FIXME: Should be run-pass
pub use main_functions::boilerplate as main;
10 changes: 0 additions & 10 deletions src/test/ui/entry-point/imported_main_from_extern_crate.stderr

This file was deleted.

0 comments on commit 28ce404

Please sign in to comment.