Skip to content

Commit 54fc75e

Browse files
committed
[AMDGPU][Next Use Analysis] Review comments addressed.
1 parent b55af4e commit 54fc75e

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ unsigned NextUseResult::materializeForRank(int64_t Stored,
4040

4141
// Tier 1: Finite distances (0 to LoopTag-1) → return as-is
4242
// Tier 2: Loop-exit distances (LoopTag to DeadTag-1) → map to 60000-64999
43-
// range Tier 3: Dead registers (DeadTag+) → return Infinity (65535)
44-
if (Mat64 >= DeadTag) {
45-
return Infinity; // Tier 3: Dead registers get maximum distance
46-
}
43+
// Tier 3: Dead registers (DeadTag+) → return DeadDistance (65535)
44+
if (Mat64 >= DeadTag)
45+
return DeadDistance;
46+
4747
if (Mat64 >= LoopTag) {
4848
// Tier 2: Loop-exit distances get mapped to high range [60000, 64999]
4949
int64_t LoopRemainder = Mat64 - LoopTag;
@@ -52,17 +52,18 @@ unsigned NextUseResult::materializeForRank(int64_t Stored,
5252
std::min(LoopRemainder, static_cast<int64_t>(4999)));
5353
return 60000 + ClampedRemainder;
5454
}
55-
if (Mat64 <= 0) {
55+
56+
if (Mat64 <= 0)
5657
return 0; // Tier 1: Zero-distance for immediate uses
57-
}
58+
5859
return static_cast<unsigned>(Mat64); // Tier 1: Finite distances as-is
5960
}
6061

6162
void NextUseResult::init(const MachineFunction &MF) {
62-
for (auto *L : LI->getLoopsInPreorder()) {
63+
for (const auto *L : LI->getLoopsInPreorder()) {
6364
SmallVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> Exiting;
6465
L->getExitEdges(Exiting);
65-
for (auto P : Exiting) {
66+
for (const auto &P : Exiting) {
6667
LoopExits[P.first->getNumber()] = P.second->getNumber();
6768
}
6869
}
@@ -108,8 +109,7 @@ void NextUseResult::analyze(const MachineFunction &MF) {
108109
// Check if the edge from MBB to Succ goes out of the Loop
109110
int64_t EdgeWeight = 0;
110111
if (LoopExits.contains(MBB->getNumber())) {
111-
unsigned ExitTo = LoopExits[MBB->getNumber()];
112-
if (SuccNum == ExitTo)
112+
if (SuccNum == LoopExits[MBB->getNumber()])
113113
EdgeWeight = LoopTag;
114114
}
115115

@@ -191,7 +191,6 @@ void NextUseResult::analyze(const MachineFunction &MF) {
191191
}
192192
NextUseMap[MBBNum].InstrDist[&MI] = Curr;
193193
NextUseMap[MBBNum].InstrOffset[&MI] = Offset;
194-
// printVregDistances(Curr, Offset);
195194
if (!MI.isPHI())
196195
++Offset;
197196
}
@@ -268,7 +267,6 @@ NextUseResult::getSortedSubregUses(const MachineBasicBlock::iterator I,
268267
unsigned MBBNum = MBB->getNumber();
269268
if (NextUseMap.contains(MBBNum) &&
270269
NextUseMap[MBBNum].InstrDist.contains(&*I)) {
271-
// VRegDistances Dists = NextUseMap[MBBNum].InstrDist[&*I];
272270
if (NextUseMap[MBBNum].InstrDist[&*I].contains(VMP.getVReg())) {
273271
VRegDistances::SortedRecords Dists =
274272
NextUseMap[MBBNum].InstrDist[&*I][VMP.getVReg()];
@@ -325,7 +323,7 @@ unsigned NextUseResult::getNextUseDistance(const MachineBasicBlock::iterator I,
325323
if (EnableTimers)
326324
GetDistanceTimer.startTimer();
327325

328-
unsigned Dist = Infinity;
326+
unsigned Dist = DeadDistance;
329327
const MachineBasicBlock *MBB = I->getParent();
330328
unsigned MBBNum = MBB->getNumber();
331329
if (NextUseMap.contains(MBBNum) &&
@@ -349,7 +347,7 @@ unsigned NextUseResult::getNextUseDistance(const MachineBasicBlock &MBB,
349347
if (EnableTimers)
350348
GetDistanceTimer.startTimer();
351349

352-
unsigned Dist = Infinity;
350+
unsigned Dist = DeadDistance;
353351
unsigned MBBNum = MBB.getNumber();
354352
if (NextUseMap.contains(MBBNum)) {
355353
if (NextUseMap[MBBNum].Bottom.contains(VMP.getVReg())) {

llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,15 @@ class NextUseResult {
238238
public:
239239
private:
240240
DenseMap<unsigned, VRegMaskPairSet> UsedInBlock;
241-
DenseMap<int, int> LoopExits;
241+
DenseMap<unsigned, unsigned> LoopExits;
242242
// Signed tag used to mark "outside current loop" in stored values.
243243
// Must be >> any finite distance you can accumulate in one function.
244244
static constexpr int64_t LoopTag = (int64_t)1 << 40; // ~1e12 headroom
245245
static constexpr int64_t DeadTag = (int64_t)1 << 60; // ~1e18, >> LoopTag
246246

247-
// Unsigned Infinity for external API/DAG users who want a sentinel.
248-
static constexpr unsigned PrintedInfinity =
249-
std::numeric_limits<unsigned>::max();
247+
// Sentinel returned by getNextUseDistance() for dead/unused registers.
248+
static constexpr unsigned DeadDistance = std::numeric_limits<uint16_t>::max();
250249

251-
const uint16_t Infinity = std::numeric_limits<unsigned short>::max();
252250
void init(const MachineFunction &MF);
253251
void analyze(const MachineFunction &MF);
254252

@@ -396,19 +394,8 @@ class NextUseResult {
396394
const VRegMaskPair VMP) {
397395
if (!VMP.getVReg().isVirtual())
398396
report_fatal_error("Only virtual registers allowed!\n", true);
399-
// FIXME: We use the same Infinity value to indicate both invalid distance
400-
// and too long for out of block values. It is okay if the use out of block
401-
// is at least one instruction further then the end of loop exit. In this
402-
// case we have a distance Infinity + 1 and hence register is not considered
403-
// dead. What if the register is defined by the last instruction in the loop
404-
// exit block and out of loop use is in PHI? By design the dist of all PHIs
405-
// from the beginning of block are ZERO and hence the distance of
406-
// out-of-the-loop use will be exactly Infinity So, the register will be
407-
// mistakenly considered DEAD! On another hand, any predecessor of the block
408-
// containing PHI must have a branch as the last instruction. In this case
409-
// the current design works.
410-
return I == MBB.end() ? getNextUseDistance(MBB, VMP) == Infinity
411-
: getNextUseDistance(I, VMP) == Infinity;
397+
return I == MBB.end() ? getNextUseDistance(MBB, VMP) == DeadDistance
398+
: getNextUseDistance(I, VMP) == DeadDistance;
412399
}
413400

414401
VRegMaskPairSet &usedInBlock(MachineBasicBlock &MBB) {

0 commit comments

Comments
 (0)