Description
Currently if we compile a Go program like:
package main
type dead int
func newDead() *dead { return new(dead) }
var x = newDead()
func main() {}
the linker can't dead code eliminate x
or dead
. This is because the "x = newDead()" initialization is compiled to an implicit init function, which causes the linker to pull in x
and newDead
.
(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 the newDead()
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status