diff mbox series

riscv: hweight: relax assembly constraints

Message ID 20240523094325.3514-1-dqfext@gmail.com (mailing list archive)
State New
Headers show
Series riscv: hweight: relax assembly constraints | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR fail PR summary
conchuod/patch-1-test-1 success .github/scripts/patches/tests/build_rv32_defconfig.sh
conchuod/patch-1-test-2 fail .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh
conchuod/patch-1-test-3 fail .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh
conchuod/patch-1-test-6 success .github/scripts/patches/tests/checkpatch.sh
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh

Commit Message

Qingfang Deng May 23, 2024, 9:43 a.m. UTC
From: Qingfang Deng <qingfang.deng@siflower.com.cn>

rd and rs don't have to be the same.

Signed-off-by: Qingfang Deng <qingfang.deng@siflower.com.cn>
---
 arch/riscv/include/asm/arch_hweight.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Wang, Xiao W May 24, 2024, 1:01 a.m. UTC | #1
> -----Original Message-----
> From: Qingfang Deng <dqfext@gmail.com>
> Sent: Thursday, May 23, 2024 5:43 PM
> To: Paul Walmsley <paul.walmsley@sifive.com>; Palmer Dabbelt
> <palmer@dabbelt.com>; Albert Ou <aou@eecs.berkeley.edu>; linux-
> riscv@lists.infradead.org; linux-kernel@vger.kernel.org
> Cc: Wang, Xiao W <xiao.w.wang@intel.com>; Qingfang Deng
> <qingfang.deng@siflower.com.cn>
> Subject: [PATCH] riscv: hweight: relax assembly constraints
> 
> From: Qingfang Deng <qingfang.deng@siflower.com.cn>
> 
> rd and rs don't have to be the same.
> 
> Signed-off-by: Qingfang Deng <qingfang.deng@siflower.com.cn>
> ---
>  arch/riscv/include/asm/arch_hweight.h | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/riscv/include/asm/arch_hweight.h
> b/arch/riscv/include/asm/arch_hweight.h
> index 85b2c443823e..613769b9cdc9 100644
> --- a/arch/riscv/include/asm/arch_hweight.h
> +++ b/arch/riscv/include/asm/arch_hweight.h
> @@ -26,9 +26,9 @@ static __always_inline unsigned int
> __arch_hweight32(unsigned int w)
> 
>  	asm (".option push\n"
>  	     ".option arch,+zbb\n"
> -	     CPOPW "%0, %0\n"
> +	     CPOPW "%0, %1\n"
>  	     ".option pop\n"
> -	     : "+r" (w) : :);
> +	     : "=r" (w) : "r" (w) :);

The above code piece takes variable "w" as both input and output, so intuitively, the previous
patch made rs and rd the same.
Though rs and rd can be different, do you see performance difference with this change?
Or any analysis from assembly dump?

BRs,
Xiao

> 
>  	return w;
> 
> @@ -57,9 +57,9 @@ static __always_inline unsigned long
> __arch_hweight64(__u64 w)
> 
>  	asm (".option push\n"
>  	     ".option arch,+zbb\n"
> -	     "cpop %0, %0\n"
> +	     "cpop %0, %1\n"
>  	     ".option pop\n"
> -	     : "+r" (w) : :);
> +	     : "=r" (w) : "r" (w) :);
> 
>  	return w;
> 
> --
> 2.34.1
Qingfang Deng May 24, 2024, 6:05 a.m. UTC | #2
Hi,

On Fri, May 24, 2024 at 9:02 AM Wang, Xiao W <xiao.w.wang@intel.com> wrote:
>
>
>
> > -----Original Message-----
> > From: Qingfang Deng <dqfext@gmail.com>
> > Sent: Thursday, May 23, 2024 5:43 PM
> > To: Paul Walmsley <paul.walmsley@sifive.com>; Palmer Dabbelt
> > <palmer@dabbelt.com>; Albert Ou <aou@eecs.berkeley.edu>; linux-
> > riscv@lists.infradead.org; linux-kernel@vger.kernel.org
> > Cc: Wang, Xiao W <xiao.w.wang@intel.com>; Qingfang Deng
> > <qingfang.deng@siflower.com.cn>
> > Subject: [PATCH] riscv: hweight: relax assembly constraints
> >
> > From: Qingfang Deng <qingfang.deng@siflower.com.cn>
> >
> > rd and rs don't have to be the same.
> >
> > Signed-off-by: Qingfang Deng <qingfang.deng@siflower.com.cn>
> > ---
> >  arch/riscv/include/asm/arch_hweight.h | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/arch/riscv/include/asm/arch_hweight.h
> > b/arch/riscv/include/asm/arch_hweight.h
> > index 85b2c443823e..613769b9cdc9 100644
> > --- a/arch/riscv/include/asm/arch_hweight.h
> > +++ b/arch/riscv/include/asm/arch_hweight.h
> > @@ -26,9 +26,9 @@ static __always_inline unsigned int
> > __arch_hweight32(unsigned int w)
> >
> >       asm (".option push\n"
> >            ".option arch,+zbb\n"
> > -          CPOPW "%0, %0\n"
> > +          CPOPW "%0, %1\n"
> >            ".option pop\n"
> > -          : "+r" (w) : :);
> > +          : "=r" (w) : "r" (w) :);
>
> The above code piece takes variable "w" as both input and output, so intuitively, the previous
> patch made rs and rd the same.
> Though rs and rd can be different, do you see performance difference with this change?
> Or any analysis from assembly dump?

By making rs and rd different, we can save some `mv` instructions.

>
> BRs,
> Xiao
>
> >
> >       return w;
> >
> > @@ -57,9 +57,9 @@ static __always_inline unsigned long
> > __arch_hweight64(__u64 w)
> >
> >       asm (".option push\n"
> >            ".option arch,+zbb\n"
> > -          "cpop %0, %0\n"
> > +          "cpop %0, %1\n"
> >            ".option pop\n"
> > -          : "+r" (w) : :);
> > +          : "=r" (w) : "r" (w) :);
> >
> >       return w;
> >
> > --
> > 2.34.1
>
Wang, Xiao W May 24, 2024, 8:07 a.m. UTC | #3
> -----Original Message-----
> From: Qingfang Deng <dqfext@gmail.com>
> Sent: Friday, May 24, 2024 2:05 PM
> To: Wang, Xiao W <xiao.w.wang@intel.com>
> Cc: Paul Walmsley <paul.walmsley@sifive.com>; Palmer Dabbelt
> <palmer@dabbelt.com>; Albert Ou <aou@eecs.berkeley.edu>; linux-
> riscv@lists.infradead.org; linux-kernel@vger.kernel.org; Qingfang Deng
> <qingfang.deng@siflower.com.cn>
> Subject: Re: [PATCH] riscv: hweight: relax assembly constraints
> 
> Hi,
> 
> On Fri, May 24, 2024 at 9:02 AM Wang, Xiao W <xiao.w.wang@intel.com>
> wrote:
> >
> >
> >
> > > -----Original Message-----
> > > From: Qingfang Deng <dqfext@gmail.com>
> > > Sent: Thursday, May 23, 2024 5:43 PM
> > > To: Paul Walmsley <paul.walmsley@sifive.com>; Palmer Dabbelt
> > > <palmer@dabbelt.com>; Albert Ou <aou@eecs.berkeley.edu>; linux-
> > > riscv@lists.infradead.org; linux-kernel@vger.kernel.org
> > > Cc: Wang, Xiao W <xiao.w.wang@intel.com>; Qingfang Deng
> > > <qingfang.deng@siflower.com.cn>
> > > Subject: [PATCH] riscv: hweight: relax assembly constraints
> > >
> > > From: Qingfang Deng <qingfang.deng@siflower.com.cn>
> > >
> > > rd and rs don't have to be the same.
> > >
> > > Signed-off-by: Qingfang Deng <qingfang.deng@siflower.com.cn>
> > > ---
> > >  arch/riscv/include/asm/arch_hweight.h | 8 ++++----
> > >  1 file changed, 4 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/arch/riscv/include/asm/arch_hweight.h
> > > b/arch/riscv/include/asm/arch_hweight.h
> > > index 85b2c443823e..613769b9cdc9 100644
> > > --- a/arch/riscv/include/asm/arch_hweight.h
> > > +++ b/arch/riscv/include/asm/arch_hweight.h
> > > @@ -26,9 +26,9 @@ static __always_inline unsigned int
> > > __arch_hweight32(unsigned int w)
> > >
> > >       asm (".option push\n"
> > >            ".option arch,+zbb\n"
> > > -          CPOPW "%0, %0\n"
> > > +          CPOPW "%0, %1\n"
> > >            ".option pop\n"
> > > -          : "+r" (w) : :);
> > > +          : "=r" (w) : "r" (w) :);
> >
> > The above code piece takes variable "w" as both input and output, so
> intuitively, the previous
> > patch made rs and rd the same.
> > Though rs and rd can be different, do you see performance difference with
> this change?
> > Or any analysis from assembly dump?
> 
> By making rs and rd different, we can save some `mv` instructions.

OK, I guess in some cases, the original data needs be saved for later usage.
Then, we can relax the assembly constraint here and gives flexibility to compiler
For optimization.

It's better to start the patch tile with " riscv: lib:", maybe you can make the title as:
riscv: lib: relax assembly constraints in hweight

BRs,
Xiao

> 
> >
> > BRs,
> > Xiao
> >
> > >
> > >       return w;
> > >
> > > @@ -57,9 +57,9 @@ static __always_inline unsigned long
> > > __arch_hweight64(__u64 w)
> > >
> > >       asm (".option push\n"
> > >            ".option arch,+zbb\n"
> > > -          "cpop %0, %0\n"
> > > +          "cpop %0, %1\n"
> > >            ".option pop\n"
> > > -          : "+r" (w) : :);
> > > +          : "=r" (w) : "r" (w) :);
> > >
> > >       return w;
> > >
> > > --
> > > 2.34.1
> >
diff mbox series

Patch

diff --git a/arch/riscv/include/asm/arch_hweight.h b/arch/riscv/include/asm/arch_hweight.h
index 85b2c443823e..613769b9cdc9 100644
--- a/arch/riscv/include/asm/arch_hweight.h
+++ b/arch/riscv/include/asm/arch_hweight.h
@@ -26,9 +26,9 @@  static __always_inline unsigned int __arch_hweight32(unsigned int w)
 
 	asm (".option push\n"
 	     ".option arch,+zbb\n"
-	     CPOPW "%0, %0\n"
+	     CPOPW "%0, %1\n"
 	     ".option pop\n"
-	     : "+r" (w) : :);
+	     : "=r" (w) : "r" (w) :);
 
 	return w;
 
@@ -57,9 +57,9 @@  static __always_inline unsigned long __arch_hweight64(__u64 w)
 
 	asm (".option push\n"
 	     ".option arch,+zbb\n"
-	     "cpop %0, %0\n"
+	     "cpop %0, %1\n"
 	     ".option pop\n"
-	     : "+r" (w) : :);
+	     : "=r" (w) : "r" (w) :);
 
 	return w;