Closed
Labels
Priority:3Work that is nice to havearch-arm64area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationoptimization
Milestone
Description
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.
#1can be combined intoldp dX, dY, [x3, #8]#2can be combined intoldp dX, dY, [x3, #32]#3can be combined intostp 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
Metadata
Metadata
Assignees
Labels
Priority:3Work that is nice to havearch-arm64area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationoptimization
Activity
kunalspathak commentedon Feb 4, 2022
@dotnet/jit-contrib
echesakov commentedon Mar 15, 2022
Un-assigning myself
cc @BruceForstall
JulieLeeMSFT commentedon May 20, 2022
@BruceForstall please move to future if we cannot accomodate this in .NET 7.
12 remaining items
VSadov commentedon Mar 21, 2023
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.
jkotas commentedon Mar 22, 2023
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."
VSadov commentedon Mar 22, 2023
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)
jkotas commentedon Mar 22, 2023
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.VSadov commentedon Mar 22, 2023
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.