Skip to content

Don't codegen items that are statically unreachable #104858

Closed
@jyn514

Description

@jyn514

Currently, all items in a crate are codegened, and only stripped later in a linker pass:

// Next we try to make as many symbols "internal" as possible, so LLVM has
// more freedom to optimize.
if !tcx.sess.link_dead_code() {
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_internalize_symbols");
partitioner.internalize_symbols(cx, &mut post_inlining);
// Try to strip as much out of the generated object by removing unused
// sections if possible. See more comments in linker.rs
if !sess.link_dead_code() {
// If PGO is enabled sometimes gc_sections will remove the profile data section
// as it appears to be unused. This can then cause the PGO profile file to lose
// some functions. If we are generating a profile we shouldn't strip those metadata
// sections to ensure we have all the data for PGO.
let keep_metadata =
crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
{
cmd.gc_sections(keep_metadata);
This unnecessarily bloats the amount of time and memory it takes to compile a crate. We already have a dead_code pass; we should use its results to avoid running codegen on unreachable items.

This is a prerequisite for #103356, but I expect it will be a large boost to compile times even if that never lands.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-compilememIssue: Problems and improvements with respect to memory usage during compilation.I-compiletimeIssue: Problems and improvements with respect to compile times.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions