logoalt Hacker News

mgsloan2 • today at 3:53 PM • 2 replies • view on HN

A cross-crate dead code analysis would mean that compilation of a crate now depends on information about its dependents. This would break reuse of compiled crates and cause recompiles when the analysis changes.

Something does seem a little off about this, though. Ideally for this `Debug` case there would be an annotation that says "compile this lazily, don't inline". Maybe there doesn't even need to be a new annotation, just `#[inline] #[cold]`. Which looks pretty weird, but might work already.


Replies

afdbcreid • today at 6:00 PM

`#[inline]` in Rust is weird and not similar to `inline` in C or C++. It has two effects: it duplicates the MIR for every codegen unit that needs it instead of precompiling (which is what causes the laziness, and also why it's commonly recommended to avoid `#[inline]` on generic functions as this happens anyway for them), and also adds LLVM `inlinehint`. It is possible to avoid the second but not the first if you want to stay lazy, and that can also cause code duplication (unless you set `codegen-units = 1`) and LLVM deciding to inline things even without the hint.

afdbcreid • today at 6:01 PM

And `#[inline] #[cold]` doesn't work: it adds both `inlinehint` and the cold hint to LLVM. Which may cause, for example, the function to be inlined but the branch be marked cold for better machine code organization.