Skip to contentSkip to conversation

riscv: hweight: relax assembly constraints

PATCH Superseded Qingfang Deng 2 participants 4 messages May 23, 2024 – May 24, 2024 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: hweight: relax assembly constraints

From: Qingfang Deng <[email protected]>

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

---
 arch/riscv/include/asm/arch_hweight.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/riscv/include/asm/arch_hweight.h +4 -4
index 85b2c443823e..613769b9cdc9 100644
@@ -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;
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 27 lines

-----Original Message-----
From: Qingfang Deng <[email protected]>
Sent: Thursday, May 23, 2024 5:43 PM
To: Paul Walmsley <[email protected]>; Palmer Dabbelt <[email protected]>; Albert Ou <[email protected]>; linux- [email protected]; [email protected]
Cc: Wang, Xiao W <[email protected]>; Qingfang Deng <[email protected]>
Subject: [PATCH] riscv: hweight: relax assembly constraints

From: Qingfang Deng <[email protected]>

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

---
 arch/riscv/include/asm/arch_hweight.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/riscv/include/asm/arch_hweight.h +0 -0
index 85b2c443823e..613769b9cdc9 100644

arch/riscv/include/asm/arch_hweight.h static __always_inline unsigned int · line 32

Quoted 9 lines
arch/riscv/include/asm/arch_hweight.h +2 -2
@@ -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

Quoted 18 lines
arch/riscv/include/asm/arch_hweight.h +2 -3
 	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

#2 lore raw

Hi,

Quoted 30 lines · Xiao W
Quoted 30 lines

-----Original Message-----
From: Qingfang Deng <[email protected]>
Sent: Thursday, May 23, 2024 5:43 PM
To: Paul Walmsley <[email protected]>; Palmer Dabbelt <[email protected]>; Albert Ou <[email protected]>; linux- [email protected]; [email protected]
Cc: Wang, Xiao W <[email protected]>; Qingfang Deng <[email protected]>
Subject: [PATCH] riscv: hweight: relax assembly constraints

From: Qingfang Deng <[email protected]>

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

---
 arch/riscv/include/asm/arch_hweight.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/riscv/include/asm/arch_hweight.h +0 -0
index 85b2c443823e..613769b9cdc9 100644
@@ -26,9 +26,9 @@ static __always_inline unsigned int
 __arch_hweight32(unsigned int w)
 
Quoted 12 lines

arch/riscv/include/asm/arch_hweight.h static __always_inline unsigned int __arch_hweight32(unsigned int w) · line 31

Quoted 7 lines
arch/riscv/include/asm/arch_hweight.h +2 -2
      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.

Quoted 23 lines

BRs,
Xiao

Quoted 18 lines
arch/riscv/include/asm/arch_hweight.h +2 -3
      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

#3 lore raw
Quoted 55 lines

-----Original Message-----
From: Qingfang Deng <[email protected]>
Sent: Friday, May 24, 2024 2:05 PM
To: Wang, Xiao W <[email protected]>
Cc: Paul Walmsley <[email protected]>; Palmer Dabbelt <[email protected]>; Albert Ou <[email protected]>; linux- [email protected]; [email protected]; Qingfang Deng <[email protected]>
Subject: Re: [PATCH] riscv: hweight: relax assembly constraints

Hi,

Quoted 30 lines · Xiao W
Quoted 30 lines

-----Original Message-----
From: Qingfang Deng <[email protected]>
Sent: Thursday, May 23, 2024 5:43 PM
To: Paul Walmsley <[email protected]>; Palmer Dabbelt <[email protected]>; Albert Ou <[email protected]>; linux- [email protected]; [email protected]
Cc: Wang, Xiao W <[email protected]>; Qingfang Deng <[email protected]>
Subject: [PATCH] riscv: hweight: relax assembly constraints

From: Qingfang Deng <[email protected]>

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

---
 arch/riscv/include/asm/arch_hweight.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/riscv/include/asm/arch_hweight.h +0 -0
index 85b2c443823e..613769b9cdc9 100644
@@ -26,9 +26,9 @@ static __always_inline unsigned int
 __arch_hweight32(unsigned int w)
 
Quoted 9 lines

arch/riscv/include/asm/arch_hweight.h static __always_inline unsigned int __arch_hweight32(unsigned int w) · line 31

Quoted 7 lines
arch/riscv/include/asm/arch_hweight.h +2 -2
      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

Quoted 7 lines

intuitively, the previous

Quoted 2 lines

patch made rs and rd the same.
Though rs and rd can be different, do you see performance difference with

this change?

Quoted 1 line

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

Quoted 24 lines
Quoted 24 lines

BRs,
Xiao

Quoted 18 lines
arch/riscv/include/asm/arch_hweight.h +2 -3
      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