You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
dead-code optimize if const { expr } even in opt-level=0 #85836
For things like if const { expr } { 0 } else { do something complex } it'd be nice to not have to even give LLVM the code for the else branch to have to emit. Ought to save time and binary size.
Tentatively marked mir-opt, since we presumably still need to make the MIR for it, in order to borrow-check and such the unreachable code. But might need to be codegen-time instead to pick up on monomorphized inline constants.
Hopefully should be doable relatively easily when the const is an inline const, since that forces const folding already, and thus there should just be a const true in the MIR.
The problem is that this can't be done on generic constants, as we don't mir-optimize post-monomorphization. We could start doing some benchmarks to see how bad running all mir opts (or at least a select few) would be if we duplicated, substituted and optimized MIR bodies for concrete generic parameters.
We could start doing some benchmarks to see how bad running all mir opts (or at least a select few) would be if we duplicated, substituted and optimized MIR bodies for concrete generic parameters.
Maybe it should also be evaluated together with polymorphization or const_evaluatable_checked since those can cut down on the duplication before applying further optimizations.
However, the place I actually needed it was more like if const { size_of::<T>() < 100 }, which doesn't work with inline_const today. So maybe come back to this once that's fixed.
@saethlin's recent PR #121421 seems related here? If the if condition becomes a constant during codegen, we should be entirely skipping it now and not even generate LLVM IR.
However, if that dead code calls functions, those still get codegen'd as usual -- the collector that determines which function to codegen doesn't know about dead branches. We'd have to embed a (simple) monomorphization-time reachability analysis in the collector to fix that.
Activity
scottmcm commentedon May 30, 2021
Well, turns out this already works for simple constants in MIR! 🎉
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4d1fe08eec8b705525535ec9ec9d1c89
However, the place I actually needed it was more like
if const { size_of::<T>() < 100 }, which doesn't work withinline_consttoday. So maybe come back to this once that's fixed.https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=80c275a75dc280ac3fcdfb24b6484adc
nagisa commentedon May 30, 2021
This should be done by SimplifyBranches which AFAIK is run unconditionally. I wonder if its match needs to be expanded?
Is there mcve?
oli-obk commentedon May 30, 2021
The problem is that this can't be done on generic constants, as we don't mir-optimize post-monomorphization. We could start doing some benchmarks to see how bad running all mir opts (or at least a select few) would be if we duplicated, substituted and optimized MIR bodies for concrete generic parameters.
oli-obk commentedon May 30, 2021
I would say
qualifies as an MCVE as the monomorphized
foo::<isize>gives the following llvm irthe8472 commentedon May 30, 2021
Maybe it should also be evaluated together with polymorphization or const_evaluatable_checked since those can cut down on the duplication before applying further optimizations.
scottmcm commentedon Jun 1, 2021
Bonus points if this can avoid monomorphizing functions called in the dead code too.
Demo extracted from
core: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=40cc11066ca1fa8462d0f0f403ee1d8f (cc #52051)Would be great to not codegen all the downstream stuff that's only called in the else block of a
br i1 true.nbdd0121 commentedon May 1, 2022
FYI this should work with #96557
DemiMarie commentedon Aug 20, 2022
2 remaining items
RalfJung commentedon Mar 18, 2024
This seems closely related to rust-lang/rfcs#3582 and #122301.
RalfJung commentedon Mar 25, 2024
@saethlin's recent PR #121421 seems related here? If the
ifcondition becomes a constant during codegen, we should be entirely skipping it now and not even generate LLVM IR.However, if that dead code calls functions, those still get codegen'd as usual -- the collector that determines which function to codegen doesn't know about dead branches. We'd have to embed a (simple) monomorphization-time reachability analysis in the collector to fix that.
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=<…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=<…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=<…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=<…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=<…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=<…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=c…
Auto merge of rust-lang#123272 - saethlin:reachable-mono-cleanup, r=c…
Auto merge of #123272 - saethlin:reachable-mono-cleanup, r=cjgillot
Auto merge of #123272 - saethlin:reachable-mono-cleanup, r=cjgillot