Skip to content

[AArch64][SME] Make getRegAllocationHints more specific for multi-vector loads #123081

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 7 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
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
19 changes: 7 additions & 12 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8763,17 +8763,9 @@ static bool checkZExtBool(SDValue Arg, const SelectionDAG &DAG) {
bool shouldUseFormStridedPseudo(MachineInstr &MI) {
MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();

const TargetRegisterClass *RegClass = nullptr;
switch (MI.getOpcode()) {
case AArch64::FORM_TRANSPOSED_REG_TUPLE_X2_PSEUDO:
RegClass = &AArch64::ZPR2StridedOrContiguousRegClass;
break;
case AArch64::FORM_TRANSPOSED_REG_TUPLE_X4_PSEUDO:
RegClass = &AArch64::ZPR4StridedOrContiguousRegClass;
break;
default:
llvm_unreachable("Unexpected opcode.");
}
assert((MI.getOpcode() == AArch64::FORM_TRANSPOSED_REG_TUPLE_X2_PSEUDO ||
MI.getOpcode() == AArch64::FORM_TRANSPOSED_REG_TUPLE_X4_PSEUDO) &&
"Unexpected opcode.");

MCRegister SubReg = MCRegister::NoRegister;
for (unsigned I = 1; I < MI.getNumOperands(); ++I) {
Expand All @@ -8790,8 +8782,11 @@ bool shouldUseFormStridedPseudo(MachineInstr &MI) {
SubReg = OpSubReg;

MachineOperand *CopySrcOp = MRI.getOneDef(CopySrc.getReg());
const TargetRegisterClass *CopySrcClass =
MRI.getRegClass(CopySrcOp->getReg());
if (!CopySrcOp || !CopySrcOp->isReg() || OpSubReg != SubReg ||
MRI.getRegClass(CopySrcOp->getReg()) != RegClass)
(CopySrcClass != &AArch64::ZPR2StridedOrContiguousRegClass &&
CopySrcClass != &AArch64::ZPR4StridedOrContiguousRegClass))
return false;
}

Expand Down
144 changes: 125 additions & 19 deletions llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "MCTargetDesc/AArch64InstPrinter.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/CodeGen/LiveRegMatrix.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
Expand Down Expand Up @@ -1097,7 +1098,11 @@ bool AArch64RegisterInfo::getRegAllocationHints(
Register VirtReg, ArrayRef<MCPhysReg> Order,
SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
const MachineRegisterInfo &MRI = MF.getRegInfo();

auto &ST = MF.getSubtarget<AArch64Subtarget>();
if (!ST.hasSME() || !ST.isStreaming())
return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF,
VRM);

// The SVE calling convention preserves registers Z8-Z23. As a result, there
// are no ZPR2Strided or ZPR4Strided registers that do not overlap with the
Expand All @@ -1107,26 +1112,127 @@ bool AArch64RegisterInfo::getRegAllocationHints(
// FORM_TRANSPOSED_REG_TUPLE pseudo, we want to favour reducing copy
// instructions over reducing the number of clobbered callee-save registers,
// so we add the strided registers as a hint.
const MachineRegisterInfo &MRI = MF.getRegInfo();
unsigned RegID = MRI.getRegClass(VirtReg)->getID();
// Look through uses of the register for FORM_TRANSPOSED_REG_TUPLE.
if ((RegID == AArch64::ZPR2StridedOrContiguousRegClassID ||
RegID == AArch64::ZPR4StridedOrContiguousRegClassID) &&
any_of(MRI.use_nodbg_instructions(VirtReg), [](const MachineInstr &Use) {
return Use.getOpcode() ==
AArch64::FORM_TRANSPOSED_REG_TUPLE_X2_PSEUDO ||
Use.getOpcode() == AArch64::FORM_TRANSPOSED_REG_TUPLE_X4_PSEUDO;
})) {
const TargetRegisterClass *StridedRC =
RegID == AArch64::ZPR2StridedOrContiguousRegClassID
? &AArch64::ZPR2StridedRegClass
: &AArch64::ZPR4StridedRegClass;

for (MCPhysReg Reg : Order)
if (StridedRC->contains(Reg))
Hints.push_back(Reg);
if (RegID == AArch64::ZPR2StridedOrContiguousRegClassID ||
RegID == AArch64::ZPR4StridedOrContiguousRegClassID) {

// Look through uses of the register for FORM_TRANSPOSED_REG_TUPLE.
for (const MachineInstr &Use : MRI.use_nodbg_instructions(VirtReg)) {
if (Use.getOpcode() != AArch64::FORM_TRANSPOSED_REG_TUPLE_X2_PSEUDO &&
Use.getOpcode() != AArch64::FORM_TRANSPOSED_REG_TUPLE_X4_PSEUDO)
continue;

unsigned UseOps = Use.getNumOperands() - 1;
const TargetRegisterClass *StridedRC;
switch (RegID) {
case AArch64::ZPR2StridedOrContiguousRegClassID:
StridedRC = &AArch64::ZPR2StridedRegClass;
break;
case AArch64::ZPR4StridedOrContiguousRegClassID:
StridedRC = &AArch64::ZPR4StridedRegClass;
break;
default:
llvm_unreachable("Unexpected RegID");
}

return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF,
VRM);
SmallVector<MCPhysReg, 4> StridedOrder;
for (MCPhysReg Reg : Order)
if (StridedRC->contains(Reg))
StridedOrder.push_back(Reg);

int OpIdx = Use.findRegisterUseOperandIdx(VirtReg, this);
assert(OpIdx != -1 && "Expected operand index from register use.");

unsigned TupleID = MRI.getRegClass(Use.getOperand(0).getReg())->getID();
bool IsMulZPR = TupleID == AArch64::ZPR2Mul2RegClassID ||
TupleID == AArch64::ZPR4Mul4RegClassID;

const MachineOperand *AssignedRegOp = llvm::find_if(
make_range(Use.operands_begin() + 1, Use.operands_end()),
[&VRM](const MachineOperand &Op) {
return VRM->hasPhys(Op.getReg());
});

// Example:
//
// When trying to find a suitable register allocation for VirtReg %v2 in:
//
// %v0:zpr2stridedorcontiguous = ld1 p0/z, [...]
// %v1:zpr2stridedorcontiguous = ld1 p0/z, [...]
// %v2:zpr2stridedorcontiguous = ld1 p0/z, [...]
// %v3:zpr2stridedorcontiguous = ld1 p0/z, [...]
// %v4:zpr4mul4 = FORM_TRANSPOSED_X4 %v0:0, %v1:0, %v2:0, %v3:0
//
// One such suitable allocation would be:
//
// { z0, z8 } = ld1 p0/z, [...]
// { z1, z9 } = ld1 p0/z, [...]
// { z2, z10 } = ld1 p0/z, [...]
// { z3, z11 } = ld1 p0/z, [...]
// { z0, z1, z2, z3 } =
// FORM_TRANSPOSED_X4 {z0, z8}:0, {z1, z9}:0, {z2, z10}:0, {z3, z11}:0
//
// Below we distinguish two cases when trying to find a register:
// * None of the registers used by FORM_TRANSPOSED_X4 have been assigned
// yet. In this case the code muse ensure that there are at least UseOps
// free consecutive registers. If IsMulZPR is true, then the first of
// registers must also be a multiple of UseOps, e.g. { z0, z1, z2, z3 }
// is valid but { z1, z2, z3, z5 } is not.
// * One or more of the registers used by FORM_TRANSPOSED_X4 is already
// assigned a physical register, which means only checking that a
// consectutive range of free tuple registers exists which includes
// the assigned register.
// e.g. in the example above, if { z0, z8 } is already allocated for
// %v0, we just need to ensure that { z1, z9 }, { z2, z10 } and
// { z3, z11 } are also free. If so, we add { z2, z10 }.

if (AssignedRegOp == Use.operands_end()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think an example would be useful here.

// There are no registers already assigned to any of the pseudo
// operands. Look for a valid starting register for the group.
for (unsigned I = 0; I < StridedOrder.size(); ++I) {
MCPhysReg Reg = StridedOrder[I];
SmallVector<MCPhysReg> Regs;

// If the FORM_TRANSPOSE nodes use the ZPRMul classes, the starting
// register of the first load should be a multiple of 2 or 4.
unsigned SubRegIdx = Use.getOperand(OpIdx).getSubReg();
if (IsMulZPR && (getSubReg(Reg, SubRegIdx) - AArch64::Z0) % UseOps !=
((unsigned)OpIdx - 1))
continue;

// In the example above, if VirtReg is the third operand of the
// tuple (%v2) and Reg == Z2_Z10, then we need to make sure that
// Z0_Z8, Z1_Z9 and Z3_Z11 are also available.
auto IsFreeConsecutiveReg = [&](unsigned UseOp) {
unsigned R = Reg - (OpIdx - 1) + UseOp;
return StridedRC->contains(R) &&
(UseOp == 0 ||
((getSubReg(R, AArch64::zsub0) - AArch64::Z0) ==
(getSubReg(R - 1, AArch64::zsub0) - AArch64::Z0) + 1)) &&
!Matrix->isPhysRegUsed(R);
};
if (all_of(iota_range<unsigned>(0U, UseOps, /*Inclusive=*/false),
IsFreeConsecutiveReg))
Hints.push_back(Reg);
}
} else {
// At least one operand already has a physical register assigned.
// Find the starting sub-register of this and use it to work out the
// correct strided register to suggest based on the current op index.
MCPhysReg TargetStartReg =
getSubReg(VRM->getPhys(AssignedRegOp->getReg()), AArch64::zsub0) +
(OpIdx - AssignedRegOp->getOperandNo());

for (unsigned I = 0; I < StridedOrder.size(); ++I)
if (getSubReg(StridedOrder[I], AArch64::zsub0) == TargetStartReg)
Hints.push_back(StridedOrder[I]);
}

if (!Hints.empty())
return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints,
MF, VRM);
}
}

for (MachineInstr &MI : MRI.def_instructions(VirtReg)) {
Expand Down
Loading