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

Open
mdempsky opened this issue Oct 25, 2017 · 6 comments

Comments

@mdempsky
Copy link
Member

@mdempsky 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.

Copy link
Member Author

@mdempsky 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.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 29, 2018

@dr2chase

This comment has been minimized.

Copy link
Contributor

@dr2chase 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.

Copy link
Contributor

@ianlancetaylor 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.

Copy link
Contributor

@dr2chase 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).

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Sep 5, 2019

Change https://golang.org/cl/193517 mentions this issue: cmd/compile: handle loopdepth in looping label

gopherbot pushed a commit that referenced this issue Sep 6, 2019
Add block method to preserve loop depth when evaluating statements in a
block, so escape analysis can handle looping label more precisely.

Updates #22438

Change-Id: I39b306544a6c0ee3fcbebbe0d0ee735cb71773e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/193517
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
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.