Skip to content

Don't codegen dead code #104860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,13 @@ struct RootCollector<'a, 'tcx> {

impl<'v> RootCollector<'_, 'v> {
fn process_item(&mut self, id: hir::ItemId) {
let def_id = id.owner_id.def_id;
let (live_symbols, _) = self.tcx.live_symbols_and_ignored_derived_traits(());
if !live_symbols.contains(&def_id) && !self.tcx.sess.link_dead_code() {
// This is dead code; ignore it.
return;
}
Copy link
Member

@bjorn3 bjorn3 Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this condition even be true when not compiling in incr comp mode? Also won't this remove all global_asm!() usages?

Copy link
Member Author

@jyn514 jyn514 Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this condition even happen when not compiling in incr comp mode?

I've observed this to happen with every unreachable item.

let crate_items = tcx.hir_crate_items(());
for id in crate_items.items() {
collector.process_item(id);
looks at every item in the crate regardless of whether it's reachable.

I hadn't considered global_asm, I can special-case it here if you think it's necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global_asm mut never be removed. Also please make this early exit conditional on the lazy collection mode. The eager collection mode doesn't omit anything on purpose to improve incr comp object file reuse.


match self.tcx.def_kind(id.owner_id) {
DefKind::Enum | DefKind::Struct | DefKind::Union => {
let item = self.tcx.hir().item(id);
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ fn create_and_seed_worklist<'tcx>(
check_foreign_item(tcx, &mut worklist, id);
}

if let Some(static_) = tcx.proc_macro_decls_static(()) {
// We assume this is always used if present.
worklist.push(static_);
}

(worklist, struct_constructors)
}

Expand Down