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: escape analysis loopdepth should reset when blocks end #22438

Open
mdempsky opened this Issue Oct 25, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@mdempsky
Member

mdempsky commented Oct 25, 2017

Compiling the package below with compile -m shows that new(int) escapes to heap:

package p

func g() bool

//go:noescape
func h(*int)

func f() {
        var p *int
        if g() {
        x:
                goto x
        } else {
                p = new(int)
        }
        h(p)
}

After replacing x: goto x with for {}, it no longer escapes.

The issue is x is considered a looping label, so (*EscState).esc increments e.loopdepth when visiting it. However, loopdepth is never decremented again at the end of its enclosing block, so loopdepth is still incremented when visiting the p = new(int) assignment.

@mdempsky

This comment has been minimized.

Member

mdempsky commented Oct 25, 2017

A variation on the issue is:

func f() {
        var p *int
        {
        x:
                if g() {
                        goto x
                }
        }
        p = new(int)
        h(p)
}

This is trickier though, because we flatten blocks in the Node tree representation, so the above is indistinguishable from:

func f() {
        var p *int
x:
        if g() {
                goto x
        }
        p = new(int)
        h(p)
}

(Which also really needn't escape, but requires more sophisticated control flow analysis.)

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Mar 29, 2018

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Mar 29, 2018

@dr2chase

This comment has been minimized.

Contributor

dr2chase commented Mar 29, 2018

It seems aggressive to make this a milestone before we even have a non-test example of a loop made from a goto.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Mar 29, 2018

The milestone is to set a point by which we should make progress. If you want to make progress by changing the milestone to unplanned, that is fine.

@dr2chase dr2chase modified the milestones: Go1.11, Unplanned Mar 30, 2018

@dr2chase

This comment has been minimized.

Contributor

dr2chase commented Mar 30, 2018

Milestone set to unplanned, lacking any performance problem in "real" code caused by this shortfall (which would be a bit of a pain to fix since the tree-based analyses lack structural loop detectors).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment