2020-04-20 12:13:42 -04:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package ld
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-02 19:36:28 -04:00
|
|
|
"cmd/internal/goobj"
|
2020-04-20 12:13:42 -04:00
|
|
|
"cmd/internal/objabi"
|
|
|
|
|
"cmd/internal/sys"
|
|
|
|
|
"cmd/link/internal/loader"
|
2017-10-04 17:54:04 -04:00
|
|
|
"cmd/link/internal/sym"
|
2020-04-20 12:13:42 -04:00
|
|
|
"fmt"
|
2024-04-02 13:08:24 +00:00
|
|
|
"internal/abi"
|
2021-04-15 23:05:49 -04:00
|
|
|
"internal/buildcfg"
|
2023-01-25 10:46:08 -05:00
|
|
|
"strings"
|
2020-04-20 12:13:42 -04:00
|
|
|
"unicode"
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
)
|
|
|
|
|
|
2020-04-20 12:13:42 -04:00
|
|
|
var _ = fmt.Print
|
|
|
|
|
|
|
|
|
|
type deadcodePass struct {
|
|
|
|
|
ctxt *Link
|
|
|
|
|
ldr *loader.Loader
|
2021-03-07 20:32:38 -08:00
|
|
|
wq heap // work queue, using min-heap for better locality
|
2020-04-20 12:13:42 -04:00
|
|
|
|
2021-10-21 18:04:55 -07:00
|
|
|
ifaceMethod map[methodsig]bool // methods called from reached interface call sites
|
|
|
|
|
genericIfaceMethod map[string]bool // names of methods called from reached generic interface call sites
|
|
|
|
|
markableMethods []methodref // methods of reached types
|
|
|
|
|
reflectSeen bool // whether we have seen a reflect method call
|
|
|
|
|
dynlink bool
|
2020-06-04 12:01:53 -04:00
|
|
|
|
|
|
|
|
methodsigstmp []methodsig // scratch buffer for decoding method signatures
|
2023-01-25 10:46:08 -05:00
|
|
|
pkginits []loader.Sym
|
|
|
|
|
mapinitnoop loader.Sym
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *deadcodePass) init() {
|
|
|
|
|
d.ldr.InitReachable()
|
|
|
|
|
d.ifaceMethod = make(map[methodsig]bool)
|
2021-10-21 18:04:55 -07:00
|
|
|
d.genericIfaceMethod = make(map[string]bool)
|
2021-04-15 23:05:49 -04:00
|
|
|
if buildcfg.Experiment.FieldTrack {
|
2020-04-20 12:13:42 -04:00
|
|
|
d.ldr.Reachparent = make([]loader.Sym, d.ldr.NSym())
|
|
|
|
|
}
|
2021-02-25 20:01:53 -05:00
|
|
|
d.dynlink = d.ctxt.DynlinkingGo()
|
2020-04-20 12:13:42 -04:00
|
|
|
|
|
|
|
|
if d.ctxt.BuildMode == BuildModeShared {
|
|
|
|
|
// Mark all symbols defined in this library as reachable when
|
|
|
|
|
// building a shared library.
|
|
|
|
|
n := d.ldr.NDef()
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
|
s := loader.Sym(i)
|
|
|
|
|
d.mark(s, 0)
|
|
|
|
|
}
|
cmd/link, runtime: initialize packages in shared build mode
Currently, for the shared build mode, we don't generate the module
inittasks. Instead, we rely on the main executable to do the
initialization, for both the executable and the shared library.
But, with the model as of CL 478916, the main executable only
has relocations to packages that are directly imported. It won't
see the dependency edges between packages within a shared library.
Therefore indirect dependencies are not included, and thus not
initialized. E.g. main imports a, which imports b, but main
doesn't directly import b. a and b are in a shared object. When
linking main, it sees main depends on a, so it generates main's
inittasks to run a's init before main's, but it doesn't know b,
so b's init doesn't run.
This CL makes it initialize all packages in a shared library when
the library is loaded, as any of them could potentially be
imported, directly or indirectly.
Also, in the runtime, when running the init functions, make sure
to go through the DSOs in dependency order. Otherwise packages
can be initialized in the wrong order.
Fixes #61973.
Change-Id: I2a090336fe9fa0d6c7e43912f3ab233c9c47e247
Reviewed-on: https://go-review.googlesource.com/c/go/+/520375
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-08-16 19:49:04 -04:00
|
|
|
d.mark(d.ctxt.mainInittasks, 0)
|
2020-04-20 12:13:42 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
|
|
// In a normal binary, start at main.main and the init
|
|
|
|
|
// functions and mark what is reachable from there.
|
|
|
|
|
if d.ctxt.linkShared && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) {
|
|
|
|
|
names = append(names, "main.main", "main..inittask")
|
|
|
|
|
} else {
|
|
|
|
|
// The external linker refers main symbol directly.
|
|
|
|
|
if d.ctxt.LinkMode == LinkExternal && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) {
|
|
|
|
|
if d.ctxt.HeadType == objabi.Hwindows && d.ctxt.Arch.Family == sys.I386 {
|
|
|
|
|
*flagEntrySymbol = "_main"
|
|
|
|
|
} else {
|
|
|
|
|
*flagEntrySymbol = "main"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
names = append(names, *flagEntrySymbol)
|
2021-05-13 16:48:50 -04:00
|
|
|
}
|
|
|
|
|
// runtime.unreachableMethod is a function that will throw if called.
|
|
|
|
|
// We redirect unreachable methods to it.
|
|
|
|
|
names = append(names, "runtime.unreachableMethod")
|
|
|
|
|
if d.ctxt.BuildMode == BuildModePlugin {
|
2021-05-08 00:45:06 +07:00
|
|
|
names = append(names, objabi.PathToPrefix(*flagPluginPath)+"..inittask", objabi.PathToPrefix(*flagPluginPath)+".main", "go:plugin.tabs")
|
2020-04-20 12:13:42 -04:00
|
|
|
|
2021-05-13 16:48:50 -04:00
|
|
|
// We don't keep the go.plugin.exports symbol,
|
|
|
|
|
// but we do keep the symbols it refers to.
|
2021-05-08 00:45:06 +07:00
|
|
|
exportsIdx := d.ldr.Lookup("go:plugin.exports", 0)
|
2021-05-13 16:48:50 -04:00
|
|
|
if exportsIdx != 0 {
|
|
|
|
|
relocs := d.ldr.Relocs(exportsIdx)
|
|
|
|
|
for i := 0; i < relocs.Count(); i++ {
|
|
|
|
|
d.mark(relocs.At(i).Sym(), 0)
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 11:13:32 -05:00
|
|
|
if d.ctxt.Debugvlog > 1 {
|
|
|
|
|
d.ctxt.Logf("deadcode start names: %v\n", names)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 12:13:42 -04:00
|
|
|
for _, name := range names {
|
|
|
|
|
// Mark symbol as a data/ABI0 symbol.
|
|
|
|
|
d.mark(d.ldr.Lookup(name, 0), 0)
|
2021-09-21 14:35:37 -04:00
|
|
|
if abiInternalVer != 0 {
|
|
|
|
|
// Also mark any Go functions (internal ABI).
|
|
|
|
|
d.mark(d.ldr.Lookup(name, abiInternalVer), 0)
|
|
|
|
|
}
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
cmd/link: build dynexp symbol list directly
Currently, setCgoAttr populates the cgo_export_{static,dynamic} maps
with symbol names of exported symbols, which are then re-looked-up by
deadcode and setupdynexp, which in turn puts the re-looked-up symbols
in ctxt.dynexp. setCgoAttr already looked up the Syms, so simplify all
of this by making setCgoAttr populate ctxt.dynexp directly and
eliminating the cgo_export_{static,dynamic} maps. Recording Syms
directly also sets us up to use correct symbol versions for these
exports, rather than just assuming version 0 for all lookups.
Since setupdynexp doesn't really do any "setting up" of dynexp any
more with this change, we fold the remaining logic from setupdynexp
directly into addexport, where it has better context anyway. This also
eliminates a sorting step, since we no longer do a non-deterministic
map iteration to build the dynexp slice.
For #40724.
Change-Id: I3e1a65165268da8c2bf50d7485f2624133433260
Reviewed-on: https://go-review.googlesource.com/c/go/+/309340
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
2021-04-12 13:21:55 -04:00
|
|
|
|
|
|
|
|
// All dynamic exports are roots.
|
|
|
|
|
for _, s := range d.ctxt.dynexp {
|
|
|
|
|
if d.ctxt.Debugvlog > 1 {
|
|
|
|
|
d.ctxt.Logf("deadcode start dynexp: %s<%d>\n", d.ldr.SymName(s), d.ldr.SymVersion(s))
|
|
|
|
|
}
|
|
|
|
|
d.mark(s, 0)
|
|
|
|
|
}
|
2023-01-25 10:46:08 -05:00
|
|
|
|
|
|
|
|
d.mapinitnoop = d.ldr.Lookup("runtime.mapinitnoop", abiInternalVer)
|
|
|
|
|
if d.mapinitnoop == 0 {
|
|
|
|
|
panic("could not look up runtime.mapinitnoop")
|
|
|
|
|
}
|
2023-01-12 20:25:39 -08:00
|
|
|
if d.ctxt.mainInittasks != 0 {
|
|
|
|
|
d.mark(d.ctxt.mainInittasks, 0)
|
|
|
|
|
}
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *deadcodePass) flood() {
|
2020-06-04 12:01:53 -04:00
|
|
|
var methods []methodref
|
2020-04-20 12:13:42 -04:00
|
|
|
for !d.wq.empty() {
|
|
|
|
|
symIdx := d.wq.pop()
|
|
|
|
|
|
2023-08-24 10:17:20 +03:00
|
|
|
// Methods may be called via reflection. Give up on static analysis,
|
|
|
|
|
// and mark all exported methods of all reachable types as reachable.
|
2020-04-20 12:13:42 -04:00
|
|
|
d.reflectSeen = d.reflectSeen || d.ldr.IsReflectMethod(symIdx)
|
|
|
|
|
|
|
|
|
|
isgotype := d.ldr.IsGoType(symIdx)
|
|
|
|
|
relocs := d.ldr.Relocs(symIdx)
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
var usedInIface bool
|
2020-04-20 12:13:42 -04:00
|
|
|
|
|
|
|
|
if isgotype {
|
2021-02-25 20:01:53 -05:00
|
|
|
if d.dynlink {
|
2021-03-14 00:09:05 +00:00
|
|
|
// When dynamic linking, a type may be passed across DSO
|
2021-02-25 20:01:53 -05:00
|
|
|
// boundary and get converted to interface at the other side.
|
|
|
|
|
d.ldr.SetAttrUsedInIface(symIdx, true)
|
|
|
|
|
}
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
usedInIface = d.ldr.AttrUsedInIface(symIdx)
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-04 12:01:53 -04:00
|
|
|
methods = methods[:0]
|
2020-04-20 12:13:42 -04:00
|
|
|
for i := 0; i < relocs.Count(); i++ {
|
2020-07-29 13:26:50 -04:00
|
|
|
r := relocs.At(i)
|
2023-09-06 10:15:37 -04:00
|
|
|
if r.Weak() {
|
|
|
|
|
convertWeakToStrong := false
|
|
|
|
|
// When build with "-linkshared", we can't tell if the
|
|
|
|
|
// interface method in itab will be used or not.
|
|
|
|
|
// Ignore the weak attribute.
|
|
|
|
|
if d.ctxt.linkShared && d.ldr.IsItab(symIdx) {
|
|
|
|
|
convertWeakToStrong = true
|
|
|
|
|
}
|
|
|
|
|
// If the program uses plugins, we can no longer treat
|
|
|
|
|
// relocs from pkg init functions to outlined map init
|
|
|
|
|
// fragments as weak, since doing so can cause package
|
|
|
|
|
// init clashes between the main program and the
|
|
|
|
|
// plugin. See #62430 for more details.
|
|
|
|
|
if d.ctxt.canUsePlugins && r.Type().IsDirectCall() {
|
|
|
|
|
convertWeakToStrong = true
|
|
|
|
|
}
|
|
|
|
|
if !convertWeakToStrong {
|
|
|
|
|
// skip this reloc
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-11-08 11:50:10 -05:00
|
|
|
}
|
2020-04-20 12:13:42 -04:00
|
|
|
t := r.Type()
|
2020-09-21 20:44:53 -04:00
|
|
|
switch t {
|
|
|
|
|
case objabi.R_METHODOFF:
|
2020-04-20 12:13:42 -04:00
|
|
|
if i+2 >= relocs.Count() {
|
|
|
|
|
panic("expect three consecutive R_METHODOFF relocs")
|
|
|
|
|
}
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
if usedInIface {
|
|
|
|
|
methods = append(methods, methodref{src: symIdx, r: i})
|
2020-09-17 21:34:52 -04:00
|
|
|
// The method descriptor is itself a type descriptor, and
|
|
|
|
|
// it can be used to reach other types, e.g. by using
|
|
|
|
|
// reflect.Type.Method(i).Type.In(j). We need to traverse
|
|
|
|
|
// its child types with UsedInIface set. (See also the
|
|
|
|
|
// comment below.)
|
|
|
|
|
rs := r.Sym()
|
|
|
|
|
if !d.ldr.AttrUsedInIface(rs) {
|
|
|
|
|
d.ldr.SetAttrUsedInIface(rs, true)
|
|
|
|
|
if d.ldr.AttrReachable(rs) {
|
|
|
|
|
d.ldr.SetAttrReachable(rs, false)
|
|
|
|
|
d.mark(rs, symIdx)
|
|
|
|
|
}
|
|
|
|
|
}
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
}
|
2020-04-20 12:13:42 -04:00
|
|
|
i += 2
|
|
|
|
|
continue
|
2020-09-21 20:44:53 -04:00
|
|
|
case objabi.R_USETYPE:
|
2020-04-20 12:13:42 -04:00
|
|
|
// type symbol used for DWARF. we need to load the symbol but it may not
|
|
|
|
|
// be otherwise reachable in the program.
|
|
|
|
|
// do nothing for now as we still load all type symbols.
|
|
|
|
|
continue
|
2020-09-21 20:44:53 -04:00
|
|
|
case objabi.R_USEIFACE:
|
2020-09-20 23:29:20 -04:00
|
|
|
// R_USEIFACE is a marker relocation that tells the linker the type is
|
|
|
|
|
// converted to an interface, i.e. should have UsedInIface set. See the
|
|
|
|
|
// comment below for why we need to unset the Reachable bit and re-mark it.
|
|
|
|
|
rs := r.Sym()
|
2023-05-02 17:37:00 +00:00
|
|
|
if d.ldr.IsItab(rs) {
|
|
|
|
|
// This relocation can also point at an itab, in which case it
|
2023-12-12 20:40:33 -08:00
|
|
|
// means "the Type field of that itab".
|
2023-05-02 17:37:00 +00:00
|
|
|
rs = decodeItabType(d.ldr, d.ctxt.Arch, rs)
|
|
|
|
|
}
|
|
|
|
|
if !d.ldr.IsGoType(rs) && !d.ctxt.linkShared {
|
|
|
|
|
panic(fmt.Sprintf("R_USEIFACE in %s references %s which is not a type or itab", d.ldr.SymName(symIdx), d.ldr.SymName(rs)))
|
|
|
|
|
}
|
2020-09-20 23:29:20 -04:00
|
|
|
if !d.ldr.AttrUsedInIface(rs) {
|
|
|
|
|
d.ldr.SetAttrUsedInIface(rs, true)
|
|
|
|
|
if d.ldr.AttrReachable(rs) {
|
|
|
|
|
d.ldr.SetAttrReachable(rs, false)
|
|
|
|
|
d.mark(rs, symIdx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue
|
2020-09-21 20:44:53 -04:00
|
|
|
case objabi.R_USEIFACEMETHOD:
|
|
|
|
|
// R_USEIFACEMETHOD is a marker relocation that marks an interface
|
|
|
|
|
// method as used.
|
|
|
|
|
rs := r.Sym()
|
2021-02-01 13:36:50 -05:00
|
|
|
if d.ctxt.linkShared && (d.ldr.SymType(rs) == sym.SDYNIMPORT || d.ldr.SymType(rs) == sym.Sxxx) {
|
|
|
|
|
// Don't decode symbol from shared library (we'll mark all exported methods anyway).
|
|
|
|
|
// We check for both SDYNIMPORT and Sxxx because name-mangled symbols haven't
|
|
|
|
|
// been resolved at this point.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
m := d.decodeIfaceMethod(d.ldr, d.ctxt.Arch, rs, r.Add())
|
|
|
|
|
if d.ctxt.Debugvlog > 1 {
|
|
|
|
|
d.ctxt.Logf("reached iface method: %v\n", m)
|
2020-09-21 20:44:53 -04:00
|
|
|
}
|
2021-02-01 13:36:50 -05:00
|
|
|
d.ifaceMethod[m] = true
|
2020-09-21 20:44:53 -04:00
|
|
|
continue
|
2023-08-25 16:06:36 +03:00
|
|
|
case objabi.R_USENAMEDMETHOD:
|
2021-10-21 18:04:55 -07:00
|
|
|
name := d.decodeGenericIfaceMethod(d.ldr, r.Sym())
|
|
|
|
|
if d.ctxt.Debugvlog > 1 {
|
|
|
|
|
d.ctxt.Logf("reached generic iface method: %s\n", name)
|
|
|
|
|
}
|
|
|
|
|
d.genericIfaceMethod[name] = true
|
|
|
|
|
continue // don't mark referenced symbol - it is not needed in the final binary.
|
2023-01-12 20:25:39 -08:00
|
|
|
case objabi.R_INITORDER:
|
|
|
|
|
// inittasks has already run, so any R_INITORDER links are now
|
|
|
|
|
// superfluous - the only live inittask records are those which are
|
|
|
|
|
// in a scheduled list somewhere (e.g. runtime.moduledata.inittasks).
|
|
|
|
|
continue
|
2020-09-20 23:29:20 -04:00
|
|
|
}
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
rs := r.Sym()
|
|
|
|
|
if isgotype && usedInIface && d.ldr.IsGoType(rs) && !d.ldr.AttrUsedInIface(rs) {
|
|
|
|
|
// If a type is converted to an interface, it is possible to obtain an
|
|
|
|
|
// interface with a "child" type of it using reflection (e.g. obtain an
|
|
|
|
|
// interface of T from []chan T). We need to traverse its "child" types
|
|
|
|
|
// with UsedInIface attribute set.
|
|
|
|
|
// When visiting the child type (chan T in the example above), it will
|
|
|
|
|
// have UsedInIface set, so it in turn will mark and (re)visit its children
|
|
|
|
|
// (e.g. T above).
|
|
|
|
|
// We unset the reachable bit here, so if the child type is already visited,
|
|
|
|
|
// it will be visited again.
|
|
|
|
|
// Note that a type symbol can be visited at most twice, one without
|
|
|
|
|
// UsedInIface and one with. So termination is still guaranteed.
|
|
|
|
|
d.ldr.SetAttrUsedInIface(rs, true)
|
|
|
|
|
d.ldr.SetAttrReachable(rs, false)
|
|
|
|
|
}
|
|
|
|
|
d.mark(rs, symIdx)
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
naux := d.ldr.NAux(symIdx)
|
|
|
|
|
for i := 0; i < naux; i++ {
|
2020-07-29 13:26:50 -04:00
|
|
|
a := d.ldr.Aux(symIdx, i)
|
2020-09-18 11:56:43 -04:00
|
|
|
if a.Type() == goobj.AuxGotype {
|
2020-04-30 23:10:35 -04:00
|
|
|
// A symbol being reachable doesn't imply we need its
|
|
|
|
|
// type descriptor. Don't mark it.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
d.mark(a.Sym(), symIdx)
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
2023-01-25 10:46:08 -05:00
|
|
|
// Record sym if package init func (here naux != 0 is a cheap way
|
|
|
|
|
// to check first if it is a function symbol).
|
|
|
|
|
if naux != 0 && d.ldr.IsPkgInit(symIdx) {
|
|
|
|
|
|
|
|
|
|
d.pkginits = append(d.pkginits, symIdx)
|
|
|
|
|
}
|
2020-04-20 12:13:42 -04:00
|
|
|
// Some host object symbols have an outer object, which acts like a
|
|
|
|
|
// "carrier" symbol, or it holds all the symbols for a particular
|
|
|
|
|
// section. We need to mark all "referenced" symbols from that carrier,
|
|
|
|
|
// so we make sure we're pulling in all outer symbols, and their sub
|
|
|
|
|
// symbols. This is not ideal, and these carrier/section symbols could
|
|
|
|
|
// be removed.
|
|
|
|
|
if d.ldr.IsExternal(symIdx) {
|
|
|
|
|
d.mark(d.ldr.OuterSym(symIdx), symIdx)
|
|
|
|
|
d.mark(d.ldr.SubSym(symIdx), symIdx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(methods) != 0 {
|
|
|
|
|
if !isgotype {
|
|
|
|
|
panic("method found on non-type symbol")
|
|
|
|
|
}
|
|
|
|
|
// Decode runtime type information for type methods
|
|
|
|
|
// to help work out which methods can be called
|
|
|
|
|
// dynamically via interfaces.
|
2020-04-29 17:34:46 -04:00
|
|
|
methodsigs := d.decodetypeMethods(d.ldr, d.ctxt.Arch, symIdx, &relocs)
|
2020-04-20 12:13:42 -04:00
|
|
|
if len(methods) != len(methodsigs) {
|
|
|
|
|
panic(fmt.Sprintf("%q has %d method relocations for %d methods", d.ldr.SymName(symIdx), len(methods), len(methodsigs)))
|
|
|
|
|
}
|
|
|
|
|
for i, m := range methodsigs {
|
|
|
|
|
methods[i].m = m
|
2020-06-03 13:28:18 -04:00
|
|
|
if d.ctxt.Debugvlog > 1 {
|
|
|
|
|
d.ctxt.Logf("markable method: %v of sym %v %s\n", m, symIdx, d.ldr.SymName(symIdx))
|
|
|
|
|
}
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
d.markableMethods = append(d.markableMethods, methods...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 10:46:08 -05:00
|
|
|
// mapinitcleanup walks all pkg init functions and looks for weak relocations
|
|
|
|
|
// to mapinit symbols that are no longer reachable. It rewrites
|
|
|
|
|
// the relocs to target a new no-op routine in the runtime.
|
|
|
|
|
func (d *deadcodePass) mapinitcleanup() {
|
|
|
|
|
for _, idx := range d.pkginits {
|
|
|
|
|
relocs := d.ldr.Relocs(idx)
|
|
|
|
|
var su *loader.SymbolBuilder
|
|
|
|
|
for i := 0; i < relocs.Count(); i++ {
|
|
|
|
|
r := relocs.At(i)
|
|
|
|
|
rs := r.Sym()
|
|
|
|
|
if r.Weak() && r.Type().IsDirectCall() && !d.ldr.AttrReachable(rs) {
|
|
|
|
|
// double check to make sure target is indeed map.init
|
|
|
|
|
rsn := d.ldr.SymName(rs)
|
|
|
|
|
if !strings.Contains(rsn, "map.init") {
|
|
|
|
|
panic(fmt.Sprintf("internal error: expected map.init sym for weak call reloc, got %s -> %s", d.ldr.SymName(idx), rsn))
|
|
|
|
|
}
|
|
|
|
|
d.ldr.SetAttrReachable(d.mapinitnoop, true)
|
|
|
|
|
if d.ctxt.Debugvlog > 1 {
|
|
|
|
|
d.ctxt.Logf("deadcode: %s rewrite %s ref to %s\n",
|
|
|
|
|
d.ldr.SymName(idx), rsn,
|
|
|
|
|
d.ldr.SymName(d.mapinitnoop))
|
|
|
|
|
}
|
|
|
|
|
if su == nil {
|
|
|
|
|
su = d.ldr.MakeSymbolUpdater(idx)
|
|
|
|
|
}
|
|
|
|
|
su.SetRelocSym(i, d.mapinitnoop)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 12:13:42 -04:00
|
|
|
func (d *deadcodePass) mark(symIdx, parent loader.Sym) {
|
|
|
|
|
if symIdx != 0 && !d.ldr.AttrReachable(symIdx) {
|
|
|
|
|
d.wq.push(symIdx)
|
|
|
|
|
d.ldr.SetAttrReachable(symIdx, true)
|
2021-04-15 23:05:49 -04:00
|
|
|
if buildcfg.Experiment.FieldTrack && d.ldr.Reachparent[symIdx] == 0 {
|
2020-04-20 12:13:42 -04:00
|
|
|
d.ldr.Reachparent[symIdx] = parent
|
|
|
|
|
}
|
|
|
|
|
if *flagDumpDep {
|
|
|
|
|
to := d.ldr.SymName(symIdx)
|
|
|
|
|
if to != "" {
|
2023-05-17 11:35:52 +02:00
|
|
|
to = d.dumpDepAddFlags(to, symIdx)
|
2020-04-20 12:13:42 -04:00
|
|
|
from := "_"
|
|
|
|
|
if parent != 0 {
|
|
|
|
|
from = d.ldr.SymName(parent)
|
2023-05-17 11:35:52 +02:00
|
|
|
from = d.dumpDepAddFlags(from, parent)
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
fmt.Printf("%s -> %s\n", from, to)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 11:35:52 +02:00
|
|
|
func (d *deadcodePass) dumpDepAddFlags(name string, symIdx loader.Sym) string {
|
|
|
|
|
var flags strings.Builder
|
|
|
|
|
if d.ldr.AttrUsedInIface(symIdx) {
|
|
|
|
|
flags.WriteString("<UsedInIface>")
|
|
|
|
|
}
|
|
|
|
|
if d.ldr.IsReflectMethod(symIdx) {
|
|
|
|
|
flags.WriteString("<ReflectMethod>")
|
|
|
|
|
}
|
|
|
|
|
if flags.Len() > 0 {
|
|
|
|
|
return name + " " + flags.String()
|
|
|
|
|
}
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 17:34:46 -04:00
|
|
|
func (d *deadcodePass) markMethod(m methodref) {
|
2020-04-20 12:13:42 -04:00
|
|
|
relocs := d.ldr.Relocs(m.src)
|
2020-07-29 13:26:50 -04:00
|
|
|
d.mark(relocs.At(m.r).Sym(), m.src)
|
|
|
|
|
d.mark(relocs.At(m.r+1).Sym(), m.src)
|
|
|
|
|
d.mark(relocs.At(m.r+2).Sym(), m.src)
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
// deadcode marks all reachable symbols.
|
|
|
|
|
//
|
|
|
|
|
// The basis of the dead code elimination is a flood fill of symbols,
|
2016-08-21 18:34:24 -04:00
|
|
|
// following their relocations, beginning at *flagEntrySymbol.
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
//
|
|
|
|
|
// This flood fill is wrapped in logic for pruning unused methods.
|
|
|
|
|
// All methods are mentioned by relocations on their receiver's *rtype.
|
cmd/compile, etc: store method tables as offsets
This CL introduces the typeOff type and a lookup method of the same
name that can turn a typeOff offset into an *rtype.
In a typical Go binary (built with buildmode=exe, pie, c-archive, or
c-shared), there is one moduledata and all typeOff values are offsets
relative to firstmoduledata.types. This makes computing the pointer
cheap in typical programs.
With buildmode=shared (and one day, buildmode=plugin) there are
multiple modules whose relative offset is determined at runtime.
We identify a type in the general case by the pair of the original
*rtype that references it and its typeOff value. We determine
the module from the original pointer, and then use the typeOff from
there to compute the final *rtype.
To ensure there is only one *rtype representing each type, the
runtime initializes a typemap for each module, using any identical
type from an earlier module when resolving that offset. This means
that types computed from an offset match the type mapped by the
pointer dynamic relocations.
A series of followup CLs will replace other *rtype values with typeOff
(and name/*string with nameOff).
For types created at runtime by reflect, type offsets are treated as
global IDs and reference into a reflect offset map kept by the runtime.
darwin/amd64:
cmd/go: -57KB (0.6%)
jujud: -557KB (0.8%)
linux/amd64 PIE:
cmd/go: -361KB (3.0%)
jujud: -3.5MB (4.2%)
For #6853.
Change-Id: Icf096fd884a0a0cb9f280f46f7a26c70a9006c96
Reviewed-on: https://go-review.googlesource.com/21285
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-28 10:32:27 -04:00
|
|
|
// These relocations are specially defined as R_METHODOFF by the compiler
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
// so we can detect and manipulated them here.
|
|
|
|
|
//
|
|
|
|
|
// There are three ways a method of a reachable type can be invoked:
|
|
|
|
|
//
|
2022-02-03 14:12:08 -05:00
|
|
|
// 1. direct call
|
|
|
|
|
// 2. through a reachable interface type
|
|
|
|
|
// 3. reflect.Value.Method (or MethodByName), or reflect.Type.Method
|
|
|
|
|
// (or MethodByName)
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
//
|
|
|
|
|
// The first case is handled by the flood fill, a directly called method
|
|
|
|
|
// is marked as reachable.
|
|
|
|
|
//
|
|
|
|
|
// The second case is handled by decomposing all reachable interface
|
|
|
|
|
// types into method signatures. Each encountered method is compared
|
|
|
|
|
// against the interface method signatures, if it matches it is marked
|
|
|
|
|
// as reachable. This is extremely conservative, but easy and correct.
|
|
|
|
|
//
|
2023-08-24 10:17:20 +03:00
|
|
|
// The third case is handled by looking for functions that compiler flagged
|
|
|
|
|
// as REFLECTMETHOD. REFLECTMETHOD on a function F means that F does a method
|
|
|
|
|
// lookup with reflection, but the compiler was not able to statically determine
|
|
|
|
|
// the method name.
|
2022-02-03 14:12:08 -05:00
|
|
|
//
|
2023-08-24 10:17:20 +03:00
|
|
|
// All functions that call reflect.Value.Method or reflect.Type.Method are REFLECTMETHODs.
|
|
|
|
|
// Functions that call reflect.Value.MethodByName or reflect.Type.MethodByName with
|
|
|
|
|
// a non-constant argument are REFLECTMETHODs, too. If we find a REFLECTMETHOD,
|
|
|
|
|
// we give up on static analysis, and mark all exported methods of all reachable
|
|
|
|
|
// types as reachable.
|
|
|
|
|
//
|
|
|
|
|
// If the argument to MethodByName is a compile-time constant, the compiler
|
|
|
|
|
// emits a relocation with the method name. Matching methods are kept in all
|
|
|
|
|
// reachable types.
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
//
|
|
|
|
|
// Any unreached text symbols are removed from ctxt.Textp.
|
|
|
|
|
func deadcode(ctxt *Link) {
|
2020-04-20 12:13:42 -04:00
|
|
|
ldr := ctxt.loader
|
|
|
|
|
d := deadcodePass{ctxt: ctxt, ldr: ldr}
|
|
|
|
|
d.init()
|
|
|
|
|
d.flood()
|
|
|
|
|
|
|
|
|
|
if ctxt.DynlinkingGo() {
|
|
|
|
|
// Exported methods may satisfy interfaces we don't know
|
|
|
|
|
// about yet when dynamically linking.
|
|
|
|
|
d.reflectSeen = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
// Mark all methods that could satisfy a discovered
|
|
|
|
|
// interface as reachable. We recheck old marked interfaces
|
|
|
|
|
// as new types (with new methods) may have been discovered
|
|
|
|
|
// in the last pass.
|
|
|
|
|
rem := d.markableMethods[:0]
|
|
|
|
|
for _, m := range d.markableMethods {
|
2022-03-16 13:07:57 -04:00
|
|
|
if (d.reflectSeen && (m.isExported() || d.dynlink)) || d.ifaceMethod[m.m] || d.genericIfaceMethod[m.m.name] {
|
2020-04-20 12:13:42 -04:00
|
|
|
d.markMethod(m)
|
|
|
|
|
} else {
|
|
|
|
|
rem = append(rem, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
d.markableMethods = rem
|
|
|
|
|
|
|
|
|
|
if d.wq.empty() {
|
|
|
|
|
// No new work was discovered. Done.
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
d.flood()
|
|
|
|
|
}
|
2023-01-25 10:46:08 -05:00
|
|
|
if *flagPruneWeakMap {
|
|
|
|
|
d.mapinitcleanup()
|
|
|
|
|
}
|
2019-09-28 22:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-03 13:28:18 -04:00
|
|
|
// methodsig is a typed method signature (name + type).
|
|
|
|
|
type methodsig struct {
|
|
|
|
|
name string
|
|
|
|
|
typ loader.Sym // type descriptor symbol of the function
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 17:34:46 -04:00
|
|
|
// methodref holds the relocations from a receiver type symbol to its
|
2020-04-20 12:13:42 -04:00
|
|
|
// method. There are three relocations, one for each of the fields in
|
|
|
|
|
// the reflect.method struct: mtyp, ifn, and tfn.
|
2020-04-29 17:34:46 -04:00
|
|
|
type methodref struct {
|
2020-04-20 12:13:42 -04:00
|
|
|
m methodsig
|
|
|
|
|
src loader.Sym // receiver type symbol
|
|
|
|
|
r int // the index of R_METHODOFF relocations
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 17:34:46 -04:00
|
|
|
func (m methodref) isExported() bool {
|
2020-06-03 13:28:18 -04:00
|
|
|
for _, r := range m.m.name {
|
2020-04-20 12:13:42 -04:00
|
|
|
return unicode.IsUpper(r)
|
|
|
|
|
}
|
|
|
|
|
panic("methodref has no signature")
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 13:28:18 -04:00
|
|
|
// decodeMethodSig decodes an array of method signature information.
|
2020-04-20 12:13:42 -04:00
|
|
|
// Each element of the array is size bytes. The first 4 bytes is a
|
|
|
|
|
// nameOff for the method name, and the next 4 bytes is a typeOff for
|
|
|
|
|
// the function type.
|
|
|
|
|
//
|
|
|
|
|
// Conveniently this is the layout of both runtime.method and runtime.imethod.
|
2020-04-29 17:34:46 -04:00
|
|
|
func (d *deadcodePass) decodeMethodSig(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs, off, size, count int) []methodsig {
|
2020-06-04 12:01:53 -04:00
|
|
|
if cap(d.methodsigstmp) < count {
|
|
|
|
|
d.methodsigstmp = append(d.methodsigstmp[:0], make([]methodsig, count)...)
|
|
|
|
|
}
|
|
|
|
|
var methods = d.methodsigstmp[:count]
|
2020-04-20 12:13:42 -04:00
|
|
|
for i := 0; i < count; i++ {
|
2020-06-03 13:28:18 -04:00
|
|
|
methods[i].name = decodetypeName(ldr, symIdx, relocs, off)
|
|
|
|
|
methods[i].typ = decodeRelocSym(ldr, symIdx, relocs, int32(off+4))
|
2020-04-20 12:13:42 -04:00
|
|
|
off += size
|
|
|
|
|
}
|
|
|
|
|
return methods
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 20:44:53 -04:00
|
|
|
// Decode the method of interface type symbol symIdx at offset off.
|
|
|
|
|
func (d *deadcodePass) decodeIfaceMethod(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, off int64) methodsig {
|
2020-04-20 12:13:42 -04:00
|
|
|
p := ldr.Data(symIdx)
|
2021-08-07 22:26:46 -07:00
|
|
|
if p == nil {
|
|
|
|
|
panic(fmt.Sprintf("missing symbol %q", ldr.SymName(symIdx)))
|
|
|
|
|
}
|
2024-04-01 15:39:41 -04:00
|
|
|
if decodetypeKind(arch, p) != abi.Interface {
|
2020-04-20 12:13:42 -04:00
|
|
|
panic(fmt.Sprintf("symbol %q is not an interface", ldr.SymName(symIdx)))
|
|
|
|
|
}
|
2020-09-21 20:44:53 -04:00
|
|
|
relocs := ldr.Relocs(symIdx)
|
|
|
|
|
var m methodsig
|
|
|
|
|
m.name = decodetypeName(ldr, symIdx, &relocs, int(off))
|
|
|
|
|
m.typ = decodeRelocSym(ldr, symIdx, &relocs, int32(off+4))
|
|
|
|
|
return m
|
2020-04-20 12:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:04:55 -07:00
|
|
|
// Decode the method name stored in symbol symIdx. The symbol should contain just the bytes of a method name.
|
|
|
|
|
func (d *deadcodePass) decodeGenericIfaceMethod(ldr *loader.Loader, symIdx loader.Sym) string {
|
2023-05-01 12:54:27 -04:00
|
|
|
return ldr.DataString(symIdx)
|
2021-10-21 18:04:55 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 17:34:46 -04:00
|
|
|
func (d *deadcodePass) decodetypeMethods(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs) []methodsig {
|
2020-04-20 12:13:42 -04:00
|
|
|
p := ldr.Data(symIdx)
|
|
|
|
|
if !decodetypeHasUncommon(arch, p) {
|
|
|
|
|
panic(fmt.Sprintf("no methods on %q", ldr.SymName(symIdx)))
|
|
|
|
|
}
|
|
|
|
|
off := commonsize(arch) // reflect.rtype
|
2024-04-01 15:39:41 -04:00
|
|
|
switch decodetypeKind(arch, p) {
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Struct: // reflect.structType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += 4 * arch.PtrSize
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Pointer: // reflect.ptrType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += arch.PtrSize
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Func: // reflect.funcType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += arch.PtrSize // 4 bytes, pointer aligned
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Slice: // reflect.sliceType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += arch.PtrSize
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Array: // reflect.arrayType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += 3 * arch.PtrSize
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Chan: // reflect.chanType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += 2 * arch.PtrSize
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Map: // reflect.mapType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += 4*arch.PtrSize + 8
|
2024-04-02 13:08:24 +00:00
|
|
|
case abi.Interface: // reflect.interfaceType
|
2020-04-20 12:13:42 -04:00
|
|
|
off += 3 * arch.PtrSize
|
|
|
|
|
default:
|
|
|
|
|
// just Sizeof(rtype)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mcount := int(decodeInuxi(arch, p[off+4:], 2))
|
|
|
|
|
moff := int(decodeInuxi(arch, p[off+4+2+2:], 4))
|
|
|
|
|
off += moff // offset to array of reflect.method values
|
|
|
|
|
const sizeofMethod = 4 * 4 // sizeof reflect.method in program
|
2020-04-29 17:34:46 -04:00
|
|
|
return d.decodeMethodSig(ldr, arch, symIdx, relocs, off, sizeofMethod, mcount)
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
}
|