[llvm] [regalloc][LiveRegMatrix][AMDGPU] Fix LiveInterval dangling pointers in LiveRegMatrix. (PR #168556)

Valery Pykhtin via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 27 08:42:58 PST 2025


vpykhtin wrote:

> I'm not too familiar with the code, so excuse any misunderstanding, but is the bug in `LiveRangeEdit::eliminateDeadDef` or `SIPreAllocateWWMRegs.cpp`?

Well, since I'm adding verifier - it fails in SIPreAllocateWWMRegs too so I fixed it as well.

> In the summary the focus is on `LiveRangeEdit::eliminateDeadDef` leaving dangling intervals, but it seems like that issue would still be present there after this patch?

The actual erase of interval happens later in the `LiveRangeEdit::eliminateDeadDef`
```c++
  // Erase any virtregs that are now empty and unused. There may be <undef>
  // uses around. Keep the empty live range in that case.
  for (Register Reg : RegsToErase) {
    if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
      ToShrink.remove(&LIS.getInterval(Reg));
      eraseVirtReg(Reg);
    }
  }
``` 
and `eraseVirtReg(Reg)` uses notifier call before actual interval removal:
```c++
void LiveRangeEdit::eraseVirtReg(Register Reg) {
  if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
    LIS.removeInterval(Reg);
}
``` 
My patch overrides this notifier with:
```c++
/// Called before a virtual register is erased from LiveIntervals.
/// Forcibly remove the register from LiveRegMatrix before it's deleted,
/// preventing dangling pointers.
bool HoistSpillHelper::LRE_CanEraseVirtReg(Register VirtReg) {
  if (VRM.hasPhys(VirtReg)) {
    Matrix.unassign(VirtReg);
  }
  return true; // Allow deletion to proceed
}
``` 
which unassignes the interval from matrix. Safe version of unassign is used here.

https://github.com/llvm/llvm-project/pull/168556


More information about the llvm-commits mailing list