Skip to content

Arm64: Evaluate if it is possible to combine subsequent field loads in a single load #64815

Closed

Description

@kunalspathak

Evaluate to see how feasible it would be to combine loads of subsequent fields using ldp instead of loading them separately during the use. It cannot be considered as a peep-hole optimization, but an analysis is needed in earlier phases to check around for consecutive field loads and if found one, combine them to a single load.

class Body { public double x, y, z, vx, vy, vz, mass; }
...
foreach (var b in bodies) {
    b.x += dt * b.vx; b.y += dt * b.vy; b.z += dt * b.vz;
}

Below code is generated for the loop that deals with multiplication of double.

  • #1 can be combined into ldp dX, dY, [x3, #8]
  • #2 can be combined into ldp dX, dY, [x3, #32]
  • #3 can be combined into stp dX, dY, [x3, #8]
G_M56457_IG05:              ;; offset=0114H
        D37D7C43          ubfiz   x3, x2, #3, #32
        91004063          add     x3, x3, #16
        F8636803          ldr     x3, [x0, x3]
        FD400470          ldr     d16, [x3,#8]   ; <-- #1
        FD401071          ldr     d17, [x3,#32]  ; <-- #2
        1E710811          fmul    d17, d0, d17
        1E712A10          fadd    d16, d16, d17
        FD000470          str     d16, [x3,#8]   ; <-- #3
        FD400870          ldr     d16, [x3,#16]  ; <-- #1
        FD401471          ldr     d17, [x3,#40]  ; <-- #2
        1E710811          fmul    d17, d0, d17
        1E712A10          fadd    d16, d16, d17
        FD000870          str     d16, [x3,#16]  ; <-- #3
        FD400C70          ldr     d16, [x3,#24]
        FD401871          ldr     d17, [x3,#48]
        1E710811          fmul    d17, d0, d17
        1E712A10          fadd    d16, d16, d17
        FD000C70          str     d16, [x3,#24]
        11000442          add     w2, w2, #1
        6B02003F          cmp     w1, w2
        54FFFD8C          bgt     G_M56457_IG05

Reference: https://godbolt.org/z/9jY5hYnoa

category:implementation
theme:codegen
skill-level:intermediate
cost:medium
impact:medium

Activity

  1. ghost added
    untriaged
    on Feb 4, 2022
  2. kunalspathak commented on Feb 4, 2022

    @kunalspathak
    ContributorAuthor

    @dotnet/jit-contrib

  3. added
    area-CodeGen-coreclr
    on Feb 4, 2022
  4. added this to the 7.0.0 milestone on Feb 8, 2022
  5. added and removed
    untriaged
    on Feb 8, 2022
  6. echesakov commented on Mar 15, 2022

    @echesakov
    Contributor

    Un-assigning myself
    cc @BruceForstall

  7. removed their assignment
    on Mar 15, 2022
  8. added
    needs-further-triage
    on May 20, 2022
  9. JulieLeeMSFT commented on May 20, 2022

    @JulieLeeMSFT
    Member

    @BruceForstall please move to future if we cannot accomodate this in .NET 7.

  10. 12 remaining items

  11. VSadov commented on Mar 21, 2023

    @VSadov
    Member

    We have some existing optimizations where we coalesce reads from the same location.
    I think folding two ldr into one ldp would be valid under the same conditions/analysis.

  12. jkotas commented on Mar 22, 2023

    @jkotas
    Member

    I would have assumed exceptions and stores are both counted as "visible" as well.

    RIght, it is what the paragraph that I quoted is trying to say. "side-effects and exceptions generated by a
    thread are visible in the order specified by the CIL. For this purpose only volatile operations
    (including volatile reads) constitute visible side-effects."

  13. VSadov commented on Mar 22, 2023

    @VSadov
    Member

    Re: exceptions

    For two ordinary ldr reads, the order of reads is not defined - that is in terms of the values that are being read. In terms of exceptions the reads happen in program order (speculative reads do not cause out of order faults).

    I believe ldp preserves the order of faults. I did not see in the spec something like "if one read faults, then whether the other read happens is undefined".
    If that was the case, I think we might need to think harder about when this optimization is valid.

    The pseudocode given for LDP semantics seems to indicate that T1 is loaded before T2 and overall ldp does look like just a compact form of encoding two subsequent reads.

    Perhaps it is worth reaching to ARM folks to clarify that, just in case?

    (interestingly the result is undefined if T1 and T2 are the same register, I assume it refers mostly to the fetched value though)

  14. jkotas commented on Mar 22, 2023

    @jkotas
    Member

    The effect of the ldp instruction has to happen fully or not at all. If it was not the case, page faults from instructions like ldp r1, r2, [r1] would be impossible to handle.

  15. VSadov commented on Mar 22, 2023

    @VSadov
    Member

    Right. The pseudocode can also be read as if there are internal buffers that are filled by the reads and then registers are filled from the temps. I think such "transactional" implementation would make more sense.

    For us it means that if the second read may AV while the first read would not, we can't combine the reads.
    However, since this is considered specifically for field reads, I think it cannot happen. In type-safe code at least.

  16. modified the milestones: 8.0.0, Future on Mar 27, 2023
  17. ghost added
    in-pr
    on Oct 6, 2023
  18. modified the milestones: Future, 9.0.0 on Oct 6, 2023
  19. ghost removed
    in-pr
    on Oct 6, 2023
  20. ghost locked as resolved and limited conversation to collaborators on Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:3arch-arm64area-CodeGen-coreclrneeds-further-triageoptimization

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions