Skip to content

cmd/compile: don't escape argument when all possible values of function pointer are known #73132

@dominikh

Description

@dominikh

Go version

go version go1.24.1 linux/amd64

Output of go env in your module/workspace:

-

What did you do?

package pkg

func Foo(arg int) {
	var x [4]int

	var fp func(*[4]int)
	if arg == 1 {
		fp = bar1
	} else {
		fp = bar2
	}
	fp(&x)
}

//go:noinline
func bar1(_ *[4]int) {}

//go:noinline
func bar2(_ *[4]int) {}

What did you see happen?

x gets allocated on the heap because the compiler cannot tell that the call to fp doesn't cause the argument to escape.

What did you expect to see?

All possible values of fp are statically known and known not to cause the argument to escape. I would expect the escape information for bar1 and bar2 to be merged and maintained with fp.

This comes up when providing multiple assembly implementations of a function for the same overall CPU architecture, for example both SSE and AVX ones. The implementation has to be chosen dynamically and I would like to pull that out of any loops, hence the function pointer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions