Skip to content

[RISCV] Partially move doPeepholeMaskedRVV into RISCVFoldMasks #72441

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 1 commit into from
Nov 27, 2023
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
46 changes: 46 additions & 0 deletions llvm/lib/Target/RISCV/RISCVFoldMasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "RISCV.h"
#include "RISCVSubtarget.h"
#include "RISCVISelDAGToDAG.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Alphabetize? Though I'm surprised a SelectionDAG header is needed in an MIR pass.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's needed for the masked pseudos table, which after everything else is moved over can also be moved into RISCVFoldMasks

#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
Expand Down Expand Up @@ -48,6 +49,7 @@ class RISCVFoldMasks : public MachineFunctionPass {
StringRef getPassName() const override { return "RISC-V Fold Masks"; }

private:
bool convertToUnmasked(MachineInstr &MI, MachineInstr *MaskDef);
bool convertVMergeToVMv(MachineInstr &MI, MachineInstr *MaskDef);

bool isAllOnesMask(MachineInstr *MaskDef);
Expand Down Expand Up @@ -132,6 +134,49 @@ bool RISCVFoldMasks::convertVMergeToVMv(MachineInstr &MI, MachineInstr *V0Def) {
return true;
}

bool RISCVFoldMasks::convertToUnmasked(MachineInstr &MI,
MachineInstr *MaskDef) {
const RISCV::RISCVMaskedPseudoInfo *I =
RISCV::getMaskedPseudoInfo(MI.getOpcode());
if (!I)
return false;

if (!isAllOnesMask(MaskDef))
return false;

// There are two classes of pseudos in the table - compares and
// everything else. See the comment on RISCVMaskedPseudo for details.
const unsigned Opc = I->UnmaskedPseudo;
const MCInstrDesc &MCID = TII->get(Opc);
const bool HasPolicyOp = RISCVII::hasVecPolicyOp(MCID.TSFlags);
const bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(MCID);
#ifndef NDEBUG
const MCInstrDesc &MaskedMCID = TII->get(MI.getOpcode());
assert(RISCVII::hasVecPolicyOp(MaskedMCID.TSFlags) ==
RISCVII::hasVecPolicyOp(MCID.TSFlags) &&
"Masked and unmasked pseudos are inconsistent");
assert(HasPolicyOp == HasPassthru && "Unexpected pseudo structure");
#endif

MI.setDesc(MCID);

// TODO: Increment all MaskOpIdxs in tablegen by num of explicit defs?
unsigned MaskOpIdx = I->MaskOpIdx + MI.getNumExplicitDefs();
MI.removeOperand(MaskOpIdx);

// The unmasked pseudo will no longer be constrained to the vrnov0 reg class,
// so try and relax it to vr.
MRI->recomputeRegClass(MI.getOperand(0).getReg());
unsigned PassthruOpIdx = MI.getNumExplicitDefs();
if (HasPassthru) {
if (MI.getOperand(PassthruOpIdx).getReg() != RISCV::NoRegister)
MRI->recomputeRegClass(MI.getOperand(PassthruOpIdx).getReg());
} else
MI.removeOperand(PassthruOpIdx);

return true;
}

bool RISCVFoldMasks::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;
Expand Down Expand Up @@ -159,6 +204,7 @@ bool RISCVFoldMasks::runOnMachineFunction(MachineFunction &MF) {
CurrentV0Def = nullptr;
for (MachineInstr &MI : MBB) {
unsigned BaseOpc = RISCV::getRVVMCOpcode(MI.getOpcode());
Changed |= convertToUnmasked(MI, CurrentV0Def);
if (BaseOpc == RISCV::VMERGE_VVM)
Changed |= convertVMergeToVMv(MI, CurrentV0Def);

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ void RISCVDAGToDAGISel::PostprocessISelDAG() {
continue;

MadeChange |= doPeepholeSExtW(N);

// FIXME: This is here only because the VMerge transform doesn't
// know how to handle masked true inputs. Once that has been moved
// to post-ISEL, this can be deleted as well.
MadeChange |= doPeepholeMaskedRVV(cast<MachineSDNode>(N));
Copy link
Contributor

Choose a reason for hiding this comment

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

}

Expand Down Expand Up @@ -3711,8 +3715,6 @@ bool RISCVDAGToDAGISel::performCombineVMergeAndVOps(SDNode *N) {
for (unsigned Idx = 1; Idx < True->getNumValues(); ++Idx)
ReplaceUses(True.getValue(Idx), SDValue(Result, Idx));

// Try to transform Result to unmasked intrinsic.
doPeepholeMaskedRVV(Result);
return true;
}

Expand Down