cmd/compile/loopvar: adjust logging messages

Michael Stapelberg thought the former messages had
upside potential, Russ and I agreed.

Also slightly tweaked the json logging, not sure if
anyone will use it but it should at least be okay.

Change-Id: Iaab75114dd5f5d8f011fab22d32b57abc0272815
Reviewed-on: https://go-review.googlesource.com/c/go/+/493135
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: David Chase <drchase@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
This commit is contained in:
David Chase 2023-05-05 16:29:37 -04:00 committed by Gopher Robot
parent 8d5065ce6e
commit 59d19ba797
2 changed files with 20 additions and 14 deletions

View file

@ -545,29 +545,35 @@ func LogTransformations(transformed []VarAndLoop) {
loops = append(loops, loopPos{l, lv.LastPos, n.Curfn}) loops = append(loops, loopPos{l, lv.LastPos, n.Curfn})
} }
pos := n.Pos() pos := n.Pos()
inner := base.Ctxt.InnermostPos(pos)
outer := base.Ctxt.OutermostPos(pos)
if logopt.Enabled() { if logopt.Enabled() {
// For automated checking of coverage of this transformation, include this in the JSON information. // For automated checking of coverage of this transformation, include this in the JSON information.
var nString interface{} = n
if inner != outer {
nString = fmt.Sprintf("%v (from inline)", n)
}
if n.Esc() == ir.EscHeap { if n.Esc() == ir.EscHeap {
logopt.LogOpt(pos, "transform-escape", "loopvar", ir.FuncName(n.Curfn)) logopt.LogOpt(pos, "iteration-variable-to-heap", "loopvar", ir.FuncName(n.Curfn), nString)
} else { } else {
logopt.LogOpt(pos, "transform-noescape", "loopvar", ir.FuncName(n.Curfn)) logopt.LogOpt(pos, "iteration-variable-to-stack", "loopvar", ir.FuncName(n.Curfn), nString)
} }
} }
if print { if print {
inner := base.Ctxt.InnermostPos(pos)
outer := base.Ctxt.OutermostPos(pos)
if inner == outer { if inner == outer {
if n.Esc() == ir.EscHeap { if n.Esc() == ir.EscHeap {
base.WarnfAt(pos, "transformed loop variable %v escapes", n) base.WarnfAt(pos, "loop variable %v now per-iteration, heap-allocated", n)
} else { } else {
base.WarnfAt(pos, "transformed loop variable %v does not escape", n) base.WarnfAt(pos, "loop variable %v now per-iteration, stack-allocated", n)
} }
} else { } else {
innerXPos := trueInlinedPos(inner) innerXPos := trueInlinedPos(inner)
if n.Esc() == ir.EscHeap { if n.Esc() == ir.EscHeap {
base.WarnfAt(innerXPos, "transformed loop variable %v escapes (loop inlined into %s:%d)", n, outer.Filename(), outer.Line()) base.WarnfAt(innerXPos, "loop variable %v now per-iteration, heap-allocated (loop inlined into %s:%d)", n, outer.Filename(), outer.Line())
} else { } else {
base.WarnfAt(innerXPos, "transformed loop variable %v does not escape (loop inlined into %s:%d)", n, outer.Filename(), outer.Line()) base.WarnfAt(innerXPos, "loop variable %v now per-iteration, stack-allocated (loop inlined into %s:%d)", n, outer.Filename(), outer.Line())
} }
} }
} }
@ -577,18 +583,18 @@ func LogTransformations(transformed []VarAndLoop) {
last := l.last last := l.last
if logopt.Enabled() { if logopt.Enabled() {
// Intended to // Intended to
logopt.LogOptRange(pos, last, "transform-loop", "loopvar", ir.FuncName(l.curfn)) logopt.LogOptRange(pos, last, "loop-modified", "loopvar", ir.FuncName(l.curfn))
} }
if print && 3 <= base.Debug.LoopVar { if print && 3 <= base.Debug.LoopVar {
// TODO decide if we want to keep this, or not. It was helpful for validating logopt, otherwise, eh. // TODO decide if we want to keep this, or not. It was helpful for validating logopt, otherwise, eh.
inner := base.Ctxt.InnermostPos(pos) inner := base.Ctxt.InnermostPos(pos)
outer := base.Ctxt.OutermostPos(pos) outer := base.Ctxt.OutermostPos(pos)
if inner == outer { if inner == outer {
base.WarnfAt(pos, "loop ending at %d:%d was transformed", last.Line(), last.Col()) base.WarnfAt(pos, "loop ending at %d:%d was modified", last.Line(), last.Col())
} else { } else {
pos = trueInlinedPos(inner) pos = trueInlinedPos(inner)
last = trueInlinedPos(base.Ctxt.InnermostPos(last)) last = trueInlinedPos(base.Ctxt.InnermostPos(last))
base.WarnfAt(pos, "loop ending at %d:%d was transformed (loop inlined into %s:%d)", last.Line(), last.Col(), outer.Filename(), outer.Line()) base.WarnfAt(pos, "loop ending at %d:%d was modified (loop inlined into %s:%d)", last.Line(), last.Col(), outer.Filename(), outer.Line())
} }
} }
} }

View file

@ -39,12 +39,12 @@ var cases = []testcase{
{"-1", "", 11, for_files[:1]}, {"-1", "", 11, for_files[:1]},
{"0", "", 0, for_files[:1]}, {"0", "", 0, for_files[:1]},
{"1", "", 0, for_files[:1]}, {"1", "", 0, for_files[:1]},
{"2", "transformed loop variable i ", 0, for_files}, {"2", "loop variable i now per-iteration,", 0, for_files},
{"-1", "", 11, range_files[:1]}, {"-1", "", 11, range_files[:1]},
{"0", "", 0, range_files[:1]}, {"0", "", 0, range_files[:1]},
{"1", "", 0, range_files[:1]}, {"1", "", 0, range_files[:1]},
{"2", "transformed loop variable i ", 0, range_files}, {"2", "loop variable i now per-iteration,", 0, range_files},
{"1", "", 0, []string{"for_nested.go"}}, {"1", "", 0, []string{"for_nested.go"}},
} }
@ -230,7 +230,7 @@ func TestLoopVarOpt(t *testing.T) {
t.Logf(m) t.Logf(m)
yCount := strings.Count(m, "opt.go:16:6: transformed loop variable private escapes (loop inlined into ./opt.go:30)") yCount := strings.Count(m, "opt.go:16:6: loop variable private now per-iteration, heap-allocated (loop inlined into ./opt.go:30)")
nCount := strings.Count(m, "shared") nCount := strings.Count(m, "shared")
if yCount != 1 { if yCount != 1 {