Skip to content

[AArch64][GlobalISel] Combine vecreduce(ext) to {U/S}ADDLV #75832

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 3 commits into from
Jan 15, 2024
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
11 changes: 10 additions & 1 deletion llvm/lib/Target/AArch64/AArch64Combine.td
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,22 @@ def ext_addv_to_udot_addv : GICombineRule<
>;
}

def ext_uaddv_to_uaddlv_matchinfo : GIDefMatchData<"std::pair<Register, bool>">;
def ext_uaddv_to_uaddlv : GICombineRule<
(defs root:$root, ext_uaddv_to_uaddlv_matchinfo:$matchinfo),
(match (wip_match_opcode G_VECREDUCE_ADD):$root,
[{ return matchExtUaddvToUaddlv(*${root}, MRI, ${matchinfo}); }]),
(apply [{ applyExtUaddvToUaddlv(*${root}, MRI, B, Observer, ${matchinfo}); }])
>;

def AArch64PreLegalizerCombiner: GICombiner<
"AArch64PreLegalizerCombinerImpl", [all_combines,
fconstant_to_constant,
icmp_redundant_trunc,
fold_global_offset,
shuffle_to_extract,
ext_addv_to_udot_addv]> {
ext_addv_to_udot_addv,
ext_uaddv_to_uaddlv]> {
let CombineAllMethodName = "tryCombineAllImpl";
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,7 @@ const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const {
MAKE_CASE(AArch64ISD::SADDV)
MAKE_CASE(AArch64ISD::UADDV)
MAKE_CASE(AArch64ISD::UADDLV)
MAKE_CASE(AArch64ISD::SADDLV)
MAKE_CASE(AArch64ISD::SDOT)
MAKE_CASE(AArch64ISD::UDOT)
MAKE_CASE(AArch64ISD::SMINV)
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ enum NodeType : unsigned {

// Unsigned sum Long across Vector
UADDLV,
SADDLV,

// Add Pairwise of two vectors
ADDP,
Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/Target/AArch64/AArch64InstrGISel.td
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ def G_SMULL : AArch64GenericInstruction {
let hasSideEffects = 0;
}

def G_UADDLV : AArch64GenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1);
let hasSideEffects = 0;
}

def G_SADDLV : AArch64GenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1);
let hasSideEffects = 0;
}

def G_UDOT : AArch64GenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1, type0:$src2, type0:$src3);
Expand Down Expand Up @@ -282,6 +294,9 @@ def : GINodeEquiv<G_BSP, AArch64bsp>;
def : GINodeEquiv<G_UMULL, AArch64umull>;
def : GINodeEquiv<G_SMULL, AArch64smull>;

def : GINodeEquiv<G_SADDLV, AArch64saddlv>;
def : GINodeEquiv<G_UADDLV, AArch64uaddlv>;

def : GINodeEquiv<G_UDOT, AArch64udot>;
def : GINodeEquiv<G_SDOT, AArch64sdot>;

Expand Down
25 changes: 17 additions & 8 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ def AArch64uminv : SDNode<"AArch64ISD::UMINV", SDT_AArch64UnaryVec>;
def AArch64smaxv : SDNode<"AArch64ISD::SMAXV", SDT_AArch64UnaryVec>;
def AArch64umaxv : SDNode<"AArch64ISD::UMAXV", SDT_AArch64UnaryVec>;
def AArch64uaddlv : SDNode<"AArch64ISD::UADDLV", SDT_AArch64uaddlp>;
def AArch64saddlv : SDNode<"AArch64ISD::SADDLV", SDT_AArch64uaddlp>;

def AArch64uabd : PatFrags<(ops node:$lhs, node:$rhs),
[(abdu node:$lhs, node:$rhs),
Expand Down Expand Up @@ -6680,17 +6681,25 @@ def : Pat<(v4i32 (AArch64uaddlv (v8i16 (AArch64uaddlp (v16i8 V128:$op))))),
def : Pat<(v4i32 (AArch64uaddlv (v4i16 (AArch64uaddlp (v8i8 V64:$op))))),
(v4i32 (SUBREG_TO_REG (i64 0), (UADDLVv8i8v V64:$op), hsub))>;

def : Pat<(v4i32 (AArch64uaddlv (v8i8 V64:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (UADDLVv8i8v V64:$Rn), hsub))>;
multiclass SIMDAcrossLaneLongReductionIntrinsic<string Opc, SDPatternOperator addlv> {
def : Pat<(v4i32 (addlv (v8i8 V64:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (!cast<Instruction>(Opc#"v8i8v") V64:$Rn), hsub))>;

def : Pat<(v4i32 (AArch64uaddlv (v4i16 V64:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (UADDLVv4i16v V64:$Rn), ssub))>;
def : Pat<(v4i32 (addlv (v4i16 V64:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (!cast<Instruction>(Opc#"v4i16v") V64:$Rn), ssub))>;

def : Pat<(v4i32 (AArch64uaddlv (v16i8 V128:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (UADDLVv16i8v V128:$Rn), hsub))>;
def : Pat<(v4i32 (addlv (v16i8 V128:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (!cast<Instruction>(Opc#"v16i8v") V128:$Rn), hsub))>;

def : Pat<(v4i32 (AArch64uaddlv (v8i16 V128:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (UADDLVv8i16v V128:$Rn), ssub))>;
def : Pat<(v4i32 (addlv (v8i16 V128:$Rn))),
(v4i32 (SUBREG_TO_REG (i64 0), (!cast<Instruction>(Opc#"v8i16v") V128:$Rn), ssub))>;

def : Pat<(v2i64 (addlv (v4i32 V128:$Rn))),
(v2i64 (SUBREG_TO_REG (i64 0), (!cast<Instruction>(Opc#"v4i32v") V128:$Rn), dsub))>;
}

defm : SIMDAcrossLaneLongReductionIntrinsic<"UADDLV", AArch64uaddlv>;
defm : SIMDAcrossLaneLongReductionIntrinsic<"SADDLV", AArch64saddlv>;

// Patterns for across-vector intrinsics, that have a node equivalent, that
// returns a vector (with only the low lane defined) instead of a scalar.
Expand Down
144 changes: 144 additions & 0 deletions llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,150 @@ void applyExtAddvToUdotAddv(MachineInstr &MI, MachineRegisterInfo &MRI,
MI.eraseFromParent();
}

// Matches {U/S}ADDV(ext(x)) => {U/S}ADDLV(x)
// Ensure that the type coming from the extend instruction is the right size
bool matchExtUaddvToUaddlv(MachineInstr &MI, MachineRegisterInfo &MRI,
std::pair<Register, bool> &MatchInfo) {
assert(MI.getOpcode() == TargetOpcode::G_VECREDUCE_ADD &&
"Expected G_VECREDUCE_ADD Opcode");

// Check if the last instruction is an extend
MachineInstr *ExtMI = getDefIgnoringCopies(MI.getOperand(1).getReg(), MRI);
auto ExtOpc = ExtMI->getOpcode();

if (ExtOpc == TargetOpcode::G_ZEXT)
std::get<1>(MatchInfo) = 0;
else if (ExtOpc == TargetOpcode::G_SEXT)
std::get<1>(MatchInfo) = 1;
else
return false;

// Check if the source register is a valid type
Register ExtSrcReg = ExtMI->getOperand(1).getReg();
LLT ExtSrcTy = MRI.getType(ExtSrcReg);
LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
if ((DstTy.getScalarSizeInBits() == 16 &&
ExtSrcTy.getNumElements() % 8 == 0 && ExtSrcTy.getNumElements() < 256) ||
(DstTy.getScalarSizeInBits() == 32 &&
ExtSrcTy.getNumElements() % 4 == 0) ||
(DstTy.getScalarSizeInBits() == 64 &&
ExtSrcTy.getNumElements() % 4 == 0)) {
std::get<0>(MatchInfo) = ExtSrcReg;
return true;
}
return false;
}

void applyExtUaddvToUaddlv(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &B, GISelChangeObserver &Observer,
std::pair<Register, bool> &MatchInfo) {
assert(MI.getOpcode() == TargetOpcode::G_VECREDUCE_ADD &&
"Expected G_VECREDUCE_ADD Opcode");

unsigned Opc = std::get<1>(MatchInfo) ? AArch64::G_SADDLV : AArch64::G_UADDLV;
Register SrcReg = std::get<0>(MatchInfo);
Register DstReg = MI.getOperand(0).getReg();
LLT SrcTy = MRI.getType(SrcReg);
LLT DstTy = MRI.getType(DstReg);

// If SrcTy has more elements than expected, split them into multiple
// insructions and sum the results
LLT MainTy;
SmallVector<Register, 1> WorkingRegisters;
unsigned SrcScalSize = SrcTy.getScalarSizeInBits();
unsigned SrcNumElem = SrcTy.getNumElements();
if ((SrcScalSize == 8 && SrcNumElem > 16) ||
(SrcScalSize == 16 && SrcNumElem > 8) ||
(SrcScalSize == 32 && SrcNumElem > 4)) {

LLT LeftoverTy;
SmallVector<Register, 4> LeftoverRegs;
if (SrcScalSize == 8)
MainTy = LLT::fixed_vector(16, 8);
else if (SrcScalSize == 16)
MainTy = LLT::fixed_vector(8, 16);
else if (SrcScalSize == 32)
MainTy = LLT::fixed_vector(4, 32);
else
llvm_unreachable("Source's Scalar Size not supported");

// Extract the parts and put each extracted sources through U/SADDLV and put
// the values inside a small vec
extractParts(SrcReg, SrcTy, MainTy, LeftoverTy, WorkingRegisters,
LeftoverRegs, B, MRI);
for (unsigned I = 0; I < LeftoverRegs.size(); I++) {
WorkingRegisters.push_back(LeftoverRegs[I]);
}
} else {
WorkingRegisters.push_back(SrcReg);
MainTy = SrcTy;
}

unsigned MidScalarSize = MainTy.getScalarSizeInBits() * 2;
LLT MidScalarLLT = LLT::scalar(MidScalarSize);
Register zeroReg = B.buildConstant(LLT::scalar(64), 0).getReg(0);
for (unsigned I = 0; I < WorkingRegisters.size(); I++) {
// If the number of elements is too small to build an instruction, extend
// its size before applying addlv
LLT WorkingRegTy = MRI.getType(WorkingRegisters[I]);
if ((WorkingRegTy.getScalarSizeInBits() == 8) &&
(WorkingRegTy.getNumElements() == 4)) {
WorkingRegisters[I] =
B.buildInstr(std::get<1>(MatchInfo) ? TargetOpcode::G_SEXT
: TargetOpcode::G_ZEXT,
{LLT::fixed_vector(4, 16)}, {WorkingRegisters[I]})
.getReg(0);
}

// Generate the {U/S}ADDLV instruction, whose output is always double of the
// Src's Scalar size
LLT addlvTy = MidScalarSize <= 32 ? LLT::fixed_vector(4, 32)
: LLT::fixed_vector(2, 64);
Register addlvReg =
B.buildInstr(Opc, {addlvTy}, {WorkingRegisters[I]}).getReg(0);

// The output from {U/S}ADDLV gets placed in the lowest lane of a v4i32 or
// v2i64 register.
// i16, i32 results uses v4i32 registers
// i64 results uses v2i64 registers
// Therefore we have to extract/truncate the the value to the right type
if (MidScalarSize == 32 || MidScalarSize == 64) {
WorkingRegisters[I] = B.buildInstr(AArch64::G_EXTRACT_VECTOR_ELT,
{MidScalarLLT}, {addlvReg, zeroReg})
.getReg(0);
} else {
Register extractReg = B.buildInstr(AArch64::G_EXTRACT_VECTOR_ELT,
{LLT::scalar(32)}, {addlvReg, zeroReg})
.getReg(0);
WorkingRegisters[I] =
B.buildTrunc({MidScalarLLT}, {extractReg}).getReg(0);
}
}

Register outReg;
if (WorkingRegisters.size() > 1) {
outReg = B.buildAdd(MidScalarLLT, WorkingRegisters[0], WorkingRegisters[1])
.getReg(0);
for (unsigned I = 2; I < WorkingRegisters.size(); I++) {
outReg = B.buildAdd(MidScalarLLT, outReg, WorkingRegisters[I]).getReg(0);
}
} else {
outReg = WorkingRegisters[0];
}

if (DstTy.getScalarSizeInBits() > MidScalarSize) {
// Handle the scalar value if the DstTy's Scalar Size is more than double
// Src's ScalarType
B.buildInstr(std::get<1>(MatchInfo) ? TargetOpcode::G_SEXT
: TargetOpcode::G_ZEXT,
{DstReg}, {outReg});
} else {
B.buildCopy(DstReg, outReg);
}

MI.eraseFromParent();
}

bool tryToSimplifyUADDO(MachineInstr &MI, MachineIRBuilder &B,
CombinerHelper &Helper, GISelChangeObserver &Observer) {
// Try simplify G_UADDO with 8 or 16 bit operands to wide G_ADD and TBNZ if
Expand Down
Loading