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

[RISCV] Enhance RISCVMoveMerger for GPRPair Moves on RV32

Open lenary opened this issue 9 months ago • 9 comments

Our calling convention handling splits 64-bit values into two 32-bit values, before assigning them to a pair of registers (potentially not a GPRPair). Register coalescing looks through COPYs and can end up splitting a COPY of GPRPairs into two COPYs of the GPR sub-regs.

This means there's quite a few places in our tests where we end up with:

  mv a0, xN
  mv a1, x(N+1)

With either Zdinx (fmv.d) or P (padd.dw), we could transform this to a single instruction that moves both registers at once. We already have a pass to do this for Zcmp (and Xqccmp) -- the RISC-V Move Merger -- but only for cm.mva01s and cm.mvsa01, which have other restrictions the two registers being moved.

(This approach means we don't change how registers are allocated/coalesced, which should mean we can still end up with two mvs when the register allocator tells us it's better the registers aren't allocated into an even-odd pair. We don't want to over-constrain allocation.)

lenary avatar Feb 10 '26 20:02 lenary

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

Author: Sam Elliott (lenary)

Our calling convention handling splits 64-bit values into two 32-bit values, before assigning them to a pair of registers (potentially not a GPRPair). Register coalescing looks through COPYs and can end up splitting a COPY of GPRPairs into two COPYs of the GPR sub-regs.

This means there's quite a few places in our tests where we end up with:

  mv a0, xN
  mv a1, x(N+1)

With either Zdinx (fmv.d) or P (addd), we could transform this to a single instruction that moves both registers at once. We already have a pass to do this for Zcmp (and Xqccmp) -- the RISC-V Move Merger -- but only for cm.mva01s and cm.mvsa01, which have other restrictions the two registers being moved.

(This approach means we don't change how registers are allocated/coalesced, which should mean we can still end up with two mvs when the register allocator tells us it's better the registers aren't allocated into an even-odd pair. We don't want to over-constrain allocation.)

llvmbot avatar Feb 10 '26 20:02 llvmbot

There are some notes about this in https://github.com/llvm/llvm-project/pull/180671#issuecomment-3875188873

lenary avatar Feb 10 '26 20:02 lenary

Hey, is it alright if I try taking a stab at this if you haven't started on it yet?

KavinTheG avatar Feb 18 '26 02:02 KavinTheG

I haven't started on it at all 😄 I filed this in case anyone else wanted to work on it. When you have a PR, tag me in reviews. If you have questions before that I can answer them here.

lenary avatar Feb 18 '26 03:02 lenary

Quick question, I noticed that the following code sets the kill to false if another instruction inbetween the original mv instruction's iterator I to Paired reads the register.

    for (auto It = std::next(I); It != Paired && PairedSource.isKill(); ++It)
      if (It->readsRegister(PairedSource.getReg(), TRI))
        PairedSource.setIsKill(false);

Why is this only done for MoveFromSToA (and not when moving A to S)?

I'm assuming that fmv.d/add would need the kill analysis regardless of MoveFromSToA?

KavinTheG avatar Feb 19 '26 02:02 KavinTheG

Quick question, I noticed that the following code sets the kill to false if another instruction inbetween the original mv instruction's iterator I to Paired reads the register.

for (auto It = std::next(I); It != Paired && PairedSource.isKill(); ++It)
  if (It->readsRegister(PairedSource.getReg(), TRI))
    PairedSource.setIsKill(false);

Why is this only done for MoveFromSToA (and not when moving A to S)?

I'm assuming that fmv.d/add would need the kill analysis regardless of MoveFromSToA?

I think it's because the A registers on MoveFromAToS are represented by implicit use operands and don't have a place to put the copied kill flag.

topperc avatar Feb 19 '26 02:02 topperc

Ah, that makes sense. Thanks!

KavinTheG avatar Feb 19 '26 14:02 KavinTheG

Reopening because I think there improvements that can be made. See the post-commit comments I left on #182416

topperc avatar Feb 25 '26 22:02 topperc

I think this is now closed by https://github.com/llvm/llvm-project/pull/183657

lenary avatar Mar 27 '26 22:03 lenary