cmd/compile: enable inlining SELECT

Change-Id: I90c8e12a0be05d82bf6e147b5249859518f35c14
Reviewed-on: https://go-review.googlesource.com/c/go/+/394074
Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Trust: Keith Randall <khr@golang.org>
This commit is contained in:
Wayne Zuo 2022-03-20 21:28:46 +08:00 committed by Matthew Dempsky
parent 3dac99ad4c
commit 80a7504a13
2 changed files with 36 additions and 4 deletions

View File

@ -358,8 +358,7 @@ func (v *hairyVisitor) doNode(n ir.Node) bool {
return true
}
case ir.OSELECT,
ir.OGO,
case ir.OGO,
ir.ODEFER,
ir.ODCLTYPE, // can't print yet
ir.OTAILCALL:
@ -1310,7 +1309,7 @@ func (subst *inlsubst) node(n ir.Node) ir.Node {
ir.EditChildren(m, subst.edit)
if subst.newclofn == nil {
// Translate any label on FOR, RANGE loops or SWITCH
// Translate any label on FOR, RANGE loops, SWITCH or SELECT
switch m.Op() {
case ir.OFOR:
m := m.(*ir.ForStmt)
@ -1326,8 +1325,12 @@ func (subst *inlsubst) node(n ir.Node) ir.Node {
m := m.(*ir.SwitchStmt)
m.Label = translateLabel(m.Label)
return m
}
case ir.OSELECT:
m := m.(*ir.SelectStmt)
m.Label = translateLabel(m.Label)
return m
}
}
switch m := m.(type) {

View File

@ -11,6 +11,7 @@ package foo
import (
"runtime"
"time"
"unsafe"
)
@ -303,3 +304,31 @@ func conv2(v uint64) uint64 { // ERROR "can inline conv2"
func conv1(v uint64) uint64 { // ERROR "can inline conv1"
return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v)))))))))))
}
func select1(x, y chan bool) int { // ERROR "can inline select1" "x does not escape" "y does not escape"
select {
case <-x:
return 1
case <-y:
return 2
}
}
func select2(x chan bool) { // ERROR "can inline select2" "x does not escape"
loop: // test that labeled select can be inlined.
select {
case <-x:
break loop
case <-time.After(time.Second): // ERROR "inlining call to time.After"
}
}
func inlineSelect2(x, y chan bool) { // ERROR "x does not escape" "y does not escape"
loop:
for i := 0; i < 5; i++ {
if i == 3 {
break loop
}
select2(x) // ERROR "inlining call to select2" "inlining call to time.After"
}
}