Alessandro Arzilli 08029be9fc cmd/compile/internal/dwarfgen: refactor putvar and putAbstractVar
Currently, changing putvar or putAbstractVar involves:

1. changing the abbrevs array to add new abbrevs or modify existing ones
2. changing the DW_ABRV_XXX const block to add the new abbrevs, this const
   block must match the changes to the abbrevs array
3. change the code at the start of putvar and putAbstractVar that selects
   the abbrev to use
4. change the body of putvar/putAbstractVar to emit the right attributes in
   the right sequence

Each change must agree with all other, this is error prone and if an mistake
is made there is no compile time or runtime check detecting it. Erroneous
code will simply produce unreadable debug sections.

This commit adds a mechanism to automatically generate code for abbrev
selection as well as the abbrev definitions based on static examination of
the body of putvar and putAbstractVar.

TestPutVarAbbrevGenerator is responsible for checking that the generated
code is kept updated and will regenerated it by passing the '-generate'
option to it.

benchstat output:

                         |    old.txt    |               new.txt                |
                         |    sec/op     |    sec/op      vs base               |
Template                    153.8m ±  6%    153.0m ±  6%       ~ (p=0.853 n=10)
Unicode                     98.98m ± 19%   100.22m ± 15%       ~ (p=0.796 n=10)
GoTypes                    1013.7m ± 13%    943.6m ±  8%       ~ (p=0.353 n=10)
Compiler                    98.48m ± 10%    97.79m ±  6%       ~ (p=0.353 n=10)
SSA                          8.921 ± 31%     6.872 ± 37%       ~ (p=0.912 n=10)
Flate                       114.3m ± 21%    128.0m ± 36%       ~ (p=0.436 n=10)
GoParser                    219.0m ± 27%    214.9m ± 26%       ~ (p=0.631 n=10)
Reflect                     447.5m ± 20%    452.6m ± 22%       ~ (p=0.684 n=10)
Tar                         166.9m ± 27%    166.2m ± 27%       ~ (p=0.529 n=10)
XML                         218.6m ± 25%    219.3m ± 24%       ~ (p=0.631 n=10)
LinkCompiler                492.7m ± 12%    523.2m ± 13%       ~ (p=0.315 n=10)
ExternalLinkCompiler         1.684 ±  3%     1.684 ±  2%       ~ (p=0.684 n=10)
LinkWithoutDebugCompiler    296.0m ±  8%    304.9m ± 12%       ~ (p=0.579 n=10)
StdCmd                       69.59 ± 15%     70.76 ± 14%       ~ (p=0.436 n=10)
geomean                     516.0m          511.5m        -0.87%

                         |   old.txt    |               new.txt               |
                         | user-sec/op  | user-sec/op   vs base               |
Template                   281.5m ± 10%   269.6m ± 13%       ~ (p=0.315 n=10)
Unicode                    107.3m ±  8%   110.2m ±  8%       ~ (p=0.165 n=10)
GoTypes                     2.414 ± 16%    2.181 ±  9%       ~ (p=0.315 n=10)
Compiler                   116.0m ± 16%   119.1m ± 11%       ~ (p=0.971 n=10)
SSA                         25.47 ± 39%    17.75 ± 52%       ~ (p=0.739 n=10)
Flate                      205.2m ± 25%   256.2m ± 43%       ~ (p=0.393 n=10)
GoParser                   456.8m ± 28%   427.0m ± 24%       ~ (p=0.912 n=10)
Reflect                    960.3m ± 22%   990.5m ± 23%       ~ (p=0.280 n=10)
Tar                        299.8m ± 27%   307.9m ± 27%       ~ (p=0.631 n=10)
XML                        425.0m ± 21%   432.8m ± 24%       ~ (p=0.353 n=10)
LinkCompiler               768.1m ± 11%   796.9m ± 14%       ~ (p=0.631 n=10)
ExternalLinkCompiler        1.713 ±  5%    1.666 ±  4%       ~ (p=0.190 n=10)
LinkWithoutDebugCompiler   313.0m ±  9%   316.7m ± 12%       ~ (p=0.481 n=10)
geomean                    588.6m         579.5m        -1.55%

          |   old.txt    |                new.txt                |
          |  text-bytes  |  text-bytes   vs base                 |
HelloSize   842.9Ki ± 0%   842.9Ki ± 0%       ~ (p=1.000 n=10) ¹
CmdGoSize   10.95Mi ± 0%   10.95Mi ± 0%       ~ (p=1.000 n=10) ¹
geomean     3.003Mi        3.003Mi       +0.00%
¹ all samples are equal

          |   old.txt    |                new.txt                |
          |  data-bytes  |  data-bytes   vs base                 |
HelloSize   15.08Ki ± 0%   15.08Ki ± 0%       ~ (p=1.000 n=10) ¹
CmdGoSize   314.7Ki ± 0%   314.7Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean     68.88Ki        68.88Ki       +0.00%
¹ all samples are equal

          |   old.txt    |                new.txt                |
          |  bss-bytes   |  bss-bytes    vs base                 |
HelloSize   396.8Ki ± 0%   396.8Ki ± 0%       ~ (p=1.000 n=10) ¹
CmdGoSize   428.8Ki ± 0%   428.8Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean     412.5Ki        412.5Ki       +0.00%
¹ all samples are equal

          |   old.txt    |               new.txt               |
          |  exe-bytes   |  exe-bytes    vs base               |
HelloSize   1.310Mi ± 0%   1.310Mi ± 0%  -0.01% (p=0.000 n=10)
CmdGoSize   16.37Mi ± 0%   16.37Mi ± 0%  -0.00% (p=0.000 n=10)
geomean     4.631Mi        4.631Mi       -0.00%

Change-Id: I7edf37b5a47fd9aceef931ddf2c701e66a7b38b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/563815
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-26 20:45:07 +00:00

442 lines
13 KiB
Go

// 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 dwarfgen
import (
"fmt"
"strings"
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/internal/dwarf"
"cmd/internal/obj"
"cmd/internal/src"
)
// To identify variables by original source position.
type varPos struct {
DeclName string
DeclFile string
DeclLine uint
DeclCol uint
}
// This is the main entry point for collection of raw material to
// drive generation of DWARF "inlined subroutine" DIEs. See proposal
// 22080 for more details and background info.
func assembleInlines(fnsym *obj.LSym, dwVars []*dwarf.Var) dwarf.InlCalls {
var inlcalls dwarf.InlCalls
if base.Debug.DwarfInl != 0 {
base.Ctxt.Logf("assembling DWARF inlined routine info for %v\n", fnsym.Name)
}
// This maps inline index (from Ctxt.InlTree) to index in inlcalls.Calls
imap := make(map[int]int)
// Walk progs to build up the InlCalls data structure
var prevpos src.XPos
for p := fnsym.Func().Text; p != nil; p = p.Link {
if p.Pos == prevpos {
continue
}
ii := posInlIndex(p.Pos)
if ii >= 0 {
insertInlCall(&inlcalls, ii, imap)
}
prevpos = p.Pos
}
// This is used to partition DWARF vars by inline index. Vars not
// produced by the inliner will wind up in the vmap[0] entry.
vmap := make(map[int32][]*dwarf.Var)
// Now walk the dwarf vars and partition them based on whether they
// were produced by the inliner (dwv.InlIndex > 0) or were original
// vars/params from the function (dwv.InlIndex == 0).
for _, dwv := range dwVars {
vmap[dwv.InlIndex] = append(vmap[dwv.InlIndex], dwv)
// Zero index => var was not produced by an inline
if dwv.InlIndex == 0 {
continue
}
// Look up index in our map, then tack the var in question
// onto the vars list for the correct inlined call.
ii := int(dwv.InlIndex) - 1
idx, ok := imap[ii]
if !ok {
// We can occasionally encounter a var produced by the
// inliner for which there is no remaining prog; add a new
// entry to the call list in this scenario.
idx = insertInlCall(&inlcalls, ii, imap)
}
inlcalls.Calls[idx].InlVars =
append(inlcalls.Calls[idx].InlVars, dwv)
}
// Post process the map above to assign child indices to vars.
//
// A given variable is treated differently depending on whether it
// is part of the top-level function (ii == 0) or if it was
// produced as a result of an inline (ii != 0).
//
// If a variable was not produced by an inline and its containing
// function was not inlined, then we just assign an ordering of
// based on variable name.
//
// If a variable was not produced by an inline and its containing
// function was inlined, then we need to assign a child index
// based on the order of vars in the abstract function (in
// addition, those vars that don't appear in the abstract
// function, such as "~r1", are flagged as such).
//
// If a variable was produced by an inline, then we locate it in
// the pre-inlining decls for the target function and assign child
// index accordingly.
for ii, sl := range vmap {
var m map[varPos]int
if ii == 0 {
if !fnsym.WasInlined() {
for j, v := range sl {
v.ChildIndex = int32(j)
}
continue
}
m = makePreinlineDclMap(fnsym)
} else {
ifnlsym := base.Ctxt.InlTree.InlinedFunction(int(ii - 1))
m = makePreinlineDclMap(ifnlsym)
}
// Here we assign child indices to variables based on
// pre-inlined decls, and set the "IsInAbstract" flag
// appropriately. In addition: parameter and local variable
// names are given "middle dot" version numbers as part of the
// writing them out to export data (see issue 4326). If DWARF
// inlined routine generation is turned on, we want to undo
// this versioning, since DWARF variables in question will be
// parented by the inlined routine and not the top-level
// caller.
synthCount := len(m)
for _, v := range sl {
vp := varPos{
DeclName: v.Name,
DeclFile: v.DeclFile,
DeclLine: v.DeclLine,
DeclCol: v.DeclCol,
}
synthesized := strings.HasPrefix(v.Name, "~") || v.Name == "_"
if idx, found := m[vp]; found {
v.ChildIndex = int32(idx)
v.IsInAbstract = !synthesized
} else {
// Variable can't be found in the pre-inline dcl list.
// In the top-level case (ii=0) this can happen
// because a composite variable was split into pieces,
// and we're looking at a piece. We can also see
// return temps (~r%d) that were created during
// lowering, or unnamed params ("_").
v.ChildIndex = int32(synthCount)
synthCount++
}
}
}
// Make a second pass through the progs to compute PC ranges for
// the various inlined calls.
start := int64(-1)
curii := -1
var prevp *obj.Prog
for p := fnsym.Func().Text; p != nil; prevp, p = p, p.Link {
if prevp != nil && p.Pos == prevp.Pos {
continue
}
ii := posInlIndex(p.Pos)
if ii == curii {
continue
}
// Close out the current range
if start != -1 {
addRange(inlcalls.Calls, start, p.Pc, curii, imap)
}
// Begin new range
start = p.Pc
curii = ii
}
if start != -1 {
addRange(inlcalls.Calls, start, fnsym.Size, curii, imap)
}
// Issue 33188: if II foo is a child of II bar, then ensure that
// bar's ranges include the ranges of foo (the loop above will produce
// disjoint ranges).
for k, c := range inlcalls.Calls {
if c.Root {
unifyCallRanges(inlcalls, k)
}
}
// Debugging
if base.Debug.DwarfInl != 0 {
dumpInlCalls(inlcalls)
dumpInlVars(dwVars)
}
// Perform a consistency check on inlined routine PC ranges
// produced by unifyCallRanges above. In particular, complain in
// cases where you have A -> B -> C (e.g. C is inlined into B, and
// B is inlined into A) and the ranges for B are not enclosed
// within the ranges for A, or C within B.
for k, c := range inlcalls.Calls {
if c.Root {
checkInlCall(fnsym.Name, inlcalls, fnsym.Size, k, -1)
}
}
return inlcalls
}
// Secondary hook for DWARF inlined subroutine generation. This is called
// late in the compilation when it is determined that we need an
// abstract function DIE for an inlined routine imported from a
// previously compiled package.
func AbstractFunc(fn *obj.LSym) {
ifn := base.Ctxt.DwFixups.GetPrecursorFunc(fn)
if ifn == nil {
base.Ctxt.Diag("failed to locate precursor fn for %v", fn)
return
}
_ = ifn.(*ir.Func)
if base.Debug.DwarfInl != 0 {
base.Ctxt.Logf("DwarfAbstractFunc(%v)\n", fn.Name)
}
base.Ctxt.DwarfAbstractFunc(ifn, fn)
}
// Given a function that was inlined as part of the compilation, dig
// up the pre-inlining DCL list for the function and create a map that
// supports lookup of pre-inline dcl index, based on variable
// position/name. NB: the recipe for computing variable pos/file/line
// needs to be kept in sync with the similar code in gc.createSimpleVars
// and related functions.
func makePreinlineDclMap(fnsym *obj.LSym) map[varPos]int {
dcl := preInliningDcls(fnsym)
m := make(map[varPos]int)
for i, n := range dcl {
pos := base.Ctxt.InnermostPos(n.Pos())
vp := varPos{
DeclName: n.Sym().Name,
DeclFile: pos.RelFilename(),
DeclLine: pos.RelLine(),
DeclCol: pos.RelCol(),
}
if _, found := m[vp]; found {
// We can see collisions (variables with the same name/file/line/col) in obfuscated or machine-generated code -- see issue 44378 for an example. Skip duplicates in such cases, since it is unlikely that a human will be debugging such code.
continue
}
m[vp] = i
}
return m
}
func insertInlCall(dwcalls *dwarf.InlCalls, inlIdx int, imap map[int]int) int {
callIdx, found := imap[inlIdx]
if found {
return callIdx
}
// Haven't seen this inline yet. Visit parent of inline if there
// is one. We do this first so that parents appear before their
// children in the resulting table.
parCallIdx := -1
parInlIdx := base.Ctxt.InlTree.Parent(inlIdx)
if parInlIdx >= 0 {
parCallIdx = insertInlCall(dwcalls, parInlIdx, imap)
}
// Create new entry for this inline
inlinedFn := base.Ctxt.InlTree.InlinedFunction(inlIdx)
callXPos := base.Ctxt.InlTree.CallPos(inlIdx)
callPos := base.Ctxt.InnermostPos(callXPos)
absFnSym := base.Ctxt.DwFixups.AbsFuncDwarfSym(inlinedFn)
ic := dwarf.InlCall{
InlIndex: inlIdx,
CallPos: callPos,
AbsFunSym: absFnSym,
Root: parCallIdx == -1,
}
dwcalls.Calls = append(dwcalls.Calls, ic)
callIdx = len(dwcalls.Calls) - 1
imap[inlIdx] = callIdx
if parCallIdx != -1 {
// Add this inline to parent's child list
dwcalls.Calls[parCallIdx].Children = append(dwcalls.Calls[parCallIdx].Children, callIdx)
}
return callIdx
}
// Given a src.XPos, return its associated inlining index if it
// corresponds to something created as a result of an inline, or -1 if
// there is no inline info. Note that the index returned will refer to
// the deepest call in the inlined stack, e.g. if you have "A calls B
// calls C calls D" and all three callees are inlined (B, C, and D),
// the index for a node from the inlined body of D will refer to the
// call to D from C. Whew.
func posInlIndex(xpos src.XPos) int {
pos := base.Ctxt.PosTable.Pos(xpos)
if b := pos.Base(); b != nil {
ii := b.InliningIndex()
if ii >= 0 {
return ii
}
}
return -1
}
func addRange(calls []dwarf.InlCall, start, end int64, ii int, imap map[int]int) {
if start == -1 {
panic("bad range start")
}
if end == -1 {
panic("bad range end")
}
if ii == -1 {
return
}
if start == end {
return
}
// Append range to correct inlined call
callIdx, found := imap[ii]
if !found {
base.Fatalf("can't find inlIndex %d in imap for prog at %d\n", ii, start)
}
call := &calls[callIdx]
call.Ranges = append(call.Ranges, dwarf.Range{Start: start, End: end})
}
func dumpInlCall(inlcalls dwarf.InlCalls, idx, ilevel int) {
for i := 0; i < ilevel; i++ {
base.Ctxt.Logf(" ")
}
ic := inlcalls.Calls[idx]
callee := base.Ctxt.InlTree.InlinedFunction(ic.InlIndex)
base.Ctxt.Logf(" %d: II:%d (%s) V: (", idx, ic.InlIndex, callee.Name)
for _, f := range ic.InlVars {
base.Ctxt.Logf(" %v", f.Name)
}
base.Ctxt.Logf(" ) C: (")
for _, k := range ic.Children {
base.Ctxt.Logf(" %v", k)
}
base.Ctxt.Logf(" ) R:")
for _, r := range ic.Ranges {
base.Ctxt.Logf(" [%d,%d)", r.Start, r.End)
}
base.Ctxt.Logf("\n")
for _, k := range ic.Children {
dumpInlCall(inlcalls, k, ilevel+1)
}
}
func dumpInlCalls(inlcalls dwarf.InlCalls) {
for k, c := range inlcalls.Calls {
if c.Root {
dumpInlCall(inlcalls, k, 0)
}
}
}
func dumpInlVars(dwvars []*dwarf.Var) {
for i, dwv := range dwvars {
typ := "local"
if dwv.Tag == dwarf.DW_TAG_formal_parameter {
typ = "param"
}
ia := 0
if dwv.IsInAbstract {
ia = 1
}
base.Ctxt.Logf("V%d: %s CI:%d II:%d IA:%d %s\n", i, dwv.Name, dwv.ChildIndex, dwv.InlIndex-1, ia, typ)
}
}
func rangesContains(par []dwarf.Range, rng dwarf.Range) (bool, string) {
for _, r := range par {
if rng.Start >= r.Start && rng.End <= r.End {
return true, ""
}
}
msg := fmt.Sprintf("range [%d,%d) not contained in {", rng.Start, rng.End)
for _, r := range par {
msg += fmt.Sprintf(" [%d,%d)", r.Start, r.End)
}
msg += " }"
return false, msg
}
func rangesContainsAll(parent, child []dwarf.Range) (bool, string) {
for _, r := range child {
c, m := rangesContains(parent, r)
if !c {
return false, m
}
}
return true, ""
}
// checkInlCall verifies that the PC ranges for inline info 'idx' are
// enclosed/contained within the ranges of its parent inline (or if
// this is a root/toplevel inline, checks that the ranges fall within
// the extent of the top level function). A panic is issued if a
// malformed range is found.
func checkInlCall(funcName string, inlCalls dwarf.InlCalls, funcSize int64, idx, parentIdx int) {
// Callee
ic := inlCalls.Calls[idx]
callee := base.Ctxt.InlTree.InlinedFunction(ic.InlIndex).Name
calleeRanges := ic.Ranges
// Caller
caller := funcName
parentRanges := []dwarf.Range{dwarf.Range{Start: int64(0), End: funcSize}}
if parentIdx != -1 {
pic := inlCalls.Calls[parentIdx]
caller = base.Ctxt.InlTree.InlinedFunction(pic.InlIndex).Name
parentRanges = pic.Ranges
}
// Callee ranges contained in caller ranges?
c, m := rangesContainsAll(parentRanges, calleeRanges)
if !c {
base.Fatalf("** malformed inlined routine range in %s: caller %s callee %s II=%d %s\n", funcName, caller, callee, idx, m)
}
// Now visit kids
for _, k := range ic.Children {
checkInlCall(funcName, inlCalls, funcSize, k, idx)
}
}
// unifyCallRanges ensures that the ranges for a given inline
// transitively include all of the ranges for its child inlines.
func unifyCallRanges(inlcalls dwarf.InlCalls, idx int) {
ic := &inlcalls.Calls[idx]
for _, childIdx := range ic.Children {
// First make sure child ranges are unified.
unifyCallRanges(inlcalls, childIdx)
// Then merge child ranges into ranges for this inline.
cic := inlcalls.Calls[childIdx]
ic.Ranges = dwarf.MergeRanges(ic.Ranges, cic.Ranges)
}
}