Skip to content

cmd/compile: rewrite escape analysis #23109

@mdempsky

Description

@mdempsky

Despite repeated efforts, I'm still confused by our escape analysis code. Also, based on discussion with other compiler devs, apparently this difficulty understanding is not unique to me. It would be good to better document this code so we can hopefully simplify and improve it.

Some specific questions I've had trying to understand the code:

  • The "Escape analysis" documentation has a very high-level explanation, but leaves me with more questions than answers:

    • It mixes discussion of "nodes" and "variables," which makes it unclear to me whether expressions other than ONAME Nodes are present in the flow graph. (I think they are, otherwise I can't make sense of how we know if new(T) can be stack allocated or not.) It also talks about "values" having addresses, when only variables do. Using terminology consistent with the Go spec would help, IMO.

    • It mentions storing edges between "pointer-containing nodes". It would be nice to understand how this is expected to work in the presence of unsafe.Pointer <-> uintptr conversions.

    • Pseudo-code like flow(closure, &var) is confusing too: what's the & here supposed to signify?

    • I can't make sense of the phrase "tags all variables of it can reach an & node as escaping".

  • The Level godocs are super opaque to me:

    • I kind of understand the meaning of value, but I don't completely understand why adding the +1 and -1 values is sound.

    • I have no idea what a "maximum-copy-started-suffix-level" is, and the x.left.left, &Node{x}, etc. examples don't really help. I'm particularly confused because a Node can represent so many different operations depending on the Op, and I'm lost on what semantic meaning we're divining from syntactic tree depth.

  • escAnalyze has a reflooding loop that keeps looping until fixed point. It's not obvious to me why this is necessary. (At least experimentally, I do see that "Reflooding foo" shows up when building the standard library with -m=3.)

/cc @dr2chase @cherrymui @paranoiacblack

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions