Skip to content

Commit 4fb4291

Browse files
zephyrtroniumcuonglm
authored andcommitted
cmd/compile: inline functions evaluated in go and defer statements
The inlining pass previously bailed upon encountering a go or defer statement, so it would not inline functions e.g. used to provide arguments to the deferred function. This change preserves the behavior of not inlining the deferred function itself, but it allows the inlining walk to proceed into its arguments. Fixes #42194 Change-Id: I4e82029d8dcbe69019cc83ae63a4b29af45ec777 Reviewed-on: https://go-review.googlesource.com/c/go/+/264997 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Trust: Cuong Manh Le <[email protected]>
1 parent 25d28ec commit 4fb4291

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,11 @@ func inlnode(n *Node, maxCost int32, inlMap map[*Node]bool) *Node {
574574
}
575575

576576
switch n.Op {
577-
// inhibit inlining of their argument
578577
case ODEFER, OGO:
579578
switch n.Left.Op {
580579
case OCALLFUNC, OCALLMETH:
581580
n.Left.SetNoInline(true)
582581
}
583-
return n
584582

585583
// TODO do them here (or earlier),
586584
// so escape analysis can avoid more heapmoves.

test/inline.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,20 @@ func ii() { // ERROR "can inline ii"
246246
f := getMeth(t1) // ERROR "inlining call to getMeth" "t1.meth does not escape"
247247
_ = f(3)
248248
}
249+
250+
// Issue #42194 - make sure that functions evaluated in
251+
// go and defer statements can be inlined.
252+
func gd1(int) {
253+
defer gd1(gd2()) // ERROR "inlining call to gd2"
254+
defer gd3()() // ERROR "inlining call to gd3"
255+
go gd1(gd2()) // ERROR "inlining call to gd2"
256+
go gd3()() // ERROR "inlining call to gd3"
257+
}
258+
259+
func gd2() int { // ERROR "can inline gd2"
260+
return 1
261+
}
262+
263+
func gd3() func() { // ERROR "can inline gd3"
264+
return ii
265+
}

0 commit comments

Comments
 (0)