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

[RISCV] Regsiter copies in a loop should be eliminated

Open mgudim opened this issue 2 years ago • 5 comments

The following happens in gcc benchmark:

For this code:

typedef struct simple_bitmap_def
{
  unsigned char *popcount;
  unsigned int n_bits;
  unsigned int size;
  unsigned long elms[1];
} *sbitmap;
typedef const struct simple_bitmap_def *const_sbitmap;

typedef unsigned long *sbitmap_ptr;
typedef const unsigned long *const_sbitmap_ptr;
static unsigned long sbitmap_elt_popcount (unsigned long);

void
sbitmap_a_or_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
{
  unsigned int i, n = dst->size;
  sbitmap_ptr dstp = dst->elms;
  const_sbitmap_ptr ap = a->elms;
  const_sbitmap_ptr bp = b->elms;
  unsigned char has_popcount = dst->popcount != ((void *) 0);

  for (i = 0; i < n; i++)
    {
      const unsigned long tmp = *ap++ | *bp++;
      *dstp++ = tmp;
    }
}

We get copies in the loop body:

  ld  a4, 0(a3)
  ld  a5, 0(a2) 
  addi  a1, a3, 8
  addi  a2, a2, 8
  or  a4, a4, a5
  addi  a3, a0, 8
  sd  a4, 0(a0)
  mv  a0, a3
  mv  a3, a1
  bne a1, a6, .LBB0_2

Copies are introduced by PHI Elimination. The code before PHI Elimination:

bb.2.for.body:
; predecessors: %bb.1, %bb.2
  successors: %bb.3(0x04000000), %bb.2(0x7c000000); %bb.3(3.12%), %bb.2(96.88%)

  %5:gpr = PHI %3:gpr, %bb.1, %10:gpr, %bb.2
  %6:gpr = PHI %1:gpr, %bb.1, %9:gpr, %bb.2
  %7:gpr = PHI %2:gpr, %bb.1, %8:gpr, %bb.2
  %8:gpr = ADDI %7:gpr, 8
  %16:gpr = LD killed %7:gpr, 0 :: (load (s64) from %ir.ap.014, !tbaa !15)
  %9:gpr = nuw ADDI %6:gpr, 8
  %17:gpr = LD killed %6:gpr, 0 :: (load (s64) from %ir.bp.015, !tbaa !15)
  %18:gpr = OR killed %17:gpr, killed %16:gpr
  %10:gpr = nuw ADDI %5:gpr, 8
  SD killed %18:gpr, killed %5:gpr, 0 :: (store (s64) into %ir.dstp.016, !tbaa !15)
  BNE %8:gpr, %4:gpr, %bb.2
  PseudoBR %bb.3

Note that SD killed %18:gpr, killed %5:gpr, 0 :: (store (s64) into %ir.dstp.016, !tbaa !15) is using the value of induction variable %5 which is updated in %10:gpr = nuw ADDI %5:gpr, 8.

However, it is legal to move the store before the add and the RegisterCoalescer will eliminate the copy. Similar situations is with the other copies.

Possible solutions:

(1) Have some scheduling pass before PHI elimination. Right after non-global ISel we have a scheduling, where target can choose a custom scheduler via ST.getDAGScheduler. None of the existing targets use this and, as I understand, this code will be replaced by something else soon? Also, these schedulers are bottom-up, while this situation is best handled in top-down I think. Another possibility is to add a top-down scheduler somewhere before PHI Elimination?

(2) Do this reordering in some other existing non-scheduler pass? Maybe as a first step of PHI elimination? We'll have to reproduce some of the scheduler's logic though which doesn't seem right.

(3) Something else?

What do you think?

CC: @topperc @preames @asb @wangpc-pp

mgudim avatar Apr 25 '24 09:04 mgudim

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

Author: Mikhail Gudim (mgudim)

The following happens in gcc benchmark:

For this code:

typedef struct simple_bitmap_def
{
  unsigned char *popcount;
  unsigned int n_bits;
  unsigned int size;
  unsigned long elms[1];
} *sbitmap;
typedef const struct simple_bitmap_def *const_sbitmap;

typedef unsigned long *sbitmap_ptr;
typedef const unsigned long *const_sbitmap_ptr;
static unsigned long sbitmap_elt_popcount (unsigned long);

void
sbitmap_a_or_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
{
  unsigned int i, n = dst-&gt;size;
  sbitmap_ptr dstp = dst-&gt;elms;
  const_sbitmap_ptr ap = a-&gt;elms;
  const_sbitmap_ptr bp = b-&gt;elms;
  unsigned char has_popcount = dst-&gt;popcount != ((void *) 0);

  for (i = 0; i &lt; n; i++)
    {
      const unsigned long tmp = *ap++ | *bp++;
      *dstp++ = tmp;
    }
}

We get copies in the loop body:

  ld  a4, 0(a3)
  ld  a5, 0(a2) 
  addi  a1, a3, 8
  addi  a2, a2, 8
  or  a4, a4, a5
  addi  a3, a0, 8
  sd  a4, 0(a0)
  mv  a0, a3
  mv  a3, a1
  bne a1, a6, .LBB0_2

Copies are introduced by PHI Elimination. The code before PHI Elimination:

bb.2.for.body:
; predecessors: %bb.1, %bb.2
  successors: %bb.3(0x04000000), %bb.2(0x7c000000); %bb.3(3.12%), %bb.2(96.88%)

  %5:gpr = PHI %3:gpr, %bb.1, %10:gpr, %bb.2
  %6:gpr = PHI %1:gpr, %bb.1, %9:gpr, %bb.2
  %7:gpr = PHI %2:gpr, %bb.1, %8:gpr, %bb.2
  %8:gpr = ADDI %7:gpr, 8
  %16:gpr = LD killed %7:gpr, 0 :: (load (s64) from %ir.ap.014, !tbaa !15)
  %9:gpr = nuw ADDI %6:gpr, 8
  %17:gpr = LD killed %6:gpr, 0 :: (load (s64) from %ir.bp.015, !tbaa !15)
  %18:gpr = OR killed %17:gpr, killed %16:gpr
  %10:gpr = nuw ADDI %5:gpr, 8
  SD killed %18:gpr, killed %5:gpr, 0 :: (store (s64) into %ir.dstp.016, !tbaa !15)
  BNE %8:gpr, %4:gpr, %bb.2
  PseudoBR %bb.3

Note that SD killed %18:gpr, killed %5:gpr, 0 :: (store (s64) into %ir.dstp.016, !tbaa !15) is using the value of induction variable %5 which is updated in %10:gpr = nuw ADDI %5:gpr, 8.

However, it is legal to move the store before the add. Similar situations is with other copies. Possible solutions:

(1) Have some scheduling pass before PHI elimination. Right after non-global ISel we have a scheduling, where target can choose a custom scheduler via ST.getDAGScheduler. None of the existing targets use this and, as I understand, this code will be replaced by something else soon? Also, these schedulers are bottom-up, while this situation is best handled in top-down I think. Another possibility is to add a top-down scheduler somewhere before PHI Elimination?

(2) Do this reordering in some other existing non-scheduler pass? Maybe as a first step of PHI elimination? We'll have to reproduce some of the scheduler's logic though which doesn't seem right.

(3) Something else?

What do you think?

CC: @topperc @preames @asb @wangpc-pp

llvmbot avatar Apr 25 '24 09:04 llvmbot

None of the existing targets use this and, as I understand, this code will be replaced by something else soon?

I don't think there's been much change in scheduling in a long time. So I don't think anything will happen "soon". Unless you found something recently that I missed.

topperc avatar Apr 30 '24 05:04 topperc

This loops looks like a good candidate for loop vectorization or loop unrolling. How much of a problem are these extract moves in practice?

I notice using sifive-p450 scheduler only has 1 mv.

topperc avatar Apr 30 '24 05:04 topperc

@topperc

Thanks for taking a look!

I don't think there's been much change in scheduling in a long time. So I don't think anything will happen "soon". Unless you found something recently that I missed.

I meant that the ScheduleDAGRRList is not used much anymore by targets and its only used by non-global ISel. Given the hope that GISel will eventually be the default, it would be preferable to fix this elsewhere?

I notice using sifive-p450 scheduler only has 1 mv.

It is a coincidence I think. The latency info for instructions doesn't matter here. We need some kind of heuristic that would say: There is an induction variable in the loop which (may be) updated on every iteration. Some users of the induction variable use the value before the update happens. So it's better to move those users before any instruction that MAY update the induction variable. So in this exampe:

the induction variable is %5:gpr = PHI %3:gpr, %bb.1, %10:gpr, %bb.2

The instruction using the "old" value is SD killed %18:gpr, killed %5:gpr, 0 :: (store (s64) into %ir.dstp.016, !tbaa !15)

Instruction that could (in this case does) update the induction variable is %10:gpr = nuw ADDI %5:gpr, 8

So the heuristic would move SD before ADDI.

Other targets like AArch64 have post-increment loads, so this problem goes away.

This loops looks like a good candidate for loop vectorization or loop unrolling. How much of a problem are these extract moves in practice?

The loop does get unrolled, but it didn't vectorize (unless I messed up something). I don't know vectorization, but this issue happens much later than vectorization so how can it interfere? I don't have an estimate of how much impact this has, but this can happen in many places, so probably worth working on.

mgudim avatar Apr 30 '24 08:04 mgudim

The loop does get unrolled, but it didn't vectorize (unless I messed up something). I don't know vectorization, but this issue happens much later than vectorization so how can it interfere? I don't have an estimate of how much impact this has, but this can happen in many places, so probably worth working on.

Vectorized - https://godbolt.org/z/Wsnn7Mdhr. the scalar loop still exists that has the issue, but hopefully the vector loop is used for most of it.

topperc avatar Apr 30 '24 15:04 topperc