A one-line change in GCC resulted in a 12% speedup in one test and a 14% slowdown in another.

An engineer from Intel made a single-line change to the GCC compiler set, increasing the weight of incorrect branch prediction on x86 processors. This change improved the performance of the generated code by 12.7% during the 544.nab_r test when optimizations "-O2 -mtune=graniterapids" were applied for Intel Granite Rapids/Xeon 6 CPUs and by 12.1% when using the optimizations "-O2 -mtune=znver5" on AMD Zen5 CPUs.

Changing the weight from "COSTS_N_INSNS (2)" to "COSTS_N_INSNS (2) + 3" increases the significance of prediction errors from 2 to 5 conditional instructions, which better reflects the characteristics of instruction processing pipelines in modern CPUs, where prediction errors are more costly. This weight change leads to the compiler forcibly transforming "if" expressions into conditional commands without jumps, like CMOV, eliminating stalls caused by incorrect branch prediction that require pipeline state resets. Previously, the weight "COSTS_N_INSNS (2) + 3" was specified in GCC only for Intel Ice Lake and Alder Lake processors, but it is now set for the general profile of x86 processors.

Interestingly, after the patch was accepted, a regression emerged causing the "Hint" test to run 30% slower for one of the GCC developers when built with the options "-march=generic -mtune=znver5" and "-march=generic -mtune=graniterapids". The performance of the SPEC2017 and SPEC2026 tests remained unchanged after the modification. The presence of the regression has been confirmed by the author of the original commit, who reported a 14% slowdown in the Hint test when built with the options "-O2 -mtune=generic -march=x86-64-v3."

The slowdown is attributed to the fact that the Hint test code contains only one conditional structure, which GCC converts into a CMOV-based representation. This structure occurs infrequently (in 3% of cases), and its optimization does not impact performance. Meanwhile, the change in code generation mode resulted in a side effect that slowed down the execution of more frequently executed code.

Source: opennet.ru

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster