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

Post-RA scheduler undoes the optimized pre-RA schedule

Open pgodeq opened this issue 3 weeks ago • 6 comments

The pre-RA schedule at https://godbolt.org/z/sfcxTnY1d , correctly inserts independent instructions between load and it's use, given that the load latency of RISC-V generic CPU is 4 (as per debug logs from machine scheduler).

        xor     a0, a0, s2
        zext.b  a0, a0
        sh2add  a0, a0, s3
   -> lw      a0, 0(a0)   // load
        srli    a1, s2, 8
   -> xor     s2, a0, a1  // use
        addi    s1, s1, -1
        sw      s2, 0(s0)

However, the post-RA hoists these instructions before load, which incurs performance penalty as load and it's use are scheduled consecutively. https://godbolt.org/z/jT38hfnoz

       xor     a0, a0, s2
       srli    a1, s2, 8   // hoisted here. 
       zext.b  a0, a0
       addi    s1, s1, -1
       sh2add  a0, a0, s3
  -> lw      a0, 0(a0)   // load
  -> xor     s2, a0, a1  // use
       sw      s2, 0(s0)

Could someone please help and share if this can be corrected or I am missing something ?

pgodeq avatar Nov 10 '26 17:11 pgodeq

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

Author: Pankaj Gode (pgodeq)

The pre-RA schedule at https://godbolt.org/z/sfcxTnY1d , correctly inserts independent instructions between load and it's use, given that the load latency of RISC-V generic CPU is 4 (as per debug logs from machine scheduler).
        xor     a0, a0, s2
        zext.b  a0, a0
        sh2add  a0, a0, s3
   -> lw      a0, 0(a0)   // load
        srli    a1, s2, 8
   -> xor     s2, a0, a1  // use
        addi    s1, s1, -1
        sw      s2, 0(s0)

However, the post-RA hoists these instructions before load, which incurs performance penalty as load and it's use are scheduled consecutively. https://godbolt.org/z/jT38hfnoz

      xor     a0, a0, s2
       srli    a1, s2, 8   // hoisted here. 
       zext.b  a0, a0
       addi    s1, s1, -1
       sh2add  a0, a0, s3
  -> lw      a0, 0(a0)   // load
  -> xor     s2, a0, a1  // use
       sw      s2, 0(s0)

Could someone please help and share if this can be corrected or I am missing something ?

I think you just need to create your own schedule model instead of using the generic one. :-)

wangpc-pp avatar Nov 11 '26 04:11 wangpc-pp

I think you just need to create your own schedule model instead of using the generic one

I'm not sure we are able to.

I think we're not having the -riscv-sched-load-latency taken into account, even though we intended it should be. I want to look closer at what's going on there, maybe my implementation has a bug.

lenary avatar Nov 11 '26 04:11 lenary

Ok, I looked more deeply at this.

  • The riscv-sched-load-latency has nothing to do with the problem, broadly. It's not being taken into account at all, which is a different problem, but not the underlying thing causing the SRLI to be promoted

When scheduling the region after the call to rand_beebs, which includes both the SRLI and the LW, the SRLI is getting scheduled earlier, I think for good reason:

The first candidate picked is su(0) - XOR. I thought this was due to having higher height, but I don't think the other candidates in TopQ.A are looked at, judging from the logs. These candidates are su(4) - SRLI and su(6) - ADDI. Looking at the MIR, the XOR has no predecessors between the call and it, and lots of successors.

The second time around, we are still in cycle 0, and we look at su(4) - SRLI and su(6) - ADDI, and end up choosing the SRLI because it has just a bit higher height, so it wants to get the shift started earlier.

I guess there are a few things here:

  • On our core, we don't have two execution pipelines, so we would be considering SRLI and ADDI at cycle 1, maybe with some other instructions that are ready already.
  • I think, but cannot confirm, that the post-ra scheduler is trying to do nothing at all about hiding the latency of the load behind other work. Maybe it would if the SRLI wasn't ready to execute in parallel?

A quick check with "tune-cpu"="rocket-rv32" in the IR (along with the other info), and it's clear that the first bullet point is correct.

After the first candidate, the cycle is incremented to 1 where su(1) is available. This means the second time around, we're choosing between su(1) - ANDI, su(4) - SRLI and su(6) - ADDI, and su(1) is chosen because of its height being so much greater than the others.

This eventually gives:

*** Final schedule for %bb.1 ***
SU(0):   renamable $x10 = XOR renamable $x10, renamable $x18
SU(1):   renamable $x10 = ANDI renamable $x10, 255
SU(2):   renamable $x10 = nuw SH2ADD renamable $x10, renamable $x19
SU(3):   renamable $x10 = LW renamable $x10, 0 :: (invariant load (s32) from %ir.i12)
SU(4):   renamable $x11 = SRLI renamable $x18, 8
SU(5):   renamable $x18 = XOR renamable $x10, renamable $x11
SU(6):   renamable $x9 = nsw ADDI renamable $x9, -1
SU(7):   SW renamable $x18, renamable $x8, 0 :: (store (s32) into %ir.i15.out)

Which doesn't promote the SRLI too early, and correctly prioritises the LW.


I think the hoisting is intended behaviour - you can do the SRLI on the generic model because you can't do anything else in the chain before the LW (the other instructions are not ready). So you might as well get on with it, as it has some other dependencies which open up later choices.

lenary avatar Nov 18 '26 04:11 lenary

I think you just need to create your own schedule model instead of using the generic one

I'm not sure we are able to.

Why?

wangpc-pp avatar Nov 18 '26 06:11 wangpc-pp

Ok, I looked more deeply at this.

  • The riscv-sched-load-latency has nothing to do with the problem, broadly. It's not being taken into account at all, which is a different problem, but not the underlying thing causing the SRLI to be promoted

When scheduling the region after the call to rand_beebs, which includes both the SRLI and the LW, the SRLI is getting scheduled earlier, I think for good reason:

The first candidate picked is su(0) - XOR. I thought this was due to having higher height, but I don't think the other candidates in TopQ.A are looked at, judging from the logs. These candidates are su(4) - SRLI and su(6) - ADDI. Looking at the MIR, the XOR has no predecessors between the call and it, and lots of successors.

The second time around, we are still in cycle 0, and we look at su(4) - SRLI and su(6) - ADDI, and end up choosing the SRLI because it has just a bit higher height, so it wants to get the shift started earlier.

I guess there are a few things here:

  • On our core, we don't have two execution pipelines, so we would be considering SRLI and ADDI at cycle 1, maybe with some other instructions that are ready already.
  • I think, but cannot confirm, that the post-ra scheduler is trying to do nothing at all about hiding the latency of the load behind other work. Maybe it would if the SRLI wasn't ready to execute in parallel?

A quick check with "tune-cpu"="rocket-rv32" in the IR (along with the other info), and it's clear that the first bullet point is correct.

After the first candidate, the cycle is incremented to 1 where su(1) is available. This means the second time around, we're choosing between su(1) - ANDI, su(4) - SRLI and su(6) - ADDI, and su(1) is chosen because of its height being so much greater than the others.

This eventually gives:

*** Final schedule for %bb.1 ***
SU(0):   renamable $x10 = XOR renamable $x10, renamable $x18
SU(1):   renamable $x10 = ANDI renamable $x10, 255
SU(2):   renamable $x10 = nuw SH2ADD renamable $x10, renamable $x19
SU(3):   renamable $x10 = LW renamable $x10, 0 :: (invariant load (s32) from %ir.i12)
SU(4):   renamable $x11 = SRLI renamable $x18, 8
SU(5):   renamable $x18 = XOR renamable $x10, renamable $x11
SU(6):   renamable $x9 = nsw ADDI renamable $x9, -1
SU(7):   SW renamable $x18, renamable $x8, 0 :: (store (s32) into %ir.i15.out)

Which doesn't promote the SRLI too early, and correctly prioritises the LW.

I think the hoisting is intended behaviour - you can do the SRLI on the generic model because you can't do anything else in the chain before the LW (the other instructions are not ready). So you might as well get on with it, as it has some other dependencies which open up later choices.

Thanks Sam, for detailed analysis and exploring the rocket for this use case. This is helpful !!

pgodeq avatar Nov 18 '26 11:11 pgodeq