Skip to content

Don't codegen items that are statically unreachable #104858

Closed
@jyn514

Description

@jyn514
Member

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.

Activity

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

scottmcm commented on Nov 25, 2022

@scottmcm
Member

A potential first step would be to handle if const { monomorphized-predicate }, #85836.

jyn514

jyn514 commented on Nov 28, 2022

@jyn514
MemberAuthor

We already do this. #104860 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @scottmcm@jyn514

      Issue actions

        Don't codegen items that are statically unreachable · Issue #104858 · rust-lang/rust