Skip to content

cmd/link: dead code elimination for side-effect free functions #14840

Open
@mdempsky

Description

@mdempsky

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

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.binary-sizecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Triage Backlog

Relationships

None yet

Development

No branches or pull requests

Issue actions