|
|
Subscribe / Log in / New account

Cross-crate dead code elimination

Cross-crate dead code elimination

Posted Dec 14, 2024 2:32 UTC (Sat) by saethlin (guest, #175049)
In reply to: Cross-crate dead code elimination by intgr
Parent article: Rust's incremental compiler architecture

In general, we try rather hard to make sure that compile errors are surfaced, even in dead code. And dead code detection is really hard on generic code. So we pretty much need to run all the code through the compiler frontend (type-checking and borrow-checking) regardless of whether the code is reachable.

But apart from that, you can do a crude version of this right now by setting the flags -Zmir-opt-level=0 and -Zcross-crate-inline-threshold=always in your dependencies. Cargo profile overrides can do this unstably, for example:

[profile.release.package.aws_sdk_ec2]
rustflags = ["-Zmir-opt-level=0", "-Zcross-crate-inline-threshold=always"]

This basically delays all codegen to dependencies of the named crate, and then you get dead code elimination through the mechanism that rustc already uses to only lower required functions through the codegen backend. But those flags also may or may not ruin the incrementality of your builds, and may also make the entire compilation single-threaded, because the whole program is a single codegen unit based on your main.

I've worked a fair bit on this part of the compiler recently.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds