Skip to content

cmd/compile: js-wasm builder broke on "isfat" optimization #33916

Closed

Description

@mdempsky

https://go-review.googlesource.com/c/go/+/179578 changed plive.go's "isfat" function to treat single element arrays and single field structs as single-value objects rather than multi-value objects. The notable thing about this change is it means for things like:

var x struct { m *blah }
x.m = foo()

the x.m = foo() assignment is considered as clobbering x, whereas before it was treated as just updating a single field, so the variable was considered live.

The CL updated test expectations accordingly, but the js-wasm builder seemed to fail on these, as it still considered the struct live in some cases.

The CL has been reverted, but I think it would be good to understand why only js-wasm failed and land the change again.

/cc @cuonglm

Activity

  1. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Aug 28, 2019
  2. mdempsky commented on Aug 28, 2019

    @mdempsky
    ContributorAuthor

    I think the problem is that ssa/gen/WasmOps.go doesn't annotate instructions with symEffect.

    I'm kind of surprised there aren't more liveness issues on GOARCH=wasm.

  3. mdempsky commented on Aug 28, 2019

    @mdempsky
    ContributorAuthor

    I see. So in live2.go's bad40, immediately before SSA lowering, we have (v25 is the return value from makemap, and v23 is the mem object returned by it):

    v2 = SP
    v26 = LocalAddr <*T40> {ret} v2 v23
    v27 = OffPtr <*map[int]int> [0] v26
    v28 = Store <mem>{map[int]int} v27 v25 v23
    

    On most architectures (I tested amd64 and arm64, and I expect others are the same), this gets lowered to something like:

    v2 = SP
    v8 = MOVQstore <mem> {ret} v2 v25 v23
    

    Critically, plive.go is able to recognize than this MOVQ clobbers ret because MOVQstore is annotated with symEffect: "write" in gen/AMD64Ops.go.

    However, on wasm, this gets lowered to:

    v2 = SP
    v26 = LoweredAddr <*T40> {ret} v2
    v28 = I64Store <mem> [0] v26 v25 v23
    

    which I think is roughly comparable to something like:

    v2 = SP
    v26 = LEAQ <*T40> {ret} v2
    v28 = MOVQstore <mem> [0] v26 v25 v23
    

    That is, the address computation is split out separately, and plive.go can't figure out the store is clobbering anything.

    I think wasm should switch to support SymOffset for its load and store operations like other GOARCHes.

    /cc @randall77 @cherrymui @neelance

  4. mdempsky commented on Aug 28, 2019

    @mdempsky
    ContributorAuthor

    I think wasm should switch to support SymOffset for its load and store operations like other GOARCHes.

    It seems like the reason this isn't a bigger issue for wasm is that gc/ssa.go inserts OpVarDef instructions around assignments where possible. This issue came up because it introduced new cases where plive.go was able to detect a variable was dead where gc/ssa.go didn't notice.

    So an alternative fix here would be to extend gc/ssa.go to incorporate the "isfat" logic for figuring out when it can insert OpVarDef.

  5. cuonglm commented on Aug 29, 2019

    @cuonglm
    Member

    @mdempsky IIRC, buildssa emits OpVarDef regardless of variable is fat or not.

  6. mdempsky commented on Aug 29, 2019

    @mdempsky
    ContributorAuthor

    IIRC, buildssa emits OpVarDef regardless of variable is fat or not.

    For assignments like x = ... it does, but I don't believe that's the case for subvariable assignments like x.m = ....

  7. cherrymui commented on Aug 31, 2019

    @cherrymui
    Member

    I think wasm should switch to support SymOffset for its load and store operations like other GOARCHes.

    I think this makes sense, at least for local variables.

  8. cuonglm commented on Sep 3, 2019

    @cuonglm
    Member

    So an alternative fix here would be to extend gc/ssa.go to incorporate the "isfat" logic for figuring out when it can insert OpVarDef.

    @mdempsky can you describe it more details?

    Looking at SSA output, I see OpVarDef was inserted when ret declared. Also where must we incorporate the "isfat" logic?

  9. mdempsky commented on Sep 3, 2019

    @mdempsky
    ContributorAuthor

    @mdempsky can you describe it more details?

    gc/ssa.go emits OpVarDef when an assignments overwrites the entire variable. Currently it does this when an object is declared, and also immediately before assignments that overwrite the entire object (eg, x = ...). Note: OpVarDef can be emitted multiple times for a single variable.

    The alternative suggestion I was making was that gc/ssa.go should also recognize x.m = ... assignments where x's type is not "fat".

    Also where must we incorporate the "isfat" logic?

    I would expect it would just be a tweak to the existing OpVarDef logic in builder.assign.

  10. cuonglm commented on Sep 3, 2019

    @cuonglm
    Member

    @mdempsky can you describe it more details?

    gc/ssa.go emits OpVarDef when an assignments overwrites the entire variable. Currently it does this when an object is declared, and also immediately before assignments that overwrite the entire object (eg, x = ...). Note: OpVarDef can be emitted multiple times for a single variable.

    The alternative suggestion I was making was that gc/ssa.go should also recognize x.m = ... assignments where x's type is not "fat".

    Also where must we incorporate the "isfat" logic?

    I would expect it would just be a tweak to the existing OpVarDef logic in builder.assign.

    Hmm, the same as I did, but still no luck, maybe I check the wrong place, let me re-check.

  11. gopherbot commented on Sep 3, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/192978 mentions this issue: Revert "Revert "cmd/compile: make isfat handle 1-element array, 1-field struct""

  12. gopherbot commented on Sep 3, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/192979 mentions this issue: cmd/compile: extend ssa.go to incorporate the "isfat" logic

  13. added a commit that references this issue on Sep 5, 2019
    c641594
  14. locked and limited conversation to collaborators on Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions