Skip to content

Commit

Permalink
cmd/link: retain only used interface methods
Browse files Browse the repository at this point in the history
Currently, in the linker's deadcode pass, when an interface type
is live, the linker thinks all its methods are live, and uses
them to match methods on concrete types. The interface method may
never be used, though.

This CL changes it to only keep used interface methods, for
matching concrete type methods. To do that, when an interface
method is used, the compiler generates a mark relocation. The
linker uses the marker relocations to mark used interface
methods, and only the used ones.

binary size    before      after
cmd/compile   18887400   18812200
cmd/go        13470652   13470492

Change-Id: I3cfd9df4a53783330ba87735853f2a0ec3c42802
Reviewed-on: https://go-review.googlesource.com/c/go/+/256798
Trust: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
  • Loading branch information
cherrymui committed Sep 29, 2020
1 parent 39a426d commit 39dde09
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 64 deletions.
19 changes: 17 additions & 2 deletions src/cmd/compile/internal/gc/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ const (
MAXELEMSIZE = 128
)

func structfieldSize() int { return 3 * Widthptr } // Sizeof(runtime.structfield{})
func imethodSize() int { return 4 + 4 } // Sizeof(runtime.imethod{})
func structfieldSize() int { return 3 * Widthptr } // Sizeof(runtime.structfield{})
func imethodSize() int { return 4 + 4 } // Sizeof(runtime.imethod{})
func commonSize() int { return 4*Widthptr + 8 + 8 } // Sizeof(runtime._type{})

func uncommonSize(t *types.Type) int { // Sizeof(runtime.uncommontype{})
if t.Sym == nil && len(methods(t)) == 0 {
Expand Down Expand Up @@ -1422,6 +1423,20 @@ func dtypesym(t *types.Type) *obj.LSym {
return lsym
}

// ifaceMethodOffset returns the offset of the i-th method in the interface
// type descriptor, ityp.
func ifaceMethodOffset(ityp *types.Type, i int64) int64 {
// interface type descriptor layout is struct {
// _type // commonSize
// pkgpath // 1 word
// []imethod // 3 words (pointing to [...]imethod below)
// uncommontype // uncommonSize
// [...]imethod
// }
// The size of imethod is 8.
return int64(commonSize()+4*Widthptr+uncommonSize(ityp)) + i*8
}

// for each itabEntry, gather the methods on
// the concrete type that implement the interface
func peekitabs() {
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/compile/internal/gc/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ opswitch:
case OCALLINTER, OCALLFUNC, OCALLMETH:
if n.Op == OCALLINTER {
usemethod(n)
markUsedIfaceMethod(n)
}

if n.Op == OCALLFUNC && n.Left.Op == OCLOSURE {
Expand Down Expand Up @@ -1630,6 +1631,20 @@ func markTypeUsedInInterface(t *types.Type, from *obj.LSym) {
r.Type = objabi.R_USEIFACE
}

// markUsedIfaceMethod marks that an interface method is used in the current
// function. n is OCALLINTER node.
func markUsedIfaceMethod(n *Node) {
ityp := n.Left.Left.Type
tsym := typenamesym(ityp).Linksym()
r := obj.Addrel(Curfn.Func.lsym)
r.Sym = tsym
// n.Left.Xoffset is the method index * Widthptr (the offset of code pointer
// in itab).
midx := n.Left.Xoffset / int64(Widthptr)
r.Add = ifaceMethodOffset(ityp, midx)
r.Type = objabi.R_USEIFACEMETHOD
}

// rtconvfn returns the parameter and result types that will be used by a
// runtime function to convert from type src to type dst. The runtime function
// name can be derived from the names of the returned types.
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/internal/objabi/reloctype.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ const (
// This is a marker relocation (0-sized), for the linker's reachabililty
// analysis.
R_USEIFACE
// R_USEIFACEMETHOD marks an interface method that is used in the function
// this relocation is applied to. The target is an interface type descriptor.
// The addend is the offset of the method in the type descriptor.
// This is a marker relocation (0-sized), for the linker's reachabililty
// analysis.
R_USEIFACEMETHOD
// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
// holding the data being relocated to the referenced symbol.
// It is a variant of R_ADDROFF used when linking from the uncommonType of a
Expand Down
67 changes: 34 additions & 33 deletions src/cmd/internal/objabi/reloctype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 24 additions & 29 deletions src/cmd/link/internal/ld/deadcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,16 @@ func (d *deadcodePass) flood() {

if isgotype {
usedInIface = d.ldr.AttrUsedInIface(symIdx)
p := d.ldr.Data(symIdx)
if len(p) != 0 && decodetypeKind(d.ctxt.Arch, p)&kindMask == kindInterface {
for _, sig := range d.decodeIfaceMethods(d.ldr, d.ctxt.Arch, symIdx, &relocs) {
if d.ctxt.Debugvlog > 1 {
d.ctxt.Logf("reached iface method: %v\n", sig)
}
d.ifaceMethod[sig] = true
}
}
}

methods = methods[:0]
for i := 0; i < relocs.Count(); i++ {
r := relocs.At(i)
t := r.Type()
if t == objabi.R_WEAKADDROFF {
switch t {
case objabi.R_WEAKADDROFF:
continue
}
if t == objabi.R_METHODOFF {
case objabi.R_METHODOFF:
if i+2 >= relocs.Count() {
panic("expect three consecutive R_METHODOFF relocs")
}
Expand All @@ -146,14 +137,12 @@ func (d *deadcodePass) flood() {
}
i += 2
continue
}
if t == objabi.R_USETYPE {
case objabi.R_USETYPE:
// 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
}
if t == objabi.R_USEIFACE {
case objabi.R_USEIFACE:
// 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.
Expand All @@ -166,6 +155,18 @@ func (d *deadcodePass) flood() {
}
}
continue
case objabi.R_USEIFACEMETHOD:
// R_USEIFACEMETHOD is a marker relocation that marks an interface
// method as used.
rs := r.Sym()
if d.ldr.SymType(rs) != sym.SDYNIMPORT { // don't decode DYNIMPORT symbol (we'll mark all exported methods anyway)
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)
}
d.ifaceMethod[m] = true
}
continue
}
rs := r.Sym()
if isgotype && usedInIface && d.ldr.IsGoType(rs) && !d.ldr.AttrUsedInIface(rs) {
Expand Down Expand Up @@ -378,23 +379,17 @@ func (d *deadcodePass) decodeMethodSig(ldr *loader.Loader, arch *sys.Arch, symId
return methods
}

func (d *deadcodePass) decodeIfaceMethods(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs) []methodsig {
// 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 {
p := ldr.Data(symIdx)
if decodetypeKind(arch, p)&kindMask != kindInterface {
panic(fmt.Sprintf("symbol %q is not an interface", ldr.SymName(symIdx)))
}
rel := decodeReloc(ldr, symIdx, relocs, int32(commonsize(arch)+arch.PtrSize))
s := rel.Sym()
if s == 0 {
return nil
}
if s != symIdx {
panic(fmt.Sprintf("imethod slice pointer in %q leads to a different symbol", ldr.SymName(symIdx)))
}
off := int(rel.Add()) // array of reflect.imethod values
numMethods := int(decodetypeIfaceMethodCount(arch, p))
sizeofIMethod := 4 + 4
return d.decodeMethodSig(ldr, arch, symIdx, relocs, off, sizeofIMethod, numMethods)
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
}

func (d *deadcodePass) decodetypeMethods(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs) []methodsig {
Expand Down
1 change: 1 addition & 0 deletions src/cmd/link/internal/ld/deadcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestDeadcode(t *testing.T) {
{"ifacemethod", "", "main.T.M"},
{"ifacemethod2", "main.T.M", ""},
{"ifacemethod3", "main.S.M", ""},
{"ifacemethod4", "", "main.T.M"},
}
for _, test := range tests {
test := test
Expand Down
23 changes: 23 additions & 0 deletions src/cmd/link/internal/ld/testdata/deadcode/ifacemethod4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test that a live type's method is not live even if
// it matches an interface method, as long as the interface
// method is not used.

package main

type T int

func (T) M() {}

type I interface{ M() }

var p *T
var pp *I

func main() {
p = new(T) // use type T
pp = new(I) // use type I
}

0 comments on commit 39dde09

Please sign in to comment.