cmd/compile: fix bloop get name logic

This CL change getNameFrom impl to pattern match addressible patterns.

Change-Id: If1faa22a3a012d501e911d8468a5702b348abf16
Reviewed-on: https://go-review.googlesource.com/c/go/+/724180
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Junyang Shao 2025-11-25 01:37:13 +00:00
parent 3353c100bb
commit 3fd9cb1895
2 changed files with 81 additions and 52 deletions

View file

@ -42,40 +42,42 @@ import (
"cmd/compile/internal/reflectdata" "cmd/compile/internal/reflectdata"
"cmd/compile/internal/typecheck" "cmd/compile/internal/typecheck"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"fmt" "cmd/internal/src"
) )
// getNameFromNode tries to iteratively peel down the node to // getNameFromNode tries to iteratively peel down the node to
// get the name. // get the name.
func getNameFromNode(n ir.Node) *ir.Name { func getNameFromNode(n ir.Node) *ir.Name {
var ret *ir.Name // Tries to iteratively peel down the node to get the names.
if n.Op() == ir.ONAME { for n != nil {
ret = n.(*ir.Name) switch n.Op() {
} else { case ir.ONAME:
// avoid infinite recursion on circular referencing nodes. // Found the name, stop the loop.
seen := map[ir.Node]bool{n: true} return n.(*ir.Name)
var findName func(ir.Node) bool case ir.OSLICE, ir.OSLICE3:
findName = func(a ir.Node) bool { n = n.(*ir.SliceExpr).X
if a.Op() == ir.ONAME { case ir.ODOT:
ret = a.(*ir.Name) n = n.(*ir.SelectorExpr).X
return true case ir.OCONV, ir.OCONVIFACE, ir.OCONVNOP:
} n = n.(*ir.ConvExpr).X
if !seen[a] { case ir.OADDR:
seen[a] = true n = n.(*ir.AddrExpr).X
return ir.DoChildren(a, findName) case ir.ODOTPTR:
} n = n.(*ir.SelectorExpr).X
return false case ir.OINDEX, ir.OINDEXMAP:
n = n.(*ir.IndexExpr).X
default:
n = nil
} }
ir.DoChildren(n, findName)
} }
return ret return nil
} }
// keepAliveAt returns a statement that is either curNode, or a // keepAliveAt returns a statement that is either curNode, or a
// block containing curNode followed by a call to runtime.keepAlive for each // block containing curNode followed by a call to runtime.keepAlive for each
// ONAME in ns. These calls ensure that names in ns will be live until // node in ns. These calls ensure that nodes in ns will be live until
// after curNode's execution. // after curNode's execution.
func keepAliveAt(ns []*ir.Name, curNode ir.Node) ir.Node { func keepAliveAt(ns []ir.Node, curNode ir.Node) ir.Node {
if len(ns) == 0 { if len(ns) == 0 {
return curNode return curNode
} }
@ -109,12 +111,12 @@ func keepAliveAt(ns []*ir.Name, curNode ir.Node) ir.Node {
return ir.NewBlockStmt(pos, calls) return ir.NewBlockStmt(pos, calls)
} }
func debugName(name *ir.Name, line string) { func debugName(name *ir.Name, pos src.XPos) {
if base.Flag.LowerM > 0 { if base.Flag.LowerM > 1 {
if name.Linksym() != nil { if name.Linksym() != nil {
fmt.Printf("%v: %s will be kept alive\n", line, name.Linksym().Name) base.WarnfAt(pos, "%s will be kept alive", name.Linksym().Name)
} else { } else {
fmt.Printf("%v: expr will be kept alive\n", line) base.WarnfAt(pos, "expr will be kept alive")
} }
} }
} }
@ -129,29 +131,50 @@ func preserveStmt(curFn *ir.Func, stmt ir.Node) (ret ir.Node) {
// Peel down struct and slice indexing to get the names // Peel down struct and slice indexing to get the names
name := getNameFromNode(n.X) name := getNameFromNode(n.X)
if name != nil { if name != nil {
debugName(name, ir.Line(stmt)) debugName(name, n.Pos())
ret = keepAliveAt([]*ir.Name{name}, n) ret = keepAliveAt([]ir.Node{name}, n)
} else if deref := n.X.(*ir.StarExpr); deref != nil {
ret = keepAliveAt([]ir.Node{deref}, n)
if base.Flag.LowerM > 1 {
base.WarnfAt(n.Pos(), "dereference will be kept alive")
}
} else if base.Flag.LowerM > 1 {
base.WarnfAt(n.Pos(), "expr is unknown to bloop pass")
} }
case *ir.AssignListStmt: case *ir.AssignListStmt:
names := []*ir.Name{} ns := []ir.Node{}
for _, lhs := range n.Lhs { for _, lhs := range n.Lhs {
name := getNameFromNode(lhs) name := getNameFromNode(lhs)
if name != nil { if name != nil {
debugName(name, ir.Line(stmt)) debugName(name, n.Pos())
names = append(names, name) ns = append(ns, name)
} else if deref := lhs.(*ir.StarExpr); deref != nil {
ns = append(ns, deref)
if base.Flag.LowerM > 1 {
base.WarnfAt(n.Pos(), "dereference will be kept alive")
}
} else if base.Flag.LowerM > 1 {
base.WarnfAt(n.Pos(), "expr is unknown to bloop pass")
} }
} }
ret = keepAliveAt(names, n) ret = keepAliveAt(ns, n)
case *ir.AssignOpStmt: case *ir.AssignOpStmt:
name := getNameFromNode(n.X) name := getNameFromNode(n.X)
if name != nil { if name != nil {
debugName(name, ir.Line(stmt)) debugName(name, n.Pos())
ret = keepAliveAt([]*ir.Name{name}, n) ret = keepAliveAt([]ir.Node{name}, n)
} else if deref := n.X.(*ir.StarExpr); deref != nil {
ret = keepAliveAt([]ir.Node{deref}, n)
if base.Flag.LowerM > 1 {
base.WarnfAt(n.Pos(), "dereference will be kept alive")
}
} else if base.Flag.LowerM > 1 {
base.WarnfAt(n.Pos(), "expr is unknown to bloop pass")
} }
case *ir.CallExpr: case *ir.CallExpr:
names := []*ir.Name{}
curNode := stmt curNode := stmt
if n.Fun != nil && n.Fun.Type() != nil && n.Fun.Type().NumResults() != 0 { if n.Fun != nil && n.Fun.Type() != nil && n.Fun.Type().NumResults() != 0 {
ns := []ir.Node{}
// This function's results are not assigned, assign them to // This function's results are not assigned, assign them to
// auto tmps and then keepAliveAt these autos. // auto tmps and then keepAliveAt these autos.
// Note: markStmt assumes the context that it's called - this CallExpr is // Note: markStmt assumes the context that it's called - this CallExpr is
@ -161,7 +184,7 @@ func preserveStmt(curFn *ir.Func, stmt ir.Node) (ret ir.Node) {
for i, res := range results { for i, res := range results {
tmp := typecheck.TempAt(n.Pos(), curFn, res.Type) tmp := typecheck.TempAt(n.Pos(), curFn, res.Type)
lhs[i] = tmp lhs[i] = tmp
names = append(names, tmp) ns = append(ns, tmp)
} }
// Create an assignment statement. // Create an assignment statement.
@ -174,33 +197,35 @@ func preserveStmt(curFn *ir.Func, stmt ir.Node) (ret ir.Node) {
if len(results) > 1 { if len(results) > 1 {
plural = "s" plural = "s"
} }
if base.Flag.LowerM > 0 { if base.Flag.LowerM > 1 {
fmt.Printf("%v: function result%s will be kept alive\n", ir.Line(stmt), plural) base.WarnfAt(n.Pos(), "function result%s will be kept alive", plural)
} }
ret = keepAliveAt(ns, curNode)
} else { } else {
// This function probably doesn't return anything, keep its args alive. // This function probably doesn't return anything, keep its args alive.
argTmps := []ir.Node{} argTmps := []ir.Node{}
names := []ir.Node{}
for i, a := range n.Args { for i, a := range n.Args {
if name := getNameFromNode(a); name != nil { if name := getNameFromNode(a); name != nil {
// If they are name, keep them alive directly. // If they are name, keep them alive directly.
debugName(name, ir.Line(stmt)) debugName(name, n.Pos())
names = append(names, name) names = append(names, name)
} else if a.Op() == ir.OSLICELIT { } else if a.Op() == ir.OSLICELIT {
// variadic args are encoded as slice literal. // variadic args are encoded as slice literal.
s := a.(*ir.CompLitExpr) s := a.(*ir.CompLitExpr)
ns := []*ir.Name{} ns := []ir.Node{}
for i, n := range s.List { for i, elem := range s.List {
if name := getNameFromNode(n); name != nil { if name := getNameFromNode(elem); name != nil {
debugName(name, ir.Line(a)) debugName(name, n.Pos())
ns = append(ns, name) ns = append(ns, name)
} else { } else {
// We need a temporary to save this arg. // We need a temporary to save this arg.
tmp := typecheck.TempAt(n.Pos(), curFn, n.Type()) tmp := typecheck.TempAt(elem.Pos(), curFn, elem.Type())
argTmps = append(argTmps, typecheck.AssignExpr(ir.NewAssignStmt(n.Pos(), tmp, n))) argTmps = append(argTmps, typecheck.AssignExpr(ir.NewAssignStmt(elem.Pos(), tmp, elem)))
names = append(names, tmp) names = append(names, tmp)
s.List[i] = tmp s.List[i] = tmp
if base.Flag.LowerM > 0 { if base.Flag.LowerM > 1 {
fmt.Printf("%v: function arg will be kept alive\n", ir.Line(n)) base.WarnfAt(n.Pos(), "function arg will be kept alive")
} }
} }
} }
@ -212,8 +237,8 @@ func preserveStmt(curFn *ir.Func, stmt ir.Node) (ret ir.Node) {
argTmps = append(argTmps, typecheck.AssignExpr(ir.NewAssignStmt(n.Pos(), tmp, a))) argTmps = append(argTmps, typecheck.AssignExpr(ir.NewAssignStmt(n.Pos(), tmp, a)))
names = append(names, tmp) names = append(names, tmp)
n.Args[i] = tmp n.Args[i] = tmp
if base.Flag.LowerM > 0 { if base.Flag.LowerM > 1 {
fmt.Printf("%v: function arg will be kept alive\n", ir.Line(stmt)) base.WarnfAt(n.Pos(), "function arg will be kept alive")
} }
} }
} }
@ -221,8 +246,8 @@ func preserveStmt(curFn *ir.Func, stmt ir.Node) (ret ir.Node) {
argTmps = append(argTmps, n) argTmps = append(argTmps, n)
curNode = ir.NewBlockStmt(n.Pos(), argTmps) curNode = ir.NewBlockStmt(n.Pos(), argTmps)
} }
ret = keepAliveAt(names, curNode)
} }
ret = keepAliveAt(names, curNode)
} }
return return
} }
@ -282,6 +307,8 @@ func (e editor) edit(n ir.Node) ir.Node {
preserveStmts(e.curFn, n.Body) preserveStmts(e.curFn, n.Body)
case *ir.CommClause: case *ir.CommClause:
preserveStmts(e.curFn, n.Body) preserveStmts(e.curFn, n.Body)
case *ir.RangeStmt:
preserveStmts(e.curFn, n.Body)
} }
} }
return n return n

View file

@ -1,4 +1,4 @@
// errorcheck -0 -m // errorcheck -0 -m=2
// Copyright 2025 The Go Authors. All rights reserved. // Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
@ -25,10 +25,11 @@ func caninlineVariadic(x ...int) { // ERROR "can inline caninlineVariadic" "x do
something = x[0] something = x[0]
} }
func test(b *testing.B, localsink, cond int) { // ERROR "leaking param: b" func test(b *testing.B, localsink, cond int) { // ERROR ".*"
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
caninline(1) // ERROR "inlining call to caninline" caninline(1) // ERROR "inlining call to caninline"
} }
somethingptr := &something
for b.Loop() { // ERROR "inlining call to testing\.\(\*B\)\.Loop" for b.Loop() { // ERROR "inlining call to testing\.\(\*B\)\.Loop"
caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape" caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape"
caninlineNoRet(1) // ERROR "inlining call to caninlineNoRet" "function arg will be kept alive" ".* does not escape" caninlineNoRet(1) // ERROR "inlining call to caninlineNoRet" "function arg will be kept alive" ".* does not escape"
@ -37,6 +38,7 @@ func test(b *testing.B, localsink, cond int) { // ERROR "leaking param: b"
localsink = caninline(1) // ERROR "inlining call to caninline" "localsink will be kept alive" ".* does not escape" localsink = caninline(1) // ERROR "inlining call to caninline" "localsink will be kept alive" ".* does not escape"
localsink += 5 // ERROR "localsink will be kept alive" ".* does not escape" localsink += 5 // ERROR "localsink will be kept alive" ".* does not escape"
localsink, cond = 1, 2 // ERROR "localsink will be kept alive" "cond will be kept alive" ".* does not escape" localsink, cond = 1, 2 // ERROR "localsink will be kept alive" "cond will be kept alive" ".* does not escape"
*somethingptr = 1 // ERROR "dereference will be kept alive"
if cond > 0 { if cond > 0 {
caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape" caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape"
} }