Skip to content

Commit b510e4c

Browse files
committed
[RISCV] Add a vsetvli insert pass that can be extended to be aware of incoming VL/VTYPE from other basic blocks.
This is a replacement for D101938 for inserting vsetvli instructions where needed. This new version changes how we track the information in such a way that we can extend it to be aware of VL/VTYPE changes in other blocks. Given how much it changes the previous patch, I've decided to abandon the previous patch and post this from scratch. For now the pass consists of a single phase that assumes the incoming state from other basic blocks is unknown. A follow up patch will extend this with a phase to collect information about how VL/VTYPE change in each block and a second phase to propagate this information to the entire function. This will be used by a third phase to do the vsetvli insertion. Reviewed By: frasercrmck Differential Revision: https://reviews.llvm.org/D102737
1 parent b2c70bd commit b510e4c

27 files changed

+3463
-3370
lines changed

llvm/lib/Target/RISCV/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ add_public_tablegen_target(RISCVCommonTableGen)
2121
add_llvm_target(RISCVCodeGen
2222
RISCVAsmPrinter.cpp
2323
RISCVCallLowering.cpp
24-
RISCVCleanupVSETVLI.cpp
2524
RISCVExpandAtomicPseudoInsts.cpp
2625
RISCVExpandPseudoInsts.cpp
2726
RISCVFrameLowering.cpp
27+
RISCVInsertVSETVLI.cpp
2828
RISCVInstrInfo.cpp
2929
RISCVInstructionSelector.cpp
3030
RISCVISelDAGToDAG.cpp

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ enum VConstraintType {
8686
VMConstraint = 0b100,
8787
};
8888

89-
enum VLMUL {
89+
enum VLMUL : uint8_t {
9090
LMUL_1 = 0,
9191
LMUL_2,
9292
LMUL_4,

llvm/lib/Target/RISCV/RISCV.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ void initializeRISCVExpandPseudoPass(PassRegistry &);
4646
FunctionPass *createRISCVExpandAtomicPseudoPass();
4747
void initializeRISCVExpandAtomicPseudoPass(PassRegistry &);
4848

49-
FunctionPass *createRISCVCleanupVSETVLIPass();
50-
void initializeRISCVCleanupVSETVLIPass(PassRegistry &);
49+
FunctionPass *createRISCVInsertVSETVLIPass();
50+
void initializeRISCVInsertVSETVLIPass(PassRegistry &);
5151

5252
InstructionSelector *createRISCVInstructionSelector(const RISCVTargetMachine &,
5353
RISCVSubtarget &,

llvm/lib/Target/RISCV/RISCVCleanupVSETVLI.cpp

Lines changed: 0 additions & 163 deletions
This file was deleted.

llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ bool RISCVExpandPseudo::expandLoadTLSGDAddress(
240240

241241
bool RISCVExpandPseudo::expandVSetVL(MachineBasicBlock &MBB,
242242
MachineBasicBlock::iterator MBBI) {
243-
assert(MBBI->getNumOperands() == 5 && "Unexpected instruction format");
243+
assert(MBBI->getNumExplicitOperands() == 3 && MBBI->getNumOperands() >= 5 &&
244+
"Unexpected instruction format");
244245

245246
DebugLoc DL = MBBI->getDebugLoc();
246247

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6463,107 +6463,9 @@ static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
64636463
return TailMBB;
64646464
}
64656465

6466-
static MachineInstr *elideCopies(MachineInstr *MI,
6467-
const MachineRegisterInfo &MRI) {
6468-
while (true) {
6469-
if (!MI->isFullCopy())
6470-
return MI;
6471-
if (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
6472-
return nullptr;
6473-
MI = MRI.getVRegDef(MI->getOperand(1).getReg());
6474-
if (!MI)
6475-
return nullptr;
6476-
}
6477-
}
6478-
6479-
static MachineBasicBlock *addVSetVL(MachineInstr &MI, MachineBasicBlock *BB,
6480-
int VLIndex, unsigned SEWIndex,
6481-
RISCVII::VLMUL VLMul,
6482-
bool ForceTailAgnostic) {
6483-
MachineFunction &MF = *BB->getParent();
6484-
DebugLoc DL = MI.getDebugLoc();
6485-
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
6486-
6487-
unsigned Log2SEW = MI.getOperand(SEWIndex).getImm();
6488-
unsigned SEW = 1 << Log2SEW;
6489-
assert(RISCVVType::isValidSEW(SEW) && "Unexpected SEW");
6490-
6491-
MachineRegisterInfo &MRI = MF.getRegInfo();
6492-
6493-
auto BuildVSETVLI = [&]() {
6494-
if (VLIndex >= 0) {
6495-
Register DestReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
6496-
const MachineOperand &VLOp = MI.getOperand(VLIndex);
6497-
6498-
// VL can be a register or an immediate.
6499-
if (VLOp.isImm())
6500-
return BuildMI(*BB, MI, DL, TII.get(RISCV::PseudoVSETIVLI))
6501-
.addReg(DestReg, RegState::Define | RegState::Dead)
6502-
.addImm(VLOp.getImm());
6503-
6504-
Register VLReg = MI.getOperand(VLIndex).getReg();
6505-
return BuildMI(*BB, MI, DL, TII.get(RISCV::PseudoVSETVLI))
6506-
.addReg(DestReg, RegState::Define | RegState::Dead)
6507-
.addReg(VLReg);
6508-
}
6509-
6510-
// With no VL operator in the pseudo, do not modify VL (rd = X0, rs1 = X0).
6511-
return BuildMI(*BB, MI, DL, TII.get(RISCV::PseudoVSETVLI))
6512-
.addReg(RISCV::X0, RegState::Define | RegState::Dead)
6513-
.addReg(RISCV::X0, RegState::Kill);
6514-
};
6515-
6516-
MachineInstrBuilder MIB = BuildVSETVLI();
6517-
6518-
// Default to tail agnostic unless the destination is tied to a source. In
6519-
// that case the user would have some control over the tail values. The tail
6520-
// policy is also ignored on instructions that only update element 0 like
6521-
// vmv.s.x or reductions so use agnostic there to match the common case.
6522-
// FIXME: This is conservatively correct, but we might want to detect that
6523-
// the input is undefined.
6524-
bool TailAgnostic = true;
6525-
unsigned UseOpIdx;
6526-
if (!ForceTailAgnostic && MI.isRegTiedToUseOperand(0, &UseOpIdx)) {
6527-
TailAgnostic = false;
6528-
// If the tied operand is an IMPLICIT_DEF we can keep TailAgnostic.
6529-
const MachineOperand &UseMO = MI.getOperand(UseOpIdx);
6530-
MachineInstr *UseMI = MRI.getVRegDef(UseMO.getReg());
6531-
if (UseMI) {
6532-
UseMI = elideCopies(UseMI, MRI);
6533-
if (UseMI && UseMI->isImplicitDef())
6534-
TailAgnostic = true;
6535-
}
6536-
}
6537-
6538-
// For simplicity we reuse the vtype representation here.
6539-
MIB.addImm(RISCVVType::encodeVTYPE(VLMul, SEW,
6540-
/*TailAgnostic*/ TailAgnostic,
6541-
/*MaskAgnostic*/ false));
6542-
6543-
// Remove (now) redundant operands from pseudo
6544-
if (VLIndex >= 0 && MI.getOperand(VLIndex).isReg()) {
6545-
MI.getOperand(VLIndex).setReg(RISCV::NoRegister);
6546-
MI.getOperand(VLIndex).setIsKill(false);
6547-
}
6548-
6549-
return BB;
6550-
}
6551-
65526466
MachineBasicBlock *
65536467
RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
65546468
MachineBasicBlock *BB) const {
6555-
uint64_t TSFlags = MI.getDesc().TSFlags;
6556-
6557-
if (RISCVII::hasSEWOp(TSFlags)) {
6558-
unsigned NumOperands = MI.getNumExplicitOperands();
6559-
int VLIndex = RISCVII::hasVLOp(TSFlags) ? NumOperands - 2 : -1;
6560-
unsigned SEWIndex = NumOperands - 1;
6561-
bool ForceTailAgnostic = RISCVII::doesForceTailAgnostic(TSFlags);
6562-
6563-
RISCVII::VLMUL VLMul = RISCVII::getLMul(TSFlags);
6564-
return addVSetVL(MI, BB, VLIndex, SEWIndex, VLMul, ForceTailAgnostic);
6565-
}
6566-
65676469
switch (MI.getOpcode()) {
65686470
default:
65696471
llvm_unreachable("Unexpected instr type to insert");

0 commit comments

Comments
 (0)