Skip to content

Commit 824e671

Browse files
wmi-11pwnall
authored andcommitted
Add a loop alignment directive to work around a performance regression.
We found LLVM upstream change at rL310792 degraded zippy benchmark by ~3%. Performance analysis showed the regression was caused by some side-effect. The incidental loop alignment change (from 32 bytes to 16 bytes) led to increase of branch miss prediction and caused the regression. The regression was reproducible on several intel micro-architectures, like sandybridge, haswell and skylake. Sadly we still don't have good understanding about the internal of intel branch predictor and cannot explain how the branch miss prediction increases when the loop alignment changes, so we cannot make a real fix here. The workaround solution in the patch is to add a directive, align the hot loop to 32 bytes, which can restore the performance. This is in order to unblock the flip of default compiler to LLVM.
1 parent 55924d1 commit 824e671

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎snappy.cc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,13 @@ class SnappyDecompressor {
685685
}
686686

687687
MAYBE_REFILL();
688+
// Add loop alignment directive. Without this directive, we observed
689+
// significant performance degradation on several intel architectures
690+
// in snappy benchmark built with LLVM. The degradation was caused by
691+
// increased branch miss prediction.
692+
#if defined(__clang__) && defined(__x86_64__)
693+
asm volatile (".p2align 5");
694+
#endif
688695
for ( ;; ) {
689696
const unsigned char c = *(reinterpret_cast<const unsigned char*>(ip++));
690697

0 commit comments

Comments
 (0)