Skip to content

Commit 84db201

Browse files
mateusz834gopherbot
authored andcommitted
cmd/compile: propagate len([]T{}) to make builtin to allow stack allocation
Updates #75620 Change-Id: I6a6a6964af4512e30eb4806e1dc7b0fd0835744f Reviewed-on: https://go-review.googlesource.com/c/go/+/707255 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 5799c13 commit 84db201

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/cmd/compile/internal/escape/escape.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,10 @@ func (b *batch) rewriteWithLiterals(n ir.Node, fn *ir.Func) {
563563
if ro == nil {
564564
base.Fatalf("no ReassignOracle for function %v with closure parent %v", fn, fn.ClosureParent)
565565
}
566-
if s := ro.StaticValue(*r); s.Op() == ir.OLITERAL {
566+
567+
s := ro.StaticValue(*r)
568+
switch s.Op() {
569+
case ir.OLITERAL:
567570
lit, ok := s.(*ir.BasicLit)
568571
if !ok || lit.Val().Kind() != constant.Int {
569572
base.Fatalf("unexpected BasicLit Kind")
@@ -577,6 +580,14 @@ func (b *batch) rewriteWithLiterals(n ir.Node, fn *ir.Func) {
577580
assignTemp(n.Pos(), *r, n.PtrInit())
578581
*r = ir.NewBasicLit(n.Pos(), (*r).Type(), lit.Val())
579582
}
583+
case ir.OLEN:
584+
x := ro.StaticValue(s.(*ir.UnaryExpr).X)
585+
if x.Op() == ir.OSLICELIT {
586+
x := x.(*ir.CompLitExpr)
587+
// Preserve any side effects of the original expression, then update the value.
588+
assignTemp(n.Pos(), *r, n.PtrInit())
589+
*r = ir.NewBasicLit(n.Pos(), types.Types[types.TINT], constant.MakeInt64(x.Len))
590+
}
580591
}
581592
}
582593
case ir.OCONVIFACE:

test/escape_make_non_const.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,18 @@ type m struct {
106106
func newM(l int) m { // ERROR "can inline"
107107
return m{make(map[string]int, l)} // ERROR "make.*escapes to heap"
108108
}
109+
110+
//go:noinline
111+
func testLenOfSliceLit() {
112+
ints := []int{0, 1, 2, 3, 4, 5} // ERROR "\[\]int\{\.\.\.\} does not escape"'
113+
_ = make([]int, len(ints)) // ERROR "make\(\[\]int, 6\) does not escape"
114+
_ = allocLenOf(ints) // ERROR "inlining call", "make\(\[\]int, 6\) does not escape"
115+
116+
_ = make([]int, 2, len(ints)) // ERROR "make\(\[\]int, 2, 6\) does not escape"
117+
_ = make([]int, len(ints), 2) // ERROR "make\(\[\]int, len\(ints\), 2\) does not escape"
118+
_ = make([]int, 10, len(ints)) // ERROR "make\(\[\]int, 10, 6\) does not escape"
119+
}
120+
121+
func allocLenOf(s []int) []int { // ERROR "can inline" "s does not escape"
122+
return make([]int, len(s)) // ERROR "escapes to heap"
123+
}

0 commit comments

Comments
 (0)