llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[RISCV] Performance regression from change in getRegisterCostTableIndex

Open KavinTheG opened this issue 5 months ago • 18 comments

Noticed this regression on the BPI-F3 (rva22u64_v): http://lnt.llvm.org/db_default/v4/nts/profile/1438/121/117

Image

It looks like that the code generated are nearly identical, but the s0 and s2 registers are swapped. I have traced this change to this commit https://github.com/llvm/llvm-project/pull/201501.

Here is the diff I have been able to produce locally.

diff --git a/pre/fib2.s b/fib2.s
index a727287..57c5b31 100644
--- a/pre/fib2.s
+++ b/fib2.s
@@ -19,8 +19,8 @@ fib:                                    # @fib
        .cfi_offset s1, -24
        .cfi_offset s2, -32
        .cfi_remember_state
-       li      s2, 2
-       bgeu    a0, s2, .LBB0_2
+       li      s0, 2
+       bgeu    a0, s0, .LBB0_2
 # %bb.1:
        li      a0, 1
        ld      ra, 24(sp)                      # 8-byte Folded Reload
@@ -37,14 +37,14 @@ fib:                                    # @fib
 .LBB0_2:                                # %if.else.preheader
        .cfi_restore_state
        li      s1, 0
-       addi    s0, a0, 1
+       addi    s2, a0, 1
 .LBB0_3:                                # %if.else
                                         # =>This Inner Loop Header: Depth=1
-       addi    a0, s0, -3
+       addi    a0, s2, -3
        call    fib
-       addi    s0, s0, -1
+       addi    s2, s2, -1
        add     s1, s1, a0
-       bltu    s2, s0, .LBB0_3
+       bltu    s0, s2, .LBB0_3
 # %bb.4:                                # %return.loopexit
        addi    a0, s1, 1
        ld      ra, 24(sp)                      # 8-byte Folded Reload
@@ -103,6 +103,6 @@ main:                                   # @main
        .asciz  "%ld\n"
        .size   .L.str, 5

-       .ident  "clang version 23.0.0git ([email protected]:KavinTheG/llvm-project.git 5e7ec28c5e3a70588bbe9368d3816b009cf3670f)"
+       .ident  "clang version 23.0.0git ([email protected]:KavinTheG/llvm-project.git 86c073af08bac41fe0349eee2bc0279e5f88d6d9)"
        .section        ".note.GNU-stack","",@progbits
        .addrsig

This is the command I use to compile the code, which I am fairly confident is the same used in these benchmarks (except without -flto, which I don't think is relevant here).

 $LBD/bin/clang --target=riscv64-unknown-linux-gnu \
    --sysroot="$RISCV_SYSROOT" \
    -march=rva22u64_v \
    -O3 \
    -S fib2.c -o fib2.s

I've only taken a cursory glance at the benchmarks below but I believe the regressions there stem from the same commit.

http://lnt.llvm.org/db_default/v4/nts/profile/1422/141/2 http://lnt.llvm.org/db_default/v4/nts/profile/1354/141/2

KavinTheG avatar Jun 17 '26 22:06 KavinTheG

@llvm/issue-subscribers-backend-risc-v

Author: Kavin Gnanapandithan (KavinTheG)

Noticed this regression on the BPI-F3 (rva22u64_v): http://lnt.llvm.org/db_default/v4/nts/profile/1438/121/117

<img width="2517" height="843" alt="Image" src="https://github.com/user-attachments/assets/c7046352-0e1d-47bc-9a79-c9627d5e87a9" />

It looks like that the code generated is the identical, but the s0 and s2 registers are swapped. I have traced this change to this commit https://github.com/llvm/llvm-project/pull/201501.

Here is the diff I have been able to produce locally.

diff --git a/pre/fib2.s b/fib2.s
index a727287..57c5b31 100644
--- a/pre/fib2.s
+++ b/fib2.s
@@ -19,8 +19,8 @@ fib:                                    # @<!-- -->fib
        .cfi_offset s1, -24
        .cfi_offset s2, -32
        .cfi_remember_state
-       li      s2, 2
-       bgeu    a0, s2, .LBB0_2
+       li      s0, 2
+       bgeu    a0, s0, .LBB0_2
 # %bb.1:
        li      a0, 1
        ld      ra, 24(sp)                      # 8-byte Folded Reload
@@ -37,14 +37,14 @@ fib:                                    # @<!-- -->fib
 .LBB0_2:                                # %if.else.preheader
        .cfi_restore_state
        li      s1, 0
-       addi    s0, a0, 1
+       addi    s2, a0, 1
 .LBB0_3:                                # %if.else
                                         # =&gt;This Inner Loop Header: Depth=1
-       addi    a0, s0, -3
+       addi    a0, s2, -3
        call    fib
-       addi    s0, s0, -1
+       addi    s2, s2, -1
        add     s1, s1, a0
-       bltu    s2, s0, .LBB0_3
+       bltu    s0, s2, .LBB0_3
 # %bb.4:                                # %return.loopexit
        addi    a0, s1, 1
        ld      ra, 24(sp)                      # 8-byte Folded Reload
@@ -103,6 +103,6 @@ main:                                   # @<!-- -->main
        .asciz  "%ld\n"
        .size   .L.str, 5

-       .ident  "clang version 23.0.0git (git@<!-- -->github.com:KavinTheG/llvm-project.git 5e7ec28c5e3a70588bbe9368d3816b009cf3670f)"
+       .ident  "clang version 23.0.0git (git@<!-- -->github.com:KavinTheG/llvm-project.git 86c073af08bac41fe0349eee2bc0279e5f88d6d9)"
        .section        ".note.GNU-stack","",@<!-- -->progbits
        .addrsig

This is the command I use to compile the code, which I am fairly confident is the same used in these benchmarks (except without -flto, which I don't think is relevant here).

 $LBD/bin/clang --target=riscv64-unknown-linux-gnu \
    --sysroot="$RISCV_SYSROOT" \
    -march=rva22u64_v \
    -O3 \
    -S fib2.c -o fib2.s

I've only taken a cursory glance at the benchmarks below but I believe the regressions stem from the same commit.

http://lnt.llvm.org/db_default/v4/nts/profile/1422/141/2 http://lnt.llvm.org/db_default/v4/nts/profile/1354/141/2

If I'm reading the code diff right, the number instructions didn't change nor did the number of bytes in their encodings?

topperc avatar Jun 17 '26 23:06 topperc

Interesting observation. Are you able to get data from the K3 for this? At least in this example I'm not seeing anything obvious as to why it would regress, maybe a quirk with the K1? Or jitter?

bababuck avatar Jun 17 '26 23:06 bababuck

nor did the number of bytes in their encodings?

I didn't check this, I'll keep investigating.

Are you able to get data from the K3 for this?

I don't have the actual hardware, but I'll try seeing if there's a difference in encoding bytes locally.

KavinTheG avatar Jun 17 '26 23:06 KavinTheG

Maybe the addi s2, s2, -1 after the recursive call has to wait longer after because s2 is restored last inside the function?

topperc avatar Jun 17 '26 23:06 topperc

Maybe the addi s2, s2, -1 after the recursive call has to wait longer after because s2 is restored last inside the function?

Would that be able to cause every basic block across the function to be slower? Or maybe that's a sampling artifact of some sort?

bababuck avatar Jun 17 '26 23:06 bababuck

Maybe the addi s2, s2, -1 after the recursive call has to wait longer after because s2 is restored last inside the function?

Would that be able to cause every basic block across the function to be slower? Or maybe that's a sampling artifact of some sort?

I wouldn't think so. I'm just trying to speculate how a different register could change things.

topperc avatar Jun 17 '26 23:06 topperc

nor did the number of bytes in their encodings?

Not sure if this is what you meant, but I have the .s files from the pre-commit build and post-commit build, compiled them into .o object files and used llvm-objdump with the -d flag to compare their output.

diff --git a/pre_bytes.txt b/post_bytes.txt
index e1ce274..dc87cbc 100644
--- a/pre_bytes.txt
+++ b/post_bytes.txt
@@ -1,5 +1,5 @@

-pre_fib2.o:    file format elf64-littleriscv
+post_fib2.o:   file format elf64-littleriscv

 Disassembly of section .text:

@@ -9,8 +9,8 @@ Disassembly of section .text:
        4: e822                 sd      s0, 0x10(sp)
        6: e426                 sd      s1, 0x8(sp)
        8: e04a                 sd      s2, 0x0(sp)
-       a: 4909                 li      s2, 0x2
-       c: 01257063             bgeu    a0, s2, 0xc <fib+0xc>
+       a: 4409                 li      s0, 0x2
+       c: 00857063             bgeu    a0, s0, 0xc <fib+0xc>
       10: 4505                 li      a0, 0x1
       12: 60e2                 ld      ra, 0x18(sp)
       14: 6442                 ld      s0, 0x10(sp)
@@ -21,15 +21,15 @@ Disassembly of section .text:

 000000000000001e <.LBB0_2>:
       1e: 4481                 li      s1, 0x0
-      20: 00150413             addi    s0, a0, 0x1
+      20: 00150913             addi    s2, a0, 0x1

 0000000000000024 <.LBB0_3>:
-      24: ffd40513             addi    a0, s0, -0x3
+      24: ffd90513             addi    a0, s2, -0x3
       28: 00000097             auipc   ra, 0x0
       2c: 000080e7             jalr    ra <.LBB0_3+0x4>
-      30: 147d                 addi    s0, s0, -0x1
+      30: 197d                 addi    s2, s2, -0x1
       32: 94aa                 add     s1, s1, a0
-      34: 00896063             bltu    s2, s0, 0x34 <.LBB0_3+0x10>
+      34: 01246063             bltu    s0, s2, 0x34 <.LBB0_3+0x10>
       38: 00148513             addi    a0, s1, 0x1
       3c: 60e2                 ld      ra, 0x18(sp)
       3e: 6442                 ld      s0, 0x10(sp)

Doesn't seem like the encoding changed if that's what you meant.

KavinTheG avatar Jun 17 '26 23:06 KavinTheG

diff --git a/pre-queens.s b/queens.s
index 26bba55..63b8558 100644
--- a/pre-queens.s
+++ b/queens.s
@@ -36,26 +36,26 @@ main:                                   # @main
 	.cfi_offset s9, -88
 	.cfi_offset s10, -96
 	.cfi_offset s11, -104
-	mv	s4, a1
-	mv	s5, a0
+	mv	s0, a1
+	mv	s1, a0
 	ld	a0, 0(a1)
 .Lpcrel_hi0:
 	auipc	a1, %pcrel_hi(.L_MergedGlobals)
 	li	a2, 7
-	addi	s1, a1, %pcrel_lo(.Lpcrel_hi0)
+	addi	s5, a1, %pcrel_lo(.Lpcrel_hi0)
 	slli	a2, a2, 33
 .Lpcrel_hi1:
-	auipc	s7, %pcrel_hi(printing)
+	auipc	s6, %pcrel_hi(printing)
 	addi	a2, a2, 1
-	li	s9, 1
-	sd	a2, 0(s1)
-	sd	a0, 24(s1)
-	sw	zero, %pcrel_lo(.Lpcrel_hi1)(s7)
+	li	s7, 1
+	sd	a2, 0(s5)
+	sd	a0, 24(s5)
+	sw	zero, %pcrel_lo(.Lpcrel_hi1)(s6)
 	li	a1, 14
-	bge	s9, s5, .LBB0_14
+	bge	s7, s1, .LBB0_14
 # %bb.1:                                # %for.body.preheader
 	li	s8, 45
-	li	s0, 97
+	li	s9, 97
 	li	s10, 99
 .Lpcrel_hi3:
 	auipc	a0, %pcrel_hi(.L.str.2)
@@ -69,44 +69,44 @@ main:                                   # @main
 .LBB0_3:                                # %for.inc
                                         #   in Loop: Header=BB0_4 Depth=1
 	addi	s2, s2, 1
-	beq	s2, s5, .LBB0_26
+	beq	s2, s1, .LBB0_26
 .LBB0_4:                                # %for.body
                                         # =>This Loop Header: Depth=1
                                         #     Child Loop BB0_7 Depth 2
-	sh3add	a0, s2, s4
-	ld	s6, 0(a0)
-	lbu	a0, 0(s6)
+	sh3add	a0, s2, s0
+	ld	s4, 0(a0)
+	lbu	a0, 0(s4)
 	bne	a0, s8, .LBB0_10
 # %bb.5:                                # %while.cond.preheader
                                         #   in Loop: Header=BB0_4 Depth=1
-	addi	a0, s6, 1
+	addi	a0, s4, 1
 	j	.LBB0_7
 .LBB0_6:                                # %sw.bb5
                                         #   in Loop: Header=BB0_7 Depth=2
-	sw	s9, 0(s1)
+	sw	s7, 0(s5)
 	addi	a0, a0, 1
 .LBB0_7:                                # %while.cond
                                         #   Parent Loop BB0_4 Depth=1
                                         # =>  This Inner Loop Header: Depth=2
 	lbu	a2, 0(a0)
-	beq	a2, s0, .LBB0_6
+	beq	a2, s9, .LBB0_6
 # %bb.8:                                # %while.cond
                                         #   in Loop: Header=BB0_7 Depth=2
 	bne	a2, s10, .LBB0_2
 # %bb.9:                                # %sw.bb
                                         #   in Loop: Header=BB0_7 Depth=2
-	sw	zero, %pcrel_lo(.Lpcrel_hi1)(s7)
+	sw	zero, %pcrel_lo(.Lpcrel_hi1)(s6)
 	j	.LBB0_6
 .LBB0_10:                               # %if.else
                                         #   in Loop: Header=BB0_4 Depth=1
-	mv	a0, s6
+	mv	a0, s4
 	mv	a1, s3
-	addi	a2, s1, 4
+	addi	a2, s5, 4
 	call	__isoc99_sscanf
-	bne	a0, s9, .LBB0_28
+	bne	a0, s7, .LBB0_28
 # %bb.11:                               # %if.end
                                         #   in Loop: Header=BB0_4 Depth=1
-	lw	a1, 4(s1)
+	lw	a1, 4(s5)
 	blez	a1, .LBB0_29
 # %bb.12:                               # %if.end18
                                         #   in Loop: Header=BB0_4 Depth=1
@@ -116,7 +116,7 @@ main:                                   # @main
 	auipc	a0, %got_pcrel_hi(stderr)
 	ld	a0, %pcrel_lo(.Lpcrel_hi11)(a0)
 	ld	a0, 0(a0)
-	ld	a2, 24(s1)
+	ld	a2, 24(s5)
 .Lpcrel_hi12:
 	auipc	a1, %pcrel_hi(.L.str.5)
 	addi	a1, a1, %pcrel_lo(.Lpcrel_hi12)
@@ -127,8 +127,8 @@ main:                                   # @main
 .LBB0_14:                               # %if.end29.thread
 .Lpcrel_hi2:
 	auipc	a0, %pcrel_hi(.L.str.8)
-	sw	a1, 8(s1)
-	sw	a1, 12(s1)
+	sw	a1, 8(s5)
+	sw	a1, 12(s5)
 	addi	a2, a0, %pcrel_lo(.Lpcrel_hi2)
 .LBB0_15:                               # %vector.ph
 .Lpcrel_hi15:
@@ -139,18 +139,18 @@ main:                                   # @main
 	call	printf
 .Lpcrel_hi16:
 	auipc	a0, %got_pcrel_hi(stdout)
-	ld	s2, %pcrel_lo(.Lpcrel_hi16)(a0)
-	ld	a0, 0(s2)
+	ld	s0, %pcrel_lo(.Lpcrel_hi16)(a0)
+	ld	a0, 0(s0)
 	call	fflush
 	li	a0, 0
-	sd	zero, 16(s1)
+	sd	zero, 16(s5)
 	li	a1, 100
 	li	a2, 101
 .LBB0_16:                               # %vector.body
                                         # =>This Inner Loop Header: Depth=1
 	vsetvli	a3, a1, e32, m2, ta, ma
 	vmv.v.x	v8, a2
-	sh2add	a4, a0, s1
+	sh2add	a4, a0, s5
 	addi	a4, a4, 32
 	sub	a1, a1, a3
 	vse32.v	v8, (a4)
@@ -167,7 +167,7 @@ main:                                   # @main
                                         # =>This Inner Loop Header: Depth=1
 	vsetvli	a4, a1, e32, m2, ta, ma
 	vmv.v.x	v8, a2
-	sh2add	a5, a0, s1
+	sh2add	a5, a0, s5
 	addi	a5, a5, 832
 	vse32.v	v8, (a5)
 	sh2add	a5, a0, a3
@@ -178,16 +178,16 @@ main:                                   # @main
 # %bb.19:                               # %for.end53
 	li	a0, 0
 	call	find
-	lw	a0, %pcrel_lo(.Lpcrel_hi1)(s7)
-	ld	a1, 16(s1)
+	lw	a0, %pcrel_lo(.Lpcrel_hi1)(s6)
+	ld	a1, 16(s5)
 	beqz	a0, .LBB0_22
 # %bb.20:                               # %for.end53
 	beqz	a1, .LBB0_22
 # %bb.21:                               # %if.then56
-	ld	a1, 0(s2)
+	ld	a1, 0(s0)
 	li	a0, 10
 	call	putc
-	ld	a1, 16(s1)
+	ld	a1, 16(s5)
 .LBB0_22:                               # %if.end58
 	li	a0, 1
 	bne	a1, a0, .LBB0_25
@@ -203,14 +203,14 @@ main:                                   # @main
 	auipc	a0, %got_pcrel_hi(stderr)
 	ld	s0, %pcrel_lo(.Lpcrel_hi4)(a0)
 	ld	a0, 0(s0)
-	ld	a2, 24(s1)
+	ld	a2, 24(s5)
 .Lpcrel_hi5:
 	auipc	a1, %pcrel_hi(.L.str.1)
 	addi	a1, a1, %pcrel_lo(.Lpcrel_hi5)
-	mv	a3, s6
+	mv	a3, s4
 	call	fprintf
 	ld	a0, 0(s0)
-	ld	a2, 24(s1)
+	ld	a2, 24(s5)
 .Lpcrel_hi6:
 	auipc	a1, %pcrel_hi(.L.str)
 	addi	a1, a1, %pcrel_lo(.Lpcrel_hi6)
@@ -226,8 +226,8 @@ main:                                   # @main
 	call	exit
 .LBB0_26:                               # %if.end29
 	li	a0, 1
-	sw	a1, 8(s1)
-	sw	a1, 12(s1)
+	sw	a1, 8(s5)
+	sw	a1, 12(s5)
 	bltu	a0, a1, .LBB0_30
 # %bb.27:                               # %if.end29
 .Lpcrel_hi14:
@@ -239,11 +239,11 @@ main:                                   # @main
 	auipc	a0, %got_pcrel_hi(stderr)
 	ld	a0, %pcrel_lo(.Lpcrel_hi7)(a0)
 	ld	a0, 0(a0)
-	ld	a2, 24(s1)
+	ld	a2, 24(s5)
 .Lpcrel_hi8:
 	auipc	a1, %pcrel_hi(.L.str.3)
 	addi	a1, a1, %pcrel_lo(.Lpcrel_hi8)
-	mv	a3, s6
+	mv	a3, s4
 	call	fprintf
 	li	a0, -1
 	call	exit
@@ -252,7 +252,7 @@ main:                                   # @main
 	auipc	a0, %got_pcrel_hi(stderr)
 	ld	a0, %pcrel_lo(.Lpcrel_hi9)(a0)
 	ld	a0, 0(a0)
-	ld	a2, 24(s1)
+	ld	a2, 24(s5)
 .Lpcrel_hi10:
 	auipc	a1, %pcrel_hi(.L.str.4)
 	addi	a1, a1, %pcrel_lo(.Lpcrel_hi10)
@@ -297,74 +297,74 @@ find:                                   # @find
 	.cfi_offset s7, -72
 	.cfi_offset s8, -80
 .Lpcrel_hi20:
-	auipc	s3, %pcrel_hi(.L_MergedGlobals)
-	addi	s3, s3, %pcrel_lo(.Lpcrel_hi20)
-	lw	a1, 4(s3)
+	auipc	s2, %pcrel_hi(.L_MergedGlobals)
+	addi	s2, s2, %pcrel_lo(.Lpcrel_hi20)
+	lw	a1, 4(s2)
 	bne	a0, a1, .LBB1_5
 # %bb.1:                                # %if.then
-	ld	a0, 16(s3)
+	ld	a0, 16(s2)
 .Lpcrel_hi21:
 	auipc	a1, %pcrel_hi(printing)
 	lw	a1, %pcrel_lo(.Lpcrel_hi21)(a1)
 	addi	a0, a0, 1
-	sd	a0, 16(s3)
+	sd	a0, 16(s2)
 	beqz	a1, .LBB1_3
 # %bb.2:                                # %if.then1
 	call	pboard
 .LBB1_3:                                # %if.end
-	lw	a0, 0(s3)
+	lw	a0, 0(s2)
 	bnez	a0, .LBB1_12
 # %bb.4:                                # %if.then3
 	call	exit
 .LBB1_5:                                # %if.else
-	mv	s5, a0
-	lw	a0, 8(s3)
+	mv	s0, a0
+	lw	a0, 8(s2)
 	blez	a0, .LBB1_12
 # %bb.6:                                # %for.body.lr.ph
-	li	s6, 0
-	addw	a1, a0, s5
+	li	s3, 0
+	addw	a1, a0, s0
 .Lpcrel_hi22:
 	auipc	a2, %pcrel_hi(fordiag)
-	sh2add	s4, s5, s3
-	sh2add	a1, a1, s3
+	sh2add	s4, s0, s2
+	sh2add	a1, a1, s2
 	addi	a2, a2, %pcrel_lo(.Lpcrel_hi22)
-	addiw	s2, s5, 1
-	sh2add	s0, s5, a2
-	addi	s7, a1, 828
-	addi	s1, s3, 32
+	addiw	s1, s0, 1
+	sh2add	s5, s0, a2
+	addi	s6, a1, 828
+	addi	s7, s2, 32
 	li	s8, 101
 	j	.LBB1_8
 .LBB1_7:                                # %for.inc
                                         #   in Loop: Header=BB1_8 Depth=1
-	addiw	s6, s6, 1
-	addi	s1, s1, 4
-	addi	s0, s0, 4
-	addi	s7, s7, -4
-	bge	s6, a0, .LBB1_12
+	addiw	s3, s3, 1
+	addi	s7, s7, 4
+	addi	s5, s5, 4
+	addi	s6, s6, -4
+	bge	s3, a0, .LBB1_12
 .LBB1_8:                                # %for.body
                                         # =>This Inner Loop Header: Depth=1
-	lw	a1, 0(s1)
-	blt	a1, s5, .LBB1_7
+	lw	a1, 0(s7)
+	blt	a1, s0, .LBB1_7
 # %bb.9:                                # %land.lhs.true
                                         #   in Loop: Header=BB1_8 Depth=1
-	lw	a1, 0(s0)
-	blt	a1, s5, .LBB1_7
+	lw	a1, 0(s5)
+	blt	a1, s0, .LBB1_7
 # %bb.10:                               # %land.lhs.true10
                                         #   in Loop: Header=BB1_8 Depth=1
-	lw	a1, 0(s7)
-	blt	a1, s5, .LBB1_7
+	lw	a1, 0(s6)
+	blt	a1, s0, .LBB1_7
 # %bb.11:                               # %if.then12
                                         #   in Loop: Header=BB1_8 Depth=1
-	sw	s6, 432(s4)
-	sw	s5, 0(s7)
-	sw	s5, 0(s0)
-	sw	s5, 0(s1)
-	mv	a0, s2
+	sw	s3, 432(s4)
+	sw	s0, 0(s6)
+	sw	s0, 0(s5)
+	sw	s0, 0(s7)
+	mv	a0, s1
 	call	find
+	sw	s8, 0(s6)
 	sw	s8, 0(s7)
-	sw	s8, 0(s1)
-	lw	a0, 8(s3)
-	sw	s8, 0(s0)
+	lw	a0, 8(s2)
+	sw	s8, 0(s5)
 	j	.LBB1_7
 .LBB1_12:                               # %if.end20
 	ld	ra, 72(sp)                      # 8-byte Folded Reload
@@ -415,18 +415,18 @@ pboard:                                 # @pboard
 	.cfi_offset s3, -40
 	.cfi_offset s4, -48
 .Lpcrel_hi23:
-	auipc	s3, %pcrel_hi(.L_MergedGlobals)
-	addi	s3, s3, %pcrel_lo(.Lpcrel_hi23)
-	lw	a0, 0(s3)
+	auipc	s0, %pcrel_hi(.L_MergedGlobals)
+	addi	s0, s0, %pcrel_lo(.Lpcrel_hi23)
+	lw	a0, 0(s0)
 	beqz	a0, .LBB2_2
 # %bb.1:                                # %if.then
-	ld	a1, 16(s3)
+	ld	a1, 16(s0)
 .Lpcrel_hi24:
 	auipc	a0, %pcrel_hi(.L.str.12)
 	addi	a0, a0, %pcrel_lo(.Lpcrel_hi24)
 	call	printf
 .LBB2_2:                                # %if.end
-	lw	a0, 12(s3)
+	lw	a0, 12(s0)
 	blez	a0, .LBB2_10
 # %bb.3:                                # %for.cond1.preheader.preheader
 .Lpcrel_hi25:
@@ -439,42 +439,42 @@ pboard:                                 # @pboard
 	ld	a1, 0(s1)
 	li	a0, 10
 	call	putc
-	lw	a0, 12(s3)
+	lw	a0, 12(s0)
 	addi	s2, s2, 1
 	bge	s2, a0, .LBB2_10
 .LBB2_5:                                # %for.cond1.preheader
                                         # =>This Loop Header: Depth=1
                                         #     Child Loop BB2_8 Depth 2
-	lw	a0, 8(s3)
+	lw	a0, 8(s0)
 	blez	a0, .LBB2_4
 # %bb.6:                                # %for.body3.lr.ph
                                         #   in Loop: Header=BB2_5 Depth=1
-	sh2add	s4, s2, s3
-	li	s0, 0
+	sh2add	s3, s2, s0
+	li	s4, 0
 	j	.LBB2_8
 .LBB2_7:                                # %for.body3
                                         #   in Loop: Header=BB2_8 Depth=2
 	li	a0, 45
 	ld	a1, 0(s1)
 	call	putc
-	lw	a0, 8(s3)
-	addiw	s0, s0, 1
-	bge	s0, a0, .LBB2_4
+	lw	a0, 8(s0)
+	addiw	s4, s4, 1
+	bge	s4, a0, .LBB2_4
 .LBB2_8:                                # %for.body3
                                         #   Parent Loop BB2_5 Depth=1
                                         # =>  This Inner Loop Header: Depth=2
 	ld	a1, 0(s1)
 	li	a0, 32
 	call	putc
-	lw	a0, 432(s4)
-	bne	s0, a0, .LBB2_7
+	lw	a0, 432(s3)
+	bne	s4, a0, .LBB2_7
 # %bb.9:                                #   in Loop: Header=BB2_8 Depth=2
 	li	a0, 81
 	ld	a1, 0(s1)
 	call	putc
-	lw	a0, 8(s3)
-	addiw	s0, s0, 1
-	blt	s0, a0, .LBB2_8
+	lw	a0, 8(s0)
+	addiw	s4, s4, 1
+	blt	s4, a0, .LBB2_8
 	j	.LBB2_4
 .LBB2_10:                               # %for.end13
 .Lpcrel_hi26:
@@ -608,7 +608,7 @@ queen = .L_MergedGlobals+432
 	.globl	bakdiag
 bakdiag = .L_MergedGlobals+832
 	.size	bakdiag, 796
-	.ident	"clang version 23.0.0git ([email protected]:KavinTheG/llvm-project.git 5e7ec28c5e3a70588bbe9368d3816b009cf3670f)"
+	.ident	"clang version 23.0.0git ([email protected]:KavinTheG/llvm-project.git 046bd54d17a057fdf344889f8e4b1b8e6d850dd7)"
 	.section	".note.GNU-stack","",@progbits
 	.addrsig
 	.addrsig_sym .L_MergedGlobals

This is the diff I got from the queens benchmark. http://lnt.llvm.org/db_default/v4/nts/profile/1354/141/2

KavinTheG avatar Jun 17 '26 23:06 KavinTheG

Maybe the addi s2, s2, -1 after the recursive call has to wait longer after because s2 is restored last inside the function?

AFAIU this is an in-order dual-issue core? If so that shouldn't matter, since any recursive calls / returns will be at least one instruction between any uses of those registers. The only thing I can think of is:

sd	s2, 0x0(sp)
li	s0, 0x2

vs

sd	s2, 0x0(sp)
li	s2, 0x2

in the prologue, where maybe there's a naive hazard detection that falsely detects the second instruction in the new code as not being able to issue at the same time as the store? But that would be quite a stupid hazard to have, so I'm rather sceptical it would be that.

jrtc27 avatar Jun 18 '26 00:06 jrtc27

Maybe the addi s2, s2, -1 after the recursive call has to wait longer after because s2 is restored last inside the function?

AFAIU this is an in-order dual-issue core? If so that shouldn't matter, since any recursive calls / returns will be at least one instruction between any uses of those registers. The only thing I can think of is:

Depending on the load to use latency, the return might not be enough gap to prevent a stall.

topperc avatar Jun 18 '26 00:06 topperc

Are there any changes in the linked binary to the addresses of the benchmark's functions and loops? (That would be caused by other code outside the hot part of the benchmark being compiled in a way that is differently compressible or relaxable and therefore changes size)

jrtc27 avatar Jun 18 '26 00:06 jrtc27

Maybe the addi s2, s2, -1 after the recursive call has to wait longer after because s2 is restored last inside the function?

AFAIU this is an in-order dual-issue core? If so that shouldn't matter, since any recursive calls / returns will be at least one instruction between any uses of those registers. The only thing I can think of is:

Depending on the load to use latency, the return might not be enough gap to prevent a stall.

Is there an actual proper datasheet for the X60 somewhere that would state these kinds of things? I can only find unhelpful SoC block diagrams that tell you nothing of real interest.

jrtc27 avatar Jun 18 '26 00:06 jrtc27

Are there any changes in the linked binary to the addresses of the benchmark's functions and loops?

I'll try taking a look tomorrow when I get the time!

KavinTheG avatar Jun 18 '26 01:06 KavinTheG

I believe the same commit is causing a performance regression on the SiFive P550 as well according to this benchmark?

http://lnt.llvm.org/db_default/v4/nts/profile/1342/144/1

Here's an example of the diff of the first hotspot (20% cycles) in main, where the new code produces 224M vs 180M cycles.

@@ -377,10 +373,10 @@ main:                                   # @main
 .LBB6_6:                                # %for.body.i.1
                                         #   Parent Loop BB6_5 Depth=1
                                         # =>  This Inner Loop Header: Depth=2
-       lw      s1, 0(a5)
+       lw      a6, 0(a5)
        xor     s0, s0, a4
        addi    a5, a5, 4
-       xor     s0, s0, s1
+       xor     s0, a6, s0
        addiw   a4, a4, 1
        bne     a5, a2, .LBB6_6

KavinTheG avatar Jun 19 '26 22:06 KavinTheG

Based on these information above, I think this is not a compiler regression. This getRegisterCostTableIndex change (RA change) may just uncover some hardware frailnesses, we need to figure it out first.

wangpc-pp avatar Jun 23 '26 04:06 wangpc-pp

I presume we're getting different amounts of compression in the p550 diff there? Is that part of the problem? a6 is not a GPRC, so it stops the lw and the xor being compressed.

Maybe our Register hinting could be better?

lenary avatar Jun 23 '26 05:06 lenary

I presume we're getting different amounts of compression in the p550 diff there

Ah, that makes sense. This tracks when I look at the instruction addresses.

KavinTheG avatar Jun 24 '26 14:06 KavinTheG