Skip to content

Commit fe2da30

Browse files
JunyangShaoGo LUCI
authored andcommitted
cmd/compile: keep variables alive in testing.B.Loop loops
For the loop body guarded by testing.B.Loop, we disable function inlining and devirtualization inside. The only legal form to be matched is `for b.Loop() {...}`. For #61515 Change-Id: I2e226f08cb4614667cbded498a7821dffe3f72d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/612043 Reviewed-by: Michael Pratt <[email protected]> TryBot-Bypass: Junyang Shao <[email protected]> Commit-Queue: Junyang Shao <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 73ac82f commit fe2da30

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ func DevirtualizeAndInlineFunc(fn *ir.Func, profile *pgoir.Profile) {
105105
})
106106
}
107107

108+
// isTestingBLoop returns true if it matches the node as a
109+
// testing.(*B).Loop. See issue #61515.
110+
func isTestingBLoop(t ir.Node) bool {
111+
if t.Op() != ir.OFOR {
112+
return false
113+
}
114+
nFor, ok := t.(*ir.ForStmt)
115+
if !ok || nFor.Cond == nil || nFor.Cond.Op() != ir.OCALLFUNC {
116+
return false
117+
}
118+
n, ok := nFor.Cond.(*ir.CallExpr)
119+
if !ok || n.Fun == nil || n.Fun.Op() != ir.OMETHEXPR {
120+
return false
121+
}
122+
name := ir.MethodExprName(n.Fun)
123+
if name == nil {
124+
return false
125+
}
126+
if fSym := name.Sym(); fSym != nil && name.Class == ir.PFUNC && fSym.Pkg != nil &&
127+
fSym.Name == "(*B).Loop" && fSym.Pkg.Path == "testing" {
128+
// Attempting to match a function call to testing.(*B).Loop
129+
return true
130+
}
131+
return false
132+
}
133+
108134
// fixpoint repeatedly edits a function until it stabilizes.
109135
//
110136
// First, fixpoint applies match to every node n within fn. Then it
@@ -133,6 +159,14 @@ func fixpoint(fn *ir.Func, match func(ir.Node) bool, edit func(ir.Node) ir.Node)
133159
return n // already visited n.X before wrapping
134160
}
135161

162+
if isTestingBLoop(n) {
163+
// No inlining nor devirtualization performed on b.Loop body
164+
if base.Flag.LowerM > 1 {
165+
fmt.Printf("%v: skip inlining within testing.B.loop for %v\n", ir.Line(n), n)
166+
}
167+
return n
168+
}
169+
136170
ok := match(n)
137171

138172
// can't wrap TailCall's child into ParenExpr

test/inline_testingbloop.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// errorcheck -0 -m=2
2+
3+
// Copyright 2024 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+
// Test no inlining of function calls in testing.B.Loop.
8+
// See issue #61515.
9+
10+
package foo
11+
12+
import "testing"
13+
14+
func caninline(x int) int { // ERROR "can inline caninline"
15+
return x
16+
}
17+
18+
func cannotinline(b *testing.B) { // ERROR "b does not escape" "cannot inline cannotinline.*"
19+
for i := 0; i < b.N; i++ {
20+
caninline(1) // ERROR "inlining call to caninline"
21+
}
22+
for b.Loop() { // ERROR "skip inlining within testing.B.loop"
23+
caninline(1)
24+
}
25+
for i := 0; i < b.N; i++ {
26+
caninline(1) // ERROR "inlining call to caninline"
27+
}
28+
for b.Loop() { // ERROR "skip inlining within testing.B.loop"
29+
caninline(1)
30+
}
31+
}

0 commit comments

Comments
 (0)