Skip to content

Commit 0d4d731

Browse files
[TEMP] Fix LiveRegMatrix dangling pointers in postOptimization
1 parent c1606b3 commit 0d4d731

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

llvm/include/llvm/CodeGen/LiveRangeEdit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class LiveRangeEdit : private MachineRegisterInfo::Delegate {
5858
/// Called before shrinking the live range of a virtual register.
5959
virtual void LRE_WillShrinkVirtReg(Register) {}
6060

61+
/// Called when a virtual register's LiveInterval is about to become empty.
62+
/// This happens when removeVRegDefAt removes the last definition.
63+
/// Implementations should unassign from LiveRegMatrix before the interval is cleared.
64+
virtual void LRE_WillClearVirtReg(Register, LiveInterval &) {}
65+
6166
/// Called after cloning a virtual register.
6267
/// This is used for new registers representing connected components of Old.
6368
virtual void LRE_DidCloneVirtReg(Register New, Register Old) {}

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class HoistSpillHelper : private LiveRangeEdit::Delegate {
8686
const TargetInstrInfo &TII;
8787
const TargetRegisterInfo &TRI;
8888
const MachineBlockFrequencyInfo &MBFI;
89+
LiveRegMatrix *Matrix;
8990

9091
InsertPointAnalysis IPA;
9192

@@ -129,16 +130,18 @@ class HoistSpillHelper : private LiveRangeEdit::Delegate {
129130

130131
public:
131132
HoistSpillHelper(const Spiller::RequiredAnalyses &Analyses,
132-
MachineFunction &mf, VirtRegMap &vrm)
133+
MachineFunction &mf, VirtRegMap &vrm,
134+
LiveRegMatrix *matrix = nullptr)
133135
: MF(mf), LIS(Analyses.LIS), LSS(Analyses.LSS), MDT(Analyses.MDT),
134136
VRM(vrm), MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
135137
TRI(*mf.getSubtarget().getRegisterInfo()), MBFI(Analyses.MBFI),
136-
IPA(LIS, mf.getNumBlockIDs()) {}
138+
Matrix(matrix), IPA(LIS, mf.getNumBlockIDs()) {}
137139

138140
void addToMergeableSpills(MachineInstr &Spill, int StackSlot,
139141
Register Original);
140142
bool rmFromMergeableSpills(MachineInstr &Spill, int StackSlot);
141143
void hoistAllSpills();
144+
void LRE_WillClearVirtReg(Register, LiveInterval &) override;
142145
void LRE_DidCloneVirtReg(Register, Register) override;
143146
};
144147

@@ -191,7 +194,7 @@ class InlineSpiller : public Spiller {
191194
: MF(MF), LIS(Analyses.LIS), LSS(Analyses.LSS), VRM(VRM),
192195
MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),
193196
TRI(*MF.getSubtarget().getRegisterInfo()), Matrix(Matrix),
194-
HSpiller(Analyses, MF, VRM), VRAI(VRAI) {}
197+
HSpiller(Analyses, MF, VRM, Matrix), VRAI(VRAI) {}
195198

196199
void spill(LiveRangeEdit &, AllocationOrder *Order = nullptr) override;
197200
ArrayRef<Register> getSpilledRegs() override { return RegsToSpill; }
@@ -1750,6 +1753,17 @@ void HoistSpillHelper::hoistAllSpills() {
17501753
}
17511754
}
17521755

1756+
/// Called when a LiveInterval is about to be cleared by removeVRegDefAt.
1757+
/// Unassign from LiveRegMatrix to prevent dangling pointers (fixes LLVM bug #48911).
1758+
void HoistSpillHelper::LRE_WillClearVirtReg(Register VirtReg,
1759+
LiveInterval &LI) {
1760+
// If this virtual register is assigned to a physical register, unassign it
1761+
// from LiveRegMatrix before the interval is cleared. Otherwise, LiveIntervalUnion
1762+
// will contain dangling pointers.
1763+
if (Matrix && VRM.hasPhys(VirtReg))
1764+
Matrix->unassign(LI);
1765+
}
1766+
17531767
/// For VirtReg clone, the \p New register should have the same physreg or
17541768
/// stackslot as the \p old register.
17551769
void HoistSpillHelper::LRE_DidCloneVirtReg(Register New, Register Old) {

llvm/lib/CodeGen/LiveRangeEdit.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
268268
if (MO.isDef()) {
269269
if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
270270
TheDelegate->LRE_WillShrinkVirtReg(LI.reg());
271+
// If this is the last definition, notify delegate before clearing
272+
// so it can unassign from LiveRegMatrix while the range is still valid.
273+
// Skip dead defs [DefSlot, DeadSlot) - they are never added to Matrix.
274+
if (TheDelegate && LI.size() == 1) {
275+
VNInfo *VNI = LI.getVNInfoAt(Idx);
276+
if (VNI && LI.begin()->end != VNI->def.getDeadSlot()) {
277+
TheDelegate->LRE_WillClearVirtReg(Reg, LI);
278+
}
279+
}
271280
LIS.removeVRegDefAt(LI, Idx);
272281
if (LI.empty())
273282
RegsToErase.push_back(Reg);

0 commit comments

Comments
 (0)