1
0
mirror of https://github.com/golang/go synced 2024-09-23 21:30:18 -06:00

cmd/compile: tighten the condition for inlining shape/non-shape function

CL 395854 made inline pass to not inlining function with shape params,
but pass no shape arguments. This is intended to be the reverse case of
CL 361260.

However, CL 361260 is using wider condition than necessary. Though it
only needs to check against function parameters, it checks whether the
function type has no shape. It does not cause any issue, because
!fn.Type().HasShape() implies !fn.Type().Params().HasShape().

But for the reverse case, it's not true. Function may have shape type,
but has no shape arguments. Thus, we must tighten the condition to
explicitly check against the function parameters only.

Fixes #52907

Change-Id: Ib87e87ff767c31d99d5b36aa4a6c1d8baf32746d
Reviewed-on: https://go-review.googlesource.com/c/go/+/406475
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Cuong Manh Le 2022-05-15 08:05:41 +07:00 committed by Gopher Robot
parent b79c135f37
commit f2b1cde544
2 changed files with 13 additions and 4 deletions

View File

@ -701,13 +701,15 @@ func mkinlcall(n *ir.CallExpr, fn *ir.Func, maxCost int32, inlMap map[*ir.Func]b
// apparent when we first created the instantiation of the generic function.
// We can't handle this if we actually do the inlining, since we want to know
// all interface conversions immediately after stenciling. So, we avoid
// inlining in this case. See #49309. (1)
if !fn.Type().HasShape() {
// inlining in this case, see issue #49309. (1)
//
// See discussion on go.dev/cl/406475 for more background.
if !fn.Type().Params().HasShape() {
for _, arg := range n.Args {
if arg.Type().HasShape() {
if logopt.Enabled() {
logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(ir.CurFunc),
fmt.Sprintf("inlining non-shape function %v with shape args", ir.FuncName(fn)))
fmt.Sprintf("inlining function %v has no-shape params with shape args", ir.FuncName(fn)))
}
return n
}
@ -725,7 +727,7 @@ func mkinlcall(n *ir.CallExpr, fn *ir.Func, maxCost int32, inlMap map[*ir.Func]b
if !inlineable {
if logopt.Enabled() {
logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(ir.CurFunc),
fmt.Sprintf("inlining shape function %v with no shape args", ir.FuncName(fn)))
fmt.Sprintf("inlining function %v has shape params with no-shape args", ir.FuncName(fn)))
}
return n
}

View File

@ -14,6 +14,13 @@ func f[T int](t T) {
}
}
func g[T int](g T) {
for true {
_ = func() T { return func(int) T { return g }(0) }()
}
}
func main() {
f(0)
g(0)
}