Skip to content

Commit 9944ba7

Browse files
mdempskygopherbot
authored andcommitted
cmd/compile: fix transitive inlining of generic functions
If an imported, non-generic function F transitively calls a generic function G[T], we may need to call CanInline on G[T]. While here, we can also take advantage of the fact that we know G[T] was already seen and compiled in an imported package, so we don't need to call InlineCalls or add it to typecheck.Target.Decls. This saves us from wasting compile time re-creating DUPOK symbols that we know already exist in the imported package's link objects. Fixes #56280. Change-Id: I3336786bee01616ee9f2b18908738e4ca41c8102 Reviewed-on: https://go-review.googlesource.com/c/go/+/443535 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: David Chase <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]>
1 parent a7538d7 commit 9944ba7

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed

src/cmd/compile/internal/inline/inl.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var (
8383
)
8484

8585
// pgoInlinePrologue records the hot callsites from ir-graph.
86-
func pgoInlinePrologue(p *pgo.Profile) {
86+
func pgoInlinePrologue(p *pgo.Profile, decls []ir.Node) {
8787
if s, err := strconv.ParseFloat(base.Debug.InlineHotCallSiteCDFThreshold, 64); err == nil {
8888
inlineCDFHotCallSiteThresholdPercent = s
8989
}
@@ -104,7 +104,7 @@ func pgoInlinePrologue(p *pgo.Profile) {
104104
}
105105
}
106106
// mark hot call sites
107-
ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
107+
ir.VisitFuncsBottomUp(decls, func(list []*ir.Func, recursive bool) {
108108
for _, f := range list {
109109
name := ir.PkgFuncName(f)
110110
if n, ok := p.WeightedCG.IRNodes[name]; ok {
@@ -164,9 +164,9 @@ func computeThresholdFromCDF(p *pgo.Profile) (float64, []pgo.NodeMapKey) {
164164
}
165165

166166
// pgoInlineEpilogue updates IRGraph after inlining.
167-
func pgoInlineEpilogue(p *pgo.Profile) {
167+
func pgoInlineEpilogue(p *pgo.Profile, decls []ir.Node) {
168168
if base.Debug.PGOInline >= 2 {
169-
ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
169+
ir.VisitFuncsBottomUp(decls, func(list []*ir.Func, recursive bool) {
170170
for _, f := range list {
171171
name := ir.PkgFuncName(f)
172172
if n, ok := p.WeightedCG.IRNodes[name]; ok {
@@ -182,11 +182,16 @@ func pgoInlineEpilogue(p *pgo.Profile) {
182182

183183
// InlinePackage finds functions that can be inlined and clones them before walk expands them.
184184
func InlinePackage(p *pgo.Profile) {
185+
InlineDecls(p, typecheck.Target.Decls, true)
186+
}
187+
188+
// InlineDecls applies inlining to the given batch of declarations.
189+
func InlineDecls(p *pgo.Profile, decls []ir.Node, doInline bool) {
185190
if p != nil {
186-
pgoInlinePrologue(p)
191+
pgoInlinePrologue(p, decls)
187192
}
188193

189-
ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
194+
ir.VisitFuncsBottomUp(decls, func(list []*ir.Func, recursive bool) {
190195
numfns := numNonClosures(list)
191196
for _, n := range list {
192197
if !recursive || numfns > 1 {
@@ -199,12 +204,14 @@ func InlinePackage(p *pgo.Profile) {
199204
fmt.Printf("%v: cannot inline %v: recursive\n", ir.Line(n), n.Nname)
200205
}
201206
}
202-
InlineCalls(n, p)
207+
if doInline {
208+
InlineCalls(n, p)
209+
}
203210
}
204211
})
205212

206213
if p != nil {
207-
pgoInlineEpilogue(p)
214+
pgoInlineEpilogue(p, decls)
208215
}
209216
}
210217

src/cmd/compile/internal/noder/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,7 @@ func unifiedInlineCall(call *ir.CallExpr, fn *ir.Func, inlIndex int) *ir.Inlined
34853485
// potentially be recursively inlined themselves; but we shouldn't
34863486
// need to read in the non-inlined bodies for the declarations
34873487
// themselves. But currently it's an easy fix to #50552.
3488-
readBodies(typecheck.Target)
3488+
readBodies(typecheck.Target, true)
34893489

34903490
deadcode.Func(r.curfn)
34913491

src/cmd/compile/internal/noder/unified.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func unified(noders []*noder) {
101101
}
102102
}
103103

104-
readBodies(target)
104+
readBodies(target, false)
105105

106106
// Check that nothing snuck past typechecking.
107107
for _, n := range target.Decls {
@@ -123,7 +123,13 @@ func unified(noders []*noder) {
123123

124124
// readBodies iteratively expands all pending dictionaries and
125125
// function bodies.
126-
func readBodies(target *ir.Package) {
126+
//
127+
// If duringInlining is true, then the inline.InlineDecls is called as
128+
// necessary on instantiations of imported generic functions, so their
129+
// inlining costs can be computed.
130+
func readBodies(target *ir.Package, duringInlining bool) {
131+
var inlDecls []ir.Node
132+
127133
// Don't use range--bodyIdx can add closures to todoBodies.
128134
for {
129135
// The order we expand dictionaries and bodies doesn't matter, so
@@ -152,7 +158,11 @@ func readBodies(target *ir.Package) {
152158
// Instantiated generic function: add to Decls for typechecking
153159
// and compilation.
154160
if fn.OClosure == nil && len(pri.dict.targs) != 0 {
155-
target.Decls = append(target.Decls, fn)
161+
if duringInlining {
162+
inlDecls = append(inlDecls, fn)
163+
} else {
164+
target.Decls = append(target.Decls, fn)
165+
}
156166
}
157167

158168
continue
@@ -163,6 +173,30 @@ func readBodies(target *ir.Package) {
163173

164174
todoDicts = nil
165175
todoBodies = nil
176+
177+
if len(inlDecls) != 0 {
178+
// If we instantiated any generic functions during inlining, we need
179+
// to call CanInline on them so they'll be transitively inlined
180+
// correctly (#56280).
181+
//
182+
// We know these functions were already compiled in an imported
183+
// package though, so we don't need to actually apply InlineCalls or
184+
// save the function bodies any further than this.
185+
//
186+
// We can also lower the -m flag to 0, to suppress duplicate "can
187+
// inline" diagnostics reported against the imported package. Again,
188+
// we already reported those diagnostics in the original package, so
189+
// it's pointless repeating them here.
190+
191+
oldLowerM := base.Flag.LowerM
192+
base.Flag.LowerM = 0
193+
inline.InlineDecls(nil, inlDecls, false)
194+
base.Flag.LowerM = oldLowerM
195+
196+
for _, fn := range inlDecls {
197+
fn.(*ir.Func).Body = nil // free memory
198+
}
199+
}
166200
}
167201

168202
// writePkgStub type checks the given parsed source files,

test/fixedbugs/issue56280.dir/a.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
func F() { // ERROR "can inline F"
8+
g(0) // ERROR "inlining call to g\[go.shape.int\]"
9+
}
10+
11+
func g[T any](_ T) {} // ERROR "can inline g\[int\]" "can inline g\[go.shape.int\]" "inlining call to g\[go.shape.int\]"

test/fixedbugs/issue56280.dir/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import "test/a"
8+
9+
func main() { // ERROR "can inline main"
10+
a.F() // ERROR "inlining call to a.F" "inlining call to a.g\[go.shape.int\]"
11+
}

test/fixedbugs/issue56280.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// errorcheckdir -0 -m
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

test/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,7 @@ var types2Failures32Bit = setOf(
20352035

20362036
var go118Failures = setOf(
20372037
"fixedbugs/issue54343.go", // 1.18 compiler assigns receiver parameter to global variable
2038+
"fixedbugs/issue56280.go", // 1.18 compiler doesn't support inlining generic functions
20382039
"typeparam/nested.go", // 1.18 compiler doesn't support function-local types with generics
20392040
"typeparam/issue47631.go", // 1.18 can not handle local type declarations
20402041
"typeparam/issue51521.go", // 1.18 compiler produces bad panic message and link error

0 commit comments

Comments
 (0)