Skip to content

Commit f537158

Browse files
committed
Revert "cmd/compile: allow more inlining of functions that construct closures"
This reverts commit http://go.dev/cl//479095 Reason for revert: causes failures in google-internal testing Change-Id: If1018b35be0b8627e2959f116179ada24d44d67c Reviewed-on: https://go-review.googlesource.com/c/go/+/481637 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Run-TryBot: Than McIntosh <[email protected]>
1 parent 8edcddd commit f537158

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,6 @@ func (v *hairyVisitor) tooHairy(fn *ir.Func) bool {
446446
return false
447447
}
448448

449-
// doNode visits n and its children, updates the state in v, and returns true if
450-
// n makes the current function too hairy for inlining.
451449
func (v *hairyVisitor) doNode(n ir.Node) bool {
452450
if n == nil {
453451
return false
@@ -579,10 +577,13 @@ func (v *hairyVisitor) doNode(n ir.Node) bool {
579577
// TODO(danscales): Maybe make budget proportional to number of closure
580578
// variables, e.g.:
581579
//v.budget -= int32(len(n.(*ir.ClosureExpr).Func.ClosureVars) * 3)
582-
// TODO(austin): However, if we're able to inline this closure into
583-
// v.curFunc, then we actually pay nothing for the closure captures. We
584-
// should try to account for that if we're going to account for captures.
585580
v.budget -= 15
581+
// Scan body of closure (which DoChildren doesn't automatically
582+
// do) to check for disallowed ops in the body and include the
583+
// body in the budget.
584+
if doList(n.(*ir.ClosureExpr).Func.Body, v.do) {
585+
return true
586+
}
586587

587588
case ir.OGO,
588589
ir.ODEFER,

test/closure3.dir/main.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,53 +232,49 @@ func main() {
232232

233233
{
234234
c := 3
235-
func() { // ERROR "can inline main.func26"
235+
func() { // ERROR "func literal does not escape"
236236
c = 4
237-
func() {
237+
func() { // ERROR "func literal does not escape"
238238
if c != 4 {
239239
ppanic("c != 4")
240240
}
241241
recover() // prevent inlining
242242
}()
243-
}() // ERROR "inlining call to main.func26" "func literal does not escape"
243+
}()
244244
if c != 4 {
245245
ppanic("c != 4")
246246
}
247247
}
248248

249249
{
250250
a := 2
251-
// This has an unfortunate exponential growth, where as we visit each
252-
// function, we inline the inner closure, and that constructs a new
253-
// function for any closures inside the inner function, and then we
254-
// revisit those. E.g., func34 and func36 are constructed by the inliner.
255-
if r := func(x int) int { // ERROR "can inline main.func27"
251+
if r := func(x int) int { // ERROR "func literal does not escape"
256252
b := 3
257-
return func(y int) int { // ERROR "can inline main.func27.1" "can inline main.func34"
253+
return func(y int) int { // ERROR "can inline main.func27.1"
258254
c := 5
259-
return func(z int) int { // ERROR "can inline main.func27.1.1" "can inline main.func27.(func)?2" "can inline main.func34.1" "can inline main.func36"
255+
return func(z int) int { // ERROR "can inline main.func27.1.1" "can inline main.func27.(func)?2"
260256
return a*x + b*y + c*z
261257
}(10) // ERROR "inlining call to main.func27.1.1"
262258
}(100) // ERROR "inlining call to main.func27.1" "inlining call to main.func27.(func)?2"
263-
}(1000); r != 2350 { // ERROR "inlining call to main.func27" "inlining call to main.func34" "inlining call to main.func36"
259+
}(1000); r != 2350 {
264260
ppanic("r != 2350")
265261
}
266262
}
267263

268264
{
269265
a := 2
270-
if r := func(x int) int { // ERROR "can inline main.func28"
266+
if r := func(x int) int { // ERROR "func literal does not escape"
271267
b := 3
272-
return func(y int) int { // ERROR "can inline main.func28.1" "can inline main.func35"
268+
return func(y int) int { // ERROR "can inline main.func28.1"
273269
c := 5
274-
func(z int) { // ERROR "can inline main.func28.1.1" "can inline main.func28.(func)?2" "can inline main.func35.1" "can inline main.func37"
270+
func(z int) { // ERROR "can inline main.func28.1.1" "can inline main.func28.(func)?2"
275271
a = a * x
276272
b = b * y
277273
c = c * z
278274
}(10) // ERROR "inlining call to main.func28.1.1"
279275
return a + c
280276
}(100) + b // ERROR "inlining call to main.func28.1" "inlining call to main.func28.(func)?2"
281-
}(1000); r != 2350 { // ERROR "inlining call to main.func28" "inlining call to main.func35" "inlining call to main.func37"
277+
}(1000); r != 2350 {
282278
ppanic("r != 2350")
283279
}
284280
if a != 2000 {

0 commit comments

Comments
 (0)