Skip to content

Commit 8854be4

Browse files
thanmgopherbot
authored andcommitted
Revert "cmd/compile: allow more inlining of functions that construct closures"
This reverts commit http://go.dev/cl/c/482356. Reason for revert: Reverting this change again, since it is causing additional failures in google-internal testing. Change-Id: I9234946f62e5bb18c2f873a65e8b298d04af0809 Reviewed-on: https://go-review.googlesource.com/c/go/+/484735 Reviewed-by: Florian Zenker <[email protected]> Run-TryBot: Than McIntosh <[email protected]> Auto-Submit: Than McIntosh <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]>
1 parent e7b04a3 commit 8854be4

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

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

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

462-
// doNode visits n and its children, updates the state in v, and returns true if
463-
// n makes the current function too hairy for inlining.
464462
func (v *hairyVisitor) doNode(n ir.Node) bool {
465463
if n == nil {
466464
return false
@@ -592,10 +590,13 @@ func (v *hairyVisitor) doNode(n ir.Node) bool {
592590
// TODO(danscales): Maybe make budget proportional to number of closure
593591
// variables, e.g.:
594592
//v.budget -= int32(len(n.(*ir.ClosureExpr).Func.ClosureVars) * 3)
595-
// TODO(austin): However, if we're able to inline this closure into
596-
// v.curFunc, then we actually pay nothing for the closure captures. We
597-
// should try to account for that if we're going to account for captures.
598593
v.budget -= 15
594+
// Scan body of closure (which DoChildren doesn't automatically
595+
// do) to check for disallowed ops in the body and include the
596+
// body in the budget.
597+
if doList(n.(*ir.ClosureExpr).Func.Body, v.do) {
598+
return true
599+
}
599600

600601
case ir.OGO,
601602
ir.ODEFER,

src/cmd/compile/internal/test/inl_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,19 @@ func TestIntendedInlining(t *testing.T) {
180180
"net": {
181181
"(*UDPConn).ReadFromUDP",
182182
},
183-
"sync": {
184-
// Both OnceFunc and its returned closure need to be inlinable so
185-
// that the returned closure can be inlined into the caller of OnceFunc.
186-
"OnceFunc",
187-
"OnceFunc.func2", // The returned closure.
188-
// TODO(austin): It would be good to check OnceValue and OnceValues,
189-
// too, but currently they aren't reported because they have type
190-
// parameters and aren't instantiated in sync.
191-
},
183+
// These testpoints commented out for now, since CL 479095
184+
// had to be reverted. We can re-enable this once we roll
185+
// forward with a new version of 479095.
186+
/*
187+
"sync": {
188+
// Both OnceFunc and its returned closure need to be inlinable so
189+
// that the returned closure can be inlined into the caller of OnceFunc.
190+
"OnceFunc",
191+
"OnceFunc.func2", // The returned closure.
192+
// TODO(austin): It would be good to check OnceValue and OnceValues,
193+
// too, but currently they aren't reported because they have type
194+
// parameters and aren't instantiated in sync.
195+
}, */
192196
"sync/atomic": {
193197
// (*Bool).CompareAndSwap handled below.
194198
"(*Bool).Load",

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)