Skip to content

[PW_SID:966141] Optimize GCD performance on RISC-V by selecting implementation at runtime#441

Closed
linux-riscv-bot wants to merge 3 commits into
workflow__riscv__fixesfrom
pw966141
Closed

[PW_SID:966141] Optimize GCD performance on RISC-V by selecting implementation at runtime#441
linux-riscv-bot wants to merge 3 commits into
workflow__riscv__fixesfrom
pw966141

Conversation

@linux-riscv-bot
Copy link
Copy Markdown

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

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>
@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 105.49 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 959.71 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1250.59 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 20.60 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 21.50 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.72 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 67.79 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
kdoc
Desc: Detects for kdoc errors
Duration: 0.85 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
module-param
Desc: Detect module_param changes
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v2,1/3] lib/math/gcd: Use static key to select implementation at runtime"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.29 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 104.95 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 890.34 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1151.75 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 20.54 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 21.37 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.66 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 67.20 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
kdoc
Desc: Detects for kdoc errors
Duration: 0.85 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
module-param
Desc: Detect module_param changes
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v2,2/3] riscv: Optimize gcd() code size when CONFIG_RISCV_ISA_ZBB is disabled"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.29 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 105.28 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 959.85 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1251.56 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 20.44 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 21.61 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.71 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 66.60 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
kdoc
Desc: Detects for kdoc errors
Duration: 0.84 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
module-param
Desc: Detect module_param changes
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v2,3/3] riscv: Optimize gcd() performance on RISC-V without Zbb extension"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.54 seconds
Result: PASS

@linux-riscv-bot linux-riscv-bot deleted the pw966141 branch June 1, 2025 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants