Skip to content

Add -Z dead-fn-elimination to skip codegen of BFS-unreachable functions #976

@yijunyu

Description

@yijunyu

MCP: Add -Z dead-fn-elimination to skip codegen of BFS-unreachable functions

Proposal

Add an unstable -Z dead-fn-elimination flag that performs BFS reachability from binary entry points over the MIR call graph, then excludes unreachable functions from codegen via an is_codegened_item query override. ~462 lines across 6 files.

Large binary crates spend significant LLVM time on functions the linker later discards via --gc-sections. This flag moves that elimination before LLVM, at the MIR level. On a patched stage2 compiler, zed builds 8% faster and rustc builds 6% faster; smaller projects break even. The algorithm has been validated as an external RUSTC_WRAPPER tool (cargo-slicer).

An 12-point safety checklist ensures only private, non-generic, non-async, safe, non-Drop standalone functions are eliminated. Generic functions, async functions, unsafe functions, vtable-dispatched methods, #[no_mangle]/#[used] items, and functions whose signatures involve fn() pointers or dyn Trait are unconditionally kept. This MCP also provides foundational BFS infrastructure for a broader crate slicing project goal (a separate proposal).

Detailed design, benchmarks, and the full safety proof: docs/upstream-rfc.md

Mentors or Reviewers

Community feedback incorporated from @lcnr, @Amanieu, @workingjubilee, @oli-obk.

Note

Concerns (0 active)

Managed by @rustbot—see help for details.

Metadata

Metadata

Assignees

No one assigned

    Labels

    major-changeA proposal to make a major change to rustc

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions