| Issue |
238011
|
| Summary |
[X86] Code-layout-dependent persistent branch mispredictions on Intel (Skylake through Redwood Cove): proposal to pad colliding branches
|
| Labels |
|
| Assignees |
|
| Reporter |
ibogosavljevic
|
Some background: We have a code consisting of two nested loops and whose performance starkly improves (4 times to be precise) when we add a NOP between the jump of the inner and jump of the outer loop. We investigated the slowdown and it is related to how branch prediction is implemented on Intel's CPUs. Here we explain the issue, give a repro for the issue and explain how to detect the issue at the machine backend and add padding to avoid it. Feel free to contact me if you need any help in testing or more info.
The repro:
```
__attribute__((noinline, aligned(64))) static long run(const char *p, long n) {
asm volatile(".rept " STR(PAD) "\n\tnop\n\t.endr"); // shifts the code by PAD bytes (runs once)
long ops = 0;
while (!done) { // outer loop
++ops;
asm volatile(".nops 144" ::: "memory"); // forces a 5-byte (rel32) outer-loop jmp
int v = 0;
#pragma clang loop unroll(disable) vectorize(disable)
for (long i = 0; i < n; ++i) // inner loop: 2 iterations for "72"
v = 10 * v + p[i] - '0';
sink = v;
}
return ops;
```
This loop converts a string into a number. Depending of the size of `PAD` it will run faster or slower on emeraldrapids (the first architecture that we tested on).
What clang 23.1.4 generates (`-O2 -march=emeraldrapids -falign-loops=1, PAD=0`), with the 144 bytes of NOPs omitted:
```
1290: inc rax ← beginning of outer loop: ++ops
1293: mov [sink], ecx ; sink = v
1299: cmp [done], 0
12a0: jne exit ; while (!done)
... 144 bytes of nops ...
1337: test rsi, rsi
133a: jle 0x1290 ; n <= 0
1340: xor ecx, ecx ; v = 0
1342: xor edx, edx ; i = 0
1344: lea ecx, [rcx + 4*rcx] ← beginning of inner loop
1347: movsx r8d, byte [rdi + rdx] ; p[i]
134c: lea ecx, [r8 + 2*rcx - 0x30] ; v = 10*v + p[i] - '0'
1351: inc rdx
1354: cmp rsi, rdx
1357: jne 0x1344 ← jump to beginning of inner loop (backedge)
1359: jmp 0x1290 ← jump to beginning of outer loop (5 bytes)
```
Depending on the value of `PAD` (`PAD=0` or `PAD=1`), the two last branches (line 1357 and line 1359) will end up with the same _footprint_ in the branch predictor and essentially the branch on line 1357 will always be misspredicted. The speed loss is dramatic: we measured 4x loss of performance (9.15 ns/op vs 2.10 ns/op).
**What is a branch footprint?**
Intel's branch prediction mechanism is not officially publicly available, but people have reverse-engineered it, and we know how it works. It is explained in here [Half&Half: Demystifying Intel’s Directional Branch Predictors for Fast, Secure Partitioned Execution](https://ieeexplore.ieee.org/document/10179415).
The structure of branch prediction is complicated to explain and unnecessary to solve this problem. What we need to know is that to update branch prediction tables, every branch gets a footprint. A footprint is a 16 bit value calculated from the address of the last byte of the branch (we call it _B_ later in formulas) + lowest six bits of the destination of the branch (we call it _T_). The exact formula depends on the architecture:
Golden Cove family: Sunny Cove (Ice Lake), Golden Cove (Alder Lake, Sapphire Rapids), Raptor Cove (Emerald Rapids), Redwood Cove (Meteor Lake, Granite Rapids). We confirmed it on Ice Lake, Sapphire Rapids, Emerald Rapids, Rocket Lake (Cypress Cove) and Meteor Lake.
```
F[15:0] = { B15, B14, B13, B12, B11⊕T5, B2⊕T4, B1⊕T3, B0⊕T2, B10, B9, B8, B7, B6, B5, B4⊕T1, B3⊕T0 }
```
Skylake family: Skylake, Skylake-SP, Cascade Lake, Coffee Lake. We confirmed it on Skylake-SP, Cascade Lake (×3) and Coffee Lake.
```
F[15:0] = { B18, B17, B16, B15, B14, B13, B10, B9, B6, B5, B12⊕T5, B11⊕T4, B8⊕T3, B7⊕T2, B4⊕T1, B3⊕T0 }
```
If two branches have the same footprint, there is a large chance they will collide and cause performance degradation. You can measure the degradation by running the repro on the architectures that have the issue. It doesn't reproduce on AMD and ARM architectures.
**What instructions use branch prediction and can be patched**
All jumps instructions, including those that are unconditional, update the branch prediction history. It also applies to `call` and `ret` instructions as well. But, many of them cannot be processed for various reasons.
* Instructions with jump destination in registers cannot be obviously analyzed because the target is not known
* Instructions that will be patched at runtime cannot be processed because the target is unknown
For the purposes of this issue, I would limit processing and patching only to relative jumps that are within the same compilation unit. The jump address for jumps, calls and returns to other compilation units cannot be properly determined at compile time.
**Determining whether two branches have the same footprint?**
Determining whether two benchmarks have the same footprint will require some symbolic math. I am leaving the details to be worked out by the implementer, just adding some high-level information that ca be useful.
* We know the alignment of the compilation unit (16 bytes or 64 bytes)
* We know the relative address of the last byte of the jump instruction
Given two jumps A and B, we can determine if they have the same bits in the address of the last byte of the instruction. The closer they are in memory the more likely they will have it.
For relative within-compilation unit jumps we can calculate the last 6 bits of the target, and combining these values it is possible to calculate if two jumps A and B have the same or likely the same footprint and add alignment to fix it.
Again, not going to write the full algorithm, leaving it to the implementer.
**Repro**
[minimal.cpp](https://github.com/user-attachments/files/34278985/minimal.cpp)
Use the attached file:
```
LLVM-23.1.4-Linux-X64/bin/clang++ -O2 -march=x86-64-v3 -falign-loops=1 -DNOPS=153 -DPAD=0 minimal.cpp -o fast_both # fast on both
LLVM-23.1.4-Linux-X64/bin/clang++ -O2 -march=x86-64-v3 -falign-loops=1 -DNOPS=153 -DPAD=2 minimal.cpp -o slow_glc # slow on Golden Cove family only
LLVM-23.1.4-Linux-X64/bin/clang++-O2 -march=x86-64-v3 -falign-loops=1 -DNOPS=153 -DPAD=11 minimal.cpp -o slow_skl # slow on Skylake family only
```
Performance numbers I tested
```
┌───────────────────┬─────────────────────────────────┬────────────────────────────────┐
│ Binary │ Golden Cove family (icelake-01) │ Skylake family (cascadelake11) │
├───────────────────┼─────────────────────────────────┼────────────────────────────────┤
│ fast_both (PAD=0) │ 7.1 cyc/iter, 0.000 misses │ 12.0 cyc/iter, 0.000 misses │
├───────────────────┼─────────────────────────────────┼────────────────────────────────┤
│ slow_glc (PAD=2) │ 29.1 cyc/iter, 1.000 misses │ 8.3 cyc/iter, 0.000 misses │
├───────────────────┼─────────────────────────────────┼────────────────────────────────┤
│ slow_skl (PAD=11) │ 7.1 cyc/iter, 0.000 misses │ 32.1 cyc/iter, 1.01 misses │
└───────────────────┴─────────────────────────────────┴────────────────────────────────┘
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs