linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [tip: core/core] ilog2 vs. GCC inlining heuristics
       [not found] <20201021132718.GB2176@tucnak>
@ 2020-11-20 12:33 ` tip-bot2 for Jakub Jelinek
  2020-11-20 12:41   ` Peter Zijlstra
  0 siblings, 1 reply; 2+ messages in thread
From: tip-bot2 for Jakub Jelinek @ 2020-11-20 12:33 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Jakub Jelinek, Peter Zijlstra (Intel), x86, linux-kernel

The following commit has been merged into the core/core branch of tip:

Commit-ID:     ecbd43f6728a5cf79c8b50ed326658e9181531b1
Gitweb:        https://git.kernel.org/tip/ecbd43f6728a5cf79c8b50ed326658e9181531b1
Author:        Jakub Jelinek <jakub@redhat.com>
AuthorDate:    Wed, 21 Oct 2020 15:27:18 +02:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Thu, 19 Nov 2020 11:26:18 +01:00

ilog2 vs. GCC inlining heuristics

Hi!

Based on the GCC PR97445 discussions, I'd like to propose following change,
which should significantly decrease the amount of code in inline functions
that use ilog2, but as I'm already two decades out of the Linux kernel
development, I'd appreciate if some kernel developer could try that (all
I have done is check that it gives the same results as before) and if it
works submit it for inclusion into the kernel?

Thanks.

Improve ilog2 for constant arguments

As discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97445
the const_ilog2 macro generates a lot of code which interferes badly
with GCC inlining heuristics, until it can be proven that the ilog2
argument can or can't be simplified into a constant.

It can be expressed using __builtin_clzll builtin which is supported
by GCC 3.4 and later and when used only in the __builtin_constant_p guarded
code it ought to always fold back to a constant.
Other compilers support the same builtin for many years too.

Other option would be to change the const_ilog2 macro, though as the
description says it is meant to be used also in C constant expressions,
and while GCC will fold it to constant with constant argument even in
those, perhaps it is better to avoid using extensions in that case.

Signed-off-by: Jakub Jelinek <jakub@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20201021132718.GB2176@tucnak
---
 include/linux/log2.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/log2.h b/include/linux/log2.h
index c619ec6..4307d34 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -156,7 +156,8 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 #define ilog2(n) \
 ( \
 	__builtin_constant_p(n) ?	\
-	const_ilog2(n) :		\
+	((n) < 2 ? 0 :			\
+	 63 - __builtin_clzll (n)) :	\
 	(sizeof(n) <= 4) ?		\
 	__ilog2_u32(n) :		\
 	__ilog2_u64(n)			\

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [tip: core/core] ilog2 vs. GCC inlining heuristics
  2020-11-20 12:33 ` [tip: core/core] ilog2 vs. GCC inlining heuristics tip-bot2 for Jakub Jelinek
@ 2020-11-20 12:41   ` Peter Zijlstra
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Zijlstra @ 2020-11-20 12:41 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-tip-commits, Jakub Jelinek, x86


Sorry, I typoed the branch name. I'll make this branch go away.

Anyway, Jacub, your patch seems to not upset the robots, so I'll go post
it properly for you.

On Fri, Nov 20, 2020 at 12:33:58PM -0000, tip-bot2 for Jakub Jelinek wrote:
> The following commit has been merged into the core/core branch of tip:
> 
> Commit-ID:     ecbd43f6728a5cf79c8b50ed326658e9181531b1
> Gitweb:        https://git.kernel.org/tip/ecbd43f6728a5cf79c8b50ed326658e9181531b1
> Author:        Jakub Jelinek <jakub@redhat.com>
> AuthorDate:    Wed, 21 Oct 2020 15:27:18 +02:00
> Committer:     Peter Zijlstra <peterz@infradead.org>
> CommitterDate: Thu, 19 Nov 2020 11:26:18 +01:00
> 
> ilog2 vs. GCC inlining heuristics
> 
> Hi!
> 
> Based on the GCC PR97445 discussions, I'd like to propose following change,
> which should significantly decrease the amount of code in inline functions
> that use ilog2, but as I'm already two decades out of the Linux kernel
> development, I'd appreciate if some kernel developer could try that (all
> I have done is check that it gives the same results as before) and if it
> works submit it for inclusion into the kernel?
> 
> Thanks.
> 
> Improve ilog2 for constant arguments
> 
> As discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97445
> the const_ilog2 macro generates a lot of code which interferes badly
> with GCC inlining heuristics, until it can be proven that the ilog2
> argument can or can't be simplified into a constant.
> 
> It can be expressed using __builtin_clzll builtin which is supported
> by GCC 3.4 and later and when used only in the __builtin_constant_p guarded
> code it ought to always fold back to a constant.
> Other compilers support the same builtin for many years too.
> 
> Other option would be to change the const_ilog2 macro, though as the
> description says it is meant to be used also in C constant expressions,
> and while GCC will fold it to constant with constant argument even in
> those, perhaps it is better to avoid using extensions in that case.
> 
> Signed-off-by: Jakub Jelinek <jakub@redhat.com>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> Link: https://lkml.kernel.org/r/20201021132718.GB2176@tucnak
> ---
>  include/linux/log2.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/log2.h b/include/linux/log2.h
> index c619ec6..4307d34 100644
> --- a/include/linux/log2.h
> +++ b/include/linux/log2.h
> @@ -156,7 +156,8 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
>  #define ilog2(n) \
>  ( \
>  	__builtin_constant_p(n) ?	\
> -	const_ilog2(n) :		\
> +	((n) < 2 ? 0 :			\
> +	 63 - __builtin_clzll (n)) :	\
>  	(sizeof(n) <= 4) ?		\
>  	__ilog2_u32(n) :		\
>  	__ilog2_u64(n)			\

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-20 12:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20201021132718.GB2176@tucnak>
2020-11-20 12:33 ` [tip: core/core] ilog2 vs. GCC inlining heuristics tip-bot2 for Jakub Jelinek
2020-11-20 12:41   ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).