Linux MIPS Architecture development
 help / color / mirror / Atom feed
From: Tzu-Ching Yang <shingekinocore@gmail.com>
To: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: "Maciej W. Rozycki" <macro@orcam.me.uk>,
	linux-mips@vger.kernel.org, "Arnd Bergmann" <arnd@arndb.de>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org,
	"Gregory CLEMENT" <gregory.clement@bootlin.com>,
	"Théo Lebrun" <theo.lebrun@bootlin.com>,
	"Benoît Monin" <benoit.monin@bootlin.com>,
	"Jiaxun Yang" <jiaxun.yang@flygoat.com>
Subject: [PATCH v2] MIPS: Make cpu_has_mips64r6 constant on 64-bit r6 kernels
Date: Thu, 26 Nov 2026 13:53:24 +0800	[thread overview]
Message-ID: <20261126055324.3562889-1-shingekinocore@gmail.com> (raw)

On 64-bit r6 kernels, __fls() and __ffs() never use dclz: they always
run the C binary search, 37 and 39 instructions with GCC instead of 4
and 6. __ffs() calls __fls(), which uses dclz only if cpu_has_mips64
is a compile-time constant:

	if (BITS_PER_LONG == 64 && !__builtin_constant_p(word) &&
	    __builtin_constant_p(cpu_has_mips64) && cpu_has_mips64) {

cpu_has_mips64 ORs cpu_has_mips64r1/r2/r5/r6. On r6 kernels the r1, r2
and r5 terms fold to 0, but the r6 term tests cpu_data[0].isa_level, so
64-bit r6 kernels have never used dclz here.

The r1, r2 and r6 macros were run-time tests until commit 1aeba347b3a9
("MIPS: Hardcode cpu_has_mips* where target ISA allows") made r1 and r2
constant in 2018; it kept for r6 the flag test that tells MIPS64r6 from
MIPS32r6 CPUs. Commit a2ac81c6ef40 ("mips: Fix cpu_has_mips64r1/2
activation for MIPS32 CPUs") later added cpu_has_64bits to r1 and r2,
and left r6 alone. Every other cpu_has_mips32rN and cpu_has_mips64rN
macro is a constant on the kernels built for its ISA (the 64-bit ones
for cpu_has_mips64rN); only cpu_has_mips64r6 still tests the flag at run
time on 64-bit r6 kernels.

Define cpu_has_mips64r6 like cpu_has_mips64r1, cpu_has_mips64r2 and
cpu_has_mips64r5, as cpu_has_64bits && __isa_ge_or_flag(6,
MIPS_CPU_ISA_M64R6), and remove __isa_ge_and_flag(), which has no other
user. On 64-bit r6 kernels cpu_has_mips64r6 then folds to 1.

Because __fls() and __ffs() are inlined into find_*_bit(), cpumask
iteration and ilog2(), the functions of each 64-bit r6 defconfig kernel
(64r6, 64r6el, eyeq5, eyeq6 and eyeq6lplus) are then smaller than
without this patch: in total by at least 105340 and at most 108352
bytes with GCC 16, and by at least 124340 and at most 146692 bytes with
Clang 23, depending on the defconfig (scripts/bloat-o-meter -t). In the
64r6el and eyeq6 builds, the users of cpu_has_mips64r6 other than
__fls() shrink 3 functions, by 148 to 248 bytes in each build; the rest
of the reduction comes through __fls() and __ffs(). __fls(0) now
returns ~0UL instead of 0 on 64-bit r6 kernels: the -1 that the comment
above __fls() documents, and the value on 64-bit r2 kernels.

On 32-bit r6 kernels cpu_has_mips64r6 is the run-time test of
cpu_has_64bits (isa_level & MIPS_CPU_ISA_64BIT) and gives the same
result as the test of MIPS_CPU_ISA_M64R6 that it replaces, because
set_isa() gives r6 CPUs no flag of MIPS_CPU_ISA_64BIT except
MIPS_CPU_ISA_M64R6. On pre-r6 kernels cpu_has_mips64r6 was the constant
0 and becomes a run-time test of MIPS_CPU_ISA_M64R6, as
cpu_has_mips32r6 already is a run-time test of MIPS_CPU_ISA_M32R6. With
GCC 16, the functions of 64r2el_defconfig are 264 bytes larger in total
than without this patch, and the functions of 32r2el_defconfig 3352
bytes larger (scripts/bloat-o-meter -t).

Suggested-by: Maciej W. Rozycki <macro@orcam.me.uk>
Signed-off-by: Tzu-Ching Yang <shingekinocore@gmail.com>
---
Changes in v2:
- Define cpu_has_mips64r6 as cpu_has_64bits && __isa_ge_or_flag(6,
  MIPS_CPU_ISA_M64R6) and remove __isa_ge_and_flag(), as Maciej
  asked [1]; v1 had cpu_has_64bits && MIPS_ISA_REV >= 6.
- Commit message: describe this definition and its cost on pre-r6
  kernels, in place of the reasons v1 gave against
  __isa_ge_or_flag(); add Suggested-by.

v1: https://lore.kernel.org/r/20261030140430.1857741-1-shingekinocore@gmail.com/

Function sizes of v1 and v2 relative to mips-next (bfe97f12a0a6), in
bytes of bloat-o-meter -t:

  defconfig   compiler   v1        v2
  ---------   --------   -------   -------
  64r6el      GCC 16.2   -105340   -105340
  64r6el      Clang 23   -134840   -134840
  32r6el      GCC 16.2         0         0
  32r6el      Clang 23         0         0
  64r2el      GCC 16.2         0      +264
  64r2el      Clang 23         0      +168
  32r2el      GCC 16.2         0     +3352
  32r2el      Clang 23         0     -2500

- r6 kernels: v2 gives the same vmlinux as v1, byte for byte, for
  64r6, 64r6el, eyeq5, eyeq6, eyeq6lplus, 32r6el and malta_qemu_32r6
  with GCC 16.2 and Clang 23, and for 64r6el with GCC 8.1 to 15 and
  Clang 21 and 22. With Clang 17 to 20, which cannot assemble genex.S
  on this base, v2 gives the same objects as v1 for 64r6el. So the r6
  measurements in the notes of v1 are also those of v2.
- pre-r6 kernels: v1 generates the same code as mips-next, because
  cpu_has_mips64r6 stays the constant 0. With v2, cpu_has_mips64r6 is
  a run-time test, and the function sizes change as in the table. On
  32-bit pre-r6 kernels, each check of cpu_has_mips_r6
  (cpu_has_mips32r6 | cpu_has_mips64r6) tests MIPS_CPU_ISA_M32R6 with
  mips-next and with v1; with v2 it also tests cpu_has_64bits and
  MIPS_CPU_ISA_M64R6. The -2500 of 32r2el
  with Clang 23 comes with build_clear_pref() and
  build_copy_store_pref() in arch/mips/mm/page.c no longer being
  inlined.

Testing: no 64-bit r6 hardware was available, so the kernels were
only booted under QEMU. 64r6el_defconfig on the boston machine (I6400)
and malta_defconfig on the malta machine (24Kf), built with GCC 16.2
and Clang 23, pass the ffs and bitops KUnit suites and test_bitmap
with mips-next and with v2. W=1 builds of the configs in the table,
also with GCC 8.1 for 64r6el, 32r6el and 64r2el, and of loongson3,
cavium_octeon, malta and ip27 with GCC 16.2, show no new warning
against mips-next. A run of the ffs and bitops KUnit suites and
find_bit_benchmark on an EyeQ or Boston board, before and after, would
be welcome.

Base: the mips-next branch of mips/linux.git at bfe97f12a0a6; the
patch also applies to the mips-next head dfe3ece66313.

[1] https://lore.kernel.org/r/alpine.DEB.2.21.2610301515350.42255@angie.orcam.me.uk/

 arch/mips/include/asm/cpu-features.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/mips/include/asm/cpu-features.h b/arch/mips/include/asm/cpu-features.h
index 3f11e5218e6c..301158e704d0 100644
--- a/arch/mips/include/asm/cpu-features.h
+++ b/arch/mips/include/asm/cpu-features.h
@@ -57,7 +57,6 @@
  * Similarly allow for ISA level checks that take into account knowledge of the
  * ISA targeted by the kernel build, provided by MIPS_ISA_REV.
  */
-#define __isa_ge_and_flag(isa, flag)	((MIPS_ISA_REV >= (isa)) && __isa(flag))
 #define __isa_ge_or_flag(isa, flag)	((MIPS_ISA_REV >= (isa)) || __isa(flag))
 #define __isa_lt_and_flag(isa, flag)	((MIPS_ISA_REV < (isa)) && __isa(flag))
 #define __isa_range(ge, lt) \
@@ -317,7 +316,8 @@
 				 __isa_range_or_flag(5, 6, MIPS_CPU_ISA_M64R5))
 #endif
 #ifndef cpu_has_mips64r6
-# define cpu_has_mips64r6	__isa_ge_and_flag(6, MIPS_CPU_ISA_M64R6)
+# define cpu_has_mips64r6	(cpu_has_64bits && \
+				 __isa_ge_or_flag(6, MIPS_CPU_ISA_M64R6))
 #endif
 
 /*

base-commit: bfe97f12a0a64c3682be21a02404238e3b91e945
-- 
2.34.1


                 reply	other threads:[~2026-11-26  5:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126055324.3562889-1-shingekinocore@gmail.com \
    --to=shingekinocore@gmail.com \
    --cc=arnd@arndb.de \
    --cc=benoit.monin@bootlin.com \
    --cc=gregory.clement@bootlin.com \
    --cc=jiaxun.yang@flygoat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=macro@orcam.me.uk \
    --cc=theo.lebrun@bootlin.com \
    --cc=tsbogend@alpha.franken.de \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox