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>
This commit is contained in:
Alessandro Arzilli 2024-02-08 12:06:00 +01:00 committed by Gopher Robot
parent 5bce5362da
commit 08029be9fc
5 changed files with 508 additions and 223 deletions

View file

@ -218,10 +218,10 @@ func createDwarfVars(fnsym *obj.LSym, complexOK bool, fn *ir.Func, apDecls []*ir
}
typename := dwarf.InfoPrefix + types.TypeSymName(n.Type())
decls = append(decls, n)
abbrev := dwarf.DW_ABRV_AUTO_LOCLIST
tag := dwarf.DW_TAG_variable
isReturnValue := (n.Class == ir.PPARAMOUT)
if n.Class == ir.PPARAM || n.Class == ir.PPARAMOUT {
abbrev = dwarf.DW_ABRV_PARAM_LOCLIST
tag = dwarf.DW_TAG_formal_parameter
}
if n.Esc() == ir.EscHeap {
// The variable in question has been promoted to the heap.
@ -233,7 +233,7 @@ func createDwarfVars(fnsym *obj.LSym, complexOK bool, fn *ir.Func, apDecls []*ir
if n.InlFormal() || n.InlLocal() {
inlIndex = posInlIndex(n.Pos()) + 1
if n.InlFormal() {
abbrev = dwarf.DW_ABRV_PARAM_LOCLIST
tag = dwarf.DW_TAG_formal_parameter
}
}
}
@ -241,7 +241,8 @@ func createDwarfVars(fnsym *obj.LSym, complexOK bool, fn *ir.Func, apDecls []*ir
vars = append(vars, &dwarf.Var{
Name: n.Sym().Name,
IsReturnValue: isReturnValue,
Abbrev: abbrev,
Tag: tag,
WithLoclist: true,
StackOffset: int32(n.FrameOffset()),
Type: base.Ctxt.Lookup(typename),
DeclFile: declpos.RelFilename(),
@ -350,7 +351,7 @@ func createSimpleVars(fnsym *obj.LSym, apDecls []*ir.Name) ([]*ir.Name, []*dwarf
}
func createSimpleVar(fnsym *obj.LSym, n *ir.Name) *dwarf.Var {
var abbrev int
var tag int
var offs int64
localAutoOffset := func() int64 {
@ -367,9 +368,9 @@ func createSimpleVar(fnsym *obj.LSym, n *ir.Name) *dwarf.Var {
switch n.Class {
case ir.PAUTO:
offs = localAutoOffset()
abbrev = dwarf.DW_ABRV_AUTO
tag = dwarf.DW_TAG_variable
case ir.PPARAM, ir.PPARAMOUT:
abbrev = dwarf.DW_ABRV_PARAM
tag = dwarf.DW_TAG_formal_parameter
if n.IsOutputParamInRegisters() {
offs = localAutoOffset()
} else {
@ -387,7 +388,7 @@ func createSimpleVar(fnsym *obj.LSym, n *ir.Name) *dwarf.Var {
if n.InlFormal() || n.InlLocal() {
inlIndex = posInlIndex(n.Pos()) + 1
if n.InlFormal() {
abbrev = dwarf.DW_ABRV_PARAM
tag = dwarf.DW_TAG_formal_parameter
}
}
}
@ -396,7 +397,7 @@ func createSimpleVar(fnsym *obj.LSym, n *ir.Name) *dwarf.Var {
Name: n.Sym().Name,
IsReturnValue: n.Class == ir.PPARAMOUT,
IsInlFormal: n.InlFormal(),
Abbrev: abbrev,
Tag: tag,
StackOffset: int32(offs),
Type: base.Ctxt.Lookup(typename),
DeclFile: declpos.RelFilename(),
@ -470,12 +471,12 @@ func createComplexVar(fnsym *obj.LSym, fn *ir.Func, varID ssa.VarID) *dwarf.Var
debug := fn.DebugInfo.(*ssa.FuncDebug)
n := debug.Vars[varID]
var abbrev int
var tag int
switch n.Class {
case ir.PAUTO:
abbrev = dwarf.DW_ABRV_AUTO_LOCLIST
tag = dwarf.DW_TAG_variable
case ir.PPARAM, ir.PPARAMOUT:
abbrev = dwarf.DW_ABRV_PARAM_LOCLIST
tag = dwarf.DW_TAG_formal_parameter
default:
return nil
}
@ -488,7 +489,7 @@ func createComplexVar(fnsym *obj.LSym, fn *ir.Func, varID ssa.VarID) *dwarf.Var
if n.InlFormal() || n.InlLocal() {
inlIndex = posInlIndex(n.Pos()) + 1
if n.InlFormal() {
abbrev = dwarf.DW_ABRV_PARAM_LOCLIST
tag = dwarf.DW_TAG_formal_parameter
}
}
}
@ -497,7 +498,8 @@ func createComplexVar(fnsym *obj.LSym, fn *ir.Func, varID ssa.VarID) *dwarf.Var
Name: n.Sym().Name,
IsReturnValue: n.Class == ir.PPARAMOUT,
IsInlFormal: n.InlFormal(),
Abbrev: abbrev,
Tag: tag,
WithLoclist: true,
Type: base.Ctxt.Lookup(typename),
// The stack offset is used as a sorting key, so for decomposed
// variables just give it the first one. It's not used otherwise.