Sinking load instructions results in worse performance and increased dynamic instruction counts
SimplifyCFG recently gained https://github.com/llvm/llvm-project/commit/ede27d8d391e3917a5aa25be7903cabde4303a66 by @nikic. Now, it seems to backfire in some cases:
Compile attached bcmp.ll (generated from llvm-project/libc/src/string/bcmp.cpp) like so:
$ clang -O3 -S bcmp.ll -o bcmp.s
Then I get:
Without the patch:
# %bb.26:
movdqu -16(%rdi,%rdx), %xmm0
movdqu -16(%rsi,%rdx), %xmm1
jmp .LBB1_34
:
:
:
# %bb.33:
movdqu (%rdi,%rdx), %xmm0
movdqu (%rsi,%rdx), %xmm1
.LBB1_34: # %.loopexit
With the patch:
# %bb.25:
addq %rdx, %rdi
addq $-16, %rdi
addq %rdx, %rsi
addq $-16, %rsi
jmp .LBB1_33
:
:
:
# %bb.32:
addq %rdx, %rdi
addq %rdx, %rsi
.LBB1_33: # %.loopexit.sink.split
movdqu (%rdi), %xmm0
movdqu (%rsi), %xmm1
Notice that the two load instructions sink just below the join point while the address calculation is left behind. This seems to result in worse performance and increased dynamic instruction counts.
Yeah, splitting apart GEP + load may be non-profitable, if it was previously a legal addressing mode. I wonder whether we should adjust sinking to not do this, or undo it in CGP -- the latter already does a lot of the "try to form legal addressing modes" optimizations.
@nikic The mentioned culprit ede27d8d391e3917a5aa25be7903cabde4303a66 introduces ~20% performance regressions for bcmp libc functions on Intel architectures (Skylake especially). The regressions can be measured using the libc benchmark in llvm-project/libc/benchmarks/LibcMemoryGoogleBenchmarkMain.cpp.
Can you consider reverting based on this?
Before we find a real solution, we should probably revert this one or put the change under a flag.
The change also fixes multiple other 20% regressions (see https://github.com/llvm/llvm-project/issues/94092 for the original context) and is an improvement on average. As such, I don't think reverting it makes sense, as we'd just trade one set of regressions for another, while regressing average performance and code size.
Or improve the patch with suggestion 1 (not do sinking if it split GEP + LOAD which are legal address mode) for now? Figuring out the right place to fix can take some time.
Out of curiosity, is anyone actively working on this?
If this issue is not being actively worked on I agree with @bgra8 and @david-xl that the offending commit should either be reverted or worked around.
For the record, we are seeing ~11% drop in one benchmark on AMDGPU.
I've put up two potential fixes for this issue.
https://github.com/llvm/llvm-project/pull/102318 implements what I suggested above, which is to not sink a load if it gets separate from a gep.
https://github.com/llvm/llvm-project/pull/102465 changes the heuristic on what we determine profitable to sink. The original bcmp test case here had a questionable sinking decision even independent of load+gep.
Based on the benchmarks linked there, I'd say both patches are mildly positive on average, but there are some big swings in either direction as well. I can already see the next person reporting a 20% regression after I land this :)
For the folks interested in this, I'd appreciate it if you could run both of those patches against whatever benchmarks you use (not just the regression case). I think it's pretty much unavoidable that something regresses when changing heuristics, what's important is that the average points in the correct direction.
I tested both patches on AMDGPU, only #102318 fixed the regressions we are seeing. It seems to have little negative impact in my tests.
@perlfu Thanks for testing! I'll go with that patch then.
Early heads-up: We bisected a failure in PGO generation on our Windows bots to this. We don't have details yet though.
https://issues.chromium.org/issues/369880653
Early heads-up: We bisected a failure in PGO generation on our Windows bots to this. We don't have details yet though.
https://issues.chromium.org/issues/369880653
False alarm. The patch caused updates to two PGO counters to be vectorized, using a register which got clobbered when calling some code generated by V8.