Skip to content

Commit

Permalink
Refactor recomputeLiveIns to operate on whole CFG (#79498) (#79641)
Browse files Browse the repository at this point in the history
Currently, the way that recomputeLiveIns works is that it will recompute
the livein registers for that MachineBasicBlock but it matters what
order you call recomputeLiveIn which can result in incorrect register
allocations down the line.

Now we do not recompute the entire CFG but we do ensure that the newly
added MBB do reach convergence. This fixes a register allocation bug
introduced in AArch64 stack probing.

(cherry picked from commit ff4636a)
  • Loading branch information
oskarwirga committed Feb 7, 2024
1 parent 0328b87 commit 7fb1186
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 46 deletions.
11 changes: 9 additions & 2 deletions llvm/include/llvm/CodeGen/LivePhysRegs.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,18 @@ void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs);
void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
MachineBasicBlock &MBB);

/// Convenience function for recomputing live-in's for \p MBB.
static inline void recomputeLiveIns(MachineBasicBlock &MBB) {
/// Convenience function for recomputing live-in's for a MBB. Returns true if
/// any changes were made.
static inline bool recomputeLiveIns(MachineBasicBlock &MBB) {
LivePhysRegs LPR;
auto oldLiveIns = MBB.getLiveIns();

MBB.clearLiveIns();
computeAndAddLiveIns(LPR, MBB);
MBB.sortUniqueLiveIns();

auto newLiveIns = MBB.getLiveIns();
return oldLiveIns != newLiveIns;
}

} // end namespace llvm
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/CodeGen/MachineBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class MachineBasicBlock

RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
: PhysReg(PhysReg), LaneMask(LaneMask) {}

bool operator==(const RegisterMaskPair &other) const {
return PhysReg == other.PhysReg && LaneMask == other.LaneMask;
}
};

private:
Expand Down Expand Up @@ -473,6 +477,8 @@ class MachineBasicBlock
/// Remove entry from the livein set and return iterator to the next.
livein_iterator removeLiveIn(livein_iterator I);

std::vector<RegisterMaskPair> getLiveIns() const { return LiveIns; }

class liveout_iterator {
public:
using iterator_category = std::input_iterator_tag;
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,10 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
FBB->erase(FBB->begin(), FIB);

if (UpdateLiveIns) {
recomputeLiveIns(*TBB);
recomputeLiveIns(*FBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*TBB) || recomputeLiveIns(*FBB);
} while (anyChange);
}

++NumHoist;
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4339,8 +4339,10 @@ AArch64FrameLowering::inlineStackProbeLoopExactMultiple(
ExitMBB->transferSuccessorsAndUpdatePHIs(&MBB);
MBB.addSuccessor(LoopMBB);
// Update liveins.
recomputeLiveIns(*LoopMBB);
recomputeLiveIns(*ExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);

return ExitMBB->begin();
}
Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9587,9 +9587,13 @@ AArch64InstrInfo::probedStackAlloc(MachineBasicBlock::iterator MBBI,

// Update liveins.
if (MF.getRegInfo().reservedRegsFrozen()) {
recomputeLiveIns(*LoopTestMBB);
recomputeLiveIns(*LoopBodyMBB);
recomputeLiveIns(*ExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) ||
recomputeLiveIns(*LoopBodyMBB) ||
recomputeLiveIns(*LoopTestMBB);
} while (anyChange);
;
}

return ExitMBB->begin();
Expand Down
13 changes: 7 additions & 6 deletions llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,12 +1806,13 @@ void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
PostOrderLoopTraversal DFS(LoLoop.ML, *MLI);
DFS.ProcessLoop();
const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder();
for (auto *MBB : PostOrder) {
recomputeLiveIns(*MBB);
// FIXME: For some reason, the live-in print order is non-deterministic for
// our tests and I can't out why... So just sort them.
MBB->sortUniqueLiveIns();
}
bool anyChange = false;
do {
anyChange = false;
for (auto *MBB : PostOrder) {
anyChange = recomputeLiveIns(*MBB) || anyChange;
}
} while (anyChange);

for (auto *MBB : reverse(PostOrder))
recomputeLivenessFlags(*MBB);
Expand Down
14 changes: 9 additions & 5 deletions llvm/lib/Target/PowerPC/PPCExpandAtomicPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ bool PPCExpandAtomicPseudo::expandAtomicRMW128(
.addMBB(LoopMBB);
CurrentMBB->addSuccessor(LoopMBB);
CurrentMBB->addSuccessor(ExitMBB);
recomputeLiveIns(*LoopMBB);
recomputeLiveIns(*ExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
NMBBI = MBB.end();
MI.eraseFromParent();
return true;
Expand Down Expand Up @@ -286,9 +288,11 @@ bool PPCExpandAtomicPseudo::expandAtomicCmpSwap128(
CurrentMBB->addSuccessor(LoopCmpMBB);
CurrentMBB->addSuccessor(ExitMBB);

recomputeLiveIns(*LoopCmpMBB);
recomputeLiveIns(*CmpSuccMBB);
recomputeLiveIns(*ExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*CmpSuccMBB) ||
recomputeLiveIns(*LoopCmpMBB);
} while (anyChange);
NMBBI = MBB.end();
MI.eraseFromParent();
return true;
Expand Down
13 changes: 9 additions & 4 deletions llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,8 +1441,11 @@ void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
ProbeLoopBodyMBB->addSuccessor(ProbeLoopBodyMBB);
}
// Update liveins.
recomputeLiveIns(*ProbeLoopBodyMBB);
recomputeLiveIns(*ProbeExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ProbeExitMBB) ||
recomputeLiveIns(*ProbeLoopBodyMBB);
} while (anyChange);
return ProbeExitMBB;
};
// For case HasBP && MaxAlign > 1, we have to realign the SP by performing
Expand Down Expand Up @@ -1534,8 +1537,10 @@ void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
buildDefCFAReg(*ExitMBB, ExitMBB->begin(), SPReg);
}
// Update liveins.
recomputeLiveIns(*LoopMBB);
recomputeLiveIns(*ExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
}
}
++NumPrologProbed;
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,10 @@ void SystemZELFFrameLowering::inlineStackProbe(
StackAllocMI->eraseFromParent();
if (DoneMBB != nullptr) {
// Compute the live-in lists for the new blocks.
recomputeLiveIns(*DoneMBB);
recomputeLiveIns(*LoopMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*DoneMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
}
}

Expand Down Expand Up @@ -1439,8 +1441,10 @@ void SystemZXPLINKFrameLowering::inlineStackProbe(
StackAllocMI->eraseFromParent();

// Compute the live-in lists for the new blocks.
recomputeLiveIns(*NextMBB);
recomputeLiveIns(*StackExtMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*StackExtMBB) || recomputeLiveIns(*NextMBB);
} while (anyChange);
}

bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
Expand Down
15 changes: 9 additions & 6 deletions llvm/lib/Target/X86/X86FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,10 @@ void X86FrameLowering::emitStackProbeInlineGenericLoop(
}

// Update Live In information
recomputeLiveIns(*testMBB);
recomputeLiveIns(*tailMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*tailMBB) || recomputeLiveIns(*testMBB);
} while (anyChange);
}

void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
Expand Down Expand Up @@ -1378,10 +1380,11 @@ void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
footMBB->addSuccessor(&MBB);
}

recomputeLiveIns(*headMBB);
recomputeLiveIns(*bodyMBB);
recomputeLiveIns(*footMBB);
recomputeLiveIns(MBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*footMBB) || recomputeLiveIns(*bodyMBB) ||
recomputeLiveIns(*headMBB) || recomputeLiveIns(MBB);
} while (anyChange);
}
} else {
MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg)
Expand Down
36 changes: 25 additions & 11 deletions llvm/test/CodeGen/SystemZ/branch-folder-hoist-livein.mir
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
# RUN: llc -verify-machineinstrs -O1 -mtriple=s390x-ibm-linux -o - %s -run-pass=branch-folder | FileCheck %s
--- |
target datalayout = "E-m:e-i1:8:16-i8:8:16-i64:64-f128:64-a:8:16-n32:64"
Expand All @@ -15,6 +16,30 @@
name: f1
tracksRegLiveness: true
body: |
; CHECK-LABEL: name: f1
; CHECK: bb.0:
; CHECK-NEXT: successors: %bb.2(0x7fffffff), %bb.1(0x00000001)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: renamable $r1d = LGRL @b :: (load (s32) from got, align 8)
; CHECK-NEXT: renamable $r1l = LH killed renamable $r1d, 0, $noreg, implicit-def $r1d :: (dereferenceable load (s8) from @b)
; CHECK-NEXT: renamable $r2l = LHI 0
; CHECK-NEXT: renamable $r3d = LGRL @d :: (load (s32) from got, align 8)
; CHECK-NEXT: renamable $r4d = LLILL 0, implicit-def $r4q
; CHECK-NEXT: renamable $r4d = COPY killed renamable $r4d, implicit killed $r4q
; CHECK-NEXT: CHI killed renamable $r2l, 0, implicit-def $cc
; CHECK-NEXT: BRC 14, 6, %bb.2, implicit killed $cc
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.1:
; CHECK-NEXT: successors:
; CHECK-NEXT: liveins: $r3d, $r4d, $r1l
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: STH renamable $r1l, killed renamable $r3d, 0, $noreg, implicit killed $r4d :: (store (s8) into @d)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2:
; CHECK-NEXT: liveins: $r3d, $r4d, $r1l
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: STH renamable $r1l, killed renamable $r3d, 0, $noreg, implicit killed $r4d :: (store (s8) into @d)
; CHECK-NEXT: Return
bb.0:
successors: %bb.2(0x7fffffff), %bb.1(0x00000001)
liveins:
Expand Down Expand Up @@ -44,14 +69,3 @@ body: |
Return
...

# CHECK: renamable $r4d = COPY killed renamable $r4d, implicit killed $r4q
# CHECK-NEXT: CHI killed renamable $r2l, 0, implicit-def $cc
# CHECK-NEXT: BRC 14, 6, %bb.2, implicit killed $cc
# CHECK-NEXT: {{^ $}}
# CHECK-NEXT: bb.1:
# CHECK-NEXT: successors:
# CHECK-NEXT: liveins: $r1l, $r3d, $r4d

# CHECK: bb.2:
# CHECK-NEXT: liveins: $r1l, $r3d, $r4d
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/Thumb2/LowOverheadLoops/spillingmove.mir
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.4:
; CHECK-NEXT: successors: %bb.5(0x04000000), %bb.2(0x7c000000)
; CHECK-NEXT: liveins: $q0, $r0, $r1, $r2, $r3, $r6, $r12
; CHECK-NEXT: liveins: $d0, $d1, $r0, $r1, $r2, $r3, $r6, $r12
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: renamable $r3, dead $cpsr = nuw nsw tADDi8 killed renamable $r3, 1, 14 /* CC::al */, $noreg
; CHECK-NEXT: renamable $r0 = tADDhirr killed renamable $r0, renamable $r1, 14 /* CC::al */, $noreg
Expand Down

0 comments on commit 7fb1186

Please sign in to comment.