mirror of
https://github.com/golang/go.git
synced 2025-05-24 00:41:24 +00:00
Today, this interface conversion causes the struct literal to be heap allocated: var sink any func example1() { sink = S{1, 1} } For basic literals like integers that are directly used in an interface conversion that would otherwise allocate, the compiler is able to use read-only global storage (see #18704). This CL extends that to struct and array literals as well by creating read-only global storage that is able to represent for example S{1, 1}, and then using a pointer to that storage in the interface when the interface conversion happens. A more challenging example is: func example2() { v := S{1, 1} sink = v } In this case, the struct literal is not directly part of the interface conversion, but is instead assigned to a local variable. To still avoid heap allocation in cases like this, in walk we construct a cache that maps from expressions used in interface conversions to earlier expressions that can be used to represent the same value (via ir.ReassignOracle.StaticValue). This is somewhat analogous to how we avoided heap allocation for basic literals in CL 649077 earlier in our stack, though here we also need to do a little more work to create the read-only global. CL 649076 (also earlier in our stack) added most of the tests along with debug diagnostics in convert.go to make it easier to test this change. See the writeup in #71359 for details. Fixes #71359 Fixes #71323 Updates #62653 Updates #53465 Updates #8618 Change-Id: I8924f0c69ff738ea33439bd6af7b4066af493b90 Reviewed-on: https://go-review.googlesource.com/c/go/+/649555 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@google.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// asmcheck
|
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Make sure a pointer variable and a zero-sized variable
|
|
// aren't allocated to the same stack slot.
|
|
// See issue 24993.
|
|
|
|
package codegen
|
|
|
|
func zeroSize() {
|
|
c := make(chan struct{})
|
|
// amd64:`MOVQ\t\$0, command-line-arguments\.s\+56\(SP\)`
|
|
var s *int
|
|
// force s to be a stack object, also use some (fixed) stack space
|
|
g(&s, 1, 2, 3, 4, 5)
|
|
|
|
// amd64:`LEAQ\tcommand-line-arguments\..*\+55\(SP\)`
|
|
c <- noliteral(struct{}{})
|
|
}
|
|
|
|
// Like zeroSize, but without hiding the zero-sized struct.
|
|
func zeroSize2() {
|
|
c := make(chan struct{})
|
|
// amd64:`MOVQ\t\$0, command-line-arguments\.s\+48\(SP\)`
|
|
var s *int
|
|
// force s to be a stack object, also use some (fixed) stack space
|
|
g(&s, 1, 2, 3, 4, 5)
|
|
|
|
// amd64:`LEAQ\tcommand-line-arguments\..*stmp_\d+\(SB\)`
|
|
c <- struct{}{}
|
|
}
|
|
|
|
//go:noinline
|
|
func g(**int, int, int, int, int, int) {}
|
|
|
|
// noliteral prevents the compiler from recognizing a literal value.
|
|
//
|
|
//go:noinline
|
|
func noliteral[T any](t T) T {
|
|
return t
|
|
}
|