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: replace indirect call with direct call when target is known after inlining #32577

Open
marigonzes opened this issue Jun 12, 2019 · 2 comments

Comments

@marigonzes
Copy link

commented Jun 12, 2019

What version of Go are you using (go version)?

$ go version
go version devel +ef84fa0 Mon Jun 10 23:23:39 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

I compiled the following functions: https://godbolt.org/z/dHW0pK.

func f(x int) int {
    return x
}

func getf() func (int) int {
    return f
}

func applyf() int {
    return getf()(2)
}

What did you expect to see?

I expected the last function not to contain any unnecessary instructions or, in a perfect world, that it would be compiled to a simple return.

What did you see instead?

Instead, there was an ineffective instruction on line 37 of the assembly code generated in the website mentioned above.

leaq    "".f·f(SB), DX

PS: Thanks to @mundaym for helping me figure this out.

@randall77

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

That instruction loads a pointer to the closure into a register for the call.
That is required for the calling convention.
It's not necessary in this case because the closure doesn't close over any variables. But if we know that it doesn't close over any variables, we likely know the exact target and we might as well replace the indirect call with a direct call (which could then inline, which would then match your perfect world).

@randall77 randall77 added this to the Unplanned milestone Jun 12, 2019
@mundaym mundaym changed the title cmd/compile: ineffective store due to closure cmd/compile: replace indirect call with direct call when target is known Jun 12, 2019
@mundaym mundaym changed the title cmd/compile: replace indirect call with direct call when target is known cmd/compile: replace indirect call with direct call when target is known after inlining Jun 12, 2019
@mundaym

This comment has been minimized.

Copy link
Member

commented Jun 12, 2019

I think it would be fairly straightforward to convert a ClosureCall into a StaticCall in SSA when the target is known (perhaps taking a similar approach to the InterCall optimization @philhofer added in CL 38139). As far as I know going from a direct call to an inlined function isn't currently possible in the SSA backend though - and inlining is probably more valuable than a ClosureCall to StaticCall optimization in this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.