[PW_SID:966141] Optimize GCD performance on RISC-V by selecting implementation at runtime#441
[PW_SID:966141] Optimize GCD performance on RISC-V by selecting implementation at runtime#441linux-riscv-bot wants to merge 3 commits into
Conversation
On platforms like RISC-V, the compiler may generate hardware FFS instructions even if the underlying CPU does not actually support them. Currently, the GCD implementation is chosen at compile time based on CONFIG_CPU_NO_EFFICIENT_FFS, which can result in suboptimal behavior on such systems. Introduce a static key, efficient_ffs_key, to enable runtime selection between the binary GCD (using ffs) and the odd-even GCD implementation. This allows the kernel to default to the faster binary GCD when FFS is efficient, while retaining the ability to fall back when needed. Co-developed-by: Yu-Chun Lin <eleanor15x@gmail.com> Signed-off-by: Yu-Chun Lin <eleanor15x@gmail.com> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The binary GCD implementation depends on efficient ffs(), which on RISC-V requires hardware support for the Zbb extension. When CONFIG_RISCV_ISA_ZBB is not enabled, the kernel will never use binary GCD, as runtime logic will always fall back to the odd-even implementation. To avoid compiling unused code and reduce code size, select CONFIG_CPU_NO_EFFICIENT_FFS when CONFIG_RISCV_ISA_ZBB is not set. $ ./scripts/bloat-o-meter ./lib/math/gcd.o.old ./lib/math/gcd.o.new add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-274 (-274) Function old new delta gcd 360 86 -274 Total: Before=384, After=110, chg -71.35% Co-developed-by: Yu-Chun Lin <eleanor15x@gmail.com> Signed-off-by: Yu-Chun Lin <eleanor15x@gmail.com> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The binary GCD implementation uses FFS (find first set), which benefits from hardware support for the ctz instruction, provided by the Zbb extension on RISC-V. Without Zbb, this results in slower software-emulated behavior. Previously, RISC-V always used the binary GCD, regardless of actual hardware support. This patch improves runtime efficiency by disabling the efficient_ffs_key static branch when Zbb is either not enabled in the kernel (config) or not supported on the executing CPU. This selects the odd-even GCD implementation, which is faster in the absence of efficient FFS. This change ensures the most suitable GCD algorithm is chosen dynamically based on actual hardware capabilities. Co-developed-by: Yu-Chun Lin <eleanor15x@gmail.com> Signed-off-by: Yu-Chun Lin <eleanor15x@gmail.com> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
|
Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension" |
PR for series 966141 applied to workflow__riscv__fixes
Name: Optimize GCD performance on RISC-V by selecting implementation at runtime
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=966141
Version: 2