[PATCH] D94949: [AArch64][RegAllocFast] Add findSpillBefore to TargetRegisterInfo

Tomas Matheson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 18 23:25:35 PST 2021


tmatheson created this revision.
Herald added subscribers: danielkiss, jfb, hiraditya, kristof.beyls, qcolombet, MatzeB.
tmatheson requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Due to interactions between RegAllocFast and expansion of atomicrmw at -O0,
both ARM and AArch64 backends would emit stores between ldrex and strex,
which clears the exclusive access monitor.

atomicrmw instructions are expanded to loops, where the main MachineBasicBlock
includes a ldrex/strex pair. It then conditionally branches if this atomic
operation was successful. Because of this, the register loaded by ldrex is
LiveOut, and RegAllocFast therefore spills this register. The issue is that it
spills between the ldrex and strex, which invalidates the monitor.

I tried several ways of fixing this which all have problems:

- Adding a pass after RegAllocFast which moved the str instructions (spills) to after the strex. For more complex sequences like those generated for 64 bit atomics with e.g. nand, this becomes difficult to do.

- Add new pseudo instructions for atomicrmw which are expanded after register allocation. This would involve duplicating all of the loop creation code. Similar approach has been used before for cmpxchg: https://reviews.llvm.org/D16239?id=52861

- Stop FastRegAlloc from spilling these registers for these instructions. However, other instructions between ldrex and strex can spill, and it is hard to catch them all.

- Move the location of the spill to after the strex. This is the approach taken.

To spill after strex, I have added a new function to TargetRegisterInfo which returns an appropriate
point to spill at for a given instruction. For all backends except ARM/AArch64 this just returns the
next instruction. For ARM/AArch64 it returns the strex.

Alternatives:

- A similar approach could have been applied to calcSpillCost instead, to set a high spill cost between ldrex/strex
- Pseudo instructions could have been used instead

It is also possible that the cmpxchg pseudoinstructions could be removed, and the same technique used for them.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94949

Files:
  llvm/include/llvm/CodeGen/TargetRegisterInfo.h
  llvm/lib/CodeGen/RegAllocFast.cpp
  llvm/lib/CodeGen/TargetRegisterInfo.cpp
  llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
  llvm/lib/Target/AArch64/AArch64RegisterInfo.h
  llvm/lib/Target/ARM/ARMRegisterInfo.cpp
  llvm/lib/Target/ARM/ARMRegisterInfo.h
  llvm/test/CodeGen/ARM/atomicrmw_exclusive_monitor.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94949.317469.patch
Type: text/x-patch
Size: 28617 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210119/9ef97e67/attachment.bin>


More information about the llvm-commits mailing list