From f2b1cde544d190e1f4642ef6f4ac8ef9ce31a0c3 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Sun, 15 May 2022 08:05:41 +0700 Subject: [PATCH] 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 TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky Reviewed-by: Ian Lance Taylor Auto-Submit: Cuong Manh Le --- src/cmd/compile/internal/inline/inl.go | 10 ++++++---- test/fixedbugs/issue52907.go | 7 +++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index ff2780de82..9ef016ab73 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -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 } diff --git a/test/fixedbugs/issue52907.go b/test/fixedbugs/issue52907.go index 776be7f280..f54b2049eb 100644 --- a/test/fixedbugs/issue52907.go +++ b/test/fixedbugs/issue52907.go @@ -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) } -- 2.50.0