From 80a7504a13a5dccb60757d1fc66d71bcba359799 Mon Sep 17 00:00:00 2001 From: Wayne Zuo Date: Sun, 20 Mar 2022 21:28:46 +0800 Subject: [PATCH] cmd/compile: enable inlining SELECT Change-Id: I90c8e12a0be05d82bf6e147b5249859518f35c14 Reviewed-on: https://go-review.googlesource.com/c/go/+/394074 Run-TryBot: Alberto Donizetti TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky Trust: Keith Randall --- src/cmd/compile/internal/inline/inl.go | 11 ++++++---- test/inline.go | 29 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index 716a7fbcd93a25..be01914d080117 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -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) { diff --git a/test/inline.go b/test/inline.go index 2780e10b196c3c..95af923a26a9b4 100644 --- a/test/inline.go +++ b/test/inline.go @@ -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" + } +}