Skip to content

[MachinePipeliner] Use RegisterClassInfo::getRegPressureSetLimit #119827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 1 addition & 42 deletions llvm/lib/CodeGen/MachinePipeliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,48 +1326,7 @@ class HighRegisterPressureDetector {
// Calculate the upper limit of each pressure set
void computePressureSetLimit(const RegisterClassInfo &RCI) {
for (unsigned PSet = 0; PSet < PSetNum; PSet++)
PressureSetLimit[PSet] = TRI->getRegPressureSetLimit(MF, PSet);

// We assume fixed registers, such as stack pointer, are already in use.
// Therefore subtracting the weight of the fixed registers from the limit of
// each pressure set in advance.
SmallDenseSet<Register, 8> FixedRegs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what a fixed register is, but it seems to not be the same as reserved. Seems like another hook with bad defaults

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is a bad API. Fixed registers are not reserved register... Fixed register are registers defined in FixedRegisters RegisterCategory by default...
The logic here seems to want to remove reserved registers @kasuga-fj.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I commented in #118787 (comment), I dared to replace it from RegisterClassInfo::getRegPressureSetLimit before (in #87312). This is because there is a duplication between the reserved registers handled inside RegisterClassInfo::getRegPressureSetLimit and the FixedRegs calculated here, so the Limit >= Weight assertion fails. If I remember correctly, in my case (AArch64), the $ffr triggers it. What I'd like to do here is to ignore registers that have "specific rolls" (e.g., stack pointer) and those where TargetRegisterInfo::isFixedRegister returns true looks appropriate in this case to me. Maybe I'm misunderstanding reserved and fixed registers, and there may be a better way to achieve what I want, but I'm not sure this change will work well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO isFixedRegister should be deleted. The intent seems to be isReserved || isConstantPhysREg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explanation! At least, after removing this part of code, the tests still pass? @kasuga-fj

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran tests and none of them failed. But that just means there are no correctness issues. This code is part of a heuristic decision, so performance degradation could happen in some cases (and I think the heuristics will be less accurate with this change).

Copy link
Contributor Author

@wangpc-pp wangpc-pp Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the heuristics will be less accurate with this change

I don't think so. These "fixed" registers are also "reserved" registers, and "reserved" registers contain runtime fixed registers like those are reserved via -ffixed-xx options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These "fixed" registers are also "reserved" registers,

Previously (where RegisterClassInfo::getRegPressureSetLimit was used here), #83437 caused the Limit >= Weight assertion to fail. This patch marks the $ffr register as reserved. I just tried to comment out the code that marks $ffr as reserved and run MachinePipeliner, and found that FixedRegs contains $ffr. It looks to me like there are some gaps between "reserved" and "fixed". Or maybe I misunderstood something (I don't know much about the LLVM API for registers).

for (const TargetRegisterClass *TRC : TRI->regclasses()) {
for (const MCPhysReg Reg : *TRC)
if (isFixedRegister(Reg))
FixedRegs.insert(Reg);
}

LLVM_DEBUG({
for (auto Reg : FixedRegs) {
dbgs() << printReg(Reg, TRI, 0, &MRI) << ": [";
for (MCRegUnit Unit : TRI->regunits(Reg)) {
const int *Sets = TRI->getRegUnitPressureSets(Unit);
for (; *Sets != -1; Sets++) {
dbgs() << TRI->getRegPressureSetName(*Sets) << ", ";
}
}
dbgs() << "]\n";
}
});

for (auto Reg : FixedRegs) {
LLVM_DEBUG(dbgs() << "fixed register: " << printReg(Reg, TRI, 0, &MRI)
<< "\n");
for (MCRegUnit Unit : TRI->regunits(Reg)) {
auto PSetIter = MRI.getPressureSets(Unit);
unsigned Weight = PSetIter.getWeight();
for (; PSetIter.isValid(); ++PSetIter) {
unsigned &Limit = PressureSetLimit[*PSetIter];
assert(
Limit >= Weight &&
"register pressure limit must be greater than or equal weight");
Limit -= Weight;
LLVM_DEBUG(dbgs() << "PSet=" << *PSetIter << " Limit=" << Limit
<< " (decreased by " << Weight << ")\n");
}
}
}
PressureSetLimit[PSet] = RCI.getRegPressureSetLimit(PSet);
}

// There are two patterns of last-use.
Expand Down
Loading