Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: inlining heuristic does not know code is dead #9274

Closed
randall77 opened this issue Dec 12, 2014 · 6 comments
Closed

cmd/compile: inlining heuristic does not know code is dead #9274

randall77 opened this issue Dec 12, 2014 · 6 comments

Comments

@randall77
Copy link
Contributor

randall77 commented Dec 12, 2014

The inlining decisions by the compiler are made before the compiler knows about dead code in the inlined routine. For instance, both of these functions inline:

func readUnaligned64(p unsafe.Pointer) uint64 {
    return _(_uint64)(p)
}

func readUnaligned64(p unsafe.Pointer) uint64 {
    q := (*[8]byte)(p)
    return uint64(q[0]) + uint64(q[1])<<8 + uint64(q[2])<<16 + uint64(q[3])<<24 + uint64(q[4])<<32 + uint64(q[5])<<40 + uint64(q[6])<<48 + uint64(q[7])<<56
}

but combine them with a compile-time-constant if, and it doesn't inline:

func readUnaligned64(p unsafe.Pointer) uint64 {
    if GOOS=="amd64" {
        return _(_uint64)(p)
    }
    return uint64(q[0]) + uint64(q[1])<<8 + uint64(q[2])<<16 + uint64(q[3])<<24 + uint64(q[4])<<32 + uint64(q[5])<<40 + uint64(q[6])<<48 + uint64(q[7])<<56
}
@minux
Copy link
Member

minux commented Dec 12, 2014

I'm beginning to suspect that it's just too complicated to inline.

$ cat t.go
package f

import "unsafe"

func readUnaligned64(p unsafe.Pointer) uint64 {
q := (*[8]byte)(p)
return uint64(q[0]) + uint64(q[1])<<8 + uint64(q[2])<<16 + uint64(q[3])<<24

  • uint64(q[4])<<32 + uint64(q[5])<<40 + uint64(q[6])<<48 + uint64(q[7])<<56
    }
    $ go version
    go version devel +3fa5d3a Thu Dec 11 15:23:18 2014 +0000 linux/amd64
    $ go tool 6g -m t.go
    t.go:5: readUnaligned64 p does not escape
    $ go tool 6g -m -l -l -l -l t.go
    t.go:5: readUnaligned64 p does not escape

$ cat t2.go
package f

import "unsafe"

func readUnaligned64(p unsafe.Pointer) uint64 {
return _(_uint64)(p)
}
$ go tool 6g -m t2.go
t2.go:5: can inline readUnaligned64
t2.go:5: readUnaligned64 p does not escape

@randall77
Copy link
Contributor Author

At least when GOARCH=="amd64", it would be nice if it inlined.

@minux
Copy link
Member

minux commented Dec 12, 2014

On Thu, Dec 11, 2014 at 8:45 PM, randall77 notifications@github.com wrote:

At least when GOARCH=="amd64", it would be nice if it inlined.

Then we only need two copies. One for architecture that supports unaligned
load,
and one for the others.

@randall77
Copy link
Contributor Author

You think unaligned1.go and unaligned2.go, each with appropriate build tags, is the way to go?

@minux
Copy link
Member

minux commented Dec 12, 2014

On Thu, Dec 11, 2014 at 10:12 PM, randall77 notifications@github.com
wrote:

You think unaligned1.go and unaligned2.go, each with appropriate build
tags, is the way to go?

Yes. And we should figure out why the portable version can not be inlined
even with 4 -l.

If the endian doesn't matter, I also recommend change the function name to
be more explicit about
the endian.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/gc: inlining heuristic does not know code is dead cmd/compile: inlining heuristic does not know code is dead Jun 8, 2015
@gopherbot
Copy link

CL https://golang.org/cl/37499 mentions this issue.

@golang golang locked and limited conversation to collaborators Feb 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants