Skip to content

Commit

Permalink
cmd/compile: enable inlining SELECT
Browse files Browse the repository at this point in the history
Change-Id: I90c8e12a0be05d82bf6e147b5249859518f35c14
Reviewed-on: https://go-review.googlesource.com/c/go/+/394074
Run-TryBot: Alberto Donizetti <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
Trust: Keith Randall <[email protected]>
  • Loading branch information
wdvxdr1123 authored and mdempsky committed Mar 25, 2022
1 parent 3dac99a commit 80a7504
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/cmd/compile/internal/inline/inl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions test/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package foo

import (
"runtime"
"time"
"unsafe"
)

Expand Down Expand Up @@ -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"
}
}

0 comments on commit 80a7504

Please sign in to comment.