New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/link: dead code elimination for side-effect free functions #14840
Comments
This could help with shrinking init-time map literal construction in e.g. the If the /cc @crawshaw @josharian |
init methods are a problem because they may reference (& initialize)
maybe we could rewrite that in the compiler to
the latter wouldn't need any init code, just relocations, so if x was On Wed, Mar 16, 2016 at 1:49 PM, Brad Fitzpatrick notifications@github.com
|
@randall77 I think during escape analysis we could conservatively detect whether a function has side effects? |
I don't think the current escape analysis can help, as it runs after the init functions are built. You'd want to decide the side-effect-freeness of the RHS of global initializers before the init function is built. And then you'd have to have an init function per global, have it associated with the global, and only link it in if the global was otherwise reachable. |
The compiler already has samesafeexpr. We could remove the "same" part and steal that. I wonder whether have an init function per global (with all the associated overhead) would end up outweighing the other good done. |
Overhead from too many init functions is a fair concern. I'd imagine a lot of the functions could be compiled as nosplit, which might reduce the overhead. Also, traditionally ELF compilers generate |
Anyone writing code for this? If not, I'll spend a couple of days on it. Assuming each global with side-effect free initialization is split off, I suspect the linker will need to merge the safe init functions together after reachability analysis. But that should be safe to do as long as we distinguish the safe generated init code from general init functions. |
All yours. |
@crawshaw Go for it. |
I made a bit of progress on this, but after looking closer I don't know how it will help binary size. The obvious target is the range tables in the unicode package. It would be nice to make it possible for the linker's deadcode pass to catch exported symbols like Nl, Nd, etc. But: all of these tables are included in the unicode.Categories map, which is used by the regexp package. All the real world programs I looked at use package regexp. Even tiny ones like objdump. So while this will make the "hello world" program from #6853 look good, I don't see how it will help in practice. If anyone can convince me otherwise I'll dust this off, but for now this looks like an academic exercise to me. |
I'm going to look into extending escape analysis to keep track of side-effect free functions for Go 1.17. |
Not sure if this is happening, but moving milestone to backlog as it is a long-standing issue. |
As a datapoint, I just tried changing the representation of Then instead of vars, I made them all consts. And instead of the five I of course also updated all the funcs taking a After minor corresponding updates to the
Which is all to say: there are wins to be had here if anybody is curious. |
Currently if we compile a Go program like:
the linker can't dead code eliminate
x
ordead
. This is because the "x = newDead()" initialization is compiled to an implicit init function, which causes the linker to pull inx
andnewDead
.(See https://golang.org/cl/20765 for a real world example.)
In general, just because
x
is otherwise unused, the linker can't get rid of thenewDead()
call because it might have side-effects. However, it should be possible for the compiler to help identify functions that are side-effect free, which could in turn let the linker be more aggressive about dead code elimination.We would probably also need to tweak how cmd/compile generates package initializer functions for the linker to be able to eliminate individual initializers.
The text was updated successfully, but these errors were encountered: