aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj
diff options
context:
space:
mode:
authorDavid Lazar <lazard@golang.org>2017-02-17 12:28:05 -0500
committerDavid Lazar <lazard@golang.org>2017-03-03 21:29:30 +0000
commit699175a11adfe57e859f4b995f4f5dfdaa5a5911 (patch)
tree3d77417cafc940851e020248d64a8139bd0328b9 /src/cmd/internal/obj
parented70f37e73a656551077332449074b6b19686ab3 (diff)
downloadgo-699175a11adfe57e859f4b995f4f5dfdaa5a5911.tar.xz
cmd/compile,link: generate PC-value tables with inlining information
In order to generate accurate tracebacks, the runtime needs to know the inlined call stack for a given PC. This creates two tables per function for this purpose. The first table is the inlining tree (stored in the function's funcdata), which has a node containing the file, line, and function name for every inlined call. The second table is a PC-value table that maps each PC to a node in the inlining tree (or -1 if the PC is not the result of inlining). To give the appearance that inlining hasn't happened, the runtime also needs the original source position information of inlined AST nodes. Previously the compiler plastered over the line numbers of inlined AST nodes with the line number of the call. This meant that the PC-line table mapped each PC to line number of the outermost call in its inlined call stack, with no way to access the innermost line number. Now the compiler retains line numbers of inlined AST nodes and writes the innermost source position information to the PC-line and PC-file tables. Some tools and tests expect to see outermost line numbers, so we provide the OutermostLine function for displaying line info. To keep track of the inlined call stack for an AST node, we extend the src.PosBase type with an index into a global inlining tree. Every time the compiler inlines a call, it creates a node in the global inlining tree for the call, and writes its index to the PosBase of every inlined AST node. The parent of this node is the inlining tree index of the call. -1 signifies no parent. For each function, the compiler creates a local inlining tree and a PC-value table mapping each PC to an index in the local tree. These are written to an object file, which is read by the linker. The linker re-encodes these tables compactly by deduplicating function names and file names. This change increases the size of binaries by 4-5%. For example, this is how the go1 benchmark binary is impacted by this change: section old bytes new bytes delta .text 3.49M ± 0% 3.49M ± 0% +0.06% .rodata 1.12M ± 0% 1.21M ± 0% +8.21% .gopclntab 1.50M ± 0% 1.68M ± 0% +11.89% .debug_line 338k ± 0% 435k ± 0% +28.78% Total 9.21M ± 0% 9.58M ± 0% +4.01% Updates #19348. Change-Id: Ic4f180c3b516018138236b0c35e0218270d957d3 Reviewed-on: https://go-review.googlesource.com/37231 Run-TryBot: David Lazar <lazard@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/cmd/internal/obj')
-rw-r--r--src/cmd/internal/obj/funcdata.go2
-rw-r--r--src/cmd/internal/obj/inl.go78
-rw-r--r--src/cmd/internal/obj/link.go3
-rw-r--r--src/cmd/internal/obj/objfile.go19
-rw-r--r--src/cmd/internal/obj/pcln.go60
-rw-r--r--src/cmd/internal/obj/util.go2
6 files changed, 163 insertions, 1 deletions
diff --git a/src/cmd/internal/obj/funcdata.go b/src/cmd/internal/obj/funcdata.go
index d5f891096b..af1dbc271d 100644
--- a/src/cmd/internal/obj/funcdata.go
+++ b/src/cmd/internal/obj/funcdata.go
@@ -12,8 +12,10 @@ package obj
const (
PCDATA_StackMapIndex = 0
+ PCDATA_InlTreeIndex = 1
FUNCDATA_ArgsPointerMaps = 0
FUNCDATA_LocalsPointerMaps = 1
+ FUNCDATA_InlTree = 2
// ArgsSizeUnknown is set in Func.argsize to mark all functions
// whose argument size is unknown (C vararg functions, and
diff --git a/src/cmd/internal/obj/inl.go b/src/cmd/internal/obj/inl.go
new file mode 100644
index 0000000000..f5e06959a2
--- /dev/null
+++ b/src/cmd/internal/obj/inl.go
@@ -0,0 +1,78 @@
+// Copyright 2017 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.
+
+package obj
+
+import "cmd/internal/src"
+
+// InlTree s a collection of inlined calls. The Parent field of an
+// InlinedCall is the index of another InlinedCall in InlTree.
+//
+// The compiler maintains a global inlining tree and adds a node to it
+// every time a function is inlined. For example, suppose f() calls g()
+// and g has two calls to h(), and that f, g, and h are inlineable:
+//
+// 1 func main() {
+// 2 f()
+// 3 }
+// 4 func f() {
+// 5 g()
+// 6 }
+// 7 func g() {
+// 8 h()
+// 9 h()
+// 10 }
+//
+// Assuming the global tree starts empty, inlining will produce the
+// following tree:
+//
+// []InlinedCall{
+// {Parent: -1, Func: "f", Pos: <line 2>},
+// {Parent: 0, Func: "g", Pos: <line 5>},
+// {Parent: 1, Func: "h", Pos: <line 8>},
+// {Parent: 1, Func: "h", Pos: <line 9>},
+// }
+//
+// The nodes of h inlined into main will have inlining indexes 2 and 3.
+//
+// Eventually, the compiler extracts a per-function inlining tree from
+// the global inlining tree (see pcln.go).
+type InlTree struct {
+ nodes []InlinedCall
+}
+
+// InlinedCall is a node in an InlTree.
+type InlinedCall struct {
+ Parent int // index of the parent in the InlTree or < 0 if outermost call
+ Pos src.XPos // position of the inlined call
+ Func *LSym // function that was inlined
+}
+
+// Add adds a new call to the tree, returning its index.
+func (tree *InlTree) Add(parent int, pos src.XPos, func_ *LSym) int {
+ r := len(tree.nodes)
+ call := InlinedCall{
+ Parent: parent,
+ Pos: pos,
+ Func: func_,
+ }
+ tree.nodes = append(tree.nodes, call)
+ return r
+}
+
+// OutermostPos returns the outermost position corresponding to xpos,
+// which is where xpos was ultimately inlined to. In the example for
+// InlTree, main() contains inlined AST nodes from h(), but the
+// outermost position for those nodes is line 2.
+func (ctxt *Link) OutermostPos(xpos src.XPos) src.Pos {
+ pos := ctxt.PosTable.Pos(xpos)
+
+ outerxpos := xpos
+ for ix := pos.Base().InliningIndex(); ix >= 0; {
+ call := ctxt.InlTree.nodes[ix]
+ ix = call.Parent
+ outerxpos = call.Pos
+ }
+ return ctxt.PosTable.Pos(outerxpos)
+}
diff --git a/src/cmd/internal/obj/link.go b/src/cmd/internal/obj/link.go
index 9dd0c14478..2b066cef34 100644
--- a/src/cmd/internal/obj/link.go
+++ b/src/cmd/internal/obj/link.go
@@ -397,12 +397,14 @@ type Pcln struct {
Pcsp Pcdata
Pcfile Pcdata
Pcline Pcdata
+ Pcinline Pcdata
Pcdata []Pcdata
Funcdata []*LSym
Funcdataoff []int64
File []*LSym
Lastfile *LSym
Lastindex int
+ InlTree InlTree // per-function inlining tree extracted from the global tree
}
// A SymKind describes the kind of memory represented by a symbol.
@@ -728,6 +730,7 @@ type Link struct {
Pathname string
Hash map[SymVer]*LSym
PosTable src.PosTable
+ InlTree InlTree // global inlining tree used by gc/inl.go
Imports []string
Sym_div *LSym
Sym_divu *LSym
diff --git a/src/cmd/internal/obj/objfile.go b/src/cmd/internal/obj/objfile.go
index be8b2b40c9..3be34d9fbe 100644
--- a/src/cmd/internal/obj/objfile.go
+++ b/src/cmd/internal/obj/objfile.go
@@ -94,6 +94,7 @@
// - pcsp [data block]
// - pcfile [data block]
// - pcline [data block]
+// - pcinline [data block]
// - npcdata [int]
// - pcdata [npcdata data blocks]
// - nfuncdata [int]
@@ -101,6 +102,8 @@
// - funcdatasym [nfuncdata ints]
// - nfile [int]
// - file [nfile symref index]
+// - ninlinedcall [int]
+// - inlinedcall [ninlinedcall int symref int symref]
//
// The file layout and meaning of type integers are architecture-independent.
//
@@ -156,6 +159,7 @@ func (w *objWriter) addLengths(s *LSym) {
data += len(pc.Pcsp.P)
data += len(pc.Pcfile.P)
data += len(pc.Pcline.P)
+ data += len(pc.Pcinline.P)
for i := 0; i < len(pc.Pcdata); i++ {
data += len(pc.Pcdata[i].P)
}
@@ -227,6 +231,7 @@ func WriteObjFile(ctxt *Link, b *bufio.Writer) {
w.wr.Write(pc.Pcsp.P)
w.wr.Write(pc.Pcfile.P)
w.wr.Write(pc.Pcline.P)
+ w.wr.Write(pc.Pcinline.P)
for i := 0; i < len(pc.Pcdata); i++ {
w.wr.Write(pc.Pcdata[i].P)
}
@@ -300,6 +305,11 @@ func (w *objWriter) writeRefs(s *LSym) {
for _, f := range pc.File {
w.writeRef(f, true)
}
+ for _, call := range pc.InlTree.nodes {
+ w.writeRef(call.Func, false)
+ f, _ := linkgetlineFromPos(w.ctxt, call.Pos)
+ w.writeRef(f, true)
+ }
}
}
@@ -452,6 +462,7 @@ func (w *objWriter) writeSym(s *LSym) {
w.writeInt(int64(len(pc.Pcsp.P)))
w.writeInt(int64(len(pc.Pcfile.P)))
w.writeInt(int64(len(pc.Pcline.P)))
+ w.writeInt(int64(len(pc.Pcinline.P)))
w.writeInt(int64(len(pc.Pcdata)))
for i := 0; i < len(pc.Pcdata); i++ {
w.writeInt(int64(len(pc.Pcdata[i].P)))
@@ -467,6 +478,14 @@ func (w *objWriter) writeSym(s *LSym) {
for _, f := range pc.File {
w.writeRefIndex(f)
}
+ w.writeInt(int64(len(pc.InlTree.nodes)))
+ for _, call := range pc.InlTree.nodes {
+ w.writeInt(int64(call.Parent))
+ f, l := linkgetlineFromPos(w.ctxt, call.Pos)
+ w.writeRefIndex(f)
+ w.writeInt(int64(l))
+ w.writeRefIndex(call.Func)
+ }
}
func (w *objWriter) writeInt(sval int64) {
diff --git a/src/cmd/internal/obj/pcln.go b/src/cmd/internal/obj/pcln.go
index 517550c72d..8db7802d0c 100644
--- a/src/cmd/internal/obj/pcln.go
+++ b/src/cmd/internal/obj/pcln.go
@@ -169,6 +169,62 @@ func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg
return int32(i)
}
+// pcinlineState holds the state used to create a function's inlining
+// tree and the PC-value table that maps PCs to nodes in that tree.
+type pcinlineState struct {
+ globalToLocal map[int]int
+ localTree InlTree
+}
+
+// addBranch adds a branch from the global inlining tree in ctxt to
+// the function's local inlining tree, returning the index in the local tree.
+func (s *pcinlineState) addBranch(ctxt *Link, globalIndex int) int {
+ if globalIndex < 0 {
+ return -1
+ }
+
+ localIndex, ok := s.globalToLocal[globalIndex]
+ if ok {
+ return localIndex
+ }
+
+ // Since tracebacks don't include column information, we could
+ // use one node for multiple calls of the same function on the
+ // same line (e.g., f(x) + f(y)). For now, we use one node for
+ // each inlined call.
+ call := ctxt.InlTree.nodes[globalIndex]
+ call.Parent = s.addBranch(ctxt, call.Parent)
+ localIndex = len(s.localTree.nodes)
+ s.localTree.nodes = append(s.localTree.nodes, call)
+ s.globalToLocal[globalIndex] = localIndex
+ return localIndex
+}
+
+// pctoinline computes the index into the local inlining tree to use at p.
+// If p is not the result of inlining, pctoinline returns -1. Because p.Pos
+// applies to p, phase == 0 (before p) takes care of the update.
+func (s *pcinlineState) pctoinline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
+ if phase == 1 {
+ return oldval
+ }
+
+ posBase := ctxt.PosTable.Pos(p.Pos).Base()
+ if posBase == nil {
+ return -1
+ }
+
+ globalIndex := posBase.InliningIndex()
+ if globalIndex < 0 {
+ return -1
+ }
+
+ if s.globalToLocal == nil {
+ s.globalToLocal = make(map[int]int)
+ }
+
+ return int32(s.addBranch(ctxt, globalIndex))
+}
+
// pctospadj computes the sp adjustment in effect.
// It is oldval plus any adjustment made by p itself.
// The adjustment by p takes effect only after p, so we
@@ -238,6 +294,10 @@ func linkpcln(ctxt *Link, cursym *LSym) {
funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln)
funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil)
+ pcinlineState := new(pcinlineState)
+ funcpctab(ctxt, &pcln.Pcinline, cursym, "pctoinline", pcinlineState.pctoinline, nil)
+ pcln.InlTree = pcinlineState.localTree
+
// tabulate which pc and func data we have.
havepc := make([]uint32, (npcdata+31)/32)
havefunc := make([]uint32, (nfuncdata+31)/32)
diff --git a/src/cmd/internal/obj/util.go b/src/cmd/internal/obj/util.go
index d0d050ab08..1e48f80616 100644
--- a/src/cmd/internal/obj/util.go
+++ b/src/cmd/internal/obj/util.go
@@ -59,7 +59,7 @@ func Getgoextlinkenabled() string {
}
func (p *Prog) Line() string {
- return p.Ctxt.PosTable.Pos(p.Pos).String()
+ return p.Ctxt.OutermostPos(p.Pos).String()
}
var armCondCode = []string{